@@ -10,13 +10,16 @@ use std::path::Path;
10
10
11
11
/// Newtype around [`spidev::Spidev`] that implements the `embedded-hal` traits
12
12
///
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
14
17
pub struct Spidev ( pub spidev:: Spidev ) ;
15
18
16
19
impl Spidev {
17
20
/// See [`spidev::Spidev::open`][0] for details.
18
21
///
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
20
23
pub fn open < P > ( path : P ) -> Result < Self , SPIError >
21
24
where
22
25
P : AsRef < Path > ,
@@ -42,36 +45,24 @@ impl ops::DerefMut for Spidev {
42
45
mod embedded_hal_impl {
43
46
use super :: * ;
44
47
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 } ;
49
49
use spidev:: SpidevTransfer ;
50
+ use std:: convert:: TryInto ;
50
51
use std:: io:: { Read , Write } ;
51
52
52
53
impl ErrorType for Spidev {
53
54
type Error = SPIError ;
54
55
}
55
56
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 {
63
58
fn read ( & mut self , words : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
64
59
self . 0 . read_exact ( words) . map_err ( |err| SPIError { err } )
65
60
}
66
- }
67
61
68
- impl SpiBusWrite < u8 > for Spidev {
69
62
fn write ( & mut self , words : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
70
63
self . 0 . write_all ( words) . map_err ( |err| SPIError { err } )
71
64
}
72
- }
73
65
74
- impl SpiBus < u8 > for Spidev {
75
66
fn transfer ( & mut self , read : & mut [ u8 ] , write : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
76
67
self . 0
77
68
. transfer ( & mut SpidevTransfer :: read_write ( write, read) )
@@ -84,37 +75,19 @@ mod embedded_hal_impl {
84
75
. transfer ( & mut SpidevTransfer :: read_write ( & tx, words) )
85
76
. map_err ( |err| SPIError { err } )
86
77
}
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
- }
102
78
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 } )
114
81
}
115
82
}
116
83
117
84
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
118
91
fn transaction (
119
92
& mut self ,
120
93
operations : & mut [ SpiOperation < ' _ , u8 > ] ,
@@ -132,6 +105,9 @@ mod embedded_hal_impl {
132
105
} ;
133
106
SpidevTransfer :: read_write ( tx, buf)
134
107
}
108
+ SpiOperation :: DelayUs ( us) => {
109
+ SpidevTransfer :: delay ( ( * us) . try_into ( ) . unwrap_or ( u16:: MAX ) )
110
+ }
135
111
} )
136
112
. collect ( ) ;
137
113
self . 0
@@ -163,6 +139,7 @@ impl From<io::Error> for SPIError {
163
139
}
164
140
165
141
impl embedded_hal:: spi:: Error for SPIError {
142
+ #[ allow( clippy:: match_single_binding) ]
166
143
fn kind ( & self ) -> embedded_hal:: spi:: ErrorKind {
167
144
use embedded_hal:: spi:: ErrorKind ;
168
145
// TODO: match any errors here if we can find any that are relevant
0 commit comments