Driver for the DS1302 trickle-charge real-time clock. The chip keeps the time in BCD, has 31 bytes of battery-backed RAM and talks over a three-wire interface that the driver bit-bangs on ordinary GPIOs.
idf.py add-dependency esp-idf-lib/ds1302The module needs three GPIOs: CE (chip enable, also called RST), SCLK and a shared data line IO. There is no MISO/MOSI pair: the same pin carries data in both directions and the driver switches its direction between the command byte and the response.
| Module | ESP32 pin in the example | Notes |
|---|---|---|
| VCC | 3V3 or 5V | 2.0-5.5 V; the chip is the 3 V part, the board often has a regulator |
| GND | GND | |
| CE | GPIO16 | pulled low when the chip is idle, then driven high for a transfer |
| IO | GPIO17 | open-drain on the chip side; a 4.7-10 kOhm pull-up to VCC helps on long wires |
| SCLK | GPIO18 | 1 us half-period in the driver, the chip is good for 2 MHz |
The examples use CONFIG_EXAMPLE_CE_GPIO, CONFIG_EXAMPLE_IO_GPIO and
CONFIG_EXAMPLE_SCLK_GPIO from menuconfig. A CR2032 cell on the board keeps
the oscillator alive while the ESP32 is powered off; the crystal is 32.768 kHz.
#include <time.h>
#include <ds1302.h>
ds1302_t dev = {
.ce_pin = GPIO_NUM_16,
.io_pin = GPIO_NUM_17,
.sclk_pin = GPIO_NUM_18,
};
ESP_ERROR_CHECK(ds1302_init(&dev));
ESP_ERROR_CHECK(ds1302_set_write_protect(&dev, false));
struct tm time = {
.tm_year = 126, /* 2026, years since 1900 */
.tm_mon = 10, /* November, 0-based */
.tm_mday = 28,
.tm_hour = 16,
.tm_min = 30,
.tm_sec = 0,
.tm_wday = 6, /* Saturday, 0 is Sunday */
};
ESP_ERROR_CHECK(ds1302_set_time(&dev, &time));
ESP_ERROR_CHECK(ds1302_start(&dev, true));ds1302_init() only configures the three pins and reads the halt bit into
dev.ch; it does not start the clock. ds1302_start() clears or sets the CH
bit and updates that field, so ds1302_set_time() after a halt keeps the clock
stopped until ds1302_start(&dev, true).
The chip stores BCD. ds1302_get_time() converts everything to struct tm,
ds1302_set_time() converts back and rejects out-of-range fields with
ESP_ERR_INVALID_ARG:
tm_sec0-59,tm_min0-59,tm_hour0-23;tm_mday1-31,tm_mon0-11,tm_wday0-6 (Sunday is 0);tm_year100-199, that is 2000-2099.
The chip rolls the date itself: months with fewer than 31 days are adjusted,
and February has 29 days in the years divisible by 4 (the two-digit year 00 is
2000, which is a leap year). The driver only converts to and from struct tm.
Reading in 12-hour mode is supported: the driver takes the AM/PM bit and the
hour field, converts 12 AM to 0:00 and leaves the rest as struct tm expects.
Writing always uses 24-hour mode.
Registers read from a chip without a working backup cell come back as 0xFF
or with invalid BCD digits. ds1302_get_time() rejects such a frame with
ESP_ERR_INVALID_RESPONSE instead of filling struct tm with impossible
values; treat the time as lost and set it again.
The write-protect bit in register 0x8E blocks every write, including the
clock registers. ds1302_set_write_protect(&dev, true) after setting the time
is a good idea while the time is meant to stay fixed: it keeps a stray write
from the bus or a firmware bug from corrupting the clock.
The clock burst write used by ds1302_set_time() carries the write-protect
byte as its last byte, so the call leaves the chip unprotected. Protect it
again afterwards if the application needs that.
31 bytes at 0x00-0x1E survive power loss with the backup cell.
ds1302_read_sram() and ds1302_write_sram() take a byte offset and a length,
and reject a range that leaves the 31 bytes with ESP_ERR_INVALID_ARG. With
offset == 0 the driver uses the RAM burst command, which transfers the whole
range in one selection; with any other offset it addresses each byte through
its own register (0xC0 + 2 * n), because the burst command always starts at
the first byte.
The three-wire transfer is bit-banged inside a critical section, so a clock burst (8 bytes) keeps interrupts disabled for about 150 us on a 240 MHz ESP32. That is short enough for the network stack and the FreeRTOS tick, but it is not free: do not call the clock functions from a tight loop at high frequency.
ds1302_init() rejects pins that are not output capable and pins that repeat;
two signals on one pin would drive against each other. The driver does not
create a mutex and is not thread safe: calls from several tasks must be
serialised by the caller, otherwise a transfer can be split in the middle.
The driver does not touch the trickle charger. Enabling it requires a diode and resistors on the board, and the register value depends on the hardware, so it stays the application's job.
For questions and discussions about the component, please use Discussions at esp-idf-lib/core.
Please read CONTRIBUTING.md at esp-idf-lib/core.