Compare commits

..

No commits in common. "c5fdd9057f20b17ac1ade52e9cf9d7484be371ac" and "fa431720876e0a8b47b32940fdeda91eba322cfc" have entirely different histories.

23 changed files with 210 additions and 235 deletions

View File

@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
rust: [stable, 1.75.0]
rust: [stable, 1.60.0]
TARGET:
- x86_64-unknown-linux-gnu
- x86_64-unknown-linux-musl
@ -23,14 +23,14 @@ jobs:
- thumbv7m-none-eabi
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
targets: ${{ matrix.TARGET }}
- name: Checkout CI scripts
uses: actions/checkout@v4
uses: actions/checkout@v3
with:
repository: 'eldruin/rust-driver-ci-scripts'
ref: 'master'
@ -46,7 +46,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
with:
targets: x86_64-unknown-linux-gnu
@ -60,10 +60,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.83.0
toolchain: 1.70.0
targets: x86_64-unknown-linux-gnu
components: clippy
@ -78,7 +78,7 @@ jobs:
TARGET: [x86_64-unknown-linux-gnu, x86_64-unknown-linux-musl]
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
@ -93,15 +93,16 @@ jobs:
coverage:
name: Coverage
runs-on: ubuntu-latest
container:
image: xd009642/tarpaulin:latest
options: --security-opt seccomp=unconfined
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov
- name: Generate code coverage
run: cargo llvm-cov --all-features --workspace --lcov --output-path lcov.info
- name: Run cargo-tarpaulin
run: cargo tarpaulin --out Lcov -- --test-threads 1
- name: upload to Coveralls
uses: coverallsapp/github-action@master

View File

@ -5,15 +5,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
<!-- next-header -->
## [Unreleased] - ReleaseDate
## [0.6.0] - 2025-01-02
### Changed
- [breaking-change] Removed `Error::Pin` variant.
- [breaking-change] Update to `embedded-hal` 1.0.0.
- Raised MSRV to version 1.75.0
## [Unreleased]
## [0.5.1] - 2023-07-17
@ -90,9 +82,7 @@ this CHANGELOG.
[`chrono`]: https://crates.io/crates/chrono
[`rtcc`]: https://crates.io/crates/rtcc
<!-- next-url -->
[Unreleased]: https://github.com/eldruin/ds323x-rs/compare/v0.6.0...HEAD
[0.6.0]: https://github.com/eldruin/ds323x-rs/compare/v0.5.1...v0.6.0
[Unreleased]: https://github.com/eldruin/ds323x-rs/compare/v0.5.1...HEAD
[0.5.1]: https://github.com/eldruin/ds323x-rs/compare/v0.5.0...v0.5.1
[0.5.0]: https://github.com/eldruin/ds323x-rs/compare/v0.4.0...v0.5.0
[0.4.0]: https://github.com/eldruin/ds323x-rs/compare/v0.3.2...v0.4.0

View File

