@@ -10,13 +10,16 @@ use std::path::Path;
1010
1111/// Newtype around [`spidev::Spidev`] that implements the `embedded-hal` traits
1212///
13- /// [`spidev::Spidev`]: https://docs.rs/spidev/0.5.0/spidev/struct.Spidev.html
13+ /// [Delay operations][delay] are capped to 65535 microseconds.
14+ ///
15+ /// [`spidev::Spidev`]: https://docs.rs/spidev/0.5.2/spidev/struct.Spidev.html
16+ /// [delay]: embedded_hal::spi::Operation::DelayUs
1417pub struct Spidev ( pub spidev:: Spidev ) ;
1518
1619impl Spidev {
1720 /// See [`spidev::Spidev::open`][0] for details.
1821 ///
19- /// [0]: https://docs.rs/spidev/0.5.0 /spidev/struct.Spidev.html#method.open
22+ /// [0]: https://docs.rs/spidev/0.5.2 /spidev/struct.Spidev.html#method.open
2023 pub fn open < P > ( path : P ) -> Result < Self , SPIError >
2124 where
2225 P : AsRef < Path > ,
@@ -42,36 +45,24 @@ impl ops::DerefMut for Spidev {
4245mod embedded_hal_impl {
4346 use super :: * ;
4447 use embedded_hal:: spi:: ErrorType ;
45- use embedded_hal:: spi:: {
46- Operation as SpiOperation , SpiBus , SpiBusFlush , SpiBusRead , SpiBusWrite , SpiDevice ,
47- SpiDeviceRead , SpiDeviceWrite ,
48- } ;
48+ use embedded_hal:: spi:: { Operation as SpiOperation , SpiBus , SpiDevice } ;
4949 use spidev:: SpidevTransfer ;
50+ use std:: convert:: TryInto ;
5051 use std:: io:: { Read , Write } ;
5152
5253 impl ErrorType for Spidev {
5354 type Error = SPIError ;
5455 }
5556
56- impl SpiBusFlush for Spidev {
57- fn flush ( & mut self ) -> Result < ( ) , Self :: Error > {
58- self . 0 . flush ( ) . map_err ( |err| SPIError { err } )
59- }
60- }
61-
62- impl SpiBusRead < u8 > for Spidev {
57+ impl SpiBus < u8 > for Spidev {
6358 fn read ( & mut self , words : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
6459 self . 0 . read_exact ( words) . map_err ( |err| SPIError { err } )
6560 }
66- }
6761
68- impl SpiBusWrite < u8 > for Spidev {
6962 fn write ( & mut self , words : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
7063 self . 0 . write_all ( words) . map_err ( |err| SPIError { err } )
7164 }
72- }
7365
74- impl SpiBus < u8 > for Spidev {
7566 fn transfer ( & mut self , read : & mut [ u8 ] , write : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
7667 self . 0
7768 . transfer ( & mut SpidevTransfer :: read_write ( write, read) )
@@ -84,37 +75,19 @@ mod embedded_hal_impl {
8475 . transfer ( & mut SpidevTransfer :: read_write ( & tx, words) )
8576 . map_err ( |err| SPIError { err } )
8677 }
87- }
88-
89- impl SpiDeviceRead for Spidev {
90- fn read_transaction ( & mut self , operations : & mut [ & mut [ u8 ] ] ) -> Result < ( ) , Self :: Error > {
91- let mut transfers: Vec < _ > = operations
92- . iter_mut ( )
93- . map ( |op| SpidevTransfer :: read ( op) )
94- . collect ( ) ;
95- self . 0
96- . transfer_multiple ( & mut transfers)
97- . map_err ( |err| SPIError { err } ) ?;
98- self . flush ( ) ?;
99- Ok ( ( ) )
100- }
101- }
10278
103- impl SpiDeviceWrite for Spidev {
104- fn write_transaction ( & mut self , operations : & [ & [ u8 ] ] ) -> Result < ( ) , Self :: Error > {
105- let mut transfers: Vec < _ > = operations
106- . iter ( )
107- . map ( |op| SpidevTransfer :: write ( op) )
108- . collect ( ) ;
109- self . 0
110- . transfer_multiple ( & mut transfers)
111- . map_err ( |err| SPIError { err } ) ?;
112- self . flush ( ) ?;
113- Ok ( ( ) )
79+ fn flush ( & mut self ) -> Result < ( ) , Self :: Error > {
80+ self . 0 . flush ( ) . map_err ( |err| SPIError { err } )
11481 }
11582 }
11683
11784 impl SpiDevice for Spidev {
85+ ///Perform a transaction against the device. [Read more][transaction]
86+ ///
87+ /// [Delay operations][delay] are capped to 65535 microseconds.
88+ ///
89+ /// [transaction]: SpiDevice::transaction
90+ /// [delay]: SpiOperation::DelayUs
11891 fn transaction (
11992 & mut self ,
12093 operations : & mut [ SpiOperation < ' _ , u8 > ] ,
@@ -132,6 +105,9 @@ mod embedded_hal_impl {
132105 } ;
133106 SpidevTransfer :: read_write ( tx, buf)
134107 }
108+ SpiOperation :: DelayUs ( us) => {
109+ SpidevTransfer :: delay ( ( * us) . try_into ( ) . unwrap_or ( u16:: MAX ) )
110+ }
135111 } )
136112 . collect ( ) ;
137113 self . 0
@@ -163,6 +139,7 @@ impl From<io::Error> for SPIError {
163139}
164140
165141impl embedded_hal:: spi:: Error for SPIError {
142+ #[ allow( clippy:: match_single_binding) ]
166143 fn kind ( & self ) -> embedded_hal:: spi:: ErrorKind {
167144 use embedded_hal:: spi:: ErrorKind ;
168145 // TODO: match any errors here if we can find any that are relevant
0 commit comments