Compare commits

...

10 Commits

Author SHA1 Message Date
Diego Barrios Romero c5fdd9057f chore: Release ds323x version 0.6.0 2025-01-02 09:26:23 +01:00
Diego Barrios Romero 86dd80a8ad Fix clippy warnings 2025-01-02 08:40:04 +01:00
Diego Barrios Romero 3597c7407e Finish updating to embedded-hal 1.0.0 2025-01-02 08:35:42 +01:00
Diego Barrios Romero 21546c07d3 Update changelog 2025-01-02 08:32:26 +01:00
Diego Barrios Romero ddca93fa30 Update dependency 2025-01-02 08:31:23 +01:00
Diego Barrios Romero b086f3086e Fix formatting 2025-01-02 08:31:08 +01:00
Louis Dupré Bertoni 03e0d9aad9 Update dependencies 2025-01-02 08:27:52 +01:00
Diego Barrios Romero 8204a4dfc1 Support cargo-release 2025-01-02 08:18:02 +01:00
Diego Barrios Romero 4e0998897a Bump MSRV 2025-01-02 08:15:40 +01:00
Diego Barrios Romero f546361477 Update CI 2025-01-02 08:14:10 +01:00
23 changed files with 235 additions and 210 deletions

View File

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

View File

@ -5,7 +5,15 @@ 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/) 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). and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
## [Unreleased] <!-- 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
## [0.5.1] - 2023-07-17 ## [0.5.1] - 2023-07-17
@ -82,7 +90,9 @@ this CHANGELOG.
[`chrono`]: https://crates.io/crates/chrono [`chrono`]: https://crates.io/crates/chrono
[`rtcc`]: https://crates.io/crates/rtcc [`rtcc`]: https://crates.io/crates/rtcc
[Unreleased]: https://github.com/eldruin/ds323x-rs/compare/v0.5.1...HEAD <!-- 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
[0.5.1]: https://github.com/eldruin/ds323x-rs/compare/v0.5.0...v0.5.1 [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.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 [0.4.0]: https://github.com/eldruin/ds323x-rs/compare/v0.3.2...v0.4.0

View File

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

View File

