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