Skip to content

Commit ea2642f

Browse files
authored
feat(pcf85063): Add espp::Pcf85063 peripheral for PCF85063 RTC (#476)
* feat(pcf85063): Add `espp::Pcf85063` peripheral for PCF85063 RTC * add image to readme
1 parent fa90840 commit ea2642f

26 files changed

+665
-32
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ jobs:
142142
target: esp32s3
143143
- path: 'components/nvs/example'
144144
target: esp32s3
145+
- path: 'components/pcf85063/example'
146+
target: esp32s3
145147
- path: 'components/pid/example'
146148
target: esp32
147149
- path: 'components/qmi8658/example'

.github/workflows/upload_components.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ jobs:
8989
components/neopixel
9090
components/nvs
9191
components/pid
92+
components/pcf85063
9293
components/qmi8658
9394
components/qtpy
9495
components/qwiicnes

components/bm8563/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
idf_component_register(
22
INCLUDE_DIRS "include"
3-
REQUIRES "base_peripheral"
3+
REQUIRES "base_peripheral" "utils"
44
)

components/bm8563/example/main/bm8563_example.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ extern "C" void app_main(void) {
2222
});
2323
// now make the bm8563
2424
auto bm8563 = espp::Bm8563({
25-
.write = std::bind(&espp::I2c::write, &i2c, std::placeholders::_1, std::placeholders::_2,
26-
std::placeholders::_3),
27-
.write_then_read =
28-
std::bind(&espp::I2c::write_read, &i2c, std::placeholders::_1, std::placeholders::_2,
29-
std::placeholders::_3, std::placeholders::_4, std::placeholders::_5),
25+
.write = std::bind_front(&espp::I2c::write, &i2c),
26+
.write_then_read = std::bind_front(&espp::I2c::write_read, &i2c),
3027
});
3128

3229
std::error_code ec;

components/bm8563/idf_component.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ dependencies:
1717
idf:
1818
version: '>=5.0'
1919
espp/base_peripheral: '>=1.0'
20+
espp/utils: '>=1.0'

components/bm8563/include/bm8563.hpp

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <functional>
44

55
#include "base_peripheral.hpp"
6+
#include "espp_chrono.hpp"
67

78
namespace espp {
89
/// @brief The BM8563 RTC driver.
@@ -75,8 +76,8 @@ class Bm8563 : public BasePeripheral<> {
7576

7677
/// @brief The configuration structure.
7778
struct Config {
78-
BasePeripheral::write_fn write; ///< The I2C write function.
79-
BasePeripheral::write_then_read_fn write_then_read; ///< The I2C write then read function.
79+
BasePeripheral<>::write_fn write; ///< The I2C write function.
80+
BasePeripheral<>::write_then_read_fn write_then_read; ///< The I2C write then read function.
8081
espp::Logger::Verbosity log_level{
8182
espp::Logger::Verbosity::WARN}; ///< Log verbosity for the input driver.
8283
};
@@ -95,16 +96,6 @@ class Bm8563 : public BasePeripheral<> {
9596
}
9697
}
9798

98-
/// @brief Convert a BCD value to a byte.
99-
/// @param value The BCD value.
100-
/// @return The byte value.
101-
static uint8_t bcd2byte(uint8_t value) { return (value >> 4) * 10 + (value & 0x0f); }
102-
103-
/// @brief Convert a byte value to BCD.
104-
/// @param value The byte value.
105-
/// @return The BCD value.
106-
static uint8_t byte2bcd(uint8_t value) { return ((value / 10) << 4) + value % 10; }
107-
10899
/// @brief Get the date and time.
109100
/// @param ec The error code.
110101
/// @return The date and time.
@@ -143,10 +134,10 @@ class Bm8563 : public BasePeripheral<> {
143134
}
144135
Date d;
145136
int base_year = (data[2] & CENTURY_BIT) ? 1900 : 2000;
146-
d.year = base_year + bcd2byte(data[3] & 0xff);
147-
d.month = bcd2byte(data[2] & 0x1f);
148-
d.weekday = bcd2byte(data[1] & 0x07);
149-
d.day = bcd2byte(data[0] & 0x3f);
137+
d.year = base_year + bcd_to_decimal(data[3] & 0xff);
138+
d.month = bcd_to_decimal(data[2] & 0x1f);
139+
d.weekday = bcd_to_decimal(data[1] & 0x07);
140+
d.day = bcd_to_decimal(data[0] & 0x3f);
150141
return d;
151142
}
152143

