@@ -26,9 +26,11 @@ LOG_MODULE_REGISTER(DHT, CONFIG_SENSOR_LOG_LEVEL);
26
26
* @return duration in usec of signal being measured,
27
27
* -1 if duration exceeds DHT_SIGNAL_MAX_WAIT_DURATION
28
28
*/
29
- static s8_t dht_measure_signal_duration (struct dht_data * drv_data ,
30
- u32_t signal_val )
29
+ static s8_t dht_measure_signal_duration (struct device * dev ,
30
+ u32_t signal_val )
31
31
{
32
+ struct dht_data * drv_data = dev -> driver_data ;
33
+ const struct dht_config * cfg = dev -> config -> config_info ;
32
34
u32_t val ;
33
35
u32_t elapsed_cycles ;
34
36
u32_t max_wait_cycles = (u32_t )(
@@ -39,7 +41,7 @@ static s8_t dht_measure_signal_duration(struct dht_data *drv_data,
39
41
u32_t start_cycles = k_cycle_get_32 ();
40
42
41
43
do {
42
- gpio_pin_read (drv_data -> gpio , CONFIG_DHT_GPIO_PIN_NUM , & val );
44
+ gpio_pin_read (drv_data -> gpio , cfg -> pin , & val );
43
45
elapsed_cycles = k_cycle_get_32 () - start_cycles ;
44
46
45
47
if (elapsed_cycles > max_wait_cycles ) {
@@ -55,6 +57,7 @@ static s8_t dht_measure_signal_duration(struct dht_data *drv_data,
55
57
static int dht_sample_fetch (struct device * dev , enum sensor_channel chan )
56
58
{
57
59
struct dht_data * drv_data = dev -> driver_data ;
60
+ const struct dht_config * cfg = dev -> config -> config_info ;
58
61
int ret = 0 ;
59
62
s8_t signal_duration [DHT_DATA_BITS_NUM ];
60
63
s8_t max_duration , min_duration , avg_duration ;
@@ -64,44 +67,44 @@ static int dht_sample_fetch(struct device *dev, enum sensor_channel chan)
64
67
__ASSERT_NO_MSG (chan == SENSOR_CHAN_ALL );
65
68
66
69
/* send start signal */
67
- gpio_pin_write (drv_data -> gpio , CONFIG_DHT_GPIO_PIN_NUM , 0 );
70
+ gpio_pin_write (drv_data -> gpio , cfg -> pin , 0 );
68
71
69
72
k_busy_wait (DHT_START_SIGNAL_DURATION );
70
73
71
- gpio_pin_write (drv_data -> gpio , CONFIG_DHT_GPIO_PIN_NUM , 1 );
74
+ gpio_pin_write (drv_data -> gpio , cfg -> pin , 1 );
72
75
73
76
/* switch to DIR_IN to read sensor signals */
74
- gpio_pin_configure (drv_data -> gpio , CONFIG_DHT_GPIO_PIN_NUM ,
77
+ gpio_pin_configure (drv_data -> gpio , cfg -> pin ,
75
78
GPIO_DIR_IN );
76
79
77
80
/* wait for sensor response */
78
- if (dht_measure_signal_duration (drv_data , 1 ) == -1 ) {
81
+ if (dht_measure_signal_duration (dev , 1 ) == -1 ) {
79
82
ret = - EIO ;
80
83
goto cleanup ;
81
84
}
82
85
83
86
/* read sensor response */
84
- if (dht_measure_signal_duration (drv_data , 0 ) == -1 ) {
87
+ if (dht_measure_signal_duration (dev , 0 ) == -1 ) {
85
88
ret = - EIO ;
86
89
goto cleanup ;
87
90
}
88
91
89
92
/* wait for sensor data */
90
- if (dht_measure_signal_duration (drv_data , 1 ) == -1 ) {
93
+ if (dht_measure_signal_duration (dev , 1 ) == -1 ) {
91
94
ret = - EIO ;
92
95
goto cleanup ;
93
96
}
94
97
95
98
/* read sensor data */
96
99
for (i = 0U ; i < DHT_DATA_BITS_NUM ; i ++ ) {
97
100
/* LOW signal to indicate a new bit */
98
- if (dht_measure_signal_duration (drv_data , 0 ) == -1 ) {
101
+ if (dht_measure_signal_duration (dev , 0 ) == -1 ) {
99
102
ret = - EIO ;
100
103
goto cleanup ;
101
104
}
102
105
103
106
/* HIGH signal duration indicates bit value */
104
- signal_duration [i ] = dht_measure_signal_duration (drv_data , 1 );
107
+ signal_duration [i ] = dht_measure_signal_duration (dev , 1 );
105
108
if (signal_duration [i ] == -1 ) {
106
109
ret = - EIO ;
107
110
goto cleanup ;
@@ -152,9 +155,8 @@ static int dht_sample_fetch(struct device *dev, enum sensor_channel chan)
152
155
153
156
cleanup :
154
157
/* switch to DIR_OUT and leave pin to HIGH until next fetch */
155
- gpio_pin_configure (drv_data -> gpio , CONFIG_DHT_GPIO_PIN_NUM ,
156
- GPIO_DIR_OUT );
157
- gpio_pin_write (drv_data -> gpio , CONFIG_DHT_GPIO_PIN_NUM , 1 );
158
+ gpio_pin_configure (drv_data -> gpio , cfg -> pin , GPIO_DIR_OUT );
159
+ gpio_pin_write (drv_data -> gpio , cfg -> pin , 1 );
158
160
159
161
return ret ;
160
162
}
@@ -165,45 +167,48 @@ static int dht_channel_get(struct device *dev,
165
167
{
166
168
struct dht_data * drv_data = dev -> driver_data ;
167
169
168
- __ASSERT_NO_MSG (chan == SENSOR_CHAN_AMBIENT_TEMP || chan == SENSOR_CHAN_HUMIDITY );
170
+ __ASSERT_NO_MSG (chan == SENSOR_CHAN_AMBIENT_TEMP
171
+ || chan == SENSOR_CHAN_HUMIDITY );
169
172
170
173
/* see data calculation example from datasheet */
171
- #if defined(CONFIG_DHT_CHIP_DHT11 )
172
- /* use only integral data byte */
173
- if (chan == SENSOR_CHAN_HUMIDITY ) {
174
- val -> val1 = drv_data -> sample [0 ];
175
- val -> val2 = 0 ;
176
- } else { /* chan == SENSOR_CHAN_AMBIENT_TEMP */
177
- val -> val1 = drv_data -> sample [2 ];
178
- val -> val2 = 0 ;
179
- }
180
- #elif defined(CONFIG_DHT_CHIP_DHT22 )
181
- /*
182
- * use both integral and decimal data bytes; resulted 16bit data has
183
- * a resolution of 0.1 units
184
- */
185
- s16_t raw_val , sign ;
186
-
187
- if (chan == SENSOR_CHAN_HUMIDITY ) {
188
- raw_val = (drv_data -> sample [0 ] << 8 ) | drv_data -> sample [1 ];
189
- val -> val1 = raw_val / 10 ;
190
- val -> val2 = (raw_val % 10 ) * 100000 ;
191
- } else { /* chan == SENSOR_CHAN_AMBIENT_TEMP */
192
- raw_val = (drv_data -> sample [2 ] << 8 ) | drv_data -> sample [3 ];
193
-
194
- sign = raw_val & 0x8000 ;
195
- raw_val = raw_val & ~0x8000 ;
196
-
197
- val -> val1 = raw_val / 10 ;
198
- val -> val2 = (raw_val % 10 ) * 100000 ;
199
-
200
- /* handle negative value */
201
- if (sign ) {
202
- val -> val1 = - val -> val1 ;
203
- val -> val2 = - val -> val2 ;
174
+ if (IS_ENABLED (DT_INST_0_AOSONG_DHT_DHT22 )) {
175
+ /*
176
+ * use both integral and decimal data bytes; resulted
177
+ * 16bit data has a resolution of 0.1 units
178
+ */
179
+ s16_t raw_val , sign ;
180
+
181
+ if (chan == SENSOR_CHAN_HUMIDITY ) {
182
+ raw_val = (drv_data -> sample [0 ] << 8 )
183
+ + drv_data -> sample [1 ];
184
+ val -> val1 = raw_val / 10 ;
185
+ val -> val2 = (raw_val % 10 ) * 100000 ;
186
+ } else { /* chan == SENSOR_CHAN_AMBIENT_TEMP */
187
+ raw_val = (drv_data -> sample [2 ] << 8 )
188
+ + drv_data -> sample [3 ];
189
+
190
+ sign = raw_val & 0x8000 ;
191
+ raw_val = raw_val & ~0x8000 ;
192
+
193
+ val -> val1 = raw_val / 10 ;
194
+ val -> val2 = (raw_val % 10 ) * 100000 ;
195
+
196
+ /* handle negative value */
197
+ if (sign ) {
198
+ val -> val1 = - val -> val1 ;
199
+ val -> val2 = - val -> val2 ;
200
+ }
201
+ }
202
+ } else {
203
+ /* use only integral data byte */
204
+ if (chan == SENSOR_CHAN_HUMIDITY ) {
205
+ val -> val1 = drv_data -> sample [0 ];
206
+ val -> val2 = 0 ;
207
+ } else { /* chan == SENSOR_CHAN_AMBIENT_TEMP */
208
+ val -> val1 = drv_data -> sample [2 ];
209
+ val -> val2 = 0 ;
204
210
}
205
211
}
206
- #endif
207
212
208
213
return 0 ;
209
214
}
@@ -215,23 +220,30 @@ static const struct sensor_driver_api dht_api = {
215
220
216
221
static int dht_init (struct device * dev )
217
222
{
223
+ int rc = 0 ;
218
224
struct dht_data * drv_data = dev -> driver_data ;
225
+ const struct dht_config * cfg = dev -> config -> config_info ;
219
226
220
- drv_data -> gpio = device_get_binding (CONFIG_DHT_GPIO_DEV_NAME );
227
+ drv_data -> gpio = device_get_binding (cfg -> ctrl );
221
228
if (drv_data -> gpio == NULL ) {
222
- LOG_ERR ("Failed to get GPIO device." );
229
+ LOG_ERR ("Failed to get GPIO device %s." , cfg -> ctrl );
223
230
return - EINVAL ;
224
231
}
225
232
226
- gpio_pin_configure (drv_data -> gpio , CONFIG_DHT_GPIO_PIN_NUM ,
227
- GPIO_DIR_OUT );
228
-
229
- gpio_pin_write ( drv_data -> gpio , CONFIG_DHT_GPIO_PIN_NUM , 1 );
233
+ rc = gpio_pin_configure (drv_data -> gpio , cfg -> pin , GPIO_DIR_OUT );
234
+ if ( rc == 0 ) {
235
+ rc = gpio_pin_write ( drv_data -> gpio , cfg -> pin , 1 );
236
+ }
230
237
231
- return 0 ;
238
+ return rc ;
232
239
}
233
240
234
- struct dht_data dht_data ;
241
+ static struct dht_data dht_data ;
242
+ static const struct dht_config dht_config = {
243
+ .ctrl = DT_INST_0_AOSONG_DHT_DIO_GPIOS_CONTROLLER ,
244
+ .pin = DT_INST_0_AOSONG_DHT_DIO_GPIOS_PIN ,
245
+ };
235
246
236
- DEVICE_AND_API_INIT (dht_dev , CONFIG_DHT_NAME , & dht_init , & dht_data ,
237
- NULL , POST_KERNEL , CONFIG_SENSOR_INIT_PRIORITY , & dht_api );
247
+ DEVICE_AND_API_INIT (dht_dev , DT_INST_0_AOSONG_DHT_LABEL , & dht_init ,
248
+ & dht_data , & dht_config ,
249
+ POST_KERNEL , CONFIG_SENSOR_INIT_PRIORITY , & dht_api );
0 commit comments