ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
rx8025t.cpp
Go to the documentation of this file.
1#include "rx8025t.h"
2#include "esphome/core/log.h"
3
4// Datasheet:
5// - https://support.epson.biz/td/api/doc_check.php?dl=app_RX8025T
6
7namespace esphome::rx8025t {
8
9static constexpr uint8_t RX8025T_REG_SEC = 0x00;
10static constexpr uint8_t RX8025T_REG_FLAG = 0x0E;
11static constexpr uint8_t RX8025T_FLAG_VDET = 0x01;
12static constexpr uint8_t RX8025T_FLAG_VLF = 0x02;
13
14ESPHOME_LOG_TAG(TAG, "rx8025t");
15
17 uint8_t flags;
18 if (!this->setup_check(this->read_flags_(&flags)))
19 return;
20
21 if (flags & RX8025T_FLAG_VLF) {
22 ESP_LOGW(TAG, "VLF flag is set - Loss of oscillator detected. Time may be invalid.");
23 }
24}
25
27
29 ESP_LOGCONFIG(TAG, "RX8025T:");
30 LOG_I2C_DEVICE(this);
32}
33
35 if (!this->read_byte(RX8025T_REG_FLAG, flags)) {
36 ESP_LOGE(TAG, "Can't read flag register.");
37 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
38 return false;
39 }
40 return true;
41}
42
44 uint8_t flags;
45 if (!this->read_flags_(&flags)) {
46 return;
47 }
48
49 uint8_t date[7];
50 if (!this->status_comm_result(this->read_bytes(RX8025T_REG_SEC, date, sizeof(date)))) {
51 ESP_LOGE(TAG, "Can't read I2C data.");
52 return;
53 }
54
55 if (flags & RX8025T_FLAG_VLF) {
56 ESP_LOGW(TAG, "VLF flag is set - time data may be invalid, not syncing to system clock.");
57 return;
58 }
59
60 ESPTime rtc_time = time::decode_bcd_calendar<time::EPSON_BCD_CALENDAR>(date);
61 if (!rtc_time.is_valid(/*check_day_of_week=*/true, /*check_day_of_year=*/false)) {
62 ESP_LOGE(TAG, "Invalid RTC time, not syncing to system clock.");
63 return;
64 }
65 ESP_LOGD(TAG, "Read UTC time: %04d-%02d-%02d %02d:%02d:%02d VDET:%s", rtc_time.year, rtc_time.month,
66 rtc_time.day_of_month, rtc_time.hour, rtc_time.minute, rtc_time.second, ONOFF(flags & RX8025T_FLAG_VDET));
67 this->synchronize_epoch_(rtc_time.timestamp);
68}
69
71 auto now = this->utcnow();
72 if (!now.is_valid()) {
73 ESP_LOGE(TAG, "Invalid system time, not syncing to RTC.");
74 return;
75 }
76
77 uint8_t buff[7];
78 time::encode_bcd_calendar<time::EPSON_BCD_CALENDAR>(now, buff);
79 if (!this->write_bytes(RX8025T_REG_SEC, buff, sizeof(buff))) {
80 ESP_LOGE(TAG, "Can't write I2C data.");
81 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
82 return;
83 }
84 ESP_LOGD(TAG, "Wrote UTC time: %04d-%02d-%02d %02d:%02d:%02d", now.year, now.month, now.day_of_month, now.hour,
86
87 // Clear VLF and VDET flags via read-modify-write of flag register
88 uint8_t flags;
89 if (!this->read_flags_(&flags)) {
90 return;
91 }
92 flags &= ~(RX8025T_FLAG_VLF | RX8025T_FLAG_VDET);
93 this->status_comm_result(this->write_byte(RX8025T_REG_FLAG, flags));
94}
95
96} // namespace esphome::rx8025t
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:236
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
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
bool read_flags_(uint8_t *flags)
Definition rx8025t.cpp:34
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.
uint16_t flags
ESPHOME_LOG_TAG(TAG, "rx8025t")
A more user-friendly version of struct tm from time.h.
Definition time.h:25
uint8_t minute
minutes after the hour [0-59]
Definition time.h:34
uint8_t second
seconds after the minute [0-60]
Definition time.h:32
uint8_t hour
hours since midnight [0-23]
Definition time.h:36
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition time.h:50
bool is_valid(bool check_day_of_week=true, bool check_day_of_year=true) const
Check if this ESPTime is valid (year >= internal::MIN_VALID_YEAR and the requested fields are in rang...
Definition time.h:84
uint8_t day_of_month
day of the month [1-31]
Definition time.h:40
uint16_t year
year
Definition time.h:46
uint8_t month
month; january=1 [1-12]
Definition time.h:44