ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ds3231.cpp
Go to the documentation of this file.
1#include "ds3231.h"
3#include "esphome/core/log.h"
4
5// Datasheet:
6// - https://www.analog.com/media/en/technical-documentation/data-sheets/DS3231.pdf
7
8namespace esphome::ds3231 {
9
10ESPHOME_LOG_TAG(TAG, "ds3231");
11
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;
19
20static constexpr uint8_t ALARM_MASK_BIT = 0x80; // AxMy: this register is ignored when matching
21static constexpr uint8_t ALARM_DAY_OF_WEEK = 0x40; // DY/DT: match day of week instead of date
22
24static constexpr uint32_t ALARM_PULSE_MS = 1000;
25
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");
28}
29
30static const LogString *square_wave_to_string(SquareWaveFrequency frequency) {
31 switch (frequency) {
32 case SQUARE_WAVE_1HZ:
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");
40 default:
41 return LOG_STR("unknown");
42 }
43}
44
46
48 if (!this->read_control_status_()) {
49 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
50 return;
51 }
52
53 // Keep the oscillator running on battery. INT/SQW is the alarm interrupt line unless a square
54 // wave is configured. The alarm interrupt enables (A1IE/A2IE) are kept as they are: the alarms
55 // live on the chip and survive a reboot of the ESP on battery.
56 uint8_t mask = CONTROL_EOSC | CONTROL_INTCN;
57 uint8_t value = CONTROL_INTCN;
58 if (this->square_wave_configured_) {
59 mask |= CONTROL_RS_MASK;
61 }
62 if (this->battery_backed_square_wave_ >= 0) {
63 mask |= CONTROL_BBSQW;
65 }
66 if (!this->update_control_(mask, value)) {
67 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
68 return;
69 }
70
71 if (this->output_32khz_ >= 0) {
73 if (!this->write_status_(0)) {
74 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
75 return;
76 }
77 }
78
80 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
81 return;
82 }
83
84 if ((this->status_reg_ & STATUS_OSF) != 0) {
85 ESP_LOGW(TAG, "Oscillator was stopped (battery empty or missing?), the stored time is invalid.");
86 }
88
89 if (!this->monitor_alarms_) {
90 this->disable_loop();
91 return;
92 }
93
94 // Start from a known state; an alarm that fired while the ESP was off is picked up by the first
95 // check, which runs from the loop right after setup.
96 this->publish_alarm_state_(1, false);
97 this->publish_alarm_state_(2, false);
98
99 if (this->interrupt_pin_ != nullptr) {
100 this->interrupt_pin_->setup();
101 // INT/SQW is open drain and active low: a falling edge means an enabled alarm matched.
102 // The handler only wakes the loop; the flags are read from there.
104 }
105 // Without an interrupt pin this is how alarms are seen. With one it is a fallback: it picks up an
106 // edge that was missed, and latched flags (auto_clear_alarms: false) that were cleared elsewhere.
107 this->set_interval(this->alarm_check_interval_, [this]() { this->check_alarms_(); });
108}
109
111 // Only runs once after setup and when woken by the interrupt pin.
112 this->disable_loop();
113 this->check_alarms_();
114}
115
117 ESP_LOGCONFIG(TAG, "DS3231:");
118 LOG_I2C_DEVICE(this);
119 if (this->is_failed())
120 return;
121 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
122 if ((this->control_reg_ & CONTROL_INTCN) == 0) {
123 ESP_LOGCONFIG(TAG, " INT/SQW: square wave %s",
124 LOG_STR_ARG(square_wave_to_string(
126 } else {
127 ESP_LOGCONFIG(TAG, " INT/SQW: alarm interrupt");
128 }
129 ESP_LOGCONFIG(TAG,
130 " Battery-backed square wave: %s\n"
131 " 32 kHz output: %s\n"
132 " Alarm 1: %s\n"
133 " Alarm 2: %s\n"
134 " Auto clear alarms: %s",
135 YESNO(this->control_reg_ & CONTROL_BBSQW), ONOFF(this->status_reg_ & STATUS_EN32KHZ),
136 LOG_STR_ARG(alarm_state_to_string(this->control_reg_, 1)),
137 LOG_STR_ARG(alarm_state_to_string(this->control_reg_, 2)), YESNO(this->auto_clear_alarms_));
138 if (this->aging_offset_configured_) {
139 ESP_LOGCONFIG(TAG, " Aging offset: %d", this->aging_offset_);
140 }
141 if (this->monitor_alarms_) {
142 ESP_LOGCONFIG(TAG, " Alarm check interval: %" PRIu32 " ms", this->alarm_check_interval_);
143 }
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_);
148#endif
149}
150
151// ---------------------------------------------------------------------------------------------
152// Clock
153// ---------------------------------------------------------------------------------------------
154
155bool DS3231Component::read_calendar(uint8_t *raw) {
156 if (this->is_failed() || !this->status_comm_result(this->read_bytes(DS3231_REG_SECONDS, raw, CALENDAR_LEN)))
157 return false;
158 // Without a valid status the oscillator-stop flag is unknown, so the time can't be trusted.
159 if (!this->read_control_status_())
160 return false;
162 if ((this->status_reg_ & STATUS_OSF) != 0) {
163 ESP_LOGW(TAG, "Oscillator stop flag set, the RTC time is invalid. Write a valid time first.");
164 return false;
165 }
166 return true;
167}
168
170 if (this->is_failed() || !this->status_comm_result(this->write_bytes(DS3231_REG_SECONDS, raw, CALENDAR_LEN)))
171 return false;
172
173 // The time is valid again: clear the oscillator stop flag. If that fails, the RTC would keep
174 // reporting its time as invalid, so the write is not reported as successful.
175 if (!this->read_control_status_())
176 return false;
177 if ((this->status_reg_ & STATUS_OSF) != 0) {
178 if (!this->write_status_(STATUS_OSF))
179 return false;
180 this->status_reg_ &= ~STATUS_OSF;
181 }
183 return true;
184}
185
186// ---------------------------------------------------------------------------------------------
187// Temperature
188// ---------------------------------------------------------------------------------------------
189
191 uint8_t raw[2];
192 if (this->is_failed() || !this->read_bytes(DS3231_REG_TEMPERATURE, raw, sizeof(raw))) {
193 return false;
194 }
195 // 10-bit two's complement, 0.25 degC per LSB.
196 int16_t value = encode_value<int16_t>(raw) >> 6;
197 out = value * 0.25f;
198 return true;
199}
200
202 // Just kick off the conversion - the chip clears CONV and updates the temperature
203 // register on its own within ~200 ms, and the next temperature poll picks it up.
204 // Blocking here to wait for it would stall the main loop.
205 if (this->is_failed() || !this->read_control_status_())
206 return;
207 if ((this->status_reg_ & STATUS_BSY) != 0) {
208 ESP_LOGW(TAG, "A temperature conversion is already in progress.");
209 return;
210 }
211 // CONV is a one-shot command bit, so it is not kept in the cached value.
212 this->status_comm_result(this->write_byte(DS3231_REG_CONTROL, this->control_reg_ | CONTROL_CONV));
213}
214
215// ---------------------------------------------------------------------------------------------
216// Alarms
217// ---------------------------------------------------------------------------------------------
218
219void DS3231Component::set_alarm_1(AlarmMode mode, uint8_t second, uint8_t minute, uint8_t hour, uint8_t day_of_month,
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 ||
222 day_of_week > 7 || mode > ALARM_MODE_WEEKLY) {
223 ESP_LOGE(TAG, "Invalid alarm 1 setting.");
224 return;
225 }
226 // Mask bits A1M1..A1M4: a set bit means the register is ignored when matching.
227 uint8_t regs[4] = {
231 mode == ALARM_MODE_WEEKLY ? static_cast<uint8_t>(ALARM_DAY_OF_WEEK | day_of_week) : uint8_to_bcd(day_of_month),
232 };
233 switch (mode) {
235 regs[0] |= ALARM_MASK_BIT;
236 [[fallthrough]];
238 regs[1] |= ALARM_MASK_BIT;
239 [[fallthrough]];
241 regs[2] |= ALARM_MASK_BIT;
242 [[fallthrough]];
243 case ALARM_MODE_DAILY:
244 regs[3] |= ALARM_MASK_BIT;
245 break;
246 default: // MONTHLY / WEEKLY: everything matches
247 break;
248 }
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"));
251 this->write_alarm_(1, regs, sizeof(regs), enabled);
252}
253
254void DS3231Component::set_alarm_2(AlarmMode mode, uint8_t minute, uint8_t hour, uint8_t day_of_month,
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.");
259 return;
260 }
261 // Mask bits A2M2..A2M4.
262 uint8_t regs[3] = {
265 mode == ALARM_MODE_WEEKLY ? static_cast<uint8_t>(ALARM_DAY_OF_WEEK | day_of_week) : uint8_to_bcd(day_of_month),
266 };
267 switch (mode) {
269 regs[0] |= ALARM_MASK_BIT;
270 [[fallthrough]];
272 regs[1] |= ALARM_MASK_BIT;
273 [[fallthrough]];
274 case ALARM_MODE_DAILY:
275 regs[2] |= ALARM_MASK_BIT;
276 break;
277 default: // MONTHLY / WEEKLY: everything matches
278 break;
279 }
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"));
282 this->write_alarm_(2, regs, sizeof(regs), enabled);
283}
284
285void DS3231Component::write_alarm_(uint8_t alarm, const uint8_t *regs, size_t len, bool enabled) {
286 if (this->is_failed())
287 return;
288 if (!this->status_comm_result(this->write_bytes(alarm == 1 ? DS3231_REG_ALARM_1 : DS3231_REG_ALARM_2, regs, len)))
289 return;
290 // Changing the registers may have matched on the way: start from a clean flag. Enabling the
291 // alarm with a stale flag would assert INT/SQW right away.
292 if (!this->clear_alarm_flag_(alarm)) {
293 ESP_LOGW(TAG, "Alarm %u written, but its flag could not be cleared; not changing its enable.", alarm);
294 return;
295 }
296 this->set_alarm_enabled_(alarm, enabled);
297}
298
299void DS3231Component::set_alarm_enabled_(uint8_t alarm, bool enabled) {
300 if (alarm != 1 && alarm != 2) {
301 ESP_LOGE(TAG, "Invalid alarm number %u.", alarm);
302 return;
303 }
304 if (this->is_failed())
305 return;
306 const uint8_t bit = alarm == 1 ? CONTROL_A1IE : CONTROL_A2IE;
307 if (!this->update_control_(bit, enabled ? bit : 0))
308 return;
309 if (enabled && (this->control_reg_ & CONTROL_INTCN) == 0) {
310 ESP_LOGW(TAG, "Alarm %u enabled, but INT/SQW is a square-wave output: only the flag will be set.", alarm);
311 }
312 ESP_LOGD(TAG, "Alarm %u %s.", alarm, enabled ? LOG_STR_LITERAL("enabled") : LOG_STR_LITERAL("disabled"));
313}
314
315void DS3231Component::enable_alarm(uint8_t alarm) {
316 // The chip sets the flag on every match even while the alarm is disabled; enabling with it set
317 // would fire the trigger (and INT/SQW) for that old match right away.
318 if (this->clear_alarm_flag_(alarm))
319 this->set_alarm_enabled_(alarm, true);
320}
321
322void DS3231Component::clear_alarm(uint8_t alarm) { this->clear_alarm_flag_(alarm); }
323
325 if (alarm != 1 && alarm != 2) {
326 ESP_LOGE(TAG, "Invalid alarm number %u.", alarm);
327 return false;
328 }
329 if (this->is_failed() || !this->read_control_status_())
330 return false;
331 const uint8_t flag = alarm == 1 ? STATUS_A1F : STATUS_A2F;
332 if ((this->status_reg_ & flag) != 0) {
333 // On failure the flag is still set on the chip: keep reporting the alarm as it is.
334 if (!this->write_status_(flag))
335 return false;
336 this->status_reg_ &= ~flag;
337 }
338 this->alarm_active_[alarm - 1] = false;
339 this->publish_alarm_state_(alarm, false);
340 return true;
341}
342
344 if (this->is_failed() || !this->read_control_status_())
345 return false;
346 return (this->status_reg_ & (alarm == 1 ? STATUS_A1F : STATUS_A2F)) != 0;
347}
348
350 if (this->is_failed() || !this->read_control_status_())
351 return;
353
354 uint8_t to_clear = 0;
355 for (uint8_t alarm = 1; alarm <= 2; alarm++) {
356 const uint8_t flag = alarm == 1 ? STATUS_A1F : STATUS_A2F;
357 const uint8_t enable = alarm == 1 ? CONTROL_A1IE : CONTROL_A2IE;
358 const bool triggered = (this->status_reg_ & flag) != 0;
359 const bool enabled = (this->control_reg_ & enable) != 0;
360 bool &active = this->alarm_active_[alarm - 1];
361
362 if (!triggered) {
363 if (active && !this->auto_clear_alarms_)
364 this->publish_alarm_state_(alarm, false);
365 active = false;
366 continue;
367 }
368 // The flag is set on every match even when the alarm is disabled; only enabled alarms count.
369 if (enabled && !active) {
370 ESP_LOGI(TAG, "Alarm %u triggered.", alarm);
371 active = true;
372 this->publish_alarm_state_(alarm, true);
373 this->alarm_callback_.call(alarm);
374 }
375 if (this->auto_clear_alarms_)
376 to_clear |= flag;
377 }
378
379 if (to_clear != 0 && this->write_status_(to_clear)) {
380 this->status_reg_ &= ~to_clear;
381 for (uint8_t alarm = 1; alarm <= 2; alarm++) {
382 if ((to_clear & (alarm == 1 ? STATUS_A1F : STATUS_A2F)) == 0 || !this->alarm_active_[alarm - 1])
383 continue;
384 this->alarm_active_[alarm - 1] = false;
385#ifdef USE_BINARY_SENSOR
386 // Keep the binary sensor on long enough for the state to be seen, then turn it off again.
387 this->set_timeout(ALARM_PULSE_TIMEOUT_ID + alarm - 1, ALARM_PULSE_MS,
388 [this, alarm]() { this->publish_alarm_state_(alarm, false); });
389#endif
390 }
391 }
392}
393
395#ifdef USE_BINARY_SENSOR
396 PUBLISH_STATE(alarm == 1 ? this->alarm_1_binary_sensor_ : this->alarm_2_binary_sensor_, state);
397#endif
398}
399
401#ifdef USE_BINARY_SENSOR
402 PUBLISH_STATE(this->oscillator_stopped_binary_sensor_, (this->status_reg_ & STATUS_OSF) != 0);
403#endif
404}
405
406// ---------------------------------------------------------------------------------------------
407// Outputs and calibration
408// ---------------------------------------------------------------------------------------------
409
411 if (this->interrupt_pin_ != nullptr) {
412 ESP_LOGE(TAG, "Can't enable the square wave: INT/SQW is used as the alarm interrupt pin.");
413 return;
414 }
415 if (this->is_failed())
416 return;
418 ESP_LOGD(TAG, "Square wave enabled at %s.", LOG_STR_ARG(square_wave_to_string(frequency)));
419 }
420}
421
423 if (this->is_failed())
424 return;
426 ESP_LOGD(TAG, "Square wave disabled, INT/SQW is the alarm interrupt.");
427 }
428}
429
431 if (this->is_failed() || !this->read_control_status_())
432 return;
433 assign_bits(this->status_reg_, STATUS_EN32KHZ, enabled);
434 if (this->write_status_(0)) {
435 ESP_LOGD(TAG, "32 kHz output %s.", ONOFF(enabled));
436 }
437}
438
440 if (this->is_failed())
441 return;
442 if (!this->write_aging_offset_(offset))
443 return;
444 ESP_LOGD(TAG, "Aging offset set to %d.", offset);
445 // The new offset takes effect with the next temperature conversion.
447}
448
449// ---------------------------------------------------------------------------------------------
450// Register access
451// ---------------------------------------------------------------------------------------------
452
454 uint8_t raw[2];
455 if (!this->status_comm_result(this->read_bytes(DS3231_REG_CONTROL, raw, sizeof(raw))))
456 return false;
457 this->control_reg_ = raw[0] & ~CONTROL_CONV;
458 this->status_reg_ = raw[1];
459 return true;
460}
461
462bool DS3231Component::update_control_(uint8_t mask, uint8_t value) {
463 // CONV is a one-shot command bit: always write it back as 0 so a control change never starts
464 // a temperature conversion.
465 if (!this->status_comm_result(this->update_register(DS3231_REG_CONTROL, mask | CONTROL_CONV, value)))
466 return false;
467 this->control_reg_ = (this->control_reg_ & ~mask) | (value & mask);
468 return true;
469}
470
472 // The flags can only be written to 0; writing 1 leaves them unchanged, so a flag that was set
473 // between the read and this write is never lost.
474 const uint8_t value = ((this->status_reg_ & STATUS_EN32KHZ) | STATUS_FLAGS) & ~clear;
475 return this->status_comm_result(this->write_byte(DS3231_REG_STATUS, value));
476}
477
479 if (!this->status_comm_result(this->write_byte(DS3231_REG_AGING_OFFSET, static_cast<uint8_t>(offset))))
480 return false;
481 this->aging_offset_ = offset;
482 return true;
483}
484
485} // namespace esphome::ds3231
BedjetMode mode
BedJet operating mode.
uint8_t raw[35]
Definition bl0939.h:0
uint16_le_t frequency
Definition bl0942.h:6
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:287
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.
Definition component.cpp:96
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.
Definition component.cpp:88
Hub for a single DS3231 real-time clock.
Definition ds3231.h:35
void enable_square_wave(SquareWaveFrequency frequency)
Switch INT/SQW to a square-wave output at frequency. Alarms then no longer drive the pin.
Definition ds3231.cpp:410
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.
Definition ds3231.cpp:285
void set_output_32khz(bool enabled)
Definition ds3231.cpp:430
static constexpr uint8_t STATUS_FLAGS
Definition ds3231.h:120
static constexpr uint8_t STATUS_A1F
Definition ds3231.h:115
static constexpr uint8_t STATUS_OSF
Definition ds3231.h:119
static constexpr uint8_t CONTROL_A2IE
Definition ds3231.h:107
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.
Definition ds3231.cpp:439
bool write_status_(uint8_t clear)
Write the status register, clearing the flags in clear (writing 1 to a flag leaves it unchanged).
Definition ds3231.cpp:471
static constexpr uint8_t CONTROL_BBSQW
Definition ds3231.h:112
static constexpr uint8_t CONTROL_RS_SHIFT
Definition ds3231.h:110
void check_alarms_()
Read the flags, fire triggers and update the binary sensors.
Definition ds3231.cpp:349
SquareWaveFrequency square_wave_frequency_
Definition ds3231.h:151
void clear_alarm(uint8_t alarm)
Clear the alarm flag, which also releases the INT/SQW line.
Definition ds3231.cpp:322
bool write_aging_offset_(int8_t offset)
Definition ds3231.cpp:478
static constexpr uint8_t CONTROL_A1IE
Definition ds3231.h:106
static constexpr uint8_t CONTROL_INTCN
Definition ds3231.h:108
bool read_temperature(float &out)
Definition ds3231.cpp:190
static void interrupt_handler(DS3231Component *arg)
Definition ds3231.cpp:45
static constexpr uint8_t STATUS_BSY
Definition ds3231.h:117
static constexpr uint8_t STATUS_A2F
Definition ds3231.h:116
LazyCallbackManager< void(uint8_t)> alarm_callback_
Definition ds3231.h:145
void enable_alarm(uint8_t alarm)
Enable alarm. A match from while it was disabled is cleared first, so only the next one fires.
Definition ds3231.cpp:315
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).
Definition ds3231.cpp:254
static constexpr uint8_t STATUS_EN32KHZ
Definition ds3231.h:118
static constexpr uint8_t CONTROL_RS_MASK
Definition ds3231.h:109
static constexpr uint8_t CONTROL_EOSC
Definition ds3231.h:113
InternalGPIOPin * interrupt_pin_
Definition ds3231.h:144
static constexpr uint32_t ALARM_PULSE_TIMEOUT_ID
Scheduler ids: one pulse timer per alarm (id + alarm - 1).
Definition ds3231.h:123
void set_alarm_enabled_(uint8_t alarm, bool enabled)
Definition ds3231.cpp:299
bool read_control_status_()
Read the control and status registers into the cached copies. Sets the warning flag on failure.
Definition ds3231.cpp:453
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.
Definition ds3231.cpp:462
int8_t battery_backed_square_wave_
-1: leave the chip as it is, 0: off, 1: on.
Definition ds3231.h:153
void disable_square_wave()
Switch INT/SQW back to the (active-low) alarm interrupt output.
Definition ds3231.cpp:422
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.
Definition ds3231.cpp:219
static constexpr uint8_t CONTROL_CONV
Definition ds3231.h:111
bool alarm_active_[2]
Set while an alarm flag has been reported, so a latched flag fires its trigger only once.
Definition ds3231.h:160
bool is_alarm_triggered(uint8_t alarm)
True while the alarm flag of alarm (1 or 2) is set on the chip.
Definition ds3231.cpp:343
void publish_alarm_state_(uint8_t alarm, bool state)
Definition ds3231.cpp:394
bool write_calendar(const uint8_t *raw)
Write the calendar registers from raw and clear the oscillator-stop flag.
Definition ds3231.cpp:169
bool clear_alarm_flag_(uint8_t alarm)
Clear the flag of alarm and report it off. Returns false (state unchanged) if that fails.
Definition ds3231.cpp:324
static constexpr size_t CALENDAR_LEN
Seconds through year, the 7-byte BCD calendar the time platform decodes with time::DS_BCD_CALENDAR.
Definition ds3231.h:38
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition gpio.h:29
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:298
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.
Definition i2c.cpp:95
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:249
uint8_t second
uint8_t minute
uint8_t hour
bool state
Definition fan.h:2
std::vector< uint8_t > & out
Definition gif.cpp:49
AlarmMode
When an alarm fires. Alarm 1 supports every mode, alarm 2 has no seconds register and so no EVERY_SEC...
Definition ds3231.h:16
@ ALARM_MODE_WEEKLY
When day of week, hours, minutes (and seconds) match.
Definition ds3231.h:22
@ ALARM_MODE_EVERY_SECOND
Alarm 1 only.
Definition ds3231.h:17
@ ALARM_MODE_DAILY
When hours, minutes (and seconds) match.
Definition ds3231.h:20
@ ALARM_MODE_EVERY_MINUTE
Alarm 1: when the seconds match. Alarm 2: at second 00 of every minute.
Definition ds3231.h:18
@ ALARM_MODE_HOURLY
When minutes (and seconds) match.
Definition ds3231.h:19
SquareWaveFrequency
Frequency of the INT/SQW pin when it is a square-wave output (RS2:RS1 in the control register).
Definition ds3231.h:26
@ SQUARE_WAVE_4096HZ
Definition ds3231.h:29
@ SQUARE_WAVE_8192HZ
Definition ds3231.h:30
@ SQUARE_WAVE_1024HZ
Definition ds3231.h:28
ESPHOME_LOG_TAG(TAG, "ds3231")
@ INTERRUPT_FALLING_EDGE
Definition gpio_pin.h:53
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.
Definition helpers.h:1146
uint32_t value
Definition hal.cpp:41
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.
Definition helpers.h:1150
const void size_t len
Definition hal.h:68
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...
Definition helpers.h:1064
static void uint32_t