@@ -40,71 +40,60 @@ impl ops::DerefMut for Spidev {
4040
4141mod embedded_hal_impl {
4242 use super :: * ;
43- use embedded_hal:: spi:: blocking:: {
44- Operation as SpiOperation , Transactional , Transfer , TransferInplace , Write ,
45- } ;
43+ use embedded_hal:: spi:: blocking:: { SpiBus , SpiBusFlush , SpiBusRead , SpiBusWrite , SpiDevice } ;
4644 use embedded_hal:: spi:: ErrorType ;
4745 use spidev:: SpidevTransfer ;
48- use std:: io:: Write as _ ;
46+ use std:: io:: { Read , Write } ;
4947
5048 impl ErrorType for Spidev {
5149 type Error = SPIError ;
5250 }
5351
54- impl Transfer < u8 > for Spidev {
55- fn transfer < ' b > ( & mut self , read : & ' b mut [ u8 ] , write : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
56- self . 0
57- . transfer ( & mut SpidevTransfer :: read_write ( & write, read) )
58- . map_err ( |err| SPIError { err } )
52+ impl SpiBusFlush for Spidev {
53+ fn flush ( & mut self ) -> Result < ( ) , Self :: Error > {
54+ self . 0 . flush ( ) . map_err ( |err| SPIError { err } )
5955 }
6056 }
6157
62- impl TransferInplace < u8 > for Spidev {
63- fn transfer_inplace < ' b > ( & mut self , buffer : & ' b mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
64- let tx = buffer. to_owned ( ) ;
65- self . 0
66- . transfer ( & mut SpidevTransfer :: read_write ( & tx, buffer) )
67- . map_err ( |err| SPIError { err } )
58+ impl SpiBusRead < u8 > for Spidev {
59+ fn read ( & mut self , words : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
60+ self . 0 . read_exact ( words) . map_err ( |err| SPIError { err } )
6861 }
6962 }
7063
71- impl Write < u8 > for Spidev {
72- fn write ( & mut self , buffer : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
73- self . 0 . write_all ( buffer ) . map_err ( |err| SPIError { err } )
64+ impl SpiBusWrite < u8 > for Spidev {
65+ fn write ( & mut self , words : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
66+ self . 0 . write_all ( words ) . map_err ( |err| SPIError { err } )
7467 }
7568 }
7669
77- /// Transactional implementation batches SPI operations into a single transaction
78- impl Transactional < u8 > for Spidev {
79- fn exec < ' a > ( & mut self , operations : & mut [ SpiOperation < ' a , u8 > ] ) -> Result < ( ) , Self :: Error > {
80- // Map types from generic to linux objects
81- let mut messages: Vec < _ > = operations
82- . iter_mut ( )
83- . map ( |a| {
84- match a {
85- SpiOperation :: Read ( w) => SpidevTransfer :: read ( w) ,
86- SpiOperation :: Write ( w) => SpidevTransfer :: write ( w) ,
87- SpiOperation :: Transfer ( r, w) => SpidevTransfer :: read_write ( w, r) ,
88- SpiOperation :: TransferInplace ( r) => {
89- // Clone read to write pointer
90- // SPIdev is okay with having w == r but this is tricky to achieve in safe rust
91- let w = unsafe {
92- let p = r. as_ptr ( ) ;
93- std:: slice:: from_raw_parts ( p, r. len ( ) )
94- } ;
95-
96- SpidevTransfer :: read_write ( w, r)
97- }
98- }
99- } )
100- . collect ( ) ;
101-
102- // Execute transfer
70+ impl SpiBus < u8 > for Spidev {
71+ fn transfer ( & mut self , read : & mut [ u8 ] , write : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
72+ self . 0
73+ . transfer ( & mut SpidevTransfer :: read_write ( write, read) )
74+ . map_err ( |err| SPIError { err } )
75+ }
76+
77+ fn transfer_in_place ( & mut self , words : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
78+ let tx = words. to_owned ( ) ;
10379 self . 0
104- . transfer_multiple ( & mut messages )
80+ . transfer ( & mut SpidevTransfer :: read_write ( & tx , words ) )
10581 . map_err ( |err| SPIError { err } )
10682 }
10783 }
84+
85+ impl SpiDevice for Spidev {
86+ type Bus = Spidev ;
87+
88+ fn transaction < R > (
89+ & mut self ,
90+ f : impl FnOnce ( & mut Self :: Bus ) -> Result < R , <Self :: Bus as ErrorType >:: Error > ,
91+ ) -> Result < R , Self :: Error > {
92+ let result = f ( self ) ?;
93+ self . flush ( ) ?;
94+ Ok ( result)
95+ }
96+ }
10897}
10998
11099/// Error type wrapping [io::Error](io::Error) to implement [embedded_hal::spi::ErrorKind]
0 commit comments