From 0114304002c31696ba7919f76f1a1007dc8f4a24 Mon Sep 17 00:00:00 2001 From: imi415 Date: Thu, 30 Jun 2022 23:00:17 +0800 Subject: [PATCH] Added AHT10 demo. --- CMakeLists.txt | 3 +- demos/dht_sensor/CMakeLists.txt | 10 +++ demos/dht_sensor/src/main.c | 146 ++++++++++++++++++++++++++++++++ demos/i2cdetect/src/main.c | 4 +- libusbsio | 2 +- 5 files changed, 161 insertions(+), 4 deletions(-) create mode 100644 demos/dht_sensor/CMakeLists.txt create mode 100644 demos/dht_sensor/src/main.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 05b5f33..7eff06b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,4 +3,5 @@ cmake_minimum_required(VERSION 3.10) project(usbsio_demos) add_subdirectory(libusbsio) -add_subdirectory(demos/i2cdetect) \ No newline at end of file +add_subdirectory(demos/i2cdetect) +add_subdirectory(demos/dht_sensor) \ No newline at end of file diff --git a/demos/dht_sensor/CMakeLists.txt b/demos/dht_sensor/CMakeLists.txt new file mode 100644 index 0000000..84bc631 --- /dev/null +++ b/demos/dht_sensor/CMakeLists.txt @@ -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") \ No newline at end of file diff --git a/demos/dht_sensor/src/main.c b/demos/dht_sensor/src/main.c new file mode 100644 index 0000000..5393884 --- /dev/null +++ b/demos/dht_sensor/src/main.c @@ -0,0 +1,146 @@ +#include +#include +#include +#include +#include + +/* USBSIO header */ +#include + +#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; } \ No newline at end of file diff --git a/demos/i2cdetect/src/main.c b/demos/i2cdetect/src/main.c index 70aebdb..6b94cbf 100644 --- a/demos/i2cdetect/src/main.c +++ b/demos/i2cdetect/src/main.c @@ -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"); diff --git a/libusbsio b/libusbsio index 6090776..ea9afb6 160000 --- a/libusbsio +++ b/libusbsio @@ -1 +1 @@ -Subproject commit 60907768d02c589a6846cf5f03480acda45eb8ee +Subproject commit ea9afb6c50c81d766b14004ef9f0a3c1283482a5