Skip to content

Commit a8f0c79

Browse files
committed
use bit-banding for rcc
1 parent 5e7f38e commit a8f0c79

File tree

1 file changed

+106
-93
lines changed

1 file changed

+106
-93
lines changed

src/rcc/enable.rs

Lines changed: 106 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,243 +1,256 @@
11
use super::*;
2+
use crate::bb;
23

34
macro_rules! bus_enable {
4-
($PER:ident => ($busenr:ident, $peren:ident)) => {
5+
($PER:ident => ($busenr:ident, $enbit:literal)) => {
56
impl Enable for crate::pac::$PER {
67
#[inline(always)]
78
fn enable(rcc: &RccRB) {
8-
rcc.$busenr.modify(|_, w| w.$peren().set_bit());
9+
unsafe {
10+
bb::set(&rcc.$busenr, $enbit);
11+
}
912
// Stall the pipeline to work around erratum 2.1.13 (DM00037591)
1013
cortex_m::asm::dsb();
1114
}
1215
#[inline(always)]
1316
fn disable(rcc: &RccRB) {
14-
rcc.$busenr.modify(|_, w| w.$peren().clear_bit());
17+
unsafe {
18+
bb::clear(&rcc.$busenr, $enbit);
19+
}
1520
}
1621
}
1722
};
1823
}
1924
macro_rules! bus_lpenable {
20-
($PER:ident => ($buslpenr:ident, $perlpen:ident)) => {
25+
($PER:ident => ($buslpenr:ident, $lpenbit:literal)) => {
2126
impl LPEnable for crate::pac::$PER {
2227
#[inline(always)]
2328
fn low_power_enable(rcc: &RccRB) {
24-
rcc.$buslpenr.modify(|_, w| w.$perlpen().set_bit());
29+
unsafe {
30+
bb::set(&rcc.$buslpenr, $lpenbit);
31+
}
2532
// Stall the pipeline to work around erratum 2.1.13 (DM00037591)
2633
cortex_m::asm::dsb();
2734
}
2835
#[inline(always)]
2936
fn low_power_disable(rcc: &RccRB) {
30-
rcc.$buslpenr.modify(|_, w| w.$perlpen().clear_bit());
37+
unsafe {
38+
bb::clear(&rcc.$buslpenr, $lpenbit);
39+
}
3140
}
3241
}
3342
};
3443
}
3544
macro_rules! bus_reset {
36-
($PER:ident => ($busrstr:ident, $perrst:ident)) => {
45+
($PER:ident => ($busrstr:ident, $resetbit:literal)) => {
3746
impl Reset for crate::pac::$PER {
3847
#[inline(always)]
3948
fn reset(rcc: &RccRB) {
40-
rcc.$busrstr.modify(|_, w| w.$perrst().set_bit());
41-
rcc.$busrstr.modify(|_, w| w.$perrst().clear_bit());
49+
unsafe {
50+
bb::set(&rcc.$busrstr, $resetbit);
51+
bb::clear(&rcc.$busrstr, $resetbit);
52+
}
4253
}
4354
}
4455
};
4556
}
4657

4758
macro_rules! bus {
48-
($($PER:ident => ($busenr:ident, $peren:ident, $buslpenr:ident, $perlpen:ident, $busrstr:ident, $perrst:ident),)+) => {
59+
($($PER:ident => ($busenr:ident, $buslpenr:ident, $busrstr:ident, $bit:literal),)+) => {
4960
$(
5061
impl private::Sealed for crate::pac::$PER {}
51-
bus_enable!($PER => ($busenr, $peren));
52-
bus_lpenable!($PER => ($buslpenr, $perlpen));
53-
bus_reset!($PER => ($busrstr, $perrst));
62+
bus_enable!($PER => ($busenr, $bit));
63+
bus_lpenable!($PER => ($buslpenr, $bit));
64+
bus_reset!($PER => ($busrstr, $bit));
5465
)+
5566
}
5667
}
5768

5869
bus! {
59-
CRC => (ahb1enr, crcen, ahb1lpenr, crclpen, ahb1rstr, crcrst),
60-
DMA1 => (ahb1enr, dma1en, ahb1lpenr, dma1lpen, ahb1rstr, dma1rst),
61-
DMA2 => (ahb1enr, dma2en, ahb1lpenr, dma2lpen, ahb1rstr, dma2rst),
70+
CRC => (ahb1enr, ahb1lpenr, ahb1rstr, 12),
71+
DMA1 => (ahb1enr, ahb1lpenr, ahb1rstr, 21),
72+
DMA2 => (ahb1enr, ahb1lpenr, ahb1rstr, 22),
6273
}
6374

6475
bus! {
65-
GPIOA => (ahb1enr, gpioaen, ahb1lpenr, gpioalpen, ahb1rstr, gpioarst),
66-
GPIOB => (ahb1enr, gpioben, ahb1lpenr, gpioblpen, ahb1rstr, gpiobrst),
67-
GPIOC => (ahb1enr, gpiocen, ahb1lpenr, gpioclpen, ahb1rstr, gpiocrst),
68-
GPIOH => (ahb1enr, gpiohen, ahb1lpenr, gpiohlpen, ahb1rstr, gpiohrst),
76+
GPIOA => (ahb1enr, ahb1lpenr, ahb1rstr, 0),
77+
GPIOB => (ahb1enr, ahb1lpenr, ahb1rstr, 1),
78+
GPIOC => (ahb1enr, ahb1lpenr, ahb1rstr, 2),
79+
GPIOH => (ahb1enr, ahb1lpenr, ahb1rstr, 7),
6980
}
7081

7182
#[cfg(any(feature = "gpiod", feature = "gpioe"))]
7283
bus! {
73-
GPIOD => (ahb1enr, gpioden, ahb1lpenr, gpiodlpen, ahb1rstr, gpiodrst),
74-
GPIOE => (ahb1enr, gpioeen, ahb1lpenr, gpioelpen, ahb1rstr, gpioerst),
84+
GPIOD => (ahb1enr, ahb1lpenr, ahb1rstr, 3),
85+
GPIOE => (ahb1enr, ahb1lpenr, ahb1rstr, 4),
7586
}
7687
#[cfg(any(feature = "gpiof", feature = "gpiog"))]
7788
bus! {
78-
GPIOF => (ahb1enr, gpiofen, ahb1lpenr, gpioflpen, ahb1rstr, gpiofrst),
79-
GPIOG => (ahb1enr, gpiogen, ahb1lpenr, gpioglpen, ahb1rstr, gpiogrst),
89+
GPIOF => (ahb1enr, ahb1lpenr, ahb1rstr, 5),
90+
GPIOG => (ahb1enr, ahb1lpenr, ahb1rstr, 6),
8091
}
8192

8293
#[cfg(feature = "gpioi")]
8394
bus! {
84-
GPIOI => (ahb1enr, gpioien, ahb1lpenr, gpioilpen, ahb1rstr, gpioirst),
95+
GPIOI => (ahb1enr, ahb1lpenr, ahb1rstr, 8),
8596
}
8697

8798
#[cfg(any(feature = "gpioj", feature = "gpiok"))]
8899
bus! {
89-
GPIOJ => (ahb1enr, gpiojen, ahb1lpenr, gpiojlpen, ahb1rstr, gpiojrst),
90-
GPIOK => (ahb1enr, gpioken, ahb1lpenr, gpioklpen, ahb1rstr, gpiokrst),
100+
GPIOJ => (ahb1enr, ahb1lpenr, ahb1rstr, 9),
101+
GPIOK => (ahb1enr, ahb1lpenr, ahb1rstr, 10),
91102
}
92103

93104
#[cfg(feature = "rng")]
94105
bus! {
95-
RNG => (ahb2enr, rngen, ahb2lpenr, rnglpen, ahb2rstr, rngrst),
106+
RNG => (ahb2enr, ahb2lpenr, ahb2rstr, 6),
96107
}
97108

98109
#[cfg(feature = "otg-fs")]
99110
bus! {
100-
OTG_FS_GLOBAL => (ahb2enr, otgfsen, ahb2lpenr, otgfslpen, ahb2rstr, otgfsrst),
111+
OTG_FS_GLOBAL => (ahb2enr, ahb2lpenr, ahb2rstr, 7),
101112
}
102113

103114
#[cfg(feature = "otg-hs")]
104115
bus! {
105-
OTG_HS_GLOBAL => (ahb1enr, otghsen, ahb1lpenr, otghslpen, ahb1rstr, otghsrst),
116+
OTG_HS_GLOBAL => (ahb1enr, ahb1lpenr, ahb1rstr, 29),
106117
}
107118

108119
#[cfg(feature = "fmc")]
109120
bus! {
110-
FMC => (ahb3enr, fmcen, ahb3lpenr, fmclpen, ahb3rstr, fmcrst),
121+
FMC => (ahb3enr, ahb3lpenr, ahb3rstr, 0),
111122
}
112123

113124
// TODO: fix absent ahb3lpenr
114125
#[cfg(feature = "fsmc")]
115126
impl private::Sealed for crate::pac::FSMC {}
116127
#[cfg(feature = "fsmc")]
117128
#[cfg(any(feature = "stm32f427", feature = "stm32f437"))]
118-
bus_enable!(FSMC => (ahb3enr, fmcen));
129+
bus_enable!(FSMC => (ahb3enr, 0));
119130
#[cfg(feature = "fsmc")]
120131
#[cfg(not(any(feature = "stm32f427", feature = "stm32f437")))]
121-
bus_enable!(FSMC => (ahb3enr, fsmcen));
132+
bus_enable!(FSMC => (ahb3enr, 0));
122133
#[cfg(feature = "fsmc")]
123134
#[cfg(any(feature = "stm32f427", feature = "stm32f437"))]
124-
bus_reset!(FSMC => (ahb3rstr, fmcrst));
135+
bus_reset!(FSMC => (ahb3rstr, 0));
125136
#[cfg(feature = "fsmc")]
126137
#[cfg(not(any(feature = "stm32f427", feature = "stm32f437")))]
127-
bus_reset!(FSMC => (ahb3rstr, fsmcrst));
138+
bus_reset!(FSMC => (ahb3rstr, 0));
128139

129140
bus! {
130-
PWR => (apb1enr, pwren, apb1lpenr, pwrlpen, apb1rstr, pwrrst),
141+
PWR => (apb1enr, apb1lpenr, apb1rstr, 28),
131142
}
132143

133144
bus! {
134-
SPI1 => (apb2enr, spi1en, apb2lpenr, spi1lpen, apb2rstr, spi1rst),
135-
SPI2 => (apb1enr, spi2en, apb1lpenr, spi2lpen, apb1rstr, spi2rst),
145+
SPI1 => (apb2enr, apb2lpenr, apb2rstr, 12),
146+
SPI2 => (apb1enr, apb1lpenr, apb1rstr, 14),
136147
}
137148
#[cfg(feature = "spi3")]
138149
bus! {
139-
SPI3 => (apb1enr, spi3en, apb1lpenr, spi3lpen, apb1rstr, spi3rst),
150+
SPI3 => (apb1enr, apb1lpenr, apb1rstr, 15),
140151
}
141152

142153
#[cfg(feature = "spi4")]
143154
bus! {
144-
SPI4 => (apb2enr, spi4en, apb2lpenr, spi4lpen, apb2rstr, spi4rst),
155+
SPI4 => (apb2enr, apb2lpenr, apb2rstr, 13),
145156
}
146157

147158
#[cfg(feature = "spi5")]
148159
bus! {
149-
SPI5 => (apb2enr, spi5en, apb2lpenr, spi5lpen, apb2rstr, spi5rst),
160+
SPI5 => (apb2enr, apb2lpenr, apb2rstr, 20),
150161
}
151162

152163
#[cfg(feature = "spi6")]
153164
bus! {
154-
SPI6 => (apb2enr, spi6en, apb2lpenr, spi6lpen, apb2rstr, spi6rst),
165+
SPI6 => (apb2enr, apb2lpenr, apb2rstr, 21),
155166
}
156167

157168
bus! {
158-
I2C1 => (apb1enr, i2c1en, apb1lpenr, i2c1lpen, apb1rstr, i2c1rst),
159-
I2C2 => (apb1enr, i2c2en, apb1lpenr, i2c2lpen, apb1rstr, i2c2rst),
169+
I2C1 => (apb1enr, apb1lpenr, apb1rstr, 21),
170+
I2C2 => (apb1enr, apb1lpenr, apb1rstr, 22),
160171
}
161172
#[cfg(feature = "i2c3")]
162173
bus! {
163-
I2C3 => (apb1enr, i2c3en, apb1lpenr, i2c3lpen, apb1rstr, i2c3rst),
174+
I2C3 => (apb1enr, apb1lpenr, apb1rstr, 23),
164175
}
165176
#[cfg(feature = "fmpi2c1")]
166177
bus! {
167-
FMPI2C1 => (apb1enr, fmpi2c1en, apb1lpenr, fmpi2c1lpen, apb1rstr, fmpi2c1rst),
178+
FMPI2C1 => (apb1enr, apb1lpenr, apb1rstr, 24),
168179
}
169180

170-
// TODO: fix uart2rst, uart3rst
171181
bus! {
172-
USART1 => (apb2enr, usart1en, apb2lpenr, usart1lpen, apb2rstr, usart1rst),
173-
USART2 => (apb1enr, usart2en, apb1lpenr, usart2lpen, apb1rstr, uart2rst),
174-
USART6 => (apb2enr, usart6en, apb2lpenr, usart6lpen, apb2rstr, usart6rst),
182+
USART1 => (apb2enr, apb2lpenr, apb2rstr, 4),
183+
USART2 => (apb1enr, apb1lpenr, apb1rstr, 17),
184+
USART6 => (apb2enr, apb2lpenr, apb2rstr, 5),
175185
}
176186
#[cfg(feature = "usart3")]
177-
#[cfg(any(feature = "stm32f412", feature = "stm32f413", feature = "stm32f423"))]
178187
bus! {
179-
USART3 => (apb1enr, usart3en, apb1lpenr, usart3lpen, apb1rstr, usart3rst),
188+
USART3 => (apb1enr, apb1lpenr, apb1rstr, 18),
180189
}
181-
#[cfg(feature = "usart3")]
182-
#[cfg(not(any(feature = "stm32f412", feature = "stm32f413", feature = "stm32f423")))]
190+
191+
#[cfg(any(feature = "uart4", feature = "uart5"))]
183192
bus! {
184-
USART3 => (apb1enr, usart3en, apb1lpenr, usart3lpen, apb1rstr, uart3rst),
193+
UART4 => (apb1enr, apb1lpenr, apb1rstr, 19),
194+
UART5 => (apb1enr, apb1lpenr, apb1rstr, 20),
185195
}
186196

187-
#[cfg(feature = "uart4")]
188-
impl private::Sealed for crate::pac::UART4 {}
189-
#[cfg(feature = "uart4")]
190-
bus_enable!(UART4 => (apb1enr, uart4en));
191-
#[cfg(feature = "uart5")]
192-
impl private::Sealed for crate::pac::UART5 {}
193-
#[cfg(feature = "uart5")]
194-
bus_enable!(UART5 => (apb1enr, uart5en));
195-
196197
#[cfg(any(feature = "uart7", feature = "uart8"))]
197198
bus! {
198-
UART7 => (apb1enr, uart7en, apb1lpenr, uart7lpen, apb1rstr, uart7rst),
199-
UART8 => (apb1enr, uart8en, apb1lpenr, uart8lpen, apb1rstr, uart8rst),
199+
UART7 => (apb1enr, apb1lpenr, apb1rstr, 30),
200+
UART8 => (apb1enr, apb1lpenr, apb1rstr, 31),
200201
}
201202
#[cfg(any(feature = "uart9", feature = "uart10"))]
202203
bus! {
203-
UART9 => (apb2enr, uart9en, apb2lpenr, uart9lpen, apb2rstr, uart9rst),
204-
UART10 => (apb2enr, uart10en, apb2lpenr, uart10lpen, apb2rstr, uart10rst),
204+
UART9 => (apb2enr, apb2lpenr, apb2rstr, 6),
205+
UART10 => (apb2enr, apb2lpenr, apb2rstr, 7),
205206
}
206207

207208
#[cfg(any(feature = "can1", feature = "can2"))]
208209
bus! {
209-
CAN1 => (apb1enr, can1en, apb1lpenr, can1lpen, apb1rstr, can1rst),
210-
CAN2 => (apb1enr, can2en, apb1lpenr, can2lpen, apb1rstr, can2rst),
210+
CAN1 => (apb1enr, apb1lpenr, apb1rstr, 25),
211+
CAN2 => (apb1enr, apb1lpenr, apb1rstr, 26),
211212
}
212213
#[cfg(feature = "dac")]
213214
bus! {
214-
DAC => (apb1enr, dacen, apb1lpenr, daclpen, apb1rstr, dacrst),
215+
DAC => (apb1enr, apb1lpenr, apb1rstr, 29),
215216
}
216217

217218
bus! {
218-
SYSCFG => (apb2enr, syscfgen, apb2lpenr, syscfglpen, apb2rstr, syscfgrst),
219+
SYSCFG => (apb2enr, apb2lpenr, apb2rstr, 14),
219220
}
220221

221222
bus! {
222-
ADC1 => (apb2enr, adc1en, apb2lpenr, adc1lpen, apb2rstr, adcrst),
223+
ADC1 => (apb2enr, apb2lpenr, apb2rstr, 8),
223224
}
224225

225-
#[cfg(any(feature = "adc2", feature = "adc3"))]
226-
bus! {
227-
ADC2 => (apb2enr, adc2en, apb2lpenr, adc2lpen, apb2rstr, adcrst),
228-
ADC3 => (apb2enr, adc3en, apb2lpenr, adc3lpen, apb2rstr, adcrst),
229-
}
226+
#[cfg(feature = "adc2")]
227+
impl private::Sealed for crate::pac::ADC2 {}
228+
#[cfg(feature = "adc2")]
229+
bus_enable!(ADC2 => (apb2enr, 9));
230+
#[cfg(feature = "adc2")]
231+
bus_lpenable!(ADC2 => (apb2lpenr, 9));
232+
#[cfg(feature = "adc2")]
233+
bus_reset!(ADC2 => (apb2rstr, 8));
234+
235+
#[cfg(feature = "adc3")]
236+
impl private::Sealed for crate::pac::ADC3 {}
237+
#[cfg(feature = "adc3")]
238+
bus_enable!(ADC3 => (apb2enr, 10));
239+
#[cfg(feature = "adc3")]
240+
bus_lpenable!(ADC3 => (apb2lpenr, 10));
241+
#[cfg(feature = "adc3")]
242+
bus_reset!(ADC3 => (apb2rstr, 8));
230243

231244
#[cfg(feature = "sdio")]
232245
bus! {
233-
SDIO => (apb2enr, sdioen, apb2lpenr, sdiolpen, apb2rstr, sdiorst),
246+
SDIO => (apb2enr, apb2lpenr, apb2rstr, 11),
234247
}
235248

236249
bus! {
237-
TIM1 => (apb2enr, tim1en, apb2lpenr, tim1lpen, apb2rstr, tim1rst),
238-
TIM5 => (apb1enr, tim5en, apb1lpenr, tim5lpen, apb1rstr, tim5rst),
239-
TIM9 => (apb2enr, tim9en, apb2lpenr, tim9lpen, apb2rstr, tim9rst),
240-
TIM11 => (apb2enr, tim11en, apb2lpenr, tim11lpen, apb2rstr, tim11rst),
250+
TIM1 => (apb2enr, apb2lpenr, apb2rstr, 0),
251+
TIM5 => (apb1enr, apb1lpenr, apb1rstr, 3),
252+
TIM9 => (apb2enr, apb2lpenr, apb2rstr, 16),
253+
TIM11 => (apb2enr, apb2lpenr, apb2rstr, 18),
241254
}
242255

243256
#[cfg(any(
@@ -259,10 +272,10 @@ bus! {
259272
feature = "stm32f479"
260273
))]
261274
bus! {
262-
TIM2 => (apb1enr, tim2en, apb1lpenr, tim2lpen, apb1rstr, tim2rst),
263-
TIM3 => (apb1enr, tim3en, apb1lpenr, tim3lpen, apb1rstr, tim3rst),
264-
TIM4 => (apb1enr, tim4en, apb1lpenr, tim4lpen, apb1rstr, tim4rst),
265-
TIM10 => (apb2enr, tim10en, apb2lpenr, tim10lpen, apb2rstr, tim10rst),
275+
TIM2 => (apb1enr, apb1lpenr, apb1rstr, 0),
276+
TIM3 => (apb1enr, apb1lpenr, apb1rstr, 1),
277+
TIM4 => (apb1enr, apb1lpenr, apb1rstr, 2),
278+
TIM10 => (apb2enr, apb2lpenr, apb2rstr, 17),
266279
}
267280

268281
#[cfg(any(
@@ -283,7 +296,7 @@ bus! {
283296
feature = "stm32f479"
284297
))]
285298
bus! {
286-
TIM6 => (apb1enr, tim6en, apb1lpenr, tim6lpen, apb1rstr, tim6rst),
299+
TIM6 => (apb1enr, apb1lpenr, apb1rstr, 4),
287300
}
288301

289302
#[cfg(any(
@@ -303,9 +316,9 @@ bus! {
303316
feature = "stm32f479"
304317
))]
305318
bus! {
306-
TIM7 => (apb1enr, tim7en, apb1lpenr, tim7lpen, apb1rstr, tim7rst),
307-
TIM8 => (apb2enr, tim8en, apb2lpenr, tim8lpen, apb2rstr, tim8rst),
308-
TIM12 => (apb1enr, tim12en, apb1lpenr, tim12lpen, apb1rstr, tim12rst),
309-
TIM13 => (apb1enr, tim13en, apb1lpenr, tim13lpen, apb1rstr, tim13rst),
310-
TIM14 => (apb1enr, tim14en, apb1lpenr, tim14lpen, apb1rstr, tim14rst),
319+
TIM7 => (apb1enr, apb1lpenr, apb1rstr, 5),
320+
TIM8 => (apb2enr, apb2lpenr, apb2rstr, 1),
321+
TIM12 => (apb1enr, apb1lpenr, apb1rstr, 6),
322+
TIM13 => (apb1enr, apb1lpenr, apb1rstr, 7),
323+
TIM14 => (apb1enr, apb1lpenr, apb1rstr, 8),
311324
}

0 commit comments

Comments
 (0)