@@ -5,6 +5,7 @@ use core::ptr;
5
5
use crate :: hal:: spi:: FullDuplex ;
6
6
pub use crate :: hal:: spi:: { Mode , Phase , Polarity } ;
7
7
use crate :: pac:: { SPI1 , SPI2 , SPI3 } ;
8
+ use crate :: stm32:: spi1;
8
9
9
10
use crate :: gpio:: gpioa:: { PA5 , PA6 , PA7 } ;
10
11
#[ cfg( any(
@@ -162,17 +163,7 @@ macro_rules! hal {
162
163
Polarity :: IdleHigh => w. cpol( ) . idle_high( ) ,
163
164
} ;
164
165
165
- match clocks. $pclkX( ) . 0 / freq. into( ) . 0 {
166
- 0 => unreachable!( ) ,
167
- 1 ..=2 => w. br( ) . div2( ) ,
168
- 3 ..=5 => w. br( ) . div4( ) ,
169
- 6 ..=11 => w. br( ) . div8( ) ,
170
- 12 ..=23 => w. br( ) . div16( ) ,
171
- 24 ..=39 => w. br( ) . div32( ) ,
172
- 40 ..=95 => w. br( ) . div64( ) ,
173
- 96 ..=191 => w. br( ) . div128( ) ,
174
- _ => w. br( ) . div256( ) ,
175
- } ;
166
+ w. br( ) . variant( Self :: compute_baud_rate( clocks. $pclkX( ) , freq. into( ) ) ) ;
176
167
177
168
w. spe( )
178
169
. enabled( )
@@ -195,6 +186,34 @@ macro_rules! hal {
195
186
pub fn free( self ) -> ( $SPIX, ( SCK , MISO , MOSI ) ) {
196
187
( self . spi, self . pins)
197
188
}
189
+
190
+ /// Change the baud rate of the SPI
191
+ pub fn reclock<F >( & mut self , freq: F , clocks: Clocks )
192
+ where F : Into <Hertz >
193
+ {
194
+ self . spi. cr1. modify( |_, w| w. spe( ) . disabled( ) ) ;
195
+ self . spi. cr1. modify( |_, w| {
196
+ w. br( ) . variant( Self :: compute_baud_rate( clocks. $pclkX( ) , freq. into( ) ) ) ;
197
+ w. spe( ) . enabled( )
198
+ } ) ;
199
+ }
200
+
201
+ fn compute_baud_rate( clocks: Hertz , freq: Hertz ) -> spi1:: cr1:: BR_A {
202
+ use spi1:: cr1:: BR_A ;
203
+ match clocks. 0 / freq. 0 {
204
+ 0 => unreachable!( ) ,
205
+ 1 ..=2 => BR_A :: DIV2 ,
206
+ 3 ..=5 => BR_A :: DIV4 ,
207
+ 6 ..=11 => BR_A :: DIV8 ,
208
+ 12 ..=23 => BR_A :: DIV16 ,
209
+ 24 ..=39 => BR_A :: DIV32 ,
210
+ 40 ..=95 => BR_A :: DIV64 ,
211
+ 96 ..=191 => BR_A :: DIV128 ,
212
+ _ => BR_A :: DIV256 ,
213
+ }
214
+ }
215
+
216
+
198
217
}
199
218
200
219
impl <PINS > FullDuplex <u8 > for Spi <$SPIX, PINS > {
0 commit comments