Browse Source

传感器驱动(ds1307/ds3231)(ahtx0_th)

master
忱 沈 3 weeks ago
parent
commit
7a3b64472d
  1. 2
      .cproject
  2. 218
      applications/drv/drv_aht20.c
  3. 2
      applications/drv/drv_ds1307.c

2
.cproject

@ -1024,7 +1024,7 @@
</toolChain> </toolChain>
</folderInfo> </folderInfo>
<sourceEntries> <sourceEntries>
<entry excluding="packages/sqlite/shell.c|packages/sqlite/rtthread_vfs.c|packages/sqlite/rtthread_mutex.c|packages/sqlite/rtthread_io_methods.c|drivers/drv_ltdc.c|//cubemx/Drivers|//cubemx/EWARM|//cubemx/Src/stm32h7xx_it.c|//cubemx/Src/system_stm32h7xx.c|//packages/LVGL-v8.3.11/env_support/rt-thread/squareline|//packages/LVGL-v8.3.11/examples|//packages/LVGL-v8.3.11/tests|//packages/littlefs-v2.11.2/runners|//packages/sqlite/rtthread_io_methods.c|//packages/sqlite/rtthread_mutex.c|//packages/sqlite/rtthread_vfs.c|//packages/sqlite/shell.c|//rt-thread/components/dfs/dfs_v1/filesystems/cromfs|//rt-thread/components/dfs/dfs_v1/filesystems/mqueue|//rt-thread/components/dfs/dfs_v1/filesystems/nfs|//rt-thread/components/dfs/dfs_v1/filesystems/ramfs|//rt-thread/components/dfs/dfs_v1/filesystems/romfs|//rt-thread/components/dfs/dfs_v1/filesystems/skeleton|//rt-thread/components/dfs/dfs_v2|//rt-thread/components/drivers/audio|//rt-thread/components/drivers/can|//rt-thread/components/drivers/clk|//rt-thread/components/drivers/core/dm.c|//rt-thread/components/drivers/core/driver.c|//rt-thread/components/drivers/core/platform.c|//rt-thread/components/drivers/core/platform_ofw.c|//rt-thread/components/drivers/cputime|//rt-thread/components/drivers/fdt|//rt-thread/components/drivers/hwcrypto|//rt-thread/components/drivers/hwtimer|//rt-thread/components/drivers/i2c/i2c_bus.c|//rt-thread/components/drivers/i2c/i2c_dm.c|//rt-thread/components/drivers/i2c/soft_i2c.c|//rt-thread/components/drivers/ktime|//rt-thread/components/drivers/misc|//rt-thread/components/drivers/mtd/mtd_nand.c|//rt-thread/components/drivers/ofw|//rt-thread/components/drivers/phy|//rt-thread/components/drivers/pic|//rt-thread/components/drivers/pin/pin_dm.c|//rt-thread/components/drivers/pin/pin_ofw.c|//rt-thread/components/drivers/pinctrl|//rt-thread/components/drivers/pm|//rt-thread/components/drivers/rtc/alarm.c|//rt-thread/components/drivers/sensor|//rt-thread/components/drivers/serial/serial_dm.c|//rt-thread/components/drivers/serial/serial_tty.c|//rt-thread/components/drivers/serial/serial_v2.c|//rt-thread/components/drivers/spi/enc28j60.c|//rt-thread/components/drivers/spi/spi-bit-ops.c|//rt-thread/components/drivers/spi/spi_wifi_rw009.c|//rt-thread/components/drivers/usb|//rt-thread/components/drivers/virtio|//rt-thread/components/drivers/wlan|//rt-thread/components/fal/samples/porting/fal_flash_stm32f2_port.c|//rt-thread/components/legacy|//rt-thread/components/libc/compilers/armlibc|//rt-thread/components/libc/compilers/dlib|//rt-thread/components/libc/compilers/musl|//rt-thread/components/libc/compilers/picolibc|//rt-thread/components/libc/cplusplus|//rt-thread/components/libc/posix|//rt-thread/components/lwp|//rt-thread/components/mm|//rt-thread/components/mprotect|//rt-thread/components/net|//rt-thread/components/utilities|//rt-thread/components/vbus|//rt-thread/libcpu/aarch64|//rt-thread/libcpu/arc|//rt-thread/libcpu/arm/AT91SAM7S|//rt-thread/libcpu/arm/AT91SAM7X|//rt-thread/libcpu/arm/am335x|//rt-thread/libcpu/arm/arm926|//rt-thread/libcpu/arm/armv6|//rt-thread/libcpu/arm/common/atomic_arm.c|//rt-thread/libcpu/arm/common/divsi3.S|//rt-thread/libcpu/arm/cortex-a|//rt-thread/libcpu/arm/cortex-m0|//rt-thread/libcpu/arm/cortex-m23|//rt-thread/libcpu/arm/cortex-m3|//rt-thread/libcpu/arm/cortex-m33|//rt-thread/libcpu/arm/cortex-m4|//rt-thread/libcpu/arm/cortex-m7/context_iar.S|//rt-thread/libcpu/arm/cortex-m7/context_rvds.S|//rt-thread/libcpu/arm/cortex-m7/mpu.c|//rt-thread/libcpu/arm/cortex-m85|//rt-thread/libcpu/arm/cortex-r4|//rt-thread/libcpu/arm/cortex-r52|//rt-thread/libcpu/arm/dm36x|//rt-thread/libcpu/arm/lpc214x|//rt-thread/libcpu/arm/lpc24xx|//rt-thread/libcpu/arm/realview-a8-vmm|//rt-thread/libcpu/arm/s3c24x0|//rt-thread/libcpu/arm/s3c44b0|//rt-thread/libcpu/arm/sep4020|//rt-thread/libcpu/arm/zynqmp-r5|//rt-thread/libcpu/avr32|//rt-thread/libcpu/blackfin|//rt-thread/libcpu/c-sky|//rt-thread/libcpu/ia32|//rt-thread/libcpu/m16c|//rt-thread/libcpu/mips|//rt-thread/libcpu/nios|//rt-thread/libcpu/ppc|//rt-thread/libcpu/risc-v|//rt-thread/libcpu/rx|//rt-thread/libcpu/sim|//rt-thread/libcpu/sparc-v8|//rt-thread/libcpu/ti-dsp|//rt-thread/libcpu/unicore32|//rt-thread/libcpu/v850|//rt-thread/libcpu/xilinx|//rt-thread/src/cpu.c|//rt-thread/src/mem.c|//rt-thread/src/scheduler_mp.c|//rt-thread/src/slab.c|//rt-thread/tools" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="" /> <entry excluding="//rt-thread/components/dfs/dfs_v1/filesystems/romfs|//rt-thread/src/slab.c|//rt-thread/libcpu/m16c|//rt-thread/libcpu/arm/arm926|//rt-thread/libcpu/arm/cortex-a|//rt-thread/libcpu/arc|//rt-thread/libcpu/arm/common/divsi3.S|//rt-thread/components/drivers/pinctrl|//rt-thread/components/libc/compilers/armlibc|//rt-thread/libcpu/arm/s3c44b0|//rt-thread/components/drivers/serial/serial_tty.c|//rt-thread/components/dfs/dfs_v1/filesystems/mqueue|//rt-thread/libcpu/c-sky|//rt-thread/libcpu/avr32|//rt-thread/components/drivers/ktime|//rt-thread/components/drivers/rtc/alarm.c|//cubemx/EWARM|//rt-thread/components/drivers/sensor|//rt-thread/libcpu/arm/am335x|//rt-thread/components/dfs/dfs_v1/filesystems/cromfs|//rt-thread/components/drivers/can|//rt-thread/components/drivers/pin/pin_dm.c|//rt-thread/components/libc/compilers/picolibc|//rt-thread/components/legacy|//rt-thread/components/drivers/wlan|//rt-thread/libcpu/arm/cortex-r4|//rt-thread/libcpu/mips|//rt-thread/src/cpu.c|//rt-thread/components/drivers/ofw|//rt-thread/libcpu/arm/sep4020|packages/sqlite/rtthread_io_methods.c|//packages/sqlite/rtthread_io_methods.c|//rt-thread/libcpu/nios|//rt-thread/components/lwp|//rt-thread/libcpu/arm/dm36x|//rt-thread/components/drivers/misc|//rt-thread/components/drivers/pic|//rt-thread/components/drivers/mtd/mtd_nand.c|//rt-thread/components/drivers/core/driver.c|//rt-thread/libcpu/arm/cortex-m85|//rt-thread/components/libc/compilers/dlib|//rt-thread/components/dfs/dfs_v1/filesystems/skeleton|//rt-thread/components/drivers/i2c/i2c_dm.c|//rt-thread/components/drivers/serial/serial_v2.c|//rt-thread/components/mprotect|//rt-thread/libcpu/ppc|//rt-thread/components/drivers/hwcrypto|//rt-thread/libcpu/sparc-v8|//rt-thread/libcpu/unicore32|//cubemx/Src/stm32h7xx_it.c|//rt-thread/components/drivers/pm|//rt-thread/libcpu/risc-v|//rt-thread/libcpu/rx|//cubemx/Src/system_stm32h7xx.c|//packages/littlefs-v2.11.2/runners|//rt-thread/libcpu/arm/zynqmp-r5|//packages/LVGL-v8.3.11/tests|//rt-thread/libcpu/arm/cortex-m4|//rt-thread/libcpu/arm/cortex-m33|//rt-thread/src/mem.c|//rt-thread/libcpu/arm/AT91SAM7S|//rt-thread/components/libc/cplusplus|//rt-thread/libcpu/v850|//rt-thread/libcpu/xilinx|//rt-thread/libcpu/arm/common/atomic_arm.c|//rt-thread/libcpu/ia32|//rt-thread/components/drivers/fdt|//rt-thread/libcpu/arm/cortex-m23|//rt-thread/components/libc/compilers/musl|//rt-thread/components/dfs/dfs_v2|//rt-thread/components/drivers/core/dm.c|//rt-thread/components/drivers/core/platform.c|//rt-thread/components/drivers/i2c/i2c_bus.c|//rt-thread/libcpu/ti-dsp|//rt-thread/libcpu/sim|//packages/LVGL-v8.3.11/examples|//rt-thread/components/dfs/dfs_v1/filesystems/nfs|//rt-thread/components/drivers/core/platform_ofw.c|packages/sqlite/shell.c|//rt-thread/libcpu/arm/realview-a8-vmm|//packages/sqlite/shell.c|//rt-thread/components/drivers/spi/spi-bit-ops.c|//rt-thread/tools|//rt-thread/components/drivers/i2c/soft_i2c.c|//rt-thread/components/net|//rt-thread/components/utilities|drivers/drv_ltdc.c|//packages/LVGL-v8.3.11/env_support/rt-thread/squareline|//rt-thread/components/drivers/spi/enc28j60.c|//rt-thread/components/drivers/phy|//rt-thread/libcpu/arm/cortex-m0|//rt-thread/libcpu/arm/lpc24xx|//rt-thread/libcpu/arm/s3c24x0|//rt-thread/components/vbus|//rt-thread/components/drivers/hwtimer|//rt-thread/libcpu/blackfin|//rt-thread/libcpu/arm/cortex-m7/context_iar.S|//packages/sqlite/rtthread_mutex.c|packages/sqlite/rtthread_mutex.c|//rt-thread/components/drivers/spi/spi_wifi_rw009.c|//rt-thread/components/dfs/dfs_v1/filesystems/ramfs|//rt-thread/components/fal/samples/porting/fal_flash_stm32f2_port.c|//rt-thread/libcpu/arm/armv6|//rt-thread/components/drivers/audio|//rt-thread/libcpu/arm/lpc214x|//rt-thread/components/drivers/clk|//rt-thread/libcpu/arm/cortex-m7/context_rvds.S|packages/sqlite/rtthread_vfs.c|//rt-thread/components/libc/posix|//rt-thread/libcpu/arm/cortex-m7/mpu.c|//packages/sqlite/rtthread_vfs.c|//rt-thread/components/drivers/cputime|//rt-thread/components/drivers/pin/pin_ofw.c|//rt-thread/components/drivers/usb|//rt-thread/libcpu/arm/AT91SAM7X|//rt-thread/components/drivers/serial/serial_dm.c|//rt-thread/components/drivers/virtio|//rt-thread/libcpu/aarch64|//rt-thread/libcpu/arm/cortex-r52|//cubemx/Drivers|//rt-thread/src/scheduler_mp.c|//rt-thread/libcpu/arm/cortex-m3|//rt-thread/components/mm" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
</sourceEntries> </sourceEntries>
</configuration> </configuration>
</storageModule> </storageModule>

