1- use embedded_hal:: spi:: blocking:: Operation ;
2- pub use embedded_hal:: spi:: blocking:: { Read , Transfer , TransferInplace , Write , WriteIter } ;
3- pub use embedded_hal:: spi:: nb :: FullDuplex ;
1+ use embedded_hal:: spi:: blocking:: { SpiBus as SpiBusTransfer , SpiBusFlush } ;
2+ use embedded_hal:: spi:: blocking:: { SpiBusRead , SpiBusWrite } ;
3+ use embedded_hal:: spi:: ErrorType ;
44pub use embedded_hal:: spi:: { ErrorKind , Mode , Phase , Polarity , MODE_0 , MODE_1 , MODE_2 , MODE_3 } ;
55
6- use nb;
7-
86use super :: { Pins , PinsNoCS , SharedBus , SpiConfig , SpiExclusiveDevice , SpiX } ;
97
108/// SPI bus abstraction
@@ -101,38 +99,18 @@ where
10199 }
102100 }
103101
104- // ex-traits now only accessible via devices
105-
106- pub ( crate ) fn read ( & mut self ) -> nb:: Result < u8 , ErrorKind > {
107- let rxdata = self . spi . rxdata . read ( ) ;
108-
109- if rxdata. empty ( ) . bit_is_set ( ) {
110- Err ( nb:: Error :: WouldBlock )
111- } else {
112- Ok ( rxdata. data ( ) . bits ( ) )
113- }
114- }
115-
116- pub ( crate ) fn send ( & mut self , byte : u8 ) -> nb:: Result < ( ) , ErrorKind > {
117- let txdata = self . spi . txdata . read ( ) ;
118-
119- if txdata. full ( ) . bit_is_set ( ) {
120- Err ( nb:: Error :: WouldBlock )
121- } else {
122- self . spi . txdata . write ( |w| unsafe { w. data ( ) . bits ( byte) } ) ;
123- Ok ( ( ) )
124- }
125- }
126-
127- pub ( crate ) fn transfer ( & mut self , read : & mut [ u8 ] , write : & [ u8 ] ) -> Result < ( ) , ErrorKind > {
102+ /// Transfer implementation out of trait for reuse in Read and Write
103+ fn perform_transfer ( & mut self , read : & mut [ u8 ] , write : & [ u8 ] ) -> Result < ( ) , ErrorKind > {
128104 let mut iwrite = 0 ;
129105 let mut iread = 0 ;
106+ let bytes = core:: cmp:: max ( read. len ( ) , write. len ( ) ) ;
130107
131108 // Ensure that RX FIFO is empty
132109 self . wait_for_rxfifo ( ) ;
133110
134111 // go through entire write buffer and read back (even if read buffer is empty)
135- while iwrite < write. len ( ) || iread < write. len ( ) {
112+ // while iwrite < write.len() || iread < write.len() {
113+ while iwrite < bytes || iread < bytes {
136114 if iwrite < write. len ( ) && self . spi . txdata . read ( ) . full ( ) . bit_is_clear ( ) {
137115 let byte = write. get ( iwrite) . unwrap_or ( & 0 ) ;
138116 iwrite += 1 ;
@@ -152,8 +130,49 @@ where
152130
153131 Ok ( ( ) )
154132 }
133+ }
134+
135+ impl < SPI , PINS > ErrorType for SpiBus < SPI , PINS > {
136+ type Error = ErrorKind ;
137+ }
138+
139+ impl < SPI , PINS > SpiBusFlush for SpiBus < SPI , PINS >
140+ where
141+ SPI : SpiX ,
142+ {
143+ fn flush ( & mut self ) -> Result < ( ) , Self :: Error > {
144+ // unnecessary
145+
146+ Ok ( ( ) )
147+ }
148+ }
149+ impl < SPI , PINS > SpiBusRead for SpiBus < SPI , PINS >
150+ where
151+ SPI : SpiX ,
152+ {
153+ fn read ( & mut self , words : & mut [ u8 ] ) -> Result < ( ) , ErrorKind > {
154+ self . perform_transfer ( words, & [ ] )
155+ }
156+ }
157+
158+ impl < SPI , PINS > SpiBusWrite for SpiBus < SPI , PINS >
159+ where
160+ SPI : SpiX ,
161+ {
162+ fn write ( & mut self , words : & [ u8 ] ) -> Result < ( ) , ErrorKind > {
163+ self . perform_transfer ( & mut [ ] , words)
164+ }
165+ }
166+
167+ impl < SPI , PINS > SpiBusTransfer for SpiBus < SPI , PINS >
168+ where
169+ SPI : SpiX ,
170+ {
171+ fn transfer ( & mut self , read : & mut [ u8 ] , write : & [ u8 ] ) -> Result < ( ) , ErrorKind > {
172+ self . perform_transfer ( read, write)
173+ }
155174
156- pub ( crate ) fn transfer_inplace ( & mut self , words : & mut [ u8 ] ) -> Result < ( ) , ErrorKind > {
175+ fn transfer_in_place ( & mut self , words : & mut [ u8 ] ) -> Result < ( ) , ErrorKind > {
157176 let mut iwrite = 0 ;
158177 let mut iread = 0 ;
159178
@@ -178,63 +197,6 @@ where
178197
179198 Ok ( ( ) )
180199 }
181-
182- pub ( crate ) fn write_iter < WI > ( & mut self , words : WI ) -> Result < ( ) , ErrorKind >
183- where
184- WI : IntoIterator < Item = u8 > ,
185- {
186- let mut iter = words. into_iter ( ) ;
187-
188- let mut read_count = 0 ;
189- let mut has_data = true ;
190-
191- // Ensure that RX FIFO is empty
192- self . wait_for_rxfifo ( ) ;
193-
194- while has_data || read_count > 0 {
195- if has_data && self . spi . txdata . read ( ) . full ( ) . bit_is_clear ( ) {
196- if let Some ( byte) = iter. next ( ) {
197- self . spi . txdata . write ( |w| unsafe { w. data ( ) . bits ( byte) } ) ;
198- read_count += 1 ;
199- } else {
200- has_data = false ;
201- }
202- }
203-
204- if read_count > 0 {
205- // Read and discard byte, if any
206- if self . spi . rxdata . read ( ) . empty ( ) . bit_is_clear ( ) {
207- read_count -= 1 ;
208- }
209- }
210- }
211-
212- Ok ( ( ) )
213- }
214-
215- pub ( crate ) fn exec < ' op > (
216- & mut self ,
217- operations : & mut [ Operation < ' op , u8 > ] ,
218- ) -> Result < ( ) , ErrorKind > {
219- for op in operations {
220- match op {
221- Operation :: Read ( words) => {
222- self . transfer ( words, & [ ] ) ?;
223- }
224- Operation :: Write ( words) => {
225- self . transfer ( & mut [ ] , words) ?;
226- }
227- Operation :: Transfer ( read_words, write_words) => {
228- self . transfer ( read_words, write_words) ?;
229- }
230- Operation :: TransferInplace ( words) => {
231- self . transfer_inplace ( words) ?;
232- }
233- }
234- }
235-
236- Ok ( ( ) )
237- }
238200}
239201
240202impl < SPI , PINS > SpiBus < SPI , PINS >
0 commit comments