15
15
* Parasite power configuration is not supported by the driver.
16
16
*/
17
17
18
+ #include <zephyr/device.h>
19
+ #include <zephyr/devicetree.h>
18
20
#include <zephyr/drivers/sensor.h>
19
21
#include <zephyr/drivers/sensor/w1_sensor.h>
22
+ #include "zephyr/drivers/w1.h"
23
+ #include <zephyr/kernel.h>
20
24
#include <zephyr/logging/log.h>
21
25
#include <zephyr/sys/__assert.h>
22
-
23
- #include "ds18b20.h"
26
+ #include <zephyr/sys/util_macro.h>
24
27
25
28
LOG_MODULE_REGISTER (DS18B20 , CONFIG_SENSOR_LOG_LEVEL );
26
29
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
+
27
74
static int ds18b20_configure (const struct device * dev );
28
75
29
76
/* 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,
55
102
struct ds18b20_scratchpad scratchpad )
56
103
{
57
104
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 ;
59
107
uint8_t sp_data [4 ] = {
60
108
DS18B20_CMD_WRITE_SCRATCHPAD ,
61
109
scratchpad .alarm_temp_high ,
@@ -70,7 +118,8 @@ static int ds18b20_read_scratchpad(const struct device *dev,
70
118
struct ds18b20_scratchpad * scratchpad )
71
119
{
72
120
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 ;
74
123
uint8_t cmd = DS18B20_CMD_READ_SCRATCHPAD ;
75
124
76
125
return w1_write_read (bus , & data -> config , & cmd , 1 ,
@@ -82,7 +131,8 @@ static int ds18b20_temperature_convert(const struct device *dev)
82
131
{
83
132
int ret ;
84
133
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 ;
86
136
87
137
(void )w1_lock_bus (bus );
88
138
ret = w1_reset_select (bus , & data -> config );
0 commit comments