Added AHT10 demo.

This commit is contained in:
imi415 2022-06-30 23:00:17 +08:00
parent 418dfc4050
commit 0114304002
Signed by: imi415
GPG Key ID: 885EC2B5A8A6F8A7
5 changed files with 161 additions and 4 deletions

View File

@ -3,4 +3,5 @@ cmake_minimum_required(VERSION 3.10)
project(usbsio_demos)
add_subdirectory(libusbsio)
add_subdirectory(demos/i2cdetect)
add_subdirectory(demos/i2cdetect)
add_subdirectory(demos/dht_sensor)

View File

@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.10)
project(usbsio_dhtsensor)
set(USBSIO_DHT_SRCS
"src/main.c"
)
add_executable(${PROJECT_NAME} ${USBSIO_DHT_SRCS})
target_link_libraries(${PROJECT_NAME} PRIVATE "usbsio")

146
demos/dht_sensor/src/main.c Normal file
View File

@ -0,0 +1,146 @@
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/* USBSIO header */
#include <lpcusbsio.h>
#define NXP_VID 0x1FC9
#define MCULINK_PID 0x0143
static bool s_running = true;
static int aht10_measure(LPC_HANDLE handle, uint8_t slave_addr, double *temp, double *hum);
static void signal_handler(int sig);
int main(int argc, const char *argv) {
int ret;
LPC_HANDLE dev_handle;
LPC_HANDLE i2c_handle;
fprintf(stdout, "USBSIO example - AHT10\n");
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
/* Find all SIO devices. */
ret = LPCUSBSIO_GetNumPorts(NXP_VID, MCULINK_PID);
if (ret == 0) {
fprintf(stderr, "No MCU-Link devices found.\n");
return -1;
}
/* Open device */
dev_handle = LPCUSBSIO_Open(0); /* For now, open the first device available. */
if (dev_handle == NULL) {
fprintf(stderr, "Failed to open device #0\n");
return -2;
}
fprintf(stdout, "%s\n", LPCUSBSIO_GetVersion(dev_handle));
I2C_PORTCONFIG_T i2c_cfg = {
.ClockRate = I2C_CLOCK_FAST_MODE,
.Options = 0U,
};
i2c_handle = I2C_Open(dev_handle, &i2c_cfg, 0);
double temperature = 0.00;
double humidity = 0.00;
while (s_running) {
int ret = aht10_measure(i2c_handle, 0x38, &temperature, &humidity);
if(ret < 0) {
fprintf(stderr, "Sensor communication error.\n");
}
else {
fprintf(stdout, "Temperature: %.2fC, humidity: %.2f%%\n", temperature, humidity);
}
usleep(100 * 1000);
}
}
static int aht10_measure(LPC_HANDLE handle, uint8_t slave_addr, double *temp, double *hum) {
uint8_t tx_buffer[3] = {0x71, 0x00, 0x00};
uint8_t rx_buffer[6];
I2C_FAST_XFER_T xfer = {
.slaveAddr = slave_addr,
.txSz = 1U,
.txBuff = tx_buffer,
.rxSz = 1U,
.rxBuff = rx_buffer,
.options = 0U,
};
usleep(40 * 1000);
int ret = I2C_FastXfer(handle, &xfer);
if (ret < 0) {
I2C_Reset(handle);
return ret;
}
if ((rx_buffer[0] & 0x08) == 0U) {
/* BIT3 indicates AHT10 not calibrated, send initialization command */
tx_buffer[0] = 0xE1;
tx_buffer[1] = 0x08;
tx_buffer[2] = 0x00;
xfer.txSz = 0x03;
xfer.rxSz = 0x00;
ret = I2C_FastXfer(handle, &xfer);
if (ret < 0) {
I2C_Reset(handle);
return ret;
}
usleep(40 * 1000);
}
tx_buffer[0] = 0xAC;
tx_buffer[1] = 0x33;
tx_buffer[2] = 0x00;
xfer.txSz = 0x03;
xfer.rxSz = 0x00;
ret = I2C_FastXfer(handle, &xfer);
if (ret < 0) {
I2C_Reset(handle);
return ret;
}
usleep(80 * 1000);
tx_buffer[0] = 0x71;
xfer.txSz = 0x01;
xfer.rxSz = 0x06;
ret = I2C_FastXfer(handle, &xfer);
if (ret < 0) {
I2C_Reset(handle);
return ret;
}
uint32_t humid_raw = rx_buffer[1] << 12U;
humid_raw |= rx_buffer[2] << 4U;
humid_raw |= rx_buffer[3] >> 4U;
uint32_t temp_raw = (rx_buffer[3] & 0x0FU) << 16U;
temp_raw |= rx_buffer[4] << 8U;
temp_raw |= rx_buffer[5];
*hum = (double)humid_raw * 100.0 / 1048576.0;
*temp = ((double)temp_raw * 200.0 / 1048576.0) - 50.0;
return ret;
}
static void signal_handler(int sig) { s_running = false; }

View File

@ -57,8 +57,8 @@ static int i2c_detect(LPC_HANDLE handle, uint8_t port_num, uint8_t start_addr, u
enum I2C_ClockRate_t rate) {
fprintf(stdout, "WARNING! This program can confuse your I2C bus, cause data loss and worse!\n");
fprintf(stdout,
"WARNING! Current MCU-Link firmware has a bug which causes device to be unresponsive\n"
" if I2C bus timeout due to lack of external pull-up resistors.\n"
"WARNING! MCU-Link firmware under a specific version has a bug which\n"
" causes device to be unresponsive if this application exits abnormally.\n"
" In case that happens, don't panic, just re-plug the MCU-Link\n"
" and the device will be available again.\n");

@ -1 +1 @@
Subproject commit 60907768d02c589a6846cf5f03480acda45eb8ee
Subproject commit ea9afb6c50c81d766b14004ef9f0a3c1283482a5