Rewrite on libavr
Same driver surface as the yazoalfa version — clock and alarm get/set, alarm interrupts, flag check/clear — plus oscillator-stop detection and die temperature. One source for tiny85 (software I2C) and mega328P (TWI), built against libavr in both generated and reflect mode, byte-identical .text across modes. Errors surface as std::expected instead of being dropped; weekday-rate alarms now really set the DY bit (legacy cleared it); multi-register access is one coherent bus transaction. Legacy stays on master. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
304
include/ds3231/ds3231.hpp
Normal file
304
include/ds3231/ds3231.hpp
Normal file
@@ -0,0 +1,304 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
|
||||
#include <libavr/i2c.hpp>
|
||||
|
||||
namespace ds3231 {
|
||||
|
||||
using avr::i2c::error;
|
||||
using avr::i2c::status;
|
||||
template <typename T>
|
||||
using result = std::expected<T, error>;
|
||||
|
||||
struct date {
|
||||
std::uint16_t year; // 2000..2099
|
||||
std::uint8_t month; // 1..12
|
||||
std::uint8_t day; // 1..31 (weekday 1..7 in weekday-alarm reads)
|
||||
friend constexpr bool operator==(const date &, const date &) = default;
|
||||
};
|
||||
|
||||
struct time_of_day {
|
||||
std::uint8_t hour; // 0..23
|
||||
std::uint8_t minute;
|
||||
std::uint8_t second;
|
||||
friend constexpr bool operator==(const time_of_day &, const time_of_day &) = default;
|
||||
};
|
||||
|
||||
struct date_time : date, time_of_day {
|
||||
friend constexpr bool operator==(const date_time &, const date_time &) = default;
|
||||
};
|
||||
|
||||
// Alarm match rates (datasheet Table 2). Alarm 2 has no seconds register
|
||||
// and fires at :00 of the matching minute.
|
||||
enum class alarm1_rate : std::uint8_t {
|
||||
once_per_second = 0b01111,
|
||||
seconds_match = 0b01110,
|
||||
minutes_seconds_match = 0b01100,
|
||||
time_match = 0b01000,
|
||||
date_time_match = 0b00000,
|
||||
weekday_time_match = 0b10000,
|
||||
};
|
||||
|
||||
enum class alarm2_rate : std::uint8_t {
|
||||
once_per_minute = 0b0111,
|
||||
minutes_match = 0b0110,
|
||||
time_match = 0b0100,
|
||||
date_time_match = 0b0000,
|
||||
weekday_time_match = 0b1000,
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
|
||||
constexpr std::uint8_t to_bcd(std::uint8_t value)
|
||||
{
|
||||
return static_cast<std::uint8_t>(((value / 10) << 4) | (value % 10));
|
||||
}
|
||||
|
||||
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.
|
||||
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 (hour == 12)
|
||||
return pm ? 12 : 0;
|
||||
return static_cast<std::uint8_t>(pm ? hour + 12 : hour);
|
||||
}
|
||||
return from_bcd(reg & 0x3f);
|
||||
}
|
||||
|
||||
// Sakamoto's method; the device counts weekdays 1..7 with a free epoch,
|
||||
// this maps Sunday to 1.
|
||||
constexpr std::uint8_t weekday(std::uint16_t year, std::uint8_t month, std::uint8_t day)
|
||||
{
|
||||
constexpr std::uint8_t offsets[]{0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
|
||||
if (month < 3)
|
||||
--year;
|
||||
return static_cast<std::uint8_t>((year + year / 4 - year / 100 + year / 400 + offsets[month - 1] + day) % 7 + 1);
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
// Maxim DS3231 RTC on any libavr i2c master (register map: datasheet
|
||||
// 19-5170 Table 1). Every call is one bus transaction; errors surface as
|
||||
// std::expected. Weekday registers maintain themselves from the date when
|
||||
// SetWeekday is on.
|
||||
template <typename Bus, bool SetWeekday = true>
|
||||
class device {
|
||||
using dev = avr::i2c::device<Bus, 0x68>;
|
||||
|
||||
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;
|
||||
|
||||
static constexpr std::uint8_t a1ie = 0x01, a2ie = 0x02, intcn = 0x04, bbsqw = 0x40;
|
||||
static constexpr std::uint8_t a1f = 0x01, a2f = 0x02, osf = 0x80;
|
||||
|
||||
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)
|
||||
return std::unexpected(s.error());
|
||||
date_time now;
|
||||
now.second = detail::from_bcd(raw[0] & 0x7f);
|
||||
now.minute = detail::from_bcd(raw[1] & 0x7f);
|
||||
now.hour = detail::hours_from_reg(raw[2]);
|
||||
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]));
|
||||
return now;
|
||||
}
|
||||
|
||||
[[nodiscard]] static result<date> read_date()
|
||||
{
|
||||
auto now = read_clock();
|
||||
if (!now)
|
||||
return std::unexpected(now.error());
|
||||
return static_cast<date>(*now);
|
||||
}
|
||||
|
||||
[[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)
|
||||
return std::unexpected(s.error());
|
||||
return time_of_day{detail::hours_from_reg(raw[2]), detail::from_bcd(raw[1] & 0x7f),
|
||||
detail::from_bcd(raw[0] & 0x7f)};
|
||||
}
|
||||
|
||||
[[nodiscard]] static status write_clock(const date_time &now)
|
||||
{
|
||||
std::array<std::uint8_t, 7> raw{detail::to_bcd(now.second),
|
||||
detail::to_bcd(now.minute),
|
||||
detail::to_bcd(now.hour),
|
||||
SetWeekday ? detail::weekday(now.year, now.month, now.day) : std::uint8_t{1},
|
||||
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);
|
||||
}
|
||||
|
||||
[[nodiscard]] static status write_date(const date &value)
|
||||
{
|
||||
if constexpr (SetWeekday) {
|
||||
std::array<std::uint8_t, 4> 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);
|
||||
} else {
|
||||
std::array<std::uint8_t, 3> 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);
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] static status write_time(const time_of_day &value)
|
||||
{
|
||||
std::array<std::uint8_t, 3> raw{detail::to_bcd(value.second), detail::to_bcd(value.minute),
|
||||
detail::to_bcd(value.hour)};
|
||||
return dev::write_regs(reg_clock, raw);
|
||||
}
|
||||
|
||||
// Alarm times use .day as the date of month, or as weekday 1..7 with
|
||||
// the weekday_time_match rates.
|
||||
[[nodiscard]] static status set_alarm1(const date_time &at, alarm1_rate rate, bool enable_interrupt = true)
|
||||
{
|
||||
auto m = static_cast<std::uint8_t>(rate);
|
||||
std::array<std::uint8_t, 4> 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 enable_interrupt ? enable_alarm_interrupt(a1ie) : status{};
|
||||
}
|
||||
|
||||
[[nodiscard]] static status set_alarm2(const date_time &at, alarm2_rate rate, bool enable_interrupt = true)
|
||||
{
|
||||
auto m = static_cast<std::uint8_t>(rate);
|
||||
std::array<std::uint8_t, 3> 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 enable_interrupt ? enable_alarm_interrupt(a2ie) : status{};
|
||||
}
|
||||
|
||||
[[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;
|
||||
}
|
||||
|
||||
[[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;
|
||||
}
|
||||
|
||||
[[nodiscard]] static result<bool> alarm1_fired()
|
||||
{
|
||||
return flag_set(a1f);
|
||||
}
|
||||
|
||||
[[nodiscard]] static result<bool> alarm2_fired()
|
||||
{
|
||||
return flag_set(a2f);
|
||||
}
|
||||
|
||||
[[nodiscard]] static status clear_alarm1()
|
||||
{
|
||||
return clear_flag(a1f);
|
||||
}
|
||||
|
||||
[[nodiscard]] static status clear_alarm2()
|
||||
{
|
||||
return clear_flag(a2f);
|
||||
}
|
||||
|
||||
// True after the oscillator was ever stopped (first power-up, battery
|
||||
// ran out): the clock needs to be set.
|
||||
[[nodiscard]] static result<bool> oscillator_stopped()
|
||||
{
|
||||
return flag_set(osf);
|
||||
}
|
||||
|
||||
[[nodiscard]] static status clear_oscillator_stopped()
|
||||
{
|
||||
return clear_flag(osf);
|
||||
}
|
||||
|
||||
// Die temperature in quarter °C (updated every 64 s by the 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)
|
||||
return std::unexpected(s.error());
|
||||
return static_cast<std::int16_t>((static_cast<std::int16_t>(static_cast<std::int8_t>(raw[0])) << 2) |
|
||||
(raw[1] >> 6));
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr std::uint8_t day_date(std::uint8_t day, bool weekday_mode)
|
||||
{
|
||||
if (weekday_mode)
|
||||
return static_cast<std::uint8_t>(0x40 | (day & 0x0f));
|
||||
return detail::to_bcd(day) & std::uint8_t{0x3f};
|
||||
}
|
||||
|
||||
static status enable_alarm_interrupt(std::uint8_t enable_bit)
|
||||
{
|
||||
auto control = dev::read_reg(reg_control);
|
||||
if (!control)
|
||||
return std::unexpected(control.error());
|
||||
return dev::write_reg(reg_control, static_cast<std::uint8_t>((*control & ~bbsqw) | intcn | enable_bit));
|
||||
}
|
||||
|
||||
static result<bool> flag_set(std::uint8_t bit)
|
||||
{
|
||||
auto flags = dev::read_reg(reg_status);
|
||||
if (!flags)
|
||||
return std::unexpected(flags.error());
|
||||
return (*flags & bit) != 0;
|
||||
}
|
||||
|
||||
static status clear_flag(std::uint8_t bit)
|
||||
{
|
||||
auto flags = dev::read_reg(reg_status);
|
||||
if (!flags)
|
||||
return std::unexpected(flags.error());
|
||||
return dev::write_reg(reg_status, static_cast<std::uint8_t>(*flags & ~bit));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ds3231
|
||||
Reference in New Issue
Block a user