ESPHome 2026.3.0-dev
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 {
8namespace ds1307 {
9
10static const char *const TAG = "ds1307";
11
13 if (!this->read_rtc_()) {
14 this->mark_failed();
15 }
16}
17
19
21 ESP_LOGCONFIG(TAG, "DS1307:");
22 LOG_I2C_DEVICE(this);
23 if (this->is_failed()) {
24 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
25 }
26 RealTimeClock::dump_config();
27}
28
30 if (!this->read_rtc_()) {
31 return;
32 }
33 if (ds1307_.reg.ch) {
34 ESP_LOGW(TAG, "RTC halted, not syncing to system clock.");
35 return;
36 }
37 ESPTime rtc_time{
38 .second = uint8_t(ds1307_.reg.second + 10 * ds1307_.reg.second_10),
39 .minute = uint8_t(ds1307_.reg.minute + 10u * ds1307_.reg.minute_10),
40 .hour = uint8_t(ds1307_.reg.hour + 10u * ds1307_.reg.hour_10),
41 .day_of_week = uint8_t(ds1307_.reg.weekday),
42 .day_of_month = uint8_t(ds1307_.reg.day + 10u * ds1307_.reg.day_10),
43 .day_of_year = 1, // ignored by recalc_timestamp_utc(false)
44 .month = uint8_t(ds1307_.reg.month + 10u * ds1307_.reg.month_10),
45 .year = uint16_t(ds1307_.reg.year + 10u * ds1307_.reg.year_10 + 2000),
46 .is_dst = false, // not used
47 .timestamp = 0 // overwritten by recalc_timestamp_utc(false)
48 };
49 rtc_time.recalc_timestamp_utc(false);
50 if (!rtc_time.is_valid()) {
51 ESP_LOGE(TAG, "Invalid RTC time, not syncing to system clock.");
52 return;
53 }
55}
56
59 if (!now.is_valid()) {
60 ESP_LOGE(TAG, "Invalid system time, not syncing to RTC.");
61 return;
62 }
63 ds1307_.reg.year = (now.year - 2000) % 10;
64 ds1307_.reg.year_10 = (now.year - 2000) / 10 % 10;
65 ds1307_.reg.month = now.month % 10;
70 ds1307_.reg.hour = now.hour % 10;
71 ds1307_.reg.hour_10 = now.hour / 10;
76 ds1307_.reg.ch = false;
77
78 this->write_rtc_();
79}
80
82 if (!this->read_bytes(0, this->ds1307_.raw, sizeof(this->ds1307_.raw))) {
83 ESP_LOGE(TAG, "Can't read I2C data.");
84 return false;
85 }
86 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,
89 ds1307_.reg.day, ONOFF(ds1307_.reg.ch), ds1307_.reg.rs, ONOFF(ds1307_.reg.sqwe), ONOFF(ds1307_.reg.out));
90
91 return true;
92}
93
95 if (!this->write_bytes(0, this->ds1307_.raw, sizeof(this->ds1307_.raw))) {
96 ESP_LOGE(TAG, "Can't write I2C data.");
97 return false;
98 }
99 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,
102 ds1307_.reg.day, ONOFF(ds1307_.reg.ch), ds1307_.reg.rs, ONOFF(ds1307_.reg.sqwe), ONOFF(ds1307_.reg.out));
103 return true;
104}
105} // namespace ds1307
106} // namespace esphome
void mark_failed()
Mark this component as failed.
bool is_failed() const
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.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
A more user-friendly version of struct tm from time.h.
Definition time.h:21
uint8_t minute
minutes after the hour [0-59]
Definition time.h:30
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:232
uint8_t second
seconds after the minute [0-60]
Definition time.h:28
uint8_t hour
hours since midnight [0-23]
Definition time.h:32
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than or equal to 2019)
Definition time.h:78
uint8_t day_of_month
day of the month [1-31]
Definition time.h:36
uint16_t year
year
Definition time.h:42
uint8_t month
month; january=1 [1-12]
Definition time.h:40
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:34
struct esphome::ds1307::DS1307Component::DS1307Reg::@76 reg