|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2018, RT-Thread Development Team |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + * 2019-08-01 LuoGong the first version. |
| 9 | + * 2019-08-15 MurphyZhao add lock and modify code style |
| 10 | + * |
| 11 | + */ |
| 12 | + |
| 13 | +#include "sensor_dallas_dht11.h" |
| 14 | +#include <rtdevice.h> |
| 15 | +#include <rthw.h> |
| 16 | +#include "sensor.h" |
| 17 | +#include "board.h" |
| 18 | +#include <stdint.h> |
| 19 | + |
| 20 | +#define SENSOR_DEBUG |
| 21 | +#define DBG_TAG "sensor.dht11" |
| 22 | + |
| 23 | +#ifdef SENSOR_DEBUG |
| 24 | +#define DBG_LVL DBG_LOG |
| 25 | +#else |
| 26 | +#define DBG_LVL DBG_ERROR |
| 27 | +#endif /* SENSOR_DEBUG */ |
| 28 | +#include <rtdbg.h> |
| 29 | + |
| 30 | +#define SENSOR_TEMP_RANGE_MAX (100) |
| 31 | +#define SENSOR_TEMP_RANGE_MIN (0) |
| 32 | +#define SENSOR_HUMI_RANGE_MAX (100) |
| 33 | +#define SENSOR_HUMI_RANGE_MIN (0) |
| 34 | + |
| 35 | +#ifndef RT_USING_PIN |
| 36 | +#error "Please enable RT_USING_PIN" |
| 37 | +#endif |
| 38 | + |
| 39 | +#ifndef rt_hw_us_delay |
| 40 | +RT_WEAK void rt_hw_us_delay(rt_uint32_t us) |
| 41 | +{ |
| 42 | + rt_uint32_t delta; |
| 43 | + |
| 44 | + us = us * (SysTick->LOAD / (1000000 / RT_TICK_PER_SECOND)); |
| 45 | + delta = SysTick->VAL; |
| 46 | + |
| 47 | + while (delta - SysTick->VAL < us) continue; |
| 48 | +} |
| 49 | +#endif |
| 50 | + |
| 51 | +static void dht11_reset(rt_base_t pin) |
| 52 | +{ |
| 53 | + rt_pin_mode(pin, PIN_MODE_OUTPUT); |
| 54 | + |
| 55 | + rt_pin_write(pin, PIN_LOW); |
| 56 | + rt_thread_mdelay(20); /* 20ms */ |
| 57 | + |
| 58 | + rt_pin_write(pin, PIN_HIGH); |
| 59 | + rt_hw_us_delay(30); /* 30us*/ |
| 60 | +} |
| 61 | + |
| 62 | +static uint8_t dht11_check(rt_base_t pin) |
| 63 | +{ |
| 64 | + uint8_t retry = 0; |
| 65 | + rt_pin_mode(pin, PIN_MODE_INPUT); |
| 66 | + |
| 67 | + while (rt_pin_read(pin) && retry < 100) |
| 68 | + { |
| 69 | + retry++; |
| 70 | + rt_hw_us_delay(1); |
| 71 | + } |
| 72 | + |
| 73 | + if(retry >= 100) |
| 74 | + { |
| 75 | + return CONNECT_FAILED; |
| 76 | + } |
| 77 | + |
| 78 | + retry = 0; |
| 79 | + while (!rt_pin_read(pin) && retry < 100) |
| 80 | + { |
| 81 | + retry++; |
| 82 | + rt_hw_us_delay(1); |
| 83 | + }; |
| 84 | + |
| 85 | + if(retry >= 100) |
| 86 | + { |
| 87 | + return CONNECT_FAILED; |
| 88 | + } |
| 89 | + |
| 90 | + return CONNECT_SUCCESS; |
| 91 | +} |
| 92 | + |
| 93 | +static uint8_t dht11_read_bit(rt_base_t pin) |
| 94 | +{ |
| 95 | + uint8_t retry = 0; |
| 96 | + while (rt_pin_read(pin) && retry < 100) |
| 97 | + { |
| 98 | + retry++; |
| 99 | + rt_hw_us_delay(1); |
| 100 | + } |
| 101 | + retry = 0; |
| 102 | + |
| 103 | + while (!rt_pin_read(pin) && retry < 100) |
| 104 | + { |
| 105 | + retry++; |
| 106 | + rt_hw_us_delay(1); |
| 107 | + } |
| 108 | + |
| 109 | + rt_hw_us_delay(40); |
| 110 | + if(rt_pin_read(pin)) |
| 111 | + return 1; |
| 112 | + return 0; |
| 113 | +} |
| 114 | + |
| 115 | +static uint8_t dht11_read_byte(rt_base_t pin) |
| 116 | +{ |
| 117 | + uint8_t i, dat = 0; |
| 118 | + |
| 119 | + for (i = 1; i <= 8; i++) |
| 120 | + { |
| 121 | + dat <<= 1; |
| 122 | + dat |= dht11_read_bit(pin); |
| 123 | + } |
| 124 | + |
| 125 | + return dat; |
| 126 | +} |
| 127 | + |
| 128 | +static uint8_t dht11_read_Data(rt_base_t pin,uint8_t *temp,uint8_t *humi) |
| 129 | +{ |
| 130 | + uint8_t i, buf[5]; |
| 131 | + dht11_reset(pin); |
| 132 | + |
| 133 | + if(dht11_check(pin) == 0) |
| 134 | + { |
| 135 | + for(i=0; i<5; i++) /* read 40 bits */ |
| 136 | + { |
| 137 | + buf[i] = dht11_read_byte(pin); |
| 138 | + } |
| 139 | + |
| 140 | + if((buf[0] + buf[1] + buf[2] + buf[3]) == buf[4]) |
| 141 | + { |
| 142 | + *humi = buf[0]; |
| 143 | + *temp = buf[2]; |
| 144 | + } |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + return 1; |
| 149 | + } |
| 150 | + |
| 151 | + return 0; |
| 152 | +} |
| 153 | + |
| 154 | +uint8_t dht11_init(rt_base_t pin) |
| 155 | +{ |
| 156 | + uint8_t ret = 0; |
| 157 | + |
| 158 | + dht11_reset(pin); |
| 159 | + ret = dht11_check(pin); |
| 160 | + |
| 161 | + return ret; |
| 162 | +} |
| 163 | + |
| 164 | +int32_t dht11_get_temperature(rt_base_t pin) |
| 165 | +{ |
| 166 | + static int32_t temOLD = 0; |
| 167 | + uint8_t humi=0, temp = 0; |
| 168 | + int32_t temNEW; |
| 169 | + |
| 170 | + dht11_read_Data(pin, &temp, &humi); |
| 171 | + |
| 172 | + temNEW = (humi << 16)|(temp<<0); |
| 173 | + |
| 174 | + if((temNEW != temOLD) && (temNEW !=0)) |
| 175 | + { |
| 176 | + temOLD = temNEW; |
| 177 | + } |
| 178 | + return temOLD; |
| 179 | +} |
| 180 | + |
| 181 | +static rt_size_t dht11_polling_get_data(rt_sensor_t sensor, struct rt_sensor_data *data) |
| 182 | +{ |
| 183 | + rt_int32_t temperature_humidity; |
| 184 | + temperature_humidity = dht11_get_temperature((rt_base_t)sensor->config.intf.user_data); |
| 185 | + data->data.temp = temperature_humidity; |
| 186 | + data->timestamp = rt_sensor_get_ts(); |
| 187 | + return 1; |
| 188 | +} |
| 189 | + |
| 190 | +static rt_size_t dht11_fetch_data(struct rt_sensor_device *sensor, void *buf, rt_size_t len) |
| 191 | +{ |
| 192 | + RT_ASSERT(buf); |
| 193 | + |
| 194 | + if (sensor->config.mode == RT_SENSOR_MODE_POLLING) |
| 195 | + { |
| 196 | + return dht11_polling_get_data(sensor, buf); |
| 197 | + } |
| 198 | + |
| 199 | + return 0; |
| 200 | +} |
| 201 | + |
| 202 | +static rt_err_t dht11_control(struct rt_sensor_device *sensor, int cmd, void *args) |
| 203 | +{ |
| 204 | + return RT_EOK; |
| 205 | +} |
| 206 | + |
| 207 | +static struct rt_sensor_ops sensor_ops = |
| 208 | +{ |
| 209 | + dht11_fetch_data, |
| 210 | + dht11_control |
| 211 | +}; |
| 212 | + |
| 213 | +static struct rt_sensor_device dht11_dev; |
| 214 | +int rt_hw_dht11_init(const char *name, struct rt_sensor_config *cfg) |
| 215 | +{ |
| 216 | + rt_err_t result = RT_EOK; |
| 217 | + rt_sensor_t sensor = &dht11_dev; |
| 218 | + |
| 219 | + rt_memset(sensor, 0x0, sizeof(struct rt_sensor_device)); |
| 220 | + |
| 221 | + if (!dht11_init((rt_base_t)cfg->intf.user_data)) |
| 222 | + { |
| 223 | + sensor->module = rt_calloc(1, sizeof(struct rt_sensor_module)); |
| 224 | + if (sensor->module == RT_NULL) |
| 225 | + { |
| 226 | + LOG_E("Memory error."); |
| 227 | + result = -RT_ENOMEM; |
| 228 | + goto __exit; |
| 229 | + } |
| 230 | + |
| 231 | + sensor->info.type = RT_SENSOR_CLASS_TEMP ; |
| 232 | + sensor->info.vendor = RT_SENSOR_VENDOR_DALLAS; |
| 233 | + sensor->info.model = "DHT11"; |
| 234 | + sensor->info.unit = RT_SENSOR_UNIT_DCELSIUS; |
| 235 | + sensor->info.intf_type = RT_SENSOR_INTF_ONEWIRE; |
| 236 | + sensor->info.range_max = SENSOR_TEMP_RANGE_MAX; |
| 237 | + sensor->info.range_min = SENSOR_TEMP_RANGE_MIN; |
| 238 | + sensor->info.period_min = 100; /* Read ten times in 1 second */ |
| 239 | + |
| 240 | + sensor->config = *cfg; |
| 241 | + sensor->ops = &sensor_ops; |
| 242 | + |
| 243 | + /* dht11 sensor register */ |
| 244 | + result = rt_hw_sensor_register(sensor, name, RT_DEVICE_FLAG_RDONLY, RT_NULL); |
| 245 | + if (result != RT_EOK) |
| 246 | + { |
| 247 | + LOG_E("device register err code: %d", result); |
| 248 | + goto __exit; |
| 249 | + } |
| 250 | + } |
| 251 | + else |
| 252 | + { |
| 253 | + LOG_E("dht11 init failed"); |
| 254 | + result = -RT_ERROR; |
| 255 | + goto __exit; |
| 256 | + } |
| 257 | + return RT_EOK; |
| 258 | + |
| 259 | +__exit: |
| 260 | + if (sensor->module) |
| 261 | + rt_free(sensor->module); |
| 262 | + return result; |
| 263 | +} |
0 commit comments