ESPHome 2025.12.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 {
7namespace rx8130 {
8
9static const uint8_t RX8130_REG_SEC = 0x10;
10static const uint8_t RX8130_REG_MIN = 0x11;
11static const uint8_t RX8130_REG_HOUR = 0x12;
12static const uint8_t RX8130_REG_WDAY = 0x13;
13static const uint8_t RX8130_REG_MDAY = 0x14;
14static const uint8_t RX8130_REG_MONTH = 0x15;
15static const uint8_t RX8130_REG_YEAR = 0x16;
16static const uint8_t RX8130_REG_EXTEN = 0x1C;
17static const uint8_t RX8130_REG_FLAG = 0x1D;
18static const uint8_t RX8130_REG_CTRL0 = 0x1E;
19static const uint8_t RX8130_REG_CTRL1 = 0x1F;
20static const uint8_t RX8130_REG_DIG_OFFSET = 0x30;
21static const uint8_t RX8130_BIT_CTRL_STOP = 0x40;
22static const uint8_t RX8130_BAT_FLAGS = 0x30;
23static const uint8_t RX8130_CLEAR_FLAGS = 0x00;
24
25static const char *const TAG = "rx8130";
26
27constexpr uint8_t bcd2dec(uint8_t val) { return (val >> 4) * 10 + (val & 0x0f); }
28constexpr uint8_t dec2bcd(uint8_t val) { return ((val / 10) << 4) + (val % 10); }
29
31 // Set digital offset to disabled with no offset
32 if (this->write_register(RX8130_REG_DIG_OFFSET, &RX8130_CLEAR_FLAGS, 1) != i2c::ERROR_OK) {
33 this->mark_failed();
34 return;
35 }
36 // Disable wakeup timers
37 if (this->write_register(RX8130_REG_EXTEN, &RX8130_CLEAR_FLAGS, 1) != i2c::ERROR_OK) {
38 this->mark_failed();
39 return;
40 }
41 // Clear VLF flag in case there has been data loss
42 if (this->write_register(RX8130_REG_FLAG, &RX8130_CLEAR_FLAGS, 1) != i2c::ERROR_OK) {
43 this->mark_failed();
44 return;
45 }
46 // Clear test flag and disable interrupts
47 if (this->write_register(RX8130_REG_CTRL0, &RX8130_CLEAR_FLAGS, 1) != i2c::ERROR_OK) {
48 this->mark_failed();
49 return;
50 }
51 // Enable battery charging and switching
52 if (this->write_register(RX8130_REG_CTRL1, &RX8130_BAT_FLAGS, 1) != i2c::ERROR_OK) {
53 this->mark_failed();
54 return;
55 }
56 // Clear STOP bit
57 this->stop_(false);
58}
59
61
63 ESP_LOGCONFIG(TAG, "RX8130:");
64 LOG_I2C_DEVICE(this);
65 RealTimeClock::dump_config();
66}
67
69 uint8_t date[7];
70 if (this->read_register(RX8130_REG_SEC, date, 7) != i2c::ERROR_OK) {
71 this->status_set_warning(ESP_LOG_MSG_COMM_FAIL);
72 return;
73 }
74 ESPTime rtc_time{
75 .second = bcd2dec(date[0] & 0x7f),
76 .minute = bcd2dec(date[1] & 0x7f),
77 .hour = bcd2dec(date[2] & 0x3f),
78 .day_of_week = bcd2dec(date[3] & 0x7f),
79 .day_of_month = bcd2dec(date[4] & 0x3f),
80 .day_of_year = 1, // ignored by recalc_timestamp_utc(false)
81 .month = bcd2dec(date[5] & 0x1f),
82 .year = static_cast<uint16_t>(bcd2dec(date[6]) + 2000),
83 .is_dst = false, // not used
84 .timestamp = 0 // overwritten by recalc_timestamp_utc(false)
85 };
86 rtc_time.recalc_timestamp_utc(false);
87 if (!rtc_time.is_valid()) {
88 ESP_LOGE(TAG, "Invalid RTC time, not syncing to system clock.");
89 return;
90 }
91 ESP_LOGD(TAG, "Read UTC time: %04d-%02d-%02d %02d:%02d:%02d", rtc_time.year, rtc_time.month, rtc_time.day_of_month,
92 rtc_time.hour, rtc_time.minute, rtc_time.second);
94}
95
98 if (!now.is_valid()) {
99 ESP_LOGE(TAG, "Invalid system time, not syncing to RTC.");
100 return;
101 }
102 uint8_t buff[7];
103 buff[0] = dec2bcd(now.second);
104 buff[1] = dec2bcd(now.minute);
105 buff[2] = dec2bcd(now.hour);
106 buff[3] = dec2bcd(now.day_of_week);
107 buff[4] = dec2bcd(now.day_of_month);
108 buff[5] = dec2bcd(now.month);
109 buff[6] = dec2bcd(now.year % 100);
110 this->stop_(true);
111 if (this->write_register(RX8130_REG_SEC, buff, 7) != i2c::ERROR_OK) {
112 this->status_set_warning(ESP_LOG_MSG_COMM_FAIL);
113 } else {
114 ESP_LOGD(TAG, "Wrote UTC time: %04d-%02d-%02d %02d:%02d:%02d", now.year, now.month, now.day_of_month, now.hour,
116 }
117 this->stop_(false);
118}
119
120void RX8130Component::stop_(bool stop) {
121 const uint8_t data = stop ? RX8130_BIT_CTRL_STOP : RX8130_CLEAR_FLAGS;
122 if (this->write_register(RX8130_REG_CTRL0, &data, 1) != i2c::ERROR_OK) {
123 this->status_set_warning(ESP_LOG_MSG_COMM_FAIL);
124 }
125}
126
127} // namespace rx8130
128} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message=nullptr)
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:44
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:35
uint8_t size_t bool stop
Definition i2c.h:273
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[4]
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:33
constexpr uint8_t dec2bcd(uint8_t val)
Definition rx8130.cpp:28
constexpr uint8_t bcd2dec(uint8_t val)
Definition rx8130.cpp:27
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:15
uint8_t minute
minutes after the hour [0-59]
Definition time.h:21
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:158
uint8_t second
seconds after the minute [0-60]
Definition time.h:19
uint8_t hour
hours since midnight [0-23]
Definition time.h:23
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than 2018)
Definition time.h:61
uint8_t day_of_month
day of the month [1-31]
Definition time.h:27
uint16_t year
year
Definition time.h:33
uint8_t month
month; january=1 [1-12]
Definition time.h:31
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:25
uint16_t timestamp
Definition tt21100.cpp:2