ESPHome 2025.9.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 {
8namespace pcf8563 {
9
10static const char *const TAG = "PCF8563";
11
13 if (!this->read_rtc_()) {
14 this->mark_failed();
15 }
16}
17
19
21 ESP_LOGCONFIG(TAG, "PCF8563:");
22 LOG_I2C_DEVICE(this);
23 if (this->is_failed()) {
24 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
25 }
26 ESP_LOGCONFIG(TAG, " Timezone: '%s'", this->timezone_.c_str());
27}
28
30
32 if (!this->read_rtc_()) {
33 return;
34 }
35 if (pcf8563_.reg.stop) {
36 ESP_LOGW(TAG, "RTC halted, not syncing to system clock.");
37 return;
38 }
39 ESPTime rtc_time{
40 .second = uint8_t(pcf8563_.reg.second + 10 * pcf8563_.reg.second_10),
41 .minute = uint8_t(pcf8563_.reg.minute + 10u * pcf8563_.reg.minute_10),
42 .hour = uint8_t(pcf8563_.reg.hour + 10u * pcf8563_.reg.hour_10),
43 .day_of_week = uint8_t(pcf8563_.reg.weekday),
44 .day_of_month = uint8_t(pcf8563_.reg.day + 10u * pcf8563_.reg.day_10),
45 .day_of_year = 1, // ignored by recalc_timestamp_utc(false)
46 .month = uint8_t(pcf8563_.reg.month + 10u * pcf8563_.reg.month_10),
47 .year = uint16_t(pcf8563_.reg.year + 10u * pcf8563_.reg.year_10 + 2000),
48 .is_dst = false, // not used
49 .timestamp = 0, // overwritten by recalc_timestamp_utc(false)
50 };
51 rtc_time.recalc_timestamp_utc(false);
52 if (!rtc_time.is_valid()) {
53 ESP_LOGE(TAG, "Invalid RTC time, not syncing to system clock.");
54 return;
55 }
57}
58
61 if (!now.is_valid()) {
62 ESP_LOGE(TAG, "Invalid system time, not syncing to RTC.");
63 return;
64 }
65 pcf8563_.reg.year = (now.year - 2000) % 10;
66 pcf8563_.reg.year_10 = (now.year - 2000) / 10 % 10;
67 pcf8563_.reg.month = now.month % 10;
72 pcf8563_.reg.hour = now.hour % 10;
78 pcf8563_.reg.stop = false;
79
80 this->write_rtc_();
81}
82
84 if (!this->read_bytes(0, this->pcf8563_.raw, sizeof(this->pcf8563_.raw))) {
85 ESP_LOGE(TAG, "Can't read I2C data.");
86 return false;
87 }
88 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,
92
93 return true;
94}
95
97 if (!this->write_bytes(0, this->pcf8563_.raw, sizeof(this->pcf8563_.raw))) {
98 ESP_LOGE(TAG, "Can't write I2C data.");
99 return false;
100 }
101 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,
105 return true;
106}
107} // namespace pcf8563
108} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition i2c.h:252
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:216
float get_setup_priority() const override
Definition pcf8563.cpp:29
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.
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:50
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
A more user-friendly version of struct tm from time.h.
Definition time.h:15
uint8_t minute
minutes after the hour [0-59]
Definition time.h:21
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:164
uint8_t second
seconds after the minute [0-60]
Definition time.h:19
uint8_t hour
hours since midnight [0-23]
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:59
uint8_t day_of_month
day of the month [1-31]
Definition time.h:27
uint16_t year
year
Definition time.h:33
uint8_t month
month; january=1 [1-12]
Definition time.h:31
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:25
struct esphome::pcf8563::PCF8563Component::PCF8563Reg::@143 reg