ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
rx8130.cpp
Go to the documentation of this file.
1#include "rx8130.h"
2#include "esphome/core/log.h"
3
4// https://download.epsondevice.com/td/pdf/app/RX8130CE_en.pdf
5
6namespace esphome::rx8130 {
7
8static const uint8_t RX8130_REG_SEC = 0x10;
9static const uint8_t RX8130_REG_MIN = 0x11;
10static const uint8_t RX8130_REG_HOUR = 0x12;
11static const uint8_t RX8130_REG_WDAY = 0x13;
12static const uint8_t RX8130_REG_MDAY = 0x14;
13static const uint8_t RX8130_REG_MONTH = 0x15;
14static const uint8_t RX8130_REG_YEAR = 0x16;
15static const uint8_t RX8130_REG_EXTEN = 0x1C;
16static const uint8_t RX8130_REG_FLAG = 0x1D;
17static const uint8_t RX8130_REG_CTRL0 = 0x1E;
18static const uint8_t RX8130_REG_CTRL1 = 0x1F;
19static const uint8_t RX8130_REG_DIG_OFFSET = 0x30;
20static const uint8_t RX8130_BIT_CTRL_STOP = 0x40;
21static const uint8_t RX8130_BAT_FLAGS = 0x30;
22static const uint8_t RX8130_CLEAR_FLAGS = 0x00;
23
24ESPHOME_LOG_TAG(TAG, "rx8130");
25
27 // Set digital offset to disabled with no offset
28 if (this->write_register(RX8130_REG_DIG_OFFSET, &RX8130_CLEAR_FLAGS, 1) != i2c::ERROR_OK) {
29 this->mark_failed();
30 return;
31 }
32 // Disable wakeup timers
33 if (this->write_register(RX8130_REG_EXTEN, &RX8130_CLEAR_FLAGS, 1) != i2c::ERROR_OK) {
34 this->mark_failed();
35 return;
36 }
37 // Clear VLF flag in case there has been data loss
38 if (this->write_register(RX8130_REG_FLAG, &RX8130_CLEAR_FLAGS, 1) != i2c::ERROR_OK) {
39 this->mark_failed();
40 return;
41 }
42 // Clear test flag and disable interrupts
43 if (this->write_register(RX8130_REG_CTRL0, &RX8130_CLEAR_FLAGS, 1) != i2c::ERROR_OK) {
44 this->mark_failed();
45 return;
46 }
47 // Enable battery charging and switching
48 if (this->write_register(RX8130_REG_CTRL1, &RX8130_BAT_FLAGS, 1) != i2c::ERROR_OK) {
49 this->mark_failed();
50 return;
51 }
52 // Clear STOP bit
53 this->stop_(false);
54}
55
57
59 ESP_LOGCONFIG(TAG, "RX8130:");
60 LOG_I2C_DEVICE(this);
61 RealTimeClock::dump_config();
62}
63
65 uint8_t date[7];
66 if (this->read_register(RX8130_REG_SEC, date, 7) != i2c::ERROR_OK) {
67 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
68 return;
69 }
70 ESPTime rtc_time = time::decode_bcd_calendar<time::EPSON_BCD_CALENDAR>(date);
71 if (!rtc_time.is_valid(/*check_day_of_week=*/true, /*check_day_of_year=*/false)) {
72 ESP_LOGE(TAG, "Invalid RTC time, not syncing to system clock.");
73 return;
74 }
75 ESP_LOGD(TAG, "Read UTC time: %04d-%02d-%02d %02d:%02d:%02d", rtc_time.year, rtc_time.month, rtc_time.day_of_month,
76 rtc_time.hour, rtc_time.minute, rtc_time.second);
78}
79
82 if (!now.is_valid()) {
83 ESP_LOGE(TAG, "Invalid system time, not syncing to RTC.");
84 return;
85 }
86 uint8_t buff[7];
87 time::encode_bcd_calendar<time::EPSON_BCD_CALENDAR>(now, buff);
88 this->stop_(true);
89 if (this->write_register(RX8130_REG_SEC, buff, 7) != i2c::ERROR_OK) {
90 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
91 } else {
92 ESP_LOGD(TAG, "Wrote UTC time: %04d-%02d-%02d %02d:%02d:%02d", now.year, now.month, now.day_of_month, now.hour,
94 }
95 this->stop_(false);
96}
97
98void RX8130Component::stop_(bool stop) {
99 const uint8_t data = stop ? RX8130_BIT_CTRL_STOP : RX8130_CLEAR_FLAGS;
100 if (this->write_register(RX8130_REG_CTRL0, &data, 1) != i2c::ERROR_OK) {
101 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
102 }
103}
104
105} // namespace esphome::rx8130
void mark_failed()
Mark this component as failed.
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:37
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:28
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.
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
ESPHOME_LOG_TAG(TAG, "rx8130")
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
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
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition time.h:48
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