ESPHome 2026.6.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
24static const char *const TAG = "rx8130";
25
26constexpr uint8_t bcd2dec(uint8_t val) { return (val >> 4) * 10 + (val & 0x0f); }
27constexpr uint8_t dec2bcd(uint8_t val) { return ((val / 10) << 4) + (val % 10); }
28
30 // Set digital offset to disabled with no offset
31 if (this->write_register(RX8130_REG_DIG_OFFSET, &RX8130_CLEAR_FLAGS, 1) != i2c::ERROR_OK) {
32 this->mark_failed();
33 return;
34 }
35 // Disable wakeup timers
36 if (this->write_register(RX8130_REG_EXTEN, &RX8130_CLEAR_FLAGS, 1) != i2c::ERROR_OK) {
37 this->mark_failed();
38 return;
39 }
40 // Clear VLF flag in case there has been data loss
41 if (this->write_register(RX8130_REG_FLAG, &RX8130_CLEAR_FLAGS, 1) != i2c::ERROR_OK) {
42 this->mark_failed();
43 return;
44 }
45 // Clear test flag and disable interrupts
46 if (this->write_register(RX8130_REG_CTRL0, &RX8130_CLEAR_FLAGS, 1) != i2c::ERROR_OK) {
47 this->mark_failed();
48 return;
49 }
50 // Enable battery charging and switching
51 if (this->write_register(RX8130_REG_CTRL1, &RX8130_BAT_FLAGS, 1) != i2c::ERROR_OK) {
52 this->mark_failed();
53 return;
54 }
55 // Clear STOP bit
56 this->stop_(false);
57}
58
60
62 ESP_LOGCONFIG(TAG, "RX8130:");
63 LOG_I2C_DEVICE(this);
64 RealTimeClock::dump_config();
65}
66
68 uint8_t date[7];
69 if (this->read_register(RX8130_REG_SEC, date, 7) != i2c::ERROR_OK) {
70 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
71 return;
72 }
73 ESPTime rtc_time{
74 .second = bcd2dec(date[0] & 0x7f),
75 .minute = bcd2dec(date[1] & 0x7f),
76 .hour = bcd2dec(date[2] & 0x3f),
77 .day_of_week = static_cast<uint8_t>((date[3] & 0x7f) ? __builtin_ctz(date[3] & 0x7f) + 1 : 1),
78 .day_of_month = bcd2dec(date[4] & 0x3f),
79 .month = bcd2dec(date[5] & 0x1f),
80 .year = static_cast<uint16_t>(bcd2dec(date[6]) + 2000),
81 };
82 rtc_time.recalc_timestamp_utc(false);
83 if (!rtc_time.is_valid(/*check_day_of_week=*/true, /*check_day_of_year=*/false)) {
84 ESP_LOGE(TAG, "Invalid RTC time, not syncing to system clock.");
85 return;
86 }
87 ESP_LOGD(TAG, "Read UTC time: %04d-%02d-%02d %02d:%02d:%02d", rtc_time.year, rtc_time.month, rtc_time.day_of_month,
88 rtc_time.hour, rtc_time.minute, rtc_time.second);
90}
91
94 if (!now.is_valid()) {
95 ESP_LOGE(TAG, "Invalid system time, not syncing to RTC.");
96 return;
97 }
98 uint8_t buff[7];
99 buff[0] = dec2bcd(now.second);
100 buff[1] = dec2bcd(now.minute);
101 buff[2] = dec2bcd(now.hour);
102 buff[3] = 1 << (now.day_of_week - 1);
103 buff[4] = dec2bcd(now.day_of_month);
104 buff[5] = dec2bcd(now.month);
105 buff[6] = dec2bcd(now.year % 100);
106 this->stop_(true);
107 if (this->write_register(RX8130_REG_SEC, buff, 7) != i2c::ERROR_OK) {
108 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
109 } else {
110 ESP_LOGD(TAG, "Wrote UTC time: %04d-%02d-%02d %02d:%02d:%02d", now.year, now.month, now.day_of_month, now.hour,
112 }
113 this->stop_(false);
114}
115
116void RX8130Component::stop_(bool stop) {
117 const uint8_t data = stop ? RX8130_BIT_CTRL_STOP : RX8130_CLEAR_FLAGS;
118 if (this->write_register(RX8130_REG_CTRL0, &data, 1) != i2c::ERROR_OK) {
119 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
120 }
121}
122
123} // 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:34
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:25
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.
mopeka_std_values val[3]
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
constexpr uint8_t dec2bcd(uint8_t val)
Definition rx8130.cpp:27
constexpr uint8_t bcd2dec(uint8_t val)
Definition rx8130.cpp:26
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