ESPHome  2024.10.2
pcf85063.cpp
Go to the documentation of this file.
1 #include "pcf85063.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 pcf85063 {
9 
10 static const char *const TAG = "pcf85063";
11 
13  ESP_LOGCONFIG(TAG, "Setting up PCF85063...");
14  if (!this->read_rtc_()) {
15  this->mark_failed();
16  }
17 }
18 
20 
22  ESP_LOGCONFIG(TAG, "PCF85063:");
23  LOG_I2C_DEVICE(this);
24  if (this->is_failed()) {
25  ESP_LOGE(TAG, "Communication with PCF85063 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 (pcf85063_.reg.osc_stop) {
37  ESP_LOGW(TAG, "RTC halted, not syncing to system clock.");
38  return;
39  }
40  ESPTime rtc_time{
41  .second = uint8_t(pcf85063_.reg.second + 10 * pcf85063_.reg.second_10),
42  .minute = uint8_t(pcf85063_.reg.minute + 10u * pcf85063_.reg.minute_10),
43  .hour = uint8_t(pcf85063_.reg.hour + 10u * pcf85063_.reg.hour_10),
44  .day_of_week = uint8_t(pcf85063_.reg.weekday),
45  .day_of_month = uint8_t(pcf85063_.reg.day + 10u * pcf85063_.reg.day_10),
46  .day_of_year = 1, // ignored by recalc_timestamp_utc(false)
47  .month = uint8_t(pcf85063_.reg.month + 10u * pcf85063_.reg.month_10),
48  .year = uint16_t(pcf85063_.reg.year + 10u * pcf85063_.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  pcf85063_.reg.year = (now.year - 2000) % 10;
67  pcf85063_.reg.year_10 = (now.year - 2000) / 10 % 10;
68  pcf85063_.reg.month = now.month % 10;
73  pcf85063_.reg.hour = now.hour % 10;
74  pcf85063_.reg.hour_10 = now.hour / 10;
75  pcf85063_.reg.minute = now.minute % 10;
77  pcf85063_.reg.second = now.second % 10;
79  pcf85063_.reg.osc_stop = false;
80 
81  this->write_rtc_();
82 }
83 
85  if (!this->read_bytes(0, this->pcf85063_.raw, sizeof(this->pcf85063_.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 OSC:%s CLKOUT:%0u", pcf85063_.reg.hour_10,
93 
94  return true;
95 }
96 
98  if (!this->write_bytes(0, this->pcf85063_.raw, sizeof(this->pcf85063_.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 OSC:%s CLKOUT:%0u", pcf85063_.reg.hour_10,
106  return true;
107 }
108 } // namespace pcf85063
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:17
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
uint8_t second
seconds after the minute [0-60]
Definition: time.h:21
uint8_t minute
minutes after the hour [0-59]
Definition: time.h:23
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than 2018) ...
Definition: time.h:61
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition: time.h:27
ESPTime utcnow()
Get the time without any time zone or DST corrections.
uint16_t year
year
Definition: time.h:35
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
float get_setup_priority() const override
Definition: pcf85063.cpp:30
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:33
uint8_t hour
hours since midnight [0-23]
Definition: time.h:25
struct esphome::pcf85063::PCF85063Component::PCF85063Reg::@126 reg
uint8_t day_of_month
day of the month [1-31]
Definition: time.h:29
union esphome::pcf85063::PCF85063Component::PCF85063Reg pcf85063_
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:248