ds1307
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. The chip answers on the fixed address 0x68 and is a 5 V part
(4.5-5.5 V in the datasheet); the backup cell on VBAT keeps the oscillator and
the RAM alive while the main supply is off. The bus runs at 100 kHz, the
DS1307 is a standard-mode device.
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 goes out as zero in the same
frame. ds1307_start() stops or starts the oscillator,
ds1307_is_running() reports the halt bit.
The chip stores BCD. ds1307_get_time() converts to struct tm and
ds1307_set_time() 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. 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, and ds1307_get_time() rejects
such a frame with ESP_ERR_INVALID_RESPONSE.
The DS1307 latches nothing on read, so the seven registers are read sequentially and a carry in the middle of the transfer can mix the old and the new second; the window is one bus transaction long.
The SQW/OUT pin can output a square wave or a static level through the control
register at 0x07: ds1307_enable_squarewave() toggles SQWE,
ds1307_set_squarewave_freq() selects 1 Hz, 4.096 kHz, 8.192 kHz or
32.768 kHz and keeps the other bits, ds1307_set_output() sets the level
driven on the pin when the square wave is off. The pin is open-drain and needs
a pull-up; the driver does not configure the GPIO.
56 bytes at 0x08-0x3F survive power loss with the
backup cell. The RAM functions take an offset into that window and a length,
reject a range that leaves it, and move a multi-byte range in one I2C
transaction.
Defines
-
DS1307_ADDR
I2C address.
Enums
Functions
-
esp_err_t ds1307_init_desc(i2c_dev_t *dev, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio)
Initialize device descriptor.
- Parameters:
dev – Device descriptor
port – I2C port
sda_gpio – SDA GPIO
scl_gpio – SCL GPIO
- Returns:
ESP_OKon success
-
esp_err_t ds1307_free_desc(i2c_dev_t *dev)
Free device descriptor.
- Parameters:
dev – Device descriptor
- Returns:
ESP_OKon success
-
esp_err_t ds1307_start(i2c_dev_t *dev, bool start)
Start/stop clock.
- Parameters:
dev – Device descriptor
start – Start clock if true
- Returns:
ESP_OKon success
-
esp_err_t ds1307_is_running(i2c_dev_t *dev, bool *running)
Get current clock state.
- Parameters:
dev – Device descriptor
running – [out] true if clock running
- Returns:
ESP_OKon success
-
esp_err_t ds1307_get_time(i2c_dev_t *dev, struct tm *time)
Get current time.
- Parameters:
dev – Device descriptor
time – [out] Pointer to the time struct to fill
- Returns:
ESP_OKon success
-
esp_err_t ds1307_set_time(i2c_dev_t *dev, const struct tm *time)
Set time to RTC.
- Parameters:
dev – Device descriptor
time – [in] Pointer to the time struct
- Returns:
ESP_OKon success
-
esp_err_t ds1307_enable_squarewave(i2c_dev_t *dev, bool enable)
Enable or disable square-wave oscillator output.
- Parameters:
dev – Device descriptor
enable – Enable oscillator if true
- Returns:
ESP_OKon success
-
esp_err_t ds1307_is_squarewave_enabled(i2c_dev_t *dev, bool *sqw_en)
Get square-wave oscillator output.
- Parameters:
dev – Device descriptor
sqw_en – [out] true if square-wave oscillator enabled
- Returns:
ESP_OKon success
-
esp_err_t ds1307_set_squarewave_freq(i2c_dev_t *dev, ds1307_squarewave_freq_t freq)
Set square-wave oscillator frequency.
- Parameters:
dev – Device descriptor
freq – Frequency
- Returns:
ESP_OKon success
-
esp_err_t ds1307_get_squarewave_freq(i2c_dev_t *dev, ds1307_squarewave_freq_t *sqw_freq)
Get current square-wave oscillator frequency.
- Parameters:
dev – Device descriptor
sqw_freq – [out] Frequency
- Returns:
ESP_OKon success
-
esp_err_t ds1307_get_output(i2c_dev_t *dev, bool *out)
Get current output level of the SQW/OUT pin.
- Parameters:
dev – Device descriptor
out – [out] current output level of the SQW/OUT pin, true if high
- Returns:
ESP_OKon success
-
esp_err_t ds1307_set_output(i2c_dev_t *dev, bool value)
Set output level of the SQW/OUT pin.
Set output level if square-wave output is disabled
- Parameters:
dev – Device descriptor
value – High level if true
- Returns:
ESP_OKon success
-
esp_err_t ds1307_read_ram(i2c_dev_t *dev, uint8_t offset, uint8_t *buf, uint8_t len)
Read RAM contents into the buffer.
- Parameters:
dev – Device descriptor
offset – Start byte, 0..55
buf – [out] Buffer to store data
len – Bytes to read, 1..56
- Returns:
ESP_OKon success
-
esp_err_t ds1307_write_ram(i2c_dev_t *dev, uint8_t offset, const uint8_t *buf, uint8_t len)
Write buffer to RTC RAM.
- Parameters:
dev – Device descriptor
offset – Start byte, 0..55
buf – Buffer
len – Bytes to write, 1..56
- Returns:
ESP_OKon success