Skip to content

Commit ec083b7

Browse files
clodnutkartben
authored andcommitted
drivers: rtc: rtc_ds1307: Fix corruption of SECONDS register
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]>
1 parent c966eac commit ec083b7

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

drivers/rtc/rtc_ds1307.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,23 @@ static int ds1307_init(const struct device *dev)
136136
/* Disable squarewave output */
137137
err = i2c_reg_write_byte_dt(&config->i2c_bus, DS1307_REG_CTRL, 0x00);
138138
if (err < 0) {
139-
LOG_ERR("Error: SQW:%d\n", err);
139+
LOG_ERR("Error: SQW: %d\n", err);
140140
}
141141

142-
/* Make clock halt = 0 */
143-
err = i2c_reg_write_byte_dt(&config->i2c_bus, DS1307_REG_SECONDS, 0x00);
142+
/* Ensure Clock Halt = 0 */
143+
uint8_t reg = 0;
144+
145+
err = i2c_reg_read_byte_dt(&config->i2c_bus, DS1307_REG_SECONDS, &reg);
144146
if (err < 0) {
145-
LOG_ERR("Error: Set clock halt bit:%d\n", err);
147+
LOG_ERR("Error: Read SECONDS/Clock Halt register: %d\n", err);
148+
}
149+
if (reg & ~SECONDS_BITS) {
150+
/* Clock Halt bit is set */
151+
err = i2c_reg_write_byte_dt(&config->i2c_bus, DS1307_REG_SECONDS,
152+
reg & SECONDS_BITS);
153+
if (err < 0) {
154+
LOG_ERR("Error: Clear Clock Halt bit: %d\n", err);
155+
}
146156
}
147157

148158
return 0;

0 commit comments

Comments
 (0)