@@ -14,7 +14,9 @@ pub trait RccExt {
14
14
impl RccExt for RCC {
15
15
fn constrain ( self ) -> Rcc {
16
16
Rcc {
17
- ahb1 : AHB1 ( ( ) ) ,
17
+ ahb1 : AHB1 { _0 : ( ) } ,
18
+ ahb2 : AHB2 { _0 : ( ) } ,
19
+ ahb3 : AHB3 { _0 : ( ) } ,
18
20
apb1 : APB1 { _0 : ( ) } ,
19
21
apb2 : APB2 { _0 : ( ) } ,
20
22
cfgr : CFGR {
@@ -34,6 +36,10 @@ impl RccExt for RCC {
34
36
pub struct Rcc {
35
37
/// Advanced High-Performance Bus 1 (AHB1) registers
36
38
pub ahb1 : AHB1 ,
39
+ /// Advanced High-Performance Bus 1 (AHB1) registers
40
+ pub ahb2 : AHB2 ,
41
+ /// Advanced High-Performance Bus 1 (AHB1) registers
42
+ pub ahb3 : AHB3 ,
37
43
38
44
/// Advanced Peripheral Bus 1 (APB1) registers
39
45
pub apb1 : APB1 ,
@@ -42,21 +48,6 @@ pub struct Rcc {
42
48
pub cfgr : CFGR ,
43
49
}
44
50
45
- /// Advanced High-Performance Bus 1 (AHB1) registers
46
- pub struct AHB1 ( ( ) ) ;
47
-
48
- impl AHB1 {
49
- pub fn enr ( & mut self ) -> & rcc:: AHB1ENR {
50
- // NOTE(unsafe) this proxy grants exclusive access to this register
51
- unsafe { & ( * RCC :: ptr ( ) ) . ahb1enr }
52
- }
53
-
54
- pub fn rstr ( & mut self ) -> & rcc:: AHB1RSTR {
55
- // NOTE(unsafe) this proxy grants exclusive access to this register
56
- unsafe { & ( * RCC :: ptr ( ) ) . ahb1rstr }
57
- }
58
- }
59
-
60
51
/// Advanced Peripheral Bus 1 (APB1) registers
61
52
pub struct APB1 {
62
53
_0 : ( ) ,
@@ -91,6 +82,58 @@ impl APB2 {
91
82
}
92
83
}
93
84
85
+ /// Advanced High-performance Bus 1 (AHB1) registers
86
+ pub struct AHB1 {
87
+ _0 : ( ) ,
88
+ }
89
+
90
+ impl AHB1 {
91
+ pub ( crate ) fn enr ( & mut self ) -> & rcc:: AHB1ENR {
92
+ // NOTE(unsafe) this proxy grants exclusive access to this register
93
+ unsafe { & ( * RCC :: ptr ( ) ) . ahb1enr }
94
+ }
95
+
96
+ pub ( crate ) fn rstr ( & mut self ) -> & rcc:: AHB1RSTR {
97
+ // NOTE(unsafe) this proxy grants exclusive access to this register
98
+ unsafe { & ( * RCC :: ptr ( ) ) . ahb1rstr }
99
+ }
100
+ }
101
+
102
+ /// Advanced High-performance Bus 1 (AHB1) registers
103
+ pub struct AHB2 {
104
+ _0 : ( ) ,
105
+ }
106
+
107
+ impl AHB2 {
108
+ pub ( crate ) fn enr ( & mut self ) -> & rcc:: AHB2ENR {
109
+ // NOTE(unsafe) this proxy grants exclusive access to this register
110
+ unsafe { & ( * RCC :: ptr ( ) ) . ahb2enr }
111
+ }
112
+
113
+ pub ( crate ) fn rstr ( & mut self ) -> & rcc:: AHB2RSTR {
114
+ // NOTE(unsafe) this proxy grants exclusive access to this register
115
+ unsafe { & ( * RCC :: ptr ( ) ) . ahb2rstr }
116
+ }
117
+ }
118
+
119
+ /// Advanced High-performance Bus 1 (AHB1) registers
120
+ pub struct AHB3 {
121
+ _0 : ( ) ,
122
+ }
123
+
124
+ impl AHB3 {
125
+ pub ( crate ) fn enr ( & mut self ) -> & rcc:: AHB3ENR {
126
+ // NOTE(unsafe) this proxy grants exclusive access to this register
127
+ unsafe { & ( * RCC :: ptr ( ) ) . ahb3enr }
128
+ }
129
+
130
+ pub ( crate ) fn rstr ( & mut self ) -> & rcc:: AHB3RSTR {
131
+ // NOTE(unsafe) this proxy grants exclusive access to this register
132
+ unsafe { & ( * RCC :: ptr ( ) ) . ahb3rstr }
133
+ }
134
+ }
135
+
136
+
94
137
95
138
/// HSE Clock modes
96
139
/// * `Oscillator`: Use of an external crystal/ceramic resonator
@@ -490,3 +533,149 @@ impl Clocks {
490
533
self . timclk2
491
534
}
492
535
}
536
+
537
+ pub trait GetBusFreq {
538
+ fn get_frequency ( clocks : & Clocks ) -> Hertz ;
539
+ fn get_timer_frequency ( clocks : & Clocks ) -> Hertz {
540
+ Self :: get_frequency ( clocks)
541
+ }
542
+ }
543
+
544
+ impl GetBusFreq for AHB1 {
545
+ fn get_frequency ( clocks : & Clocks ) -> Hertz {
546
+ clocks. hclk
547
+ }
548
+ }
549
+
550
+ impl GetBusFreq for AHB2 {
551
+ fn get_frequency ( clocks : & Clocks ) -> Hertz {
552
+ clocks. hclk
553
+ }
554
+ }
555
+
556
+ impl GetBusFreq for AHB3 {
557
+ fn get_frequency ( clocks : & Clocks ) -> Hertz {
558
+ clocks. hclk
559
+ }
560
+ }
561
+
562
+ impl GetBusFreq for APB1 {
563
+ fn get_frequency ( clocks : & Clocks ) -> Hertz {
564
+ clocks. pclk1
565
+ }
566
+ fn get_timer_frequency ( clocks : & Clocks ) -> Hertz {
567
+ clocks. timclk1 ( )
568
+ }
569
+ }
570
+
571
+ impl GetBusFreq for APB2 {
572
+ fn get_frequency ( clocks : & Clocks ) -> Hertz {
573
+ clocks. pclk2
574
+ }
575
+ fn get_timer_frequency ( clocks : & Clocks ) -> Hertz {
576
+ clocks. timclk2 ( )
577
+ }
578
+ }
579
+
580
+ pub ( crate ) mod sealed {
581
+ /// Bus associated to peripheral
582
+ pub trait RccBus {
583
+ /// Bus type;
584
+ type Bus ;
585
+ }
586
+ }
587
+ use sealed:: RccBus ;
588
+
589
+ /// Enable/disable peripheral
590
+ pub trait Enable : RccBus {
591
+ fn enable ( apb : & mut Self :: Bus ) ;
592
+ fn disable ( apb : & mut Self :: Bus ) ;
593
+ }
594
+
595
+ /// Reset peripheral
596
+ pub trait Reset : RccBus {
597
+ fn reset ( apb : & mut Self :: Bus ) ;
598
+ }
599
+
600
+ macro_rules! bus {
601
+ ( $( $PER: ident => ( $apbX: ty, $peren: ident, $perrst: ident) , ) +) => {
602
+ $(
603
+ impl RccBus for crate :: device:: $PER {
604
+ type Bus = $apbX;
605
+ }
606
+ impl Enable for crate :: device:: $PER {
607
+ #[ inline( always) ]
608
+ fn enable( apb: & mut Self :: Bus ) {
609
+ apb. enr( ) . modify( |_, w| w. $peren( ) . set_bit( ) ) ;
610
+ }
611
+ #[ inline( always) ]
612
+ fn disable( apb: & mut Self :: Bus ) {
613
+ apb. enr( ) . modify( |_, w| w. $peren( ) . clear_bit( ) ) ;
614
+ }
615
+ }
616
+ impl Reset for crate :: device:: $PER {
617
+ #[ inline( always) ]
618
+ fn reset( apb: & mut Self :: Bus ) {
619
+ apb. rstr( ) . modify( |_, w| w. $perrst( ) . set_bit( ) ) ;
620
+ apb. rstr( ) . modify( |_, w| w. $perrst( ) . clear_bit( ) ) ;
621
+ }
622
+ }
623
+ ) +
624
+ }
625
+ }
626
+
627
+ // Peripherals respective buses
628
+ // TODO: check which processor has which peripheral and add them
629
+ bus ! {
630
+ I2C1 => ( APB1 , i2c1en, i2c1rst) ,
631
+ I2C2 => ( APB1 , i2c2en, i2c2rst) ,
632
+ I2C3 => ( APB1 , i2c3en, i2c3rst) ,
633
+ I2C4 => ( APB1 , i2c4en, i2c4rst) ,
634
+
635
+ SPI1 => ( APB2 , spi1en, spi1rst) ,
636
+ SPI2 => ( APB1 , spi2en, spi2rst) ,
637
+ SPI3 => ( APB1 , spi3en, spi3rst) ,
638
+
639
+ USART1 => ( APB2 , usart1en, usart1rst) ,
640
+ USART2 => ( APB1 , usart2en, uart2rst) ,
641
+ USART3 => ( APB1 , usart3en, uart3rst) ,
642
+ UART4 => ( APB1 , uart4en, uart4rst) ,
643
+ UART5 => ( APB1 , uart5en, uart5rst) ,
644
+ USART6 => ( APB2 , usart6en, usart6rst) ,
645
+ UART7 => ( APB1 , uart7en, uart7rst) ,
646
+ UART8 => ( APB1 , uart8en, uart8rst) ,
647
+
648
+ WWDG => ( APB1 , wwdgen, wwdgrst) ,
649
+
650
+ DMA1 => ( AHB1 , dma1en, dma1rst) ,
651
+ DMA2 => ( AHB1 , dma2en, dma2rst) ,
652
+
653
+ DMA2D => ( AHB1 , dma2den, dma2drst) ,
654
+
655
+ GPIOA => ( AHB1 , gpioaen, gpioarst) ,
656
+ GPIOB => ( AHB1 , gpioben, gpiobrst) ,
657
+ GPIOC => ( AHB1 , gpiocen, gpiocrst) ,
658
+ GPIOD => ( AHB1 , gpioden, gpiodrst) ,
659
+ GPIOE => ( AHB1 , gpioeen, gpioerst) ,
660
+ GPIOF => ( AHB1 , gpiofen, gpiofrst) ,
661
+ GPIOG => ( AHB1 , gpiogen, gpiogrst) ,
662
+ GPIOH => ( AHB1 , gpiohen, gpiohrst) ,
663
+ GPIOI => ( AHB1 , gpioien, gpioirst) ,
664
+ GPIOJ => ( AHB1 , gpiojen, gpiojrst) ,
665
+ GPIOK => ( AHB1 , gpioken, gpiokrst) ,
666
+
667
+ TIM1 => ( APB2 , tim1en, tim1rst) ,
668
+ TIM2 => ( APB1 , tim2en, tim2rst) ,
669
+ TIM3 => ( APB1 , tim3en, tim3rst) ,
670
+ TIM4 => ( APB1 , tim4en, tim4rst) ,
671
+ TIM5 => ( APB1 , tim5en, tim5rst) ,
672
+ TIM6 => ( APB1 , tim6en, tim6rst) ,
673
+ TIM7 => ( APB1 , tim7en, tim7rst) ,
674
+ TIM8 => ( APB2 , tim8en, tim8rst) ,
675
+ TIM9 => ( APB2 , tim9en, tim9rst) ,
676
+ TIM10 => ( APB2 , tim10en, tim10rst) ,
677
+ TIM11 => ( APB2 , tim11en, tim11rst) ,
678
+ TIM12 => ( APB1 , tim12en, tim12rst) ,
679
+ TIM13 => ( APB1 , tim13en, tim13rst) ,
680
+ TIM14 => ( APB1 , tim14en, tim14rst) ,
681
+ }
0 commit comments