ESPHome 2026.5.0-dev
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
7namespace esphome {
8namespace pcf85063 {
9
10static const char *const TAG = "pcf85063";
11
13 if (!this->read_rtc_()) {
14 this->mark_failed();
15 }
16}
17
19
21 ESP_LOGCONFIG(TAG, "PCF85063:");
22 LOG_I2C_DEVICE(this);
23 if (this->is_failed()) {
24 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
25 }
26 RealTimeClock::dump_config();
27}
28
30 if (!this->read_rtc_()) {
31 return;
32 }
34 ESP_LOGW(TAG, "RTC halted, not syncing to system clock.");
35 return;
36 }
37 ESPTime rtc_time{
38 .second = uint8_t(pcf85063_.reg.second + 10 * pcf85063_.reg.second_10),
39 .minute = uint8_t(pcf85063_.reg.minute + 10u * pcf85063_.reg.minute_10),
40 .hour = uint8_t(pcf85063_.reg.hour + 10u * pcf85063_.reg.hour_10),
41 .day_of_week = uint8_t(pcf85063_.reg.weekday),
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
75 this->write_rtc_();
76}
77
79 if (!this->read_bytes(0, this->pcf85063_.raw, sizeof(this->pcf85063_.raw))) {
80 ESP_LOGE(TAG, "Can't read I2C data.");
81 return false;
82 }
83 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,
87
88 return true;
89}
90
92 if (!this->write_bytes(0, this->pcf85063_.raw, sizeof(this->pcf85063_.raw))) {
93 ESP_LOGE(TAG, "Can't write I2C data.");
94 return false;
95 }
96 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,
100 return true;
101}
102} // namespace pcf85063
103} // namespace esphome
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::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.
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: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::pcf85063::PCF85063Component::PCF85063Reg::@145 reg