ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
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
7namespace esphome::pcf8563 {
8
9ESPHOME_LOG_TAG(TAG, "PCF8563");
10
12 if (!this->read_rtc_()) {
13 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
14 }
15}
16
18
20 ESP_LOGCONFIG(TAG, "PCF8563:");
21 LOG_I2C_DEVICE(this);
22 RealTimeClock::dump_config();
23}
24
26 if (!this->read_rtc_()) {
27 return;
28 }
29 if (pcf8563_.reg.stop) {
30 ESP_LOGW(TAG, "RTC halted, not syncing to system clock.");
31 return;
32 }
33 ESPTime rtc_time{
34 .second = uint8_t(pcf8563_.reg.second + 10 * pcf8563_.reg.second_10),
35 .minute = uint8_t(pcf8563_.reg.minute + 10u * pcf8563_.reg.minute_10),
36 .hour = uint8_t(pcf8563_.reg.hour + 10u * pcf8563_.reg.hour_10),
37 .day_of_week = uint8_t(pcf8563_.reg.weekday),
38 .day_of_month = uint8_t(pcf8563_.reg.day + 10u * pcf8563_.reg.day_10),
39 .month = uint8_t(pcf8563_.reg.month + 10u * pcf8563_.reg.month_10),
40 .year = uint16_t(pcf8563_.reg.year + 10u * pcf8563_.reg.year_10 + 2000),
41 };
42 rtc_time.recalc_timestamp_utc(false);
43 if (!rtc_time.is_valid(/*check_day_of_week=*/true, /*check_day_of_year=*/false)) {
44 ESP_LOGE(TAG, "Invalid RTC time, not syncing to system clock.");
45 return;
46 }
48}
49
52 if (!now.is_valid()) {
53 ESP_LOGE(TAG, "Invalid system time, not syncing to RTC.");
54 return;
55 }
56 pcf8563_.reg.year = (now.year - 2000) % 10;
57 pcf8563_.reg.year_10 = (now.year - 2000) / 10 % 10;
58 pcf8563_.reg.month = now.month % 10;
63 pcf8563_.reg.hour = now.hour % 10;
69 pcf8563_.reg.stop = false;
70
71 this->write_rtc_();
72}
73
75 if (!this->read_bytes(0, this->pcf8563_.raw, sizeof(this->pcf8563_.raw))) {
76 ESP_LOGE(TAG, "Can't read I2C data.");
77 return false;
78 }
79 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,
83
84 return true;
85}
86
88 if (!this->write_bytes(0, this->pcf8563_.raw, sizeof(this->pcf8563_.raw))) {
89 ESP_LOGE(TAG, "Can't write I2C data.");
90 return false;
91 }
92 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,
96 return true;
97}
98} // namespace esphome::pcf8563
void mark_failed()
Mark this component as failed.
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:298
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:249
union esphome::pcf8563::PCF8563Component::PCF8563Reg pcf8563_
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.
ESPHOME_LOG_TAG(TAG, "PCF8563")
A more user-friendly version of struct tm from time.h.
Definition time.h:23
uint8_t minute
minutes after the hour [0-59]
Definition time.h:32
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:280
uint8_t second
seconds after the minute [0-60]
Definition time.h:30
uint8_t hour
hours since midnight [0-23]
Definition time.h:34
bool is_valid(bool check_day_of_week=true, bool check_day_of_year=true) const
Check if this ESPTime is valid (year >= 2019 and the requested fields are in range).
Definition time.h:82
uint8_t day_of_month
day of the month [1-31]
Definition time.h:38
uint16_t year
year
Definition time.h:44
uint8_t month
month; january=1 [1-12]
Definition time.h:42
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:36
struct esphome::pcf8563::PCF8563Component::PCF8563Reg::@153 reg