Lines Matching +full:crc +full:- +full:en

4  * SPDX-License-Identifier: Apache-2.0
8 * Driver for DS18B20 and DS18S20 1-Wire temperature sensors
10 * - https://www.analog.com/media/en/technical-documentation/data-sheets/ds18b20.pdf
11 * - https://www.analog.com/media/en/technical-documentation/data-sheets/ds18s20.pdf
43 #define DS18B20_RESOLUTION(res) ((res - 9) << DS18B20_RESOLUTION_POS)
45 #define DS18B20_RESOLUTION_INDEX(res) (res - 9)
58 uint8_t crc; member
77 /* measure wait time for 9-bit, 10-bit, 11-bit, 12-bit resolution respectively */
87 const struct ds18b20_config *cfg = dev->config; in ds18b20_temperature_from_raw()
90 if (cfg->chip == type_ds18s20) { in ds18b20_temperature_from_raw()
91 val->val1 = temp / 2; in ds18b20_temperature_from_raw()
92 val->val2 = (temp % 2) * 5000000; in ds18b20_temperature_from_raw()
94 val->val1 = temp / 16; in ds18b20_temperature_from_raw()
95 val->val2 = (temp % 16) * 1000000 / 16; in ds18b20_temperature_from_raw()
116 struct ds18b20_data *data = dev->data; in ds18b20_write_scratchpad()
117 const struct ds18b20_config *cfg = dev->config; in ds18b20_write_scratchpad()
118 const struct device *bus = cfg->bus; in ds18b20_write_scratchpad()
127 ret = w1_write_read(bus, &data->config, sp_data, sizeof(sp_data), NULL, 0); in ds18b20_write_scratchpad()
139 return -EIO; in ds18b20_write_scratchpad()
148 struct ds18b20_data *data = dev->data; in ds18b20_read_scratchpad()
149 const struct ds18b20_config *cfg = dev->config; in ds18b20_read_scratchpad()
150 const struct device *bus = cfg->bus; in ds18b20_read_scratchpad()
153 uint8_t crc; in ds18b20_read_scratchpad() local
156 ret = w1_write_read(bus, &data->config, &cmd, 1, in ds18b20_read_scratchpad()
164 return -ENODEV; in ds18b20_read_scratchpad()
167 crc = w1_crc8((uint8_t *)scratchpad, sizeof(*scratchpad) - 1); in ds18b20_read_scratchpad()
168 if (crc != scratchpad->crc) { in ds18b20_read_scratchpad()
169 LOG_WRN("CRC does not match"); in ds18b20_read_scratchpad()
170 return -EIO; in ds18b20_read_scratchpad()
180 struct ds18b20_data *data = dev->data; in ds18b20_temperature_convert()
181 const struct ds18b20_config *cfg = dev->config; in ds18b20_temperature_convert()
182 const struct device *bus = cfg->bus; in ds18b20_temperature_convert()
185 ret = w1_reset_select(bus, &data->config); in ds18b20_temperature_convert()
201 struct ds18b20_data *data = dev->data; in ds18b20_set_resolution()
203 data->scratchpad.config &= ~DS18B20_RESOLUTION_MASK; in ds18b20_set_resolution()
204 data->scratchpad.config |= DS18B20_RESOLUTION(resolution); in ds18b20_set_resolution()
209 const struct ds18b20_config *cfg = dev->config; in measure_wait_ms()
211 if (cfg->chip == type_ds18s20) { in measure_wait_ms()
215 return measure_wait_ds18b20_ms[DS18B20_RESOLUTION_INDEX(cfg->resolution)]; in measure_wait_ms()
221 struct ds18b20_data *data = dev->data; in ds18b20_sample_fetch()
227 if (!data->lazy_loaded) { in ds18b20_sample_fetch()
232 data->lazy_loaded = true; in ds18b20_sample_fetch()
241 return ds18b20_read_scratchpad(dev, &data->scratchpad); in ds18b20_sample_fetch()
248 struct ds18b20_data *data = dev->data; in ds18b20_channel_get()
251 return -ENOTSUP; in ds18b20_channel_get()
254 ds18b20_temperature_from_raw(dev, (uint8_t *)&data->scratchpad.temp, val); in ds18b20_channel_get()
260 const struct ds18b20_config *cfg = dev->config; in ds18b20_configure()
261 struct ds18b20_data *data = dev->data; in ds18b20_configure()
264 if (w1_reset_bus(cfg->bus) <= 0) { in ds18b20_configure()
265 LOG_ERR("No 1-Wire slaves connected"); in ds18b20_configure()
266 return -ENODEV; in ds18b20_configure()
270 if (w1_get_slave_count(cfg->bus) == 1) { in ds18b20_configure()
271 if (w1_rom_to_uint64(&data->config.rom) == 0ULL) { in ds18b20_configure()
272 (void)w1_read_rom(cfg->bus, &data->config.rom); in ds18b20_configure()
274 } else if (w1_rom_to_uint64(&data->config.rom) == 0ULL) { in ds18b20_configure()
275 LOG_DBG("nr: %d", w1_get_slave_count(cfg->bus)); in ds18b20_configure()
277 return -EINVAL; in ds18b20_configure()
280 if ((cfg->family != 0) && (cfg->family != data->config.rom.family)) { in ds18b20_configure()
281 LOG_ERR("Found 1-Wire slave is not a %s", dev->name); in ds18b20_configure()
282 return -EINVAL; in ds18b20_configure()
286 if (cfg->chip == type_ds18b20) { in ds18b20_configure()
287 ds18b20_set_resolution(dev, cfg->resolution); in ds18b20_configure()
288 ret = ds18b20_write_scratchpad(dev, data->scratchpad); in ds18b20_configure()
293 LOG_DBG("Init %s: ROM=%016llx\n", dev->name, in ds18b20_configure()
294 w1_rom_to_uint64(&data->config.rom)); in ds18b20_configure()
302 struct ds18b20_data *data = dev->data; in ds18b20_attr_set()
305 return -ENOTSUP; in ds18b20_attr_set()
308 data->lazy_loaded = false; in ds18b20_attr_set()
309 w1_sensor_value_to_rom(thr, &data->config.rom); in ds18b20_attr_set()
321 const struct ds18b20_config *cfg = dev->config; in ds18b20_init()
322 struct ds18b20_data *data = dev->data; in ds18b20_init()
324 if (device_is_ready(cfg->bus) == 0) { in ds18b20_init()
326 return -ENODEV; in ds18b20_init()
329 w1_uint64_to_rom(0ULL, &data->config.rom); in ds18b20_init()
330 data->lazy_loaded = false; in ds18b20_init()