@ -1,4 +1,4 @@
Copyright (C) 2018-2023 Diego Barrios Romero Copyright (C) 2018-2025 Diego Barrios Romero
Permission is hereby granted, free of charge, to any person obtaining a copy of 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 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) [![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) [![Docs](https://docs.rs/ds323x/badge.svg)](https://docs.rs/ds323x)
![MSRV](https://img.shields.io/badge/rustc-1.60+-blue.svg) ![MSRV](https://img.shields.io/badge/rustc-1.75+-blue.svg)
[![Build Status](https://github.com/eldruin/ds323x-rs/workflows/Build/badge.svg)](https://github.com/eldruin/ds323x-rs/actions?query=workflow%3ABuild) [![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) [![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) ## Minimum Supported Rust Version (MSRV)
This crate is guaranteed to compile on stable Rust 1.35 and up. It *might* This crate is guaranteed to compile on stable Rust 1.75 and up. It *might*
compile with older versions but that may change in any new patch release. compile with older versions but that may change in any new patch release.
## License ## License

7
release.toml 100644
View File

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

View File

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

View File

@ -150,9 +150,9 @@ fn amend_hour(hours: Hours) -> Hours {
} }
} }
impl<DI, IC, CommE, PinE> Ds323x<DI, IC> impl<DI, IC, E> Ds323x<DI, IC>
where where
DI: ReadData<Error = Error<CommE, PinE>> + WriteData<Error = Error<CommE, PinE>>, DI: ReadData<Error = Error<E>> + WriteData<Error = Error<E>>,
{ {
/// Set Alarm1 for day of the month. /// Set Alarm1 for day of the month.
/// ///
@ -165,7 +165,7 @@ where
&mut self, &mut self,
when: DayAlarm1, when: DayAlarm1,
matching: Alarm1Matching, matching: Alarm1Matching,
) -> Result<(), Error<CommE, PinE>> { ) -> Result<(), Error<E>> {
let day_invalid = when.day < 1 || when.day > 31; let day_invalid = when.day < 1 || when.day > 31;
let hour_invalid = is_hour_valid(when.hour); let hour_invalid = is_hour_valid(when.hour);
let minute_invalid = when.minute > 59; 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. /// 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. /// 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<CommE, PinE>> { pub fn set_alarm1_hms(&mut self, when: NaiveTime) -> Result<(), Error<E>> {
let alarm = DayAlarm1 { let alarm = DayAlarm1 {
day: 1, day: 1,
hour: Hours::H24(when.hour() as u8), hour: Hours::H24(when.hour() as u8),
@ -221,7 +221,7 @@ where
&mut self, &mut self,
when: WeekdayAlarm1, when: WeekdayAlarm1,
matching: Alarm1Matching, matching: Alarm1Matching,
) -> Result<(), Error<CommE, PinE>> { ) -> Result<(), Error<E>> {
let weekday_invalid = when.weekday < 1 || when.weekday > 7; let weekday_invalid = when.weekday < 1 || when.weekday > 7;
let hour_invalid = is_hour_valid(when.hour); let hour_invalid = is_hour_valid(when.hour);
let minute_invalid = when.minute > 59; let minute_invalid = when.minute > 59;
@ -263,7 +263,7 @@ where
&mut self, &mut self,
when: DayAlarm2, when: DayAlarm2,
matching: Alarm2Matching, matching: Alarm2Matching,
) -> Result<(), Error<CommE, PinE>> { ) -> Result<(), Error<E>> {
let day_invalid = when.day < 1 || when.day > 31; let day_invalid = when.day < 1 || when.day > 31;
let hour_invalid = is_hour_valid(when.hour); let hour_invalid = is_hour_valid(when.hour);
let minute_invalid = when.minute > 59; 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. /// 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. /// 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<CommE, PinE>> { pub fn set_alarm2_hm(&mut self, when: NaiveTime) -> Result<(), Error<E>> {
let alarm = DayAlarm2 { let alarm = DayAlarm2 {
day: 1, day: 1,
hour: Hours::H24(when.hour() as u8), hour: Hours::H24(when.hour() as u8),
@ -313,7 +313,7 @@ where
&mut self, &mut self,
when: WeekdayAlarm2, when: WeekdayAlarm2,
matching: Alarm2Matching, matching: Alarm2Matching,
) -> Result<(), Error<CommE, PinE>> { ) -> Result<(), Error<E>> {
let weekday_invalid = when.weekday < 1 || when.weekday > 7; let weekday_invalid = when.weekday < 1 || when.weekday > 7;
let hour_invalid = is_hour_valid(when.hour); let hour_invalid = is_hour_valid(when.hour);
let minute_invalid = when.minute > 59; let minute_invalid = when.minute > 59;

View File

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

View File

@ -9,11 +9,11 @@ use crate::{
Register, Rtcc, Timelike, Register, Rtcc, Timelike,
}; };
impl<DI, IC, CommE, PinE> DateTimeAccess for Ds323x<DI, IC> impl<DI, IC, E> DateTimeAccess for Ds323x<DI, IC>
where where
DI: ReadData<Error = Error<CommE, PinE>> + WriteData<Error = Error<CommE, PinE>>, DI: ReadData<Error = Error<E>> + WriteData<Error = Error<E>>,
{ {
type Error = Error<CommE, PinE>; type Error = Error<E>;
fn datetime(&mut self) -> Result<NaiveDateTime, Self::Error> { fn datetime(&mut self) -> Result<NaiveDateTime, Self::Error> {
let mut data = [0; 8]; let mut data = [0; 8];
@ -54,9 +54,9 @@ where
} }
} }
impl<DI, IC, CommE, PinE> Rtcc for Ds323x<DI, IC> impl<DI, IC, E> Rtcc for Ds323x<DI, IC>
where where
DI: ReadData<Error = Error<CommE, PinE>> + WriteData<Error = Error<CommE, PinE>>, DI: ReadData<Error = Error<E>> + WriteData<Error = Error<E>>,
{ {
fn seconds(&mut self) -> Result<u8, Self::Error> { fn seconds(&mut self) -> Result<u8, Self::Error> {
self.read_register_decimal(Register::SECONDS) self.read_register_decimal(Register::SECONDS)
@ -212,20 +212,16 @@ where
} }
} }
impl<DI, IC, CommE, PinE> Ds323x<DI, IC> impl<DI, IC, E> Ds323x<DI, IC>
where where
DI: ReadData<Error = Error<CommE, PinE>> + WriteData<Error = Error<CommE, PinE>>, DI: ReadData<Error = Error<E>> + WriteData<Error = Error<E>>,
{ {
fn read_register_decimal(&mut self, register: u8) -> Result<u8, Error<CommE, PinE>> { fn read_register_decimal(&mut self, register: u8) -> Result<u8, Error<E>> {
let data = self.iface.read_register(register)?; let data = self.iface.read_register(register)?;
Ok(packed_bcd_to_decimal(data)) Ok(packed_bcd_to_decimal(data))
} }
fn write_register_decimal( fn write_register_decimal(&mut self, register: u8, decimal_number: u8) -> Result<(), Error<E>> {
&mut self,
register: u8,
decimal_number: u8,
) -> Result<(), Error<CommE, PinE>> {
self.iface self.iface
.write_register(register, decimal_to_packed_bcd(decimal_number)) .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) (bcd >> 4) * 10 + (bcd & 0xF)
} }
fn hours_to_register<CommE, PinE>(hours: Hours) -> Result<u8, Error<CommE, PinE>> { fn hours_to_register<E>(hours: Hours) -> Result<u8, Error<E>> {
match hours { match hours {
Hours::H24(h) if h > 23 => Err(Error::InvalidInputData), Hours::H24(h) if h > 23 => Err(Error::InvalidInputData),
Hours::H24(h) => Ok(decimal_to_packed_bcd(h)), Hours::H24(h) => Ok(decimal_to_packed_bcd(h)),
@ -28,7 +28,7 @@ fn hours_to_register<CommE, PinE>(hours: Hours) -> Result<u8, Error<CommE, PinE>
} }
} }
fn some_or_invalid_error<T, CommE, PinE>(data: Option<T>) -> Result<T, Error<CommE, PinE>> { fn some_or_invalid_error<T, E>(data: Option<T>) -> Result<T, Error<E>> {
if let Some(data) = data { if let Some(data) = data {
Ok(data) Ok(data)
} else { } else {
@ -42,7 +42,7 @@ mod tests {
#[test] #[test]
fn if_some_then_get_inner() { fn if_some_then_get_inner() {
match some_or_invalid_error::<u8, (), ()>(Some(1)) { match some_or_invalid_error::<u8, ()>(Some(1)) {
Ok(1) => (), Ok(1) => (),
_ => panic!(), _ => panic!(),
} }
@ -50,7 +50,7 @@ mod tests {
#[test] #[test]
fn if_none_then_error() { fn if_none_then_error() {
match some_or_invalid_error::<u8, (), ()>(None) { match some_or_invalid_error::<u8, ()>(None) {
Err(Error::InvalidDeviceState) => (), Err(Error::InvalidDeviceState) => (),
_ => panic!(), _ => panic!(),
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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