12static constexpr uint8_t DS3231_REG_SECONDS = 0x00;
13static constexpr uint8_t DS3231_REG_ALARM_1 = 0x07;
14static constexpr uint8_t DS3231_REG_ALARM_2 = 0x0B;
15static constexpr uint8_t DS3231_REG_CONTROL = 0x0E;
16static constexpr uint8_t DS3231_REG_STATUS = 0x0F;
17static constexpr uint8_t DS3231_REG_AGING_OFFSET = 0x10;
18static constexpr uint8_t DS3231_REG_TEMPERATURE = 0x11;
20static constexpr uint8_t ALARM_MASK_BIT = 0x80;
21static constexpr uint8_t ALARM_DAY_OF_WEEK = 0x40;
24static constexpr uint32_t ALARM_PULSE_MS = 1000;
26static const LogString *alarm_state_to_string(uint8_t control, uint8_t alarm) {
27 return (control & (alarm == 1 ? 0x01 : 0x02)) != 0 ? LOG_STR(
"enabled") : LOG_STR(
"disabled");
33 return LOG_STR(
"1 Hz");
35 return LOG_STR(
"1.024 kHz");
37 return LOG_STR(
"4.096 kHz");
39 return LOG_STR(
"8.192 kHz");
41 return LOG_STR(
"unknown");
85 ESP_LOGW(TAG,
"Oscillator was stopped (battery empty or missing?), the stored time is invalid.");
117 ESP_LOGCONFIG(TAG,
"DS3231:");
118 LOG_I2C_DEVICE(
this);
123 ESP_LOGCONFIG(TAG,
" INT/SQW: square wave %s",
124 LOG_STR_ARG(square_wave_to_string(
127 ESP_LOGCONFIG(TAG,
" INT/SQW: alarm interrupt");
130 " Battery-backed square wave: %s\n"
131 " 32 kHz output: %s\n"
134 " Auto clear alarms: %s",
136 LOG_STR_ARG(alarm_state_to_string(this->
control_reg_, 1)),
139 ESP_LOGCONFIG(TAG,
" Aging offset: %d", this->
aging_offset_);
144#ifdef USE_BINARY_SENSOR
145 LOG_BINARY_SENSOR(
" ",
"Alarm 1", this->alarm_1_binary_sensor_);
146 LOG_BINARY_SENSOR(
" ",
"Alarm 2", this->alarm_2_binary_sensor_);
147 LOG_BINARY_SENSOR(
" ",
"Oscillator Stopped", this->oscillator_stopped_binary_sensor_);
155bool DS3231Component::read_calendar(uint8_t *
raw) {
163 ESP_LOGW(TAG,
"Oscillator stop flag set, the RTC time is invalid. Write a valid time first.");
208 ESP_LOGW(TAG,
"A temperature conversion is already in progress.");
220 uint8_t day_of_week,
bool enabled) {
221 if (
second > 59 ||
minute > 59 ||
hour > 23 || day_of_month < 1 || day_of_month > 31 || day_of_week < 1 ||
223 ESP_LOGE(TAG,
"Invalid alarm 1 setting.");
235 regs[0] |= ALARM_MASK_BIT;
238 regs[1] |= ALARM_MASK_BIT;
241 regs[2] |= ALARM_MASK_BIT;
244 regs[3] |= ALARM_MASK_BIT;
249 ESP_LOGD(TAG,
"Setting alarm 1: mode %u, %02u:%02u:%02u, day of month %u, day of week %u, %s",
mode,
hour,
minute,
250 second, day_of_month, day_of_week, enabled ? LOG_STR_LITERAL(
"enabled") : LOG_STR_LITERAL(
"disabled"));
255 uint8_t day_of_week,
bool enabled) {
256 if (
minute > 59 ||
hour > 23 || day_of_month < 1 || day_of_month > 31 || day_of_week < 1 || day_of_week > 7 ||
258 ESP_LOGE(TAG,
"Invalid alarm 2 setting.");
269 regs[0] |= ALARM_MASK_BIT;
272 regs[1] |= ALARM_MASK_BIT;
275 regs[2] |= ALARM_MASK_BIT;
280 ESP_LOGD(TAG,
"Setting alarm 2: mode %u, %02u:%02u, day of month %u, day of week %u, %s",
mode,
hour,
minute,
281 day_of_month, day_of_week, enabled ? LOG_STR_LITERAL(
"enabled") : LOG_STR_LITERAL(
"disabled"));
293 ESP_LOGW(TAG,
"Alarm %u written, but its flag could not be cleared; not changing its enable.", alarm);
300 if (alarm != 1 && alarm != 2) {
301 ESP_LOGE(TAG,
"Invalid alarm number %u.", alarm);
310 ESP_LOGW(TAG,
"Alarm %u enabled, but INT/SQW is a square-wave output: only the flag will be set.", alarm);
312 ESP_LOGD(TAG,
"Alarm %u %s.", alarm, enabled ? LOG_STR_LITERAL(
"enabled") : LOG_STR_LITERAL(
"disabled"));
325 if (alarm != 1 && alarm != 2) {
326 ESP_LOGE(TAG,
"Invalid alarm number %u.", alarm);
354 uint8_t to_clear = 0;
355 for (uint8_t alarm = 1; alarm <= 2; alarm++) {
358 const bool triggered = (this->
status_reg_ & flag) != 0;
359 const bool enabled = (this->
control_reg_ & enable) != 0;
369 if (enabled && !active) {
370 ESP_LOGI(TAG,
"Alarm %u triggered.", alarm);
381 for (uint8_t alarm = 1; alarm <= 2; alarm++) {
385#ifdef USE_BINARY_SENSOR
395#ifdef USE_BINARY_SENSOR
396 PUBLISH_STATE(alarm == 1 ? this->alarm_1_binary_sensor_ : this->alarm_2_binary_sensor_,
state);
401#ifdef USE_BINARY_SENSOR
412 ESP_LOGE(TAG,
"Can't enable the square wave: INT/SQW is used as the alarm interrupt pin.");
418 ESP_LOGD(TAG,
"Square wave enabled at %s.", LOG_STR_ARG(square_wave_to_string(
frequency)));
426 ESP_LOGD(TAG,
"Square wave disabled, INT/SQW is the alarm interrupt.");
435 ESP_LOGD(TAG,
"32 kHz output %s.", ONOFF(enabled));
444 ESP_LOGD(TAG,
"Aging offset set to %d.", offset);
BedjetMode mode
BedJet operating mode.
void mark_failed()
Mark this component as failed.
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
void disable_loop()
Disable this component's loop.
void set_interval(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a const char* name.
Hub for a single DS3231 real-time clock.
void enable_square_wave(SquareWaveFrequency frequency)
Switch INT/SQW to a square-wave output at frequency. Alarms then no longer drive the pin.
void publish_oscillator_stopped_()
uint32_t alarm_check_interval_
void write_alarm_(uint8_t alarm, const uint8_t *regs, size_t len, bool enabled)
Write the alarm registers, clear the stale flag and set the interrupt enable.
void set_output_32khz(bool enabled)
static constexpr uint8_t STATUS_FLAGS
static constexpr uint8_t STATUS_A1F
static constexpr uint8_t STATUS_OSF
static constexpr uint8_t CONTROL_A2IE
void set_aging_offset(int8_t offset)
Aging offset in LSB (about 0.1 ppm each at 25 degC); positive values slow the clock down.
bool write_status_(uint8_t clear)
Write the status register, clearing the flags in clear (writing 1 to a flag leaves it unchanged).
static constexpr uint8_t CONTROL_BBSQW
static constexpr uint8_t CONTROL_RS_SHIFT
void check_alarms_()
Read the flags, fire triggers and update the binary sensors.
SquareWaveFrequency square_wave_frequency_
void clear_alarm(uint8_t alarm)
Clear the alarm flag, which also releases the INT/SQW line.
bool write_aging_offset_(int8_t offset)
void dump_config() override
bool aging_offset_configured_
static constexpr uint8_t CONTROL_A1IE
static constexpr uint8_t CONTROL_INTCN
bool read_temperature(float &out)
static void interrupt_handler(DS3231Component *arg)
static constexpr uint8_t STATUS_BSY
static constexpr uint8_t STATUS_A2F
LazyCallbackManager< void(uint8_t)> alarm_callback_
bool square_wave_configured_
void enable_alarm(uint8_t alarm)
Enable alarm. A match from while it was disabled is cleared first, so only the next one fires.
void set_alarm_2(AlarmMode mode, uint8_t minute, uint8_t hour, uint8_t day_of_month, uint8_t day_of_week, bool enabled)
Program alarm 2 (minute resolution, fires at second 00).
static constexpr uint8_t STATUS_EN32KHZ
static constexpr uint8_t CONTROL_RS_MASK
static constexpr uint8_t CONTROL_EOSC
InternalGPIOPin * interrupt_pin_
static constexpr uint32_t ALARM_PULSE_TIMEOUT_ID
Scheduler ids: one pulse timer per alarm (id + alarm - 1).
void set_alarm_enabled_(uint8_t alarm, bool enabled)
bool read_control_status_()
Read the control and status registers into the cached copies. Sets the warning flag on failure.
bool update_control_(uint8_t mask, uint8_t value)
Replace the bits in mask of the control register with value, on the chip and in the cache.
int8_t battery_backed_square_wave_
-1: leave the chip as it is, 0: off, 1: on.
void disable_square_wave()
Switch INT/SQW back to the (active-low) alarm interrupt output.
void set_alarm_1(AlarmMode mode, uint8_t second, uint8_t minute, uint8_t hour, uint8_t day_of_month, uint8_t day_of_week, bool enabled)
Program alarm 1. day_of_month is used in MONTHLY mode, day_of_week (1 = Sunday) in WEEKLY mode.
static constexpr uint8_t CONTROL_CONV
bool alarm_active_[2]
Set while an alarm flag has been reported, so a latched flag fires its trigger only once.
bool is_alarm_triggered(uint8_t alarm)
True while the alarm flag of alarm (1 or 2) is set on the chip.
void force_temperature_conversion()
void publish_alarm_state_(uint8_t alarm, bool state)
bool write_calendar(const uint8_t *raw)
Write the calendar registers from raw and clear the oscillator-stop flag.
bool clear_alarm_flag_(uint8_t alarm)
Clear the flag of alarm and report it off. Returns false (state unchanged) if that fails.
static constexpr size_t CALENDAR_LEN
Seconds through year, the 7-byte BCD calendar the time platform decodes with time::DS_BCD_CALENDAR.
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
bool write_byte(uint8_t a_register, uint8_t data) const
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
bool update_register(uint8_t a_register, uint8_t mask, uint8_t value)
Read a register, replace the bits in mask with those of value and write it back.
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
std::vector< uint8_t > & out
AlarmMode
When an alarm fires. Alarm 1 supports every mode, alarm 2 has no seconds register and so no EVERY_SEC...
@ ALARM_MODE_WEEKLY
When day of week, hours, minutes (and seconds) match.
@ ALARM_MODE_EVERY_SECOND
Alarm 1 only.
@ ALARM_MODE_DAILY
When hours, minutes (and seconds) match.
@ ALARM_MODE_EVERY_MINUTE
Alarm 1: when the seconds match. Alarm 2: at second 00 of every minute.
@ ALARM_MODE_HOURLY
When minutes (and seconds) match.
SquareWaveFrequency
Frequency of the INT/SQW pin when it is a square-wave output (RS2:RS1 in the control register).
ESPHOME_LOG_TAG(TAG, "ds3231")
constexpr uint8_t uint8_to_bcd(uint8_t value)
Encode a value from 0 to 99 as a two-digit packed BCD byte, e.g. 59 to 0x59.
ESPHOME_ALWAYS_INLINE constexpr void assign_bits(T &value, std::type_identity_t< T > mask, bool on)
Set the bits of mask in value when on is true, clear them otherwise.
constexpr T encode_value(const uint8_t *bytes)
Encode a value from its constituent bytes (from most to least significant) in an array with length si...