@@ -12,7 +12,7 @@ use crate::rcc::{Enable, GetBusFreq, Rcc, Reset};
12
12
use crate :: stm32:: SPI4 ;
13
13
use crate :: stm32:: { spi1, SPI1 , SPI2 , SPI3 } ;
14
14
use crate :: time:: Hertz ;
15
- use core:: ptr;
15
+ use core:: { ptr, ops :: Deref } ;
16
16
17
17
use embedded_hal:: spi:: ErrorKind ;
18
18
pub use embedded_hal:: spi:: { Mode , Phase , Polarity , MODE_0 , MODE_1 , MODE_2 , MODE_3 } ;
@@ -87,17 +87,21 @@ impl FrameSize for u16 {
87
87
88
88
pub trait Instance :
89
89
crate :: Sealed
90
- // everything derefs to spi4, except spi1
91
- // + Deref<Target = crate::stm32::spi1::RegisterBlock>
90
+ + Deref < Target = spi1:: RegisterBlock >
92
91
+ Enable
93
92
+ Reset
94
93
+ GetBusFreq
95
94
{
96
- const PTR : * const spi1:: RegisterBlock ;
97
- #[ inline]
98
- fn registers ( & self ) -> & spi1:: RegisterBlock {
99
- unsafe { & * Self :: PTR }
95
+ const DMA_MUX_RESOURCE : DmaMuxResources ;
96
+ }
97
+
98
+ unsafe impl < SPI : Instance , PINS > TargetAddress < MemoryToPeripheral > for Spi < SPI , PINS > {
99
+ #[ inline( always) ]
100
+ fn address ( & self ) -> u32 {
101
+ self . spi . dr ( ) as * const _ as u32
100
102
}
103
+ type MemSize = u8 ;
104
+ const REQUEST_LINE : Option < u8 > = Some ( SPI :: DMA_MUX_RESOURCE as u8 ) ;
101
105
}
102
106
103
107
macro_rules! spi {
@@ -125,17 +129,7 @@ macro_rules! spi {
125
129
) *
126
130
127
131
impl Instance for $SPIX {
128
- const PTR : * const crate :: stm32:: spi1:: RegisterBlock = $SPIX:: PTR as * const crate :: stm32:: spi1:: RegisterBlock ;
129
- }
130
-
131
- unsafe impl <PINS > TargetAddress <MemoryToPeripheral > for Spi <$SPIX, PINS > {
132
- #[ inline( always) ]
133
- fn address( & self ) -> u32 {
134
- // unsafe: only the Tx part accesses the Tx register
135
- unsafe { & * <$SPIX>:: ptr( ) } . dr( ) as * const _ as u32
136
- }
137
- type MemSize = u8 ;
138
- const REQUEST_LINE : Option <u8 > = Some ( $mux as u8 ) ;
132
+ const DMA_MUX_RESOURCE : DmaMuxResources = $mux;
139
133
}
140
134
}
141
135
}
@@ -147,7 +141,6 @@ impl<SPI: Instance, PINS> Spi<SPI, PINS> {
147
141
148
142
pub fn enable_tx_dma ( self ) -> Spi < SPI , PINS > {
149
143
self . spi
150
- . registers ( )
151
144
. cr2 ( )
152
145
. modify ( |_, w| w. txdmaen ( ) . set_bit ( ) ) ;
153
146
Spi {
@@ -158,7 +151,7 @@ impl<SPI: Instance, PINS> Spi<SPI, PINS> {
158
151
159
152
#[ inline]
160
153
fn nb_read < W : FrameSize > ( & mut self ) -> nb:: Result < W , Error > {
161
- let sr = self . spi . registers ( ) . sr ( ) . read ( ) ;
154
+ let sr = self . spi . sr ( ) . read ( ) ;
162
155
Err ( if sr. ovr ( ) . bit_is_set ( ) {
163
156
nb:: Error :: Other ( Error :: Overrun )
164
157
} else if sr. modf ( ) . bit_is_set ( ) {
@@ -173,7 +166,7 @@ impl<SPI: Instance, PINS> Spi<SPI, PINS> {
173
166
}
174
167
#[ inline]
175
168
fn nb_write < W : FrameSize > ( & mut self , word : W ) -> nb:: Result < ( ) , Error > {
176
- let sr = self . spi . registers ( ) . sr ( ) . read ( ) ;
169
+ let sr = self . spi . sr ( ) . read ( ) ;
177
170
Err ( if sr. ovr ( ) . bit_is_set ( ) {
178
171
nb:: Error :: Other ( Error :: Overrun )
179
172
} else if sr. modf ( ) . bit_is_set ( ) {
@@ -189,7 +182,7 @@ impl<SPI: Instance, PINS> Spi<SPI, PINS> {
189
182
}
190
183
#[ inline]
191
184
fn nb_read_no_err < W : FrameSize > ( & mut self ) -> nb:: Result < W , core:: convert:: Infallible > {
192
- if self . spi . registers ( ) . sr ( ) . read ( ) . rxne ( ) . bit_is_set ( ) {
185
+ if self . spi . sr ( ) . read ( ) . rxne ( ) . bit_is_set ( ) {
193
186
Ok ( self . read_unchecked ( ) )
194
187
} else {
195
188
Err ( nb:: Error :: WouldBlock )
@@ -199,29 +192,28 @@ impl<SPI: Instance, PINS> Spi<SPI, PINS> {
199
192
fn read_unchecked < W : FrameSize > ( & mut self ) -> W {
200
193
// NOTE(read_volatile) read only 1 byte (the svd2rust API only allows
201
194
// reading a half-word)
202
- unsafe { ptr:: read_volatile ( & self . spi . registers ( ) . dr ( ) as * const _ as * const W ) }
195
+ unsafe { ptr:: read_volatile ( & self . spi . dr ( ) as * const _ as * const W ) }
203
196
}
204
197
#[ inline]
205
198
fn write_unchecked < W : FrameSize > ( & mut self , word : W ) {
206
- let dr = self . spi . registers ( ) . dr ( ) . as_ptr ( ) as * mut W ;
199
+ // NOTE(write_volatile) see note above
200
+ let dr = self . spi . dr ( ) . as_ptr ( ) as * mut W ;
207
201
unsafe { ptr:: write_volatile ( dr, word) } ;
208
202
}
209
203
#[ inline]
210
204
pub fn set_tx_only ( & mut self ) {
211
205
self . spi
212
- . registers ( )
213
206
. cr1 ( )
214
207
. modify ( |_, w| w. bidimode ( ) . set_bit ( ) . bidioe ( ) . set_bit ( ) ) ;
215
208
}
216
209
#[ inline]
217
210
pub fn set_bidi ( & mut self ) {
218
211
self . spi
219
- . registers ( )
220
212
. cr1 ( )
221
213
. modify ( |_, w| w. bidimode ( ) . clear_bit ( ) . bidioe ( ) . clear_bit ( ) ) ;
222
214
}
223
215
fn tx_fifo_cap ( & self ) -> u8 {
224
- match self . spi . registers ( ) . sr ( ) . read ( ) . ftlvl ( ) . bits ( ) {
216
+ match self . spi . sr ( ) . read ( ) . ftlvl ( ) . bits ( ) {
225
217
0 => 4 ,
226
218
1 => 3 ,
227
219
2 => 2 ,
@@ -231,7 +223,7 @@ impl<SPI: Instance, PINS> Spi<SPI, PINS> {
231
223
fn flush_inner ( & mut self ) -> Result < ( ) , Error > {
232
224
// stop receiving data
233
225
self . set_tx_only ( ) ;
234
- self . spi . registers ( ) . cr2 ( ) . modify ( |_, w| w. frxth ( ) . set_bit ( ) ) ;
226
+ self . spi . cr2 ( ) . modify ( |_, w| w. frxth ( ) . set_bit ( ) ) ;
235
227
// drain rx fifo
236
228
while match self . nb_read :: < u8 > ( ) {
237
229
Ok ( _) => true ,
@@ -241,7 +233,7 @@ impl<SPI: Instance, PINS> Spi<SPI, PINS> {
241
233
core:: hint:: spin_loop ( )
242
234
}
243
235
// wait for idle
244
- Ok ( while self . spi . registers ( ) . sr ( ) . read ( ) . bsy ( ) . bit ( ) {
236
+ Ok ( while self . spi . sr ( ) . read ( ) . bsy ( ) . bit ( ) {
245
237
core:: hint:: spin_loop ( )
246
238
} )
247
239
}
@@ -303,7 +295,7 @@ impl<SPI: Instance> SpiExt<SPI> for SPI {
303
295
304
296
let spi_freq = freq. into ( ) . raw ( ) ;
305
297
let bus_freq = SPI :: get_frequency ( & rcc. clocks ) . raw ( ) ;
306
- setup_spi_regs ( self . registers ( ) , spi_freq, bus_freq, mode) ;
298
+ setup_spi_regs ( & self , spi_freq, bus_freq, mode) ;
307
299
308
300
Spi { spi : self , pins }
309
301
}
@@ -324,7 +316,6 @@ impl<SPI: Instance, PINS: Pins<SPI>> embedded_hal::spi::SpiBus<u8> for Spi<SPI,
324
316
self . flush_inner ( ) ?;
325
317
// FIFO threshold to 16 bits
326
318
self . spi
327
- . registers ( )
328
319
. cr2 ( )
329
320
. modify ( |_, w| w. frxth ( ) . clear_bit ( ) ) ;
330
321
self . set_bidi ( ) ;
@@ -349,7 +340,7 @@ impl<SPI: Instance, PINS: Pins<SPI>> embedded_hal::spi::SpiBus<u8> for Spi<SPI,
349
340
350
341
let odd_idx = len. saturating_sub ( 2 * prefill + pair_left) ;
351
342
// FIFO threshold to 8 bits
352
- self . spi . registers ( ) . cr2 ( ) . modify ( |_, w| w. frxth ( ) . set_bit ( ) ) ;
343
+ self . spi . cr2 ( ) . modify ( |_, w| w. frxth ( ) . set_bit ( ) ) ;
353
344
if pair_left == 1 {
354
345
nb:: block!( self . nb_write( 0u8 ) ) ?;
355
346
words[ odd_idx] = nb:: block!( self . nb_read_no_err( ) ) . unwrap ( ) ;
@@ -389,7 +380,6 @@ impl<SPI: Instance, PINS: Pins<SPI>> embedded_hal::spi::SpiBus<u8> for Spi<SPI,
389
380
390
381
// FIFO threshold to 16 bits
391
382
self . spi
392
- . registers ( )
393
383
. cr2 ( )
394
384
. modify ( |_, w| w. frxth ( ) . clear_bit ( ) ) ;
395
385
@@ -414,7 +404,7 @@ impl<SPI: Instance, PINS: Pins<SPI>> embedded_hal::spi::SpiBus<u8> for Spi<SPI,
414
404
}
415
405
416
406
// FIFO threshold to 8 bits
417
- self . spi . registers ( ) . cr2 ( ) . modify ( |_, w| w. frxth ( ) . set_bit ( ) ) ;
407
+ self . spi . cr2 ( ) . modify ( |_, w| w. frxth ( ) . set_bit ( ) ) ;
418
408
419
409
if pair_left == 1 {
420
410
let write_idx = common_len - 1 ;
@@ -448,7 +438,6 @@ impl<SPI: Instance, PINS: Pins<SPI>> embedded_hal::spi::SpiBus<u8> for Spi<SPI,
448
438
self . flush_inner ( ) ?;
449
439
self . set_bidi ( ) ;
450
440
self . spi
451
- . registers ( )
452
441
. cr2 ( )
453
442
. modify ( |_, w| w. frxth ( ) . clear_bit ( ) ) ;
454
443
let half_len = len / 2 ;
@@ -471,7 +460,7 @@ impl<SPI: Instance, PINS: Pins<SPI>> embedded_hal::spi::SpiBus<u8> for Spi<SPI,
471
460
let write = u16:: from_le_bytes ( words_alias[ i + prefill] ) ;
472
461
nb:: block!( self . nb_write( write) ) ?;
473
462
}
474
- self . spi . registers ( ) . cr2 ( ) . modify ( |_, w| w. frxth ( ) . set_bit ( ) ) ;
463
+ self . spi . cr2 ( ) . modify ( |_, w| w. frxth ( ) . set_bit ( ) ) ;
475
464
476
465
if pair_left == 1 {
477
466
let read_idx = len - 2 * prefill - 1 ;
@@ -503,7 +492,6 @@ impl<SPI: Instance, PINS: Pins<SPI>> embedded_hal::spi::SpiBus<u16> for Spi<SPI,
503
492
self . flush_inner ( ) ?;
504
493
// FIFO threshold to 16 bits
505
494
self . spi
506
- . registers ( )
507
495
. cr2 ( )
508
496
. modify ( |_, w| w. frxth ( ) . clear_bit ( ) ) ;
509
497
self . set_bidi ( ) ;
@@ -537,7 +525,6 @@ impl<SPI: Instance, PINS: Pins<SPI>> embedded_hal::spi::SpiBus<u16> for Spi<SPI,
537
525
self . flush_inner ( ) ?;
538
526
// FIFO threshold to 16 bits
539
527
self . spi
540
- . registers ( )
541
528
. cr2 ( )
542
529
. modify ( |_, w| w. frxth ( ) . clear_bit ( ) ) ;
543
530
self . set_bidi ( ) ;
@@ -575,7 +562,6 @@ impl<SPI: Instance, PINS: Pins<SPI>> embedded_hal::spi::SpiBus<u16> for Spi<SPI,
575
562
self . flush_inner ( ) ?;
576
563
self . set_bidi ( ) ;
577
564
self . spi
578
- . registers ( )
579
565
. cr2 ( )
580
566
. modify ( |_, w| w. frxth ( ) . clear_bit ( ) ) ;
581
567
let prefill = core:: cmp:: min ( self . tx_fifo_cap ( ) as usize / 2 , len) ;
0 commit comments