[Add] AHT10驱动 测试程序

1. 添加测试程序 app_th
This commit is contained in:
gaoyang3513
2026-04-28 20:43:21 +08:00
parent 8347ce25fa
commit 1e9ad4c8bb
11 changed files with 185 additions and 0 deletions

3
src/apps/Makefile Normal file
View File

@ -0,0 +1,3 @@
LIST=apps
include recurse.mk

9
src/apps/sensor/Makefile Normal file
View File

@ -0,0 +1,9 @@
LIST=sensor
ifndef QRECURSE
QRECURSE=recurse.mk
ifdef QCONFIG
QRDIR=$(dir $(QCONFIG))
endif
endif
include $(QRDIR)$(QRECURSE)

View File

@ -0,0 +1,9 @@
LIST=VARIANT
ifndef QRECURSE
QRECURSE=recurse.mk
ifdef QCONFIG
QRDIR=$(dir $(QCONFIG))
endif
endif
include $(QRDIR)$(QRECURSE)

View File

@ -0,0 +1,2 @@
include ../../extra.mk
include ../../common.mk

14
src/apps/sensor/common.mk Normal file
View File

@ -0,0 +1,14 @@
ifndef QCONFIG
QCONFIG=qconfig.mk
endif
include $(QCONFIG)
include $(MKFILES_ROOT)/qmacros.mk
NAME=app_th
USE_INSTALL_ROOT = 1
INSTALL_ROOT_nto = $(PROJECT_ROOT)/../../../../install
INSTALLDIR = sbin
#This has to be included last
include $(MKFILES_ROOT)/qtargets.mk

2
src/apps/sensor/extra.mk Normal file
View File

@ -0,0 +1,2 @@
EXTRA_INCVPATH = $(PROJECT_ROOT)/include
EXTRA_SRCVPATH += $(PROJECT_ROOT)/utils

View File

@ -0,0 +1,7 @@
#ifndef __UTILS_TH_H__
#define __UTILS_TH_H__
int humi_get(int chnl, float *humi);
int temp_get(int chnl, float *temperature);
#endif /* __UTILS_TH_H__ */

43
src/apps/sensor/main.c Normal file
View File

@ -0,0 +1,43 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <hw/i2c.h>
#include <devctl.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <errno.h>
#include <sys/iofunc.h>
#include <sys/neutrino.h>
#include "bsp_th.h"
#include "utils_th.h"
int main()
{
int ret = 0, fd_th = 0;
float temp = 0.0f, humi = 0.0f;
while(1) {
ret = temp_get(0, &temp);
if (ret < 0) {
printf("[%12s|%4u] ErrNo(%d), failed to read data from AHT1x\n", __FILE_NAME__, __LINE__, ret);
return ret;
}
ret = humi_get(0, &humi);
if (ret < 0) {
printf("[%12s|%4u] ErrNo(%d), failed to read data from AHT1x\n", __FILE_NAME__, __LINE__, ret);
return ret;
}
printf("Temperature: %2.3f ℃\n", temp / 1000.0f);
printf("Humidity : %2.3f %%rH\n", humi / 1000.0f);
sleep(1); // wait 500ms before next read
}
close(fd_th);
return 0;
}

View File

@ -0,0 +1,22 @@
#ifndef __BSP_TH_H__
#define __BSP_TH_H__
#include <stdint.h>
#include <devctl.h>
enum th_pwr {
TH_PWR_OFF = 0,
TH_PWR_ON,
TH_PWR_RESET,
TH_PWR_MAX
};
struct th_data {
uint32_t temperature;
uint32_t humidity;
};
#define DCMD_TH_SET_PWR __DIOT (_DCMD_MIXER, 0x00, enum th_pwr)
#define DCMD_TH_GET_DATA __DIOTF(_DCMD_MIXER, 0x01, struct th_data)
#endif /* __BSP_TH_H__ */

View File

@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <hw/i2c.h>
#include <devctl.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <errno.h>
#include <sys/iofunc.h>
#include <sys/neutrino.h>
#include "bsp_th.h"
int humi_get(int chnl, float *humi)
{
int ret = 0, fd_th = 0;
struct th_data th_data;
fd_th = open ("/dev/th0", O_RDWR);
if (fd_th < 0) {
printf("ErrNo(%d) %s, failed to open I2C device\n", errno, strerror(errno));
return -1;
}
ret = devctl(fd_th, DCMD_TH_GET_DATA, &th_data, sizeof(th_data), NULL);
if (ret < 0) {
printf("[%12s|%4u] ErrNo(%d), failed to read data from AHT1x\n", __FILE_NAME__, __LINE__, ret);
return ret;
}
close(fd_th);
return 0;
}

View File

@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <hw/i2c.h>
#include <devctl.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <errno.h>
#include <sys/iofunc.h>
#include <sys/neutrino.h>
#include "bsp_th.h"
int temp_get(int chnl, float *temperature)
{
int ret = 0, fd_th = 0;
struct th_data th_data;
fd_th = open ("/dev/th0", O_RDWR);
if (fd_th < 0) {
printf("ErrNo(%d) %s, failed to open I2C device\n", errno, strerror(errno));
return -1;
}
ret = devctl(fd_th, DCMD_TH_GET_DATA, &th_data, sizeof(th_data), NULL);
if (ret < 0) {
printf("[%12s|%4u] ErrNo(%d), failed to read data from AHT1x\n", __FILE_NAME__, __LINE__, ret);
return ret;
}
close(fd_th);
return 0;
}