@@ -155,9 +146,9 @@ class Bm8563 : public BasePeripheral<> {
155146
/// @param ec The error code.
156147
void set_date(const Date &d, std::error_code &ec) {
157148
logger_.info("setting date");
158-
const uint8_t data[] = {byte2bcd(d.day), byte2bcd(d.weekday),
159-
(uint8_t)(byte2bcd(d.month) | ((d.year < 2000) ? 0x80 : 0x00)),
160-
byte2bcd(d.year % 100)};
149+
const uint8_t data[] = {decimal_to_bcd(d.day), decimal_to_bcd(d.weekday),
150+
(uint8_t)(decimal_to_bcd(d.month) | ((d.year < 2000) ? 0x80 : 0x00)),
151+
decimal_to_bcd(d.year % 100)};
161152
write_many_to_register((uint8_t)Registers::DATE, data, 4, ec);
162153
}
163154

@@ -172,9 +163,9 @@ class Bm8563 : public BasePeripheral<> {
172163
return {};
173164
}
174165
Time t;
175-
t.hour = bcd2byte(data[2] & 0x3f);
176-
t.minute = bcd2byte(data[1] & 0x7f);
177-
t.second = bcd2byte(data[0] & 0x7f);
166+
t.hour = bcd_to_decimal(data[2] & 0x3f);
167+
t.minute = bcd_to_decimal(data[1] & 0x7f);
168+
t.second = bcd_to_decimal(data[0] & 0x7f);
178169
return t;
179170
}
180171

@@ -183,7 +174,8 @@ class Bm8563 : public BasePeripheral<> {
183174
/// @param ec The error code.
184175
void set_time(const Time &t, std::error_code &ec) {
185176
logger_.info("Setting time");
186-
const uint8_t data[] = {byte2bcd(t.second), byte2bcd(t.minute), byte2bcd(t.hour)};
177+
const uint8_t data[] = {decimal_to_bcd(t.second), decimal_to_bcd(t.minute),
178+
decimal_to_bcd(t.hour)};
187179
write_many_to_register((uint8_t)Registers::TIME, data, 3, ec);
188180
}
189181

components/pcf85063/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
idf_component_register(
2+
INCLUDE_DIRS "include"
3+
REQUIRES "base_peripheral" "utils"
4+
)

components/pcf85063/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# PCF85063 Example
2+
3+
[![Badge](https://components.espressif.com/components/espp/pcf85063/badge.svg)](https://components.espressif.com/components/espp/pcf85063)
4+
5+
The `pcf85063` component provides a driver for the PCF85063 Real Time Clock (RTC).
6+
7+
It supports configuring and reading the time, setting alarms, and using the timer functionality.
8+
9+
## Example
10+
11+
The [example](./example) shows how to use the `espp::Pcf85063` component to initialize and
12+
communicate with the PCF85063 RTC.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The following lines of boilerplate have to be in your project's CMakeLists
2+
# in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.20)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
7+
# add the component directories that we want to use
8+
set(EXTRA_COMPONENT_DIRS
9+
"../../../components/"
10+
)
11+
12+
set(
13+
COMPONENTS
14+
"main esptool_py i2c pcf85063"
15+
CACHE STRING
16+
"List of components to include"
17+
)
18+
19+
project(pcf85063_example)
20+
21+
set(CMAKE_CXX_STANDARD 20)

components/pcf85063/example/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# PCF85063 Example
2+
3+
This example shows how to use the `espp::Pcf85063` component to initialize and
4+
communicate with the PCF85063 RTC.
5+
6+
## How to use example
7+
8+
### Hardware Required
9+
10+
This example is designed to run on a custom board with a PCF85063 RTC.
11+
You will need to configure the I2C pins for your specific hardware.
12+
13+
### Build and Flash
14+
15+
Build the project and flash it to the board, then run monitor tool to view
16+
serial output:
17+
18+
```
19+
idf.py -p PORT flash monitor
20+
```
21+
22+
(Replace PORT with the name of the serial port to use.)
23+
24+
(To exit the serial monitor, type ``Ctrl-]``.)
25+
26+
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
27+
28+
## Example Output
29+
30+
<img width="453" height="330" alt="CleanShot 2025-07-13 at 22 14 08" src="https://github.com/user-attachments/assets/3b51ac17-db40-4d2f-90d2-476c90e085ba" />

0 commit comments

Comments
 (0)