Skip to content

Commit 53e3936

Browse files
str4t0mnashif
authored andcommitted
drivers: sensor: ds18b20: verify crc of scratchpad.
After each read of the scratchpad the crc of the data is checked and in case of a mismatch an error is returned. Signed-off-by: Thomas Stranger <[email protected]>
1 parent f4acf39 commit 53e3936

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

drivers/sensor/maxim/ds18b20/ds18b20.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,24 @@ static int ds18b20_read_scratchpad(const struct device *dev,
120120
struct ds18b20_data *data = dev->data;
121121
const struct ds18b20_config *cfg = dev->config;
122122
const struct device *bus = cfg->bus;
123+
int ret;
123124
uint8_t cmd = DS18B20_CMD_READ_SCRATCHPAD;
125+
uint8_t crc;
126+
127+
memset(scratchpad, 0, sizeof(*scratchpad));
128+
ret = w1_write_read(bus, &data->config, &cmd, 1,
129+
(uint8_t *)scratchpad, sizeof(*scratchpad));
130+
if (ret != 0) {
131+
return ret;
132+
}
133+
134+
crc = w1_crc8((uint8_t *)scratchpad, sizeof(*scratchpad) - 1);
135+
if (crc != scratchpad->crc) {
136+
LOG_WRN("CRC does not match");
137+
return -EIO;
138+
}
124139

125-
return w1_write_read(bus, &data->config, &cmd, 1,
126-
(uint8_t *)&scratchpad[0], 9);
140+
return 0;
127141
}
128142

129143
/* Starts sensor temperature conversion without waiting for completion. */

0 commit comments

Comments
 (0)