Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating to embedded-hal 1.0 traits #373

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
539 changes: 295 additions & 244 deletions Cargo.lock

Large diffs are not rendered by default.

27 changes: 12 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ serde-json-core = "0.5"
cortex-m = "0.7.7"
cortex-m-rt = "0.7"
fugit = "0.3"
rtic-sync = "1"
rtic = {version = "2.1", features = ["thumbv7-backend"] }
rtic-monotonics = { version = "1.5", features = ["cortex-m-systick"] }
rand_core = "0.6"
Expand All @@ -41,11 +40,11 @@ cortex-m-log = { version = "0.8.0", features = ["log-integration"] }
log = "0.4.20"
heapless = { version = "0.7", features = ["serde"] }
bit_field = "0.10.2"
debounced-pin = "0.3.0"
debounced-pin = {git = "https://github.com/quartiq/rust-debounced-pin"}
serde = {version = "1.0", features = ["derive"], default-features = false }
rtt-logger = "0.2"
bbqueue = "0.5"
shared-bus = { version = "0.3", features = ["cortex-m"] }
embedded-hal-bus = { path = "../embedded-hal/embedded-hal-bus" }
usb-device = "0.3.2"
usbd-serial = "0.2.1"
encdec = { version = "0.9", default-features = false }
Expand All @@ -61,48 +60,46 @@ rtt-target = {version = "0.3", features=["cortex-m"]}
enum-iterator = { version = "1.4", default-features = false }
enc424j600 = { version = "0.3", features = ["cortex-m-cpu"] }
smoltcp-nal = { version = "0.4", features=["shared-stack"] }
stm32f4xx-hal = {version = "0.20.0", features = ["stm32f407", "usb_fs"] }
embedded-hal = "1"

serial-settings = {git = "https://github.com/quartiq/stabilizer"}
postcard = "1"

[build-dependencies]
built = { version = "0.7", features = ["git2"], default-features = false }

[dependencies.stm32f4xx-hal]
version = "0.20.0"
features = ["stm32f407", "usb_fs"]

[dependencies.ad5627]
path = "ad5627"
version = "0.1"
version = "0.2"

[dependencies.ads7924]
path = "ads7924"
version = "0.1"
version = "0.2"

[dependencies.dac7571]
path = "dac7571"
version = "0.1"
version = "0.2"

[dependencies.max6639]
path = "max6639"
version = "0.1"
version = "0.2"

[dependencies.max6642]
path = "max6642"
version = "0.1"
version = "0.2"

[dependencies.mcp3221]
path = "mcp3221"
version = "0.1"
version = "0.2"

[dependencies.microchip-24aa02e48]
path = "microchip-24aa02e48"
version = "0.1"
version = "0.2"

[dependencies.tca9548]
path = "tca9548"
version = "0.1"
version = "0.2"

[profile.dev]
# Note: Opt-level 1 is required to avoid issues with stack overflow during hardware configuration.
Expand Down
4 changes: 2 additions & 2 deletions ad5627/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ad5627"
version = "0.1.0"
version = "0.2.0"
authors = ["Ryan Summers <ryan.summers@vertigo-designs.com>"]
edition = "2018"
description = "no_std driver for the AD5627 2-channel digital-to-analog converter (DAC)"
Expand All @@ -10,4 +10,4 @@ repository = "https://github.com/quartiq/booster/"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
embedded-hal = "0.2.4"
embedded-hal = "1"
26 changes: 16 additions & 10 deletions ad5627/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
#![no_std]
#![deny(warnings)]

use embedded_hal::blocking::i2c::Write;
use embedded_hal::i2c::{ErrorType, I2c};

/// The maximum voltage that the DAC can output.
pub const MAX_VOLTAGE: f32 = 2.5;

