13static constexpr uint8_t DS3231_REG_SECONDS = 0x00;
14static constexpr uint8_t DS3231_REG_ALARM_1 = 0x07;
15static constexpr uint8_t DS3231_REG_ALARM_2 = 0x0B;
16static constexpr uint8_t DS3231_REG_CONTROL = 0x0E;
17static constexpr uint8_t DS3231_REG_STATUS = 0x0F;
18static constexpr uint8_t DS3231_REG_AGING_OFFSET = 0x10;
19static constexpr uint8_t DS3231_REG_TEMPERATURE = 0x11;
21static constexpr uint8_t ALARM_MASK_BIT = 0x80;
22static constexpr uint8_t ALARM_DAY_OF_WEEK = 0x40;
25static constexpr uint32_t ALARM_PULSE_MS = 1000;
27static const LogString *alarm_state_to_string(uint8_t control, uint8_t alarm) {
28 return (control & (alarm == 1 ? 0x01 : 0x02)) != 0 ? LOG_STR(
"enabled") : LOG_STR(
"disabled");
35 return SquareWaveStrings::get_log_str(
frequency);
70 ESP_LOGW(TAG,
"Oscillator was stopped (battery empty or missing?), the stored time is invalid.");
98 ESP_LOGCONFIG(TAG,
"DS3231:");
104 ESP_LOGCONFIG(TAG,
" INT/SQW: square wave %s",
105 LOG_STR_ARG(square_wave_to_string(
108 ESP_LOGCONFIG(TAG,
" INT/SQW: alarm interrupt");
111 " Battery-backed square wave: %s\n"
112 " 32 kHz output: %s\n"
115 " Auto clear alarms: %s",
117 LOG_STR_ARG(alarm_state_to_string(this->
control_reg_, 1)),
120 ESP_LOGCONFIG(TAG,
" Aging offset: %d", this->
aging_offset_);
125#ifdef USE_BINARY_SENSOR
126 LOG_BINARY_SENSOR(
" ",
"Alarm 1", this->alarm_1_binary_sensor_);
127 LOG_BINARY_SENSOR(
" ",
"Alarm 2", this->alarm_2_binary_sensor_);
128 LOG_BINARY_SENSOR(
" ",
"Oscillator Stopped", this->oscillator_stopped_binary_sensor_);
136bool DS3231Component::read_calendar(uint8_t *
raw) {
144 ESP_LOGW(TAG,
"Oscillator stop flag set, the RTC time is invalid. Write a valid time first.");
189 ESP_LOGW(TAG,
"A temperature conversion is already in progress.");
201 uint8_t day_of_week,
bool enabled) {
202 if (
second > 59 ||
minute > 59 ||
hour > 23 || day_of_month < 1 || day_of_month > 31 || day_of_week < 1 ||
204 ESP_LOGE(TAG,
"Invalid alarm 1 setting.");
216 regs[0] |= ALARM_MASK_BIT;
219 regs[1] |= ALARM_MASK_BIT;
222 regs[2] |= ALARM_MASK_BIT;
225 regs[3] |= ALARM_MASK_BIT;
230 ESP_LOGD(TAG,
"Setting alarm 1: mode %u, %02u:%02u:%02u, day of month %u, day of week %u, %s",
mode,
hour,
minute,
231 second, day_of_month, day_of_week, enabled ? LOG_STR_LITERAL(
"enabled") : LOG_STR_LITERAL(
"disabled"));
236 uint8_t day_of_week,
bool enabled) {
237 if (
minute > 59 ||
hour > 23 || day_of_month < 1 || day_of_month > 31 || day_of_week < 1 || day_of_week > 7 ||
239 ESP_LOGE(TAG,
"Invalid alarm 2 setting.");
250 regs[0] |= ALARM_MASK_BIT;
253 regs[1] |= ALARM_MASK_BIT;
256 regs[2] |= ALARM_MASK_BIT;
261 ESP_LOGD(TAG,
"Setting alarm 2: mode %u, %02u:%02u, day of month %u, day of week %u, %s",
mode,
hour,
minute,
262 day_of_month, day_of_week, enabled ? LOG_STR_LITERAL(
"enabled") : LOG_STR_LITERAL(
"disabled"));
274 ESP_LOGW(TAG,
"Alarm %u written, but its flag could not be cleared; not changing its enable.", alarm);
281 if (alarm != 1 && alarm != 2) {
282 ESP_LOGE(TAG,
"Invalid alarm number %u.", alarm);
291 ESP_LOGW(TAG,
"Alarm %u enabled, but INT/SQW is a square-wave output: only the flag will be set.", alarm);
293 ESP_LOGD(TAG,
"Alarm %u %s.", alarm, enabled ? LOG_STR_LITERAL(
"enabled") : LOG_STR_LITERAL(
"disabled"));
306 if (alarm != 1 && alarm != 2) {
307 ESP_LOGE(TAG,
"Invalid alarm number %u.", alarm);
335 uint8_t to_clear = 0;
336 for (uint8_t alarm = 1; alarm <= 2; alarm++) {
339 const bool triggered = (this->
status_reg_ & flag) != 0;
340 const bool enabled = (this->
control_reg_ & enable) != 0;
350 if (enabled && !active) {
351 ESP_LOGI(TAG,
"Alarm %u triggered.", alarm);
362 for (uint8_t alarm = 1; alarm <= 2; alarm++) {
366#ifdef USE_BINARY_SENSOR
376#ifdef USE_BINARY_SENSOR
377 PUBLISH_STATE(alarm == 1 ? this->alarm_1_binary_sensor_ : this->alarm_2_binary_sensor_,
state);
382#ifdef USE_BINARY_SENSOR
393 ESP_LOGE(TAG,
"Can't enable the square wave: INT/SQW is used as the alarm interrupt pin.");
399 ESP_LOGD(TAG,
"Square wave enabled at %s.", LOG_STR_ARG(square_wave_to_string(
frequency)));
407 ESP_LOGD(TAG,
"Square wave disabled, INT/SQW is the alarm interrupt.");
416 ESP_LOGD(TAG,
"32 kHz output %s.", ONOFF(enabled));
425 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.
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
void disable_loop()
Disable this component's loop.
void set_interval(const char *name, uint32_t interval, SchedulerCallback &&f)
Set an interval function with a const char* name.
void set_timeout(const char *name, uint32_t timeout, SchedulerCallback &&f)
Set a timeout function with a const char* name.
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 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.
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.
struct @67::@68 __attribute__
Wake the main loop task from an ISR. ISR-safe.
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")
PROGMEM_STRING_TABLE(SquareWaveStrings, "1 Hz", "1.024 kHz", "4.096 kHz", "8.192 kHz", "unknown")
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...
bool setup_wake_interrupt(GPIOPin *pin, Component *component, gpio::InterruptType type)
Set up pin and attach the wake interrupt when it is a GPIO of the chip itself.