ds1302

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.

The module needs three GPIOs: CE, 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. The chip runs from 2.0-5.5 V, the crystal is 32.768 kHz and a backup cell keeps the time while the ESP32 is off. The examples use CONFIG_EXAMPLE_CE_GPIO, CONFIG_EXAMPLE_IO_GPIO and CONFIG_EXAMPLE_SCLK_GPIO from menuconfig.

ds1302_init() only configures the 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.

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: seconds and minutes 0-59, hours 0-23, day 1-31, month 0-11, weekday 0-6 with Sunday as 0, year 100-199 (2000-2099). The chip adjusts the date for months with fewer than 31 days and for leap years, the driver only converts to and from struct tm. Reading in 12-hour mode is supported, 12 AM becomes 0:00; writing always uses 24-hour mode. A frame with invalid BCD digits, as read from a chip without a working backup cell, is rejected with ESP_ERR_INVALID_RESPONSE.

The write-protect bit in register 0x8E blocks every write, including the clock registers, so ds1302_set_write_protect() after setting the time is a good idea. The clock burst write used by ds1302_set_time() carries the write-protect byte as its last byte and therefore leaves the chip unprotected.

DS1302_RAM_SIZE bytes of RAM survive power loss with the backup cell, ds1302_read_sram() and ds1302_write_sram() take an offset and a length and reject a range beyond those 31 bytes.

The transfer is bit-banged inside a critical section: a clock burst keeps interrupts disabled for about 150 us. The driver does not create a mutex and is not thread safe, calls from several tasks must be serialised by the caller. The trickle charger is not touched: enabling it depends on the parts on the board.

Defines

DS1302_RAM_SIZE

Functions

esp_err_t ds1302_init(ds1302_t *dev)

Initialize device.

Parameters:

dev – Device descriptor

Returns:

ESP_OK on success

esp_err_t ds1302_start(ds1302_t *dev, bool start)

Start/stop clock.

Parameters:
  • dev – Device descriptor

  • start – Start clock if true

Returns:

ESP_OK on success

esp_err_t ds1302_is_running(ds1302_t *dev, bool *running)

Get current clock state.

Parameters:
  • dev – Device descriptor

  • running – [out] true if clock running

Returns:

ESP_OK on success

esp_err_t ds1302_set_write_protect(ds1302_t *dev, bool wp)

Enable/disable write protection.

Parameters:
  • dev – Device descriptor

  • wp – Set RTC write-protected if true

Returns:

ESP_OK on success

esp_err_t ds1302_get_write_protect(ds1302_t *dev, bool *wp)

Get write protection status.

Parameters:
  • dev – Device descriptor

  • wp – [out] true if RTC write-protected

Returns:

ESP_OK on success

esp_err_t ds1302_get_time(ds1302_t *dev, struct tm *time)

Get current time.

Parameters:
  • dev – Device descriptor

  • time – [out] Current time

Returns:

ESP_OK on success

esp_err_t ds1302_set_time(ds1302_t *dev, const struct tm *time)

Set time to RTC.

Parameters:
  • dev – Device descriptor

  • time – Time

Returns:

ESP_OK on success

esp_err_t ds1302_read_sram(ds1302_t *dev, uint8_t offset, void *buf, uint8_t len)

Read RAM contents into the buffer.

Parameters:
  • dev – Device descriptor

  • offset – Start byte, 0..DS1302_RAM_SIZE-1

  • buf – [out] Buffer

  • len – Bytes to read, 1..DS1302_RAM_SIZE-offset

Returns:

ESP_OK on success

esp_err_t ds1302_write_sram(ds1302_t *dev, uint8_t offset, const void *buf, uint8_t len)

Write buffer to RTC RAM.

Parameters:
  • dev – Device descriptor

  • offset – Start byte, 0..DS1302_RAM_SIZE-1

  • buf – Buffer

  • len – Bytes to write, 1..DS1302_RAM_SIZE-offset

Returns:

ESP_OK on success

struct ds1302_t
#include <ds1302.h>

Device descriptor.

Public Members

gpio_num_t ce_pin

GPIO pin connected to CE.

gpio_num_t io_pin

GPIO pin connected to chip I/O.

gpio_num_t sclk_pin

GPIO pin connected to SCLK.

bool ch

true if clock is halted