Skip to content

Commit fa2569f

Browse files
authored
Merge pull request #16 from posborne/use-upstream-nix
Use upstream nix
2 parents 97ef13b + 0b89f8b commit fa2569f

File tree

6 files changed

+52
-50
lines changed

6 files changed

+52
-50
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: rust
22
sudo: false
33
rust:
4-
- 1.22.1
4+
- 1.26.0 # MSRV
55
- stable
66
- beta
77
- nightly

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "spidev"
4-
version = "0.3.0"
4+
version = "0.4.0"
55
authors = ["Paul Osborne <[email protected]>"]
66
license = "MIT/Apache-2.0"
77
repository = "https://github.com/rust-embedded/rust-spidev"
@@ -14,6 +14,6 @@ half-duplex SPI access, and full-duplex SPI access.
1414
"""
1515

1616
[dependencies]
17-
libc = "0.2.2"
18-
bitflags = "0.3.3"
19-
nix = "0.6.0"
17+
libc = "0.2"
18+
bitflags = "1.0"
19+
nix = "0.14"

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ The following features are implemented and planned for the library:
7474
- [x] Support for configuring spidev device
7575
- [ ] Support for querying spidev configuration state
7676

77+
## Minimum Supported Rust Version (MSRV)
78+
79+
This crate is guaranteed to compile on stable Rust 1.26 and up. It *might*
80+
compile with older versions but that may change in any new patch release.
81+
7782
## Cross Compiling
7883

7984
Most likely, the machine you are running on is not your development

examples/spidev-bidir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
extern crate spidev;
2-
use spidev::{Spidev, SpidevOptions, SPI_MODE_0};
2+
use spidev::{Spidev, SpidevOptions, SpiModeFlags};
33
use spidev::spidevioctl::SpidevTransfer;
44

55
fn main() {
@@ -8,7 +8,7 @@ fn main() {
88
.bits_per_word(8)
99
.max_speed_hz(5000)
1010
.lsb_first(false)
11-
.mode(SPI_MODE_0)
11+
.mode(SpiModeFlags::SPI_MODE_0)
1212
.build();
1313
spidev.configure(&options).unwrap();
1414

src/lib.rs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
//! extern crate spidev;
2626
//! use std::io;
2727
//! use std::io::prelude::*;
28-
//! use spidev::{Spidev, SpidevOptions, SpidevTransfer, SPI_MODE_0};
28+
//! use spidev::{Spidev, SpidevOptions, SpidevTransfer, SpiModeFlags};
2929
//!
3030
//! fn create_spi() -> io::Result<Spidev> {
3131
//! let mut spi = try!(Spidev::open("/dev/spidev0.0"));
3232
//! let options = SpidevOptions::new()
3333
//! .bits_per_word(8)
3434
//! .max_speed_hz(20_000)
35-
//! .mode(SPI_MODE_0)
35+
//! .mode(SpiModeFlags::SPI_MODE_0)
3636
//! .build();
3737
//! try!(spi.configure(&options));
3838
//! Ok(spi)
@@ -85,40 +85,40 @@ use std::os::unix::prelude::*;
8585

8686
// Constants extracted from linux/spi/spidev.h
8787
bitflags! {
88-
flags SpiModeFlags: u32 {
88+
pub struct SpiModeFlags: u32 {
8989
/// Clock Phase
90-
const SPI_CPHA = 0x01,
90+
const SPI_CPHA = 0x01;
9191
/// Clock Polarity
92-
const SPI_CPOL = 0x02,
92+
const SPI_CPOL = 0x02;
9393
/// Chipselect Active High?
94-
const SPI_CS_HIGH = 0x04,
94+
const SPI_CS_HIGH = 0x04;
9595
/// Per-word Bits On Wire
96-
const SPI_LSB_FIRST = 0x08,
96+
const SPI_LSB_FIRST = 0x08;
9797
/// SI/SO Signals Shared
98-
const SPI_3WIRE = 0x10,
98+
const SPI_3WIRE = 0x10;
9999
/// Loopback Mode
100-
const SPI_LOOP = 0x20,
101-
/// 1 dev/bus, no chipselect
102-
const SPI_NO_CS = 0x40,
100+
const SPI_LOOP = 0x20;
101+
/// 1 dev/bus; no chipselect
102+
const SPI_NO_CS = 0x40;
103103
/// Slave pulls low to pause
104-
const SPI_READY = 0x80,
104+
const SPI_READY = 0x80;
105105

106106
// Common Configurations
107-
const SPI_MODE_0 = 0x00,
108-
const SPI_MODE_1 = SPI_CPHA.bits,
109-
const SPI_MODE_2 = SPI_CPOL.bits,
110-
const SPI_MODE_3 = (SPI_CPOL.bits | SPI_CPHA.bits),
107+
const SPI_MODE_0 = 0x00;
108+
const SPI_MODE_1 = Self::SPI_CPHA.bits;
109+
const SPI_MODE_2 = Self::SPI_CPOL.bits;
110+
const SPI_MODE_3 = (Self::SPI_CPOL.bits | Self::SPI_CPHA.bits);
111111

112112
// == Only Supported with 32-bits ==
113113

114114
/// Transmit with 2 wires
115-
const SPI_TX_DUAL = 0x100,
115+
const SPI_TX_DUAL = 0x100;
116116
/// Transmit with 4 wires
117-
const SPI_TX_QUAD = 0x200,
117+
const SPI_TX_QUAD = 0x200;
118118
/// Receive with 2 wires
119-
const SPI_RX_DUAL = 0x400,
119+
const SPI_RX_DUAL = 0x400;
120120
/// Receive with 4 wires
121-
const SPI_RX_QUAD = 0x800,
121+
const SPI_RX_QUAD = 0x800;
122122
}
123123
}
124124

@@ -290,21 +290,20 @@ impl Write for Spidev {
290290

291291
#[cfg(test)]
292292
mod test {
293-
294-
use super::{SpidevOptions, SPI_MODE_0};
293+
use super::{SpidevOptions, SpiModeFlags};
295294

296295
#[test]
297296
fn test_spidev_options_all() {
298297
let options = SpidevOptions::new()
299298
.bits_per_word(8)
300299
.max_speed_hz(20_000)
301300
.lsb_first(false)
302-
.mode(SPI_MODE_0)
301+
.mode(SpiModeFlags::SPI_MODE_0)
303302
.build();
304303
assert_eq!(options.bits_per_word, Some(8));
305304
assert_eq!(options.max_speed_hz, Some(20_000));
306305
assert_eq!(options.lsb_first, Some(false));
307-
assert_eq!(options.spi_mode, Some(SPI_MODE_0));
306+
assert_eq!(options.spi_mode, Some(SpiModeFlags::SPI_MODE_0));
308307
}
309308

310309
#[test]

src/spidevioctl.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
#![allow(dead_code)]
1010

11+
use nix;
1112
use std::io;
1213
use std::marker::PhantomData;
13-
use std::mem;
1414
use std::os::unix::prelude::*;
1515
use super::SpiModeFlags;
1616

1717
fn from_nix_error(err: ::nix::Error) -> io::Error {
18-
io::Error::from_raw_os_error(err.errno() as i32)
18+
io::Error::from_raw_os_error(err.as_errno().unwrap_or_else(|| nix::errno::Errno::UnknownErrno) as i32)
1919
}
2020

2121
fn from_nix_result<T>(res: ::nix::Result<T>) -> io::Result<T> {
@@ -123,26 +123,26 @@ mod ioctl {
123123
const SPI_IOC_NR_MAX_SPEED_HZ: u8 = 4;
124124
const SPI_IOC_NR_MODE32: u8 = 5;
125125

126-
ioctl!(read get_mode_u8 with SPI_IOC_MAGIC, SPI_IOC_NR_MODE; u8);
127-
ioctl!(read get_mode_u32 with SPI_IOC_MAGIC, SPI_IOC_NR_MODE; u32);
128-
ioctl!(write set_mode_u8 with SPI_IOC_MAGIC, SPI_IOC_NR_MODE; u8);
129-
ioctl!(write set_mode_u32 with SPI_IOC_MAGIC, SPI_IOC_NR_MODE32; u32);
126+
ioctl_read!(get_mode_u8, SPI_IOC_MAGIC, SPI_IOC_NR_MODE, u8);
127+
ioctl_read!(get_mode_u32, SPI_IOC_MAGIC, SPI_IOC_NR_MODE32, u32);
128+
ioctl_write_ptr!(set_mode, SPI_IOC_MAGIC, SPI_IOC_NR_MODE, u8);
129+
ioctl_write_ptr!(set_mode32, SPI_IOC_MAGIC, SPI_IOC_NR_MODE32, u32);
130130

131-
ioctl!(read get_lsb_first with SPI_IOC_MAGIC, SPI_IOC_NR_LSB_FIRST; u8);
132-
ioctl!(write set_lsb_first with SPI_IOC_MAGIC, SPI_IOC_NR_LSB_FIRST; u8);
131+
ioctl_read!(get_lsb_first, SPI_IOC_MAGIC, SPI_IOC_NR_LSB_FIRST, u8);
132+
ioctl_write_ptr!(set_lsb_first, SPI_IOC_MAGIC, SPI_IOC_NR_LSB_FIRST, u8);
133133

134-
ioctl!(read get_bits_per_word with SPI_IOC_MAGIC, SPI_IOC_NR_BITS_PER_WORD; u8);
135-
ioctl!(write set_bits_per_word with SPI_IOC_MAGIC, SPI_IOC_NR_BITS_PER_WORD; u8);
134+
ioctl_read!(get_bits_per_word, SPI_IOC_MAGIC, SPI_IOC_NR_BITS_PER_WORD, u8);
135+
ioctl_write_ptr!(set_bits_per_word, SPI_IOC_MAGIC, SPI_IOC_NR_BITS_PER_WORD, u8);
136136

137-
ioctl!(read get_max_speed_hz with SPI_IOC_MAGIC, SPI_IOC_NR_MAX_SPEED_HZ; u32);
138-
ioctl!(write set_max_speed_hz with SPI_IOC_MAGIC, SPI_IOC_NR_MAX_SPEED_HZ; u32);
137+
ioctl_read!(get_max_speed_hz, SPI_IOC_MAGIC, SPI_IOC_NR_MAX_SPEED_HZ, u32);
138+
ioctl_write_ptr!(set_max_speed_hz, SPI_IOC_MAGIC, SPI_IOC_NR_MAX_SPEED_HZ, u32);
139139

140140
// NOTE: this macro works for single transfers but cannot properly
141141
// calculate size for multi transfer whose length we will not know
142142
// until runtime. We fallback to using the underlying ioctl for that
143143
// use case.
144-
ioctl!(write spidev_transfer with SPI_IOC_MAGIC, SPI_IOC_NR_TRANSFER; spi_ioc_transfer);
145-
ioctl!(write buf spidev_transfer_buf with SPI_IOC_MAGIC, SPI_IOC_NR_TRANSFER; spi_ioc_transfer);
144+
ioctl_write_ptr!(spidev_transfer, SPI_IOC_MAGIC, SPI_IOC_NR_TRANSFER, spi_ioc_transfer);
145+
ioctl_write_buf!(spidev_transfer_buf, SPI_IOC_MAGIC, SPI_IOC_NR_TRANSFER, spi_ioc_transfer);
146146
}
147147

148148
/// Representation of a spidev transfer that is shared
@@ -161,10 +161,10 @@ pub fn set_mode(fd: RawFd, mode: SpiModeFlags) -> io::Result<()> {
161161
// added until later kernels. This provides a reasonable story
162162
// for forwards and backwards compatibility
163163
if (mode.bits & 0xFFFFFF00) != 0 {
164-
try!(from_nix_result(unsafe { ioctl::set_mode_u32(fd, &mode.bits) }));
164+
try!(from_nix_result(unsafe { ioctl::set_mode32(fd, &mode.bits) }));
165165
} else {
166166
let bits: u8 = mode.bits as u8;
167-
try!(from_nix_result(unsafe { ioctl::set_mode_u8(fd, &bits) }));
167+
try!(from_nix_result(unsafe { ioctl::set_mode(fd, &bits) }));
168168
}
169169
Ok(())
170170
}
@@ -215,10 +215,8 @@ pub fn transfer(fd: RawFd, transfer: &mut SpidevTransfer) -> io::Result<()> {
215215
}
216216

217217
pub fn transfer_multiple(fd: RawFd, transfers: &mut [SpidevTransfer]) -> io::Result<()> {
218-
let tot_size = transfers.len() * mem::size_of::<SpidevTransfer>();
219-
220218
try!(from_nix_result(unsafe {
221-
ioctl::spidev_transfer_buf(fd, transfers.as_mut_ptr(), tot_size)
219+
ioctl::spidev_transfer_buf(fd, transfers)
222220
}));
223221
Ok(())
224222
}

0 commit comments

Comments
 (0)