1515 * Parasite power configuration is not supported by the driver.
1616 */
1717
18+ #include <zephyr/device.h>
19+ #include <zephyr/devicetree.h>
1820#include <zephyr/drivers/sensor.h>
1921#include <zephyr/drivers/sensor/w1_sensor.h>
22+ #include "zephyr/drivers/w1.h"
23+ #include <zephyr/kernel.h>
2024#include <zephyr/logging/log.h>
2125#include <zephyr/sys/__assert.h>
22-
23- #include "ds18b20.h"
26+ #include <zephyr/sys/util_macro.h>
2427
2528LOG_MODULE_REGISTER (DS18B20 , CONFIG_SENSOR_LOG_LEVEL );
2629
30+ #define DS18B20_CMD_CONVERT_T 0x44
31+ #define DS18B20_CMD_WRITE_SCRATCHPAD 0x4E
32+ #define DS18B20_CMD_READ_SCRATCHPAD 0xBE
33+ #define DS18B20_CMD_COPY_SCRATCHPAD 0x48
34+ #define DS18B20_CMD_RECALL_EEPROM 0xB8
35+ #define DS18B20_CMD_READ_POWER_SUPPLY 0xB4
36+
37+ /* resolution is set using bit 5 and 6 of configuration register
38+ * macro only valid for values 9 to 12
39+ */
40+ #define DS18B20_RESOLUTION_POS 5
41+ #define DS18B20_RESOLUTION_MASK (BIT_MASK(2) << DS18B20_RESOLUTION_POS)
42+ /* convert resolution in bits to scratchpad config format */
43+ #define DS18B20_RESOLUTION (res ) ((res - 9) << DS18B20_RESOLUTION_POS)
44+ /* convert resolution in bits to array index (for resolution specific elements) */
45+ #define DS18B20_RESOLUTION_INDEX (res ) (res - 9)
46+
47+ #define DS18B20_FAMILYCODE 0x28
48+ #define DS18S20_FAMILYCODE 0x10
49+
50+ enum chip_type {type_ds18b20 , type_ds18s20 };
51+
52+ struct ds18b20_scratchpad {
53+ int16_t temp ;
54+ uint8_t alarm_temp_high ;
55+ uint8_t alarm_temp_low ;
56+ uint8_t config ;
57+ uint8_t res [3 ];
58+ uint8_t crc ;
59+ } __packed ;
60+
61+ struct ds18b20_config {
62+ const struct device * bus ;
63+ uint8_t family ;
64+ uint8_t resolution ;
65+ enum chip_type chip ;
66+ };
67+
68+ struct ds18b20_data {
69+ struct w1_slave_config config ;
70+ struct ds18b20_scratchpad scratchpad ;
71+ bool lazy_loaded ;
72+ };
73+
2774static int ds18b20_configure (const struct device * dev );
2875
2976/* measure wait time for 9-bit, 10-bit, 11-bit, 12-bit resolution respectively */
@@ -55,7 +102,8 @@ static int ds18b20_write_scratchpad(const struct device *dev,
55102 struct ds18b20_scratchpad scratchpad )
56103{
57104 struct ds18b20_data * data = dev -> data ;
58- const struct device * bus = ds18b20_bus (dev );
105+ const struct ds18b20_config * cfg = dev -> config ;
106+ const struct device * bus = cfg -> bus ;
59107 uint8_t sp_data [4 ] = {
60108 DS18B20_CMD_WRITE_SCRATCHPAD ,
61109 scratchpad .alarm_temp_high ,
@@ -70,7 +118,8 @@ static int ds18b20_read_scratchpad(const struct device *dev,
70118 struct ds18b20_scratchpad * scratchpad )
71119{
72120 struct ds18b20_data * data = dev -> data ;
73- const struct device * bus = ds18b20_bus (dev );
121+ const struct ds18b20_config * cfg = dev -> config ;
122+ const struct device * bus = cfg -> bus ;
74123 uint8_t cmd = DS18B20_CMD_READ_SCRATCHPAD ;
75124
76125 return w1_write_read (bus , & data -> config , & cmd , 1 ,
@@ -82,7 +131,8 @@ static int ds18b20_temperature_convert(const struct device *dev)
82131{
83132 int ret ;
84133 struct ds18b20_data * data = dev -> data ;
85- const struct device * bus = ds18b20_bus (dev );
134+ const struct ds18b20_config * cfg = dev -> config ;
135+ const struct device * bus = cfg -> bus ;
86136
87137 (void )w1_lock_bus (bus );
88138 ret = w1_reset_select (bus , & data -> config );
0 commit comments