ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
ds3231.cpp
Go to the documentation of this file.
1#include "ds3231.h"
3#include "esphome/core/log.h"
5
6// Datasheet:
7// - https://www.analog.com/media/en/technical-documentation/data-sheets/DS3231.pdf
8
9namespace esphome::ds3231 {
10
11ESPHOME_LOG_TAG(TAG, "ds3231");
12
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;
20
21static constexpr uint8_t ALARM_MASK_BIT = 0x80; // AxMy: this register is ignored when matching
22static constexpr uint8_t ALARM_DAY_OF_WEEK = 0x40; // DY/DT: match day of week instead of date
23
25static constexpr uint32_t ALARM_PULSE_MS = 1000;
26
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");
29}
30
31// Indexed by SquareWaveFrequency
32PROGMEM_STRING_TABLE(SquareWaveStrings, "1 Hz", "1.024 kHz", "4.096 kHz", "8.192 kHz", "unknown");
33
34static __attribute__((noinline)) const LogString *square_wave_to_string(SquareWaveFrequency frequency) {
35 return SquareWaveStrings::get_log_str(frequency);
36}
37
39 if (!this->setup_check(this->read_control_status_()))
40 return;
41
42 // Keep the oscillator running on battery. INT/SQW is the alarm interrupt line unless a square
43 // wave is configured. The alarm interrupt enables (A1IE/A2IE) are kept as they are: the alarms
44 // live on the chip and survive a reboot of the ESP on battery.
45 uint8_t mask = CONTROL_EOSC | CONTROL_INTCN;
46 uint8_t value = CONTROL_INTCN;
47 if (this->square_wave_configured_) {
48 mask |= CONTROL_RS_MASK;
50 }
51 if (this->battery_backed_square_wave_ >= 0) {
52 mask |= CONTROL_BBSQW;
54 }
55 if (!this->setup_check(this->update_control_(mask, value)))
56 return;
57
58 if (this->output_32khz_ >= 0) {
60 if (!this->setup_check(this->write_status_(0)))
61 return;
62 }
63
65 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
66 return;
67 }
68
69 if ((this->status_reg_ & STATUS_OSF) != 0) {
70 ESP_LOGW(TAG, "Oscillator was stopped (battery empty or missing?), the stored time is invalid.");
71 }
73
74 if (!this->monitor_alarms_) {
75 this->disable_loop();
76 return;
77 }
78
79 // Start from a known state; an alarm that fired while the ESP was off is picked up by the first
80 // check, which runs from the loop right after setup.
81 this->publish_alarm_state_(1, false);
82 this->publish_alarm_state_(2, false);
83
84 // INT/SQW is open drain and active low: a falling edge means an enabled alarm matched.
86 // Without an interrupt pin this is how alarms are seen. With one it is a fallback: it picks up an
87 // edge that was missed, and latched flags (auto_clear_alarms: false) that were cleared elsewhere.
88 this->set_interval(this->alarm_check_interval_, [this]() { this->check_alarms_(); });
89}
90
92 // Only runs once after setup and when woken by the interrupt pin.
93 this->disable_loop();
94 this->check_alarms_();
95}
96
98 ESP_LOGCONFIG(TAG, "DS3231:");
99 LOG_I2C_DEVICE(this);
100 if (this->is_failed())
101 return;
102 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
103 if ((this->control_reg_ & CONTROL_INTCN) == 0) {
104 ESP_LOGCONFIG(TAG, " INT/SQW: square wave %s",
105 LOG_STR_ARG(square_wave_to_string(
107 } else {
108 ESP_LOGCONFIG(TAG, " INT/SQW: alarm interrupt");
109 }
110 ESP_LOGCONFIG(TAG,
111 " Battery-backed square wave: %s\n"
112 " 32 kHz output: %s\n"
113 " Alarm 1: %s\n"
114 " Alarm 2: %s\n"
115 " Auto clear alarms: %s",
116 YESNO(this->control_reg_ & CONTROL_BBSQW), ONOFF(this->status_reg_ & STATUS_EN32KHZ),
117 LOG_STR_ARG(alarm_state_to_string(this->control_reg_, 1)),
118 LOG_STR_ARG(alarm_state_to_string(this->control_reg_, 2)), YESNO(this->auto_clear_alarms_));
119 if (this->aging_offset_configured_) {
120 ESP_LOGCONFIG(TAG, " Aging offset: %d", this->aging_offset_);
121 }
122 if (this->monitor_alarms_) {
123 ESP_LOGCONFIG(TAG, " Alarm check interval: %" PRIu32 " ms", this->alarm_check_interval_);
124 }
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_);
129#endif
130}
131
132// ---------------------------------------------------------------------------------------------
133// Clock
134// ---------------------------------------------------------------------------------------------
135
136bool DS3231Component::read_calendar(uint8_t *raw) {
137 if (this->is_failed() || !this->status_comm_result(this->read_bytes(DS3231_REG_SECONDS, raw, CALENDAR_LEN)))
138 return false;
139 // Without a valid status the oscillator-stop flag is unknown, so the time can't be trusted.
140 if (!this->read_control_status_())
141 return false;
143 if ((this->status_reg_ & STATUS_OSF) != 0) {
144 ESP_LOGW(TAG, "Oscillator stop flag set, the RTC time is invalid. Write a valid time first.");
145 return false;
146 }
147 return true;
148}
149
151 if (this->is_failed() || !this->status_comm_result(this->write_bytes(DS3231_REG_SECONDS, raw, CALENDAR_LEN)))
152 return false;
153
154 // The time is valid again: clear the oscillator stop flag. If that fails, the RTC would keep
155 // reporting its time as invalid, so the write is not reported as successful.
156 if (!this->read_control_status_())
157 return false;
158 if ((this->status_reg_ & STATUS_OSF) != 0) {
159 if (!this->write_status_(STATUS_OSF))
160 return false;
161 this->status_reg_ &= ~STATUS_OSF;
162 }
164 return true;
165}
166
167// ---------------------------------------------------------------------------------------------
168// Temperature
169// ---------------------------------------------------------------------------------------------
170
172 uint8_t raw[2];
173 if (this->is_failed() || !this->read_bytes(DS3231_REG_TEMPERATURE, raw, sizeof(raw))) {
174 return false;
175 }
176 // 10-bit two's complement, 0.25 degC per LSB.
177 int16_t value = encode_value<int16_t>(raw) >> 6;
178 out = value * 0.25f;
179 return true;
180}
181
183 // Just kick off the conversion - the chip clears CONV and updates the temperature
184 // register on its own within ~200 ms, and the next temperature poll picks it up.
185 // Blocking here to wait for it would stall the main loop.
186 if (this->is_failed() || !this->read_control_status_())
187 return;
188 if ((this->status_reg_ & STATUS_BSY) != 0) {
189 ESP_LOGW(TAG, "A temperature conversion is already in progress.");
190 return;
191 }
192 // CONV is a one-shot command bit, so it is not kept in the cached value.
193 this->status_comm_result(this->write_byte(DS3231_REG_CONTROL, this->control_reg_ | CONTROL_CONV));
194}
195
196// ---------------------------------------------------------------------------------------------
197// Alarms
198// ---------------------------------------------------------------------------------------------
199
200void DS3231Component::set_alarm_1(AlarmMode mode, uint8_t second, uint8_t minute, uint8_t hour, uint8_t day_of_month,
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 ||
203 day_of_week > 7 || mode > ALARM_MODE_WEEKLY) {
204 ESP_LOGE(TAG, "Invalid alarm 1 setting.");
205 return;
206 }
207 // Mask bits A1M1..A1M4: a set bit means the register is ignored when matching.
208 uint8_t regs[4] = {
212 mode == ALARM_MODE_WEEKLY ? static_cast<uint8_t>(ALARM_DAY_OF_WEEK | day_of_week) : uint8_to_bcd(day_of_month),
213 };
214 switch (mode) {
216 regs[0] |= ALARM_MASK_BIT;
217 [[fallthrough]];
219 regs[1] |= ALARM_MASK_BIT;
220 [[fallthrough]];
222 regs[2] |= ALARM_MASK_BIT;
223 [[fallthrough]];
224 case ALARM_MODE_DAILY:
225 regs[3] |= ALARM_MASK_BIT;
226 break;
227 default: // MONTHLY / WEEKLY: everything matches
228 break;
229 }
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"));
232 this->write_alarm_(1, regs, sizeof(regs), enabled);
233}
234
235void DS3231Component::set_alarm_2(AlarmMode mode, uint8_t minute, uint8_t hour, uint8_t day_of_month,
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.");
240 return;
241 }
242 // Mask bits A2M2..A2M4.
243 uint8_t regs[3] = {
246 mode == ALARM_MODE_WEEKLY ? static_cast<uint8_t>(ALARM_DAY_OF_WEEK | day_of_week) : uint8_to_bcd(day_of_month),
247 };
248 switch (mode) {
250 regs[0] |= ALARM_MASK_BIT;
251 [[fallthrough]];
253 regs[1] |= ALARM_MASK_BIT;
254 [[fallthrough]];
255 case ALARM_MODE_DAILY:
256 regs[2] |= ALARM_MASK_BIT;
257 break;
258 default: // MONTHLY / WEEKLY: everything matches
259 break;
260 }
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"));
263 this->write_alarm_(2, regs, sizeof(regs), enabled);
264}
265
266void DS3231Component::write_alarm_(uint8_t alarm, const uint8_t *regs, size_t len, bool enabled) {
267 if (this->is_failed())
268 return;
269 if (!this->status_comm_result(this->write_bytes(alarm == 1 ? DS3231_REG_ALARM_1 : DS3231_REG_ALARM_2, regs, len)))
270 return;
271 // Changing the registers may have matched on the way: start from a clean flag. Enabling the
272 // alarm with a stale flag would assert INT/SQW right away.
273 if (!this->clear_alarm_flag_(alarm)) {
274 ESP_LOGW(TAG, "Alarm %u written, but its flag could not be cleared; not changing its enable.", alarm);
275 return;
276 }
277 this->set_alarm_enabled_(alarm, enabled);
278}
279
280void DS3231Component::set_alarm_enabled_(uint8_t alarm, bool enabled) {
281 if (alarm != 1 && alarm != 2) {
282 ESP_LOGE(TAG, "Invalid alarm number %u.", alarm);
283 return;
284 }
285 if (this->is_failed())
286 return;
287 const uint8_t bit = alarm == 1 ? CONTROL_A1IE : CONTROL_A2IE;
288 if (!this->update_control_(bit, enabled ? bit : 0))
289 return;
290 if (enabled && (this->control_reg_ & CONTROL_INTCN) == 0) {
291 ESP_LOGW(TAG, "Alarm %u enabled, but INT/SQW is a square-wave output: only the flag will be set.", alarm);
292 }
293 ESP_LOGD(TAG, "Alarm %u %s.", alarm, enabled ? LOG_STR_LITERAL("enabled") : LOG_STR_LITERAL("disabled"));
294}
295
296void DS3231Component::enable_alarm(uint8_t alarm) {
297 // The chip sets the flag on every match even while the alarm is disabled; enabling with it set
298 // would fire the trigger (and INT/SQW) for that old match right away.
299 if (this->clear_alarm_flag_(alarm))
300 this->set_alarm_enabled_(alarm, true);
301}
302
303void DS3231Component::clear_alarm(uint8_t alarm) { this->clear_alarm_flag_(alarm); }
304
306 if (alarm != 1 && alarm != 2) {
307 ESP_LOGE(TAG, "Invalid alarm number %u.", alarm);
308 return false;
309 }
310 if (this->is_failed() || !this->read_control_status_())
311 return false;
312 const uint8_t flag = alarm == 1 ? STATUS_A1F : STATUS_A2F;
313 if ((this->status_reg_ & flag) != 0) {
314 // On failure the flag is still set on the chip: keep reporting the alarm as it is.
315 if (!this->write_status_(flag))
316 return false;
317 this->status_reg_ &= ~flag;
318 }
319 this->alarm_active_[alarm - 1] = false;
320 this->publish_alarm_state_(alarm, false);
321 return true;
322}
323
325 if (this->is_failed() || !this->read_control_status_())
326 return false;
327 return (this->status_reg_ & (alarm == 1 ? STATUS_A1F : STATUS_A2F)) != 0;
328}
329
331 if (this->is_failed() || !this->read_control_status_())
332 return;
334
335 uint8_t to_clear = 0;
336 for (uint8_t alarm = 1; alarm <= 2; alarm++) {
337 const uint8_t flag = alarm == 1 ? STATUS_A1F : STATUS_A2F;
338 const uint8_t enable = alarm == 1 ? CONTROL_A1IE : CONTROL_A2IE;
339 const bool triggered = (this->status_reg_ & flag) != 0;
340 const bool enabled = (this->control_reg_ & enable) != 0;
341 bool &active = this->alarm_active_[alarm - 1];
342
343 if (!triggered) {
344 if (active && !this->auto_clear_alarms_)
345 this->publish_alarm_state_(alarm, false);
346 active = false;
347 continue;
348 }
349 // The flag is set on every match even when the alarm is disabled; only enabled alarms count.
350 if (enabled && !active) {
351 ESP_LOGI(TAG, "Alarm %u triggered.", alarm);
352 active = true;
353 this->publish_alarm_state_(alarm, true);
354 this->alarm_callback_.call(alarm);
355 }
356 if (this->auto_clear_alarms_)
357 to_clear |= flag;
358 }
359
360 if (to_clear != 0 && this->write_status_(to_clear)) {
361 this->status_reg_ &= ~to_clear;
362 for (uint8_t alarm = 1; alarm <= 2; alarm++) {
363 if ((to_clear & (alarm == 1 ? STATUS_A1F : STATUS_A2F)) == 0 || !this->alarm_active_[alarm - 1])
364 continue;
365 this->alarm_active_[alarm - 1] = false;
366#ifdef USE_BINARY_SENSOR
367 // Keep the binary sensor on long enough for the state to be seen, then turn it off again.
368 this->set_timeout(ALARM_PULSE_TIMEOUT_ID + alarm - 1, ALARM_PULSE_MS,
369 [this, alarm]() { this->publish_alarm_state_(alarm, false); });
370#endif
371 }
372 }
373}
374
376#ifdef USE_BINARY_SENSOR
377 PUBLISH_STATE(alarm == 1 ? this->alarm_1_binary_sensor_ : this->alarm_2_binary_sensor_, state);
378#endif
379}
380
382#ifdef USE_BINARY_SENSOR
383 PUBLISH_STATE(this->oscillator_stopped_binary_sensor_, (this->status_reg_ & STATUS_OSF) != 0);
384#endif
385}
386
387// ---------------------------------------------------------------------------------------------
388// Outputs and calibration
389// ---------------------------------------------------------------------------------------------
390
392 if (this->interrupt_pin_ != nullptr) {
393 ESP_LOGE(TAG, "Can't enable the square wave: INT/SQW is used as the alarm interrupt pin.");
394 return;
395 }
396 if (this->is_failed())
397 return;
399 ESP_LOGD(TAG, "Square wave enabled at %s.", LOG_STR_ARG(square_wave_to_string(frequency)));
400 }
401}
402
404 if (this->is_failed())
405 return;
407 ESP_LOGD(TAG, "Square wave disabled, INT/SQW is the alarm interrupt.");
408 }
409}
410
412 if (this->is_failed() || !this->read_control_status_())
413 return;
414 assign_bits(this->status_reg_, STATUS_EN32KHZ, enabled);
415 if (this->write_status_(0)) {
416 ESP_LOGD(TAG, "32 kHz output %s.", ONOFF(enabled));
417 }
418}
419
421 if (this->is_failed())
422 return;
423 if (!this->write_aging_offset_(offset))
424 return;
425 ESP_LOGD(TAG, "Aging offset set to %d.", offset);
426 // The new offset takes effect with the next temperature conversion.
428}
429
430// ---------------------------------------------------------------------------------------------
431// Register access
432// ---------------------------------------------------------------------------------------------
433
435 uint8_t raw[2];
436 if (!this->status_comm_result(this->read_bytes(DS3231_REG_CONTROL, raw, sizeof(raw))))
437 return false;
438 this->control_reg_ = raw[0] & ~CONTROL_CONV;
439 this->status_reg_ = raw[1];
440 return true;
441}
442
443bool DS3231Component::update_control_(uint8_t mask, uint8_t value) {
444 // CONV is a one-shot command bit: always write it back as 0 so a control change never starts
445 // a temperature conversion.
446 if (!this->status_comm_result(this->update_register(DS3231_REG_CONTROL, mask | CONTROL_CONV, value)))
447 return false;
448 this->control_reg_ = (this->control_reg_ & ~mask) | (value & mask);
449 return true;
450}
451
453 // The flags can only be written to 0; writing 1 leaves them unchanged, so a flag that was set
454 // between the read and this write is never lost.
455 const uint8_t value = ((this->status_reg_ & STATUS_EN32KHZ) | STATUS_FLAGS) & ~clear;
456 return this->status_comm_result(this->write_byte(DS3231_REG_STATUS, value));
457}
458
460 if (!this->status_comm_result(this->write_byte(DS3231_REG_AGING_OFFSET, static_cast<uint8_t>(offset))))
461 return false;
462 this->aging_offset_ = offset;
463 return true;
464}
465
466} // 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:288
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.
Definition component.h:236
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.
Definition component.cpp:88
void set_timeout(const char *name, uint32_t timeout, SchedulerCallback &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
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:391
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:266
void set_output_32khz(bool enabled)
Definition ds3231.cpp:411
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:420
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:452
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:330
SquareWaveFrequency square_wave_frequency_
Definition ds3231.h:149
void clear_alarm(uint8_t alarm)
Clear the alarm flag, which also releases the INT/SQW line.
Definition ds3231.cpp:303
bool write_aging_offset_(int8_t offset)
Definition ds3231.cpp:459
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:171
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:143
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:296
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:235
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:142
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:280
bool read_control_status_()
Read the control and status registers into the cached copies. Sets the warning flag on failure.
Definition ds3231.cpp:434
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:443
int8_t battery_backed_square_wave_
-1: leave the chip as it is, 0: off, 1: on.
Definition ds3231.h:151
void disable_square_wave()
Switch INT/SQW back to the (active-low) alarm interrupt output.
Definition ds3231.cpp:403
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:200
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:158
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:324
void publish_alarm_state_(uint8_t alarm, bool state)
Definition ds3231.cpp:375
bool write_calendar(const uint8_t *raw)
Write the calendar registers from raw and clear the oscillator-stop flag.
Definition ds3231.cpp:150
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:305
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
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:111
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
struct @67::@68 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:45
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
ESPHOME_LOG_TAG(TAG, "ds3231")
PROGMEM_STRING_TABLE(SquareWaveStrings, "1 Hz", "1.024 kHz", "4.096 kHz", "8.192 kHz", "unknown")
@ 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:1330
uint32_t value
Definition hal.cpp:59
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:1334
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:1248
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.
Definition gpio.h:60
static void uint32_t