218
applications/drv/drv_aht20.c

@ -0,0 +1,218 @@
/*
* Copyright (c) 2025, Your Name
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-12-27 Developer First version for AHT20 on RT-Thread
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#define DBG_TAG "sensor.aht20"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
/* AHT20 I2C 地址 */
#define AHT20_I2C_ADDR 0x38
/* 使用的 I2C 总线设备名 */
#define AHT20_I2C_BUS_NAME "i2c2" // 根据你的板子修改,如 "i2c2"
static struct rt_i2c_bus_device *i2c_bus = RT_NULL;
/**
* AHT20
*/
static rt_err_t aht20_send_cmd(rt_uint8_t cmd, const rt_uint8_t *data, rt_size_t len)
{
rt_uint8_t buf[3] = {cmd};
if (len > 0)
{
rt_memcpy(&buf[1], data, len);
}
struct rt_i2c_msg msgs[1] = {
{
.addr = AHT20_I2C_ADDR,
.flags = RT_I2C_WR,
.buf = buf,
.len = 1 + len,
}
};
if (rt_i2c_transfer(i2c_bus, msgs, 1) != 1)
{
LOG_E("I2C send command 0x%02X failed.", cmd);
return -RT_ERROR;
}
return RT_EOK;
}
/**
* AHT20
*/
static rt_err_t aht20_read_data(rt_uint8_t *buf, rt_size_t len)
{
struct rt_i2c_msg msgs[1] = {
{
.addr = AHT20_I2C_ADDR,
.flags = RT_I2C_RD,
.buf = buf,
.len = len,
}
};
if (rt_i2c_transfer(i2c_bus, msgs, 1) != 1)
{
LOG_E("I2C read data failed.");
return -RT_ERROR;
}
return RT_EOK;
}
/**
* AHT20
*/
static rt_err_t aht20_init_sensor(void)
{
rt_uint8_t status = 0;
rt_uint8_t cmd_data[2] = {0x08, 0x00};
/* 发送初始化命令 */
if (aht20_send_cmd(0xBE, cmd_data, 2) != RT_EOK)
{
LOG_E("Failed to send init command.");
return -RT_ERROR;
}
rt_thread_mdelay(20); // 等待初始化完成
/* 读取状态字节(可选) */
if (aht20_read_data(&status, 1) == RT_EOK)
{
LOG_D("AHT20 status: 0x%02X", status);
}
return RT_EOK;
}
/**
* 湿
*/
static rt_err_t aht20_trigger_measure(void)
{
rt_uint8_t cmd[2] = {0x33, 0x00};
return aht20_send_cmd(0xAC, cmd, 2);
}
/**
* 湿6
*/
static rt_err_t aht20_fetch_raw(rt_uint8_t raw[6])
{
return aht20_read_data(raw, 6);
}
/**
* 0.1°C湿0.1%RH
*/
static void aht20_parse_data(const rt_uint8_t raw[6], rt_int32_t *temp, rt_int32_t *humi)
{
rt_uint32_t humi_raw = ((rt_uint32_t)raw[1] << 12) | ((rt_uint32_t)raw[2] << 4) | (raw[3] >> 4);
rt_uint32_t temp_raw = ((rt_uint32_t)raw[3] & 0x0F) << 16 | ((rt_uint32_t)raw[4] << 8) | raw[5];
*humi = (rt_int32_t)((humi_raw * 1000) / (1 << 20)); // 转换为 0.1%RH
*temp = (rt_int32_t)((temp_raw * 2000) / (1 << 20) - 500); // 转换为 0.1°C(公式:T = (raw / 2^20) * 200 - 50)
}
/**
* AHT20 线
*/
static void aht20_thread_entry(void *parameter)
{
rt_uint8_t raw_data[6];
rt_int32_t temperature, humidity;
while (1)
{
if (aht20_trigger_measure() != RT_EOK)
{
LOG_E("Trigger measurement failed.");
rt_thread_mdelay(2000);
continue;
}
rt_thread_mdelay(80); // AHT20 典型转换时间 75ms
if (aht20_fetch_raw(raw_data) != RT_EOK)
{
LOG_E("Fetch raw data failed.");
rt_thread_mdelay(2000);
continue;
}
/* 检查忙标志(bit7 of raw_data[0] 应为 0)*/
if (raw_data[0] & 0x80)
{
LOG_W("AHT20 is busy, retrying...");
rt_thread_mdelay(100);
continue;
}
aht20_parse_data(raw_data, &temperature, &humidity);
LOG_I("Temp: %d.%dC, Humidity: %d.%dRH",
temperature / 10, temperature % 10,
humidity / 10, humidity % 10);
rt_thread_mdelay(5000); // 每 2 秒读取一次
}
}
/**
* AHT20
*/
static int rt_hw_aht20_init(void)
{
i2c_bus = rt_i2c_bus_device_find(AHT20_I2C_BUS_NAME);
if (i2c_bus == RT_NULL)
{
LOG_E("Failed to find I2C bus: %s", AHT20_I2C_BUS_NAME);
return -RT_ENOSYS;
}
LOG_I("Found I2C bus: %s", AHT20_I2C_BUS_NAME);
if (aht20_init_sensor() != RT_EOK)
{
LOG_E("AHT20 sensor initialization failed!");
return -RT_ERROR;
}
LOG_I("AHT20 initialized successfully.");
/* 创建读取线程 */
rt_thread_t tid = rt_thread_create("ahtX0",
aht20_thread_entry,
RT_NULL,
512,
RT_THREAD_PRIORITY_MAX - 2,
10);
if (tid != RT_NULL)
{
rt_thread_startup(tid);
}
else
{
LOG_E("Failed to create AHT20 thread.");
return -RT_ERROR;
}
return RT_EOK;
}
/* 自动初始化 */
INIT_APP_EXPORT(rt_hw_aht20_init);

2
drivers/drv_ds1307.c → applications/drv/drv_ds1307.c

@ -210,7 +210,7 @@ static int rt_hw_ds1307_rtc_init(void)
ds1307_read_time(); ds1307_read_time();
ds1307.thd_soft_update_sec = rt_thread_create("ds1307",auto_update_current_datatime,RT_NULL,512, 20, 5); ds1307.thd_soft_update_sec = rt_thread_create("ds1307",auto_update_current_datatime,RT_NULL,256, 20, 5);
if (ds1307.thd_soft_update_sec) if (ds1307.thd_soft_update_sec)
{ {
rt_thread_startup(ds1307.thd_soft_update_sec); rt_thread_startup(ds1307.thd_soft_update_sec);
Loading…
Cancel
Save