ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
ds1307.cpp
Go to the documentation of this file.
1#include "ds1307.h"
2#include "esphome/core/log.h"
3
4// Datasheet:
5// - https://datasheets.maximintegrated.com/en/ds/DS1307.pdf
6
7namespace esphome::ds1307 {
8
9static const char *const TAG = "ds1307";
10
12 if (!this->read_rtc_()) {
13 this->mark_failed();
14 }
15}
16
18
20 ESP_LOGCONFIG(TAG, "DS1307:");
21 LOG_I2C_DEVICE(this);
22 if (this->is_failed()) {
23 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
24 }
25 RealTimeClock::dump_config();
26}
27
29 if (!this->read_rtc_()) {
30 return;
31 }
32 if (ds1307_.reg.ch) {
33 ESP_LOGW(TAG, "RTC halted, not syncing to system clock.");
34 return;
35 }
36 ESPTime rtc_time{
37 .second = uint8_t(ds1307_.reg.second + 10 * ds1307_.reg.second_10),
38 .minute = uint8_t(ds1307_.reg.minute + 10u * ds1307_.reg.minute_10),
39 .hour = uint8_t(ds1307_.reg.hour + 10u * ds1307_.reg.hour_10),
40 .day_of_week = uint8_t(ds1307_.reg.weekday),
41 .day_of_month = uint8_t(ds1307_.reg.day + 10u * ds1307_.reg.day_10),
42 .month = uint8_t(ds1307_.reg.month + 10u * ds1307_.reg.month_10),
43 .year = uint16_t(ds1307_.reg.year + 10u * ds1307_.reg.year_10 + 2000),
44 };
45 rtc_time.recalc_timestamp_utc(false);
46 if (!rtc_time.is_valid(/*check_day_of_week=*/true, /*check_day_of_year=*/false)) {
47 ESP_LOGE(TAG, "Invalid RTC time, not syncing to system clock.");
48 return;
49 }
51}
52
55 if (!now.is_valid()) {
56 ESP_LOGE(TAG, "Invalid system time, not syncing to RTC.");
57 return;
58 }
59 ds1307_.reg.year = (now.year - 2000) % 10;
60 ds1307_.reg.year_10 = (now.year - 2000) / 10 % 10;
61 ds1307_.reg.month = now.month % 10;
66 ds1307_.reg.hour = now.hour % 10;
67 ds1307_.reg.hour_10 = now.hour / 10;
72 ds1307_.reg.ch = false;
73
74 this->write_rtc_();
75}
76
78 if (!this->read_bytes(0, this->ds1307_.raw, sizeof(this->ds1307_.raw))) {
79 ESP_LOGE(TAG, "Can't read I2C data.");
80 return false;
81 }
82 ESP_LOGD(TAG, "Read %0u%0u:%0u%0u:%0u%0u 20%0u%0u-%0u%0u-%0u%0u CH:%s RS:%0u SQWE:%s OUT:%s", ds1307_.reg.hour_10,
85 ds1307_.reg.day, ONOFF(ds1307_.reg.ch), ds1307_.reg.rs, ONOFF(ds1307_.reg.sqwe), ONOFF(ds1307_.reg.out));
86
87 return true;
88}
89
91 if (!this->write_bytes(0, this->ds1307_.raw, sizeof(this->ds1307_.raw))) {
92 ESP_LOGE(TAG, "Can't write I2C data.");
93 return false;
94 }
95 ESP_LOGD(TAG, "Write %0u%0u:%0u%0u:%0u%0u 20%0u%0u-%0u%0u-%0u%0u CH:%s RS:%0u SQWE:%s OUT:%s", ds1307_.reg.hour_10,
98 ds1307_.reg.day, ONOFF(ds1307_.reg.ch), ds1307_.reg.rs, ONOFF(ds1307_.reg.sqwe), ONOFF(ds1307_.reg.out));
99 return true;
100}
101} // namespace esphome::ds1307
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:284
union esphome::ds1307::DS1307Component::DS1307Reg ds1307_
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:251
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:217
ESPTime now()
Get the time in the currently defined timezone.
ESPTime utcnow()
Get the time without any time zone or DST corrections.
void synchronize_epoch_(uint32_t epoch)
Report a unix epoch as current time.
A more user-friendly version of struct tm from time.h.
Definition time.h:23
uint8_t minute
minutes after the hour [0-59]
Definition time.h:32
void recalc_timestamp_utc(bool use_day_of_year=true)
Recalculate the timestamp field from the other fields of this ESPTime instance (must be UTC).
Definition time.cpp:282
uint8_t second
seconds after the minute [0-60]
Definition time.h:30
uint8_t hour
hours since midnight [0-23]
Definition time.h:34
bool is_valid(bool check_day_of_week=true, bool check_day_of_year=true) const
Check if this ESPTime is valid (year >= 2019 and the requested fields are in range).
Definition time.h:82
uint8_t day_of_month
day of the month [1-31]
Definition time.h:38
uint16_t year
year
Definition time.h:44
uint8_t month
month; january=1 [1-12]
Definition time.h:42
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:36
struct esphome::ds1307::DS1307Component::DS1307Reg::@76 reg