@ -1,6 +1,6 @@
[package]
name = "ds323x"
version = "0.6.0"
version = "0.5.1"
authors = ["Diego Barrios Romero <eldruin@gmail.com>"]
repository = "https://github.com/eldruin/ds323x-rs"
license = "MIT OR Apache-2.0"
@ -21,13 +21,12 @@ include = [
edition = "2018"
[dependencies]
embedded-hal = "1.0.0"
embedded-hal = "0.2.7"
rtcc = "0.3"
[dev-dependencies]
embedded-hal-mock = { version = "0.11.1", features = ["eh1"] }
embedded-hal-bus = "0.2"
linux-embedded-hal = "0.4.0"
embedded-hal-mock = "0.9.0"
linux-embedded-hal = "0.3.2"
[profile.release]
lto = true

View File

@ -1,4 +1,4 @@
Copyright (C) 2018-2025 Diego Barrios Romero
Copyright (C) 2018-2023 Diego Barrios Romero
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in

View File

@ -2,7 +2,7 @@
[![crates.io](https://img.shields.io/crates/v/ds323x.svg)](https://crates.io/crates/ds323x)
[![Docs](https://docs.rs/ds323x/badge.svg)](https://docs.rs/ds323x)
![MSRV](https://img.shields.io/badge/rustc-1.75+-blue.svg)
![MSRV](https://img.shields.io/badge/rustc-1.60+-blue.svg)
[![Build Status](https://github.com/eldruin/ds323x-rs/workflows/Build/badge.svg)](https://github.com/eldruin/ds323x-rs/actions?query=workflow%3ABuild)
[![Coverage Status](https://coveralls.io/repos/eldruin/ds323x-rs/badge.svg?branch=master)](https://coveralls.io/r/eldruin/ds323x-rs?branch=master)
@ -138,7 +138,7 @@ changes, please file an
## Minimum Supported Rust Version (MSRV)
This crate is guaranteed to compile on stable Rust 1.75 and up. It *might*
This crate is guaranteed to compile on stable Rust 1.35 and up. It *might*
compile with older versions but that may change in any new patch release.
## License

View File

@ -1,7 +0,0 @@
pre-release-replacements = [
{file="CHANGELOG.md", search="Unreleased", replace="{{version}}", min=1},
{file="CHANGELOG.md", search="\\.\\.\\.HEAD", replace="...{{tag_name}}", exactly=1},
{file="CHANGELOG.md", search="ReleaseDate", replace="{{date}}", min=1},
{file="CHANGELOG.md", search="<!-- next-header -->", replace="<!-- next-header -->\n## [Unreleased] - ReleaseDate\n", exactly=1},
{file="CHANGELOG.md", search="<!-- next-url -->", replace="<!-- next-url -->\n[Unreleased]: https://github.com/eldruin/{{crate_name}}-rs/compare/{{tag_name}}...HEAD", exactly=1},
]

View File

@ -2,11 +2,11 @@
use crate::{ic, interface::I2cInterface, BitFlags, Ds323x, CONTROL_POR_VALUE};
use core::marker::PhantomData;
use embedded_hal::i2c;
use embedded_hal::blocking::i2c;
impl<I2C, E> Ds323x<I2cInterface<I2C>, ic::DS3231>
where
I2C: i2c::I2c<Error = E>,
I2C: i2c::Write<Error = E> + i2c::WriteRead<Error = E>,
{
/// Create a new instance of the DS3231 device.
pub fn new_ds3231(i2c: I2C) -> Self {

View File

@ -4,11 +4,11 @@ use crate::{
ic, interface::I2cInterface, BitFlags, Ds323x, Error, TempConvRate, CONTROL_POR_VALUE,
};
use core::marker::PhantomData;
use embedded_hal::i2c;
use embedded_hal::blocking::i2c;
impl<I2C, E> Ds323x<I2cInterface<I2C>, ic::DS3232>
where
I2C: i2c::I2c<Error = E>,
I2C: i2c::Write<Error = E> + i2c::WriteRead<Error = E>,
{
/// Create a new instance of the DS3232 device.
pub fn new_ds3232(i2c: I2C) -> Self {
@ -32,7 +32,7 @@ where
/// [`enable_32khz_output()`](#method.enable_32khz_output).
///
/// Note: This is only available for DS3232 and DS3234 devices.
pub fn enable_32khz_output_on_battery(&mut self) -> Result<(), Error<E>> {
pub fn enable_32khz_output_on_battery(&mut self) -> Result<(), Error<E, ()>> {
let status = self.status | BitFlags::BB32KHZ;
self.write_status_without_clearing_alarm(status)
}
@ -43,7 +43,7 @@ where
/// it enabled. See [`enable_32khz_output()`](#method.enable_32khz_output).
///
/// Note: This is only available for DS3232 and DS3234 devices.
pub fn disable_32khz_output_on_battery(&mut self) -> Result<(), Error<E>> {
pub fn disable_32khz_output_on_battery(&mut self) -> Result<(), Error<E, ()>> {
let status = self.status & !BitFlags::BB32KHZ;
self.write_status_without_clearing_alarm(status)
}
@ -55,7 +55,10 @@ where
/// temperature changes will not be compensated for.
///
/// Note: This is only available for DS3232 and DS3234 devices.
pub fn set_temperature_conversion_rate(&mut self, rate: TempConvRate) -> Result<(), Error<E>> {
pub fn set_temperature_conversion_rate(
&mut self,
rate: TempConvRate,
) -> Result<(), Error<E, ()>> {
let status = match rate {
TempConvRate::_64s => self.status & !BitFlags::CRATE1 & !BitFlags::CRATE0,
TempConvRate::_128s => self.status & !BitFlags::CRATE1 | BitFlags::CRATE0,

View File

@ -2,17 +2,21 @@
use crate::interface::{SpiInterface, WriteData};
use crate::{ic, BitFlags, Ds323x, Error, Register, TempConvRate, CONTROL_POR_VALUE};
use core::marker::PhantomData;
use embedded_hal::spi;
use embedded_hal::{blocking::spi, digital::v2::OutputPin};
impl<SPI, E> Ds323x<SpiInterface<SPI>, ic::DS3234>
impl<SPI, CS, CommE, PinE> Ds323x<SpiInterface<SPI, CS>, ic::DS3234>
where
SPI: spi::SpiDevice<u8, Error = E>,
SPI: spi::Transfer<u8, Error = CommE> + spi::Write<u8, Error = CommE>,
CS: OutputPin<Error = PinE>,
{
/// Create a new instance.
pub fn new_ds3234(spi: SPI) -> Self {
pub fn new_ds3234(spi: SPI, chip_select: CS) -> Self {
const STATUS_POR_VALUE: u8 = BitFlags::OSC_STOP | BitFlags::BB32KHZ | BitFlags::EN32KHZ;
Ds323x {
iface: SpiInterface { spi },
iface: SpiInterface {
spi,
cs: chip_select,
},
control: CONTROL_POR_VALUE,
status: STATUS_POR_VALUE,
_ic: PhantomData,
@ -20,8 +24,8 @@ where
}
/// Destroy driver instance, return SPI bus instance and CS output pin.
pub fn destroy_ds3234(self) -> SPI {
self.iface.spi
pub fn destroy_ds3234(self) -> (SPI, CS) {
(self.iface.spi, self.iface.cs)
}
/// Enable the 32kHz output when battery-powered. (enabled per default)
@ -30,7 +34,7 @@ where
/// [`enable_32khz_output()`](#method.enable_32khz_output).
///
/// Note: This is only available for DS3232 and DS3234 devices.
pub fn enable_32khz_output_on_battery(&mut self) -> Result<(), Error<E>> {
pub fn enable_32khz_output_on_battery(&mut self) -> Result<(), Error<CommE, PinE>> {
let status = self.status | BitFlags::BB32KHZ;
self.write_status_without_clearing_alarm(status)
}
@ -41,7 +45,7 @@ where
/// it enabled. See [`enable_32khz_output()`](#method.enable_32khz_output).
///
/// Note: This is only available for DS3232 and DS3234 devices.
pub fn disable_32khz_output_on_battery(&mut self) -> Result<(), Error<E>> {
pub fn disable_32khz_output_on_battery(&mut self) -> Result<(), Error<CommE, PinE>> {
let status = self.status & !BitFlags::BB32KHZ;
self.write_status_without_clearing_alarm(status)
}
@ -53,7 +57,10 @@ where
/// temperature changes will not be compensated for.
///
/// Note: This is only available for DS3232 and DS3234 devices.
pub fn set_temperature_conversion_rate(&mut self, rate: TempConvRate) -> Result<(), Error<E>> {
pub fn set_temperature_conversion_rate(
&mut self,
rate: TempConvRate,
) -> Result<(), Error<CommE, PinE>> {
let status = match rate {
TempConvRate::_64s => self.status & !BitFlags::CRATE1 & !BitFlags::CRATE0,
TempConvRate::_128s => self.status & !BitFlags::CRATE1 | BitFlags::CRATE0,
@ -66,14 +73,14 @@ where
/// Enable the temperature conversions when battery-powered. (enabled per default)
///
/// Note: This is only available for DS3234 devices.
pub fn enable_temperature_conversions_on_battery(&mut self) -> Result<(), Error<E>> {
pub fn enable_temperature_conversions_on_battery(&mut self) -> Result<(), Error<CommE, PinE>> {
self.iface.write_register(Register::TEMP_CONV, 0)
}
/// Disable the temperature conversions when battery-powered.
///
/// Note: This is only available for DS3234 devices.
pub fn disable_temperature_conversions_on_battery(&mut self) -> Result<(), Error<E>> {
pub fn disable_temperature_conversions_on_battery(&mut self) -> Result<(), Error<CommE, PinE>> {
self.iface
.write_register(Register::TEMP_CONV, BitFlags::TEMP_CONV_BAT)
}

View File

@ -150,9 +150,9 @@ fn amend_hour(hours: Hours) -> Hours {
}
}
impl<DI, IC, E> Ds323x<DI, IC>
impl<DI, IC, CommE, PinE> Ds323x<DI, IC>
where
DI: ReadData<Error = Error<E>> + WriteData<Error = Error<E>>,
DI: ReadData<Error = Error<CommE, PinE>> + WriteData<Error = Error<CommE, PinE>>,
{
/// Set Alarm1 for day of the month.
///
@ -165,7 +165,7 @@ where
&mut self,
when: DayAlarm1,
matching: Alarm1Matching,
) -> Result<(), Error<E>> {
) -> Result<(), Error<CommE, PinE>> {
let day_invalid = when.day < 1 || when.day > 31;
let hour_invalid = is_hour_valid(when.hour);
let minute_invalid = when.minute > 59;
@ -200,7 +200,7 @@ where
///
/// Will return an `Error::InvalidInputData` if any of the parameters is out of range.
/// The day is not used by this matching strategy and is set to 1.
pub fn set_alarm1_hms(&mut self, when: NaiveTime) -> Result<(), Error<E>> {
pub fn set_alarm1_hms(&mut self, when: NaiveTime) -> Result<(), Error<CommE, PinE>> {
let alarm = DayAlarm1 {
day: 1,
hour: Hours::H24(when.hour() as u8),
@ -221,7 +221,7 @@ where
&mut self,
when: WeekdayAlarm1,
matching: Alarm1Matching,
) -> Result<(), Error<E>> {
) -> Result<(), Error<CommE, PinE>> {
let weekday_invalid = when.weekday < 1 || when.weekday > 7;
let hour_invalid = is_hour_valid(when.hour);
let minute_invalid = when.minute > 59;
@ -263,7 +263,7 @@ where
&mut self,
when: DayAlarm2,
matching: Alarm2Matching,
) -> Result<(), Error<E>> {
) -> Result<(), Error<CommE, PinE>> {
let day_invalid = when.day < 1 || when.day > 31;
let hour_invalid = is_hour_valid(when.hour);
let minute_invalid = when.minute > 59;
@ -293,7 +293,7 @@ where
///
/// Will return an `Error::InvalidInputData` if any of the parameters is out of range.
/// The day is not used by this matching strategy and is set to 1.
pub fn set_alarm2_hm(&mut self, when: NaiveTime) -> Result<(), Error<E>> {
pub fn set_alarm2_hm(&mut self, when: NaiveTime) -> Result<(), Error<CommE, PinE>> {
let alarm = DayAlarm2 {
day: 1,
hour: Hours::H24(when.hour() as u8),
@ -313,7 +313,7 @@ where
&mut self,
when: WeekdayAlarm2,
matching: Alarm2Matching,
) -> Result<(), Error<E>> {
) -> Result<(), Error<CommE, PinE>> {
let weekday_invalid = when.weekday < 1 || when.weekday > 7;
let hour_invalid = is_hour_valid(when.hour);
let minute_invalid = when.minute > 59;

View File

@ -5,18 +5,18 @@ use crate::{
BitFlags, Ds323x, Error, Register, SqWFreq,
};
impl<DI, IC, E> Ds323x<DI, IC>
impl<DI, IC, CommE, PinE> Ds323x<DI, IC>
where
DI: ReadData<Error = Error<E>> + WriteData<Error = Error<E>>,
DI: ReadData<Error = Error<CommE, PinE>> + WriteData<Error = Error<CommE, PinE>>,
{
/// Enable the oscillator (set the clock running) (default).
pub fn enable(&mut self) -> Result<(), Error<E>> {
pub fn enable(&mut self) -> Result<(), Error<CommE, PinE>> {
let control = self.control;
self.write_control(control & !BitFlags::EOSC)
}
/// Disable the oscillator (stops the clock).
pub fn disable(&mut self) -> Result<(), Error<E>> {
pub fn disable(&mut self) -> Result<(), Error<CommE, PinE>> {
let control = self.control;
self.write_control(control | BitFlags::EOSC)
}
@ -24,7 +24,7 @@ where
/// Force a temperature conversion and time compensation with TXCO algorithm.
///
/// The *busy* status should be checked before doing this. See [`busy()`](#method.busy)
pub fn convert_temperature(&mut self) -> Result<(), Error<E>> {
pub fn convert_temperature(&mut self) -> Result<(), Error<CommE, PinE>> {
let control = self.iface.read_register(Register::CONTROL)?;
// do not overwrite if a conversion is in progress
if (control & BitFlags::TEMP_CONV) == 0 {
@ -35,55 +35,55 @@ where
}
/// Enable the 32kHz output. (enabled per default)
pub fn enable_32khz_output(&mut self) -> Result<(), Error<E>> {
pub fn enable_32khz_output(&mut self) -> Result<(), Error<CommE, PinE>> {
let status = self.status | BitFlags::EN32KHZ;
self.write_status_without_clearing_alarm(status)
}
/// Disable the 32kHz output.
pub fn disable_32khz_output(&mut self) -> Result<(), Error<E>> {
pub fn disable_32khz_output(&mut self) -> Result<(), Error<CommE, PinE>> {
let status = self.status & !BitFlags::EN32KHZ;
self.write_status_without_clearing_alarm(status)
}
/// Set the aging offset.
pub fn set_aging_offset(&mut self, offset: i8) -> Result<(), Error<E>> {
pub fn set_aging_offset(&mut self, offset: i8) -> Result<(), Error<CommE, PinE>> {
self.iface
.write_register(Register::AGING_OFFSET, offset as u8)
}
/// Read the aging offset.
pub fn aging_offset(&mut self) -> Result<i8, Error<E>> {
pub fn aging_offset(&mut self) -> Result<i8, Error<CommE, PinE>> {
let offset = self.iface.read_register(Register::AGING_OFFSET)?;
Ok(offset as i8)
}
/// Set the interrupt/square-wave output to be used as interrupt output.
pub fn use_int_sqw_output_as_interrupt(&mut self) -> Result<(), Error<E>> {
pub fn use_int_sqw_output_as_interrupt(&mut self) -> Result<(), Error<CommE, PinE>> {
let control = self.control;
self.write_control(control | BitFlags::INTCN)
}
/// Set the interrupt/square-wave output to be used as square-wave output. (default)
pub fn use_int_sqw_output_as_square_wave(&mut self) -> Result<(), Error<E>> {
pub fn use_int_sqw_output_as_square_wave(&mut self) -> Result<(), Error<CommE, PinE>> {
let control = self.control;
self.write_control(control & !BitFlags::INTCN)
}
/// Enable battery-backed square wave generation.
pub fn enable_square_wave(&mut self) -> Result<(), Error<E>> {
pub fn enable_square_wave(&mut self) -> Result<(), Error<CommE, PinE>> {
let control = self.control;
self.write_control(control | BitFlags::BBSQW)
}
/// Disable battery-backed square wave generation.
pub fn disable_square_wave(&mut self) -> Result<(), Error<E>> {
pub fn disable_square_wave(&mut self) -> Result<(), Error<CommE, PinE>> {
let control = self.control;
self.write_control(control & !BitFlags::BBSQW)
}
/// Set the square-wave output frequency.
pub fn set_square_wave_frequency(&mut self, freq: SqWFreq) -> Result<(), Error<E>> {
pub fn set_square_wave_frequency(&mut self, freq: SqWFreq) -> Result<(), Error<CommE, PinE>> {
let new_control = match freq {
SqWFreq::_1Hz => self.control & !BitFlags::RS2 & !BitFlags::RS1,
SqWFreq::_1_024Hz => self.control & !BitFlags::RS2 | BitFlags::RS1,
@ -94,30 +94,30 @@ where
}
/// Enable Alarm1 interrupts.
pub fn enable_alarm1_interrupts(&mut self) -> Result<(), Error<E>> {
pub fn enable_alarm1_interrupts(&mut self) -> Result<(), Error<CommE, PinE>> {
let control = self.control;
self.write_control(control | BitFlags::ALARM1_INT_EN)
}
/// Disable Alarm1 interrupts.
pub fn disable_alarm1_interrupts(&mut self) -> Result<(), Error<E>> {
pub fn disable_alarm1_interrupts(&mut self) -> Result<(), Error<CommE, PinE>> {
let control = self.control;
self.write_control(control & !BitFlags::ALARM1_INT_EN)
}
/// Enable Alarm2 interrupts.
pub fn enable_alarm2_interrupts(&mut self) -> Result<(), Error<E>> {
pub fn enable_alarm2_interrupts(&mut self) -> Result<(), Error<CommE, PinE>> {
let control = self.control;
self.write_control(control | BitFlags::ALARM2_INT_EN)
}
/// Disable Alarm2 interrupts.
pub fn disable_alarm2_interrupts(&mut self) -> Result<(), Error<E>> {
pub fn disable_alarm2_interrupts(&mut self) -> Result<(), Error<CommE, PinE>> {
let control = self.control;
self.write_control(control & !BitFlags::ALARM2_INT_EN)
}
fn write_control(&mut self, control: u8) -> Result<(), Error<E>> {
fn write_control(&mut self, control: u8) -> Result<(), Error<CommE, PinE>> {
self.iface.write_register(Register::CONTROL, control)?;
self.control = control;
Ok(())
@ -126,7 +126,7 @@ where
pub(crate) fn write_status_without_clearing_alarm(
&mut self,
status: u8,
) -> Result<(), Error<E>> {
) -> Result<(), Error<CommE, PinE>> {
// avoid clearing alarm flags
let new_status = status | BitFlags::ALARM2F | BitFlags::ALARM1F;
self.iface.write_register(Register::STATUS, new_status)?;

View File

@ -9,11 +9,11 @@ use crate::{
Register, Rtcc, Timelike,
};
impl<DI, IC, E> DateTimeAccess for Ds323x<DI, IC>
impl<DI, IC, CommE, PinE> DateTimeAccess for Ds323x<DI, IC>
where
DI: ReadData<Error = Error<E>> + WriteData<Error = Error<E>>,
DI: ReadData<Error = Error<CommE, PinE>> + WriteData<Error = Error<CommE, PinE>>,
{
type Error = Error<E>;
type Error = Error<CommE, PinE>;
fn datetime(&mut self) -> Result<NaiveDateTime, Self::Error> {
let mut data = [0; 8];
@ -54,9 +54,9 @@ where
}
}
impl<DI, IC, E> Rtcc for Ds323x<DI, IC>
impl<DI, IC, CommE, PinE> Rtcc for Ds323x<DI, IC>
where
DI: ReadData<Error = Error<E>> + WriteData<Error = Error<E>>,
DI: ReadData<Error = Error<CommE, PinE>> + WriteData<Error = Error<CommE, PinE>>,
{
fn seconds(&mut self) -> Result<u8, Self::Error> {
self.read_register_decimal(Register::SECONDS)
@ -212,16 +212,20 @@ where
}
}
impl<DI, IC, E> Ds323x<DI, IC>
impl<DI, IC, CommE, PinE> Ds323x<DI, IC>
where
DI: ReadData<Error = Error<E>> + WriteData<Error = Error<E>>,
DI: ReadData<Error = Error<CommE, PinE>> + WriteData<Error = Error<CommE, PinE>>,
{
fn read_register_decimal(&mut self, register: u8) -> Result<u8, Error<E>> {
fn read_register_decimal(&mut self, register: u8) -> Result<u8, Error<CommE, PinE>> {
let data = self.iface.read_register(register)?;
Ok(packed_bcd_to_decimal(data))
}
fn write_register_decimal(&mut self, register: u8, decimal_number: u8) -> Result<(), Error<E>> {
fn write_register_decimal(
&mut self,
register: u8,
decimal_number: u8,
) -> Result<(), Error<CommE, PinE>> {
self.iface
.write_register(register, decimal_to_packed_bcd(decimal_number))
}

View File

@ -17,7 +17,7 @@ fn packed_bcd_to_decimal(bcd: u8) -> u8 {
(bcd >> 4) * 10 + (bcd & 0xF)
}
fn hours_to_register<E>(hours: Hours) -> Result<u8, Error<E>> {
fn hours_to_register<CommE, PinE>(hours: Hours) -> Result<u8, Error<CommE, PinE>> {
match hours {
Hours::H24(h) if h > 23 => Err(Error::InvalidInputData),
Hours::H24(h) => Ok(decimal_to_packed_bcd(h)),
@ -28,7 +28,7 @@ fn hours_to_register<E>(hours: Hours) -> Result<u8, Error<E>> {
}
}
fn some_or_invalid_error<T, E>(data: Option<T>) -> Result<T, Error<E>> {
fn some_or_invalid_error<T, CommE, PinE>(data: Option<T>) -> Result<T, Error<CommE, PinE>> {
if let Some(data) = data {
Ok(data)
} else {
@ -42,7 +42,7 @@ mod tests {
#[test]
fn if_some_then_get_inner() {
match some_or_invalid_error::<u8, ()>(Some(1)) {
match some_or_invalid_error::<u8, (), ()>(Some(1)) {
Ok(1) => (),
_ => panic!(),
}
@ -50,7 +50,7 @@ mod tests {
#[test]
fn if_none_then_error() {
match some_or_invalid_error::<u8, ()>(None) {
match some_or_invalid_error::<u8, (), ()>(None) {
Err(Error::InvalidDeviceState) => (),
_ => panic!(),
}

View File

@ -5,18 +5,18 @@ use crate::{
BitFlags, Ds323x, Error, Register,
};
impl<DI, IC, E> Ds323x<DI, IC>
impl<DI, IC, CommE, PinE> Ds323x<DI, IC>
where
DI: ReadData<Error = Error<E>> + WriteData<Error = Error<E>>,
DI: ReadData<Error = Error<CommE, PinE>> + WriteData<Error = Error<CommE, PinE>>,
{
/// Read whether the oscillator is running
pub fn running(&mut self) -> Result<bool, Error<E>> {
pub fn running(&mut self) -> Result<bool, Error<CommE, PinE>> {
let control = self.iface.read_register(Register::CONTROL)?;
Ok((control & BitFlags::EOSC) == 0)
}
/// Read the busy status
pub fn busy(&mut self) -> Result<bool, Error<E>> {
pub fn busy(&mut self) -> Result<bool, Error<CommE, PinE>> {
let status = self.iface.read_register(Register::STATUS)?;
Ok((status & BitFlags::BUSY) != 0)
}
@ -28,7 +28,7 @@ where
///
/// Once this is true, it will stay as such until cleared with
/// [`clear_has_been_stopped_flag()`](#method.clear_has_been_stopped_flag)
pub fn has_been_stopped(&mut self) -> Result<bool, Error<E>> {
pub fn has_been_stopped(&mut self) -> Result<bool, Error<CommE, PinE>> {
let status = self.iface.read_register(Register::STATUS)?;
Ok((status & BitFlags::OSC_STOP) != 0)
}
@ -37,7 +37,7 @@ where
/// stopped at some point.
///
/// See also: [`has_been_stopped()`](#method.has_been_stopped)
pub fn clear_has_been_stopped_flag(&mut self) -> Result<(), Error<E>> {
pub fn clear_has_been_stopped_flag(&mut self) -> Result<(), Error<CommE, PinE>> {
let status = self.status & !BitFlags::OSC_STOP;
self.write_status_without_clearing_alarm(status)
}
@ -46,7 +46,7 @@ where
///
/// Once this is true, it will stay as such until cleared with
/// [`clear_alarm1_matched_flag()`](#method.clear_alarm1_matched_flag)
pub fn has_alarm1_matched(&mut self) -> Result<bool, Error<E>> {
pub fn has_alarm1_matched(&mut self) -> Result<bool, Error<CommE, PinE>> {
let status = self.iface.read_register(Register::STATUS)?;
Ok((status & BitFlags::ALARM1F) != 0)
}
@ -54,7 +54,7 @@ where
/// Clear flag signalling whether the Alarm1 has matched at some point.
///
/// See also: [`has_alarm1_matched()`](#method.has_alarm1_matched)
pub fn clear_alarm1_matched_flag(&mut self) -> Result<(), Error<E>> {
pub fn clear_alarm1_matched_flag(&mut self) -> Result<(), Error<CommE, PinE>> {
let status = self.status | BitFlags::ALARM2F;
self.iface.write_register(Register::STATUS, status)
}
@ -63,7 +63,7 @@ where
///
/// Once this is true, it will stay as such until cleared with
/// [`clear_alarm2_matched_flag()`](#method.clear_alarm2_matched_flag)
pub fn has_alarm2_matched(&mut self) -> Result<bool, Error<E>> {
pub fn has_alarm2_matched(&mut self) -> Result<bool, Error<CommE, PinE>> {
let status = self.iface.read_register(Register::STATUS)?;
Ok((status & BitFlags::ALARM2F) != 0)
}
@ -71,7 +71,7 @@ where
/// Clear flag signalling whether the Alarm2 has matched at some point.
///
/// See also: [`has_alarm2_matched()`](#method.has_alarm2_matched)
pub fn clear_alarm2_matched_flag(&mut self) -> Result<(), Error<E>> {
pub fn clear_alarm2_matched_flag(&mut self) -> Result<(), Error<CommE, PinE>> {
let status = self.status | BitFlags::ALARM1F;
self.iface.write_register(Register::STATUS, status)
}
@ -80,7 +80,7 @@ where
///
/// Note: It is possible to manually force a temperature conversion with
/// [`convert_temperature()`](#method.convert_temperature)
pub fn temperature(&mut self) -> Result<f32, Error<E>> {
pub fn temperature(&mut self) -> Result<f32, Error<CommE, PinE>> {
let mut data = [Register::TEMP_MSB, 0, 0];
self.iface.read_data(&mut data)?;
let is_negative = (data[1] & 0b1000_0000) != 0;

View File

@ -1,7 +1,10 @@
//! I2C/SPI interfaces
use crate::{private, Error, DEVICE_ADDRESS};
use embedded_hal::{i2c, spi};
use embedded_hal::{
blocking::{i2c, spi},
digital::v2::OutputPin,
};
/// I2C interface
#[derive(Debug, Default)]
@ -11,8 +14,9 @@ pub struct I2cInterface<I2C> {
/// SPI interface
#[derive(Debug, Default)]
pub struct SpiInterface<SPI> {
pub struct SpiInterface<SPI, CS> {
pub(crate) spi: SPI,
pub(crate) cs: CS,
}
/// Write data
@ -27,9 +31,9 @@ pub trait WriteData: private::Sealed {
impl<I2C, E> WriteData for I2cInterface<I2C>
where
I2C: i2c::I2c<Error = E>,
I2C: i2c::Write<Error = E>,
{
type Error = Error<E>;
type Error = Error<E, ()>;
fn write_register(&mut self, register: u8, data: u8) -> Result<(), Self::Error> {
let payload: [u8; 2] = [register, data];
self.i2c
@ -42,19 +46,29 @@ where
}
}
impl<SPI, E> WriteData for SpiInterface<SPI>
impl<SPI, CS, CommE, PinE> WriteData for SpiInterface<SPI, CS>
where
SPI: spi::SpiDevice<u8, Error = E>,
SPI: spi::Write<u8, Error = CommE>,
CS: OutputPin<Error = PinE>,
{
type Error = Error<E>;
type Error = Error<CommE, PinE>;
fn write_register(&mut self, register: u8, data: u8) -> Result<(), Self::Error> {
self.cs.set_low().map_err(Error::Pin)?;
let payload: [u8; 2] = [register + 0x80, data];
self.spi.write(&payload).map_err(Error::Comm)
let result = self.spi.write(&payload).map_err(Error::Comm);
self.cs.set_high().map_err(Error::Pin)?;
result
}
fn write_data(&mut self, payload: &mut [u8]) -> Result<(), Self::Error> {
self.cs.set_low().map_err(Error::Pin)?;
payload[0] += 0x80;
self.spi.write(payload).map_err(Error::Comm)
let result = self.spi.write(payload).map_err(Error::Comm);
self.cs.set_high().map_err(Error::Pin)?;
result
}
}
@ -70,9 +84,9 @@ pub trait ReadData: private::Sealed {
impl<I2C, E> ReadData for I2cInterface<I2C>
where
I2C: i2c::I2c<Error = E>,
I2C: i2c::WriteRead<Error = E>,
{
type Error = Error<E>;
type Error = Error<E, ()>;
fn read_register(&mut self, register: u8) -> Result<u8, Self::Error> {
let mut data = [0];
self.i2c
@ -89,18 +103,25 @@ where
}
}
impl<SPI, E> ReadData for SpiInterface<SPI>
impl<SPI, CS, CommE, PinE> ReadData for SpiInterface<SPI, CS>
where
SPI: spi::SpiDevice<u8, Error = E>,
SPI: spi::Transfer<u8, Error = CommE>,
CS: OutputPin<Error = PinE>,
{
type Error = Error<E>;
type Error = Error<CommE, PinE>;
fn read_register(&mut self, register: u8) -> Result<u8, Self::Error> {
self.cs.set_low().map_err(Error::Pin)?;
let mut data = [register, 0];
let result = self.spi.transfer_in_place(&mut data).map_err(Error::Comm);
result.and(Ok(data[1]))
let result = self.spi.transfer(&mut data).map_err(Error::Comm);
self.cs.set_high().map_err(Error::Pin)?;
Ok(result?[1])
}
fn read_data(&mut self, payload: &mut [u8]) -> Result<(), Self::Error> {
self.spi.transfer_in_place(payload).map_err(Error::Comm)
self.cs.set_low().map_err(Error::Pin)?;
let result = self.spi.transfer(payload).map_err(Error::Comm);
self.cs.set_high().map_err(Error::Pin)?;
result?;
Ok(())
}
}

View File

@ -157,17 +157,15 @@
//!
//! ```no_run
//! use ds323x::Ds323x;
//! use embedded_hal_bus::spi::ExclusiveDevice;
//! use linux_embedded_hal::{Delay, SpidevBus, SysfsPin};
//! use linux_embedded_hal::{SysfsPin as Pin, Spidev};
//!
//! let spi = SpidevBus::open("/dev/spidev0.0").unwrap();
//! let chip_select = SysfsPin::new(25);
//! let dev = ExclusiveDevice::new(spi, chip_select, Delay).unwrap();
//! let rtc = Ds323x::new_ds3234(dev);
//! let dev = Spidev::open("/dev/spidev0.0").unwrap();
//! let chip_select = Pin::new(24);
//! let rtc = Ds323x::new_ds3234(dev, chip_select);
//! // do something...
//!
//! // get the SPI device back
//! let dev = rtc.destroy_ds3234();
//! // get the SPI device and chip select pin back
//! let (dev, chip_select) = rtc.destroy_ds3234();
//! ```
//!
//! ### Set the current date and time at once
@ -377,9 +375,11 @@ pub const SPI_MODE_3: Mode = MODE_3;
/// All possible errors in this crate
#[derive(Debug)]
pub enum Error<E> {
pub enum Error<CommE, PinE> {
/// I²C/SPI bus error
Comm(E),
Comm(CommE),
/// Pin setting error
Pin(PinE),
/// Invalid input data provided
InvalidInputData,
/// Internal device state is invalid.
@ -498,7 +498,7 @@ mod private {
use super::{ic, interface};
pub trait Sealed {}
impl<SPI> Sealed for interface::SpiInterface<SPI> {}
impl<SPI, CS> Sealed for interface::SpiInterface<SPI, CS> {}
impl<I2C> Sealed for interface::I2cInterface<I2C> {}
impl Sealed for ic::DS3231 {}

View File

@ -1,4 +1,4 @@
use embedded_hal_mock::eh1::{i2c::Transaction as I2cTrans, spi::Transaction as SpiTrans};
use embedded_hal_mock::{i2c::Transaction as I2cTrans, spi::Transaction as SpiTrans};
mod common;
use self::common::{
destroy_ds3231, destroy_ds3232, destroy_ds3234, new_ds3231, new_ds3232, new_ds3234,
@ -583,7 +583,7 @@ macro_rules! set_alarm_test {
($name:ident, $method:ident, $register:ident, [ $( $registers:expr ),+ ], $( $value:expr ),+) => {
set_values_test!($name, $method,
[ I2cTrans::write(DEV_ADDR, vec![Register::$register, $( $registers ),*]) ],
[ SpiTrans::transaction_start(), SpiTrans::write_vec(vec![Register::$register + 0x80, $( $registers ),*]), SpiTrans::transaction_end() ],
[ SpiTrans::write(vec![Register::$register + 0x80, $( $registers ),*]) ],
$($value),*
);
};

View File

@ -1,5 +1,5 @@
use ds323x::{ic, interface, Ds323x};
use embedded_hal_mock::eh1::{
use embedded_hal_mock::{
i2c::{Mock as I2cMock, Transaction as I2cTrans},
spi::{Mock as SpiMock, Transaction as SpiTrans},
};
@ -59,7 +59,8 @@ impl BitFlags {
pub struct DummyOutputPin;
impl embedded_hal::digital::OutputPin for DummyOutputPin {
impl embedded_hal::digital::v2::OutputPin for DummyOutputPin {
type Error = ();
fn set_low(&mut self) -> Result<(), Self::Error> {
Ok(())
}
@ -68,10 +69,6 @@ impl embedded_hal::digital::OutputPin for DummyOutputPin {
}
}
impl embedded_hal::digital::ErrorType for DummyOutputPin {
type Error = embedded_hal::digital::ErrorKind;
}
pub fn new_ds3231(
transactions: &[I2cTrans],
) -> Ds323x<interface::I2cInterface<I2cMock>, ic::DS3231> {
@ -85,9 +82,9 @@ pub fn new_ds3232(
}
pub fn new_ds3234(
transactions: &[SpiTrans<u8>],
) -> Ds323x<interface::SpiInterface<SpiMock<u8>>, ic::DS3234> {
Ds323x::new_ds3234(SpiMock::new(transactions))
transactions: &[SpiTrans],
) -> Ds323x<interface::SpiInterface<SpiMock, DummyOutputPin>, ic::DS3234> {
Ds323x::new_ds3234(SpiMock::new(transactions), DummyOutputPin)
}
pub fn destroy_ds3231(dev: Ds323x<interface::I2cInterface<I2cMock>, ic::DS3231>) {
@ -98,8 +95,8 @@ pub fn destroy_ds3232(dev: Ds323x<interface::I2cInterface<I2cMock>, ic::DS3232>)
dev.destroy_ds3232().done();
}
pub fn destroy_ds3234(dev: Ds323x<interface::SpiInterface<SpiMock<u8>>, ic::DS3234>) {
dev.destroy_ds3234().done();
pub fn destroy_ds3234(dev: Ds323x<interface::SpiInterface<SpiMock, DummyOutputPin>, ic::DS3234>) {
dev.destroy_ds3234().0.done();
}
#[macro_export]
@ -208,14 +205,10 @@ macro_rules! get_param_test {
vec![Register::$register],
vec![$binary_value]
)],
[
SpiTrans::transaction_start(),
SpiTrans::transfer_in_place(
vec![Register::$register, 0],
vec![Register::$register, $binary_value]
),
SpiTrans::transaction_end(),
]
[SpiTrans::transfer(
vec![Register::$register, 0],
vec![Register::$register, $binary_value]
)]
);
};
}
@ -230,10 +223,7 @@ macro_rules! transactions_i2c_read {
#[macro_export]
macro_rules! transactions_spi_read {
($register1:ident, [ $( $read_bin:expr ),+ ], [ $( $read_bin2:expr ),+ ]) => {
[SpiTrans::transaction_start(),
SpiTrans::transfer_in_place(vec![Register::$register1, $( $read_bin2 ),*], vec![Register::$register1, $( $read_bin ),*]),
SpiTrans::transaction_end()
]
[ SpiTrans::transfer(vec![Register::$register1, $( $read_bin2 ),*], vec![Register::$register1, $( $read_bin ),*]) ]
}
}
@ -290,11 +280,10 @@ macro_rules! set_param_test {
DEV_ADDR,
vec![Register::$register, $binary_value]
)],
[
SpiTrans::transaction_start(),
SpiTrans::write_vec(vec![Register::$register + 0x80, $binary_value]),
SpiTrans::transaction_end(),
]
[SpiTrans::write(vec![
Register::$register + 0x80,
$binary_value
])]
);
};
}

View File

@ -1,5 +1,5 @@
use ds323x::SqWFreq;
use embedded_hal_mock::eh1::{i2c::Transaction as I2cTrans, spi::Transaction as SpiTrans};
use embedded_hal_mock::{i2c::Transaction as I2cTrans, spi::Transaction as SpiTrans};
mod common;
use self::common::{
@ -46,11 +46,10 @@ macro_rules! call_method_test {
DEV_ADDR,
vec![Register::$register, $value_enabled]
)],
[
SpiTrans::transaction_start(),
SpiTrans::write_vec(vec![Register::$register + 0x80, $value_enabled]),
SpiTrans::transaction_end(),
]
[SpiTrans::write(vec![
Register::$register + 0x80,
$value_enabled
])]
);
};
}
@ -84,11 +83,10 @@ macro_rules! call_method_status_test {
$method,
new_ds3234,
destroy_ds3234,
[
SpiTrans::transaction_start(),
SpiTrans::write_vec(vec![Register::STATUS + 0x80, $value_ds323x]),
SpiTrans::transaction_end(),
]
[SpiTrans::write(vec![
Register::STATUS + 0x80,
$value_ds323x
])]
);
}
};
@ -106,14 +104,10 @@ macro_rules! change_if_necessary_test {
vec![Register::$register],
vec![$value_enabled]
)],
[
SpiTrans::transaction_start(),
SpiTrans::transfer_in_place(
vec![Register::$register, 0],
vec![Register::$register, $value_enabled]
),
SpiTrans::transaction_end(),
]
[SpiTrans::transfer(
vec![Register::$register, 0],
vec![Register::$register, $value_enabled]
)]
);
call_triple_test!(
@ -128,15 +122,11 @@ macro_rules! change_if_necessary_test {
I2cTrans::write(DEV_ADDR, vec![Register::$register, $value_enabled])
],
[
SpiTrans::transaction_start(),
SpiTrans::transfer_in_place(
SpiTrans::transfer(
vec![Register::$register, 0],
vec![Register::$register, $value_disabled]
),
SpiTrans::transaction_end(),
SpiTrans::transaction_start(),
SpiTrans::write_vec(vec![Register::$register + 0x80, $value_enabled]),
SpiTrans::transaction_end(),
SpiTrans::write(vec![Register::$register + 0x80, $value_enabled])
]
);
}

View File

@ -1,4 +1,4 @@
use embedded_hal_mock::eh1::{i2c::Transaction as I2cTrans, spi::Transaction as SpiTrans};
use embedded_hal_mock::{i2c::Transaction as I2cTrans, spi::Transaction as SpiTrans};
use rtcc::NaiveDateTime;
mod common;
use self::common::{
@ -35,15 +35,11 @@ macro_rules! read_set_param_write_two_test {
I2cTrans::write(DEV_ADDR, vec![Register::$register, $bin1, $bin2])
],
[
SpiTrans::transaction_start(),
SpiTrans::transfer_in_place(
SpiTrans::transfer(
vec![Register::$register, 0],
vec![Register::$register, $binary_value1_read]
),
SpiTrans::transaction_end(),
SpiTrans::transaction_start(),
SpiTrans::write_vec(vec![Register::$register + 0x80, $bin1, $bin2]),
SpiTrans::transaction_end(),
SpiTrans::write(vec![Register::$register + 0x80, $bin1, $bin2])
]
);
};
@ -64,15 +60,11 @@ macro_rules! read_set_param_test {
I2cTrans::write(DEV_ADDR, vec![Register::$register, $binary_value_write])
],
[
SpiTrans::transaction_start(),
SpiTrans::transfer_in_place(
SpiTrans::transfer(
vec![Register::$register, 0],
vec![Register::$register, $binary_value_read]
),
SpiTrans::transaction_end(),
SpiTrans::transaction_start(),
SpiTrans::write_vec(vec![Register::$register + 0x80, $binary_value_write]),
SpiTrans::transaction_end(),
SpiTrans::write(vec![Register::$register + 0x80, $binary_value_write])
]
);
};
@ -264,11 +256,7 @@ macro_rules! transactions_i2c_write {
macro_rules! transactions_spi_write {
($register:ident, [ $( $exp_bin:expr ),+ ]) => {
[
SpiTrans::transaction_start(),
SpiTrans::write_vec(vec![Register::$register + 0x80, $( $exp_bin ),*]),
SpiTrans::transaction_end()
]
[ SpiTrans::write(vec![Register::$register + 0x80, $( $exp_bin ),*]) ]
};
}

View File

@ -1,5 +1,5 @@
use ds323x::TempConvRate;
use embedded_hal_mock::eh1::{i2c::Transaction as I2cTrans, spi::Transaction as SpiTrans};
use embedded_hal_mock::{i2c::Transaction as I2cTrans, spi::Transaction as SpiTrans};
#[allow(unused)]
mod common;
@ -24,11 +24,7 @@ macro_rules! call_method_status_test {
$method,
new_ds3234,
destroy_ds3234,
[
SpiTrans::transaction_start(),
SpiTrans::write_vec(vec![Register::STATUS + 0x80, $value]),
SpiTrans::transaction_end(),
]
[SpiTrans::write(vec![Register::STATUS + 0x80, $value])]
);
}
};
@ -70,11 +66,10 @@ macro_rules! set_param_test_2_4 {
DEV_ADDR,
vec![Register::$register, $binary_value]
)],
[
SpiTrans::transaction_start(),
SpiTrans::write_vec(vec![Register::$register + 0x80, $binary_value]),
SpiTrans::transaction_end(),
]
[SpiTrans::write(vec![
Register::$register + 0x80,
$binary_value
])]
);
};
}

View File

@ -1,4 +1,4 @@
use embedded_hal_mock::eh1::spi::Transaction as SpiTrans;
use embedded_hal_mock::spi::Transaction as SpiTrans;
#[allow(unused)]
mod common;
@ -9,11 +9,7 @@ call_test!(
enable_temperature_conversions_on_battery,
new_ds3234,
destroy_ds3234,
[
SpiTrans::transaction_start(),
SpiTrans::write_vec(vec![Register::TEMP_CONV + 0x80, 0]),
SpiTrans::transaction_end()
]
[SpiTrans::write(vec![Register::TEMP_CONV + 0x80, 0])]
);
call_test!(
@ -21,9 +17,8 @@ call_test!(
disable_temperature_conversions_on_battery,
new_ds3234,
destroy_ds3234,
[
SpiTrans::transaction_start(),
SpiTrans::write_vec(vec![Register::TEMP_CONV + 0x80, BitFlags::TEMP_CONV_BAT]),
SpiTrans::transaction_end(),
]
[SpiTrans::write(vec![
Register::TEMP_CONV + 0x80,
BitFlags::TEMP_CONV_BAT
])]
);

View File

@ -1,4 +1,4 @@
use embedded_hal_mock::eh1::{i2c::Transaction as I2cTrans, spi::Transaction as SpiTrans};
use embedded_hal_mock::{i2c::Transaction as I2cTrans, spi::Transaction as SpiTrans};
mod common;
use self::common::{
destroy_ds3231, destroy_ds3232, destroy_ds3234, new_ds3231, new_ds3232, new_ds3234,