34
34
*/
35
35
36
36
mod hal_02;
37
+ mod hal_1;
37
38
38
39
use core:: ops:: Deref ;
39
40
use core:: ptr;
40
41
41
42
use crate :: pac:: { self , RCC } ;
42
- pub use embedded_hal_02:: spi:: { Mode , Phase , Polarity } ;
43
43
44
44
use crate :: afio:: MAPR ;
45
45
use crate :: dma:: dma1;
@@ -53,6 +53,33 @@ use crate::time::Hertz;
53
53
use core:: sync:: atomic:: { self , Ordering } ;
54
54
use embedded_dma:: { ReadBuffer , WriteBuffer } ;
55
55
56
+ /// Clock polarity
57
+ #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
58
+ pub enum Polarity {
59
+ /// Clock signal low when idle
60
+ IdleLow ,
61
+ /// Clock signal high when idle
62
+ IdleHigh ,
63
+ }
64
+
65
+ /// Clock phase
66
+ #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
67
+ pub enum Phase {
68
+ /// Data in "captured" on the first clock transition
69
+ CaptureOnFirstTransition ,
70
+ /// Data in "captured" on the second clock transition
71
+ CaptureOnSecondTransition ,
72
+ }
73
+
74
+ /// SPI mode
75
+ #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
76
+ pub struct Mode {
77
+ /// Clock polarity
78
+ pub polarity : Polarity ,
79
+ /// Clock phase
80
+ pub phase : Phase ,
81
+ }
82
+
56
83
/// Interrupt event
57
84
pub enum Event {
58
85
/// New data has been received
@@ -193,7 +220,7 @@ impl<REMAP, PINS> Spi<pac::SPI1, REMAP, PINS, u8, Master> {
193
220
spi : pac:: SPI1 ,
194
221
pins : PINS ,
195
222
mapr : & mut MAPR ,
196
- mode : Mode ,
223
+ mode : impl Into < Mode > ,
197
224
freq : Hertz ,
198
225
clocks : Clocks ,
199
226
) -> Self
@@ -214,7 +241,7 @@ impl<REMAP, PINS> Spi<pac::SPI1, REMAP, PINS, u8, Slave> {
214
241
215
242
You can also use `NoMiso` or `NoMosi` if you don't want to use the pins
216
243
*/
217
- pub fn spi1_slave ( spi : pac:: SPI1 , pins : PINS , mapr : & mut MAPR , mode : Mode ) -> Self
244
+ pub fn spi1_slave ( spi : pac:: SPI1 , pins : PINS , mapr : & mut MAPR , mode : impl Into < Mode > ) -> Self
218
245
where
219
246
REMAP : Remap < Periph = pac:: SPI1 > ,
220
247
PINS : Pins < REMAP , Slave > ,
@@ -457,7 +484,8 @@ impl<SPI, REMAP, PINS> Spi<SPI, REMAP, PINS, u8, Master>
457
484
where
458
485
SPI : Instance ,
459
486
{
460
- fn configure ( spi : SPI , pins : PINS , mode : Mode , freq : Hertz , clocks : Clocks ) -> Self {
487
+ fn configure ( spi : SPI , pins : PINS , mode : impl Into < Mode > , freq : Hertz , clocks : Clocks ) -> Self {
488
+ let mode = mode. into ( ) ;
461
489
// enable or reset SPI
462
490
let rcc = unsafe { & ( * RCC :: ptr ( ) ) } ;
463
491
SPI :: enable ( rcc) ;
@@ -517,7 +545,8 @@ impl<SPI, REMAP, PINS> Spi<SPI, REMAP, PINS, u8, Slave>
517
545
where
518
546
SPI : Instance ,
519
547
{
520
- fn configure ( spi : SPI , pins : PINS , mode : Mode ) -> Self {
548
+ fn configure ( spi : SPI , pins : PINS , mode : impl Into < Mode > ) -> Self {
549
+ let mode = mode. into ( ) ;
521
550
// enable or reset SPI
522
551
let rcc = unsafe { & ( * RCC :: ptr ( ) ) } ;
523
552
SPI :: enable ( rcc) ;
@@ -597,6 +626,49 @@ where
597
626
}
598
627
}
599
628
629
+ impl < SPI , REMAP , PINS , FrameSize , OP > Spi < SPI , REMAP , PINS , FrameSize , OP >
630
+ where
631
+ SPI : Instance ,
632
+ FrameSize : Copy ,
633
+ {
634
+ pub fn read_nonblocking ( & mut self ) -> nb:: Result < FrameSize , Error > {
635
+ let sr = self . spi . sr . read ( ) ;
636
+
637
+ Err ( if sr. ovr ( ) . bit_is_set ( ) {
638
+ Error :: Overrun . into ( )
639
+ } else if sr. modf ( ) . bit_is_set ( ) {
640
+ Error :: ModeFault . into ( )
641
+ } else if sr. crcerr ( ) . bit_is_set ( ) {
642
+ Error :: Crc . into ( )
643
+ } else if sr. rxne ( ) . bit_is_set ( ) {
644
+ // NOTE(read_volatile) read only 1 byte (the svd2rust API only allows
645
+ // reading a half-word)
646
+ return Ok ( self . read_data_reg ( ) ) ;
647
+ } else {
648
+ nb:: Error :: WouldBlock
649
+ } )
650
+ }
651
+ pub fn write_nonblocking ( & mut self , data : FrameSize ) -> nb:: Result < ( ) , Error > {
652
+ let sr = self . spi . sr . read ( ) ;
653
+
654
+ // NOTE: Error::Overrun was deleted in #408. Need check
655
+ Err ( if sr. modf ( ) . bit_is_set ( ) {
656
+ Error :: ModeFault . into ( )
657
+ } else if sr. crcerr ( ) . bit_is_set ( ) {
658
+ Error :: Crc . into ( )
659
+ } else if sr. txe ( ) . bit_is_set ( ) {
660
+ // NOTE(write_volatile) see note above
661
+ self . write_data_reg ( data) ;
662
+ return Ok ( ( ) ) ;
663
+ } else {
664
+ nb:: Error :: WouldBlock
665
+ } )
666
+ }
667
+ pub fn write ( & mut self , words : & [ FrameSize ] ) -> Result < ( ) , Error > {
668
+ self . spi_write ( words)
669
+ }
670
+ }
671
+
600
672
// DMA
601
673
602
674
pub type SpiTxDma < SPI , REMAP , PINS , OP , CHANNEL > = TxDma < Spi < SPI , REMAP , PINS , u8 , OP > , CHANNEL > ;
0 commit comments