12
12
13
13
#![ deny( missing_docs) ]
14
14
15
- extern crate cast;
16
- extern crate core;
17
- extern crate embedded_hal as hal;
18
- pub extern crate i2cdev;
19
- pub extern crate nb;
20
- pub extern crate serial_core;
21
- pub extern crate serial_unix;
22
- pub extern crate spidev;
15
+ use cast;
16
+ pub use i2cdev;
17
+ pub use nb;
18
+ pub use serial_core;
19
+ pub use serial_unix;
20
+ pub use spidev;
23
21
24
22
#[ cfg( feature = "gpio_sysfs" ) ]
25
- pub extern crate sysfs_gpio;
23
+ pub use sysfs_gpio;
26
24
27
25
#[ cfg( feature = "gpio_cdev" ) ]
28
- pub extern crate gpio_cdev;
26
+ pub use gpio_cdev;
27
+
29
28
30
29
use core:: convert:: Infallible ;
31
30
use std:: io:: { self , Write } ;
@@ -34,7 +33,7 @@ use std::time::Duration;
34
33
use std:: { ops, thread} ;
35
34
36
35
use cast:: { u32, u64} ;
37
- use hal :: blocking:: i2c:: Operation as I2cOperation ;
36
+ use embedded_hal :: blocking:: i2c:: Operation as I2cOperation ;
38
37
use i2cdev:: core:: { I2CDevice , I2CMessage , I2CTransfer } ;
39
38
use i2cdev:: linux:: LinuxI2CMessage ;
40
39
use spidev:: SpidevTransfer ;
@@ -63,7 +62,7 @@ pub use sysfs_pin::SysfsPin;
63
62
/// Empty struct that provides delay functionality on top of `thread::sleep`
64
63
pub struct Delay ;
65
64
66
- impl hal :: blocking:: delay:: DelayUs < u8 > for Delay {
65
+ impl embedded_hal :: blocking:: delay:: DelayUs < u8 > for Delay {
67
66
type Error = Infallible ;
68
67
69
68
fn try_delay_us ( & mut self , n : u8 ) -> Result < ( ) , Self :: Error > {
@@ -72,7 +71,7 @@ impl hal::blocking::delay::DelayUs<u8> for Delay {
72
71
}
73
72
}
74
73
75
- impl hal :: blocking:: delay:: DelayUs < u16 > for Delay {
74
+ impl embedded_hal :: blocking:: delay:: DelayUs < u16 > for Delay {
76
75
type Error = Infallible ;
77
76
78
77
fn try_delay_us ( & mut self , n : u16 ) -> Result < ( ) , Self :: Error > {
@@ -81,7 +80,7 @@ impl hal::blocking::delay::DelayUs<u16> for Delay {
81
80
}
82
81
}
83
82
84
- impl hal :: blocking:: delay:: DelayUs < u32 > for Delay {
83
+ impl embedded_hal :: blocking:: delay:: DelayUs < u32 > for Delay {
85
84
type Error = Infallible ;
86
85
87
86
fn try_delay_us ( & mut self , n : u32 ) -> Result < ( ) , Self :: Error > {
@@ -93,7 +92,7 @@ impl hal::blocking::delay::DelayUs<u32> for Delay {
93
92
}
94
93
}
95
94
96
- impl hal :: blocking:: delay:: DelayUs < u64 > for Delay {
95
+ impl embedded_hal :: blocking:: delay:: DelayUs < u64 > for Delay {
97
96
type Error = Infallible ;
98
97
99
98
fn try_delay_us ( & mut self , n : u64 ) -> Result < ( ) , Self :: Error > {
@@ -105,7 +104,7 @@ impl hal::blocking::delay::DelayUs<u64> for Delay {
105
104
}
106
105
}
107
106
108
- impl hal :: blocking:: delay:: DelayMs < u8 > for Delay {
107
+ impl embedded_hal :: blocking:: delay:: DelayMs < u8 > for Delay {
109
108
type Error = Infallible ;
110
109
111
110
fn try_delay_ms ( & mut self , n : u8 ) -> Result < ( ) , Self :: Error > {
@@ -114,7 +113,7 @@ impl hal::blocking::delay::DelayMs<u8> for Delay {
114
113
}
115
114
}
116
115
117
- impl hal :: blocking:: delay:: DelayMs < u16 > for Delay {
116
+ impl embedded_hal :: blocking:: delay:: DelayMs < u16 > for Delay {
118
117
type Error = Infallible ;
119
118
120
119
fn try_delay_ms ( & mut self , n : u16 ) -> Result < ( ) , Self :: Error > {
@@ -123,7 +122,7 @@ impl hal::blocking::delay::DelayMs<u16> for Delay {
123
122
}
124
123
}
125
124
126
- impl hal :: blocking:: delay:: DelayMs < u32 > for Delay {
125
+ impl embedded_hal :: blocking:: delay:: DelayMs < u32 > for Delay {
127
126
type Error = Infallible ;
128
127
129
128
fn try_delay_ms ( & mut self , n : u32 ) -> Result < ( ) , Self :: Error > {
@@ -132,7 +131,7 @@ impl hal::blocking::delay::DelayMs<u32> for Delay {
132
131
}
133
132
}
134
133
135
- impl hal :: blocking:: delay:: DelayMs < u64 > for Delay {
134
+ impl embedded_hal :: blocking:: delay:: DelayMs < u64 > for Delay {
136
135
type Error = Infallible ;
137
136
138
137
fn try_delay_ms ( & mut self , n : u64 ) -> Result < ( ) , Self :: Error > {
@@ -175,7 +174,7 @@ impl I2cdev {
175
174
}
176
175
}
177
176
178
- impl hal :: blocking:: i2c:: Read for I2cdev {
177
+ impl embedded_hal :: blocking:: i2c:: Read for I2cdev {
179
178
type Error = i2cdev:: linux:: LinuxI2CError ;
180
179
181
180
fn try_read ( & mut self , address : u8 , buffer : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
@@ -184,7 +183,7 @@ impl hal::blocking::i2c::Read for I2cdev {
184
183
}
185
184
}
186
185
187
- impl hal :: blocking:: i2c:: Write for I2cdev {
186
+ impl embedded_hal :: blocking:: i2c:: Write for I2cdev {
188
187
type Error = i2cdev:: linux:: LinuxI2CError ;
189
188
190
189
fn try_write ( & mut self , address : u8 , bytes : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
@@ -193,7 +192,7 @@ impl hal::blocking::i2c::Write for I2cdev {
193
192
}
194
193
}
195
194
196
- impl hal :: blocking:: i2c:: WriteRead for I2cdev {
195
+ impl embedded_hal :: blocking:: i2c:: WriteRead for I2cdev {
197
196
type Error = i2cdev:: linux:: LinuxI2CError ;
198
197
199
198
fn try_write_read (
@@ -203,12 +202,15 @@ impl hal::blocking::i2c::WriteRead for I2cdev {
203
202
buffer : & mut [ u8 ] ,
204
203
) -> Result < ( ) , Self :: Error > {
205
204
self . set_address ( address) ?;
206
- let mut messages = [ LinuxI2CMessage :: write ( bytes) , LinuxI2CMessage :: read ( buffer) ] ;
205
+ let mut messages = [
206
+ LinuxI2CMessage :: write ( bytes) ,
207
+ LinuxI2CMessage :: read ( buffer) ,
208
+ ] ;
207
209
self . inner . transfer ( & mut messages) . map ( drop)
208
210
}
209
211
}
210
212
211
- impl hal :: blocking:: i2c:: Transactional for I2cdev {
213
+ impl embedded_hal :: blocking:: i2c:: Transactional for I2cdev {
212
214
type Error = i2cdev:: linux:: LinuxI2CError ;
213
215
214
216
fn try_exec ( & mut self , address : u8 , operations : & mut [ I2cOperation ] ) -> Result < ( ) , Self :: Error >
@@ -259,7 +261,7 @@ impl Spidev {
259
261
}
260
262
}
261
263
262
- impl hal :: blocking:: spi:: Transfer < u8 > for Spidev {
264
+ impl embedded_hal :: blocking:: spi:: Transfer < u8 > for Spidev {
263
265
type Error = io:: Error ;
264
266
265
267
fn try_transfer < ' b > ( & mut self , buffer : & ' b mut [ u8 ] ) -> io:: Result < & ' b [ u8 ] > {
@@ -270,14 +272,44 @@ impl hal::blocking::spi::Transfer<u8> for Spidev {
270
272
}
271
273
}
272
274
273
- impl hal :: blocking:: spi:: Write < u8 > for Spidev {
275
+ impl embedded_hal :: blocking:: spi:: Write < u8 > for Spidev {
274
276
type Error = io:: Error ;
275
277
276
278
fn try_write ( & mut self , buffer : & [ u8 ] ) -> io:: Result < ( ) > {
277
279
self . 0 . write_all ( buffer)
278
280
}
279
281
}
280
282
283
+ pub use embedded_hal:: blocking:: spi:: { Operation as SpiOperation } ;
284
+
285
+ /// Transactional implementation batches SPI operations into a single transaction
286
+ impl embedded_hal:: blocking:: spi:: Transactional < u8 > for Spidev {
287
+ type Error = io:: Error ;
288
+
289
+ fn try_exec < ' a > ( & mut self , operations : & mut [ SpiOperation < ' a , u8 > ] ) -> Result < ( ) , Self :: Error > {
290
+
291
+ // Map types from generic to linux objects
292
+ let mut messages: Vec < _ > = operations. iter_mut ( ) . map ( |a| {
293
+ match a {
294
+ SpiOperation :: Write ( w) => SpidevTransfer :: write ( w) ,
295
+ SpiOperation :: Transfer ( r) => {
296
+ // Clone read to write pointer
297
+ // SPIdev is okay with having w == r but this is tricky to achieve in safe rust
298
+ let w = unsafe {
299
+ let p = r. as_ptr ( ) ;
300
+ std:: slice:: from_raw_parts ( p, r. len ( ) )
301
+ } ;
302
+
303
+ SpidevTransfer :: read_write ( w, r)
304
+ } ,
305
+ }
306
+ } ) . collect ( ) ;
307
+
308
+ // Execute transfer
309
+ self . 0 . transfer_multiple ( & mut messages)
310
+ }
311
+ }
312
+
281
313
impl ops:: Deref for Spidev {
282
314
type Target = spidev:: Spidev ;
283
315
0 commit comments