PCF85063
The Pcf85063 component provides a driver for the Pcf85063 RTC chip.
API Reference
Header File
Classes
-
class Pcf85063 : public espp::BasePeripheral<uint8_t, true>
Class for the PCF85063 real-time clock.
The PCF85063 is a CMOS Real-Time Clock (RTC) and calendar optimized for low power consumption.
Example
// make the i2c 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}); // now make the pcf85063 espp::Pcf85063 rtc({.device_address = espp::Pcf85063::DEFAULT_ADDRESS, .write = std::bind_front(&espp::I2c::write, &i2c), .read = std::bind_front(&espp::I2c::read, &i2c)}); // set the time std::tm time; time.tm_sec = 0; time.tm_min = 0; time.tm_hour = 0; time.tm_mday = 1; time.tm_mon = 0; time.tm_year = 2023 - 1900; std::error_code ec; rtc.set_time(time, ec); if (ec) { logger.error("Failed to set time: {}", ec.message()); return; } logger.info("Time set to: {}.{:02d}.{:02d} {:02d}:{:02d}:{:02d}", time.tm_year + 1900, time.tm_mon + 1, time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec); // now start a task to read the time every second auto task_fn = [&](std::mutex &m, std::condition_variable &cv) -> bool { std::error_code ec; auto time = rtc.get_time(ec); if (ec) { logger.error("Failed to get time: {}", ec.message()); return true; // stop the task } logger.info("Time: {}.{:02d}.{:02d} {:02d}:{:02d}:{:02d}", time.tm_year + 1900, time.tm_mon + 1, time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec); // use the mutex and condition variable to perform an interruptible sleep std::unique_lock<std::mutex> lock(m); cv.wait_for(lock, 1s, [] { return false; }); // wait for 1 second or until notified // don't stop the task return false; }; auto task = espp::Task({.callback = task_fn, .task_config = { .name = "pcf85063", .stack_size_bytes = 4096, }, .log_level = espp::Logger::Verbosity::INFO}); task.start();
Note
The PCF85063 can be interfaced with I2C.
Public Types
-
enum class ClockOutFrequency : uint8_t
The different clock out frequencies that can be used.
Values:
-
enumerator FREQ_32768_HZ
32.768 kHz
-
enumerator FREQ_16384_HZ
16.384 kHz
-
enumerator FREQ_8192_HZ
8.192 kHz
-
enumerator FREQ_4096_HZ
4.096 kHz
-
enumerator FREQ_2048_HZ
2.048 kHz
-
enumerator FREQ_1024_HZ
1.024 kHz
-
enumerator FREQ_1_HZ
1 Hz
-
enumerator DISABLED
Disabled.
-
enumerator FREQ_32768_HZ
-
enum class TimerSourceClock : uint8_t
Timer source clock frequency.
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
-
enumerator FREQ_4096_HZ
-
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 Pcf85063(const Config &config)
Construct a new Pcf85063 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 set_time(const std::tm &time, std::error_code &ec)
Set the time on the device.
- 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 from the device.
- Parameters:
ec – The error code to set if an error occurs.
- Returns:
The time from the device.
-
inline bool set_minute_alarm(uint8_t minute, bool enable, std::error_code &ec)
Set the minute alarm.
- Parameters:
minute – The minute to set the alarm to.
enable – Whether to enable the alarm.
ec – The error code to set if an error occurs.
- Returns:
True if the alarm was set successfully, false otherwise.
-
inline bool set_hour_alarm(uint8_t hour, bool enable, std::error_code &ec)
Set the hour alarm.
- Parameters:
hour – The hour to set the alarm to.
enable – Whether to enable the alarm.
ec – The error code to set if an error occurs.
- Returns:
True if the alarm was set successfully, false otherwise.
-
inline bool set_day_alarm(uint8_t day, bool enable, std::error_code &ec)
Set the day alarm.
- Parameters:
day – The day to set the alarm to.
enable – Whether to enable the alarm.
ec – The error code to set if an error occurs.
- Returns:
True if the alarm was set successfully, false otherwise.
-
inline bool set_weekday_alarm(uint8_t weekday, bool enable, std::error_code &ec)
Set the weekday alarm.
- Parameters:
weekday – The weekday to set the alarm to.
enable – Whether to enable the alarm.
ec – The error code to set if an error occurs.
- Returns:
True if the alarm was set successfully, false otherwise.
-
inline bool is_alarm_triggered(std::error_code &ec)
Check if the alarm is triggered.
- Parameters:
ec – The error code to set if an error occurs.
- Returns:
True if the alarm is triggered, false otherwise.
-
inline bool clear_alarm(std::error_code &ec)
Clear the alarm flag.
- Parameters:
ec – The error code to set if an error occurs.
- Returns:
True if the alarm was cleared successfully, false otherwise.
-
inline bool enable_timer(bool enable, std::error_code &ec)
Enable the timer.
- Parameters:
enable – Whether to enable the timer.
ec – The error code to set if an error occurs.
- Returns:
True if the timer was enabled/disabled successfully, false otherwise.
-
inline bool set_timer_value(uint8_t value, std::error_code &ec)
Set the timer value.
- Parameters:
value – The value to set the timer to.
ec – The error code to set if an error occurs.
- Returns:
True if the timer was set successfully, false otherwise.
-
inline bool set_timer_clock(TimerSourceClock freq, std::error_code &ec)
Set the timer clock frequency.
- Parameters:
freq – The frequency to set the timer clock to.
ec – The error code to set if an error occurs.
- Returns:
True if the timer clock was set successfully, false otherwise.
-
inline bool set_clock_out_frequency(ClockOutFrequency freq, std::error_code &ec)
Set the clock out frequency.
- Parameters:
freq – The frequency to set the clock out to.
ec – The error code to set if an error occurs.
- Returns:
True if the clock out was set successfully, false otherwise.
-
inline bool probe(std::error_code &ec)
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 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
See also
See also
- Returns:
The verbosity level of the logger
-
inline void set_log_level(espp::Logger::Verbosity level)
Set the log level for the logger
See also
See also
- 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
See also
See also
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
See also
See also
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
See also
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 = 0x51
Default I2C address of the PCF85063.
-
struct Config
Configuration structure for the Pcf85063.
Public Members
-
uint8_t device_address = DEFAULT_ADDRESS
I2C address of the device.
-
write_fn write
Write function for the I2C bus.
-
read_fn read
Read function for the I2C bus.
-
bool auto_init = {true}
Whether to automatically initialize the device.
-
uint8_t device_address = DEFAULT_ADDRESS
-
enum class ClockOutFrequency : uint8_t