@@ -88,17 +88,7 @@ macro_rules! hal {
88
88
w. frxth( ) . set_bit( ) . ds( ) . bits( 0b111 ) . ssoe( ) . clear_bit( )
89
89
} ) ;
90
90
91
- let br = match clocks. $pclkX( ) . 0 / freq. into( ) . 0 {
92
- 0 => unreachable!( ) ,
93
- 1 ..=2 => 0b000 ,
94
- 3 ..=5 => 0b001 ,
95
- 6 ..=11 => 0b010 ,
96
- 12 ..=23 => 0b011 ,
97
- 24 ..=39 => 0b100 ,
98
- 40 ..=95 => 0b101 ,
99
- 96 ..=191 => 0b110 ,
100
- _ => 0b111 ,
101
- } ;
91
+ let br = Self :: compute_baud_rate( clocks. $pclkX( ) , freq. into( ) ) ;
102
92
103
93
// CPHA: phase
104
94
// CPOL: polarity
@@ -136,6 +126,31 @@ macro_rules! hal {
136
126
Spi { spi, pins }
137
127
}
138
128
129
+ /// Change the baud rate of the SPI
130
+ pub fn reclock<F >( & mut self , freq: F , clocks: Clocks )
131
+ where F : Into <Hertz >
132
+ {
133
+ self . spi. cr1. modify( |_, w| w. spe( ) . clear_bit( ) ) ;
134
+ self . spi. cr1. modify( |_, w| {
135
+ unsafe { w. br( ) . bits( Self :: compute_baud_rate( clocks. $pclkX( ) , freq. into( ) ) ) ; }
136
+ w. spe( ) . set_bit( )
137
+ } ) ;
138
+ }
139
+
140
+ fn compute_baud_rate( clocks: Hertz , freq: Hertz ) -> u8 {
141
+ match clocks. 0 / freq. 0 {
142
+ 0 => unreachable!( ) ,
143
+ 1 ..=2 => 0b000 ,
144
+ 3 ..=5 => 0b001 ,
145
+ 6 ..=11 => 0b010 ,
146
+ 12 ..=23 => 0b011 ,
147
+ 24 ..=39 => 0b100 ,
148
+ 40 ..=95 => 0b101 ,
149
+ 96 ..=191 => 0b110 ,
150
+ _ => 0b111 ,
151
+ }
152
+ }
153
+
139
154
/// Releases the SPI peripheral and associated pins
140
155
pub fn free( self ) -> ( $SPIX, ( SCK , MISO , MOSI ) ) {
141
156
( self . spi, self . pins)
@@ -191,7 +206,12 @@ macro_rules! hal {
191
206
}
192
207
}
193
208
194
- #[ cfg( any( feature = "stm32l4x3" , feature = "stm32l4x5" , feature = "stm32l4x6" , ) ) ]
209
+ #[ cfg( any(
210
+ feature = "stm32l4x1" ,
211
+ feature = "stm32l4x3" ,
212
+ feature = "stm32l4x5" ,
213
+ feature = "stm32l4x6" ,
214
+ ) ) ]
195
215
use crate :: gpio:: gpiod:: * ;
196
216
#[ cfg( any( feature = "stm32l4x5" , feature = "stm32l4x6" ) ) ]
197
217
use crate :: gpio:: gpiog:: * ;
@@ -256,15 +276,30 @@ pins!(SPI3, AF6,
256
276
#[ cfg( any( feature = "stm32l4x5" , feature = "stm32l4x6" , ) ) ]
257
277
pins ! ( SPI3 , AF6 , SCK : [ PG9 ] , MISO : [ PG10 ] , MOSI : [ PG11 ] ) ;
258
278
259
- #[ cfg( any( feature = "stm32l4x3" , feature = "stm32l4x5" , feature = "stm32l4x6" , ) ) ]
279
+ #[ cfg( any(
280
+ feature = "stm32l4x1" ,
281
+ feature = "stm32l4x3" ,
282
+ feature = "stm32l4x5" ,
283
+ feature = "stm32l4x6" ,
284
+ ) ) ]
260
285
use crate :: stm32:: SPI2 ;
261
286
262
- #[ cfg( any( feature = "stm32l4x3" , feature = "stm32l4x5" , feature = "stm32l4x6" , ) ) ]
287
+ #[ cfg( any(
288
+ feature = "stm32l4x1" ,
289
+ feature = "stm32l4x3" ,
290
+ feature = "stm32l4x5" ,
291
+ feature = "stm32l4x6" ,
292
+ ) ) ]
263
293
hal ! {
264
294
SPI2 : ( spi2, APB1R1 , spi2en, spi2rst, pclk1) ,
265
295
}
266
296
267
- #[ cfg( any( feature = "stm32l4x3" , feature = "stm32l4x5" , feature = "stm32l4x6" , ) ) ]
297
+ #[ cfg( any(
298
+ feature = "stm32l4x1" ,
299
+ feature = "stm32l4x3" ,
300
+ feature = "stm32l4x5" ,
301
+ feature = "stm32l4x6" ,
302
+ ) ) ]
268
303
pins ! ( SPI2 , AF5 ,
269
304
SCK : [ PB13 , PB10 , PD1 ] ,
270
305
MISO : [ PB14 , PC2 , PD3 ] ,
0 commit comments