RX8130CE

The Rx8130ce component provides a driver for the RX8130CE Real-Time Clock chip from EPSON.

This component supports both std::tm based APIs for portability across different RTC chips, and device-specific APIs for accessing advanced features like temperature sensing, timer functionality, and clock output.

API Reference

Header File

Classes

template<bool UseAddress = true>
class Rx8130ce : public espp::BasePeripheral<uint8_t, true>

The RX8130CE Real-Time Clock driver.

This driver is for the RX8130CE RTC chip from EPSON.

Example

  // make the I2C that we'll use to communicate
  static constexpr auto i2c_port = I2C_NUM_0;
  static constexpr auto i2c_clock_speed = CONFIG_EXAMPLE_I2C_CLOCK_SPEED_HZ;
  static constexpr gpio_num_t i2c_sda = (gpio_num_t)CONFIG_EXAMPLE_I2C_SDA_GPIO;
  static constexpr gpio_num_t i2c_scl = (gpio_num_t)CONFIG_EXAMPLE_I2C_SCL_GPIO;
  espp::I2c i2c({.port = i2c_port,
                 .sda_io_num = i2c_sda,
                 .scl_io_num = i2c_scl,
                 .sda_pullup_en = GPIO_PULLUP_ENABLE,
                 .scl_pullup_en = GPIO_PULLUP_ENABLE,
                 .clk_speed = i2c_clock_speed});

  using Rtc = espp::Rx8130ce<>;

  // Configure the RX8130CE
  Rtc::Config config{.device_address = Rtc::DEFAULT_ADDRESS,
                     .write = std::bind_front(&espp::I2c::write, &i2c),
                     .read = std::bind_front(&espp::I2c::read, &i2c),
                     .log_level = espp::Logger::Verbosity::INFO};

  // Create RTC instance
  Rtc rtc(config);

  // Test std::tm based API
  logger.info("Testing std::tm based API");

  // Set time using std::tm
  std::tm time = {};
  time.tm_year = 124; // 2024 - 1900
  time.tm_mon = 0;    // January (0-based)
  time.tm_mday = 15;  // 15th
  time.tm_hour = 14;  // 2 PM
  time.tm_min = 30;
  time.tm_sec = 45;
  time.tm_wday = 1; // Monday

  std::error_code ec;
  if (rtc.set_time(time, ec)) {
    logger.info("Time set successfully");
  } else {
    logger.error("Failed to set time: {}", ec.message());
  }

  // Get time using std::tm
  auto current_time = rtc.get_time(ec);
  if (!ec) {
    logger.info("Current time: {}-{:02d}-{:02d} {:02d}:{:02d}:{:02d} (wday: {})",
                current_time.tm_year + 1900, current_time.tm_mon + 1, current_time.tm_mday,
                current_time.tm_hour, current_time.tm_min, current_time.tm_sec,
                current_time.tm_wday);
  } else {
    logger.error("Failed to get time: {}", ec.message());
  }

  // Test device-specific API
  logger.info("Testing device-specific API");

  auto dt = rtc.get_date_time(ec);
  if (!ec) {
    logger.info("Device format - Date: {}-{:02d}-{:02d} (wday: {}), Time: {:02d}:{:02d}:{:02d}",
                dt.date.year, dt.date.month, dt.date.day, dt.date.weekday, dt.time.hour,
                dt.time.minute, dt.time.second);
  }

  // Test alarm functionality
  logger.info("Testing alarm functionality");

  std::tm alarm_time = current_time;
  alarm_time.tm_sec += 10; // Alarm in 10 seconds
  mktime(&alarm_time);     // Normalize the time

  if (rtc.set_alarm(alarm_time, false, ec)) {
    logger.info("Alarm set for {:02d}:{:02d}:{:02d}", alarm_time.tm_hour, alarm_time.tm_min,
                alarm_time.tm_sec);
  }

  // Test timer functionality
  logger.info("Testing timer functionality");

  // Set a 5-second timer
  if (rtc.set_timer(5, Rtc::TimerClockSource::FREQ_1_HZ, ec)) {
    logger.info("Timer set for 5 seconds");
  }

  // Configure clock output
  logger.info("Configuring clock output to 1Hz");
  rtc.set_clock_output(Rtc::ClockOutFrequency::FREQ_1_HZ, ec);

  // Main loop - monitor alarms and timer
  logger.info("Monitoring RTC events...");

  while (true) {
    std::this_thread::sleep_for(1s);

    // Get current time
    current_time = rtc.get_time(ec);
    if (!ec) {
      logger.info("Time: {:02d}:{:02d}:{:02d}", current_time.tm_hour, current_time.tm_min,
                  current_time.tm_sec);
    }

    // Check alarm
    if (rtc.is_alarm_triggered(ec) && !ec) {
      logger.info("🚨 ALARM TRIGGERED!");
      rtc.clear_alarm_flag(ec);
    }

    // Check timer
    if (rtc.is_timer_expired(ec) && !ec) {
      logger.info("⏰ TIMER EXPIRED!");
      rtc.clear_timer_flag(ec);

      // Restart timer for another 5 seconds
      rtc.set_timer(5, Rtc::TimerClockSource::FREQ_1_HZ, ec);
      logger.info("Timer restarted");
    }
  }

