ESPHome 2026.5.0b1
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
9static const char *const TAG = "PCF8563";
10
12 if (!this->read_rtc_()) {
13 this->mark_failed();
14 }
15}
16
18
20 ESP_LOGCONFIG(TAG, "PCF8563:");
21 LOG_I2C_DEVICE(this);
22 if (this->is_failed()) {
23 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
24 }
25 RealTimeClock::dump_config();
26}
27
29 if (!this->read_rtc_()) {
30 return;
31 }
32 if (pcf8563_.reg.stop) {
33 ESP_LOGW(TAG, "RTC halted, not syncing to system clock.");
34 return;
35 }
36 ESPTime rtc_time{
37 .second = uint8_t(pcf8563_.reg.second + 10 * pcf8563_.reg.second_10),
38 .minute = uint8_t(pcf8563_.reg.minute + 10u * pcf8563_.reg.minute_10),
39 .hour = uint8_t(pcf8563_.reg.hour + 10u * pcf8563_.reg.hour_10),
40 .day_of_week = uint8_t(pcf8563_.reg.weekday),
41 .day_of_month = uint8_t(pcf8563_.reg.day + 10u * pcf8563_.reg.day_10),
42 .month = uint8_t(pcf8563_.reg.month + 10u * pcf8563_.reg.month_10),
43 .year = uint16_t(pcf8563_.reg.year + 10u * pcf8563_.reg.year_10 + 2000),
44 };
45 rtc_time.recalc_timestamp_utc(false);
46 if (!rtc_time.is_valid(/*check_day_of_week=*/true, /*check_day_of_year=*/false)) {
47 ESP_LOGE(TAG, "Invalid RTC time, not syncing to system clock.");
48 return;
49 }
51}
52
55 if (!now.is_valid()) {
56 ESP_LOGE(TAG, "Invalid system time, not syncing to RTC.");
57 return;
58 }
59 pcf8563_.reg.year = (now.year - 2000) % 10;
60 pcf8563_.reg.year_10 = (now.year - 2000) / 10 % 10;
61 pcf8563_.reg.month = now.month % 10;
66 pcf8563_.reg.hour = now.hour % 10;
72 pcf8563_.reg.stop = false;
73
74 this->write_rtc_();
75}
76
78 if (!this->read_bytes(0, this->pcf8563_.raw, sizeof(this->pcf8563_.raw))) {
79 ESP_LOGE(TAG, "Can't read I2C data.");
80 return false;
81 }
82 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,
86
87 return true;
88}
89
91 if (!this->write_bytes(0, this->pcf8563_.raw, sizeof(this->pcf8563_.raw))) {
92 ESP_LOGE(TAG, "Can't write I2C data.");
93 return false;
94 }
95 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,
99 return true;
100}
101} // namespace esphome::pcf8563
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:284
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:251
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:217
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.
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:282
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::@146 reg