ESPHome  2024.12.2
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 
7 namespace esphome {
8 namespace ds1307 {
9 
10 static const char *const TAG = "ds1307";
11 
13  ESP_LOGCONFIG(TAG, "Setting up DS1307...");
14  if (!this->read_rtc_()) {
15  this->mark_failed();
16  }
17 }
18 
20 
22  ESP_LOGCONFIG(TAG, "DS1307:");
23  LOG_I2C_DEVICE(this);
24  if (this->is_failed()) {
25  ESP_LOGE(TAG, "Communication with DS1307 failed!");
26  }
27  ESP_LOGCONFIG(TAG, " Timezone: '%s'", this->timezone_.c_str());
28 }
29 
31 
33  if (!this->read_rtc_()) {
34  return;
35  }
36  if (ds1307_.reg.ch) {
37  ESP_LOGW(TAG, "RTC halted, not syncing to system clock.");
38  return;
39  }
40  ESPTime rtc_time{
41  .second = uint8_t(ds1307_.reg.second + 10 * ds1307_.reg.second_10),
42  .minute = uint8_t(ds1307_.reg.minute + 10u * ds1307_.reg.minute_10),
43  .hour = uint8_t(ds1307_.reg.hour + 10u * ds1307_.reg.hour_10),
44  .day_of_week = uint8_t(ds1307_.reg.weekday),
45  .day_of_month = uint8_t(ds1307_.reg.day + 10u * ds1307_.reg.day_10),
46  .day_of_year = 1, // ignored by recalc_timestamp_utc(false)
47  .month = uint8_t(ds1307_.reg.month + 10u * ds1307_.reg.month_10),
48  .year = uint16_t(ds1307_.reg.year + 10u * ds1307_.reg.year_10 + 2000),
49  .is_dst = false, // not used
50  .timestamp = 0 // overwritten by recalc_timestamp_utc(false)
51  };
52  rtc_time.recalc_timestamp_utc(false);
53  if (!rtc_time.is_valid()) {
54  ESP_LOGE(TAG, "Invalid RTC time, not syncing to system clock.");
55  return;
56  }
57  time::RealTimeClock::synchronize_epoch_(rtc_time.timestamp);
58 }
59 
62  if (!now.is_valid()) {
63  ESP_LOGE(TAG, "Invalid system time, not syncing to RTC.");
64  return;
65  }
66  ds1307_.reg.year = (now.year - 2000) % 10;
67  ds1307_.reg.year_10 = (now.year - 2000) / 10 % 10;
68  ds1307_.reg.month = now.month % 10;
69  ds1307_.reg.month_10 = now.month / 10;
73  ds1307_.reg.hour = now.hour % 10;
74  ds1307_.reg.hour_10 = now.hour / 10;
75  ds1307_.reg.minute = now.minute % 10;
77  ds1307_.reg.second = now.second % 10;
79  ds1307_.reg.ch = false;
80 
81  this->write_rtc_();
82 }
83 
85  if (!this->read_bytes(0, this->ds1307_.raw, sizeof(this->ds1307_.raw))) {
86  ESP_LOGE(TAG, "Can't read I2C data.");
87  return false;
88  }
89  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,
92  ds1307_.reg.day, ONOFF(ds1307_.reg.ch), ds1307_.reg.rs, ONOFF(ds1307_.reg.sqwe), ONOFF(ds1307_.reg.out));
93 
94  return true;
95 }
96 
98  if (!this->write_bytes(0, this->ds1307_.raw, sizeof(this->ds1307_.raw))) {
99  ESP_LOGE(TAG, "Can't write I2C data.");
100  return false;
101  }
102  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,
105  ds1307_.reg.day, ONOFF(ds1307_.reg.ch), ds1307_.reg.rs, ONOFF(ds1307_.reg.sqwe), ONOFF(ds1307_.reg.out));
106  return true;
107 }
108 } // namespace ds1307
109 } // namespace esphome
ESPTime now()
Get the time in the currently defined timezone.
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
bool is_failed() const
Definition: component.cpp:143
A more user-friendly version of struct tm from time.h.
Definition: time.h:15
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:212
void dump_config() override
Definition: ds1307.cpp:21
struct esphome::ds1307::DS1307Component::DS1307Reg::@75 reg
uint8_t second
seconds after the minute [0-60]
Definition: time.h:19
uint8_t minute
minutes after the hour [0-59]
Definition: time.h:21
union esphome::ds1307::DS1307Component::DS1307Reg ds1307_
float get_setup_priority() const override
Definition: ds1307.cpp:30
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than 2018) ...
Definition: time.h:59
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition: time.h:25
ESPTime utcnow()
Get the time without any time zone or DST corrections.
uint16_t year
year
Definition: time.h:33
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void synchronize_epoch_(uint32_t epoch)
Report a unix epoch as current time.
uint8_t month
month; january=1 [1-12]
Definition: time.h:31
uint8_t hour
hours since midnight [0-23]
Definition: time.h:23
uint8_t day_of_month
day of the month [1-31]
Definition: time.h:27
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:248