refactor: one alarm encoder, named register bits, and the example 14 B smaller
Both alarms' registers come from one encoder that derives DY/DT and the mask bits from the rate, and the battery checks the bytes each Table 2 row writes instead of restating the enum values. Register addresses and bits are named, the year register is the year less 2000 rather than a division, and an alarm day reads back with one decode for both forms. Over the whole API the image is 10 B smaller on both parts; a rate only known at run time costs set_alarm1 6 B more. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4df536d0a5
commit
f9bc2cd8e2
4 files changed
+156
-134
No files matched your search
+110
-98
@@ -1,9 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <span>
|
||||
#include <utility>
|
||||
|
||||
#include <libavr/i2c.hpp>
|
||||
|
||||
@@ -65,6 +67,19 @@ enum class alarm2_rate : std::uint8_t {
|
||||
|
||||
namespace detail {
|
||||
|
||||
// The register map, datasheet 19-5170 Figure 1.
|
||||
inline constexpr std::uint8_t reg_seconds = 0x00, reg_day = 0x03, reg_date = 0x04;
|
||||
inline constexpr std::uint8_t reg_alarm1 = 0x07, reg_alarm2 = 0x0b;
|
||||
inline constexpr std::uint8_t reg_control = 0x0e, reg_status = 0x0f, reg_temp = 0x11;
|
||||
|
||||
inline constexpr std::uint8_t alarm_mask = 0x80, day_of_week = 0x40;
|
||||
inline constexpr std::uint8_t twelve_hour = 0x40, pm = 0x20;
|
||||
inline constexpr std::uint8_t century = 0x80;
|
||||
inline constexpr std::uint8_t a1ie = 0x01, a2ie = 0x02, intcn = 0x04;
|
||||
inline constexpr std::uint8_t a1f = 0x01, a2f = 0x02, osf = 0x80;
|
||||
|
||||
inline constexpr std::uint16_t first_year = 2000;
|
||||
|
||||
constexpr std::uint8_t to_bcd(std::uint8_t value)
|
||||
{
|
||||
return static_cast<std::uint8_t>(((value / 10) << 4) | (value % 10));
|
||||
@@ -75,19 +90,18 @@ constexpr std::uint8_t from_bcd(std::uint8_t value)
|
||||
return static_cast<std::uint8_t>((value >> 4) * 10 + (value & 0x0f));
|
||||
}
|
||||
|
||||
// Hours register: bit 6 selects 12-hour mode, bit 5 is then PM. Writes
|
||||
// always use 24-hour form; reads accept either.
|
||||
// Writes always use the 24-hour form; reads accept either.
|
||||
constexpr std::uint8_t hours_from_reg(std::uint8_t reg)
|
||||
{
|
||||
if (reg & 0x40) {
|
||||
auto hour = from_bcd(reg & 0x1f);
|
||||
bool pm = reg & 0x20;
|
||||
if (reg & twelve_hour) {
|
||||
const auto hour = from_bcd(reg & ~(alarm_mask | twelve_hour | pm));
|
||||
const bool after_noon = reg & pm;
|
||||
if (hour == 12) {
|
||||
return pm ? 12 : 0;
|
||||
return after_noon ? 12 : 0;
|
||||
}
|
||||
return static_cast<std::uint8_t>(pm ? hour + 12 : hour);
|
||||
return static_cast<std::uint8_t>(after_noon ? hour + 12 : hour);
|
||||
}
|
||||
return from_bcd(reg & 0x3f);
|
||||
return from_bcd(reg & ~(alarm_mask | twelve_hour));
|
||||
}
|
||||
|
||||
// Sakamoto's method; the device counts weekdays 1..7 with a free epoch,
|
||||
@@ -101,9 +115,32 @@ constexpr std::uint8_t weekday(std::uint16_t year, std::uint8_t month, std::uint
|
||||
return static_cast<std::uint8_t>((year + year / 4 - year / 100 + year / 400 + offsets[month - 1] + day) % 7 + 1);
|
||||
}
|
||||
|
||||
// Control and status bits, datasheet 19-5170 Figure 1.
|
||||
inline constexpr std::uint8_t a1ie = 0x01, a2ie = 0x02, intcn = 0x04;
|
||||
inline constexpr std::uint8_t a1f = 0x01, a2f = 0x02, osf = 0x80;
|
||||
constexpr time_of_day time_from_registers(std::span<const std::uint8_t> raw)
|
||||
{
|
||||
return {hours_from_reg(raw[2]), from_bcd(raw[1] & ~alarm_mask), from_bcd(raw[0] & ~alarm_mask)};
|
||||
}
|
||||
|
||||
// Rate bit n is the mask bit of the alarm's n-th register and the bit above
|
||||
// them is DY/DT (Table 2). Alarm 2 has no seconds register, so its registers
|
||||
// are alarm 1's last three.
|
||||
inline constexpr std::size_t alarm1_registers = reg_alarm2 - reg_alarm1;
|
||||
inline constexpr std::size_t alarm2_registers = reg_control - reg_alarm2;
|
||||
|
||||
template <std::size_t Count>
|
||||
constexpr std::array<std::uint8_t, Count> alarm_registers(const date_time &at, std::uint8_t rate)
|
||||
{
|
||||
const bool weekday_mode = (rate >> Count) & 1;
|
||||
const std::array<std::uint8_t, alarm1_registers> fields{
|
||||
to_bcd(at.second),
|
||||
to_bcd(at.minute),
|
||||
to_bcd(at.hour),
|
||||
weekday_mode ? static_cast<std::uint8_t>(day_of_week | at.day) : to_bcd(at.day),
|
||||
};
|
||||
return [&]<std::size_t... I>(std::index_sequence<I...>) {
|
||||
return std::array{
|
||||
static_cast<std::uint8_t>(fields[alarm1_registers - Count + I] | (((rate >> I) & 1) ? alarm_mask : 0))...};
|
||||
}(std::make_index_sequence<Count>{});
|
||||
}
|
||||
|
||||
constexpr std::uint8_t control_with(std::uint8_t control, std::uint8_t enable_bit, alarm_interrupt interrupt)
|
||||
{
|
||||
@@ -125,10 +162,9 @@ constexpr std::uint8_t status_clearing(std::uint8_t status, std::uint8_t flag)
|
||||
// address pins (datasheet 19-5170, "I2C interface").
|
||||
inline constexpr std::uint8_t bus_address = 0x68;
|
||||
|
||||
// Maxim DS3231 RTC on any libavr i2c bus (register map: datasheet
|
||||
// 19-5170 Table 1): the master role is taken from the bus's own
|
||||
// resolution, so a declared `dev::i2c<...>` is handed over whole. Every
|
||||
// call is one bus transaction; errors surface as std::expected. Weekday
|
||||
// Maxim DS3231 RTC on any libavr i2c bus: the master role is taken from the
|
||||
// bus's own resolution, so a declared `dev::i2c<...>` is handed over whole.
|
||||
// Every read is one bus transaction; errors surface as std::expected. Weekday
|
||||
// registers are derived from the date unless the program owns them.
|
||||
template <typename Bus, weekday_source Weekday = weekday_source::computed>
|
||||
class device {
|
||||
@@ -136,25 +172,18 @@ class device {
|
||||
|
||||
using dev = avr::i2c::device<typename Bus::master, bus_address>;
|
||||
|
||||
static constexpr std::uint8_t reg_clock = 0x00;
|
||||
static constexpr std::uint8_t reg_alarm1 = 0x07;
|
||||
static constexpr std::uint8_t reg_alarm2 = 0x0b;
|
||||
static constexpr std::uint8_t reg_control = 0x0e;
|
||||
static constexpr std::uint8_t reg_status = 0x0f;
|
||||
static constexpr std::uint8_t reg_temp = 0x11;
|
||||
|
||||
public:
|
||||
[[nodiscard]] static result<date_time> read_clock()
|
||||
{
|
||||
std::array<std::uint8_t, 7> raw;
|
||||
if (auto s = dev::read_regs(reg_clock, raw); !s) {
|
||||
if (auto s = dev::read_regs(detail::reg_seconds, raw); !s) {
|
||||
return std::unexpected(s.error());
|
||||
}
|
||||
date_time now;
|
||||
static_cast<time_of_day &>(now) = decode_time(raw);
|
||||
now.day = detail::from_bcd(raw[4] & 0x3f);
|
||||
now.month = detail::from_bcd(raw[5] & 0x1f);
|
||||
now.year = static_cast<std::uint16_t>(2000 + detail::from_bcd(raw[6]));
|
||||
static_cast<time_of_day &>(now) = detail::time_from_registers(raw);
|
||||
now.day = detail::from_bcd(raw[4]);
|
||||
now.month = detail::from_bcd(raw[5] & ~detail::century);
|
||||
now.year = static_cast<std::uint16_t>(detail::first_year + detail::from_bcd(raw[6]));
|
||||
return now;
|
||||
}
|
||||
|
||||
@@ -170,50 +199,50 @@ class device {
|
||||
[[nodiscard]] static result<time_of_day> read_time()
|
||||
{
|
||||
std::array<std::uint8_t, 3> raw;
|
||||
if (auto s = dev::read_regs(reg_clock, raw); !s) {
|
||||
if (auto s = dev::read_regs(detail::reg_seconds, raw); !s) {
|
||||
return std::unexpected(s.error());
|
||||
}
|
||||
return decode_time(raw);
|
||||
return detail::time_from_registers(raw);
|
||||
}
|
||||
|
||||
[[nodiscard]] static status write_clock(const date_time &now)
|
||||
{
|
||||
if constexpr (!computes_weekday) {
|
||||
// Two transactions, because the weekday register sits between the
|
||||
// time and the date and this mode may not write it.
|
||||
if (auto s = write_time(now); !s) {
|
||||
return s;
|
||||
}
|
||||
return write_date(now);
|
||||
} else {
|
||||
if constexpr (computes_weekday) {
|
||||
std::array raw{detail::to_bcd(now.second),
|
||||
detail::to_bcd(now.minute),
|
||||
detail::to_bcd(now.hour),
|
||||
detail::weekday(now.year, now.month, now.day),
|
||||
detail::to_bcd(now.day),
|
||||
detail::to_bcd(now.month),
|
||||
detail::to_bcd(static_cast<std::uint8_t>(now.year % 100))};
|
||||
return dev::write_regs(reg_clock, raw);
|
||||
detail::to_bcd(static_cast<std::uint8_t>(now.year - detail::first_year))};
|
||||
return dev::write_regs(detail::reg_seconds, raw);
|
||||
} else {
|
||||
// Two transactions, because the weekday register sits between the
|
||||
// time and the date and this mode may not write it.
|
||||
if (auto s = write_time(now); !s) {
|
||||
return s;
|
||||
}
|
||||
return write_date(now);
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] static status write_date(const date &value)
|
||||
{
|
||||
const auto year = detail::to_bcd(static_cast<std::uint8_t>(value.year - detail::first_year));
|
||||
if constexpr (computes_weekday) {
|
||||
std::array raw{detail::weekday(value.year, value.month, value.day), detail::to_bcd(value.day),
|
||||
detail::to_bcd(value.month), detail::to_bcd(static_cast<std::uint8_t>(value.year % 100))};
|
||||
return dev::write_regs(reg_clock + 3, raw);
|
||||
detail::to_bcd(value.month), year};
|
||||
return dev::write_regs(detail::reg_day, raw);
|
||||
} else {
|
||||
std::array raw{detail::to_bcd(value.day), detail::to_bcd(value.month),
|
||||
detail::to_bcd(static_cast<std::uint8_t>(value.year % 100))};
|
||||
return dev::write_regs(reg_clock + 4, raw);
|
||||
std::array raw{detail::to_bcd(value.day), detail::to_bcd(value.month), year};
|
||||
return dev::write_regs(detail::reg_date, raw);
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] static status write_time(const time_of_day &value)
|
||||
{
|
||||
std::array raw{detail::to_bcd(value.second), detail::to_bcd(value.minute), detail::to_bcd(value.hour)};
|
||||
return dev::write_regs(reg_clock, raw);
|
||||
return dev::write_regs(detail::reg_seconds, raw);
|
||||
}
|
||||
|
||||
// Alarm times use .day as the date of month, or as weekday 1..7 with
|
||||
@@ -221,57 +250,25 @@ class device {
|
||||
[[nodiscard]] static status set_alarm1(const date_time &at, alarm1_rate rate,
|
||||
alarm_interrupt interrupt = alarm_interrupt::on)
|
||||
{
|
||||
auto m = static_cast<std::uint8_t>(rate);
|
||||
std::array raw{static_cast<std::uint8_t>(detail::to_bcd(at.second) | ((m & 1) << 7)),
|
||||
static_cast<std::uint8_t>(detail::to_bcd(at.minute) | (((m >> 1) & 1) << 7)),
|
||||
static_cast<std::uint8_t>(detail::to_bcd(at.hour) | (((m >> 2) & 1) << 7)),
|
||||
static_cast<std::uint8_t>(day_date(at.day, rate == alarm1_rate::weekday_time_match) |
|
||||
(((m >> 3) & 1) << 7))};
|
||||
if (auto s = dev::write_regs(reg_alarm1, raw); !s) {
|
||||
return s;
|
||||
}
|
||||
return set_alarm_interrupt(detail::a1ie, interrupt);
|
||||
return set_alarm<detail::alarm1_registers>(detail::reg_alarm1, at, std::to_underlying(rate), detail::a1ie,
|
||||
interrupt);
|
||||
}
|
||||
|
||||
[[nodiscard]] static status set_alarm2(const date_time &at, alarm2_rate rate,
|
||||
alarm_interrupt interrupt = alarm_interrupt::on)
|
||||
{
|
||||
auto m = static_cast<std::uint8_t>(rate);
|
||||
std::array raw{static_cast<std::uint8_t>(detail::to_bcd(at.minute) | ((m & 1) << 7)),
|
||||
static_cast<std::uint8_t>(detail::to_bcd(at.hour) | (((m >> 1) & 1) << 7)),
|
||||
static_cast<std::uint8_t>(day_date(at.day, rate == alarm2_rate::weekday_time_match) |
|
||||
(((m >> 2) & 1) << 7))};
|
||||
if (auto s = dev::write_regs(reg_alarm2, raw); !s) {
|
||||
return s;
|
||||
}
|
||||
return set_alarm_interrupt(detail::a2ie, interrupt);
|
||||
return set_alarm<detail::alarm2_registers>(detail::reg_alarm2, at, std::to_underlying(rate), detail::a2ie,
|
||||
interrupt);
|
||||
}
|
||||
|
||||
[[nodiscard]] static result<date_time> read_alarm1()
|
||||
{
|
||||
std::array<std::uint8_t, 4> raw;
|
||||
if (auto s = dev::read_regs(reg_alarm1, raw); !s) {
|
||||
return std::unexpected(s.error());
|
||||
}
|
||||
date_time at{};
|
||||
at.second = detail::from_bcd(raw[0] & 0x7f);
|
||||
at.minute = detail::from_bcd(raw[1] & 0x7f);
|
||||
at.hour = detail::hours_from_reg(raw[2] & 0x7f);
|
||||
at.day = (raw[3] & 0x40) ? (raw[3] & 0x0f) : detail::from_bcd(raw[3] & 0x3f);
|
||||
return at;
|
||||
return read_alarm<detail::alarm1_registers>(detail::reg_alarm1);
|
||||
}
|
||||
|
||||
[[nodiscard]] static result<date_time> read_alarm2()
|
||||
{
|
||||
std::array<std::uint8_t, 3> raw;
|
||||
if (auto s = dev::read_regs(reg_alarm2, raw); !s) {
|
||||
return std::unexpected(s.error());
|
||||
}
|
||||
date_time at{};
|
||||
at.minute = detail::from_bcd(raw[0] & 0x7f);
|
||||
at.hour = detail::hours_from_reg(raw[1] & 0x7f);
|
||||
at.day = (raw[2] & 0x40) ? (raw[2] & 0x0f) : detail::from_bcd(raw[2] & 0x3f);
|
||||
return at;
|
||||
return read_alarm<detail::alarm2_registers>(detail::reg_alarm2);
|
||||
}
|
||||
|
||||
[[nodiscard]] static result<bool> alarm1_fired()
|
||||
@@ -310,7 +307,7 @@ class device {
|
||||
[[nodiscard]] static result<std::int16_t> temperature_quarters()
|
||||
{
|
||||
std::array<std::uint8_t, 2> raw;
|
||||
if (auto s = dev::read_regs(reg_temp, raw); !s) {
|
||||
if (auto s = dev::read_regs(detail::reg_temp, raw); !s) {
|
||||
return std::unexpected(s.error());
|
||||
}
|
||||
return static_cast<std::int16_t>((static_cast<std::int16_t>(static_cast<std::int8_t>(raw[0])) << 2) |
|
||||
@@ -324,50 +321,65 @@ class device {
|
||||
{
|
||||
return set_alarm_interrupt(detail::a1ie, alarm_interrupt::on);
|
||||
}
|
||||
|
||||
[[nodiscard]] static status mute_alarm1()
|
||||
{
|
||||
return set_alarm_interrupt(detail::a1ie, alarm_interrupt::off);
|
||||
}
|
||||
|
||||
[[nodiscard]] static status listen_alarm2()
|
||||
{
|
||||
return set_alarm_interrupt(detail::a2ie, alarm_interrupt::on);
|
||||
}
|
||||
|
||||
[[nodiscard]] static status mute_alarm2()
|
||||
{
|
||||
return set_alarm_interrupt(detail::a2ie, alarm_interrupt::off);
|
||||
}
|
||||
|
||||
private:
|
||||
// Seconds, minutes and hours decode the same way wherever they are read
|
||||
// from - the high bit of the first two is reserved, and the hours register
|
||||
// carries its own 12/24-hour flag.
|
||||
static constexpr time_of_day decode_time(std::span<const std::uint8_t> clock)
|
||||
template <std::size_t Count>
|
||||
static status set_alarm(std::uint8_t reg, const date_time &at, std::uint8_t rate, std::uint8_t enable_bit,
|
||||
alarm_interrupt interrupt)
|
||||
{
|
||||
return {detail::hours_from_reg(clock[2]), detail::from_bcd(clock[1] & 0x7f), detail::from_bcd(clock[0] & 0x7f)};
|
||||
if (auto s = dev::write_regs(reg, detail::alarm_registers<Count>(at, rate)); !s) {
|
||||
return s;
|
||||
}
|
||||
return set_alarm_interrupt(enable_bit, interrupt);
|
||||
}
|
||||
|
||||
static constexpr std::uint8_t day_date(std::uint8_t day, bool weekday_mode)
|
||||
template <std::size_t Count>
|
||||
static result<date_time> read_alarm(std::uint8_t reg)
|
||||
{
|
||||
if (weekday_mode) {
|
||||
return static_cast<std::uint8_t>(0x40 | (day & 0x0f));
|
||||
std::array<std::uint8_t, Count> raw;
|
||||
if (auto s = dev::read_regs(reg, raw); !s) {
|
||||
return std::unexpected(s.error());
|
||||
}
|
||||
return detail::to_bcd(day) & std::uint8_t{0x3f};
|
||||
constexpr auto first = detail::alarm1_registers - Count;
|
||||
date_time at{};
|
||||
if constexpr (first == 0) {
|
||||
at.second = detail::from_bcd(raw[0] & ~detail::alarm_mask);
|
||||
}
|
||||
at.minute = detail::from_bcd(raw[1 - first] & ~detail::alarm_mask);
|
||||
at.hour = detail::hours_from_reg(raw[2 - first]);
|
||||
at.day = detail::from_bcd(raw[3 - first] & ~(detail::alarm_mask | detail::day_of_week));
|
||||
return at;
|
||||
}
|
||||
|
||||
// INTCN routes the alarms to INT/SQW, so arming an alarm's interrupt takes
|
||||
// the pin and muting it gives up only that alarm's claim on it.
|
||||
static status set_alarm_interrupt(std::uint8_t enable_bit, alarm_interrupt interrupt)
|
||||
{
|
||||
auto control = dev::read_reg(reg_control);
|
||||
auto control = dev::read_reg(detail::reg_control);
|
||||
if (!control) {
|
||||
return std::unexpected(control.error());
|
||||
}
|
||||
return dev::write_reg(reg_control, detail::control_with(*control, enable_bit, interrupt));
|
||||
return dev::write_reg(detail::reg_control, detail::control_with(*control, enable_bit, interrupt));
|
||||
}
|
||||
|
||||
static result<bool> flag_set(std::uint8_t bit)
|
||||
{
|
||||
auto flags = dev::read_reg(reg_status);
|
||||
auto flags = dev::read_reg(detail::reg_status);
|
||||
if (!flags) {
|
||||
return std::unexpected(flags.error());
|
||||
}
|
||||
@@ -376,11 +388,11 @@ class device {
|
||||
|
||||
static status clear_flag(std::uint8_t bit)
|
||||
{
|
||||
auto flags = dev::read_reg(reg_status);
|
||||
auto flags = dev::read_reg(detail::reg_status);
|
||||
if (!flags) {
|
||||
return std::unexpected(flags.error());
|
||||
}
|
||||
return dev::write_reg(reg_status, detail::status_clearing(*flags, bit));
|
||||
return dev::write_reg(detail::reg_status, detail::status_clearing(*flags, bit));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user