@@ -4,8 +4,36 @@ use core::ptr;
44
55use crate :: dma:: traits:: PeriAddress ;
66use crate :: gpio:: { Const , NoPin , PinA , PushPull , SetAlternate } ;
7- use embedded_hal:: spi;
8- pub use embedded_hal:: spi:: { Mode , Phase , Polarity } ;
7+
8+ /// Clock polarity
9+ #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
10+ pub enum Polarity {
11+ /// Clock signal low when idle
12+ IdleLow ,
13+ /// Clock signal high when idle
14+ IdleHigh ,
15+ }
16+
17+ /// Clock phase
18+ #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
19+ pub enum Phase {
20+ /// Data in "captured" on the first clock transition
21+ CaptureOnFirstTransition ,
22+ /// Data in "captured" on the second clock transition
23+ CaptureOnSecondTransition ,
24+ }
25+
26+ /// SPI mode
27+ #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
28+ pub struct Mode {
29+ /// Clock polarity
30+ pub polarity : Polarity ,
31+ /// Clock phase
32+ pub phase : Phase ,
33+ }
34+
35+ mod hal_02;
36+ mod hal_1;
937
1038use crate :: pac:: { spi1, RCC , SPI1 , SPI2 } ;
1139use crate :: rcc;
@@ -141,7 +169,7 @@ where
141169 pub fn new (
142170 spi : SPI ,
143171 mut pins : PINS ,
144- mode : Mode ,
172+ mode : impl Into < Mode > ,
145173 freq : impl Into < Hertz > ,
146174 clocks : & Clocks ,
147175 ) -> Self {
@@ -159,7 +187,7 @@ where
159187 pins,
160188 transfer_mode : TransferModeNormal ,
161189 }
162- . pre_init ( mode, freq. into ( ) , SPI :: clock ( clocks) )
190+ . pre_init ( mode. into ( ) , freq. into ( ) , SPI :: clock ( clocks) )
163191 . init ( )
164192 }
165193
@@ -178,7 +206,7 @@ where
178206 pub fn new_bidi (
179207 spi : SPI ,
180208 mut pins : PINS ,
181- mode : Mode ,
209+ mode : impl Into < Mode > ,
182210 freq : impl Into < Hertz > ,
183211 clocks : & Clocks ,
184212 ) -> Self {
@@ -196,7 +224,7 @@ where
196224 pins,
197225 transfer_mode : TransferModeBidi ,
198226 }
199- . pre_init ( mode, freq. into ( ) , SPI :: clock ( clocks) )
227+ . pre_init ( mode. into ( ) , freq. into ( ) , SPI :: clock ( clocks) )
200228 . init ( )
201229 }
202230
@@ -284,7 +312,7 @@ where
284312 }
285313
286314 /// Pre initializing the SPI bus.
287- pub fn pre_init ( self , mode : Mode , freq : Hertz , clock : Hertz ) -> Self {
315+ fn pre_init ( self , mode : Mode , freq : Hertz , clock : Hertz ) -> Self {
288316 // disable SS output
289317 self . spi . cr2 . write ( |w| w. ssoe ( ) . clear_bit ( ) ) ;
290318
@@ -500,148 +528,3 @@ where
500528
501529 type MemSize = u8 ;
502530}
503-
504- impl < SPI , PINS > spi:: FullDuplex < u8 > for Spi < SPI , PINS , TransferModeNormal >
505- where
506- SPI : Instance ,
507- {
508- type Error = Error ;
509-
510- fn read ( & mut self ) -> nb:: Result < u8 , Error > {
511- self . check_read ( )
512- }
513-
514- fn send ( & mut self , byte : u8 ) -> nb:: Result < ( ) , Error > {
515- self . check_send ( byte)
516- }
517- }
518-
519- impl < SPI , PINS > spi:: FullDuplex < u8 > for Spi < SPI , PINS , TransferModeBidi >
520- where
521- SPI : Instance ,
522- {
523- type Error = Error ;
524-
525- fn read ( & mut self ) -> nb:: Result < u8 , Error > {
526- self . spi . cr1 . modify ( |_, w| w. bidioe ( ) . clear_bit ( ) ) ;
527- self . check_read ( )
528- }
529-
530- fn send ( & mut self , byte : u8 ) -> nb:: Result < ( ) , Error > {
531- self . spi . cr1 . modify ( |_, w| w. bidioe ( ) . set_bit ( ) ) ;
532- self . check_send ( byte)
533- }
534- }
535-
536- mod blocking {
537- use super :: { Error , Instance , Spi , TransferModeBidi , TransferModeNormal } ;
538- use embedded_hal:: blocking:: spi:: { Operation , Transactional , Transfer , Write , WriteIter } ;
539- use embedded_hal:: spi:: FullDuplex ;
540-
541- impl < SPI , PINS , TRANSFER_MODE > Transfer < u8 > for Spi < SPI , PINS , TRANSFER_MODE >
542- where
543- Self : FullDuplex < u8 , Error = Error > ,
544- SPI : Instance ,
545- {
546- type Error = Error ;
547-
548- fn transfer < ' w > ( & mut self , words : & ' w mut [ u8 ] ) -> Result < & ' w [ u8 ] , Self :: Error > {
549- for word in words. iter_mut ( ) {
550- nb:: block!( self . send( * word) ) ?;
551- * word = nb:: block!( self . read( ) ) ?;
552- }
553-
554- Ok ( words)
555- }
556- }
557-
558- impl < SPI , PINS > Write < u8 > for Spi < SPI , PINS , TransferModeNormal >
559- where
560- Self : FullDuplex < u8 , Error = Error > ,
561- SPI : Instance ,
562- {
563- type Error = Error ;
564-
565- fn write ( & mut self , words : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
566- for word in words {
567- nb:: block!( self . send( * word) ) ?;
568- nb:: block!( self . read( ) ) ?;
569- }
570-
571- Ok ( ( ) )
572- }
573- }
574-
575- impl < SPI , PINS > Write < u8 > for Spi < SPI , PINS , TransferModeBidi >
576- where
577- Self : FullDuplex < u8 , Error = Error > ,
578- SPI : Instance ,
579- {
580- type Error = Error ;
581-
582- fn write ( & mut self , words : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
583- for word in words {
584- nb:: block!( self . send( * word) ) ?;
585- }
586-
587- Ok ( ( ) )
588- }
589- }
590-
591- impl < SPI , PINS > WriteIter < u8 > for Spi < SPI , PINS , TransferModeNormal >
592- where
593- Self : FullDuplex < u8 , Error = Error > ,
594- SPI : Instance ,
595- {
596- type Error = Error ;
597-
598- fn write_iter < WI > ( & mut self , words : WI ) -> Result < ( ) , Self :: Error >
599- where
600- WI : IntoIterator < Item = u8 > ,
601- {
602- for word in words. into_iter ( ) {
603- nb:: block!( self . send( word) ) ?;
604- nb:: block!( self . read( ) ) ?;
605- }
606-
607- Ok ( ( ) )
608- }
609- }
610-
611- impl < SPI , PINS > WriteIter < u8 > for Spi < SPI , PINS , TransferModeBidi >
612- where
613- Self : FullDuplex < u8 , Error = Error > ,
614- SPI : Instance ,
615- {
616- type Error = Error ;
617-
618- fn write_iter < WI > ( & mut self , words : WI ) -> Result < ( ) , Self :: Error >
619- where
620- WI : IntoIterator < Item = u8 > ,
621- {
622- for word in words. into_iter ( ) {
623- nb:: block!( self . send( word) ) ?;
624- }
625-
626- Ok ( ( ) )
627- }
628- }
629-
630- impl < SPI , PINS , TRANSFER_MODE , W : ' static > Transactional < W > for Spi < SPI , PINS , TRANSFER_MODE >
631- where
632- Self : Write < W , Error = Error > + Transfer < W , Error = Error > ,
633- {
634- type Error = Error ;
635-
636- fn exec < ' a > ( & mut self , operations : & mut [ Operation < ' a , W > ] ) -> Result < ( ) , Error > {
637- for op in operations {
638- match op {
639- Operation :: Write ( w) => self . write ( w) ?,
640- Operation :: Transfer ( t) => self . transfer ( t) . map ( |_| ( ) ) ?,
641- }
642- }
643-
644- Ok ( ( ) )
645- }
646- }
647- }
0 commit comments