Note

The RX8130CE is a temperature compensated crystal oscillator (TCXO) RTC with I2C interface and various timing/alarm features.

Template Parameters:

UseAddress – Whether to use I2C addressing (default: true)

Public Types

enum class ClockOutFrequency : uint8_t

Clock output frequencies available on the RX8130CE.

Values:

enumerator FREQ_32768_HZ

32.768 kHz

enumerator FREQ_1024_HZ

1.024 kHz

enumerator FREQ_1_HZ

1 Hz

enumerator DISABLED

Clock output disabled.

enum class TimerClockSource : uint8_t

Timer clock source frequencies.

Values:

enumerator FREQ_4096_HZ

4.096 kHz

enumerator FREQ_64_HZ

64 Hz

enumerator FREQ_1_HZ

1 Hz

enumerator FREQ_1_60_HZ

1/60 Hz (1 minute)

enumerator FREQ_1_3600_HZ

1/3600 Hz (1 hour)

typedef std::function<bool(uint8_t)> probe_fn

Function to probe the peripheral

Param address:

The address to probe

Return:

True if the peripheral is found at the given address

Public Functions

inline explicit Rx8130ce(const Config &config)

Construct a new Rx8130ce object.

Parameters:

config – The configuration for the device

inline bool init(std::error_code &ec)

Initialize the device.

Parameters:

ec – The error code to set if an error occurs

Returns:

True if the device was initialized successfully, false otherwise

inline bool reset(std::error_code &ec)

Reset the RX8130CE device.

Parameters:

ec – The error code to set if an error occurs

Returns:

True if the device was reset successfully, false otherwise

inline bool set_time(const std::tm &time, std::error_code &ec)

Set the time using std::tm structure.

Parameters:
  • time – The time to set

  • ec – The error code to set if an error occurs

Returns:

True if the time was set successfully, false otherwise

inline std::tm get_time(std::error_code &ec)

Get the time using std::tm structure.

Parameters:

ec – The error code to set if an error occurs

Returns:

The time from the device

inline DateTime get_date_time(std::error_code &ec)

Get the date and time using device-specific structures.

Parameters:

ec – The error code to set if an error occurs

Returns:

The date and time

inline void set_date_time(const DateTime &dt, std::error_code &ec)

Set the date and time using device-specific structures.

Parameters:
  • dt – The date and time to set

  • ec – The error code to set if an error occurs

inline bool get_voltage_loss_flag(std::error_code &ec)

Check if the voltage loss flag is set.

Parameters:

ec – The error code to set if an error occurs

Returns:

True if a voltage loss has occurred, false otherwise

inline bool clear_voltage_loss_flag(std::error_code &ec)

Clear the voltage loss flag.

Parameters:

ec – The error code to set if an error occurs

Returns:

True if the flag was cleared successfully, false otherwise

inline bool set_alarm(const std::tm &alarm_time, bool is_week_day_target, std::error_code &ec)

