1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#![allow(dead_code)]
use nix::sys::ioctl;
use std::mem;
use std::io;
use std::os::unix::prelude::*;
use super::SpiModeFlags;
const SPI_IOC_MAGIC: u8 = 'k' as u8;
const SPI_IOC_NR_TRANSFER: u8 = 0;
const SPI_IOC_NR_MODE: u8 = 1;
const SPI_IOC_NR_LSB_FIRST: u8 = 2;
const SPI_IOC_NR_BITS_PER_WORD: u8 = 3;
const SPI_IOC_NR_MAX_SPEED_HZ: u8 = 4;
const SPI_IOC_NR_MODE32: u8 = 5;
fn from_nix_error(err: ::nix::Error) -> io::Error {
io::Error::from_raw_os_error(err.errno() as i32)
}
fn from_nix_result<T>(res: ::nix::Result<T>) -> io::Result<T> {
match res {
Ok(r) => Ok(r),
Err(err) => Err(from_nix_error(err)),
}
}
#[allow(non_camel_case_types)]
#[derive(Debug)]
struct spi_ioc_transfer {
pub tx_buf: u64,
pub rx_buf: u64,
pub len: u32,
pub speed_hz: u32,
pub delay_usecs: u16,
pub bits_per_word: u8,
pub cs_change: u8,
pub pad: u32,
}
#[derive(Default)]
pub struct SpidevTransfer {
pub tx_buf: Option<Box<[u8]>>,
pub rx_buf: Option<Box<[u8]>>,
len: u32,
speed_hz: u32,
delay_usecs: u16,
bits_per_word: u8,
cs_change: u8,
pad: u32,
}
impl SpidevTransfer {
pub fn read(length: usize) -> SpidevTransfer {
SpidevTransfer {
tx_buf: None,
rx_buf: Some(vec![0u8; length].into_boxed_slice()),
len: length as u32,
..Default::default()
}
}
pub fn write(tx_buf: &[u8]) -> SpidevTransfer {
let len = tx_buf.len();
let rx_buf_vec: Vec<u8> = vec![0; len];
let mut tx_buf_vec = Vec::with_capacity(len);
for i in 0..len {
tx_buf_vec.push(tx_buf[i]);
}
SpidevTransfer {
tx_buf: Some(tx_buf_vec.into_boxed_slice()),
rx_buf: Some(rx_buf_vec.into_boxed_slice()),
len: tx_buf.len() as u32,
..Default::default()
}
}
fn as_spi_ioc_transfer(&self) -> spi_ioc_transfer {
spi_ioc_transfer {
tx_buf: match self.tx_buf {
Some(ref bufbox) => bufbox.as_ptr() as u64,
None => 0,
},
rx_buf: match self.rx_buf {
Some(ref bufbox) => bufbox.as_ptr() as u64,
None => 0,
},
len: self.len,
speed_hz: self.speed_hz,
delay_usecs: self.delay_usecs,
bits_per_word: self.bits_per_word,
cs_change: self.cs_change,
pad: self.pad,
}
}
}
fn spidev_ioc_read<T>(fd: RawFd, nr: u8) -> io::Result<T> {
let size = mem::size_of::<T>();
let op = ioctl::op_read(SPI_IOC_MAGIC, nr, size);
from_nix_result(unsafe { ioctl::read(fd, op) })
}
fn spidev_ioc_write<T>(fd: RawFd, nr: u8, data: &T) -> io::Result<()> {
let size = mem::size_of::<T>();
let op = ioctl::op_write(SPI_IOC_MAGIC, nr, size);
try!(from_nix_result(unsafe { ioctl::write(fd, op, data) }));
Ok(())
}
pub fn get_mode(fd: RawFd) -> io::Result<u8> {
spidev_ioc_read::<u8>(fd, SPI_IOC_NR_MODE)
}
pub fn set_mode(fd: RawFd, mode: SpiModeFlags) -> io::Result<()> {
if (mode.bits & 0xFFFFFF00) != 0 {
spidev_ioc_write::<u32>(fd, SPI_IOC_NR_MODE32, &mode.bits)
} else {
let bits: u8 = mode.bits as u8;
spidev_ioc_write::<u8>(fd, SPI_IOC_NR_MODE, &bits)
}
}
pub fn get_lsb_first(fd: RawFd) -> io::Result<bool> {
Ok(try!(spidev_ioc_read::<u8>(fd, SPI_IOC_NR_LSB_FIRST)) != 0)
}
pub fn set_lsb_first(fd: RawFd, lsb_first: bool) -> io::Result<()> {
let lsb_first_value: u8 = if lsb_first { 1 } else { 0 };
spidev_ioc_write(fd, SPI_IOC_NR_LSB_FIRST, &lsb_first_value)
}
pub fn get_bits_per_word(fd: RawFd) -> io::Result<u8> {
spidev_ioc_read::<u8>(fd, SPI_IOC_NR_BITS_PER_WORD)
}
pub fn set_bits_per_word(fd: RawFd, bits_per_word: u8) -> io::Result<()> {
spidev_ioc_write(fd, SPI_IOC_NR_BITS_PER_WORD, &bits_per_word)
}
pub fn get_max_speed_hz(fd: RawFd) -> io::Result<u32> {
spidev_ioc_read::<u32>(fd, SPI_IOC_NR_MAX_SPEED_HZ)
}
pub fn set_max_speed_hz(fd: RawFd, max_speed_hz: u32) -> io::Result<()> {
spidev_ioc_write(fd, SPI_IOC_NR_MAX_SPEED_HZ, &max_speed_hz)
}
pub fn transfer(fd: RawFd, transfer: &mut SpidevTransfer) -> io::Result<()> {
let mut raw_transfer = transfer.as_spi_ioc_transfer();
let op = ioctl::op_write(
SPI_IOC_MAGIC,
SPI_IOC_NR_TRANSFER,
1 * mem::size_of::<spi_ioc_transfer>());
try!(from_nix_result(unsafe {
ioctl::read_into(fd, op, &mut raw_transfer)
}));
Ok(())
}
pub fn transfer_multiple(fd: RawFd, transfers: &Vec<SpidevTransfer>) -> io::Result<()> {
let mut raw_transfers_vec: Vec<spi_ioc_transfer> = Vec::with_capacity(transfers.len());
for transfer in transfers.iter() {
raw_transfers_vec.push(transfer.as_spi_ioc_transfer());
}
let mut raw_transfers = raw_transfers_vec.into_boxed_slice();
let tot_size = raw_transfers.len() * mem::size_of::<spi_ioc_transfer>();
let op = ioctl::op_write(
SPI_IOC_MAGIC,
SPI_IOC_NR_TRANSFER,
tot_size);
try!(from_nix_result(unsafe {
ioctl::read_into_ptr(fd, op, raw_transfers.as_mut_ptr())
}));
Ok(())
}