Skip to content

Commit

Permalink
drivers: rtc: rtc_ds1307: Fix corruption of SECONDS register
Browse files Browse the repository at this point in the history
We read/modify/write the CH/SECONDS register at initialisation to clear
only the Clock Halt bit and only if it is set.

The previous implementation zeroes the entire register unconditionally at
initialisation, which wipes the SECONDS fields.

Fixes #77354.

Signed-off-by: Andrew Feldhaus ([email protected])
  • Loading branch information
clodnut committed Sep 17, 2024
1 parent 874e4e2 commit dbfcb8d
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions drivers/rtc/rtc_ds1307.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,23 @@ static int ds1307_init(const struct device *dev)
/* Disable squarewave output */
err = i2c_reg_write_byte_dt(&config->i2c_bus, DS1307_REG_CTRL, 0x00);
if (err < 0) {
LOG_ERR("Error: SQW:%d\n", err);
LOG_ERR("Error: SQW: %d\n", err);
}

/* Make clock halt = 0 */
err = i2c_reg_write_byte_dt(&config->i2c_bus, DS1307_REG_SECONDS, 0x00);
/* Ensure Clock Halt = 0 */
uint8_t reg = 0;

err = i2c_reg_read_byte_dt(&config->i2c_bus, DS1307_REG_SECONDS, &reg);
if (err < 0) {
LOG_ERR("Error: Set clock halt bit:%d\n", err);
LOG_ERR("Error: Read SECONDS/Clock Halt register: %d\n", err);
}
if (reg & ~SECONDS_BITS) {
/* Clock Halt bit is set */
err = i2c_reg_write_byte_dt(&config->i2c_bus, DS1307_REG_SECONDS,
reg & SECONDS_BITS);
if (err < 0) {
LOG_ERR("Error: Clear Clock Halt bit: %d\n", err);
}
}

return 0;
Expand Down

0 comments on commit dbfcb8d

Please sign in to comment.