Set an alarm using std::tm structure.

Note

If is_week_day_target is true, the alarm will trigger on the specified day of the month. If false, it will trigger on the specified day of the week.

Note

This will match exactly the minute, hour, and day/week specified because it will set all the alarm bits to 0 (enabled for exact match).

Parameters:
  • alarm_time – The time to trigger the alarm

  • is_week_day_target – True to use week day as target of alarm function, false to use month day

  • ec – The error code to set if an error occurs

Returns:

True if the alarm was set successfully, false otherwise

inline bool set_alarm_interrupt_enabled(bool enabled, std::error_code &ec)

Enable or disable the alarm interrupt.

Parameters:
  • enabled – True to enable, false to disable

  • ec – The error code to set if an error occurs

Returns:

True if the operation was successful, false otherwise

inline bool disable_alarm(std::error_code &ec)

Disable the alarm.

Parameters:

ec – The error code to set if an error occurs

Returns:

True if the alarm was disabled successfully, false otherwise

inline bool is_alarm_triggered(std::error_code &ec)

Check if the alarm flag is set.

Parameters:

ec – The error code to set if an error occurs

Returns:

True if the alarm is triggered, false otherwise

inline bool clear_alarm_flag(std::error_code &ec)

Clear the alarm flag.

Parameters:

ec – The error code to set if an error occurs

Returns:

True if the flag was cleared successfully, false otherwise

inline bool set_timer(uint16_t value, TimerClockSource clock_source, std::error_code &ec)

Set the countdown timer.

Parameters:
  • valueTimer value (0-4095)

  • clock_sourceTimer clock source

  • ec – The error code to set if an error occurs

Returns:

True if the timer was set successfully, false otherwise

inline bool stop_timer(std::error_code &ec)

Stop the timer.

Parameters:

ec – The error code to set if an error occurs

Returns:

True if the timer was stopped successfully, false otherwise

inline bool set_timer_interrupt_enabled(bool enabled, std::error_code &ec)

Enable or disable the timer interrupt.

Parameters:
  • enabled – True to enable, false to disable

  • ec – The error code to set if an error occurs

Returns:

True if the operation was successful, false otherwise

inline bool is_timer_expired(std::error_code &ec)

Check if the timer flag is set.

Parameters:

ec – The error code to set if an error occurs

Returns:

True if the timer has expired, false otherwise

inline bool clear_timer_flag(std::error_code &ec)

Clear the timer flag.

Parameters:

ec – The error code to set if an error occurs

Returns:

True if the flag was cleared successfully, false otherwise

inline bool set_clock_output(ClockOutFrequency frequency, std::error_code &ec)

Configure the clock output.

Parameters:
  • frequency – The output frequency

  • ec – The error code to set if an error occurs

Returns:

True if the clock output was configured successfully, false otherwise

inline bool probe(std::error_code &ec) const

Probe the peripheral

Note

This function is thread safe

Note

If the probe function is not set, this function will return false and set the error code to operation_not_supported

Note

This function is only available if UseAddress is true

Parameters:

ec – The error code to set if there is an error

Returns:

True if the peripheral is found

inline void set_address(uint8_t address)

Set the address of the peripheral

Note

This function is thread safe

Note

This function is only available if UseAddress is true

Parameters:

address – The address of the peripheral

inline void set_probe(const probe_fn &probe)

Set the probe function

Note

This function is thread safe

Note

This should rarely be used, as the probe function is usually set in the constructor. If you need to change the probe function, consider using the set_config function instead.

Note

This function is only available if UseAddress is true

Parameters:

probe – The probe function

inline void set_write(const write_fn &write)

Set the write function

Note

This function is thread safe

Note

This should rarely be used, as the write function is usually set in the constructor. If you need to change the write function, consider using the set_config function instead.

Parameters:

write – The write function

inline void set_read(const read_fn &read)

Set the read function

Note

This function is thread safe

Note

This should rarely be used, as the read function is usually set in the constructor. If you need to change the read function, consider using the set_config function instead.

Parameters:

read – The read function

