mirror of https://github.com/eldruin/ds323x-rs
Compare commits
48 Commits
Author | SHA1 | Date |
---|---|---|
![]() |
c5fdd9057f | |
![]() |
86dd80a8ad | |
![]() |
3597c7407e | |
![]() |
21546c07d3 | |
![]() |
ddca93fa30 | |
![]() |
b086f3086e | |
![]() |
03e0d9aad9 | |
![]() |
8204a4dfc1 | |
![]() |
4e0998897a | |
![]() |
f546361477 | |
![]() |
fa43172087 | |
![]() |
74009022bf | |
![]() |
fd6a145825 | |
![]() |
de0231b958 | |
![]() |
a8b8428570 | |
![]() |
f7d48398dc | |
![]() |
fe990d7295 | |
![]() |
b06eb59cd6 | |
![]() |
b0ab213098 | |
![]() |
6c6088f890 | |
![]() |
b3323f1a15 | |
![]() |
944e661053 | |
![]() |
faf48c2870 | |
![]() |
92e6de6c47 | |
![]() |
774b3cf527 | |
![]() |
fd66a531e2 | |
![]() |
e4f15715ed | |
![]() |
7329fe7c50 | |
![]() |
1930de500d | |
![]() |
a657eec8d6 | |
![]() |
9c6fe164b8 | |
![]() |
0b17437d4c | |
![]() |
7a566fd59c | |
![]() |
082fb2d159 | |
![]() |
b8ab37f57a | |
![]() |
a28b7c76fc | |
![]() |
2799590575 | |
![]() |
5414721d1d | |
![]() |
75291cde4c | |
![]() |
5ce8722fad | |
![]() |
f146d1d26e | |
![]() |
ff632625b2 | |
![]() |
73302b1866 | |
![]() |
c5a4e3df62 | |
![]() |
b62488f592 | |
![]() |
044051c2d9 | |
![]() |
092af13298 | |
![]() |
8c5bb16030 |
.github/workflows
examples
|
@ -0,0 +1,110 @@
|
|||
name: Build
|
||||
on: [push, pull_request]
|
||||
|
||||
env:
|
||||
RUSTFLAGS: '--deny warnings'
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: Build
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
rust: [stable, 1.75.0]
|
||||
TARGET:
|
||||
- x86_64-unknown-linux-gnu
|
||||
- x86_64-unknown-linux-musl
|
||||
- arm-unknown-linux-gnueabi # Raspberry Pi 1
|
||||
- armv7-unknown-linux-gnueabihf # Raspberry Pi 2, 3, etc
|
||||
# Bare metal
|
||||
- thumbv6m-none-eabi
|
||||
- thumbv7em-none-eabi
|
||||
- thumbv7em-none-eabihf
|
||||
- thumbv7m-none-eabi
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: dtolnay/rust-toolchain@master
|
||||
with:
|
||||
toolchain: ${{ matrix.rust }}
|
||||
targets: ${{ matrix.TARGET }}
|
||||
|
||||
- name: Checkout CI scripts
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
repository: 'eldruin/rust-driver-ci-scripts'
|
||||
ref: 'master'
|
||||
path: 'ci'
|
||||
|
||||
- run: ./ci/patch-no-std.sh
|
||||
if: ${{ ! contains(matrix.TARGET, 'x86_64') }}
|
||||
|
||||
- run: cargo build --target=${{ matrix.TARGET }}
|
||||
|
||||
checks:
|
||||
name: Checks
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
targets: x86_64-unknown-linux-gnu
|
||||
components: rustfmt
|
||||
|
||||
- run: cargo doc
|
||||
- run: cargo fmt --all -- --check
|
||||
|
||||
clippy:
|
||||
name: Clippy
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: dtolnay/rust-toolchain@master
|
||||
with:
|
||||
toolchain: 1.83.0
|
||||
targets: x86_64-unknown-linux-gnu
|
||||
components: clippy
|
||||
|
||||
- run: cargo clippy --all-targets
|
||||
|
||||
test:
|
||||
name: Tests
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
rust: [stable]
|
||||
TARGET: [x86_64-unknown-linux-gnu, x86_64-unknown-linux-musl]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: dtolnay/rust-toolchain@master
|
||||
with:
|
||||
toolchain: ${{ matrix.rust }}
|
||||
targets: ${{ matrix.TARGET }}
|
||||
|
||||
- name: Test
|
||||
run: cargo test --target=${{ matrix.TARGET }}
|
||||
|
||||
- name: Build examples
|
||||
run: cargo build --target=${{ matrix.TARGET }} --examples
|
||||
|
||||
coverage:
|
||||
name: Coverage
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- 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: upload to Coveralls
|
||||
uses: coverallsapp/github-action@master
|
||||
with:
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
path-to-lcov: './lcov.info'
|
66
.travis.yml
66
.travis.yml
|
@ -1,66 +0,0 @@
|
|||
language: rust
|
||||
dist: bionic # because of tarpaulin
|
||||
|
||||
env:
|
||||
global:
|
||||
- RUSTFLAGS="-D warnings"
|
||||
|
||||
before_install:
|
||||
- git clone https://github.com/eldruin/rust-driver-ci-scripts ci
|
||||
- bash ci/before_install.sh
|
||||
|
||||
jobs:
|
||||
include:
|
||||
- env: TARGET=x86_64-unknown-linux-gnu
|
||||
before_script:
|
||||
- bash ci/coverage_before_script.sh
|
||||
after_success:
|
||||
- bash ci/coverage_after_success.sh
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- libcurl4-openssl-dev
|
||||
- libelf-dev
|
||||
- libdw-dev
|
||||
- binutils-dev
|
||||
- cmake
|
||||
|
||||
# Minimum Supported Rust Version
|
||||
- env: TARGET=x86_64-unknown-linux-gnu
|
||||
rust: 1.31.0
|
||||
|
||||
# Raspberry Pi 1
|
||||
- env: TARGET=arm-unknown-linux-gnueabi DISABLE_EXAMPLES=1 DISABLE_TESTS=1
|
||||
|
||||
# Raspberry Pi 2, 3, etc
|
||||
- env: TARGET=armv7-unknown-linux-gnueabihf DISABLE_EXAMPLES=1 DISABLE_TESTS=1
|
||||
|
||||
# Bare metal
|
||||
- env: TARGET=thumbv6m-none-eabi
|
||||
- env: TARGET=thumbv7em-none-eabi
|
||||
- env: TARGET=thumbv7em-none-eabihf
|
||||
- env: TARGET=thumbv7m-none-eabi
|
||||
|
||||
allow_failures:
|
||||
- env: TARGET=x86_64-unknown-linux-gnu
|
||||
rust: nightly
|
||||
|
||||
- env: TARGET=x86_64-unknown-linux-musl
|
||||
rust: nightly
|
||||
|
||||
install:
|
||||
bash ci/install.sh
|
||||
|
||||
script:
|
||||
bash ci/script.sh
|
||||
|
||||
after_script: set +e
|
||||
|
||||
cache: cargo
|
||||
|
||||
before_cache:
|
||||
bash ci/before_cache.sh
|
||||
|
||||
notifications:
|
||||
email:
|
||||
on_success: never
|
61
CHANGELOG.md
61
CHANGELOG.md
|
@ -5,7 +5,56 @@ 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).
|
||||
|
||||
## [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
|
||||
|
||||
### Fixed
|
||||
- Fixed `set_day` method. See: [PR #9](https://github.com/eldruin/ds323x-rs/pull/9)
|
||||
|
||||
### Changed
|
||||
- Raised MSRV to version 1.60.0
|
||||
|
||||
## [0.5.0] - 2022-02-21
|
||||
|
||||
### Changed
|
||||
|
||||
- [breaking-change] Update `rtcc` to version 0.3.
|
||||
- [breaking-change] Remove `get_` from all public method names to comply with the Rust API guidelines.
|
||||
- Raise MSRV to version 1.35.0
|
||||
|
||||
## [0.4.0] - 2021-05-22
|
||||
|
||||
### Changed
|
||||
- [breaking-change] Return `Error::InvalidDeviceState` if it was not possible to read the
|
||||
date and/or time from the device because the state of the device corresponds to
|
||||
an invalid date and/or time.
|
||||
|
||||
## [0.3.2] - 2021-02-22
|
||||
|
||||
### Fixed
|
||||
- Day bounds on the `set_day()` method. Thanks to @jamesmunns. See:
|
||||
[PR #5](https://github.com/eldruin/ds323x-rs/pull/5)
|
||||
|
||||
## [0.3.1] - 2020-07-10
|
||||
|
||||
### Added
|
||||
- Added methods to set alarms 1 and 2 with a `chrono::NaiveTime`: `set_alarm1_hms()`
|
||||
and `set_alarm2_hm()`.
|
||||
|
||||
### Changed
|
||||
- Changed alarm setting methods to automatically correct invalid values to irrelevant
|
||||
input parameters due to the selected matching strategy.
|
||||
|
||||
## [0.3.0] - 2020-05-02
|
||||
|
||||
### Changed
|
||||
- [breaking-change] Renamed `is_busy()` and `is_running()` methods `busy()` and `running()`
|
||||
|
@ -41,5 +90,13 @@ this CHANGELOG.
|
|||
[`chrono`]: https://crates.io/crates/chrono
|
||||
[`rtcc`]: https://crates.io/crates/rtcc
|
||||
|
||||
[Unreleased]: https://github.com/eldruin/ds323x-rs/compare/v0.2.0...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.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.3.2]: https://github.com/eldruin/ds323x-rs/compare/v0.3.1...v0.3.2
|
||||
[0.3.1]: https://github.com/eldruin/ds323x-rs/compare/v0.3.0...v0.3.1
|
||||
[0.3.0]: https://github.com/eldruin/ds323x-rs/compare/v0.2.0...v0.3.0
|
||||
[0.2.0]: https://github.com/eldruin/ds323x-rs/compare/v0.1.0...v0.2.0
|
17
Cargo.toml
17
Cargo.toml
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "ds323x"
|
||||
version = "0.3.0"
|
||||
version = "0.6.0"
|
||||
authors = ["Diego Barrios Romero <eldruin@gmail.com>"]
|
||||
repository = "https://github.com/eldruin/ds323x-rs"
|
||||
license = "MIT OR Apache-2.0"
|
||||
|
@ -18,19 +18,16 @@ include = [
|
|||
"/LICENSE-MIT",
|
||||
"/LICENSE-APACHE",
|
||||
]
|
||||
|
||||
[badges]
|
||||
travis-ci = { repository = "eldruin/ds323x-rs", branch = "master" }
|
||||
coveralls = { repository = "eldruin/ds323x-rs", branch = "master", service = "github" }
|
||||
maintenance = { status = "actively-developed" }
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
embedded-hal = "0.2.3"
|
||||
rtcc = "0.2"
|
||||
embedded-hal = "1.0.0"
|
||||
rtcc = "0.3"
|
||||
|
||||
[dev-dependencies]
|
||||
linux-embedded-hal = "0.3"
|
||||
embedded-hal-mock = "0.7"
|
||||
embedded-hal-mock = { version = "0.11.1", features = ["eh1"] }
|
||||
embedded-hal-bus = "0.2"
|
||||
linux-embedded-hal = "0.4.0"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
Copyright (C) 2018 Diego Barrios Romero
|
||||
Copyright (C) 2018-2025 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
|
||||
|
|
83
README.md
83
README.md
|
@ -2,9 +2,9 @@
|
|||
|
||||
[](https://crates.io/crates/ds323x)
|
||||
[](https://docs.rs/ds323x)
|
||||
[](https://travis-ci.org/eldruin/ds323x-rs)
|
||||

|
||||
[](https://github.com/eldruin/ds323x-rs/actions?query=workflow%3ABuild)
|
||||
[](https://coveralls.io/r/eldruin/ds323x-rs?branch=master)
|
||||

|
||||
|
||||
This is a platform agnostic Rust driver for the DS3231, DS3232 and DS3234
|
||||
extremely accurate real-time clocks, based on the [`embedded-hal`] traits.
|
||||
|
@ -12,8 +12,8 @@ extremely accurate real-time clocks, based on the [`embedded-hal`] traits.
|
|||
[`embedded-hal`]: https://github.com/rust-embedded/embedded-hal
|
||||
|
||||
This driver allows you to:
|
||||
- Read and set date and time in 12-hour and 24-hour format. See: `get_datetime`.
|
||||
- Read and set date and time individual elements. For example, see: `get_year`.
|
||||
- Read and set date and time in 12-hour and 24-hour format. See: `datetime`.
|
||||
- Read and set date and time individual elements. For example, see: `year`.
|
||||
- Enable and disable the real-time clock. See: `enable`.
|
||||
- Read the busy status. See `busy`.
|
||||
- Read whether the oscillator is or has been stopped. See `has_been_stopped`.
|
||||
|
@ -22,6 +22,7 @@ This driver allows you to:
|
|||
- Select the function of the INT/SQW output pin. See `use_int_sqw_output_as_interrupt`.
|
||||
- Alarms:
|
||||
- Set alarms 1 and 2 with several matching policies. See `set_alarm1_day`.
|
||||
- Set alarms 1 and 2 for a time. See `set_alarm1_hms`.
|
||||
- Read whether alarms 1 or 2 have matched. See `has_alarm1_matched`.
|
||||
- Clear flag indicating that alarms 1 or 2 have matched. See `clear_alarm1_matched_flag`.
|
||||
- Enable and disable alarms 1 and 2 interrupt generation. See `enable_alarm1_interrupts`.
|
||||
|
@ -31,7 +32,7 @@ This driver allows you to:
|
|||
- Enable and disable the 32kHz output. See `enable_32khz_output`.
|
||||
- Enable and disable the 32kHz output when battery powered. See `enable_32khz_output_on_battery`.
|
||||
- Temperature conversion:
|
||||
- Read the temperature. See `get_temperature`.
|
||||
- Read the temperature. See `temperature`.
|
||||
- Force a temperature conversion and time compensation. See `convert_temperature`.
|
||||
- Set the temperature conversion rate. See `set_temperature_conversion_rate`.
|
||||
- Enable and disable the temperature conversions when battery-powered. See `enable_temperature_conversions_on_battery`.
|
||||
|
@ -41,16 +42,18 @@ This driver allows you to:
|
|||
This driver is compatible with the DS3231 and DS3232 I2C devices and the
|
||||
DS3234 SPI device.
|
||||
|
||||
### DS3231
|
||||
The DS3231 is a low-cost, extremely accurate I2C real-time clock (RTC) with
|
||||
an integrated temperature-compensated crystal oscillator (TCXO) and crystal.
|
||||
These devices are low-cost temperature-compensated crystal oscillator (TCXO)
|
||||
with a very accurate, temperature-compensated, integrated real-time clock
|
||||
(RTC) including 236/256 bytes of battery-backed SRAM, depending on the model.
|
||||
|
||||
The device incorporates a battery input, and maintains accurate timekeeping
|
||||
when main power to the device is interrupted. The integration of the
|
||||
crystal resonator enhances the long-term accuracy of the device as well as
|
||||
### DS3231 and DS3232 details
|
||||
|
||||
The devices incorporate a battery input, and maintain accurate timekeeping
|
||||
when main power to the devices is interrupted. The integration of the
|
||||
crystal resonator enhances the long-term accuracy of the devices as well as
|
||||
reduces the piece-part count in a manufacturing line.
|
||||
The DS3231 is available in commercial and industrial temperature ranges,
|
||||
and is offered in a 16-pin, 300-mil SO package.
|
||||
The devices are available in commercial and industrial temperature ranges,
|
||||
and are offered in a 16-pin, 300-mil SO package.
|
||||
|
||||
The RTC maintains seconds, minutes, hours, day, date, month, and year
|
||||
information. The date at the end of the month is automatically adjusted for
|
||||
|
@ -66,36 +69,7 @@ reset output, and to automatically switch to the backup supply when
|
|||
necessary. Additionally, the RST pin is monitored as a pushbutton
|
||||
input for generating a μP reset.
|
||||
|
||||
### DS3232
|
||||
The DS3232 is a low-cost temperature-compensated crystal oscillator (TCXO)
|
||||
with a very accurate, temperature-compensated, integrated real-time clock
|
||||
(RTC) and 236 bytes of battery-backed SRAM.
|
||||
|
||||
Additionally, the DS3232 incorporates a battery input and maintains
|
||||
accurate timekeeping when main power to the device is interrupted. The
|
||||
integration of the crystal resonator enhances the long-term accuracy of the
|
||||
device as well as reduces the piece-part count in a manufacturing line.
|
||||
The DS3232 is available in commercial and industrial temperature ranges,
|
||||
and is offered in an industry-standard 20-pin, 300-mil SO package.
|
||||
|
||||
The RTC maintains seconds, minutes, hours, day, date, month, and year
|
||||
information. The date at the end of the month is automatically adjusted for
|
||||
months with fewer than 31 days, including corrections for leap year. The
|
||||
clock operates in either the 24-hour or 12-hour format with an AM/PM
|
||||
indicator. Two programmable time-of-day alarms and a programmable
|
||||
square-wave output are provided. Address and data are transferred serially
|
||||
through an I2C bidirectional bus.
|
||||
|
||||
A precision temperature-compensated voltage reference and comparator
|
||||
circuit monitors the status of VCC to detect power failures, to provide a
|
||||
reset output, and to automatically switch to the backup supply when
|
||||
necessary. Additionally, the RST pin is monitored as a pushbutton input for
|
||||
generating a μP reset.
|
||||
|
||||
### DS3234
|
||||
The DS3234 is a low-cost, extremely accurate SPI bus real-time clock (RTC)
|
||||
with an integrated temperature-compensated crystal oscillator (TCXO) and
|
||||
crystal.
|
||||
### DS3234 details
|
||||
|
||||
The DS3234 incorporates a precision, temperature-compensated voltage
|
||||
reference and comparator circuit to monitor VCC. When VCC drops below the
|
||||
|
@ -137,17 +111,19 @@ Please find additional examples using hardware in this repository: [driver-examp
|
|||
[driver-examples]: https://github.com/eldruin/driver-examples
|
||||
|
||||
```rust
|
||||
extern crate ds323x;
|
||||
extern crate linux_embedded_hal as hal;
|
||||
use ds323x::{Ds323x, NaiveDate, Rtcc};
|
||||
use ds323x::{DateTimeAccess, Ds323x, NaiveDate, Rtcc};
|
||||
use linux_embedded_hal::I2cdev;
|
||||
|
||||
fn main() {
|
||||
let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
|
||||
let dev = I2cdev::new("/dev/i2c-1").unwrap();
|
||||
let mut rtc = Ds323x::new_ds3231(dev);
|
||||
let datetime = NaiveDate::from_ymd(2020, 5, 1).and_hms(19, 59, 58);
|
||||
let datetime = NaiveDate::from_ymd_opt(2020, 5, 1)
|
||||
.unwrap()
|
||||
.and_hms_opt(19, 59, 58)
|
||||
.unwrap();
|
||||
rtc.set_datetime(&datetime).unwrap();
|
||||
// do something else...
|
||||
let time = rtc.get_time().unwrap();
|
||||
let time = rtc.time().unwrap();
|
||||
println!("Time: {}", time);
|
||||
|
||||
let _dev = rtc.destroy_ds3231();
|
||||
|
@ -156,9 +132,15 @@ fn main() {
|
|||
|
||||
## Support
|
||||
|
||||
For questions, issues, feature requests, and other changes, please file an
|
||||
For questions, issues, feature requests like compatibility with other devices and other
|
||||
changes, please file an
|
||||
[issue in the github project](https://github.com/eldruin/ds323x-rs/issues).
|
||||
|
||||
## Minimum Supported Rust Version (MSRV)
|
||||
|
||||
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.
|
||||
|
||||
## License
|
||||
|
||||
Licensed under either of
|
||||
|
@ -175,4 +157,3 @@ at your option.
|
|||
Unless you explicitly state otherwise, any contribution intentionally submitted
|
||||
for inclusion in the work by you, as defined in the Apache-2.0 license, shall
|
||||
be dual licensed as above, without any additional terms or conditions.
|
||||
|
||||
|
|
|
@ -1,14 +1,16 @@
|
|||
extern crate ds323x;
|
||||
extern crate linux_embedded_hal as hal;
|
||||
use ds323x::{Ds323x, NaiveDate, Rtcc};
|
||||
use ds323x::{DateTimeAccess, Ds323x, NaiveDate, Rtcc};
|
||||
use linux_embedded_hal::I2cdev;
|
||||
|
||||
fn main() {
|
||||
let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
|
||||
let dev = I2cdev::new("/dev/i2c-1").unwrap();
|
||||
let mut rtc = Ds323x::new_ds3231(dev);
|
||||
let datetime = NaiveDate::from_ymd(2020, 5, 1).and_hms(19, 59, 58);
|
||||
let datetime = NaiveDate::from_ymd_opt(2020, 5, 1)
|
||||
.unwrap()
|
||||
.and_hms_opt(19, 59, 58)
|
||||
.unwrap();
|
||||
rtc.set_datetime(&datetime).unwrap();
|
||||
// do something else...
|
||||
let time = rtc.get_time().unwrap();
|
||||
let time = rtc.time().unwrap();
|
||||
println!("Time: {}", time);
|
||||
|
||||
let _dev = rtc.destroy_ds3231();
|
||||
|
|
|
@ -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},
|
||||
]
|
|
@ -1,13 +1,12 @@
|
|||
//! Functions exclusive of DS3231
|
||||
|
||||
use super::{ic, BitFlags, Ds323x, CONTROL_POR_VALUE};
|
||||
use crate::{ic, interface::I2cInterface, BitFlags, Ds323x, CONTROL_POR_VALUE};
|
||||
use core::marker::PhantomData;
|
||||
use hal::blocking;
|
||||
use interface::I2cInterface;
|
||||
use embedded_hal::i2c;
|
||||
|
||||
impl<I2C, E> Ds323x<I2cInterface<I2C>, ic::DS3231>
|
||||
where
|
||||
I2C: blocking::i2c::Write<Error = E> + blocking::i2c::WriteRead<Error = E>,
|
||||
I2C: i2c::I2c<Error = E>,
|
||||
{
|
||||
/// Create a new instance of the DS3231 device.
|
||||
pub fn new_ds3231(i2c: I2C) -> Self {
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
//! Functions exclusive of DS3232
|
||||
|
||||
use super::{ic, BitFlags, Ds323x, Error, TempConvRate, CONTROL_POR_VALUE};
|
||||
use crate::{
|
||||
ic, interface::I2cInterface, BitFlags, Ds323x, Error, TempConvRate, CONTROL_POR_VALUE,
|
||||
};
|
||||
use core::marker::PhantomData;
|
||||
use hal::blocking;
|
||||
use interface::I2cInterface;
|
||||
use embedded_hal::i2c;
|
||||
|
||||
impl<I2C, E> Ds323x<I2cInterface<I2C>, ic::DS3232>
|
||||
where
|
||||
I2C: blocking::i2c::Write<Error = E> + blocking::i2c::WriteRead<Error = E>,
|
||||
I2C: i2c::I2c<Error = E>,
|
||||
{
|
||||
/// Create a new instance of the DS3232 device.
|
||||
pub fn new_ds3232(i2c: I2C) -> Self {
|
||||
|
@ -31,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)
|
||||
}
|
||||
|
@ -42,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)
|
||||
}
|
||||
|
@ -54,10 +55,7 @@ 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,
|
||||
|
|
|
@ -1,22 +1,18 @@
|
|||
//! Functions exclusive of DS3234
|
||||
use super::{ic, BitFlags, Ds323x, Error, Register, TempConvRate, CONTROL_POR_VALUE};
|
||||
use crate::interface::{SpiInterface, WriteData};
|
||||
use crate::{ic, BitFlags, Ds323x, Error, Register, TempConvRate, CONTROL_POR_VALUE};
|
||||
use core::marker::PhantomData;
|
||||
use hal::blocking;
|
||||
use interface::{SpiInterface, WriteData};
|
||||
use embedded_hal::spi;
|
||||
|
||||
impl<SPI, CS, CommE, PinE> Ds323x<SpiInterface<SPI, CS>, ic::DS3234>
|
||||
impl<SPI, E> Ds323x<SpiInterface<SPI>, ic::DS3234>
|
||||
where
|
||||
SPI: blocking::spi::Transfer<u8, Error = CommE> + blocking::spi::Write<u8, Error = CommE>,
|
||||
CS: hal::digital::v2::OutputPin<Error = PinE>,
|
||||
SPI: spi::SpiDevice<u8, Error = E>,
|
||||
{
|
||||
/// 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;
|
||||
Ds323x {
|
||||
iface: SpiInterface {
|
||||
spi,
|
||||
cs: chip_select,
|
||||
},
|
||||
iface: SpiInterface { spi },
|
||||
control: CONTROL_POR_VALUE,
|
||||
status: STATUS_POR_VALUE,
|
||||
_ic: PhantomData,
|
||||
|
@ -24,8 +20,8 @@ where
|
|||
}
|
||||
|
||||
/// Destroy driver instance, return SPI bus instance and CS output pin.
|
||||
pub fn destroy_ds3234(self) -> (SPI, CS) {
|
||||
(self.iface.spi, self.iface.cs)
|
||||
pub fn destroy_ds3234(self) -> SPI {
|
||||
self.iface.spi
|
||||
}
|
||||
|
||||
/// Enable the 32kHz output when battery-powered. (enabled per default)
|
||||
|
@ -34,7 +30,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<CommE, PinE>> {
|
||||
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)
|
||||
}
|
||||
|
@ -45,7 +41,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<CommE, PinE>> {
|
||||
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)
|
||||
}
|
||||
|
@ -57,10 +53,7 @@ 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<CommE, PinE>> {
|
||||
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,
|
||||
|
@ -73,14 +66,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<CommE, PinE>> {
|
||||
pub fn enable_temperature_conversions_on_battery(&mut self) -> Result<(), Error<E>> {
|
||||
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<CommE, PinE>> {
|
||||
pub fn disable_temperature_conversions_on_battery(&mut self) -> Result<(), Error<E>> {
|
||||
self.iface
|
||||
.write_register(Register::TEMP_CONV, BitFlags::TEMP_CONV_BAT)
|
||||
}
|
||||
|
|
|
@ -1,10 +1,19 @@
|
|||
//! Alarm support
|
||||
|
||||
use super::super::{BitFlags, Ds323x, Error, Hours, Register};
|
||||
use super::{decimal_to_packed_bcd, hours_to_register};
|
||||
use interface::{ReadData, WriteData};
|
||||
use crate::{
|
||||
ds323x::{NaiveTime, Timelike},
|
||||
interface::{ReadData, WriteData},
|
||||
BitFlags, Ds323x, Error, Hours, Register,
|
||||
};
|
||||
|
||||
/// Parameters for setting Alarm1 on a day of the month
|
||||
///
|
||||
/// Depending on the matching strategy, some fields may not be relevant. In this
|
||||
/// case, invalid values are ignored and the minimum valid values are used instead to
|
||||
/// configure the alarm:
|
||||
/// - Second, minute and hour: 0
|
||||
/// - Day: 1
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct DayAlarm1 {
|
||||
/// Day of the month [1-31]
|
||||
|
@ -18,6 +27,12 @@ pub struct DayAlarm1 {
|
|||
}
|
||||
|
||||
/// Parameters for setting Alarm1 on a weekday
|
||||
///
|
||||
/// Depending on the matching strategy, some fields may not be relevant. In this
|
||||
/// case, invalid values are ignored and the minimum valid values are used instead to
|
||||
/// configure the alarm:
|
||||
/// - Second, minute and hour: 0
|
||||
/// - Weekday: 1
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct WeekdayAlarm1 {
|
||||
/// Weekday [1-7]
|
||||
|
@ -46,6 +61,12 @@ pub enum Alarm1Matching {
|
|||
}
|
||||
|
||||
/// Parameters for setting Alarm2 on a day of the month
|
||||
///
|
||||
/// Depending on the matching strategy, some fields may not be relevant. In this
|
||||
/// case, invalid values are ignored and the minimum valid values are used instead to
|
||||
/// configure the alarm:
|
||||
/// - Minute and hour: 0
|
||||
/// - Day: 1
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct DayAlarm2 {
|
||||
/// Day of month [1-31]
|
||||
|
@ -57,6 +78,12 @@ pub struct DayAlarm2 {
|
|||
}
|
||||
|
||||
/// Parameters for setting Alarm2 on a weekday
|
||||
///
|
||||
/// Depending on the matching strategy, some fields may not be relevant. In this
|
||||
/// case, invalid values are ignored and the minimum valid values are used instead to
|
||||
/// configure the alarm:
|
||||
/// - Minute and hour: 0
|
||||
/// - Weekday: 1
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct WeekdayAlarm2 {
|
||||
/// Weekday [1-7]
|
||||
|
@ -101,92 +128,212 @@ fn get_matching_mask_alarm2(matching: Alarm2Matching) -> [u8; 3] {
|
|||
}
|
||||
}
|
||||
|
||||
impl<DI, IC, CommE, PinE> Ds323x<DI, IC>
|
||||
/// Test if hour value is valid
|
||||
fn is_hour_valid(hours: Hours) -> bool {
|
||||
match hours {
|
||||
Hours::H24(h) if h > 23 => true,
|
||||
Hours::AM(h) if !(1..=12).contains(&h) => true,
|
||||
Hours::PM(h) if !(1..=12).contains(&h) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Amend invalid hour values
|
||||
fn amend_hour(hours: Hours) -> Hours {
|
||||
match hours {
|
||||
Hours::H24(h) if h > 23 => Hours::H24(0),
|
||||
Hours::H24(h) => Hours::H24(h),
|
||||
Hours::AM(h) if !(1..=12).contains(&h) => Hours::AM(1),
|
||||
Hours::AM(h) => Hours::AM(h),
|
||||
Hours::PM(h) if !(1..=12).contains(&h) => Hours::PM(1),
|
||||
Hours::PM(h) => Hours::PM(h),
|
||||
}
|
||||
}
|
||||
|
||||
impl<DI, IC, E> Ds323x<DI, IC>
|
||||
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.
|
||||
///
|
||||
/// Will return an `Error::InvalidInputData` if any of the parameters is out of range.
|
||||
/// Will return an `Error::InvalidInputData` if any of the used parameters
|
||||
/// (depending on the matching startegy) is out of range. Any unused
|
||||
/// parameter is set to the corresponding minimum valid value:
|
||||
/// - Second, minute, hour: 0
|
||||
/// - Day: 1
|
||||
pub fn set_alarm1_day(
|
||||
&mut self,
|
||||
when: DayAlarm1,
|
||||
matching: Alarm1Matching,
|
||||
) -> Result<(), Error<CommE, PinE>> {
|
||||
if when.day < 1 || when.day > 31 || when.minute > 59 || when.second > 59 {
|
||||
) -> Result<(), Error<E>> {
|
||||
let day_invalid = when.day < 1 || when.day > 31;
|
||||
let hour_invalid = is_hour_valid(when.hour);
|
||||
let minute_invalid = when.minute > 59;
|
||||
let second_invalid = when.second > 59;
|
||||
|
||||
let day = if day_invalid { 1 } else { when.day };
|
||||
let hour = amend_hour(when.hour);
|
||||
let minute = if minute_invalid { 0 } else { when.minute };
|
||||
|
||||
if (matching == Alarm1Matching::AllMatch && (day_invalid || hour_invalid))
|
||||
|| (hour_invalid && matching == Alarm1Matching::HoursMinutesAndSecondsMatch)
|
||||
|| ((matching != Alarm1Matching::SecondsMatch
|
||||
&& matching != Alarm1Matching::OncePerSecond)
|
||||
&& minute_invalid)
|
||||
|| second_invalid
|
||||
{
|
||||
return Err(Error::InvalidInputData);
|
||||
}
|
||||
|
||||
let match_mask = get_matching_mask_alarm1(matching);
|
||||
let mut data = [
|
||||
Register::ALARM1_SECONDS,
|
||||
decimal_to_packed_bcd(when.second) | match_mask[0],
|
||||
decimal_to_packed_bcd(when.minute) | match_mask[1],
|
||||
hours_to_register(when.hour)? | match_mask[2],
|
||||
decimal_to_packed_bcd(when.day) | match_mask[3],
|
||||
decimal_to_packed_bcd(minute) | match_mask[1],
|
||||
hours_to_register(hour)? | match_mask[2],
|
||||
decimal_to_packed_bcd(day) | match_mask[3],
|
||||
];
|
||||
self.iface.write_data(&mut data)
|
||||
}
|
||||
|
||||
/// Set Alarm1 for weekday.
|
||||
/// Set Alarm1 for a time (fires when hours, minutes and seconds match).
|
||||
///
|
||||
/// 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>> {
|
||||
let alarm = DayAlarm1 {
|
||||
day: 1,
|
||||
hour: Hours::H24(when.hour() as u8),
|
||||
minute: when.minute() as u8,
|
||||
second: when.second() as u8,
|
||||
};
|
||||
self.set_alarm1_day(alarm, Alarm1Matching::HoursMinutesAndSecondsMatch)
|
||||
}
|
||||
|
||||
/// Set Alarm1 for weekday.
|
||||
///
|
||||
/// Will return an `Error::InvalidInputData` if any of the used parameters
|
||||
/// (depending on the matching startegy) is out of range. Any unused
|
||||
/// parameter is set to the corresponding minimum valid value:
|
||||
/// - Second, minute, hour: 0
|
||||
/// - Weekday: 1
|
||||
pub fn set_alarm1_weekday(
|
||||
&mut self,
|
||||
when: WeekdayAlarm1,
|
||||
matching: Alarm1Matching,
|
||||
) -> Result<(), Error<CommE, PinE>> {
|
||||
if when.weekday < 1 || when.weekday > 7 || when.minute > 59 || when.second > 59 {
|
||||
) -> Result<(), Error<E>> {
|
||||
let weekday_invalid = when.weekday < 1 || when.weekday > 7;
|
||||
let hour_invalid = is_hour_valid(when.hour);
|
||||
let minute_invalid = when.minute > 59;
|
||||
let second_invalid = when.second > 59;
|
||||
|
||||
let weekday = if weekday_invalid { 1 } else { when.weekday };
|
||||
let hour = amend_hour(when.hour);
|
||||
let minute = if minute_invalid { 0 } else { when.minute };
|
||||
let second = if second_invalid { 0 } else { when.second };
|
||||
|
||||
if ((hour_invalid || weekday_invalid) && matching == Alarm1Matching::AllMatch)
|
||||
|| (hour_invalid && matching == Alarm1Matching::HoursMinutesAndSecondsMatch)
|
||||
|| (minute_invalid
|
||||
&& (matching != Alarm1Matching::OncePerSecond
|
||||
&& matching != Alarm1Matching::SecondsMatch))
|
||||
|| (second_invalid && matching != Alarm1Matching::OncePerSecond)
|
||||
{
|
||||
return Err(Error::InvalidInputData);
|
||||
}
|
||||
let match_mask = get_matching_mask_alarm1(matching);
|
||||
let mut data = [
|
||||
Register::ALARM1_SECONDS,
|
||||
decimal_to_packed_bcd(when.second) | match_mask[0],
|
||||
decimal_to_packed_bcd(when.minute) | match_mask[1],
|
||||
hours_to_register(when.hour)? | match_mask[2],
|
||||
decimal_to_packed_bcd(when.weekday) | match_mask[3] | BitFlags::WEEKDAY,
|
||||
decimal_to_packed_bcd(second) | match_mask[0],
|
||||
decimal_to_packed_bcd(minute) | match_mask[1],
|
||||
hours_to_register(hour)? | match_mask[2],
|
||||
decimal_to_packed_bcd(weekday) | match_mask[3] | BitFlags::WEEKDAY,
|
||||
];
|
||||
self.iface.write_data(&mut data)
|
||||
}
|
||||
|
||||
/// Set Alarm2 for date (day of month).
|
||||
///
|
||||
/// Will return an `Error::InvalidInputData` if any of the parameters is out of range.
|
||||
/// Will return an `Error::InvalidInputData` if any of the used parameters
|
||||
/// (depending on the matching startegy) is out of range. Any unused
|
||||
/// parameter is set to the corresponding minimum valid value:
|
||||
/// - Minute, hour: 0
|
||||
/// - Day: 1
|
||||
pub fn set_alarm2_day(
|
||||
&mut self,
|
||||
when: DayAlarm2,
|
||||
matching: Alarm2Matching,
|
||||
) -> Result<(), Error<CommE, PinE>> {
|
||||
if when.day < 1 || when.day > 31 || when.minute > 59 {
|
||||
) -> Result<(), Error<E>> {
|
||||
let day_invalid = when.day < 1 || when.day > 31;
|
||||
let hour_invalid = is_hour_valid(when.hour);
|
||||
let minute_invalid = when.minute > 59;
|
||||
|
||||
let day = if day_invalid { 1 } else { when.day };
|
||||
let hour = amend_hour(when.hour);
|
||||
let minute = if minute_invalid { 0 } else { when.minute };
|
||||
|
||||
if ((day_invalid || hour_invalid) && matching == Alarm2Matching::AllMatch)
|
||||
|| (hour_invalid && matching == Alarm2Matching::HoursAndMinutesMatch)
|
||||
|| (matching != Alarm2Matching::OncePerMinute && minute_invalid)
|
||||
{
|
||||
return Err(Error::InvalidInputData);
|
||||
}
|
||||
|
||||
let match_mask = get_matching_mask_alarm2(matching);
|
||||
let mut data = [
|
||||
Register::ALARM2_MINUTES,
|
||||
decimal_to_packed_bcd(when.minute) | match_mask[0],
|
||||
hours_to_register(when.hour)? | match_mask[1],
|
||||
decimal_to_packed_bcd(when.day) | match_mask[2],
|
||||
decimal_to_packed_bcd(minute) | match_mask[0],
|
||||
hours_to_register(hour)? | match_mask[1],
|
||||
decimal_to_packed_bcd(day) | match_mask[2],
|
||||
];
|
||||
self.iface.write_data(&mut data)
|
||||
}
|
||||
|
||||
/// Set Alarm2 for weekday.
|
||||
/// Set Alarm2 for a time (fires when hours and minutes match).
|
||||
///
|
||||
/// 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>> {
|
||||
let alarm = DayAlarm2 {
|
||||
day: 1,
|
||||
hour: Hours::H24(when.hour() as u8),
|
||||
minute: when.minute() as u8,
|
||||
};
|
||||
self.set_alarm2_day(alarm, Alarm2Matching::HoursAndMinutesMatch)
|
||||
}
|
||||
|
||||
/// Set Alarm2 for weekday.
|
||||
///
|
||||
/// Will return an `Error::InvalidInputData` if any of the used parameters
|
||||
/// (depending on the matching startegy) is out of range. Any unused
|
||||
/// parameter is set to the corresponding minimum valid value:
|
||||
/// - Minute, hour: 0
|
||||
/// - Weekday: 1
|
||||
pub fn set_alarm2_weekday(
|
||||
&mut self,
|
||||
when: WeekdayAlarm2,
|
||||
matching: Alarm2Matching,
|
||||
) -> Result<(), Error<CommE, PinE>> {
|
||||
if when.weekday < 1 || when.weekday > 7 || when.minute > 59 {
|
||||
) -> Result<(), Error<E>> {
|
||||
let weekday_invalid = when.weekday < 1 || when.weekday > 7;
|
||||
let hour_invalid = is_hour_valid(when.hour);
|
||||
let minute_invalid = when.minute > 59;
|
||||
|
||||
let weekday = if weekday_invalid { 1 } else { when.weekday };
|
||||
let hour = amend_hour(when.hour);
|
||||
let minute = if minute_invalid { 0 } else { when.minute };
|
||||
|
||||
if (matching == Alarm2Matching::AllMatch && (weekday_invalid || hour_invalid))
|
||||
|| (matching == Alarm2Matching::HoursAndMinutesMatch && hour_invalid)
|
||||
|| (minute_invalid && matching != Alarm2Matching::OncePerMinute)
|
||||
{
|
||||
return Err(Error::InvalidInputData);
|
||||
}
|
||||
let match_mask = get_matching_mask_alarm2(matching);
|
||||
let mut data = [
|
||||
Register::ALARM2_MINUTES,
|
||||
decimal_to_packed_bcd(when.minute) | match_mask[0],
|
||||
hours_to_register(when.hour)? | match_mask[1],
|
||||
decimal_to_packed_bcd(when.weekday) | match_mask[2] | BitFlags::WEEKDAY,
|
||||
decimal_to_packed_bcd(minute) | match_mask[0],
|
||||
hours_to_register(hour)? | match_mask[1],
|
||||
decimal_to_packed_bcd(weekday) | match_mask[2] | BitFlags::WEEKDAY,
|
||||
];
|
||||
self.iface.write_data(&mut data)
|
||||
}
|
||||
|
|
|
@ -1,20 +1,22 @@
|
|||
//! Device configuration
|
||||
|
||||
use super::super::{BitFlags, Ds323x, Error, Register, SqWFreq};
|
||||
use interface::{ReadData, WriteData};
|
||||
use crate::{
|
||||
interface::{ReadData, WriteData},
|
||||
BitFlags, Ds323x, Error, Register, SqWFreq,
|
||||
};
|
||||
|
||||
impl<DI, IC, CommE, PinE> Ds323x<DI, IC>
|
||||
impl<DI, IC, E> Ds323x<DI, IC>
|
||||
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).
|
||||
pub fn enable(&mut self) -> Result<(), Error<CommE, PinE>> {
|
||||
pub fn enable(&mut self) -> Result<(), Error<E>> {
|
||||
let control = self.control;
|
||||
self.write_control(control & !BitFlags::EOSC)
|
||||
}
|
||||
|
||||
/// 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;
|
||||
self.write_control(control | BitFlags::EOSC)
|
||||
}
|
||||
|
@ -22,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<CommE, PinE>> {
|
||||
pub fn convert_temperature(&mut self) -> Result<(), Error<E>> {
|
||||
let control = self.iface.read_register(Register::CONTROL)?;
|
||||
// do not overwrite if a conversion is in progress
|
||||
if (control & BitFlags::TEMP_CONV) == 0 {
|
||||
|
@ -33,90 +35,89 @@ where
|
|||
}
|
||||
|
||||
/// 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;
|
||||
self.write_status_without_clearing_alarm(status)
|
||||
}
|
||||
|
||||
/// 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;
|
||||
self.write_status_without_clearing_alarm(status)
|
||||
}
|
||||
|
||||
/// 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
|
||||
.write_register(Register::AGING_OFFSET, offset as u8)
|
||||
}
|
||||
|
||||
/// Read the aging offset.
|
||||
pub fn get_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)?;
|
||||
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<CommE, PinE>> {
|
||||
pub fn use_int_sqw_output_as_interrupt(&mut self) -> Result<(), Error<E>> {
|
||||
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<CommE, PinE>> {
|
||||
pub fn use_int_sqw_output_as_square_wave(&mut self) -> Result<(), Error<E>> {
|
||||
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<CommE, PinE>> {
|
||||
pub fn enable_square_wave(&mut self) -> Result<(), Error<E>> {
|
||||
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<CommE, PinE>> {
|
||||
pub fn disable_square_wave(&mut self) -> Result<(), Error<E>> {
|
||||
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<CommE, PinE>> {
|
||||
let new_control;
|
||||
match freq {
|
||||
SqWFreq::_1Hz => new_control = self.control & !BitFlags::RS2 & !BitFlags::RS1,
|
||||
SqWFreq::_1_024Hz => new_control = self.control & !BitFlags::RS2 | BitFlags::RS1,
|
||||
SqWFreq::_4_096Hz => new_control = self.control | BitFlags::RS2 & !BitFlags::RS1,
|
||||
SqWFreq::_8_192Hz => new_control = self.control | BitFlags::RS2 | BitFlags::RS1,
|
||||
}
|
||||
pub fn set_square_wave_frequency(&mut self, freq: SqWFreq) -> Result<(), Error<E>> {
|
||||
let new_control = match freq {
|
||||
SqWFreq::_1Hz => self.control & !BitFlags::RS2 & !BitFlags::RS1,
|
||||
SqWFreq::_1_024Hz => self.control & !BitFlags::RS2 | BitFlags::RS1,
|
||||
SqWFreq::_4_096Hz => self.control | BitFlags::RS2 & !BitFlags::RS1,
|
||||
SqWFreq::_8_192Hz => self.control | BitFlags::RS2 | BitFlags::RS1,
|
||||
};
|
||||
self.write_control(new_control)
|
||||
}
|
||||
|
||||
/// 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;
|
||||
self.write_control(control | BitFlags::ALARM1_INT_EN)
|
||||
}
|
||||
|
||||
/// 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;
|
||||
self.write_control(control & !BitFlags::ALARM1_INT_EN)
|
||||
}
|
||||
|
||||
/// 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;
|
||||
self.write_control(control | BitFlags::ALARM2_INT_EN)
|
||||
}
|
||||
|
||||
/// 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;
|
||||
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.control = control;
|
||||
Ok(())
|
||||
|
@ -125,7 +126,7 @@ where
|
|||
pub(crate) fn write_status_without_clearing_alarm(
|
||||
&mut self,
|
||||
status: u8,
|
||||
) -> Result<(), Error<CommE, PinE>> {
|
||||
) -> Result<(), Error<E>> {
|
||||
// avoid clearing alarm flags
|
||||
let new_status = status | BitFlags::ALARM2F | BitFlags::ALARM1F;
|
||||
self.iface.write_register(Register::STATUS, new_status)?;
|
||||
|
|
|
@ -1,81 +1,21 @@
|
|||
//! Common implementation
|
||||
|
||||
use super::super::{BitFlags, Ds323x, Error, Register};
|
||||
use super::{decimal_to_packed_bcd, hours_to_register, packed_bcd_to_decimal};
|
||||
use super::{Datelike, Hours, NaiveDate, NaiveDateTime, NaiveTime, Rtcc, Timelike};
|
||||
use interface::{ReadData, WriteData};
|
||||
use super::{
|
||||
decimal_to_packed_bcd, hours_to_register, packed_bcd_to_decimal, some_or_invalid_error,
|
||||
};
|
||||
use crate::{
|
||||
interface::{ReadData, WriteData},
|
||||
BitFlags, DateTimeAccess, Datelike, Ds323x, Error, Hours, NaiveDate, NaiveDateTime, NaiveTime,
|
||||
Register, Rtcc, Timelike,
|
||||
};
|
||||
|
||||
impl<DI, IC, CommE, PinE> Rtcc for Ds323x<DI, IC>
|
||||
impl<DI, IC, E> DateTimeAccess for Ds323x<DI, IC>
|
||||
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 get_seconds(&mut self) -> Result<u8, Self::Error> {
|
||||
self.read_register_decimal(Register::SECONDS)
|
||||
}
|
||||
|
||||
fn get_minutes(&mut self) -> Result<u8, Self::Error> {
|
||||
self.read_register_decimal(Register::MINUTES)
|
||||
}
|
||||
|
||||
fn get_hours(&mut self) -> Result<Hours, Self::Error> {
|
||||
let data = self.iface.read_register(Register::HOURS)?;
|
||||
Ok(hours_from_register(data))
|
||||
}
|
||||
|
||||
fn get_time(&mut self) -> Result<NaiveTime, Self::Error> {
|
||||
let mut data = [0; 4];
|
||||
self.iface.read_data(&mut data)?;
|
||||
let hour = hours_from_register(data[Register::HOURS as usize + 1]);
|
||||
let minute = packed_bcd_to_decimal(data[Register::MINUTES as usize + 1]);
|
||||
let second = packed_bcd_to_decimal(data[Register::SECONDS as usize + 1]);
|
||||
|
||||
Ok(NaiveTime::from_hms(
|
||||
get_h24(hour).into(),
|
||||
minute.into(),
|
||||
second.into(),
|
||||
))
|
||||
}
|
||||
|
||||
fn get_weekday(&mut self) -> Result<u8, Self::Error> {
|
||||
self.read_register_decimal(Register::DOW)
|
||||
}
|
||||
|
||||
fn get_day(&mut self) -> Result<u8, Self::Error> {
|
||||
self.read_register_decimal(Register::DOM)
|
||||
}
|
||||
|
||||
fn get_month(&mut self) -> Result<u8, Self::Error> {
|
||||
let data = self.iface.read_register(Register::MONTH)?;
|
||||
let value = data & !BitFlags::CENTURY;
|
||||
Ok(packed_bcd_to_decimal(value))
|
||||
}
|
||||
|
||||
fn get_year(&mut self) -> Result<u16, Self::Error> {
|
||||
let mut data = [0; 3];
|
||||
data[0] = Register::MONTH;
|
||||
self.iface.read_data(&mut data)?;
|
||||
Ok(year_from_registers(data[1], data[2]))
|
||||
}
|
||||
|
||||
fn get_date(&mut self) -> Result<NaiveDate, Self::Error> {
|
||||
let mut data = [0; 4];
|
||||
data[0] = Register::DOM;
|
||||
self.iface.read_data(&mut data)?;
|
||||
|
||||
let offset = Register::DOM as usize;
|
||||
let year = year_from_registers(
|
||||
data[Register::MONTH as usize + 1 - offset],
|
||||
data[Register::YEAR as usize + 1 - offset],
|
||||
);
|
||||
let month =
|
||||
packed_bcd_to_decimal(data[Register::MONTH as usize + 1 - offset] & !BitFlags::CENTURY);
|
||||
let day = packed_bcd_to_decimal(data[Register::DOM as usize + 1 - offset]);
|
||||
Ok(NaiveDate::from_ymd(year.into(), month.into(), day.into()))
|
||||
}
|
||||
|
||||
fn get_datetime(&mut self) -> Result<NaiveDateTime, Self::Error> {
|
||||
fn datetime(&mut self) -> Result<NaiveDateTime, Self::Error> {
|
||||
let mut data = [0; 8];
|
||||
self.iface.read_data(&mut data)?;
|
||||
|
||||
|
@ -89,13 +29,95 @@ where
|
|||
let minute = packed_bcd_to_decimal(data[Register::MINUTES as usize + 1]);
|
||||
let second = packed_bcd_to_decimal(data[Register::SECONDS as usize + 1]);
|
||||
|
||||
Ok(
|
||||
rtcc::NaiveDate::from_ymd(year.into(), month.into(), day.into()).and_hms(
|
||||
get_h24(hour).into(),
|
||||
minute.into(),
|
||||
second.into(),
|
||||
),
|
||||
)
|
||||
let date = NaiveDate::from_ymd_opt(year.into(), month.into(), day.into());
|
||||
let date = some_or_invalid_error(date)?;
|
||||
let datetime = date.and_hms_opt(get_h24(hour).into(), minute.into(), second.into());
|
||||
some_or_invalid_error(datetime)
|
||||
}
|
||||
|
||||
fn set_datetime(&mut self, datetime: &NaiveDateTime) -> Result<(), Self::Error> {
|
||||
if datetime.year() < 2000 || datetime.year() > 2100 {
|
||||
return Err(Error::InvalidInputData);
|
||||
}
|
||||
let (month, year) = month_year_to_registers(datetime.month() as u8, datetime.year() as u16);
|
||||
let mut payload = [
|
||||
Register::SECONDS,
|
||||
decimal_to_packed_bcd(datetime.second() as u8),
|
||||
decimal_to_packed_bcd(datetime.minute() as u8),
|
||||
hours_to_register(Hours::H24(datetime.hour() as u8))?,
|
||||
datetime.weekday().number_from_sunday() as u8,
|
||||
decimal_to_packed_bcd(datetime.day() as u8),
|
||||
month,
|
||||
year,
|
||||
];
|
||||
self.iface.write_data(&mut payload)
|
||||
}
|
||||
}
|
||||
|
||||
impl<DI, IC, E> Rtcc for Ds323x<DI, IC>
|
||||
where
|
||||
DI: ReadData<Error = Error<E>> + WriteData<Error = Error<E>>,
|
||||
{
|
||||
fn seconds(&mut self) -> Result<u8, Self::Error> {
|
||||
self.read_register_decimal(Register::SECONDS)
|
||||
}
|
||||
|
||||
fn minutes(&mut self) -> Result<u8, Self::Error> {
|
||||
self.read_register_decimal(Register::MINUTES)
|
||||
}
|
||||
|
||||
fn hours(&mut self) -> Result<Hours, Self::Error> {
|
||||
let data = self.iface.read_register(Register::HOURS)?;
|
||||
Ok(hours_from_register(data))
|
||||
}
|
||||
|
||||
fn time(&mut self) -> Result<NaiveTime, Self::Error> {
|
||||
let mut data = [0; 4];
|
||||
self.iface.read_data(&mut data)?;
|
||||
let hour = hours_from_register(data[Register::HOURS as usize + 1]);
|
||||
let minute = packed_bcd_to_decimal(data[Register::MINUTES as usize + 1]);
|
||||
let second = packed_bcd_to_decimal(data[Register::SECONDS as usize + 1]);
|
||||
|
||||
let time = NaiveTime::from_hms_opt(get_h24(hour).into(), minute.into(), second.into());
|
||||
some_or_invalid_error(time)
|
||||
}
|
||||
|
||||
fn weekday(&mut self) -> Result<u8, Self::Error> {
|
||||
self.read_register_decimal(Register::DOW)
|
||||
}
|
||||
|
||||
fn day(&mut self) -> Result<u8, Self::Error> {
|
||||
self.read_register_decimal(Register::DOM)
|
||||
}
|
||||
|
||||
fn month(&mut self) -> Result<u8, Self::Error> {
|
||||
let data = self.iface.read_register(Register::MONTH)?;
|
||||
let value = data & !BitFlags::CENTURY;
|
||||
Ok(packed_bcd_to_decimal(value))
|
||||
}
|
||||
|
||||
fn year(&mut self) -> Result<u16, Self::Error> {
|
||||
let mut data = [0; 3];
|
||||
data[0] = Register::MONTH;
|
||||
self.iface.read_data(&mut data)?;
|
||||
Ok(year_from_registers(data[1], data[2]))
|
||||
}
|
||||
|
||||
fn date(&mut self) -> Result<NaiveDate, Self::Error> {
|
||||
let mut data = [0; 4];
|
||||
data[0] = Register::DOM;
|
||||
self.iface.read_data(&mut data)?;
|
||||
|
||||
let offset = Register::DOM as usize;
|
||||
let year = year_from_registers(
|
||||
data[Register::MONTH as usize + 1 - offset],
|
||||
data[Register::YEAR as usize + 1 - offset],
|
||||
);
|
||||
let month =
|
||||
packed_bcd_to_decimal(data[Register::MONTH as usize + 1 - offset] & !BitFlags::CENTURY);
|
||||
let day = packed_bcd_to_decimal(data[Register::DOM as usize + 1 - offset]);
|
||||
let date = NaiveDate::from_ymd_opt(year.into(), month.into(), day.into());
|
||||
some_or_invalid_error(date)
|
||||
}
|
||||
|
||||
fn set_seconds(&mut self, seconds: u8) -> Result<(), Self::Error> {
|
||||
|
@ -128,21 +150,21 @@ where
|
|||
}
|
||||
|
||||
fn set_weekday(&mut self, weekday: u8) -> Result<(), Self::Error> {
|
||||
if weekday < 1 || weekday > 7 {
|
||||
if !(1..=7).contains(&weekday) {
|
||||
return Err(Error::InvalidInputData);
|
||||
}
|
||||
self.iface.write_register(Register::DOW, weekday)
|
||||
}
|
||||
|
||||
fn set_day(&mut self, day: u8) -> Result<(), Self::Error> {
|
||||
if day < 1 || day > 7 {
|
||||
if !(1..=31).contains(&day) {
|
||||
return Err(Error::InvalidInputData);
|
||||
}
|
||||
self.iface.write_register(Register::DOM, day)
|
||||
self.write_register_decimal(Register::DOM, day)
|
||||
}
|
||||
|
||||
fn set_month(&mut self, month: u8) -> Result<(), Self::Error> {
|
||||
if month < 1 || month > 12 {
|
||||
if !(1..=12).contains(&month) {
|
||||
return Err(Error::InvalidInputData);
|
||||
}
|
||||
// keep the century bit
|
||||
|
@ -152,7 +174,7 @@ where
|
|||
}
|
||||
|
||||
fn set_year(&mut self, year: u16) -> Result<(), Self::Error> {
|
||||
if year < 2000 || year > 2100 {
|
||||
if !(2000..=2100).contains(&year) {
|
||||
return Err(Error::InvalidInputData);
|
||||
}
|
||||
let data = self.iface.read_register(Register::MONTH)?;
|
||||
|
@ -188,40 +210,18 @@ where
|
|||
];
|
||||
self.iface.write_data(&mut payload)
|
||||
}
|
||||
|
||||
fn set_datetime(&mut self, datetime: &NaiveDateTime) -> Result<(), Self::Error> {
|
||||
if datetime.year() < 2000 || datetime.year() > 2100 {
|
||||
return Err(Error::InvalidInputData);
|
||||
}
|
||||
let (month, year) = month_year_to_registers(datetime.month() as u8, datetime.year() as u16);
|
||||
let mut payload = [
|
||||
Register::SECONDS,
|
||||
decimal_to_packed_bcd(datetime.second() as u8),
|
||||
decimal_to_packed_bcd(datetime.minute() as u8),
|
||||
hours_to_register(Hours::H24(datetime.hour() as u8))?,
|
||||
datetime.weekday().number_from_sunday() as u8,
|
||||
decimal_to_packed_bcd(datetime.day() as u8),
|
||||
month,
|
||||
year,
|
||||
];
|
||||
self.iface.write_data(&mut payload)
|
||||
}
|
||||
}
|
||||
|
||||
impl<DI, IC, CommE, PinE> Ds323x<DI, IC>
|
||||
impl<DI, IC, E> Ds323x<DI, IC>
|
||||
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)?;
|
||||
Ok(packed_bcd_to_decimal(data))
|
||||
}
|
||||
|
||||
fn write_register_decimal(
|
||||
&mut self,
|
||||
register: u8,
|
||||
decimal_number: u8,
|
||||
) -> Result<(), Error<CommE, PinE>> {
|
||||
fn write_register_decimal(&mut self, register: u8, decimal_number: u8) -> Result<(), Error<E>> {
|
||||
self.iface
|
||||
.write_register(register, decimal_to_packed_bcd(decimal_number))
|
||||
}
|
||||
|
|
|
@ -5,8 +5,7 @@ pub use self::alarms::{
|
|||
Alarm1Matching, Alarm2Matching, DayAlarm1, DayAlarm2, WeekdayAlarm1, WeekdayAlarm2,
|
||||
};
|
||||
mod datetime;
|
||||
use super::{BitFlags, Error};
|
||||
use super::{Datelike, Hours, NaiveDate, NaiveDateTime, NaiveTime, Rtcc, Timelike};
|
||||
use crate::{BitFlags, Error, Hours, NaiveTime, Timelike};
|
||||
|
||||
// Transforms a decimal number to packed BCD format
|
||||
fn decimal_to_packed_bcd(dec: u8) -> u8 {
|
||||
|
@ -18,21 +17,45 @@ fn packed_bcd_to_decimal(bcd: u8) -> u8 {
|
|||
(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 {
|
||||
Hours::H24(h) if h > 23 => Err(Error::InvalidInputData),
|
||||
Hours::H24(h) => Ok(decimal_to_packed_bcd(h)),
|
||||
Hours::AM(h) if h < 1 || h > 12 => Err(Error::InvalidInputData),
|
||||
Hours::AM(h) if !(1..=12).contains(&h) => Err(Error::InvalidInputData),
|
||||
Hours::AM(h) => Ok(BitFlags::H24_H12 | decimal_to_packed_bcd(h)),
|
||||
Hours::PM(h) if h < 1 || h > 12 => Err(Error::InvalidInputData),
|
||||
Hours::PM(h) if !(1..=12).contains(&h) => Err(Error::InvalidInputData),
|
||||
Hours::PM(h) => Ok(BitFlags::H24_H12 | BitFlags::AM_PM | decimal_to_packed_bcd(h)),
|
||||
}
|
||||
}
|
||||
|
||||
fn some_or_invalid_error<T, E>(data: Option<T>) -> Result<T, Error<E>> {
|
||||
if let Some(data) = data {
|
||||
Ok(data)
|
||||
} else {
|
||||
Err(Error::InvalidDeviceState)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn if_some_then_get_inner() {
|
||||
match some_or_invalid_error::<u8, ()>(Some(1)) {
|
||||
Ok(1) => (),
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn if_none_then_error() {
|
||||
match some_or_invalid_error::<u8, ()>(None) {
|
||||
Err(Error::InvalidDeviceState) => (),
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_convert_packed_bcd_to_decimal() {
|
||||
assert_eq!(0, packed_bcd_to_decimal(0b0000_0000));
|
||||
|
|
|
@ -1,20 +1,22 @@
|
|||
//! Device status
|
||||
|
||||
use super::super::{BitFlags, Ds323x, Error, Register};
|
||||
use interface::{ReadData, WriteData};
|
||||
use crate::{
|
||||
interface::{ReadData, WriteData},
|
||||
BitFlags, Ds323x, Error, Register,
|
||||
};
|
||||
|
||||
impl<DI, IC, CommE, PinE> Ds323x<DI, IC>
|
||||
impl<DI, IC, E> Ds323x<DI, IC>
|
||||
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
|
||||
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)?;
|
||||
Ok((control & BitFlags::EOSC) == 0)
|
||||
}
|
||||
|
||||
/// 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)?;
|
||||
Ok((status & BitFlags::BUSY) != 0)
|
||||
}
|
||||
|
@ -26,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<CommE, PinE>> {
|
||||
pub fn has_been_stopped(&mut self) -> Result<bool, Error<E>> {
|
||||
let status = self.iface.read_register(Register::STATUS)?;
|
||||
Ok((status & BitFlags::OSC_STOP) != 0)
|
||||
}
|
||||
|
@ -35,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<CommE, PinE>> {
|
||||
pub fn clear_has_been_stopped_flag(&mut self) -> Result<(), Error<E>> {
|
||||
let status = self.status & !BitFlags::OSC_STOP;
|
||||
self.write_status_without_clearing_alarm(status)
|
||||
}
|
||||
|
@ -44,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<CommE, PinE>> {
|
||||
pub fn has_alarm1_matched(&mut self) -> Result<bool, Error<E>> {
|
||||
let status = self.iface.read_register(Register::STATUS)?;
|
||||
Ok((status & BitFlags::ALARM1F) != 0)
|
||||
}
|
||||
|
@ -52,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<CommE, PinE>> {
|
||||
pub fn clear_alarm1_matched_flag(&mut self) -> Result<(), Error<E>> {
|
||||
let status = self.status | BitFlags::ALARM2F;
|
||||
self.iface.write_register(Register::STATUS, status)
|
||||
}
|
||||
|
@ -61,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<CommE, PinE>> {
|
||||
pub fn has_alarm2_matched(&mut self) -> Result<bool, Error<E>> {
|
||||
let status = self.iface.read_register(Register::STATUS)?;
|
||||
Ok((status & BitFlags::ALARM2F) != 0)
|
||||
}
|
||||
|
@ -69,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<CommE, PinE>> {
|
||||
pub fn clear_alarm2_matched_flag(&mut self) -> Result<(), Error<E>> {
|
||||
let status = self.status | BitFlags::ALARM1F;
|
||||
self.iface.write_register(Register::STATUS, status)
|
||||
}
|
||||
|
@ -78,7 +80,7 @@ where
|
|||
///
|
||||
/// Note: It is possible to manually force a temperature conversion with
|
||||
/// [`convert_temperature()`](#method.convert_temperature)
|
||||
pub fn get_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];
|
||||
self.iface.read_data(&mut data)?;
|
||||
let is_negative = (data[1] & 0b1000_0000) != 0;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
//! I2C/SPI interfaces
|
||||
|
||||
use super::{private, Error, DEVICE_ADDRESS};
|
||||
use hal::blocking;
|
||||
use crate::{private, Error, DEVICE_ADDRESS};
|
||||
use embedded_hal::{i2c, spi};
|
||||
|
||||
/// I2C interface
|
||||
#[derive(Debug, Default)]
|
||||
|
@ -11,9 +11,8 @@ pub struct I2cInterface<I2C> {
|
|||
|
||||
/// SPI interface
|
||||
#[derive(Debug, Default)]
|
||||
pub struct SpiInterface<SPI, CS> {
|
||||
pub struct SpiInterface<SPI> {
|
||||
pub(crate) spi: SPI,
|
||||
pub(crate) cs: CS,
|
||||
}
|
||||
|
||||
/// Write data
|
||||
|
@ -28,9 +27,9 @@ pub trait WriteData: private::Sealed {
|
|||
|
||||
impl<I2C, E> WriteData for I2cInterface<I2C>
|
||||
where
|
||||
I2C: blocking::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> {
|
||||
let payload: [u8; 2] = [register, data];
|
||||
self.i2c
|
||||
|
@ -39,35 +38,23 @@ where
|
|||
}
|
||||
|
||||
fn write_data(&mut self, payload: &mut [u8]) -> Result<(), Self::Error> {
|
||||
self.i2c
|
||||
.write(DEVICE_ADDRESS, &payload)
|
||||
.map_err(Error::Comm)
|
||||
self.i2c.write(DEVICE_ADDRESS, payload).map_err(Error::Comm)
|
||||
}
|
||||
}
|
||||
|
||||
impl<SPI, CS, CommE, PinE> WriteData for SpiInterface<SPI, CS>
|
||||
impl<SPI, E> WriteData for SpiInterface<SPI>
|
||||
where
|
||||
SPI: blocking::spi::Write<u8, Error = CommE>,
|
||||
CS: hal::digital::v2::OutputPin<Error = PinE>,
|
||||
SPI: spi::SpiDevice<u8, Error = E>,
|
||||
{
|
||||
type Error = Error<CommE, PinE>;
|
||||
type Error = Error<E>;
|
||||
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 result = self.spi.write(&payload).map_err(Error::Comm);
|
||||
|
||||
self.cs.set_high().map_err(Error::Pin)?;
|
||||
result
|
||||
self.spi.write(&payload).map_err(Error::Comm)
|
||||
}
|
||||
|
||||
fn write_data(&mut self, payload: &mut [u8]) -> Result<(), Self::Error> {
|
||||
self.cs.set_low().map_err(Error::Pin)?;
|
||||
payload[0] += 0x80;
|
||||
let result = self.spi.write(&payload).map_err(Error::Comm);
|
||||
|
||||
self.cs.set_high().map_err(Error::Pin)?;
|
||||
result
|
||||
self.spi.write(payload).map_err(Error::Comm)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -83,9 +70,9 @@ pub trait ReadData: private::Sealed {
|
|||
|
||||
impl<I2C, E> ReadData for I2cInterface<I2C>
|
||||
where
|
||||
I2C: blocking::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> {
|
||||
let mut data = [0];
|
||||
self.i2c
|
||||
|
@ -102,25 +89,18 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<SPI, CS, CommE, PinE> ReadData for SpiInterface<SPI, CS>
|
||||
impl<SPI, E> ReadData for SpiInterface<SPI>
|
||||
where
|
||||
SPI: blocking::spi::Transfer<u8, Error = CommE>,
|
||||
CS: hal::digital::v2::OutputPin<Error = PinE>,
|
||||
SPI: spi::SpiDevice<u8, Error = E>,
|
||||
{
|
||||
type Error = Error<CommE, PinE>;
|
||||
type Error = Error<E>;
|
||||
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(&mut data).map_err(Error::Comm);
|
||||
self.cs.set_high().map_err(Error::Pin)?;
|
||||
Ok(result?[1])
|
||||
let result = self.spi.transfer_in_place(&mut data).map_err(Error::Comm);
|
||||
result.and(Ok(data[1]))
|
||||
}
|
||||
|
||||
fn read_data(&mut self, mut payload: &mut [u8]) -> Result<(), Self::Error> {
|
||||
self.cs.set_low().map_err(Error::Pin)?;
|
||||
let result = self.spi.transfer(&mut payload).map_err(Error::Comm);
|
||||
self.cs.set_high().map_err(Error::Pin)?;
|
||||
result?;
|
||||
Ok(())
|
||||
fn read_data(&mut self, payload: &mut [u8]) -> Result<(), Self::Error> {
|
||||
self.spi.transfer_in_place(payload).map_err(Error::Comm)
|
||||
}
|
||||
}
|
||||
|
|
264
src/lib.rs
264
src/lib.rs
|
@ -4,8 +4,8 @@
|
|||
//! [`embedded-hal`]: https://github.com/rust-embedded/embedded-hal
|
||||
//!
|
||||
//! This driver allows you to:
|
||||
//! - Read and set date and time in 12-hour and 24-hour format. See: [`get_datetime`].
|
||||
//! - Read and set date and time individual elements. For example, see: [`get_year`].
|
||||
//! - Read and set date and time in 12-hour and 24-hour format. See: [`datetime`].
|
||||
//! - Read and set date and time individual elements. For example, see: [`year`].
|
||||
//! - Enable and disable the real-time clock. See: [`enable`].
|
||||
//! - Read the busy status. See [`busy`].
|
||||
//! - Read whether the oscillator is or has been stopped. See [`has_been_stopped`].
|
||||
|
@ -14,6 +14,7 @@
|
|||
//! - Select the function of the INT/SQW output pin. See [`use_int_sqw_output_as_interrupt`].
|
||||
//! - Alarms:
|
||||
//! - Set alarms 1 and 2 with several matching policies. See [`set_alarm1_day`].
|
||||
//! - Set alarms 1 and 2 for a time. See [`set_alarm1_hms`].
|
||||
//! - Read whether alarms 1 or 2 have matched. See [`has_alarm1_matched`].
|
||||
//! - Clear flag indicating that alarms 1 or 2 have matched. See [`clear_alarm1_matched_flag`].
|
||||
//! - Enable and disable alarms 1 and 2 interrupt generation. See [`enable_alarm1_interrupts`].
|
||||
|
@ -23,47 +24,50 @@
|
|||
//! - Enable and disable the 32kHz output. See [`enable_32khz_output`].
|
||||
//! - Enable and disable the 32kHz output when battery powered. See [`enable_32khz_output_on_battery`].
|
||||
//! - Temperature conversion:
|
||||
//! - Read the temperature. See [`get_temperature`].
|
||||
//! - Read the temperature. See [`temperature`].
|
||||
//! - Force a temperature conversion and time compensation. See [`convert_temperature`].
|
||||
//! - Set the temperature conversion rate. See [`set_temperature_conversion_rate`].
|
||||
//! - Enable and disable the temperature conversions when battery-powered. See [`enable_temperature_conversions_on_battery`].
|
||||
//!
|
||||
//! [`get_datetime`]: struct.Ds323x.html#method.get_datetime
|
||||
//! [`get_year`]: struct.Ds323x.html#method.get_year
|
||||
//! [`enable`]: struct.Ds323x.html#method.enable
|
||||
//! [`get_temperature`]: struct.Ds323x.html#method.get_temperature
|
||||
//! [`convert_temperature`]: struct.Ds323x.html#method.convert_temperature
|
||||
//! [`busy`]: struct.Ds323x.html#method.busy
|
||||
//! [`has_been_stopped`]: struct.Ds323x.html#method.has_been_stopped
|
||||
//! [`clear_has_been_stopped_flag`]: struct.Ds323x.html#method.clear_has_been_stopped_flag
|
||||
//! [`set_aging_offset`]: struct.Ds323x.html#method.set_aging_offset
|
||||
//! [`enable_32khz_output`]: struct.Ds323x.html#method.enable_32khz_output
|
||||
//! [`use_int_sqw_output_as_interrupt`]: struct.Ds323x.html#method.use_int_sqw_output_as_interrupt
|
||||
//! [`enable_square_wave`]: struct.Ds323x.html#method.enable_square_wave
|
||||
//! [`set_square_wave_frequency`]: struct.Ds323x.html#method.set_square_wave_frequency
|
||||
//! [`set_alarm1_day`]: struct.Ds323x.html#method.set_alarm1_day
|
||||
//! [`has_alarm1_matched`]: struct.Ds323x.html#method.has_alarm1_matched
|
||||
//! [`clear_alarm1_matched_flag`]: struct.Ds323x.html#method.clear_alarm1_matched_flag
|
||||
//! [`enable_alarm1_interrupts`]: struct.Ds323x.html#method.enable_alarm1_interrupts
|
||||
//! [`enable_32khz_output_on_battery`]: struct.Ds323x.html#method.enable_32khz_output_on_battery
|
||||
//! [`set_temperature_conversion_rate`]: struct.Ds323x.html#method.set_temperature_conversion_rate
|
||||
//! [`enable_temperature_conversions_on_battery`]: struct.Ds323x.html#method.enable_temperature_conversions_on_battery
|
||||
//! [`datetime`]: Ds323x::datetime
|
||||
//! [`year`]: Ds323x::year
|
||||
//! [`enable`]: Ds323x::enable
|
||||
//! [`temperature`]: Ds323x::temperature
|
||||
//! [`convert_temperature`]: Ds323x::convert_temperature
|
||||
//! [`busy`]: Ds323x::busy
|
||||
//! [`has_been_stopped`]: Ds323x::has_been_stopped
|
||||
//! [`clear_has_been_stopped_flag`]: Ds323x::clear_has_been_stopped_flag
|
||||
//! [`set_aging_offset`]: Ds323x::set_aging_offset
|
||||
//! [`enable_32khz_output`]: Ds323x::enable_32khz_output
|
||||
//! [`use_int_sqw_output_as_interrupt`]: Ds323x::use_int_sqw_output_as_interrupt
|
||||
//! [`enable_square_wave`]: Ds323x::enable_square_wave
|
||||
//! [`set_square_wave_frequency`]: Ds323x::set_square_wave_frequency
|
||||
//! [`set_alarm1_day`]: Ds323x::set_alarm1_day
|
||||
//! [`set_alarm1_hms`]: Ds323x::set_alarm1_hms
|
||||
//! [`has_alarm1_matched`]: Ds323x::has_alarm1_matched
|
||||
//! [`clear_alarm1_matched_flag`]: Ds323x::clear_alarm1_matched_flag
|
||||
//! [`enable_alarm1_interrupts`]: Ds323x::enable_alarm1_interrupts
|
||||
//! [`enable_32khz_output_on_battery`]: Ds323x::enable_32khz_output_on_battery
|
||||
//! [`set_temperature_conversion_rate`]: Ds323x::set_temperature_conversion_rate
|
||||
//! [`enable_temperature_conversions_on_battery`]: Ds323x::enable_temperature_conversions_on_battery
|
||||
//!
|
||||
//! ## The devices
|
||||
//!
|
||||
//! This driver is compatible with the DS3231 and DS3232 I2C devices and the
|
||||
//! DS3234 SPI device.
|
||||
//!
|
||||
//! ### DS3231
|
||||
//! The DS3231 is a low-cost, extremely accurate I2C real-time clock (RTC) with
|
||||
//! an integrated temperature-compensated crystal oscillator (TCXO) and crystal.
|
||||
//! These devices are low-cost temperature-compensated crystal oscillator (TCXO)
|
||||
//! with a very accurate, temperature-compensated, integrated real-time clock
|
||||
//! (RTC) including 236/256 bytes of battery-backed SRAM, depending on the model.
|
||||
//!
|
||||
//! The device incorporates a battery input, and maintains accurate timekeeping
|
||||
//! when main power to the device is interrupted. The integration of the
|
||||
//! crystal resonator enhances the long-term accuracy of the device as well as
|
||||
//! ### DS3231 and DS3232 details
|
||||
//!
|
||||
//! The devices incorporate a battery input, and maintain accurate timekeeping
|
||||
//! when main power to the devices is interrupted. The integration of the
|
||||
//! crystal resonator enhances the long-term accuracy of the devices as well as
|
||||
//! reduces the piece-part count in a manufacturing line.
|
||||
//! The DS3231 is available in commercial and industrial temperature ranges,
|
||||
//! and is offered in a 16-pin, 300-mil SO package.
|
||||
//! The devices are available in commercial and industrial temperature ranges,
|
||||
//! and are offered in a 16-pin, 300-mil SO package.
|
||||
//!
|
||||
//! The RTC maintains seconds, minutes, hours, day, date, month, and year
|
||||
//! information. The date at the end of the month is automatically adjusted for
|
||||
|
@ -79,36 +83,7 @@
|
|||
//! necessary. Additionally, the RST pin is monitored as a pushbutton
|
||||
//! input for generating a μP reset.
|
||||
//!
|
||||
//! ### DS3232
|
||||
//! The DS3232 is a low-cost temperature-compensated crystal oscillator (TCXO)
|
||||
//! with a very accurate, temperature-compensated, integrated real-time clock
|
||||
//! (RTC) and 236 bytes of battery-backed SRAM.
|
||||
//!
|
||||
//! Additionally, the DS3232 incorporates a battery input and maintains
|
||||
//! accurate timekeeping when main power to the device is interrupted. The
|
||||
//! integration of the crystal resonator enhances the long-term accuracy of the
|
||||
//! device as well as reduces the piece-part count in a manufacturing line.
|
||||
//! The DS3232 is available in commercial and industrial temperature ranges,
|
||||
//! and is offered in an industry-standard 20-pin, 300-mil SO package.
|
||||
//!
|
||||
//! The RTC maintains seconds, minutes, hours, day, date, month, and year
|
||||
//! information. The date at the end of the month is automatically adjusted for
|
||||
//! months with fewer than 31 days, including corrections for leap year. The
|
||||
//! clock operates in either the 24-hour or 12-hour format with an AM/PM
|
||||
//! indicator. Two programmable time-of-day alarms and a programmable
|
||||
//! square-wave output are provided. Address and data are transferred serially
|
||||
//! through an I2C bidirectional bus.
|
||||
//!
|
||||
//! A precision temperature-compensated voltage reference and comparator
|
||||
//! circuit monitors the status of VCC to detect power failures, to provide a
|
||||
//! reset output, and to automatically switch to the backup supply when
|
||||
//! necessary. Additionally, the RST pin is monitored as a pushbutton input for
|
||||
//! generating a μP reset.
|
||||
//!
|
||||
//! ### DS3234
|
||||
//! The DS3234 is a low-cost, extremely accurate SPI bus real-time clock (RTC)
|
||||
//! with an integrated temperature-compensated crystal oscillator (TCXO) and
|
||||
//! crystal.
|
||||
//! ### DS3234 details
|
||||
//!
|
||||
//! The DS3234 incorporates a precision, temperature-compensated voltage
|
||||
//! reference and comparator circuit to monitor VCC. When VCC drops below the
|
||||
|
@ -153,84 +128,71 @@
|
|||
//! ### Create a driver instance for the DS3231
|
||||
//!
|
||||
//! ```no_run
|
||||
//! extern crate ds323x;
|
||||
//! extern crate linux_embedded_hal as hal;
|
||||
//! use ds323x::Ds323x;
|
||||
//! use linux_embedded_hal::I2cdev;
|
||||
//!
|
||||
//! # fn main() {
|
||||
//! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let rtc = Ds323x::new_ds3231(dev);
|
||||
//! // do something...
|
||||
//!
|
||||
//! // get the I2C device back
|
||||
//! let dev = rtc.destroy_ds3231();
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ### Create a driver instance for the DS3232
|
||||
//!
|
||||
//! ```no_run
|
||||
//! extern crate ds323x;
|
||||
//! extern crate linux_embedded_hal as hal;
|
||||
//! use ds323x::Ds323x;
|
||||
//! use linux_embedded_hal::I2cdev;
|
||||
//!
|
||||
//! # fn main() {
|
||||
//! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let rtc = Ds323x::new_ds3232(dev);
|
||||
//! // do something...
|
||||
//!
|
||||
//! // get the I2C device back
|
||||
//! let dev = rtc.destroy_ds3232();
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ### Create a driver instance for the DS3234
|
||||
//!
|
||||
//! ```no_run
|
||||
//! extern crate ds323x;
|
||||
//! extern crate linux_embedded_hal as hal;
|
||||
//! use ds323x::Ds323x;
|
||||
//! use embedded_hal_bus::spi::ExclusiveDevice;
|
||||
//! use linux_embedded_hal::{Delay, SpidevBus, SysfsPin};
|
||||
//!
|
||||
//! # fn main() {
|
||||
//! let dev = hal::Spidev::open("/dev/spidev0.0").unwrap();
|
||||
//! let chip_select = hal::Pin::new(24);
|
||||
//! let rtc = Ds323x::new_ds3234(dev, chip_select);
|
||||
//! 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);
|
||||
//! // do something...
|
||||
//!
|
||||
//! // get the SPI device and chip select pin back
|
||||
//! let (dev, chip_select) = rtc.destroy_ds3234();
|
||||
//! # }
|
||||
//! // get the SPI device back
|
||||
//! let dev = rtc.destroy_ds3234();
|
||||
//! ```
|
||||
//!
|
||||
//! ### Set the current date and time at once
|
||||
//!
|
||||
//! ```no_run
|
||||
//! extern crate ds323x;
|
||||
//! extern crate linux_embedded_hal as hal;
|
||||
//! use ds323x::{Ds323x, NaiveDate, Rtcc};
|
||||
//! use ds323x::{Ds323x, NaiveDate, DateTimeAccess};
|
||||
//! use linux_embedded_hal::I2cdev;
|
||||
//!
|
||||
//! # fn main() {
|
||||
//! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let mut rtc = Ds323x::new_ds3231(dev);
|
||||
//! let datetime = NaiveDate::from_ymd(2020, 5, 1).and_hms(19, 59, 58);
|
||||
//! rtc.set_datetime(&datetime).unwrap();
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ### Get the current date and time at once
|
||||
//!
|
||||
//! ```no_run
|
||||
//! extern crate ds323x;
|
||||
//! extern crate linux_embedded_hal as hal;
|
||||
//! use ds323x::{Ds323x, Rtcc};
|
||||
//! use ds323x::{Ds323x, DateTimeAccess};
|
||||
//! use linux_embedded_hal::I2cdev;
|
||||
//!
|
||||
//! # fn main() {
|
||||
//! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let mut rtc = Ds323x::new_ds3231(dev);
|
||||
//! let dt = rtc.get_datetime().unwrap();
|
||||
//! let dt = rtc.datetime().unwrap();
|
||||
//! println!("{}", dt);
|
||||
//! // This will print something like: 2020-05-01 19:59:58
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ### Get the year
|
||||
|
@ -238,16 +200,13 @@
|
|||
//! Similar methods exist for month, day, weekday, hours, minutes and seconds.
|
||||
//!
|
||||
//! ```no_run
|
||||
//! extern crate ds323x;
|
||||
//! extern crate linux_embedded_hal as hal;
|
||||
//! use ds323x::{Ds323x, Rtcc};
|
||||
//! use linux_embedded_hal::I2cdev;
|
||||
//!
|
||||
//! # fn main() {
|
||||
//! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let mut rtc = Ds323x::new_ds3231(dev);
|
||||
//! let year = rtc.get_year().unwrap();
|
||||
//! let year = rtc.year().unwrap();
|
||||
//! println!("Year: {}", year);
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ### Set the year
|
||||
|
@ -255,78 +214,63 @@
|
|||
//! Similar methods exist for month, day, weekday, hours, minutes and seconds.
|
||||
//!
|
||||
//! ```no_run
|
||||
//! extern crate ds323x;
|
||||
//! extern crate linux_embedded_hal as hal;
|
||||
//! use ds323x::{Ds323x, Rtcc};
|
||||
//! use linux_embedded_hal::I2cdev;
|
||||
//!
|
||||
//! # fn main() {
|
||||
//! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let mut rtc = Ds323x::new_ds3231(dev);
|
||||
//! rtc.set_year(2018).unwrap();
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ### Enable/disable the device
|
||||
//!
|
||||
//! ```no_run
|
||||
//! extern crate ds323x;
|
||||
//! extern crate linux_embedded_hal as hal;
|
||||
//! use ds323x::Ds323x;
|
||||
//! use linux_embedded_hal::I2cdev;
|
||||
//!
|
||||
//! # fn main() {
|
||||
//! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let mut rtc = Ds323x::new_ds3231(dev);
|
||||
//! rtc.disable().unwrap(); // stops the clock
|
||||
//! let running = rtc.running().unwrap();
|
||||
//! println!("Is running: {}", running); // will print false
|
||||
//! rtc.enable().unwrap(); // set clock to run
|
||||
//! println!("Is running: {}", running); // will print true
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ### Read the temperature
|
||||
//!
|
||||
//! ```no_run
|
||||
//! extern crate ds323x;
|
||||
//! extern crate linux_embedded_hal as hal;
|
||||
//! use ds323x::Ds323x;
|
||||
//! use linux_embedded_hal::I2cdev;
|
||||
//!
|
||||
//! # fn main() {
|
||||
//! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let mut rtc = Ds323x::new_ds3231(dev);
|
||||
//! let temperature = rtc.get_temperature().unwrap();
|
||||
//! # }
|
||||
//! let temperature = rtc.temperature().unwrap();
|
||||
//! ```
|
||||
//!
|
||||
//! ### Read busy status
|
||||
//!
|
||||
//! ```no_run
|
||||
//! extern crate ds323x;
|
||||
//! extern crate linux_embedded_hal as hal;
|
||||
//! use ds323x::Ds323x;
|
||||
//! use linux_embedded_hal::I2cdev;
|
||||
//!
|
||||
//! # fn main() {
|
||||
//! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let mut rtc = Ds323x::new_ds3231(dev);
|
||||
//! let busy = rtc.busy().unwrap();
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ### Enable the square-wave output with a frequency of 4.096Hz
|
||||
//!
|
||||
//! ```no_run
|
||||
//! extern crate ds323x;
|
||||
//! extern crate linux_embedded_hal as hal;
|
||||
//! use ds323x::{Ds323x, SqWFreq};
|
||||
//! use linux_embedded_hal::I2cdev;
|
||||
//!
|
||||
//! # fn main() {
|
||||
//! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let mut rtc = Ds323x::new_ds3231(dev);
|
||||
//! rtc.set_square_wave_frequency(SqWFreq::_4_096Hz).unwrap();
|
||||
//! // The same output pin can be used for interrupts or as square-wave output
|
||||
//! rtc.use_int_sqw_output_as_square_wave().unwrap();
|
||||
//! rtc.enable_square_wave().unwrap();
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ### Enable the 32kHz output except when on battery power
|
||||
|
@ -335,30 +279,24 @@
|
|||
//! available for the devices DS3232 and DS3234.
|
||||
//!
|
||||
//! ```no_run
|
||||
//! extern crate ds323x;
|
||||
//! extern crate linux_embedded_hal as hal;
|
||||
//! use ds323x::{Ds323x, SqWFreq};
|
||||
//! use linux_embedded_hal::I2cdev;
|
||||
//!
|
||||
//! # fn main() {
|
||||
//! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let mut rtc = Ds323x::new_ds3232(dev);
|
||||
//! rtc.disable_32khz_output_on_battery().unwrap(); // only available for DS3232 and DS3234
|
||||
//! rtc.enable_32khz_output().unwrap();
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ### Set the aging offset
|
||||
//!
|
||||
//! ```no_run
|
||||
//! extern crate ds323x;
|
||||
//! extern crate linux_embedded_hal as hal;
|
||||
//! use ds323x::Ds323x;
|
||||
//! use linux_embedded_hal::I2cdev;
|
||||
//!
|
||||
//! # fn main() {
|
||||
//! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let mut rtc = Ds323x::new_ds3231(dev);
|
||||
//! rtc.set_aging_offset(-15).unwrap();
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ### Set the temperature conversion rate to once every 128 seconds
|
||||
|
@ -366,26 +304,21 @@
|
|||
//! This is only available for the devices DS3232 and DS3234.
|
||||
//!
|
||||
//! ```no_run
|
||||
//! extern crate ds323x;
|
||||
//! extern crate linux_embedded_hal as hal;
|
||||
//! use ds323x::{Ds323x, TempConvRate};
|
||||
//! use linux_embedded_hal::I2cdev;
|
||||
//!
|
||||
//! # fn main() {
|
||||
//! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let mut rtc = Ds323x::new_ds3232(dev);
|
||||
//! rtc.set_temperature_conversion_rate(TempConvRate::_128s).unwrap();
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ### Set the Alarm1 to each week on a week day at a specific time
|
||||
//!
|
||||
//! ```no_run
|
||||
//! extern crate ds323x;
|
||||
//! extern crate linux_embedded_hal as hal;
|
||||
//! use ds323x::{Ds323x, Hours, WeekdayAlarm1, Alarm1Matching};
|
||||
//! use linux_embedded_hal::I2cdev;
|
||||
//!
|
||||
//! # fn main() {
|
||||
//! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let mut rtc = Ds323x::new_ds3231(dev);
|
||||
//! let alarm1 = WeekdayAlarm1 {
|
||||
//! weekday: 1,
|
||||
|
@ -394,7 +327,6 @@
|
|||
//! second: 15
|
||||
//! };
|
||||
//! rtc.set_alarm1_weekday(alarm1, Alarm1Matching::AllMatch).unwrap();
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ### Set the Alarm2 to each day at the same time and enable interrupts on output
|
||||
|
@ -402,12 +334,10 @@
|
|||
//! The INT/SQW output pin will be set to 1 when it the alarm matches.
|
||||
//!
|
||||
//! ```no_run
|
||||
//! extern crate ds323x;
|
||||
//! extern crate linux_embedded_hal as hal;
|
||||
//! use ds323x::{Ds323x, Hours, DayAlarm2, Alarm2Matching};
|
||||
//! use linux_embedded_hal::I2cdev;
|
||||
//!
|
||||
//! # fn main() {
|
||||
//! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let mut rtc = Ds323x::new_ds3231(dev);
|
||||
//! let alarm2 = DayAlarm2 {
|
||||
//! day: 1, // does not matter given the chosen matching
|
||||
|
@ -417,17 +347,28 @@
|
|||
//! rtc.set_alarm2_day(alarm2, Alarm2Matching::HoursAndMinutesMatch).unwrap();
|
||||
//! rtc.use_int_sqw_output_as_interrupt().unwrap();
|
||||
//! rtc.enable_alarm2_interrupts().unwrap();
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ### Set the Alarm1 to a specific time
|
||||
//!
|
||||
//! ```no_run
|
||||
//! use ds323x::{Ds323x, Hours, NaiveTime};
|
||||
//! use linux_embedded_hal::I2cdev;
|
||||
//!
|
||||
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
|
||||
//! let mut rtc = Ds323x::new_ds3231(dev);
|
||||
//! let time = NaiveTime::from_hms(19, 59, 58);
|
||||
//! rtc.set_alarm1_hms(time).unwrap();
|
||||
//! ```
|
||||
|
||||
#![deny(unsafe_code, missing_docs)]
|
||||
#![no_std]
|
||||
|
||||
extern crate embedded_hal as hal;
|
||||
use core::marker::PhantomData;
|
||||
use hal::spi::{Mode, MODE_1, MODE_3};
|
||||
extern crate rtcc;
|
||||
pub use self::rtcc::{Datelike, Hours, NaiveDate, NaiveDateTime, NaiveTime, Rtcc, Timelike};
|
||||
use embedded_hal::spi::{Mode, MODE_1, MODE_3};
|
||||
pub use rtcc::{
|
||||
DateTimeAccess, Datelike, Hours, NaiveDate, NaiveDateTime, NaiveTime, Rtcc, Timelike,
|
||||
};
|
||||
|
||||
/// SPI mode 1 (CPOL = 0, CPHA = 1)
|
||||
pub const SPI_MODE_1: Mode = MODE_1;
|
||||
|
@ -436,13 +377,16 @@ pub const SPI_MODE_3: Mode = MODE_3;
|
|||
|
||||
/// All possible errors in this crate
|
||||
#[derive(Debug)]
|
||||
pub enum Error<CommE, PinE> {
|
||||
pub enum Error<E> {
|
||||
/// I²C/SPI bus error
|
||||
Comm(CommE),
|
||||
/// Pin setting error
|
||||
Pin(PinE),
|
||||
Comm(E),
|
||||
/// Invalid input data provided
|
||||
InvalidInputData,
|
||||
/// Internal device state is invalid.
|
||||
///
|
||||
/// It was not possible to read a valid date and/or time.
|
||||
/// The device is probably missing initialization.
|
||||
InvalidDeviceState,
|
||||
}
|
||||
|
||||
/// Square-wave output frequency
|
||||
|
@ -543,7 +487,7 @@ pub struct Ds323x<DI, IC> {
|
|||
|
||||
mod ds323x;
|
||||
pub mod interface;
|
||||
pub use ds323x::{
|
||||
pub use crate::ds323x::{
|
||||
Alarm1Matching, Alarm2Matching, DayAlarm1, DayAlarm2, WeekdayAlarm1, WeekdayAlarm2,
|
||||
};
|
||||
mod ds3231;
|
||||
|
@ -554,7 +498,7 @@ mod private {
|
|||
use super::{ic, interface};
|
||||
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 Sealed for ic::DS3231 {}
|
||||
|
|
485
tests/alarms.rs
485
tests/alarms.rs
|
@ -1,31 +1,28 @@
|
|||
extern crate embedded_hal_mock as hal;
|
||||
use hal::i2c::Transaction as I2cTrans;
|
||||
use hal::spi::Transaction as SpiTrans;
|
||||
use embedded_hal_mock::eh1::{i2c::Transaction as I2cTrans, spi::Transaction as SpiTrans};
|
||||
mod common;
|
||||
use common::{
|
||||
use self::common::{
|
||||
destroy_ds3231, destroy_ds3232, destroy_ds3234, new_ds3231, new_ds3232, new_ds3234,
|
||||
BitFlags as BF, Register, DEVICE_ADDRESS as DEV_ADDR,
|
||||
};
|
||||
extern crate ds323x;
|
||||
use ds323x::{
|
||||
Alarm1Matching as A1M, Alarm2Matching as A2M, DayAlarm1, DayAlarm2, Error, Hours,
|
||||
Alarm1Matching as A1M, Alarm2Matching as A2M, DayAlarm1, DayAlarm2, Error, Hours, NaiveTime,
|
||||
WeekdayAlarm1, WeekdayAlarm2,
|
||||
};
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! _set_invalid_alarm_test {
|
||||
($name:ident, $method:ident, $create_method:ident, $destroy_method:ident, $alarm:expr, $matching:expr) => {
|
||||
($name:ident, $method:ident, $create_method:ident, $destroy_method:ident, $( $value:expr ),+) => {
|
||||
#[test]
|
||||
fn $name() {
|
||||
let mut dev = $create_method(&[]);
|
||||
assert_invalid_input_data!(dev.$method($alarm, $matching));
|
||||
assert_invalid_input_data!(dev.$method($($value),*));
|
||||
$destroy_method(dev);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! set_invalid_alarm_test {
|
||||
($name:ident, $method:ident, $alarm:expr, $matching:expr) => {
|
||||
($name:ident, $method:ident, $( $value:expr ),+) => {
|
||||
mod $name {
|
||||
use super::*;
|
||||
_set_invalid_alarm_test!(
|
||||
|
@ -33,24 +30,21 @@ macro_rules! set_invalid_alarm_test {
|
|||
$method,
|
||||
new_ds3231,
|
||||
destroy_ds3231,
|
||||
$alarm,
|
||||
$matching
|
||||
$($value),*
|
||||
);
|
||||
_set_invalid_alarm_test!(
|
||||
cannot_set_invalid_ds3232,
|
||||
$method,
|
||||
new_ds3232,
|
||||
destroy_ds3232,
|
||||
$alarm,
|
||||
$matching
|
||||
$($value),*
|
||||
);
|
||||
_set_invalid_alarm_test!(
|
||||
cannot_set_invalid_ds3234,
|
||||
$method,
|
||||
new_ds3234,
|
||||
destroy_ds3234,
|
||||
$alarm,
|
||||
$matching
|
||||
$($value),*
|
||||
);
|
||||
}
|
||||
};
|
||||
|
@ -80,6 +74,28 @@ mod alarm1 {
|
|||
},
|
||||
A1M::AllMatch
|
||||
);
|
||||
set_invalid_alarm_test!(
|
||||
day_invalid_second,
|
||||
set_alarm1_day,
|
||||
DayAlarm1 {
|
||||
day: 1,
|
||||
hour: Hours::H24(1),
|
||||
minute: 59,
|
||||
second: 60
|
||||
},
|
||||
A1M::SecondsMatch
|
||||
);
|
||||
set_invalid_alarm_test!(
|
||||
day_invalid_minute,
|
||||
set_alarm1_day,
|
||||
DayAlarm1 {
|
||||
day: 1,
|
||||
hour: Hours::H24(1),
|
||||
minute: 60,
|
||||
second: 10
|
||||
},
|
||||
A1M::MinutesAndSecondsMatch
|
||||
);
|
||||
set_invalid_alarm_test!(
|
||||
day_invalid_h,
|
||||
set_alarm1_day,
|
||||
|
@ -91,6 +107,17 @@ mod alarm1 {
|
|||
},
|
||||
A1M::AllMatch
|
||||
);
|
||||
set_invalid_alarm_test!(
|
||||
day_invalid_h_hmasm,
|
||||
set_alarm1_day,
|
||||
DayAlarm1 {
|
||||
day: 1,
|
||||
hour: Hours::H24(24),
|
||||
minute: 1,
|
||||
second: 1
|
||||
},
|
||||
A1M::HoursMinutesAndSecondsMatch
|
||||
);
|
||||
set_invalid_alarm_test!(
|
||||
day_invalid_am1,
|
||||
set_alarm1_day,
|
||||
|
@ -191,6 +218,18 @@ mod alarm1 {
|
|||
},
|
||||
A1M::AllMatch
|
||||
);
|
||||
set_invalid_alarm_test!(
|
||||
wd_invalid_h_hmasm,
|
||||
set_alarm1_weekday,
|
||||
WeekdayAlarm1 {
|
||||
weekday: 1,
|
||||
hour: Hours::H24(24),
|
||||
minute: 1,
|
||||
second: 1
|
||||
},
|
||||
A1M::HoursMinutesAndSecondsMatch
|
||||
);
|
||||
|
||||
set_invalid_alarm_test!(
|
||||
wd_invalid_am1,
|
||||
set_alarm1_weekday,
|
||||
|
@ -257,10 +296,42 @@ mod alarm1 {
|
|||
},
|
||||
A1M::AllMatch
|
||||
);
|
||||
set_invalid_alarm_test!(
|
||||
wd_invalid_sec_sm,
|
||||
set_alarm1_weekday,
|
||||
WeekdayAlarm1 {
|
||||
weekday: 1,
|
||||
hour: Hours::H24(1),
|
||||
minute: 1,
|
||||
second: 60
|
||||
},
|
||||
A1M::SecondsMatch
|
||||
);
|
||||
set_invalid_alarm_test!(
|
||||
wd_invalid_min_masm,
|
||||
set_alarm1_weekday,
|
||||
WeekdayAlarm1 {
|
||||
weekday: 1,
|
||||
hour: Hours::H24(1),
|
||||
minute: 60,
|
||||
second: 1
|
||||
},
|
||||
A1M::MinutesAndSecondsMatch
|
||||
);
|
||||
}
|
||||
|
||||
mod alarm2 {
|
||||
use super::*;
|
||||
set_invalid_alarm_test!(
|
||||
day_invalid_min_mm,
|
||||
set_alarm2_day,
|
||||
DayAlarm2 {
|
||||
day: 1,
|
||||
hour: Hours::H24(1),
|
||||
minute: 60
|
||||
},
|
||||
A2M::MinutesMatch
|
||||
);
|
||||
set_invalid_alarm_test!(
|
||||
day_invalid_min,
|
||||
set_alarm2_day,
|
||||
|
@ -281,6 +352,16 @@ mod alarm2 {
|
|||
},
|
||||
A2M::AllMatch
|
||||
);
|
||||
set_invalid_alarm_test!(
|
||||
day_invalid_h_hamm,
|
||||
set_alarm2_day,
|
||||
DayAlarm2 {
|
||||
day: 1,
|
||||
hour: Hours::H24(24),
|
||||
minute: 1
|
||||
},
|
||||
A2M::HoursAndMinutesMatch
|
||||
);
|
||||
set_invalid_alarm_test!(
|
||||
day_invalid_am1,
|
||||
set_alarm2_day,
|
||||
|
@ -342,6 +423,16 @@ mod alarm2 {
|
|||
A2M::AllMatch
|
||||
);
|
||||
|
||||
set_invalid_alarm_test!(
|
||||
wd_invalid_min_mm,
|
||||
set_alarm2_weekday,
|
||||
WeekdayAlarm2 {
|
||||
weekday: 1,
|
||||
hour: Hours::H24(1),
|
||||
minute: 60
|
||||
},
|
||||
A2M::MinutesMatch
|
||||
);
|
||||
set_invalid_alarm_test!(
|
||||
wd_invalid_min,
|
||||
set_alarm2_weekday,
|
||||
|
@ -362,6 +453,16 @@ mod alarm2 {
|
|||
},
|
||||
A2M::AllMatch
|
||||
);
|
||||
set_invalid_alarm_test!(
|
||||
wd_invalid_h_hmm,
|
||||
set_alarm2_weekday,
|
||||
WeekdayAlarm2 {
|
||||
weekday: 1,
|
||||
hour: Hours::H24(24),
|
||||
minute: 1
|
||||
},
|
||||
A2M::HoursAndMinutesMatch
|
||||
);
|
||||
set_invalid_alarm_test!(
|
||||
wd_invalid_am1,
|
||||
set_alarm2_weekday,
|
||||
|
@ -422,22 +523,32 @@ mod alarm2 {
|
|||
},
|
||||
A2M::AllMatch
|
||||
);
|
||||
set_invalid_alarm_test!(
|
||||
wd_invalid_minute,
|
||||
set_alarm2_weekday,
|
||||
WeekdayAlarm2 {
|
||||
weekday: 1,
|
||||
hour: Hours::H24(1),
|
||||
minute: 60
|
||||
},
|
||||
A2M::HoursAndMinutesMatch
|
||||
);
|
||||
}
|
||||
|
||||
macro_rules! _set_values_test {
|
||||
($name:ident, $method:ident, $create_method:ident, $destroy_method:ident, $value1:expr, $value2:expr, $transactions:expr) => {
|
||||
($name:ident, $method:ident, $create_method:ident, $destroy_method:ident, $transactions:expr, $( $value:expr ),+) => {
|
||||
#[test]
|
||||
fn $name() {
|
||||
let trans = $transactions;
|
||||
let mut dev = $create_method(&trans);
|
||||
dev.$method($value1, $value2).unwrap();
|
||||
dev.$method($($value),*).unwrap();
|
||||
$destroy_method(dev);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! set_values_test {
|
||||
($name:ident, $method:ident, $value1:expr, $value2:expr, $i2c_transactions:expr, $spi_transactions:expr) => {
|
||||
($name:ident, $method:ident, $i2c_transactions:expr, $spi_transactions:expr, $( $value:expr ),+) => {
|
||||
mod $name {
|
||||
use super::*;
|
||||
_set_values_test!(
|
||||
|
@ -445,37 +556,36 @@ macro_rules! set_values_test {
|
|||
$method,
|
||||
new_ds3231,
|
||||
destroy_ds3231,
|
||||
$value1,
|
||||
$value2,
|
||||
$i2c_transactions
|
||||
$i2c_transactions,
|
||||
$($value),*
|
||||
);
|
||||
_set_values_test!(
|
||||
can_set_ds3232,
|
||||
$method,
|
||||
new_ds3232,
|
||||
destroy_ds3232,
|
||||
$value1,
|
||||
$value2,
|
||||
$i2c_transactions
|
||||
$i2c_transactions,
|
||||
$($value),*
|
||||
);
|
||||
_set_values_test!(
|
||||
can_set_ds3234,
|
||||
$method,
|
||||
new_ds3234,
|
||||
destroy_ds3234,
|
||||
$value1,
|
||||
$value2,
|
||||
$spi_transactions
|
||||
$spi_transactions,
|
||||
$($value),*
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! set_alarm_test {
|
||||
($name:ident, $method:ident, $alarm:expr, $matching:expr, $register:ident, [ $( $registers:expr ),+ ]) => {
|
||||
set_values_test!($name, $method, $alarm, $matching,
|
||||
($name:ident, $method:ident, $register:ident, [ $( $registers:expr ),+ ], $( $value:expr ),+) => {
|
||||
set_values_test!($name, $method,
|
||||
[ 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),*
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -486,94 +596,152 @@ mod alarm1_day {
|
|||
set_alarm_test!(
|
||||
h24,
|
||||
set_alarm1_day,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, 2, 1],
|
||||
DayAlarm1 {
|
||||
day: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3,
|
||||
second: 4
|
||||
},
|
||||
A1M::AllMatch,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, 2, 1]
|
||||
A1M::AllMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
am,
|
||||
set_alarm1_day,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, 0b0100_0010, 1],
|
||||
DayAlarm1 {
|
||||
day: 1,
|
||||
hour: Hours::AM(2),
|
||||
minute: 3,
|
||||
second: 4
|
||||
},
|
||||
A1M::AllMatch,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, 0b0100_0010, 1]
|
||||
A1M::AllMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
pm,
|
||||
set_alarm1_day,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, 0b0110_0010, 1],
|
||||
DayAlarm1 {
|
||||
day: 1,
|
||||
hour: Hours::PM(2),
|
||||
minute: 3,
|
||||
second: 4
|
||||
},
|
||||
A1M::AllMatch,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, 0b0110_0010, 1]
|
||||
A1M::AllMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_hms_naivetime,
|
||||
set_alarm1_hms,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, 2, AM | 1],
|
||||
NaiveTime::from_hms_opt(2, 3, 4).unwrap()
|
||||
);
|
||||
|
||||
set_alarm_test!(
|
||||
match_hms,
|
||||
set_alarm1_day,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, 2, AM | 1],
|
||||
DayAlarm1 {
|
||||
day: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3,
|
||||
second: 4
|
||||
},
|
||||
A1M::HoursMinutesAndSecondsMatch,
|
||||
A1M::HoursMinutesAndSecondsMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_hms_ignore_incorrect_day,
|
||||
set_alarm1_day,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, 2, AM | 1]
|
||||
[4, 3, 2, AM | 1],
|
||||
DayAlarm1 {
|
||||
day: 0,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3,
|
||||
second: 4
|
||||
},
|
||||
A1M::HoursMinutesAndSecondsMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_ms_ignore_invalid_hour,
|
||||
set_alarm1_day,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, AM, AM | 1],
|
||||
DayAlarm1 {
|
||||
day: 1,
|
||||
hour: Hours::H24(24),
|
||||
minute: 3,
|
||||
second: 4
|
||||
},
|
||||
A1M::MinutesAndSecondsMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_ms,
|
||||
set_alarm1_day,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, AM | 2, AM | 1],
|
||||
DayAlarm1 {
|
||||
day: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3,
|
||||
second: 4
|
||||
},
|
||||
A1M::MinutesAndSecondsMatch,
|
||||
A1M::MinutesAndSecondsMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_ms_ignore_incorrect_day,
|
||||
set_alarm1_day,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, AM | 2, AM | 1]
|
||||
[4, 3, AM | 2, AM | 1],
|
||||
DayAlarm1 {
|
||||
day: 0,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3,
|
||||
second: 4
|
||||
},
|
||||
A1M::MinutesAndSecondsMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_s,
|
||||
set_alarm1_day,
|
||||
ALARM1_SECONDS,
|
||||
[4, AM | 3, AM | 2, AM | 1],
|
||||
DayAlarm1 {
|
||||
day: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3,
|
||||
second: 4
|
||||
},
|
||||
A1M::SecondsMatch,
|
||||
A1M::SecondsMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_s_ignore_incorrect_min,
|
||||
set_alarm1_day,
|
||||
ALARM1_SECONDS,
|
||||
[4, AM | 3, AM | 2, AM | 1]
|
||||
[4, AM, AM | 2, AM | 1],
|
||||
DayAlarm1 {
|
||||
day: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 60,
|
||||
second: 4
|
||||
},
|
||||
A1M::SecondsMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_ops,
|
||||
set_alarm1_day,
|
||||
ALARM1_SECONDS,
|
||||
[AM | 4, AM | 3, AM | 2, AM | 1],
|
||||
DayAlarm1 {
|
||||
day: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3,
|
||||
second: 4
|
||||
},
|
||||
A1M::OncePerSecond,
|
||||
ALARM1_SECONDS,
|
||||
[AM | 4, AM | 3, AM | 2, AM | 1]
|
||||
A1M::OncePerSecond
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -582,94 +750,132 @@ mod alarm1_weekday {
|
|||
set_alarm_test!(
|
||||
h24,
|
||||
set_alarm1_weekday,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, 2, BF::WEEKDAY | 1],
|
||||
WeekdayAlarm1 {
|
||||
weekday: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3,
|
||||
second: 4
|
||||
},
|
||||
A1M::AllMatch,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, 2, BF::WEEKDAY | 1]
|
||||
A1M::AllMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
am,
|
||||
set_alarm1_weekday,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, 0b0100_0010, BF::WEEKDAY | 1],
|
||||
WeekdayAlarm1 {
|
||||
weekday: 1,
|
||||
hour: Hours::AM(2),
|
||||
minute: 3,
|
||||
second: 4
|
||||
},
|
||||
A1M::AllMatch,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, 0b0100_0010, BF::WEEKDAY | 1]
|
||||
A1M::AllMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
pm,
|
||||
set_alarm1_weekday,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, 0b0110_0010, BF::WEEKDAY | 1],
|
||||
WeekdayAlarm1 {
|
||||
weekday: 1,
|
||||
hour: Hours::PM(2),
|
||||
minute: 3,
|
||||
second: 4
|
||||
},
|
||||
A1M::AllMatch,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, 0b0110_0010, BF::WEEKDAY | 1]
|
||||
A1M::AllMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_hms_ignore_incorrect_wd,
|
||||
set_alarm1_weekday,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, 2, AM | BF::WEEKDAY | 1],
|
||||
WeekdayAlarm1 {
|
||||
weekday: 0,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3,
|
||||
second: 4
|
||||
},
|
||||
A1M::HoursMinutesAndSecondsMatch
|
||||
);
|
||||
|
||||
set_alarm_test!(
|
||||
match_hms,
|
||||
set_alarm1_weekday,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, 2, AM | BF::WEEKDAY | 1],
|
||||
WeekdayAlarm1 {
|
||||
weekday: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3,
|
||||
second: 4
|
||||
},
|
||||
A1M::HoursMinutesAndSecondsMatch,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, 2, AM | BF::WEEKDAY | 1]
|
||||
A1M::HoursMinutesAndSecondsMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_ms,
|
||||
set_alarm1_weekday,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, AM | 2, AM | BF::WEEKDAY | 1],
|
||||
WeekdayAlarm1 {
|
||||
weekday: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3,
|
||||
second: 4
|
||||
},
|
||||
A1M::MinutesAndSecondsMatch,
|
||||
ALARM1_SECONDS,
|
||||
[4, 3, AM | 2, AM | BF::WEEKDAY | 1]
|
||||
A1M::MinutesAndSecondsMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_s,
|
||||
set_alarm1_weekday,
|
||||
ALARM1_SECONDS,
|
||||
[4, AM | 3, AM | 2, AM | BF::WEEKDAY | 1],
|
||||
WeekdayAlarm1 {
|
||||
weekday: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3,
|
||||
second: 4
|
||||
},
|
||||
A1M::SecondsMatch,
|
||||
A1M::SecondsMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_s_ignore_incorrect_min,
|
||||
set_alarm1_weekday,
|
||||
ALARM1_SECONDS,
|
||||
[4, AM | 3, AM | 2, AM | BF::WEEKDAY | 1]
|
||||
[4, AM, AM | 2, AM | BF::WEEKDAY | 1],
|
||||
WeekdayAlarm1 {
|
||||
weekday: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 60,
|
||||
second: 4
|
||||
},
|
||||
A1M::SecondsMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_ops,
|
||||
set_alarm1_weekday,
|
||||
ALARM1_SECONDS,
|
||||
[AM | 4, AM | 3, AM | 2, AM | BF::WEEKDAY | 1],
|
||||
WeekdayAlarm1 {
|
||||
weekday: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3,
|
||||
second: 4
|
||||
},
|
||||
A1M::OncePerSecond,
|
||||
A1M::OncePerSecond
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_ops_ignore_incorrect_sec,
|
||||
set_alarm1_weekday,
|
||||
ALARM1_SECONDS,
|
||||
[AM | 4, AM | 3, AM | 2, AM | BF::WEEKDAY | 1]
|
||||
[AM, AM | 3, AM | 2, AM | BF::WEEKDAY | 1],
|
||||
WeekdayAlarm1 {
|
||||
weekday: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3,
|
||||
second: 60
|
||||
},
|
||||
A1M::OncePerSecond
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -678,75 +884,117 @@ mod alarm2_day {
|
|||
set_alarm_test!(
|
||||
h24,
|
||||
set_alarm2_day,
|
||||
ALARM2_MINUTES,
|
||||
[3, 2, 1],
|
||||
DayAlarm2 {
|
||||
day: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3
|
||||
},
|
||||
A2M::AllMatch,
|
||||
ALARM2_MINUTES,
|
||||
[3, 2, 1]
|
||||
A2M::AllMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
am,
|
||||
set_alarm2_day,
|
||||
ALARM2_MINUTES,
|
||||
[3, 0b0100_0010, 1],
|
||||
DayAlarm2 {
|
||||
day: 1,
|
||||
hour: Hours::AM(2),
|
||||
minute: 3
|
||||
},
|
||||
A2M::AllMatch,
|
||||
ALARM2_MINUTES,
|
||||
[3, 0b0100_0010, 1]
|
||||
A2M::AllMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
pm,
|
||||
set_alarm2_day,
|
||||
ALARM2_MINUTES,
|
||||
[3, 0b0110_0010, 1],
|
||||
DayAlarm2 {
|
||||
day: 1,
|
||||
hour: Hours::PM(2),
|
||||
minute: 3
|
||||
},
|
||||
A2M::AllMatch,
|
||||
ALARM2_MINUTES,
|
||||
[3, 0b0110_0010, 1]
|
||||
A2M::AllMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_hm_naivetime,
|
||||
set_alarm2_hm,
|
||||
ALARM2_MINUTES,
|
||||
[3, 2, AM | 1],
|
||||
NaiveTime::from_hms_opt(2, 3, 0).unwrap()
|
||||
);
|
||||
|
||||
set_alarm_test!(
|
||||
match_hm,
|
||||
set_alarm2_day,
|
||||
ALARM2_MINUTES,
|
||||
[3, 2, AM | 1],
|
||||
DayAlarm2 {
|
||||
day: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3
|
||||
},
|
||||
A2M::HoursAndMinutesMatch,
|
||||
A2M::HoursAndMinutesMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_hm_ignore_incorrect_day,
|
||||
set_alarm2_day,
|
||||
ALARM2_MINUTES,
|
||||
[3, 2, AM | 1]
|
||||
[3, 2, AM | 1],
|
||||
DayAlarm2 {
|
||||
day: 0,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3
|
||||
},
|
||||
A2M::HoursAndMinutesMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_m,
|
||||
set_alarm2_day,
|
||||
ALARM2_MINUTES,
|
||||
[3, AM | 2, AM | 1],
|
||||
DayAlarm2 {
|
||||
day: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3
|
||||
},
|
||||
A2M::MinutesMatch,
|
||||
A2M::MinutesMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_m_ignore_invalid_h,
|
||||
set_alarm2_day,
|
||||
ALARM2_MINUTES,
|
||||
[3, AM | 2, AM | 1]
|
||||
[3, AM, AM | 1],
|
||||
DayAlarm2 {
|
||||
day: 1,
|
||||
hour: Hours::H24(25),
|
||||
minute: 3
|
||||
},
|
||||
A2M::MinutesMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_opm,
|
||||
set_alarm2_day,
|
||||
ALARM2_MINUTES,
|
||||
[AM | 3, AM | 2, AM | 1],
|
||||
DayAlarm2 {
|
||||
day: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3
|
||||
},
|
||||
A2M::OncePerMinute,
|
||||
A2M::OncePerMinute
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_opm_ignore_incorrect_min,
|
||||
set_alarm2_day,
|
||||
ALARM2_MINUTES,
|
||||
[AM | 3, AM | 2, AM | 1]
|
||||
[AM, AM | 2, AM | 1],
|
||||
DayAlarm2 {
|
||||
day: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 60
|
||||
},
|
||||
A2M::OncePerMinute
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -755,74 +1003,109 @@ mod alarm2_weekday {
|
|||
set_alarm_test!(
|
||||
h24,
|
||||
set_alarm2_weekday,
|
||||
ALARM2_MINUTES,
|
||||
[3, 2, BF::WEEKDAY | 1],
|
||||
WeekdayAlarm2 {
|
||||
weekday: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3
|
||||
},
|
||||
A2M::AllMatch,
|
||||
ALARM2_MINUTES,
|
||||
[3, 2, BF::WEEKDAY | 1]
|
||||
A2M::AllMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
am,
|
||||
set_alarm2_weekday,
|
||||
ALARM2_MINUTES,
|
||||
[3, 0b0100_0010, BF::WEEKDAY | 1],
|
||||
WeekdayAlarm2 {
|
||||
weekday: 1,
|
||||
hour: Hours::AM(2),
|
||||
minute: 3
|
||||
},
|
||||
A2M::AllMatch,
|
||||
ALARM2_MINUTES,
|
||||
[3, 0b0100_0010, BF::WEEKDAY | 1]
|
||||
A2M::AllMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
pm,
|
||||
set_alarm2_weekday,
|
||||
ALARM2_MINUTES,
|
||||
[3, 0b0110_0010, BF::WEEKDAY | 1],
|
||||
WeekdayAlarm2 {
|
||||
weekday: 1,
|
||||
hour: Hours::PM(2),
|
||||
minute: 3
|
||||
},
|
||||
A2M::AllMatch,
|
||||
ALARM2_MINUTES,
|
||||
[3, 0b0110_0010, BF::WEEKDAY | 1]
|
||||
A2M::AllMatch
|
||||
);
|
||||
|
||||
set_alarm_test!(
|
||||
match_hm,
|
||||
set_alarm2_weekday,
|
||||
ALARM2_MINUTES,
|
||||
[3, 2, AM | BF::WEEKDAY | 1],
|
||||
WeekdayAlarm2 {
|
||||
weekday: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3
|
||||
},
|
||||
A2M::HoursAndMinutesMatch,
|
||||
A2M::HoursAndMinutesMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_hm_ignore_incorrect_wd,
|
||||
set_alarm2_weekday,
|
||||
ALARM2_MINUTES,
|
||||
[3, 2, AM | BF::WEEKDAY | 1]
|
||||
[3, 2, AM | BF::WEEKDAY | 1],
|
||||
WeekdayAlarm2 {
|
||||
weekday: 0,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3
|
||||
},
|
||||
A2M::HoursAndMinutesMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_m,
|
||||
set_alarm2_weekday,
|
||||
ALARM2_MINUTES,
|
||||
[3, AM | 2, AM | BF::WEEKDAY | 1],
|
||||
WeekdayAlarm2 {
|
||||
weekday: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3
|
||||
},
|
||||
A2M::MinutesMatch,
|
||||
A2M::MinutesMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_m_ignore_invalid_hour,
|
||||
set_alarm2_weekday,
|
||||
ALARM2_MINUTES,
|
||||
[3, AM | 2, AM | BF::WEEKDAY | 1]
|
||||
[3, AM, AM | BF::WEEKDAY | 1],
|
||||
WeekdayAlarm2 {
|
||||
weekday: 1,
|
||||
hour: Hours::H24(24),
|
||||
minute: 3
|
||||
},
|
||||
A2M::MinutesMatch
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_opm,
|
||||
set_alarm2_weekday,
|
||||
ALARM2_MINUTES,
|
||||
[AM | 3, AM | 2, AM | BF::WEEKDAY | 1],
|
||||
WeekdayAlarm2 {
|
||||
weekday: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 3
|
||||
},
|
||||
A2M::OncePerMinute,
|
||||
A2M::OncePerMinute
|
||||
);
|
||||
set_alarm_test!(
|
||||
match_opm_ignore_incorrect_min,
|
||||
set_alarm2_weekday,
|
||||
ALARM2_MINUTES,
|
||||
[AM | 3, AM | 2, AM | BF::WEEKDAY | 1]
|
||||
[AM, AM | 2, AM | BF::WEEKDAY | 1],
|
||||
WeekdayAlarm2 {
|
||||
weekday: 1,
|
||||
hour: Hours::H24(2),
|
||||
minute: 60
|
||||
},
|
||||
A2M::OncePerMinute
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
extern crate ds323x;
|
||||
extern crate embedded_hal;
|
||||
use self::ds323x::{ic, interface, Ds323x};
|
||||
extern crate embedded_hal_mock as hal;
|
||||
use self::hal::i2c::{Mock as I2cMock, Transaction as I2cTrans};
|
||||
use self::hal::spi::{Mock as SpiMock, Transaction as SpiTrans};
|
||||
use ds323x::{ic, interface, Ds323x};
|
||||
use embedded_hal_mock::eh1::{
|
||||
i2c::{Mock as I2cMock, Transaction as I2cTrans},
|
||||
spi::{Mock as SpiMock, Transaction as SpiTrans},
|
||||
};
|
||||
|
||||
#[allow(unused)]
|
||||
pub const DEVICE_ADDRESS: u8 = 0b110_1000;
|
||||
|
@ -60,8 +59,7 @@ impl BitFlags {
|
|||
|
||||
pub struct DummyOutputPin;
|
||||
|
||||
impl embedded_hal::digital::v2::OutputPin for DummyOutputPin {
|
||||
type Error = ();
|
||||
impl embedded_hal::digital::OutputPin for DummyOutputPin {
|
||||
fn set_low(&mut self) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
@ -70,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(
|
||||
transactions: &[I2cTrans],
|
||||
) -> Ds323x<interface::I2cInterface<I2cMock>, ic::DS3231> {
|
||||
|
@ -83,9 +85,9 @@ pub fn new_ds3232(
|
|||
}
|
||||
|
||||
pub fn new_ds3234(
|
||||
transactions: &[SpiTrans],
|
||||
) -> Ds323x<interface::SpiInterface<SpiMock, DummyOutputPin>, ic::DS3234> {
|
||||
Ds323x::new_ds3234(SpiMock::new(transactions), DummyOutputPin)
|
||||
transactions: &[SpiTrans<u8>],
|
||||
) -> Ds323x<interface::SpiInterface<SpiMock<u8>>, ic::DS3234> {
|
||||
Ds323x::new_ds3234(SpiMock::new(transactions))
|
||||
}
|
||||
|
||||
pub fn destroy_ds3231(dev: Ds323x<interface::I2cInterface<I2cMock>, ic::DS3231>) {
|
||||
|
@ -96,8 +98,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, DummyOutputPin>, ic::DS3234>) {
|
||||
dev.destroy_ds3234().0.done();
|
||||
pub fn destroy_ds3234(dev: Ds323x<interface::SpiInterface<SpiMock<u8>>, ic::DS3234>) {
|
||||
dev.destroy_ds3234().done();
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
|
@ -206,10 +208,14 @@ macro_rules! get_param_test {
|
|||
vec![Register::$register],
|
||||
vec![$binary_value]
|
||||
)],
|
||||
[SpiTrans::transfer(
|
||||
vec![Register::$register, 0],
|
||||
vec![Register::$register, $binary_value]
|
||||
)]
|
||||
[
|
||||
SpiTrans::transaction_start(),
|
||||
SpiTrans::transfer_in_place(
|
||||
vec![Register::$register, 0],
|
||||
vec![Register::$register, $binary_value]
|
||||
),
|
||||
SpiTrans::transaction_end(),
|
||||
]
|
||||
);
|
||||
};
|
||||
}
|
||||
|
@ -224,7 +230,10 @@ macro_rules! transactions_i2c_read {
|
|||
#[macro_export]
|
||||
macro_rules! transactions_spi_read {
|
||||
($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()
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -281,10 +290,11 @@ macro_rules! set_param_test {
|
|||
DEV_ADDR,
|
||||
vec![Register::$register, $binary_value]
|
||||
)],
|
||||
[SpiTrans::write(vec![
|
||||
Register::$register + 0x80,
|
||||
$binary_value
|
||||
])]
|
||||
[
|
||||
SpiTrans::transaction_start(),
|
||||
SpiTrans::write_vec(vec![Register::$register + 0x80, $binary_value]),
|
||||
SpiTrans::transaction_end(),
|
||||
]
|
||||
);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,12 +1,8 @@
|
|||
extern crate embedded_hal_mock as hal;
|
||||
use hal::i2c::Transaction as I2cTrans;
|
||||
use hal::spi::Transaction as SpiTrans;
|
||||
|
||||
extern crate ds323x;
|
||||
use ds323x::SqWFreq;
|
||||
use embedded_hal_mock::eh1::{i2c::Transaction as I2cTrans, spi::Transaction as SpiTrans};
|
||||
|
||||
mod common;
|
||||
use common::{
|
||||
use self::common::{
|
||||
destroy_ds3231, destroy_ds3232, destroy_ds3234, new_ds3231, new_ds3232, new_ds3234,
|
||||
BitFlags as BF, Register, CONTROL_POR_VALUE, DEVICE_ADDRESS as DEV_ADDR, DS3231_POR_STATUS,
|
||||
DS323X_POR_STATUS,
|
||||
|
@ -50,10 +46,11 @@ macro_rules! call_method_test {
|
|||
DEV_ADDR,
|
||||
vec![Register::$register, $value_enabled]
|
||||
)],
|
||||
[SpiTrans::write(vec![
|
||||
Register::$register + 0x80,
|
||||
$value_enabled
|
||||
])]
|
||||
[
|
||||
SpiTrans::transaction_start(),
|
||||
SpiTrans::write_vec(vec![Register::$register + 0x80, $value_enabled]),
|
||||
SpiTrans::transaction_end(),
|
||||
]
|
||||
);
|
||||
};
|
||||
}
|
||||
|
@ -87,10 +84,11 @@ macro_rules! call_method_status_test {
|
|||
$method,
|
||||
new_ds3234,
|
||||
destroy_ds3234,
|
||||
[SpiTrans::write(vec![
|
||||
Register::STATUS + 0x80,
|
||||
$value_ds323x
|
||||
])]
|
||||
[
|
||||
SpiTrans::transaction_start(),
|
||||
SpiTrans::write_vec(vec![Register::STATUS + 0x80, $value_ds323x]),
|
||||
SpiTrans::transaction_end(),
|
||||
]
|
||||
);
|
||||
}
|
||||
};
|
||||
|
@ -108,10 +106,14 @@ macro_rules! change_if_necessary_test {
|
|||
vec![Register::$register],
|
||||
vec![$value_enabled]
|
||||
)],
|
||||
[SpiTrans::transfer(
|
||||
vec![Register::$register, 0],
|
||||
vec![Register::$register, $value_enabled]
|
||||
)]
|
||||
[
|
||||
SpiTrans::transaction_start(),
|
||||
SpiTrans::transfer_in_place(
|
||||
vec![Register::$register, 0],
|
||||
vec![Register::$register, $value_enabled]
|
||||
),
|
||||
SpiTrans::transaction_end(),
|
||||
]
|
||||
);
|
||||
|
||||
call_triple_test!(
|
||||
|
@ -126,11 +128,15 @@ macro_rules! change_if_necessary_test {
|
|||
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, $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(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
@ -224,18 +230,12 @@ set_param_test!(
|
|||
|
||||
get_param_test!(
|
||||
get_aging_offset_min,
|
||||
get_aging_offset,
|
||||
aging_offset,
|
||||
AGING_OFFSET,
|
||||
-128,
|
||||
0b1000_0000
|
||||
);
|
||||
get_param_test!(
|
||||
get_aging_offset_max,
|
||||
get_aging_offset,
|
||||
AGING_OFFSET,
|
||||
127,
|
||||
127
|
||||
);
|
||||
get_param_test!(get_aging_offset_max, aging_offset, AGING_OFFSET, 127, 127);
|
||||
|
||||
call_method_test!(
|
||||
int_sqw_out_int,
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
mod common;
|
||||
use common::{destroy_ds3231, destroy_ds3232, destroy_ds3234, new_ds3231, new_ds3232, new_ds3234};
|
||||
use self::common::{
|
||||
destroy_ds3231, destroy_ds3232, destroy_ds3234, new_ds3231, new_ds3232, new_ds3234,
|
||||
};
|
||||
|
||||
macro_rules! construction_test {
|
||||
($name:ident, $create:ident, $destroy:ident) => {
|
||||
|
|
|
@ -1,15 +1,24 @@
|
|||
extern crate embedded_hal_mock as hal;
|
||||
use hal::i2c::Transaction as I2cTrans;
|
||||
use hal::spi::Transaction as SpiTrans;
|
||||
use embedded_hal_mock::eh1::{i2c::Transaction as I2cTrans, spi::Transaction as SpiTrans};
|
||||
use rtcc::NaiveDateTime;
|
||||
mod common;
|
||||
use common::{
|
||||
use self::common::{
|
||||
destroy_ds3231, destroy_ds3232, destroy_ds3234, new_ds3231, new_ds3232, new_ds3234, Register,
|
||||
DEVICE_ADDRESS as DEV_ADDR,
|
||||
};
|
||||
extern crate ds323x;
|
||||
#[allow(unused)] // Rust 1.31.0 is confused due to the macros
|
||||
use ds323x::Rtcc;
|
||||
use ds323x::{Error, Hours, NaiveDate, NaiveTime};
|
||||
use ds323x::{DateTimeAccess, Error, Hours, NaiveDate, NaiveTime};
|
||||
|
||||
fn new_datetime(y: i32, mo: u32, d: u32, h: u32, min: u32, s: u32) -> NaiveDateTime {
|
||||
NaiveDate::from_ymd_opt(y, mo, d)
|
||||
.unwrap()
|
||||
.and_hms_opt(h, min, s)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn new_date(y: i32, mo: u32, d: u32) -> NaiveDate {
|
||||
NaiveDate::from_ymd_opt(y, mo, d).unwrap()
|
||||
}
|
||||
|
||||
macro_rules! read_set_param_write_two_test {
|
||||
($name:ident, $method:ident, $value:expr, $register:ident, $binary_value1_read:expr, $bin1:expr, $bin2:expr) => {
|
||||
|
@ -26,11 +35,15 @@ macro_rules! read_set_param_write_two_test {
|
|||
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, $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(),
|
||||
]
|
||||
);
|
||||
};
|
||||
|
@ -51,11 +64,15 @@ macro_rules! read_set_param_test {
|
|||
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, $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(),
|
||||
]
|
||||
);
|
||||
};
|
||||
|
@ -116,76 +133,69 @@ macro_rules! for_all {
|
|||
|
||||
mod seconds {
|
||||
use super::*;
|
||||
get_param_test!(get, get_seconds, SECONDS, 1, 1);
|
||||
get_param_test!(get, seconds, SECONDS, 1, 1);
|
||||
set_param_test!(set, set_seconds, SECONDS, 1, 1);
|
||||
set_invalid_param_test!(invalid, set_seconds, 60);
|
||||
}
|
||||
|
||||
mod minutes {
|
||||
use super::*;
|
||||
get_param_test!(get, get_minutes, MINUTES, 1, 1);
|
||||
get_param_test!(get, minutes, MINUTES, 1, 1);
|
||||
set_param_test!(set, set_minutes, MINUTES, 1, 1);
|
||||
set_invalid_param_test!(invalid, set_minutes, 60);
|
||||
}
|
||||
|
||||
mod hours_24h {
|
||||
use super::*;
|
||||
get_param_test!(get, get_hours, HOURS, Hours::H24(21), 0b0010_0001);
|
||||
get_param_test!(get, hours, HOURS, Hours::H24(21), 0b0010_0001);
|
||||
set_param_test!(set, set_hours, HOURS, Hours::H24(21), 0b0010_0001);
|
||||
set_invalid_param_test!(invalid, set_hours, Hours::H24(24));
|
||||
}
|
||||
|
||||
mod hours_12h_am {
|
||||
use super::*;
|
||||
get_param_test!(get, get_hours, HOURS, Hours::AM(12), 0b0101_0010);
|
||||
get_param_test!(get, hours, HOURS, Hours::AM(12), 0b0101_0010);
|
||||
set_param_test!(set, set_hours, HOURS, Hours::AM(12), 0b0101_0010);
|
||||
set_invalid_param_range_test!(invalid, set_hours, Hours::AM(0), Hours::AM(13));
|
||||
}
|
||||
|
||||
mod hours_12h_pm {
|
||||
use super::*;
|
||||
get_param_test!(get, get_hours, HOURS, Hours::PM(12), 0b0111_0010);
|
||||
get_param_test!(get, hours, HOURS, Hours::PM(12), 0b0111_0010);
|
||||
set_param_test!(set, set_hours, HOURS, Hours::PM(12), 0b0111_0010);
|
||||
set_invalid_param_range_test!(invalid, set_hours, Hours::PM(0), Hours::PM(13));
|
||||
}
|
||||
|
||||
mod weekday {
|
||||
use super::*;
|
||||
get_param_test!(get, get_weekday, DOW, 1, 1);
|
||||
get_param_test!(get, weekday, DOW, 1, 1);
|
||||
set_param_test!(set, set_weekday, DOW, 1, 1);
|
||||
set_invalid_param_range_test!(invalid, set_weekday, 0, 8);
|
||||
}
|
||||
|
||||
mod day {
|
||||
use super::*;
|
||||
get_param_test!(get, get_day, DOM, 1, 1);
|
||||
get_param_test!(get, day, DOM, 1, 1);
|
||||
set_param_test!(set, set_day, DOM, 1, 1);
|
||||
set_invalid_param_range_test!(invalid, set_day, 0, 8);
|
||||
set_invalid_param_range_test!(invalid, set_day, 0, 32);
|
||||
}
|
||||
|
||||
mod month {
|
||||
use super::*;
|
||||
get_param_test!(get, get_month, MONTH, 1, 1);
|
||||
get_param_test!(get, month, MONTH, 1, 1);
|
||||
read_set_param_test!(set, set_month, MONTH, 12, 0b0000_0010, 0b0001_0010);
|
||||
set_invalid_param_range_test!(invalid, set_month, 0, 13);
|
||||
|
||||
mod keeps_century {
|
||||
use super::*;
|
||||
get_param_test!(get, get_month, MONTH, 12, 0b1001_0010);
|
||||
get_param_test!(get, month, MONTH, 12, 0b1001_0010);
|
||||
read_set_param_test!(set, set_month, MONTH, 12, 0b1000_0010, 0b1001_0010);
|
||||
}
|
||||
}
|
||||
|
||||
mod year {
|
||||
use super::*;
|
||||
get_param_read_array_test!(
|
||||
century0_get,
|
||||
get_year,
|
||||
2099,
|
||||
MONTH,
|
||||
[0, 0b1001_1001],
|
||||
[0, 0]
|
||||
);
|
||||
get_param_read_array_test!(century0_get, year, 2099, MONTH, [0, 0b1001_1001], [0, 0]);
|
||||
read_set_param_write_two_test!(
|
||||
century0_set,
|
||||
set_year,
|
||||
|
@ -196,14 +206,7 @@ mod year {
|
|||
0b1001_1001
|
||||
);
|
||||
|
||||
get_param_read_array_test!(
|
||||
century1_get,
|
||||
get_year,
|
||||
2100,
|
||||
MONTH,
|
||||
[0b1000_0000, 0],
|
||||
[0, 0]
|
||||
);
|
||||
get_param_read_array_test!(century1_get, year, 2100, MONTH, [0b1000_0000, 0], [0, 0]);
|
||||
read_set_param_write_two_test!(
|
||||
century1_set,
|
||||
set_year,
|
||||
|
@ -223,28 +226,28 @@ macro_rules! invalid_dt_test {
|
|||
use super::*;
|
||||
#[test]
|
||||
fn datetime_too_small() {
|
||||
let dt = NaiveDate::from_ymd(1999, 1, 2).and_hms(3, 4, 5);
|
||||
let dt = new_datetime(1999, 1, 2, 3, 4, 5);
|
||||
let mut dev = $create_method(&[]);
|
||||
assert_invalid_input_data!(dev.set_datetime(&dt));
|
||||
$destroy_method(dev);
|
||||
}
|
||||
#[test]
|
||||
fn datetime_too_big() {
|
||||
let dt = NaiveDate::from_ymd(2101, 1, 2).and_hms(3, 4, 5);
|
||||
let dt = new_datetime(2101, 1, 2, 3, 4, 5);
|
||||
let mut dev = $create_method(&[]);
|
||||
assert_invalid_input_data!(dev.set_datetime(&dt));
|
||||
$destroy_method(dev);
|
||||
}
|
||||
#[test]
|
||||
fn date_too_small() {
|
||||
let d = NaiveDate::from_ymd(1999, 1, 2);
|
||||
let d = new_date(1999, 1, 2);
|
||||
let mut dev = $create_method(&[]);
|
||||
assert_invalid_input_data!(dev.set_date(&d));
|
||||
$destroy_method(dev);
|
||||
}
|
||||
#[test]
|
||||
fn date_too_big() {
|
||||
let d = NaiveDate::from_ymd(2101, 1, 2);
|
||||
let d = new_date(2101, 1, 2);
|
||||
let mut dev = $create_method(&[]);
|
||||
assert_invalid_input_data!(dev.set_date(&d));
|
||||
$destroy_method(dev);
|
||||
|
@ -261,7 +264,11 @@ macro_rules! transactions_i2c_write {
|
|||
|
||||
macro_rules! transactions_spi_write {
|
||||
($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()
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -272,7 +279,7 @@ macro_rules! dt_test {
|
|||
use super::*;
|
||||
#[test]
|
||||
fn get_datetime() {
|
||||
let dt = NaiveDate::from_ymd(2018, 8, 13).and_hms(23, 59, 58);
|
||||
let dt = new_datetime(2018, 8, 13, 23, 59, 58);
|
||||
let mut dev = $create_method(&$mac_trans_read!(
|
||||
SECONDS,
|
||||
[
|
||||
|
@ -286,13 +293,13 @@ macro_rules! dt_test {
|
|||
],
|
||||
[0, 0, 0, 0, 0, 0, 0]
|
||||
));
|
||||
assert_eq!(dt, dev.get_datetime().unwrap());
|
||||
assert_eq!(dt, dev.datetime().unwrap());
|
||||
$destroy_method(dev);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_datetime() {
|
||||
let dt = NaiveDate::from_ymd(2018, 8, 13).and_hms(23, 59, 58);
|
||||
let dt = new_datetime(2018, 8, 13, 23, 59, 58);
|
||||
let mut dev = $create_method(&$mac_trans_write!(
|
||||
SECONDS,
|
||||
[
|
||||
|
@ -311,19 +318,19 @@ macro_rules! dt_test {
|
|||
|
||||
#[test]
|
||||
fn get_date() {
|
||||
let d = NaiveDate::from_ymd(2018, 8, 13);
|
||||
let d = new_date(2018, 8, 13);
|
||||
let mut dev = $create_method(&$mac_trans_read!(
|
||||
DOM,
|
||||
[0b0001_0011, 0b0000_1000, 0b0001_1000],
|
||||
[0, 0, 0]
|
||||
));
|
||||
assert_eq!(d, dev.get_date().unwrap());
|
||||
assert_eq!(d, dev.date().unwrap());
|
||||
$destroy_method(dev);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_date() {
|
||||
let d = NaiveDate::from_ymd(2018, 8, 13);
|
||||
let d = new_date(2018, 8, 13);
|
||||
let mut dev = $create_method(&$mac_trans_write!(
|
||||
DOW,
|
||||
[0b0000_0010, 0b0001_0011, 0b0000_1000, 0b0001_1000]
|
||||
|
@ -334,7 +341,7 @@ macro_rules! dt_test {
|
|||
|
||||
#[test]
|
||||
fn set_date_century() {
|
||||
let d = NaiveDate::from_ymd(2100, 8, 13);
|
||||
let d = new_date(2100, 8, 13);
|
||||
let mut dev = $create_method(&$mac_trans_write!(
|
||||
DOW,
|
||||
[0b0000_0110, 0b0001_0011, 0b1000_1000, 0]
|
||||
|
@ -345,19 +352,19 @@ macro_rules! dt_test {
|
|||
|
||||
#[test]
|
||||
fn get_time() {
|
||||
let t = NaiveTime::from_hms(23, 59, 58);
|
||||
let t = NaiveTime::from_hms_opt(23, 59, 58).unwrap();
|
||||
let mut dev = $create_method(&$mac_trans_read!(
|
||||
SECONDS,
|
||||
[0b0101_1000, 0b0101_1001, 0b0010_0011],
|
||||
[0, 0, 0]
|
||||
));
|
||||
assert_eq!(t, dev.get_time().unwrap());
|
||||
assert_eq!(t, dev.time().unwrap());
|
||||
$destroy_method(dev);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_time() {
|
||||
let t = NaiveTime::from_hms(23, 59, 58);
|
||||
let t = NaiveTime::from_hms_opt(23, 59, 58).unwrap();
|
||||
let mut dev = $create_method(&$mac_trans_write!(
|
||||
SECONDS,
|
||||
[0b0101_1000, 0b0101_1001, 0b0010_0011]
|
||||
|
|
|
@ -1,13 +1,9 @@
|
|||
extern crate embedded_hal_mock as hal;
|
||||
use hal::i2c::Transaction as I2cTrans;
|
||||
use hal::spi::Transaction as SpiTrans;
|
||||
|
||||
extern crate ds323x;
|
||||
use ds323x::TempConvRate;
|
||||
use embedded_hal_mock::eh1::{i2c::Transaction as I2cTrans, spi::Transaction as SpiTrans};
|
||||
|
||||
#[allow(unused)]
|
||||
mod common;
|
||||
use common::{
|
||||
use self::common::{
|
||||
destroy_ds3232, destroy_ds3234, new_ds3232, new_ds3234, BitFlags as BF, Register,
|
||||
DEVICE_ADDRESS as DEV_ADDR, DS323X_POR_STATUS,
|
||||
};
|
||||
|
@ -28,7 +24,11 @@ macro_rules! call_method_status_test {
|
|||
$method,
|
||||
new_ds3234,
|
||||
destroy_ds3234,
|
||||
[SpiTrans::write(vec![Register::STATUS + 0x80, $value])]
|
||||
[
|
||||
SpiTrans::transaction_start(),
|
||||
SpiTrans::write_vec(vec![Register::STATUS + 0x80, $value]),
|
||||
SpiTrans::transaction_end(),
|
||||
]
|
||||
);
|
||||
}
|
||||
};
|
||||
|
@ -70,10 +70,11 @@ macro_rules! set_param_test_2_4 {
|
|||
DEV_ADDR,
|
||||
vec![Register::$register, $binary_value]
|
||||
)],
|
||||
[SpiTrans::write(vec![
|
||||
Register::$register + 0x80,
|
||||
$binary_value
|
||||
])]
|
||||
[
|
||||
SpiTrans::transaction_start(),
|
||||
SpiTrans::write_vec(vec![Register::$register + 0x80, $binary_value]),
|
||||
SpiTrans::transaction_end(),
|
||||
]
|
||||
);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,16 +1,19 @@
|
|||
extern crate embedded_hal_mock as hal;
|
||||
use hal::spi::Transaction as SpiTrans;
|
||||
use embedded_hal_mock::eh1::spi::Transaction as SpiTrans;
|
||||
|
||||
#[allow(unused)]
|
||||
mod common;
|
||||
use common::{destroy_ds3234, new_ds3234, BitFlags, Register};
|
||||
use self::common::{destroy_ds3234, new_ds3234, BitFlags, Register};
|
||||
|
||||
call_test!(
|
||||
can_en_temp_conv_bat,
|
||||
enable_temperature_conversions_on_battery,
|
||||
new_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!(
|
||||
|
@ -18,8 +21,9 @@ call_test!(
|
|||
disable_temperature_conversions_on_battery,
|
||||
new_ds3234,
|
||||
destroy_ds3234,
|
||||
[SpiTrans::write(vec![
|
||||
Register::TEMP_CONV + 0x80,
|
||||
BitFlags::TEMP_CONV_BAT
|
||||
])]
|
||||
[
|
||||
SpiTrans::transaction_start(),
|
||||
SpiTrans::write_vec(vec![Register::TEMP_CONV + 0x80, BitFlags::TEMP_CONV_BAT]),
|
||||
SpiTrans::transaction_end(),
|
||||
]
|
||||
);
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
extern crate embedded_hal_mock as hal;
|
||||
use hal::i2c::Transaction as I2cTrans;
|
||||
use hal::spi::Transaction as SpiTrans;
|
||||
use embedded_hal_mock::eh1::{i2c::Transaction as I2cTrans, spi::Transaction as SpiTrans};
|
||||
mod common;
|
||||
use common::{
|
||||
use self::common::{
|
||||
destroy_ds3231, destroy_ds3232, destroy_ds3234, new_ds3231, new_ds3232, new_ds3234,
|
||||
BitFlags as BF, Register, DEVICE_ADDRESS as DEV_ADDR,
|
||||
};
|
||||
|
@ -34,10 +32,10 @@ get_param_test!(
|
|||
!BF::ALARM2F
|
||||
);
|
||||
|
||||
get_param_read_array_test!(temp_0, get_temperature, 0.0, TEMP_MSB, [0, 0], [0, 0]);
|
||||
get_param_read_array_test!(temp_0, temperature, 0.0, TEMP_MSB, [0, 0], [0, 0]);
|
||||
get_param_read_array_test!(
|
||||
temp_min,
|
||||
get_temperature,
|
||||
temperature,
|
||||
-128.0,
|
||||
TEMP_MSB,
|
||||
[0b1000_0000, 0],
|
||||
|
@ -45,7 +43,7 @@ get_param_read_array_test!(
|
|||
);
|
||||
get_param_read_array_test!(
|
||||
temp_max,
|
||||
get_temperature,
|
||||
temperature,
|
||||
127.75,
|
||||
TEMP_MSB,
|
||||
[0b0111_1111, 0b1100_0000],
|
||||
|
|
Loading…
Reference in New Issue