@@ -35,7 +35,7 @@ use core::ptr;
35
35
36
36
use nb;
37
37
38
- pub use crate :: hal:: spi:: { Mode , Phase , Polarity } ;
38
+ pub use crate :: hal:: spi:: { Mode , Phase , Polarity , FullDuplex } ;
39
39
#[ cfg( feature = "high" ) ]
40
40
use crate :: pac:: SPI3 ;
41
41
use crate :: pac:: { SPI1 , SPI2 } ;
@@ -334,9 +334,52 @@ impl<SPI, REMAP, PINS> crate::hal::blocking::spi::transfer::Default<u8> for Spi<
334
334
{
335
335
}
336
336
337
- impl < SPI , REMAP , PINS > crate :: hal:: blocking:: spi:: write :: Default < u8 > for Spi < SPI , REMAP , PINS > where
337
+ impl < SPI , REMAP , PINS > crate :: hal:: blocking:: spi:: Write < u8 > for Spi < SPI , REMAP , PINS > where
338
338
SPI : Deref < Target = SpiRegisterBlock >
339
339
{
340
+ type Error = Error ;
341
+
342
+ // Implement write as per the "Transmit only procedure" page 712
343
+ // of RM0008 Rev 20. This is more than twice as fast as the
344
+ // default Write<> implementation (which reads and drops each
345
+ // received value)
346
+ fn write ( & mut self , words : & [ u8 ] ) -> Result < ( ) , Error > {
347
+ // Write each word when the tx buffer is empty
348
+ for word in words {
349
+ loop {
350
+ let sr = self . spi . sr . read ( ) ;
351
+ if sr. txe ( ) . bit_is_set ( ) {
352
+ // NOTE(write_volatile) see note above
353
+ unsafe { ptr:: write_volatile ( & self . spi . dr as * const _ as * mut u8 , * word) }
354
+ if sr. modf ( ) . bit_is_set ( ) {
355
+ return Err ( Error :: ModeFault ) ;
356
+ }
357
+ break ;
358
+ }
359
+ }
360
+ }
361
+ // Wait for final TXE
362
+ loop {
363
+ let sr = self . spi . sr . read ( ) ;
364
+ if sr. txe ( ) . bit_is_set ( ) {
365
+ break ;
366
+ }
367
+ }
368
+ // Wait for final !BSY
369
+ loop {
370
+ let sr = self . spi . sr . read ( ) ;
371
+ if !sr. bsy ( ) . bit_is_set ( ) {
372
+ break ;
373
+ }
374
+ }
375
+ // Clear OVR set due to dropped received values
376
+ // NOTE(read_volatile) see note aboev
377
+ unsafe {
378
+ let _ = ptr:: read_volatile ( & self . spi . dr as * const _ as * const u8 ) ;
379
+ }
380
+ let _ = self . spi . sr . read ( ) ;
381
+ Ok ( ( ) )
382
+ }
340
383
}
341
384
342
385
// DMA
0 commit comments