From 475018fa9b0a600bccf2a986d34fee3d82347793 Mon Sep 17 00:00:00 2001 From: kurisaw <2053731441@qq.com> Date: Tue, 2 Jul 2024 16:39:23 +0800 Subject: [PATCH 1/6] sensor mode separation --- SConscript | 5 +- dht11_sample.c | 38 ++++++++- rt_hw_dht11.c | 179 ++++++++++++++++++++++++++++++++++++++++++ rt_hw_dht11.h | 26 ++++++ sensor_dallas_dht11.c | 161 +------------------------------------ sensor_dallas_dht11.h | 23 +++--- 6 files changed, 260 insertions(+), 172 deletions(-) create mode 100644 rt_hw_dht11.c create mode 100644 rt_hw_dht11.h diff --git a/SConscript b/SConscript index 23706ce..ec4877a 100644 --- a/SConscript +++ b/SConscript @@ -6,10 +6,13 @@ cwd = GetCurrentDir() src = [] inc = [cwd] -src += ['sensor_dallas_dht11.c'] +src += ['rt_hw_dht11.c'] if GetDepend(['PKG_USING_DHT11_SAMPLE']): src += ['dht11_sample.c'] +if GetDepend('PKG_DHT11_USING_SENSOR_V1'): + src += ['sensor_dallas_dht11.c'] + group = DefineGroup('dht11', src, depend = ['PKG_USING_DHT11'], CPPPATH = inc) Return('group') diff --git a/dht11_sample.c b/dht11_sample.c index f95339a..e8eb162 100644 --- a/dht11_sample.c +++ b/dht11_sample.c @@ -1,25 +1,42 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2024, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: - * Date Author Notes + * Date Author Notes * 2019-08-01 LuoGong the first version. * 2019-08-15 MurphyZhao add lock and modify code style + * 2024-07-02 KurisaW sensor mode separation + * */ #include #include -#include "sensor.h" #include "sensor_dallas_dht11.h" -#include "drv_gpio.h" +#include "rt_hw_dht11.h" +#include /* Modify this pin according to the actual wiring situation */ #define DHT11_DATA_PIN GET_PIN(B, 12) static void read_temp_entry(void *parameter) { +#ifndef RT_USING_SENSOR + rt_uint8_t ret; + rt_uint8_t humidity, temp; + + while (1) + { + ret = dht11_read_Data(DHT11_DATA_PIN, &temp, &humidity); + if (ret == 0) + { + rt_kprintf("Temperature: %dC, Humidity: %d%%\n", temp, humidity); + } + + rt_thread_mdelay(2000); + } +#else rt_device_t dev = RT_NULL; struct rt_sensor_data sensor_data; rt_size_t res; @@ -61,6 +78,7 @@ static void read_temp_entry(void *parameter) rt_thread_delay(1000); } +#endif } static int dht11_read_temp_sample(void) @@ -84,11 +102,23 @@ INIT_APP_EXPORT(dht11_read_temp_sample); static int rt_hw_dht11_port(void) { +#ifndef RT_USING_SENSOR + rt_uint8_t ret; + ret = dht11_init(DHT11_DATA_PIN); + if (ret != 0) + { + rt_kprintf("DHT11 init failed!\n"); + return 1; + } + return 0; +#else struct rt_sensor_config cfg; cfg.intf.user_data = (void *)DHT11_DATA_PIN; rt_hw_dht11_init("dht11", &cfg); return RT_EOK; +#endif } INIT_COMPONENT_EXPORT(rt_hw_dht11_port); + diff --git a/rt_hw_dht11.c b/rt_hw_dht11.c new file mode 100644 index 0000000..1baf27e --- /dev/null +++ b/rt_hw_dht11.c @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2006-2024, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-08-01 LuoGong the first version. + * 2019-08-15 MurphyZhao add lock and modify code style + * 2024-07-02 KurisaW sensor mode separation + * + */ + +#include +#include +#include "board.h" +#include +#include "rt_hw_dht11.h" + +#define SENSOR_DEBUG +#define DBG_TAG "sensor.dht11" + +#ifdef SENSOR_DEBUG +#define DBG_LVL DBG_LOG +#else +#define DBG_LVL DBG_ERROR +#endif /* SENSOR_DEBUG */ +#include + +#ifndef RT_USING_PIN +#error "Please enable RT_USING_PIN" +#endif + +#ifndef rt_hw_us_delay +rt_weak void rt_hw_us_delay(rt_uint32_t us) +{ + rt_uint32_t delta; + + us = us * (SysTick->LOAD / (1000000 / RT_TICK_PER_SECOND)); + delta = SysTick->VAL; + + while (delta - SysTick->VAL < us) continue; +} +#endif + +static void dht11_reset(rt_base_t pin) +{ + rt_pin_mode(pin, PIN_MODE_OUTPUT); + + rt_pin_write(pin, PIN_LOW); + rt_thread_mdelay(20); /* 20ms */ + + rt_pin_write(pin, PIN_HIGH); + rt_hw_us_delay(30); /* 30us*/ +} + +static uint8_t dht11_check(rt_base_t pin) +{ + uint8_t retry = 0; + rt_pin_mode(pin, PIN_MODE_INPUT); + + while (rt_pin_read(pin) && retry < 100) + { + retry++; + rt_hw_us_delay(1); + } + + if(retry >= 100) + { + return CONNECT_FAILED; + } + + retry = 0; + while (!rt_pin_read(pin) && retry < 100) + { + retry++; + rt_hw_us_delay(1); + }; + + if(retry >= 100) + { + return CONNECT_FAILED; + } + + return CONNECT_SUCCESS; +} + +static uint8_t dht11_read_bit(rt_base_t pin) +{ + uint8_t retry = 0; + while (rt_pin_read(pin) && retry < 100) + { + retry++; + rt_hw_us_delay(1); + } + retry = 0; + + while (!rt_pin_read(pin) && retry < 100) + { + retry++; + rt_hw_us_delay(1); + } + + rt_hw_us_delay(40); + if(rt_pin_read(pin)) + return 1; + return 0; +} + +static uint8_t dht11_read_byte(rt_base_t pin) +{ + uint8_t i, dat = 0; + + for (i = 1; i <= 8; i++) + { + dat <<= 1; + dat |= dht11_read_bit(pin); + } + + return dat; +} + +uint8_t dht11_read_Data(rt_base_t pin,uint8_t *temp,uint8_t *humi) +{ + uint8_t i, buf[5]; + dht11_reset(pin); + + if(dht11_check(pin) == 0) + { + for(i=0; i<5; i++) /* read 40 bits */ + { + buf[i] = dht11_read_byte(pin); + } + + if((buf[0] + buf[1] + buf[2] + buf[3]) == buf[4]) + { + *humi = buf[0]; + *temp = buf[2]; + } + } + else + { + return 1; + } + + return 0; +} + +uint8_t dht11_init(rt_base_t pin) +{ + uint8_t ret = 0; + + dht11_reset(pin); + ret = dht11_check(pin); + if (ret != 0) + { + dht11_reset(pin); + ret = dht11_check(pin); + } + + return ret; +} + +int32_t dht11_get_temperature(rt_base_t pin) +{ + static int32_t temOLD = 0; + uint8_t humi=0, temp = 0; + int32_t temNEW; + + dht11_read_Data(pin, &temp, &humi); + + temNEW = (humi << 16)|(temp<<0); + + if((temNEW != temOLD) && (temNEW !=0)) + { + temOLD = temNEW; + } + return temOLD; +} \ No newline at end of file diff --git a/rt_hw_dht11.h b/rt_hw_dht11.h new file mode 100644 index 0000000..9b93f3e --- /dev/null +++ b/rt_hw_dht11.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2006-2024, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-08-01 LuoGong the first version. + * 2019-08-15 MurphyZhao add lock and modify code style + * 2024-07-02 KurisaW sensor mode separation + * + */ + +#ifndef __RT_HW_DHT11_H__ +#define __RT_HW_DHT11_H__ + +#include + +#define CONNECT_SUCCESS 0 +#define CONNECT_FAILED 1 + +uint8_t dht11_init(rt_base_t pin); +uint8_t dht11_read_Data(rt_base_t pin,uint8_t *temp,uint8_t *humi); +int32_t dht11_get_temperature(rt_base_t pin); + +#endif /* __RT_HW_DHT11_H__ */ \ No newline at end of file diff --git a/sensor_dallas_dht11.c b/sensor_dallas_dht11.c index 74c40a6..4a756f9 100644 --- a/sensor_dallas_dht11.c +++ b/sensor_dallas_dht11.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2024, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -7,15 +7,12 @@ * Date Author Notes * 2019-08-01 LuoGong the first version. * 2019-08-15 MurphyZhao add lock and modify code style + * 2024-07-02 KurisaW sensor mode separation * */ +#include "rt_hw_dht11.h" #include "sensor_dallas_dht11.h" -#include -#include -#include "sensor.h" -#include "board.h" -#include #define SENSOR_DEBUG #define DBG_TAG "sensor.dht11" @@ -32,161 +29,10 @@ #define SENSOR_HUMI_RANGE_MAX (100) #define SENSOR_HUMI_RANGE_MIN (0) -#ifndef RT_USING_PIN -#error "Please enable RT_USING_PIN" -#endif - #ifndef RT_SENSOR_VENDOR_DALLAS #define RT_SENSOR_VENDOR_DALLAS (7u) #endif -#ifndef rt_hw_us_delay -RT_WEAK void rt_hw_us_delay(rt_uint32_t us) -{ - rt_uint32_t delta; - - us = us * (SysTick->LOAD / (1000000 / RT_TICK_PER_SECOND)); - delta = SysTick->VAL; - - while (delta - SysTick->VAL < us) continue; -} -#endif - -static void dht11_reset(rt_base_t pin) -{ - rt_pin_mode(pin, PIN_MODE_OUTPUT); - - rt_pin_write(pin, PIN_LOW); - rt_thread_mdelay(20); /* 20ms */ - - rt_pin_write(pin, PIN_HIGH); - rt_hw_us_delay(30); /* 30us*/ -} - -static uint8_t dht11_check(rt_base_t pin) -{ - uint8_t retry = 0; - rt_pin_mode(pin, PIN_MODE_INPUT); - - while (rt_pin_read(pin) && retry < 100) - { - retry++; - rt_hw_us_delay(1); - } - - if(retry >= 100) - { - return CONNECT_FAILED; - } - - retry = 0; - while (!rt_pin_read(pin) && retry < 100) - { - retry++; - rt_hw_us_delay(1); - }; - - if(retry >= 100) - { - return CONNECT_FAILED; - } - - return CONNECT_SUCCESS; -} - -static uint8_t dht11_read_bit(rt_base_t pin) -{ - uint8_t retry = 0; - while (rt_pin_read(pin) && retry < 100) - { - retry++; - rt_hw_us_delay(1); - } - retry = 0; - - while (!rt_pin_read(pin) && retry < 100) - { - retry++; - rt_hw_us_delay(1); - } - - rt_hw_us_delay(40); - if(rt_pin_read(pin)) - return 1; - return 0; -} - -static uint8_t dht11_read_byte(rt_base_t pin) -{ - uint8_t i, dat = 0; - - for (i = 1; i <= 8; i++) - { - dat <<= 1; - dat |= dht11_read_bit(pin); - } - - return dat; -} - -static uint8_t dht11_read_Data(rt_base_t pin,uint8_t *temp,uint8_t *humi) -{ - uint8_t i, buf[5]; - dht11_reset(pin); - - if(dht11_check(pin) == 0) - { - for(i=0; i<5; i++) /* read 40 bits */ - { - buf[i] = dht11_read_byte(pin); - } - - if((buf[0] + buf[1] + buf[2] + buf[3]) == buf[4]) - { - *humi = buf[0]; - *temp = buf[2]; - } - } - else - { - return 1; - } - - return 0; -} - -uint8_t dht11_init(rt_base_t pin) -{ - uint8_t ret = 0; - - dht11_reset(pin); - ret = dht11_check(pin); - if (ret != 0) - { - dht11_reset(pin); - ret = dht11_check(pin); - } - - return ret; -} - -int32_t dht11_get_temperature(rt_base_t pin) -{ - static int32_t temOLD = 0; - uint8_t humi=0, temp = 0; - int32_t temNEW; - - dht11_read_Data(pin, &temp, &humi); - - temNEW = (humi << 16)|(temp<<0); - - if((temNEW != temOLD) && (temNEW !=0)) - { - temOLD = temNEW; - } - return temOLD; -} - static rt_size_t dht11_polling_get_data(rt_sensor_t sensor, struct rt_sensor_data *data) { rt_int32_t temperature_humidity; @@ -220,6 +66,7 @@ static struct rt_sensor_ops sensor_ops = }; static struct rt_sensor_device dht11_dev; + int rt_hw_dht11_init(const char *name, struct rt_sensor_config *cfg) { rt_err_t result = RT_EOK; diff --git a/sensor_dallas_dht11.h b/sensor_dallas_dht11.h index 917a9e7..42c8c69 100644 --- a/sensor_dallas_dht11.h +++ b/sensor_dallas_dht11.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2024, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -7,17 +7,22 @@ * Date Author Notes * 2019-08-01 LuoGong the first version. * 2019-08-15 MurphyZhao add lock and modify code style + * 2024-07-02 KurisaW sensor mode separation * */ -#ifndef __DHT11_H__ -#define __DHT11_H__ +#ifndef __SENSOR_DALLAS_DHT11_H__ +#define __SENSOR_DALLAS_DHT11_H__ #include -#include "sensor.h" -#define CONNECT_SUCCESS 0 -#define CONNECT_FAILED 1 +#ifdef RT_USING_SENSOR + +#if RT_VER_NUM >= 0x50000 + #define rt_size_t rt_ssize_t +#endif + +#include "drivers/sensor.h" struct dht11_device { @@ -26,10 +31,8 @@ struct dht11_device }; typedef struct dht11_device *dht11_device_t; -uint8_t dht11_init(rt_base_t pin); -int32_t dht11_get_temperature(rt_base_t pin); int rt_hw_dht11_init(const char *name, struct rt_sensor_config *cfg); -#endif /* __DS18B20_H_ */ - +#endif +#endif /* __SENSOR_DALLAS_DHT11_H__ */ From 3bda169ffaaafb45d31e6bb356307c8a6d419268 Mon Sep 17 00:00:00 2001 From: kurisaw <2053731441@qq.com> Date: Tue, 2 Jul 2024 16:54:54 +0800 Subject: [PATCH 2/6] version adaptation --- sensor_dallas_dht11.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sensor_dallas_dht11.h b/sensor_dallas_dht11.h index 42c8c69..d02355c 100644 --- a/sensor_dallas_dht11.h +++ b/sensor_dallas_dht11.h @@ -19,11 +19,12 @@ #ifdef RT_USING_SENSOR #if RT_VER_NUM >= 0x50000 + #include "drivers/sensor.h" #define rt_size_t rt_ssize_t +#else + #include "sensor.h" #endif -#include "drivers/sensor.h" - struct dht11_device { rt_base_t pin; From a46a0cae024b1c4be2bf37efbebfbc3b2e808924 Mon Sep 17 00:00:00 2001 From: kurisaw <2053731441@qq.com> Date: Fri, 6 Sep 2024 11:51:01 +0800 Subject: [PATCH 3/6] Specification version specification --- sensor_dallas_dht11.c | 4 ++-- sensor_dallas_dht11.h | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/sensor_dallas_dht11.c b/sensor_dallas_dht11.c index 4a756f9..9b34ae7 100644 --- a/sensor_dallas_dht11.c +++ b/sensor_dallas_dht11.c @@ -33,7 +33,7 @@ #define RT_SENSOR_VENDOR_DALLAS (7u) #endif -static rt_size_t dht11_polling_get_data(rt_sensor_t sensor, struct rt_sensor_data *data) +static RT_SIZE_TYPE dht11_polling_get_data(rt_sensor_t sensor, struct rt_sensor_data *data) { rt_int32_t temperature_humidity; temperature_humidity = dht11_get_temperature((rt_base_t)sensor->config.intf.user_data); @@ -42,7 +42,7 @@ static rt_size_t dht11_polling_get_data(rt_sensor_t sensor, struct rt_sensor_dat return 1; } -static rt_size_t dht11_fetch_data(struct rt_sensor_device *sensor, void *buf, rt_size_t len) +static RT_SIZE_TYPE dht11_fetch_data(struct rt_sensor_device *sensor, void *buf, rt_size_t len) { RT_ASSERT(buf); diff --git a/sensor_dallas_dht11.h b/sensor_dallas_dht11.h index d02355c..aca5815 100644 --- a/sensor_dallas_dht11.h +++ b/sensor_dallas_dht11.h @@ -18,11 +18,12 @@ #ifdef RT_USING_SENSOR -#if RT_VER_NUM >= 0x50000 +#if defined(RT_VERSION_CHECK) && (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 0, 1)) #include "drivers/sensor.h" - #define rt_size_t rt_ssize_t + #define RT_SIZE_TYPE rt_ssize_t #else #include "sensor.h" + #define RT_SIZE_TYPE rt_size_t #endif struct dht11_device From 3ef7cd719d2d95f8e6151fd46243246ac70b4ede Mon Sep 17 00:00:00 2001 From: kurisaw <2053731441@qq.com> Date: Fri, 6 Sep 2024 13:11:35 +0800 Subject: [PATCH 4/6] Optimized version macro definition --- sensor_dallas_dht11.h | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/sensor_dallas_dht11.h b/sensor_dallas_dht11.h index aca5815..23c1a05 100644 --- a/sensor_dallas_dht11.h +++ b/sensor_dallas_dht11.h @@ -18,12 +18,18 @@ #ifdef RT_USING_SENSOR -#if defined(RT_VERSION_CHECK) && (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 0, 1)) - #include "drivers/sensor.h" - #define RT_SIZE_TYPE rt_ssize_t -#else - #include "sensor.h" - #define RT_SIZE_TYPE rt_size_t +#if defined(RT_VERSION_CHECK) + #if (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 0, 2)) + #define RT_SIZE_TYPE rt_ssize_t + #else + #define RT_SIZE_TYPE rt_size_t + #endif + + #if (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 1, 0)) + #include "drivers/sensor.h" + #else + #include "sensor.h" + #endif #endif struct dht11_device From 28dca1a1de3c677b3c1ce7ad450668ba1c17cf6c Mon Sep 17 00:00:00 2001 From: Yuqiang Wang Date: Wed, 11 Sep 2024 09:22:32 +0800 Subject: [PATCH 5/6] Update sensor_dallas_dht11.h --- sensor_dallas_dht11.h | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/sensor_dallas_dht11.h b/sensor_dallas_dht11.h index 23c1a05..35defb1 100644 --- a/sensor_dallas_dht11.h +++ b/sensor_dallas_dht11.h @@ -15,6 +15,7 @@ #define __SENSOR_DALLAS_DHT11_H__ #include +#include #ifdef RT_USING_SENSOR @@ -24,12 +25,6 @@ #else #define RT_SIZE_TYPE rt_size_t #endif - - #if (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 1, 0)) - #include "drivers/sensor.h" - #else - #include "sensor.h" - #endif #endif struct dht11_device From 443734110508b4f52781302c5cf535b7fb5e3c5a Mon Sep 17 00:00:00 2001 From: Yuqiang Wang Date: Wed, 11 Sep 2024 09:23:32 +0800 Subject: [PATCH 6/6] Update sensor_dallas_dht11.h --- sensor_dallas_dht11.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/sensor_dallas_dht11.h b/sensor_dallas_dht11.h index 35defb1..c7b4e55 100644 --- a/sensor_dallas_dht11.h +++ b/sensor_dallas_dht11.h @@ -17,8 +17,6 @@ #include #include -#ifdef RT_USING_SENSOR - #if defined(RT_VERSION_CHECK) #if (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 0, 2)) #define RT_SIZE_TYPE rt_ssize_t @@ -36,6 +34,4 @@ typedef struct dht11_device *dht11_device_t; int rt_hw_dht11_init(const char *name, struct rt_sensor_config *cfg); -#endif - #endif /* __SENSOR_DALLAS_DHT11_H__ */