diff --git a/.cargo/config b/.cargo/config.toml similarity index 100% rename from .cargo/config rename to .cargo/config.toml diff --git a/Cargo.toml b/Cargo.toml index d6c066e..49768a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ description = "libusb based driver for the CP2130 USB-SPI bridge by Silicon Labs repository = "https://github.com/ryankurte/rust-driver-cp2130" keywords = [ "driver", "cp2130", "usb", "spi", "embedded" ] license = "MPL-2.0" -edition = "2018" +edition = "2021" [features] util = [ "clap", "simplelog", "rand", "hex" ] @@ -14,14 +14,14 @@ examples = [] default = [ "util" ] [dependencies] -embedded-hal = { version = "1.0.0-rc.1" } +embedded-hal = { version = "1.0.0" } libc = "0.2.66" log = "0.4.8" bitflags = "1.2.1" byteorder = "1.3.2" lazy_static = "1.4.0" -failure = "0.1.7" +thiserror = "1.0.58" rusb = "0.9.0" clap = { version = "4.4.7", optional = true, features = [ "derive", "env" ] } @@ -30,10 +30,10 @@ hex = { version = "0.4.2", optional = true } rand = { version = "0.8.0", optional = true } [dev-dependencies] -ssd1306 = "0.7.0" -embedded-graphics = "0.7.1" -linux-embedded-hal = "0.3.0" -embedded-hal-compat = "0.4.0" +ssd1306 = "0.8.4" +embedded-graphics = "0.8.1" +linux-embedded-hal = "0.4.0" +#embedded-hal-compat = "0.12.0" [[bin]] name = "cp2130-util" diff --git a/src/device.rs b/src/device.rs index b0a16a9..c1e157d 100644 --- a/src/device.rs +++ b/src/device.rs @@ -8,6 +8,7 @@ use std::str::FromStr; use byteorder::{LE, BE, ByteOrder}; use bitflags::bitflags; +use log::{trace, debug, error}; use rusb::{Device as UsbDevice, Context as UsbContext, DeviceDescriptor, DeviceHandle, Direction, TransferType}; @@ -155,7 +156,7 @@ pub(crate) struct Inner { /// TODO: given it's one device this could all be hard-coded #[derive(Debug)] struct Endpoints { - control: Endpoint, + _control: Endpoint, read: Endpoint, write: Endpoint, } @@ -328,7 +329,7 @@ impl Inner { handle.set_active_configuration(read.config)?; // Build endpoints - let endpoints = Endpoints{control, write, read}; + let endpoints = Endpoints{_control: control, write, read}; Ok((Inner{_device: device, handle, endpoints, gpio_allocated: [false; 11], spi_clock: SpiClock::Clock12Mhz}, info)) } } diff --git a/src/lib.rs b/src/lib.rs index f983fd3..2bc8ffe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,14 +5,6 @@ use std::{sync::{Arc, Mutex}, time::{Instant, Duration}}; -#[macro_use] -extern crate log; - -#[macro_use] -extern crate lazy_static; - -use failure::Fail; - pub use embedded_hal::spi::{Mode as SpiMode}; use rusb::{Device as UsbDevice, Context as UsbContext, DeviceDescriptor}; @@ -24,24 +16,24 @@ pub use crate::device::{UsbOptions, GpioMode, GpioLevel, SpiConfig, SpiClock}; use crate::device::*; -#[derive(Debug, Fail)] +#[derive(Debug, thiserror::Error)] pub enum Error { // Io(IoError), - #[fail(display = "USB error: {:?}", 0)] + #[error("USB error: {0}")] Usb(rusb::Error), - #[fail(display = "No matching endpoint languages found")] + #[error("No matching endpoint languages found")] NoLanguages, - #[fail(display = "No valid endpoint configuration found")] + #[error("No valid endpoint configuration found")] Configurations, - #[fail(display = "No matching endpoint found")] + #[error("No matching endpoint found")] Endpoint, - #[fail(display = "GPIO pin already in use")] + #[error("GPIO pin already in use")] GpioInUse, - #[fail(display = "Invalid SPI index")] + #[error("Invalid SPI index")] InvalidIndex, - #[fail(display = "Invalid SPI baud rate")] + #[error("Invalid SPI baud rate")] InvalidBaud, } @@ -200,9 +192,9 @@ impl embedded_hal::spi::SpiDevice for Spi { SpiOp::Transfer(r, w) => self.transfer(r, w)?, SpiOp::TransferInPlace(b) => self.transfer_in_place(b)?, SpiOp::Read(r) => self.read(r)?, - SpiOp::DelayUs(us) => { + SpiOp::DelayNs(ns) => { let now = Instant::now(); - while now.elapsed() < Duration::from_micros(*us as u64) {} + while now.elapsed() < Duration::from_nanos(*ns as u64) {} } } } @@ -250,11 +242,11 @@ pub struct InputPin { } impl embedded_hal::digital::InputPin for InputPin { - fn is_high(&self) -> Result { + fn is_high(&mut self) -> Result { self.inner.lock().unwrap().get_gpio_level(self.index) } - fn is_low(&self) -> Result { + fn is_low(&mut self) -> Result { let v = self.is_high()?; Ok(!v) } diff --git a/src/manager.rs b/src/manager.rs index bd27d8f..a90f927 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -11,10 +11,12 @@ use std::num::ParseIntError; #[cfg(feature = "clap")] use clap::Parser; +use log::{trace, debug, error}; + use crate::Error; use crate::device::{VID, PID}; -lazy_static!{ +lazy_static::lazy_static!{ // LibUSB context created automagically static ref CONTEXT: UsbContext = { UsbContext::new().unwrap()