ESPHome  2022.12.8
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  time::ESPTime rtc_time{.second = uint8_t(ds1307_.reg.second + 10 * ds1307_.reg.second_10),
41  .minute = uint8_t(ds1307_.reg.minute + 10u * ds1307_.reg.minute_10),
42  .hour = uint8_t(ds1307_.reg.hour + 10u * ds1307_.reg.hour_10),
43  .day_of_week = uint8_t(ds1307_.reg.weekday),
44  .day_of_month = uint8_t(ds1307_.reg.day + 10u * ds1307_.reg.day_10),
45  .day_of_year = 1, // ignored by recalc_timestamp_utc(false)
46  .month = uint8_t(ds1307_.reg.month + 10u * ds1307_.reg.month_10),
47  .year = uint16_t(ds1307_.reg.year + 10u * ds1307_.reg.year_10 + 2000)};
48  rtc_time.recalc_timestamp_utc(false);
49  if (!rtc_time.is_valid()) {
50  ESP_LOGE(TAG, "Invalid RTC time, not syncing to system clock.");
51  return;
52  }
53  time::RealTimeClock::synchronize_epoch_(rtc_time.timestamp);
54 }
55 
58  if (!now.is_valid()) {
59  ESP_LOGE(TAG, "Invalid system time, not syncing to RTC.");
60  return;
61  }
62  ds1307_.reg.year = (now.year - 2000) % 10;
63  ds1307_.reg.year_10 = (now.year - 2000) / 10 % 10;
64  ds1307_.reg.month = now.month % 10;
65  ds1307_.reg.month_10 = now.month / 10;
69  ds1307_.reg.hour = now.hour % 10;
70  ds1307_.reg.hour_10 = now.hour / 10;
71  ds1307_.reg.minute = now.minute % 10;
73  ds1307_.reg.second = now.second % 10;
75  ds1307_.reg.ch = false;
76 
77  this->write_rtc_();
78 }
79 
81  if (!this->read_bytes(0, this->ds1307_.raw, sizeof(this->ds1307_.raw))) {
82  ESP_LOGE(TAG, "Can't read I2C data.");
83  return false;
84  }
85  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,
88  ds1307_.reg.day, ONOFF(ds1307_.reg.ch), ds1307_.reg.rs, ONOFF(ds1307_.reg.sqwe), ONOFF(ds1307_.reg.out));
89 
90  return true;
91 }
92 
94  if (!this->write_bytes(0, this->ds1307_.raw, sizeof(this->ds1307_.raw))) {
95  ESP_LOGE(TAG, "Can't write I2C data.");
96  return false;
97  }
98  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,
101  ds1307_.reg.day, ONOFF(ds1307_.reg.ch), ds1307_.reg.rs, ONOFF(ds1307_.reg.sqwe), ONOFF(ds1307_.reg.out));
102  return true;
103 }
104 } // namespace ds1307
105 } // namespace esphome
uint8_t month
month; january=1 [1-12]
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:18
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than 2018) ...
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Definition: i2c.h:68
uint8_t minute
minutes after the hour [0-59]
void dump_config() override
Definition: ds1307.cpp:21
A more user-friendly version of struct tm from time.h.
uint8_t second
seconds after the minute [0-60]
union esphome::ds1307::DS1307Component::DS1307Reg ds1307_
float get_setup_priority() const override
Definition: ds1307.cpp:30
ESPTime utcnow()
Get the time without any time zone or DST corrections.
uint8_t day_of_week
day of the week; sunday=1 [1-7]
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:112
Definition: a4988.cpp:4
void synchronize_epoch_(uint32_t epoch)
Report a unix epoch as current time.
uint8_t day_of_month
day of the month [1-31]
struct esphome::ds1307::DS1307Component::DS1307Reg::@57 reg
uint8_t hour
hours since midnight [0-23]
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:109