Skip to content
esp-idf-libPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

esp-idf-lib/ds1307

Build examples Build docs Validation

Driver for the DS1307 serial real-time clock: BCD time and date registers, 56 bytes of battery-backed RAM and a programmable square-wave output, all over plain I2C.

Installation

idf.py add-dependency esp-idf-lib/ds1307

Hardware

The chip answers on the fixed address 0x68 and is a 5 V part: the supply range in the datasheet is 4.5-5.5 V, and the backup cell on VBAT keeps the oscillator and the RAM alive while the main supply is off. Many breakout boards are used from 3.3 V with pull-ups to 3.3 V; that works on the bench but is outside the datasheet, so a board that misbehaves on 3.3 V is the first suspect.

The bus runs at 100 kHz (I2C_FREQ_HZ in the driver); the DS1307 is a standard-mode device and does not do 400 kHz.

Module ESP32 pin in the example
VCC 5V (see above)
GND GND
SDA GPIO21, pulled up to the bus voltage
SCL GPIO22, pulled up to the bus voltage

The example takes the pins from CONFIG_EXAMPLE_I2C_MASTER_SDA and CONFIG_EXAMPLE_I2C_MASTER_SCL, the port is I2C_NUM_0.

Usage

#include <time.h>
#include <ds1307.h>

i2c_dev_t dev;
ESP_ERROR_CHECK(ds1307_init_desc(&dev, I2C_NUM_0, GPIO_NUM_21, GPIO_NUM_22));

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(ds1307_set_time(&dev, &time));

ds1307_init_desc() only fills the descriptor and creates the mutex; the bus is set up on the first transfer. ds1307_set_time() leaves the clock running: the halt bit of the seconds register is written as zero as part of the frame. ds1307_start() stops or starts the oscillator, ds1307_is_running() reports the halt bit.

Time and date

The chip stores BCD, ds1307_get_time() converts to struct tm and ds1307_set_time() converts back. Out-of-range fields are rejected with ESP_ERR_INVALID_ARG: seconds and minutes 0-59, hours 0-23, weekday 0-6 with Sunday as 0, day 1-31, month 0-11, year 100-199 (2000-2099).

There is no century register: the chip stores two digits and the driver reads them as 2000-2099. Reading in 12-hour mode is supported, 12 AM becomes 0:00; writing always uses 24-hour mode.

A chip without a working backup cell answers with 0xFF or with invalid BCD digits. ds1307_get_time() rejects such a frame with ESP_ERR_INVALID_RESPONSE instead of filling struct tm with impossible values; set the time again in that case.

The DS1307 latches nothing on read: the seven registers are read sequentially, and a carry that happens in the middle of the transfer can mix the old and the new second. The window is one bus transaction long (about 1 ms at 100 kHz), so the effect is a single odd reading at most.

Square wave output

The SQW/OUT pin can output a square wave or a static level, controlled by the control register at 0x07:

  • ds1307_enable_squarewave() toggles the SQWE bit;
  • ds1307_set_squarewave_freq() selects 1 Hz, 4.096 kHz, 8.192 kHz or 32.768 kHz and keeps the other bits of the register;
  • ds1307_set_output() sets the OUT level, which is driven on the pin when the square wave is disabled.

The pin is open-drain and needs a pull-up. The driver does not touch the pin itself, only the chip's registers.

RAM

56 bytes at 0x08-0x3F survive power loss with the backup cell. ds1307_read_ram() and ds1307_write_ram() take a byte offset into that window and a length, and reject a range that leaves it with ESP_ERR_INVALID_ARG. A multi-byte read or write is one I2C transaction; the chip increments the address by itself.

Support

For questions and discussions about the component, please use Discussions at esp-idf-lib/core.

Contributing

Please read CONTRIBUTING.md at esp-idf-lib/core.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages