Skip to content

Commit b5d3dea

Browse files
str4t0mnashif
authored andcommitted
drivers: sensor: ds18b20: indicate error for missing response
When multiple devices are on the bus, and no slave matches the sent rom, the master receives only ones. This should be handled explicitly and the error reporting should not rely on the crc mismatch of the scratchpad. Note: Make sure that the sensor configuration is loaded to the eeprom (copy scratchpad), otherwise the resolution will not match the expectations in case a node looses power and recovers. As in that case the configuration will not be applied again. Also the sensor may still report the power on reset value of +85°C just after the sensor powered up. Signed-off-by: Thomas Stranger <[email protected]>
1 parent 6fd767d commit b5d3dea

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

drivers/sensor/maxim/ds18b20/ds18b20.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* Parasite power configuration is not supported by the driver.
1616
*/
1717

18+
#include <stdbool.h>
19+
1820
#include <zephyr/device.h>
1921
#include <zephyr/devicetree.h>
2022
#include <zephyr/drivers/sensor.h>
@@ -96,6 +98,17 @@ static inline void ds18b20_temperature_from_raw(const struct device *dev,
9698
}
9799
}
98100

101+
static inline bool slave_responded(uint8_t *rx_buf, size_t len)
102+
{
103+
uint8_t cmp_byte = 0xff;
104+
105+
for (int i = 0; i < len; i++) {
106+
cmp_byte &= rx_buf[i];
107+
}
108+
109+
return (cmp_byte == 0xff) ? false : true;
110+
}
111+
99112
/*
100113
* Write scratch pad, read back, then copy to eeprom
101114
*/
@@ -148,6 +161,11 @@ static int ds18b20_read_scratchpad(const struct device *dev,
148161
return ret;
149162
}
150163

164+
if (!slave_responded((uint8_t *)scratchpad, sizeof(*scratchpad))) {
165+
LOG_WRN("Slave not reachable");
166+
return -ENODEV;
167+
}
168+
151169
crc = w1_crc8((uint8_t *)scratchpad, sizeof(*scratchpad) - 1);
152170
if (crc != scratchpad->crc) {
153171
LOG_WRN("CRC does not match");

0 commit comments

Comments
 (0)