ESPHome  2024.11.2
pcf8563.cpp
Go to the documentation of this file.
1 #include "pcf8563.h"
2 #include "esphome/core/log.h"
3 
4 // Datasheet:
5 // - https://nl.mouser.com/datasheet/2/302/PCF8563-1127619.pdf
6 
7 namespace esphome {
8 namespace pcf8563 {
9 
10 static const char *const TAG = "PCF8563";
11 
13  ESP_LOGCONFIG(TAG, "Setting up PCF8563...");
14  if (!this->read_rtc_()) {
15  this->mark_failed();
16  }
17 }
18 
20 
22  ESP_LOGCONFIG(TAG, "PCF8563:");
23  LOG_I2C_DEVICE(this);
24  if (this->is_failed()) {
25  ESP_LOGE(TAG, "Communication with PCF8563 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 (pcf8563_.reg.stop) {
37  ESP_LOGW(TAG, "RTC halted, not syncing to system clock.");
38  return;
39  }
40  ESPTime rtc_time{
41  .second = uint8_t(pcf8563_.reg.second + 10 * pcf8563_.reg.second_10),
42  .minute = uint8_t(pcf8563_.reg.minute + 10u * pcf8563_.reg.minute_10),
43  .hour = uint8_t(pcf8563_.reg.hour + 10u * pcf8563_.reg.hour_10),
44  .day_of_week = uint8_t(pcf8563_.reg.weekday),
45  .day_of_month = uint8_t(pcf8563_.reg.day + 10u * pcf8563_.reg.day_10),
46  .day_of_year = 1, // ignored by recalc_timestamp_utc(false)
47  .month = uint8_t(pcf8563_.reg.month + 10u * pcf8563_.reg.month_10),
48  .year = uint16_t(pcf8563_.reg.year + 10u * pcf8563_.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  pcf8563_.reg.year = (now.year - 2000) % 10;
67  pcf8563_.reg.year_10 = (now.year - 2000) / 10 % 10;
68  pcf8563_.reg.month = now.month % 10;
69  pcf8563_.reg.month_10 = now.month / 10;
73  pcf8563_.reg.hour = now.hour % 10;
74  pcf8563_.reg.hour_10 = now.hour / 10;
75  pcf8563_.reg.minute = now.minute % 10;
77  pcf8563_.reg.second = now.second % 10;
79  pcf8563_.reg.stop = false;
80 
81  this->write_rtc_();
82 }
83 
85  if (!this->read_bytes(0, this->pcf8563_.raw, sizeof(this->pcf8563_.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 STOP:%s CLKOUT:%0u", pcf8563_.reg.hour_10,
93 
94  return true;
95 }
96 
98  if (!this->write_bytes(0, this->pcf8563_.raw, sizeof(this->pcf8563_.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", pcf8563_.reg.hour_10,
106  return true;
107 }
108 } // namespace pcf8563
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
struct esphome::pcf8563::PCF8563Component::PCF8563Reg::@127 reg
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
union esphome::pcf8563::PCF8563Component::PCF8563Reg pcf8563_
float get_setup_priority() const override
Definition: pcf8563.cpp:30
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
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
uint8_t day_of_month
day of the month [1-31]
Definition: time.h:29
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:248