Compare commits
3 Commits
ff52f4f152
...
dfd2289aef
| Author | SHA1 | Date | |
|---|---|---|---|
| dfd2289aef | |||
| 14e608d397 | |||
| 1bc7e66389 |
24
alarms.hpp
Normal file
24
alarms.hpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
namespace rtc {
|
||||||
|
|
||||||
|
enum class Alarm1Rate : uint8_t {
|
||||||
|
ONCE_PER_S = 0b1111,
|
||||||
|
WHEN_S_MATCH = 0b1110,
|
||||||
|
WHEN_M_S_MATCH = 0b1100,
|
||||||
|
WHEN_H_M_S_MATCH = 0b1000,
|
||||||
|
WHEN_DATE_H_M_S_MATCH = 0b00000,
|
||||||
|
WHEN_DAY_H_N_S_MATCH = 0b10000,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class Alarm2Rate : uint8_t {
|
||||||
|
ONCE_PER_M = 0b111,
|
||||||
|
WHEN_M_MATCH = 0b110,
|
||||||
|
WHEN_H_M_MATCH = 0b100,
|
||||||
|
WHEN_DATE_H_M_MATCH = 0b0000,
|
||||||
|
WHEN_DAY_H_N_MATCH = 0b1000,
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace rtc
|
||||||
45
ds3231.hpp
45
ds3231.hpp
@ -187,6 +187,19 @@ class DS3231 {
|
|||||||
writePartialRegister<START_OFFSET, END_OFFSET>(timeReg);
|
writePartialRegister<START_OFFSET, END_OFFSET>(timeReg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void setAlarm1(const DateTime &alarmTime, const Alarm1Rate &alarmRate, bool enableInterrupt = true)
|
||||||
|
{
|
||||||
|
setAlarmHelper(alarmTime, alarmRate);
|
||||||
|
if (enableInterrupt)
|
||||||
|
enableInterruptHelper<detail::ControlRegFlags::A1IE>();
|
||||||
|
}
|
||||||
|
static void setAlarm2(const DateTime &alarmTime, const Alarm2Rate &alarmRate, bool enableInterrupt = true)
|
||||||
|
{
|
||||||
|
setAlarmHelper(alarmTime, alarmRate);
|
||||||
|
if (enableInterrupt)
|
||||||
|
enableInterruptHelper<detail::ControlRegFlags::A2IE>();
|
||||||
|
}
|
||||||
|
|
||||||
static bool checkAlarm1()
|
static bool checkAlarm1()
|
||||||
{
|
{
|
||||||
return checkAlarmHelper<detail::ControlStatusRegFlags::A1F>();
|
return checkAlarmHelper<detail::ControlStatusRegFlags::A1F>();
|
||||||
@ -286,6 +299,38 @@ class DS3231 {
|
|||||||
writePartialRegister<0, sizeof(Register) - 1>(reg);
|
writePartialRegister<0, sizeof(Register) - 1>(reg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename AlarmRate>
|
||||||
|
static inline void setAlarmHelper(const DateTime &alarmTime, const AlarmRate &alarmRate)
|
||||||
|
{
|
||||||
|
constexpr auto IsAlarm1 = util::is_same_v<AlarmRate, Alarm1Rate>;
|
||||||
|
static_assert(IsAlarm1 || util::is_same_v<AlarmRate, Alarm2Rate>, "Must use valid alarm rate");
|
||||||
|
|
||||||
|
using alarm_reg_t = util::conditional_t<IsAlarm1, detail::Alarm1Reg, detail::Alarm2Reg>;
|
||||||
|
|
||||||
|
alarm_reg_t alarmReg;
|
||||||
|
alarmReg.setAlarmRate(alarmRate);
|
||||||
|
alarmReg.setDate(alarmTime.day);
|
||||||
|
alarmReg.setHours(alarmTime.hour);
|
||||||
|
alarmReg.setMinutes(alarmTime.minute);
|
||||||
|
if constexpr (IsAlarm1)
|
||||||
|
alarmReg.setSeconds(alarmTime.second);
|
||||||
|
writeRegister(alarmReg);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <detail::ControlRegFlags AlarmFlag>
|
||||||
|
static inline void enableInterruptHelper()
|
||||||
|
{
|
||||||
|
constexpr auto IsAlarm1Flag = AlarmFlag == detail::ControlRegFlags::A1IE;
|
||||||
|
constexpr auto IsAlarm2Flag = AlarmFlag == detail::ControlRegFlags::A2IE;
|
||||||
|
static_assert(IsAlarm1Flag || IsAlarm2Flag, "Must use valid alarm flag");
|
||||||
|
|
||||||
|
auto controlReg = readRegister<CONTROL_REG_ADDR>();
|
||||||
|
using Flags = typename decltype(controlReg)::Flags;
|
||||||
|
controlReg &= ~Flags::BBSQW;
|
||||||
|
controlReg |= Flags::INTCN | AlarmFlag;
|
||||||
|
writeRegister(controlReg);
|
||||||
|
}
|
||||||
|
|
||||||
template <detail::ControlStatusRegFlags AlarmFlag>
|
template <detail::ControlStatusRegFlags AlarmFlag>
|
||||||
static inline bool checkAlarmHelper()
|
static inline bool checkAlarmHelper()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "alarms.hpp"
|
||||||
#include "flags.hpp"
|
#include "flags.hpp"
|
||||||
|
|
||||||
namespace rtc {
|
namespace rtc {
|
||||||
@ -194,15 +195,6 @@ struct [[gnu::packed]] Alarm1Reg
|
|||||||
uint8_t hours = 0;
|
uint8_t hours = 0;
|
||||||
uint8_t day_date = 0;
|
uint8_t day_date = 0;
|
||||||
|
|
||||||
enum class AlarmRate {
|
|
||||||
ONCE_PER_S = 0b1111,
|
|
||||||
WHEN_S_MATCH = 0b1110,
|
|
||||||
WHEN_M_S_MATCH = 0b1100,
|
|
||||||
WHEN_H_M_S_MATCH = 0b1000,
|
|
||||||
WHEN_DATE_H_M_S_MATCH = 0b00000,
|
|
||||||
WHEN_DAY_H_N_S_MATCH = 0b10000,
|
|
||||||
};
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
inline uint8_t getSeconds() const
|
inline uint8_t getSeconds() const
|
||||||
@ -225,7 +217,7 @@ struct [[gnu::packed]] Alarm1Reg
|
|||||||
{
|
{
|
||||||
return getEncodedDate(date, day_date);
|
return getEncodedDate(date, day_date);
|
||||||
}
|
}
|
||||||
inline AlarmRate getAlarmRate() const
|
inline Alarm1Rate getAlarmRate() const
|
||||||
{
|
{
|
||||||
constexpr auto M_FLAG = 7;
|
constexpr auto M_FLAG = 7;
|
||||||
const auto m1 = (seconds >> M_FLAG) & 1;
|
const auto m1 = (seconds >> M_FLAG) & 1;
|
||||||
@ -236,9 +228,9 @@ struct [[gnu::packed]] Alarm1Reg
|
|||||||
if (m == 0) {
|
if (m == 0) {
|
||||||
constexpr auto DAY_FLAG = 6;
|
constexpr auto DAY_FLAG = 6;
|
||||||
const auto dayFormat = ((day_date >> DAY_FLAG) & 1) << 4;
|
const auto dayFormat = ((day_date >> DAY_FLAG) & 1) << 4;
|
||||||
return static_cast<AlarmRate>(dayFormat);
|
return static_cast<Alarm1Rate>(dayFormat);
|
||||||
}
|
}
|
||||||
return static_cast<AlarmRate>(m);
|
return static_cast<Alarm1Rate>(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
@ -263,7 +255,7 @@ struct [[gnu::packed]] Alarm1Reg
|
|||||||
{
|
{
|
||||||
setEncodedDate(day_date, date);
|
setEncodedDate(day_date, date);
|
||||||
}
|
}
|
||||||
inline void setAlarmRate(const AlarmRate &alarmRate)
|
inline void setAlarmRate(const Alarm1Rate &alarmRate)
|
||||||
{
|
{
|
||||||
const auto alarmRateFlags = static_cast<uint8_t>(alarmRate);
|
const auto alarmRateFlags = static_cast<uint8_t>(alarmRate);
|
||||||
constexpr auto M_FLAG = 7;
|
constexpr auto M_FLAG = 7;
|
||||||
@ -288,14 +280,6 @@ struct [[gnu::packed]] Alarm2Reg
|
|||||||
uint8_t hours = 0;
|
uint8_t hours = 0;
|
||||||
uint8_t day_date = 0;
|
uint8_t day_date = 0;
|
||||||
|
|
||||||
enum class AlarmRate {
|
|
||||||
ONCE_PER_M = 0b111,
|
|
||||||
WHEN_M_MATCH = 0b110,
|
|
||||||
WHEN_H_M_MATCH = 0b100,
|
|
||||||
WHEN_DATE_H_M_MATCH = 0b0000,
|
|
||||||
WHEN_DAY_H_N_MATCH = 0b1000,
|
|
||||||
};
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
inline uint8_t getMinutes() const
|
inline uint8_t getMinutes() const
|
||||||
@ -314,7 +298,7 @@ struct [[gnu::packed]] Alarm2Reg
|
|||||||
{
|
{
|
||||||
return getEncodedDate(date, day_date);
|
return getEncodedDate(date, day_date);
|
||||||
}
|
}
|
||||||
inline AlarmRate getAlarmRate() const
|
inline Alarm2Rate getAlarmRate() const
|
||||||
{
|
{
|
||||||
constexpr auto M_FLAG = 7;
|
constexpr auto M_FLAG = 7;
|
||||||
const auto m2 = (minutes >> M_FLAG) & 1;
|
const auto m2 = (minutes >> M_FLAG) & 1;
|
||||||
@ -324,9 +308,9 @@ struct [[gnu::packed]] Alarm2Reg
|
|||||||
if (m == 0) {
|
if (m == 0) {
|
||||||
constexpr auto DAY_FLAG = 6;
|
constexpr auto DAY_FLAG = 6;
|
||||||
const auto dayFormat = ((day_date >> DAY_FLAG) & 1) << 3;
|
const auto dayFormat = ((day_date >> DAY_FLAG) & 1) << 3;
|
||||||
return static_cast<AlarmRate>(dayFormat);
|
return static_cast<Alarm2Rate>(dayFormat);
|
||||||
}
|
}
|
||||||
return static_cast<AlarmRate>(m);
|
return static_cast<Alarm2Rate>(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
@ -347,7 +331,7 @@ struct [[gnu::packed]] Alarm2Reg
|
|||||||
{
|
{
|
||||||
setEncodedDate(day_date, date);
|
setEncodedDate(day_date, date);
|
||||||
}
|
}
|
||||||
inline void setAlarmRate(const AlarmRate &alarmRate)
|
inline void setAlarmRate(const Alarm2Rate &alarmRate)
|
||||||
{
|
{
|
||||||
const auto alarmRateFlags = static_cast<uint8_t>(alarmRate);
|
const auto alarmRateFlags = static_cast<uint8_t>(alarmRate);
|
||||||
constexpr auto M_FLAG = 7;
|
constexpr auto M_FLAG = 7;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user