ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
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
8
9ESPHOME_LOG_TAG(TAG, "pcf85063");
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, "PCF85063:");
21 LOG_I2C_DEVICE(this);
22 RealTimeClock::dump_config();
23}
24
26 if (!this->read_rtc_()) {
27 return;
28 }
30 ESP_LOGW(TAG, "RTC halted, not syncing to system clock.");
31 return;
32 }
33 if (this->pcf85063_.reg.am_pm) {
34 ESP_LOGD(TAG, "RTC in 12-hour mode, the next sync switches it to 24-hour mode");
35 }
36 ESPTime rtc_time{
37 .second = uint8_t(pcf85063_.reg.second + 10 * pcf85063_.reg.second_10),
38 .minute = uint8_t(pcf85063_.reg.minute + 10u * pcf85063_.reg.minute_10),
39 .hour = decode_hour(this->pcf85063_.reg.hour, this->pcf85063_.reg.hour_10, this->pcf85063_.reg.am_pm),
40 // The chip counts weekdays 0-6 from Sunday, ESPTime counts 1-7
41 .day_of_week = uint8_t(this->pcf85063_.reg.weekday + 1),
42 .day_of_month = uint8_t(pcf85063_.reg.day + 10u * pcf85063_.reg.day_10),
43 .month = uint8_t(pcf85063_.reg.month + 10u * pcf85063_.reg.month_10),
44 .year = uint16_t(pcf85063_.reg.year + 10u * pcf85063_.reg.year_10 + 2000),
45 };
46 rtc_time.recalc_timestamp_utc(false);
47 if (!rtc_time.is_valid(/*check_day_of_week=*/true, /*check_day_of_year=*/false)) {
48 ESP_LOGE(TAG, "Invalid RTC time, not syncing to system clock.");
49 return;
50 }
52}
53
56 if (!now.is_valid()) {
57 ESP_LOGE(TAG, "Invalid system time, not syncing to RTC.");
58 return;
59 }
60 pcf85063_.reg.year = (now.year - 2000) % 10;
61 pcf85063_.reg.year_10 = (now.year - 2000) / 10 % 10;
67 pcf85063_.reg.hour = now.hour % 10;
73 pcf85063_.reg.osc_stop = false;
74 this->pcf85063_.reg.am_pm = false; // 24-hour mode
75
76 this->write_rtc_();
77}
78
80 if (!this->read_bytes(0, this->pcf85063_.raw, sizeof(this->pcf85063_.raw))) {
81 ESP_LOGE(TAG, "Can't read I2C data.");
82 return false;
83 }
84 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,
88
89 return true;
90}
91
93 if (!this->write_bytes(0, this->pcf85063_.raw, sizeof(this->pcf85063_.raw))) {
94 ESP_LOGE(TAG, "Can't write I2C data.");
95 return false;
96 }
97 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,
101 return true;
102}
103} // namespace esphome::pcf85063
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::pcf85063::PCF85063Component::PCF85063Reg pcf85063_
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, "pcf85063")
uint8_t decode_hour(uint8_t units, uint8_t tens, bool twelve_hour)
Hour 0-23 from the hours register digits.
Definition pcf85063.h:11
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::pcf85063::PCF85063Component::PCF85063Reg::@157 reg