/// The driver representing the programmable reference generator.
pub struct Ad5627<I2C>
where
I2C: Write,
{
pub struct Ad5627<I2C> {
i2c: I2C,
address: u8,
}
Expand Down Expand Up @@ -55,14 +52,14 @@ impl<E> From<E> for Error<E> {

impl<I2C> Ad5627<I2C>
where
I2C: Write,
I2C: I2c,
{
/// Construct a driver for the DAC.
///
/// # Args
/// * `i2c` - The I2C bus to communicate with the DAC.
/// * `address` - The 7-bit I2C address of the device.
pub fn new(i2c: I2C, address: u8) -> Result<Self, I2C::Error> {
pub fn new(i2c: I2C, address: u8) -> Result<Self, <I2C as ErrorType>::Error> {
let mut device = Ad5627 { i2c, address };

// Reset the DAC outputs.
Expand All @@ -74,7 +71,12 @@ where
Ok(device)
}

fn write(&mut self, command: Command, dac: Dac, payload: [u8; 2]) -> Result<(), I2C::Error> {
fn write(
&mut self,
command: Command,
dac: Dac,
payload: [u8; 2],
) -> Result<(), <I2C as ErrorType>::Error> {
// Construct the command byte.
let write: [u8; 3] = [((command as u8) << 3) | dac as u8, payload[0], payload[1]];

Expand All @@ -88,7 +90,7 @@ where
///
/// # Args
/// * `i2c` - The I2C bus to communicate with the DAC.
pub fn default(i2c: I2C) -> Result<Self, I2C::Error> {
pub fn default(i2c: I2C) -> Result<Self, <I2C as ErrorType>::Error> {
Ad5627::new(i2c, 0b0001110)
}

Expand All @@ -100,7 +102,11 @@ where
///
/// # Returns
/// The actual voltage programmed into the DAC after digitization.
pub fn set_voltage(&mut self, voltage: f32, dac: Dac) -> Result<f32, Error<I2C::Error>> {
pub fn set_voltage(
&mut self,
voltage: f32,
dac: Dac,
) -> Result<f32, Error<<I2C as ErrorType>::Error>> {
// Assuming a 1.25V internal reference with a 2x output stage gain, our full scale range is
// 2.5V.
if !(0.0..=crate::MAX_VOLTAGE).contains(&voltage) {
Expand Down
4 changes: 2 additions & 2 deletions ads7924/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ads7924"
version = "0.1.0"
version = "0.2.0"
authors = ["Ryan Summers <ryan.summers@vertigo-designs.com>"]
edition = "2018"
description = "no_std driver for the ADS7924 external analog-to-digital converter (ADC)"
Expand All @@ -10,5 +10,5 @@ repository = "https://github.com/quartiq/booster/"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
embedded-hal = "0.2.4"
embedded-hal = "1"
bit_field = "0.10.0"
48 changes: 21 additions & 27 deletions ads7924/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ use core::convert::TryInto;

use bit_field::BitField;

use embedded_hal::blocking::{
delay::DelayUs,
i2c::{Write, WriteRead},
use embedded_hal::{
delay::DelayNs,
i2c::{ErrorType, I2c},
};

/// A driver for the ADS7924 4-channel analog-to-digital converter.
pub struct Ads7924<I2C>
where
I2C: Write + WriteRead,
{
pub struct Ads7924<I2C> {
i2c: I2C,
address: u8,
volts_per_lsb: f32,
Expand Down Expand Up @@ -85,8 +82,8 @@ impl<E> From<E> for Error<E> {

impl<I2C> Ads7924<I2C>
where
I2C: Write + WriteRead,
<I2C as Write>::Error: Into<<I2C as WriteRead>::Error>,
I2C: I2c,
<I2C as ErrorType>::Error: Into<<I2C as ErrorType>::Error>,
{
/// Create a new ADC driver.
///
Expand All @@ -99,11 +96,11 @@ where
i2c: I2C,
address: u8,
avdd: f32,
delay: &mut impl DelayUs<u16>,
) -> Result<Self, Error<<I2C as WriteRead>::Error>> {
delay: &mut impl DelayNs,
) -> Result<Self, Error<<I2C as ErrorType>::Error>> {
let mut ads7924 = Ads7924 {
i2c: i2c,
address: address,
i2c,
address,
volts_per_lsb: avdd / 0x1000 as f32,
};

Expand Down Expand Up @@ -132,16 +129,16 @@ where
/// * `delay` - A means of delaying during initialization.
pub fn default(
i2c: I2C,
delay: &mut impl DelayUs<u16>,
) -> Result<Self, Error<<I2C as WriteRead>::Error>> {
delay: &mut impl DelayNs,
) -> Result<Self, Error<<I2C as ErrorType>::Error>> {
Ads7924::new(i2c, 0x49, 3.434, delay)
}

fn set_mode(
&mut self,
mode: OperationMode,
channel: Option<Channel>,
) -> Result<(), Error<<I2C as WriteRead>::Error>> {
) -> Result<(), Error<<I2C as ErrorType>::Error>> {
let mut mode_control: [u8; 1] = [0];
if let Some(channel) = channel {
mode_control[0].set_bits(0..3, channel as u8);
Expand All @@ -153,14 +150,11 @@ where
Ok(())
}

fn reset(
&mut self,
delay: &mut impl DelayUs<u16>,
) -> Result<(), Error<<I2C as WriteRead>::Error>> {
fn reset(&mut self, delay: &mut impl DelayNs) -> Result<(), Error<<I2C as ErrorType>::Error>> {
self.write(Register::Reset, &[0xAA])?;

// Wait a small delay to ensure the device is processing the reset request.
delay.delay_us(500_u16);
delay.delay_us(500_u32);

Ok(())
}
Expand All @@ -169,7 +163,7 @@ where
&mut self,
register: Register,
data: &[u8],
) -> Result<(), Error<<I2C as WriteRead>::Error>> {
) -> Result<(), Error<<I2C as ErrorType>::Error>> {
if data.len() > 2 {
return Err(Error::Size);
}
Expand All @@ -193,7 +187,7 @@ where
&mut self,
register: Register,
data: &mut [u8],
) -> Result<(), Error<<I2C as WriteRead>::Error>> {
) -> Result<(), Error<<I2C as ErrorType>::Error>> {
let mut command_byte = register as u8;

// Set the INC bit in the command byte if reading more than 1 register.
Expand All @@ -217,7 +211,7 @@ where
channel: Channel,
low_threshold: f32,
high_threshold: f32,
) -> Result<(), Error<<I2C as WriteRead>::Error>> {
) -> Result<(), Error<<I2C as ErrorType>::Error>> {
if high_threshold < low_threshold || low_threshold < 0.0 || high_threshold < 0.0 {
return Err(Error::Bounds);
}
Expand Down Expand Up @@ -262,7 +256,7 @@ where
/// # Returns
/// A bit mask of which channel caused the alarm. The position of the bit corresponds with the
/// channel number.
pub fn clear_alarm(&mut self) -> Result<u8, Error<<I2C as WriteRead>::Error>> {
pub fn clear_alarm(&mut self) -> Result<u8, Error<<I2C as ErrorType>::Error>> {
// Clearing the alarm is completed by reading the interrupt control register.
let mut alarm_status: [u8; 1] = [0];
self.read(Register::IntCntrl, &mut alarm_status)?;
Expand All @@ -285,7 +279,7 @@ where
pub fn get_voltage(
&mut self,
channel: Channel,
) -> Result<f32, Error<<I2C as WriteRead>::Error>> {
) -> Result<f32, Error<<I2C as ErrorType>::Error>> {
let upper_data_register = match channel {
Channel::Zero => Register::Data0Upper,
Channel::One => Register::Data1Upper,
Expand Down Expand Up @@ -313,7 +307,7 @@ where
///
/// # Returns
/// The analog measurements of all channel in volts.
pub fn get_voltages(&mut self) -> Result<[f32; 4], Error<<I2C as WriteRead>::Error>> {
pub fn get_voltages(&mut self) -> Result<[f32; 4], Error<<I2C as ErrorType>::Error>> {
// First, disable Autoscan mode.
self.set_mode(OperationMode::Idle, None)?;

Expand Down
4 changes: 2 additions & 2 deletions dac7571/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dac7571"
version = "0.1.0"
version = "0.2.0"
authors = ["Ryan Summers <ryan.summers@vertigo-designs.com>"]
edition = "2018"
description = "no_std driver for the DAC7571 external digital-to-analog converter (DAC)"
Expand All @@ -10,4 +10,4 @@ repository = "https://github.com/quartiq/booster/"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
embedded-hal = "0.2.4"
embedded-hal = "1"
11 changes: 4 additions & 7 deletions dac7571/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
//! This driver does not support any low-power operation modes.
#![no_std]

use embedded_hal::blocking::i2c::Write;
use embedded_hal::i2c::{ErrorType, I2c};

/// A driver for the DAC7571 digital to analog converter.
pub struct Dac7571<I2C>
where
I2C: Write,
{
pub struct Dac7571<I2C> {
i2c: I2C,
address: u8,
supply_voltage: f32,
Expand All @@ -31,7 +28,7 @@ impl<E> From<E> for Error<E> {

impl<I2C> Dac7571<I2C>
where
I2C: Write,
I2C: I2c,
{
/// Construct a new DAC7571 driver.
///
Expand Down Expand Up @@ -65,7 +62,7 @@ where
///
/// # Returns
/// The voltage nominal DAC output voltage.
pub fn set_voltage(&mut self, voltage: f32) -> Result<f32, Error<I2C::Error>> {
pub fn set_voltage(&mut self, voltage: f32) -> Result<f32, Error<<I2C as ErrorType>::Error>> {
if voltage >= self.supply_voltage || voltage < 0.0 {
return Err(Error::Bounds);
}
Expand Down
4 changes: 2 additions & 2 deletions max6639/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "max6639"
version = "0.1.0"
version = "0.2.0"
authors = ["Ryan Summers <ryan.summers@vertigo-designs.com>"]
edition = "2018"
description = "no_std driver for the MAX6639 fan speed controller"
Expand All @@ -10,5 +10,5 @@ repository = "https://github.com/quartiq/booster/"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
embedded-hal = "0.2.4"
embedded-hal = "1"
bit_field = "0.10.0"
Loading