inline void set_read_register(const read_register_fn &read_register)

Set the read register function

Note

This function is thread safe

Note

This should rarely be used, as the read register function is usually set in the constructor. If you need to change the read register function, consider using the set_config function instead.

Parameters:

read_register – The read register function

inline void set_write_then_read(const write_then_read_fn &write_then_read)

Set the write then read function

Note

This function is thread safe

Note

This should rarely be used, as the write then read function is usually set in the constructor. If you need to change the write then

Parameters:

write_then_read – The write then read function

inline void set_separate_write_then_read_delay(const std::chrono::milliseconds &delay)

Set the delay between the write and read operations in write_then_read

Note

This function is thread safe

Note

This should rarely be used, as the delay is usually set in the constructor. If you need to change the delay, consider using the set_config function instead.

Note

This delay is only used if the write_then_read function is not set to a custom function and the write and read functions are separate functions.

Parameters:

delay – The delay between the write and read operations in write_then_read

inline void set_config(const Config &config)

Set the configuration for the peripheral

Note

This function is thread safe

Note

The configuration should normally be set in the constructor, but this function can be used to change the configuration after the peripheral has been created - for instance if the peripheral could be found on different communications buses.

Parameters:

config – The configuration for the peripheral

inline void set_config(Config &&config)

Set the configuration for the peripheral

Note

This function is thread safe

Note

The configuration should normally be set in the constructor, but this function can be used to change the configuration after the peripheral has been created - for instance if the peripheral could be found on different communications buses.

Parameters:

config – The configuration for the peripheral

inline const Config &config() const

Get the configuration for the peripheral

Returns:

The configuration for the peripheral

inline uint8_t address() const

Get the address of the peripheral

Returns:

The address of the peripheral

inline const std::string &get_name() const

Get the name of the component

Note

This is the tag of the logger

Returns:

A const reference to the name of the component

inline void set_log_tag(const std::string_view &tag)

Set the tag for the logger

Parameters:

tag – The tag to use for the logger

inline espp::Logger::Verbosity get_log_level() const

Get the log level for the logger

Returns:

The verbosity level of the logger

inline void set_log_level(espp::Logger::Verbosity level)

Set the log level for the logger

Parameters:

level – The verbosity level to use for the logger

inline void set_log_verbosity(espp::Logger::Verbosity level)

Set the log verbosity for the logger

See also

set_log_level

Note

This is a convenience method that calls set_log_level

Parameters:

level – The verbosity level to use for the logger

inline espp::Logger::Verbosity get_log_verbosity() const

Get the log verbosity for the logger

See also

get_log_level

Note

This is a convenience method that calls get_log_level

Returns:

The verbosity level of the logger

inline void set_log_rate_limit(std::chrono::duration<float> rate_limit)

Set the rate limit for the logger

Note

Only calls to the logger that have _rate_limit suffix will be rate limited

Parameters:

rate_limit – The rate limit to use for the logger

Public Static Attributes

static constexpr uint8_t DEFAULT_ADDRESS = 0x32

Default I2C address of the RX8130CE.

struct Config

Configuration structure for the Rx8130ce.

Public Members

uint8_t device_address = DEFAULT_ADDRESS

I2C address of the device.

BasePeripheral<uint8_t, UseAddress>::write_fn write

Write function for the I2C bus.

BasePeripheral<uint8_t, UseAddress>::read_fn read

Read function for the I2C bus.

bool auto_init = {true}

Whether to automatically initialize the device.

Logger::Verbosity log_level = {Logger::Verbosity::WARN}

Log level for the device.

struct Date

The date structure (device-specific format)

Public Members

uint16_t year

The year (full 4-digit year)

uint8_t month

The month (1-12)

uint8_t weekday

The day of the week (0-6, Sunday=0)

uint8_t day

The day of the month (1-31)

struct DateTime

The date and time structure (device-specific format)

Public Members

Date date

The date.

Time time

The time.

struct Time

The time structure (device-specific format)

Public Members

uint8_t hour

The hour (0-23)

uint8_t minute

The minute (0-59)

uint8_t second

The second (0-59)