From 378c8c898212d25868da4cbae35bbdaedb202d6f Mon Sep 17 00:00:00 2001 From: jeetrohan Date: Fri, 2 May 2025 23:31:46 +0530 Subject: [PATCH 1/8] feat(aht20): update to v1.1.0 with internal i2c_bus and new sensor APIs Bumped driver version from 1.0.0 to 1.1.0. Switched to internal i2c_bus component from esp-iot-solution, replacing legacy I2C code. Added new APIs to retrieve temperature and humidity. Introduced Kconfig options for CRC check and I2C clock frequency configuration. Updated README to reflect new APIs and configuration options. Changes to be committed: modified: CHANGELOG.md modified: CMakeLists.txt new file: Kconfig modified: README.md modified: aht20.c modified: idf_component.yml modified: include/aht20.h --- .../sensors/humiture/aht20/CHANGELOG.md | 3 + .../sensors/humiture/aht20/CMakeLists.txt | 11 +- components/sensors/humiture/aht20/Kconfig | 18 + components/sensors/humiture/aht20/README.md | 102 +++-- components/sensors/humiture/aht20/aht20.c | 370 ++++++++++++------ .../sensors/humiture/aht20/idf_component.yml | 33 +- .../sensors/humiture/aht20/include/aht20.h | 234 ++++++++--- 7 files changed, 566 insertions(+), 205 deletions(-) create mode 100644 components/sensors/humiture/aht20/Kconfig diff --git a/components/sensors/humiture/aht20/CHANGELOG.md b/components/sensors/humiture/aht20/CHANGELOG.md index 4c1a110ec3..00b545fc60 100644 --- a/components/sensors/humiture/aht20/CHANGELOG.md +++ b/components/sensors/humiture/aht20/CHANGELOG.md @@ -1,5 +1,8 @@ # ChangeLog +## v1.1.1 (2025-05-02) +* replace the i2c interface with i2c_bus + ## v1.0.0 (2024-08-09) * Added description of AHT30 diff --git a/components/sensors/humiture/aht20/CMakeLists.txt b/components/sensors/humiture/aht20/CMakeLists.txt index 0ebacc8d74..7c39296667 100644 --- a/components/sensors/humiture/aht20/CMakeLists.txt +++ b/components/sensors/humiture/aht20/CMakeLists.txt @@ -1,9 +1,2 @@ -idf_component_register( - SRCS "aht20.c" - INCLUDE_DIRS "include" - PRIV_INCLUDE_DIRS "priv_include" - REQUIRES "driver" -) - -include(package_manager) -cu_pkg_define_version(${CMAKE_CURRENT_LIST_DIR}) +idf_component_register(SRCS "aht20.c" + INCLUDE_DIRS "include") diff --git a/components/sensors/humiture/aht20/Kconfig b/components/sensors/humiture/aht20/Kconfig new file mode 100644 index 0000000000..2c216155c2 --- /dev/null +++ b/components/sensors/humiture/aht20/Kconfig @@ -0,0 +1,18 @@ +menu "AHT20 : CONFIGURATION" + + config AHT20_CHECK_CRC + bool "perform crc check on AHT20 readings" + help + CRC check to be performed on results or not?. + default n + + + config AHT20_I2C_CLK_SPEED + int "I2C clock speed" + default 100000 + range 1 400000 + help + Clock speed used for i2c communication by AHT20 device. + Limited to maximum of 400KHZ. + +endmenu diff --git a/components/sensors/humiture/aht20/README.md b/components/sensors/humiture/aht20/README.md index 11af7422c8..8af2cff2e5 100644 --- a/components/sensors/humiture/aht20/README.md +++ b/components/sensors/humiture/aht20/README.md @@ -1,32 +1,90 @@ -[![Component Registry](https://components.espressif.com/components/espressif/aht20/badge.svg)](https://components.espressif.com/components/espressif/aht20) +# aht20 +I2C driver for Aosong AHT20 humidity and temperature sensor using esp-idf. +Tested with AHT20 using ESP32 and ESP32-S3 devkits. -# Component: AHT20 -I2C driver and definition of AHT20 humidity and temperature sensor. +# Features -Components compatible with AHT30 and AHT21 (AHT21 is deprecated). + Temperature and humidity measurement -See [AHT20 datasheet](http://www.aosong.com/en/products-32.html), [AHT30 datasheet](http://www.aosong.com/en/products-131.html). + Thread-safe via esp-i2c-driver + CRC checksum verification (optional via menuconfig) -## Usage + Configurable I2C clock speed (pre-compilation, not runtime,via menuconfig)) -### Initialization -> Note: Note: You need to initialize the I2C bus first. + Unit tested + + + ┌───────────────────┐ + │ Application │ + └────────┬──────────┘ + │ + ▼ + ┌───────────────────┐ + │ AHT20 Driver │ + │ (this component) │ + └────────┬──────────┘ + │ + ▼ + ┌───────────────────┐ + │ i2c_bus component │ + └────────┬──────────┘ + │ + ▼ + ┌───────────────────┐ + │ I2C Bus │ + └────────┬──────────┘ + │ + ▼ + ┌────────────────────────────┐ + │ AHT20 Temperature/Humidity │ + │ Sensor │ + └────────────────────────────┘ + + + +# How To Use + +This driver includes a demo example project. + + Follow the example to learn how to initialize the driver and read the sensor data. + + All public APIs are documented in aht20.h. + + + +However, following are the general guiedlines. ```c - aht20_i2c_config_t i2c_conf = { - .i2c_port = I2C_MASTER_NUM, - .i2c_addr = AHT20_ADDRRES_0, - }; - aht20_new_sensor(&i2c_conf, &handle); + //create a AHT20 device object and receive a device handle for it + // my_i2c_bus_handle is a preintialized i2c_bus_handle_t object + aht20_handle_t aht20_handle = aht20_create( my_i2c_bus_handle, AHT20_ADDRESS_LOW ); //addresses are in aht.h + + //use the previously created AHT20 device handle for initializing the associated device + aht20_init(aht20_handle); + + //read both humidity and temperature at once from device, using AHT20 device handle + aht20_read_humiture(aht20_handle); //Other public APIs are documented in AHT20.h. + + //access the results stored in AHT20 device object, using the AHT20 device handle + //other apis require user to explicitly pass variable address to hold data + printf("tempertature = %.2fC humidity = %.3f \n", aht20_handle->humiture.temperature, aht20_handle->humiture.humidity); + + //to get reaw values create a object of following data type + aht20_raw_reading_t raw_value; + aht20_read_raw( aht20_handle, &raw_value); + printf("tempertature = %uC humidity = %u \n", raw_value.temperature, raw_value.humidity); ``` -### Read data -> The user can periodically call the aht20_read_temp_hum API to retrieve real-time data. -```c - uint32_t temp_raw, hum_raw; - float temp, hum; - aht20_read_temp_hum(aht20, &temp_raw, &temp, &hum_raw, &hum); - ESP_LOGI(TAG, "Humidity : %2.2f %%", hum); - ESP_LOGI(TAG, "Temperature : %2.2f degC", temp); -``` \ No newline at end of file +# How to Configure CRC and I2C clock speed +Additionally, select in menuconfig under Component Config → AHT20,; to use CRC(default is not used) +or change the clock speed of device (default is 100KHz). + +Note : It is recommended to use clock speeds in upper ranges of 100kHz to 200kHz. +Higher clock speeds may cause occasional data inconsistencies depending on your board layout and wiring. + +![image](https://github.com/user-attachments/assets/fc8680fb-1567-477c-92f8-52dd126e6f9d) + +or +In sdkconfig under Component Config → AHT20, +![image](https://github.com/user-attachments/assets/1f9612df-8d73-4ad1-bec7-75cbe6ed327a) diff --git a/components/sensors/humiture/aht20/aht20.c b/components/sensors/humiture/aht20/aht20.c index c34ee320fa..1318a5da01 100644 --- a/components/sensors/humiture/aht20/aht20.c +++ b/components/sensors/humiture/aht20/aht20.c @@ -1,160 +1,308 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ +/* + * @File: aht20.c + * + * @brief: AHT20 driver function definitions + * + * @Date: May 2, 2025 + * + * @Author: Rohan Jeet + * + */ -#include #include -#include #include "aht20.h" -#include "driver/gpio.h" -#include "esp_log.h" -#include "esp_check.h" -#include "aht20_reg.h" +static const char *s_TAG = "AHT20"; -const static char *TAG = "AHT20"; +/** +* @brief a function used to handle resetting of registers of the device, if not found calibrated when initialized +* +* @param[in] aht20_handle AHT20 device handle +* +* @param[in] read_buffer storage for read values +* +* @param[in] read_size data size to read +* +*/ +static esp_err_t aht20_read_reg(aht20_handle_t sensor, uint8_t * read_buffer, uint8_t read_size); -typedef struct { - i2c_port_t i2c_port; - uint8_t i2c_addr; -} aht20_dev_t; +/** +* @brief a function used to handle resetting of registers of the device, if not found calibrated when initialized +* +* @param[in] aht20_handle AHT20 device handle +* +* @param[in] cmd AHT20 command to be written +* +* @param[in] write_size data size to write +* +*/ +static esp_err_t aht20_write_reg(aht20_handle_t sensor, uint8_t * cmd, uint8_t write_size); -static esp_err_t aht20_write_reg(aht20_dev_handle_t dev, uint8_t reg_addr, uint8_t *data, uint8_t len) +/** +* @brief a function used to handle reinitialization of registers of the device, if not found calibrated when initialized +* +* @param[in] aht20_handle AHT20 device handle +* +*/ +static esp_err_t aht20_Start_Init(aht20_handle_t aht20_handle); + +/** +* @brief a function used to handle resetting of registers of the device, if not found calibrated when initialized +* +* @param[in] aht20_handle AHT20 device handle +* +* @param[in] addr AHT20 internal register, undocumented in datasheet +* +*/ +static esp_err_t aht_JH_Reset_REG(aht20_handle_t aht20_handle, uint8_t addr); + +/** +* @brief check crc validity of response received +* +* @param[in] message AHT reading +* +* +* @param[in] num Data bytes to check in message for crc +* +* @return crc calculated from the provided message +* +*/ +static uint8_t calc_CRC8(uint8_t *message, uint8_t Num); + +static esp_err_t aht20_read_reg(aht20_handle_t sensor, uint8_t * read_buffer, uint8_t read_size) { - aht20_dev_t *sens = (aht20_dev_t *) dev; - esp_err_t ret; - - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - ret = i2c_master_start(cmd); - assert(ESP_OK == ret); - ret = i2c_master_write_byte(cmd, sens->i2c_addr | I2C_MASTER_WRITE, true); - assert(ESP_OK == ret); - ret = i2c_master_write_byte(cmd, reg_addr, true); - assert(ESP_OK == ret); - if (len) { - ret = i2c_master_write(cmd, data, len, true); - assert(ESP_OK == ret); - } - ret = i2c_master_stop(cmd); - assert(ESP_OK == ret); - ret = i2c_master_cmd_begin(sens->i2c_port, cmd, 5000 / portTICK_PERIOD_MS); - i2c_cmd_link_delete(cmd); + ESP_RETURN_ON_ERROR(i2c_bus_read_bytes(sensor->i2c_dev, NULL_I2C_MEM_ADDR, read_size, read_buffer), + s_TAG, "unable to read from aht20"); - return ret; + return ESP_OK; } -static esp_err_t aht20_read_reg(aht20_dev_handle_t dev, uint8_t *data, size_t len) +static esp_err_t aht20_write_reg(aht20_handle_t sensor, uint8_t * cmd, uint8_t write_size) { - aht20_dev_t *sens = (aht20_dev_t *) dev; - esp_err_t ret; - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - ret = i2c_master_start(cmd); - assert(ESP_OK == ret); - ret = i2c_master_write_byte(cmd, sens->i2c_addr | I2C_MASTER_READ, true); - assert(ESP_OK == ret); - ret = i2c_master_read(cmd, data, len, I2C_MASTER_LAST_NACK); - assert(ESP_OK == ret); - ret = i2c_master_stop(cmd); - assert(ESP_OK == ret); - ret = i2c_master_cmd_begin(sens->i2c_port, cmd, 1000 / portTICK_PERIOD_MS); - i2c_cmd_link_delete(cmd); - - return ret; + ESP_RETURN_ON_ERROR(i2c_bus_write_bytes(sensor->i2c_dev, NULL_I2C_MEM_ADDR, write_size, cmd), + s_TAG, "unable to set mode for AHT20\n"); + + return ESP_OK; } -static uint8_t aht20_calc_crc(uint8_t *data, uint8_t len) +static uint8_t calc_CRC8(uint8_t *message, uint8_t Num) { uint8_t i; uint8_t byte; uint8_t crc = 0xFF; - - for (byte = 0; byte < len; byte++) { - crc ^= data[byte]; + for (byte = 0; byte < Num; byte++) { + crc ^= (message[byte]); for (i = 8; i > 0; --i) { - if ((crc & 0x80) != 0) { + if (crc & 0x80) { crc = (crc << 1) ^ 0x31; } else { - crc = crc << 1; + crc = (crc << 1); } } } - return crc; } -esp_err_t aht20_read_temperature_humidity(aht20_dev_handle_t handle, - uint32_t *temperature_raw, float *temperature, - uint32_t *humidity_raw, float *humidity) +esp_err_t aht20_read_raw(aht20_handle_t aht20_handle, aht20_raw_reading_t *raw_read) +{ + ESP_RETURN_ON_FALSE((aht20_handle != NULL), ESP_ERR_INVALID_ARG, s_TAG, "empty handle, provide a valid AHT20 handle"); + + uint8_t measure_cmd[] = {AHT20_MEASURE_CYC, 0x33, 0x00}; + + ESP_RETURN_ON_ERROR(aht20_write_reg(aht20_handle, measure_cmd, sizeof(measure_cmd)), + s_TAG, "unable to set mode for AHT20\n"); + + uint8_t read_measurement[7], read_bytes = 6;; +#ifdef CONFIG_AHT20_CHECK_CRC + read_bytes = 7; +#endif + + bool busy = true; + while (busy) { + aht20_busy_status(aht20_handle, &busy); // wait for measurement + } + + ESP_RETURN_ON_ERROR(aht20_read_reg(aht20_handle, read_measurement, read_bytes), + s_TAG, "unable to read raw measurement"); + +#ifdef CONFIG_AHT20_CHECK_CRC + ESP_RETURN_ON_FALSE((calc_CRC8(read_measurement, 6) == read_measurement[6]), + ESP_ERR_INVALID_RESPONSE, + s_TAG, "CRC match failed, invalid response received from AHT20"); +#endif + + raw_read->humidity = ((read_measurement[1] << 16) | (read_measurement[2] << 8) | read_measurement[3]) >> 4; + raw_read->temperature = ((read_measurement[3] << 16) | (read_measurement[4] << 8) | read_measurement[5]) & 0xfffff; + + return ESP_OK; +} + +esp_err_t aht20_read_humiture(aht20_handle_t aht20_handle) +{ + aht20_raw_reading_t raw_read; + ESP_RETURN_ON_ERROR(aht20_read_raw(aht20_handle, &raw_read), + "", ""); + + aht20_handle->humiture.humidity = raw_read.humidity * 100.0 / 1024 / 1024; //Calculated humidity value + aht20_handle->humiture.temperature = (raw_read.temperature * 200.0 / 1024 / 1024) - 50; //Calculated temperature value + + return ESP_OK; +} + +esp_err_t aht20_read_humidity(aht20_handle_t aht20_handle, float_t *humidity) +{ + aht20_raw_reading_t raw_read; + ESP_RETURN_ON_ERROR(aht20_read_raw(aht20_handle, &raw_read), + "", ""); + + *humidity = raw_read.humidity * 100.0 / 1024 / 1024; //Calculated humidity value + return ESP_OK; +} + +esp_err_t aht20_read_temperature(aht20_handle_t aht20_handle, float_t *temperature) +{ + aht20_raw_reading_t raw_read; + ESP_RETURN_ON_ERROR(aht20_read_raw(aht20_handle, &raw_read), + "", ""); + + *temperature = (raw_read.temperature * 200.0 / 1024 / 1024) - 50; //Calculated temperature value + + return ESP_OK; +} + +esp_err_t aht20_calibration_status(aht20_handle_t aht20_handle, bool *calibration) { - uint8_t status; - uint8_t buf[7]; - uint32_t raw_data; - - ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, TAG, "invalid device handle pointer"); - - buf[0] = 0x33; - buf[1] = 0x00; - ESP_RETURN_ON_ERROR(aht20_write_reg(handle, AHT20_START_MEASURMENT_CMD, buf, 2), TAG, "I2C read/write error"); - - vTaskDelay(pdMS_TO_TICKS(100)); - - ESP_RETURN_ON_ERROR(aht20_read_reg(handle, &status, 1), TAG, "I2C read/write error"); - - if ((status & BIT(AT581X_STATUS_Calibration_Enable)) && - (status & BIT(AT581X_STATUS_CRC_FLAG)) && - ((status & BIT(AT581X_STATUS_BUSY_INDICATION)) == 0)) { - ESP_RETURN_ON_ERROR(aht20_read_reg(handle, buf, 7), TAG, "I2C read/write error"); - ESP_RETURN_ON_ERROR((aht20_calc_crc(buf, 6) != buf[6]), TAG, "crc is error"); - - raw_data = buf[1]; - raw_data = raw_data << 8; - raw_data += buf[2]; - raw_data = raw_data << 8; - raw_data += buf[3]; - raw_data = raw_data >> 4; - *humidity_raw = raw_data; - *humidity = (float)raw_data * 100 / 1048576; - - raw_data = buf[3] & 0x0F; - raw_data = raw_data << 8; - raw_data += buf[4]; - raw_data = raw_data << 8; - raw_data += buf[5]; - *temperature_raw = raw_data; - *temperature = (float)raw_data * 200 / 1048576 - 50; - return ESP_OK; + ESP_RETURN_ON_FALSE((aht20_handle != NULL), ESP_ERR_INVALID_ARG, + s_TAG, "empty handle, initialize AHT20 handle"); + + ESP_RETURN_ON_FALSE((calibration != NULL), ESP_ERR_INVALID_ARG, + s_TAG, "provide a variable to store status value"); + + uint8_t read_status; + + ESP_RETURN_ON_ERROR(aht20_read_reg(aht20_handle, &read_status, sizeof(read_status)), + s_TAG, "unable to read status"); + + if (read_status & BIT3) { + *calibration = true; } else { - ESP_LOGI(TAG, "data is not ready"); - return ESP_ERR_NOT_FINISHED; + *calibration = false; } + + return ESP_OK; } -esp_err_t aht20_new_sensor(const aht20_i2c_config_t *i2c_conf, aht20_dev_handle_t *handle_out) +esp_err_t aht20_busy_status(aht20_handle_t aht20_handle, bool *busy) { - ESP_LOGI(TAG, "%-15s: %d.%d.%d", CHIP_NAME, AHT20_VER_MAJOR, AHT20_VER_MINOR, AHT20_VER_PATCH); - ESP_LOGI(TAG, "%-15s: %1.1f - %1.1fV", "SUPPLY_VOLTAGE", SUPPLY_VOLTAGE_MIN, SUPPLY_VOLTAGE_MAX); - ESP_LOGI(TAG, "%-15s: %.2f - %.2f℃", "TEMPERATURE", TEMPERATURE_MIN, TEMPERATURE_MAX); + ESP_RETURN_ON_FALSE((aht20_handle != NULL), ESP_ERR_INVALID_ARG, + s_TAG, "empty handle, initialize AHT20 handle"); + + ESP_RETURN_ON_FALSE((busy != NULL), ESP_ERR_INVALID_ARG, + s_TAG, "provide a variable to store status value"); + uint8_t read_status; + ESP_RETURN_ON_ERROR(aht20_read_reg(aht20_handle, &read_status, sizeof(read_status)), + s_TAG, "unable to read status"); + + if (read_status & BIT7) { + *busy = true; + } else { + *busy = false; + } + + return ESP_OK; +} + +static esp_err_t aht_JH_Reset_REG(aht20_handle_t aht20_handle, uint8_t addr) +{ + + uint8_t reset_cmd[] = {addr, 0x00, 0x00}, read_bytes[3]; + + ESP_RETURN_ON_ERROR(aht20_write_reg(aht20_handle, reset_cmd, sizeof(reset_cmd)), + s_TAG, "unable to reset, check log"); + + ESP_RETURN_ON_ERROR(aht20_read_reg(aht20_handle, read_bytes, sizeof(read_bytes)), + s_TAG, "unable to reset, check log"); - ESP_RETURN_ON_FALSE(i2c_conf, ESP_ERR_INVALID_ARG, TAG, "invalid device config pointer"); - ESP_RETURN_ON_FALSE(handle_out, ESP_ERR_INVALID_ARG, TAG, "invalid device handle pointer"); + vTaskDelay(10 / portTICK_PERIOD_MS); + reset_cmd[0] = 0xB0 | addr; + reset_cmd[1] = read_bytes[1]; + reset_cmd[2] = read_bytes[2]; - aht20_dev_t *handle = calloc(1, sizeof(aht20_dev_t)); - ESP_RETURN_ON_FALSE(handle, ESP_ERR_NO_MEM, TAG, "memory allocation for device handler failed"); + ESP_RETURN_ON_ERROR(aht20_write_reg(aht20_handle, reset_cmd, sizeof(reset_cmd)), + s_TAG, "unable to reset, check log"); - handle->i2c_port = i2c_conf->i2c_port; - handle->i2c_addr = i2c_conf->i2c_addr; + return ESP_OK; +} + +static esp_err_t aht20_Start_Init(aht20_handle_t aht20_handle) +{ + ESP_RETURN_ON_ERROR(aht_JH_Reset_REG(aht20_handle, 0x1b), "", ""); + ESP_RETURN_ON_ERROR(aht_JH_Reset_REG(aht20_handle, 0x1c), "", ""); + ESP_RETURN_ON_ERROR(aht_JH_Reset_REG(aht20_handle, 0x1e), "", ""); - *handle_out = handle; return ESP_OK; } -esp_err_t aht20_del_sensor(aht20_dev_handle_t handle) +esp_err_t aht20_init(aht20_handle_t aht20_handle) { - ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_ARG, TAG, "invalid device handle pointer"); + ESP_RETURN_ON_FALSE((aht20_handle != NULL), ESP_ERR_INVALID_ARG, s_TAG, "empty handle, initialize AHT20 handle"); + + vTaskDelay(20 / portTICK_PERIOD_MS); //time for AHT20 SCL to stabilize + + /***********************************************************************************/ + /** // This is undocumented in user manual + //The first time the power is turned on, read the status word at 0x71, determine whether the status word is 0x18, + //if it is not 0x18, reset the registers + **/ + uint8_t read_status; + + ESP_RETURN_ON_ERROR(aht20_read_reg(aht20_handle, &read_status, sizeof(read_status)), + s_TAG, "unable to read status"); + + if ((read_status & 0x18) != 0x18) { + ESP_RETURN_ON_ERROR(aht20_Start_Init(aht20_handle), + s_TAG, "reset failed, retry"); + vTaskDelay(10 / portTICK_PERIOD_MS); + } + /***********************************************************************************/ + + // initialize AHT20 + uint8_t aht20_init_cmd [] = { AHT20_INIT_REG, 0x08, 0x00 }; + + ESP_RETURN_ON_ERROR(aht20_write_reg(aht20_handle, aht20_init_cmd, sizeof(aht20_init_cmd)), + s_TAG, "unable to initialize AHT20\n"); - free(handle); + ESP_LOGI(s_TAG, "AHT20 initialized\n"); return ESP_OK; + +} + +aht20_handle_t aht20_create(i2c_bus_handle_t bus_handle, uint8_t aht20_address) +{ + ESP_LOGI(s_TAG, "adding aht20 as device to bus\n"); + i2c_bus_device_handle_t dev_handle = i2c_bus_device_create(bus_handle, aht20_address, CONFIG_AHT20_I2C_CLK_SPEED); + ESP_RETURN_ON_FALSE((dev_handle != NULL), NULL, + s_TAG, "unable to create device\n"); + ESP_LOGI(s_TAG, "device added to bus\n"); + + aht20_handle_t my_aht20_handle = malloc(sizeof(aht20_dev_config_t)); + + ESP_RETURN_ON_FALSE((my_aht20_handle != NULL), NULL, + s_TAG, "unable to allocate memory to initialize aht20 handle"); + + my_aht20_handle->i2c_dev = dev_handle; + return my_aht20_handle; +} + +void aht20_remove(aht20_handle_t *aht20ptr) +{ + i2c_bus_device_delete(&((*aht20ptr)->i2c_dev)); + free(*aht20ptr); + *aht20ptr = NULL; // now AHT20 handle is not a dangling pointer } diff --git a/components/sensors/humiture/aht20/idf_component.yml b/components/sensors/humiture/aht20/idf_component.yml index 786851a67b..a372920461 100644 --- a/components/sensors/humiture/aht20/idf_component.yml +++ b/components/sensors/humiture/aht20/idf_component.yml @@ -1,4 +1,25 @@ -version: 1.0.0 +name: aht20 + +version: "1.1.0" + +author: Rohan Jeet + +description: AHT20 Temperature and Humidity Sensor I2C Driver for ESP-IDF + +issues: https://github.com/espressif/esp-iot-solution/issues + +repository: git://github.com/espressif/esp-iot-solution.git + +url: https://github.com/espressif/esp-iot-solution/tree/master/components/sensors/humiture/aht20 + +dependencies: + + i2c_bus: + public: true + override_path: "../../../i2c_bus" + + idf: ">=4.0" + targets: - esp32 - esp32c2 @@ -7,13 +28,3 @@ targets: - esp32h2 - esp32s2 - esp32s3 -description: I2C driver for AHT20 humidity and temperature sensor -issues: https://github.com/espressif/esp-iot-solution/issues -repository: git://github.com/espressif/esp-iot-solution.git -url: https://github.com/espressif/esp-iot-solution/tree/master/components/sensors/humiture/aht20 -dependencies: - idf: '>=4.3' - cmake_utilities: "0.*" -sbom: - supplier: 'Organization: Espressif Systems (Shanghai) CO LTD' - originator: 'Organization: Espressif Systems (Shanghai) CO LTD' diff --git a/components/sensors/humiture/aht20/include/aht20.h b/components/sensors/humiture/aht20/include/aht20.h index 59d5b5bb1d..575d61da47 100644 --- a/components/sensors/humiture/aht20/include/aht20.h +++ b/components/sensors/humiture/aht20/include/aht20.h @@ -1,79 +1,209 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ +/* + * @File: aht20.h + * + * @brief: AHT20 driver function declarations and typedefinitons + * + * @Date: May 2, 2025 + * + * @Author: Rohan Jeet + * + */ +#ifndef AHT20_H +#define AHT20_H -#pragma once +#include +#include +#include +#include +#include +#include +#include + +#include "esp_bit_defs.h" +#include "esp_log.h" +#include "esp_err.h" +#include "esp_check.h" + +#include "freertos/FreeRTOS.h" + +#include "i2c_bus.h" #ifdef __cplusplus extern "C" { #endif -#include "esp_types.h" -#include "esp_err.h" - -#include "driver/i2c.h" - -/* AHT20 address: CE pin low - 0x38, CE pin high - 0x39 */ -#define AHT20_ADDRRES_0 (0x38<<1) -#define AHT20_ADDRESS_1 (0x39<<1) +#define AHT20_ADDRESS_LOW 0X38 /*!< AHT20 I2C ADDRESS LOW */ +#define AHT20_ADDRESS_HIGH 0X39 /*!< AHT20 I2C ADDRESS HIGH */ +#define AHT20_INIT_REG 0XBE /*!< initialize the AHT20 */ +#define AHT20_MEASURE_CYC 0xAC /*!< trigger measurement in cycle mode */ /** - * @brief Type of AHT20 device handle - * + *@brief AHT20 result */ -typedef void *aht20_dev_handle_t; +typedef struct { + float_t humidity; /*!< humidity reading. */ + float_t temperature; /*!< temperature reading. */ +} aht20_reading_t; /** - * @brief AHT20 I2C config struct - * + *@brief AHT20 raw result */ typedef struct { - i2c_port_t i2c_port; /*!< I2C port used to connected AHT20 device */ - uint8_t i2c_addr; /*!< I2C address of AHT20 device, can be 0x38 or 0x39 according to A0 pin */ -} aht20_i2c_config_t; + uint32_t humidity; /*!< raw humidity reading. */ + uint32_t temperature; /*!< raw temperature reading. */ +} aht20_raw_reading_t; /** - * @brief Create new AHT20 device handle. - * - * @param[in] i2c_conf Config for I2C used by AHT20 - * @param[out] handle_out New AHT20 device handle - * @return - * - ESP_OK Device handle creation success. - * - ESP_ERR_INVALID_ARG Invalid device handle or argument. - * - ESP_ERR_NO_MEM Memory allocation failed. - * + * @brief AHT20 device object */ -esp_err_t aht20_new_sensor(const aht20_i2c_config_t *i2c_conf, aht20_dev_handle_t *handle_out); +typedef struct { + i2c_bus_device_handle_t i2c_dev; /*!< i2c device handle. */ + aht20_reading_t humiture; /*!< AHT20 reading. */ +} aht20_dev_config_t; /** - * @brief Delete AHT20 device handle. - * - * @param[in] handle AHT20 device handle - * @return - * - ESP_OK Device handle deletion success. - * - ESP_ERR_INVALID_ARG Invalid device handle or argument. - * + * @brief AHT20 device handle */ -esp_err_t aht20_del_sensor(aht20_dev_handle_t handle); +typedef aht20_dev_config_t *aht20_handle_t; /** - * @brief read the temperature and humidity data - * - * @param[in] *handle points to an aht20 handle structure - * @param[out] *temperature_raw points to a raw temperature buffer - * @param[out] *temperature points to a converted temperature buffer - * @param[out] *humidity_raw points to a raw humidity buffer - * @param[out] *humidity points to a converted humidity buffer - * - * @return - * - ESP_OK Success - * - ESP_FAIL Fail - */ -esp_err_t aht20_read_temperature_humidity(aht20_dev_handle_t handle, - uint32_t *temperature_raw, float *temperature, - uint32_t *humidity_raw, float *humidity); +* @brief soft reset AHT20 +* +* @param[in] aht20_handle AHT20 device handle +* +* @return +* - ESP_OK: successful reset +* - other error codes : failed to reset +* +*/ +esp_err_t aht20_reset(aht20_handle_t aht20_handle); + +/** +* @brief get AHT20 sensor raw readings +*- +* @param[in] aht20_handle AHT20 device handle +* +* @param[out] raw_read raw reading +* +* @return +* - ESP_OK: successful read +* - other error codes : failed to read +* +*/ +esp_err_t aht20_read_raw(aht20_handle_t aht20_handle, aht20_raw_reading_t *raw_read); + +/** +* @brief get AHT20 sensor readings +*- +* @param[in] aht20_handle AHT20 device handle +* +* @return +* - ESP_OK: successful read +* - other error codes : failed to read +* +*/ +esp_err_t aht20_read_humiture(aht20_handle_t aht20_handle); + +/** +* @brief get AHT20 humidity readings +* +* @param[in] aht20_handle AHT20 device handle +* +* @param[out] humidity AHT20 humidity reading +* +* @return +* - ESP_OK: successful read +* - other error codes : failed to read +* +*/ +esp_err_t aht20_read_humidity(aht20_handle_t aht20_handle, float_t *humidity); + +/** +* @brief get AHT20 temperature readings +* +* @param[in] aht20_handle AHT20 device handle +* +* @param[out] temperature AHT20 temperature reading +* +* @return +* - ESP_OK: successful read +* - other error codes : failed to read +* +*/ +esp_err_t aht20_read_temperature(aht20_handle_t aht20_handle, float_t *temperature); + +/** +* @brief check AHT20 calibration status +* +* @param[in] aht20_handle AHT20 device handle +* +* @param[out] calibration calibrated if value is true +* +* @return +* - ESP_OK: successfully read AHT20 calibration status +* - other error codes : failure in reading AHT20 calibration status +* +*/ +esp_err_t aht20_calibration_status(aht20_handle_t aht20_handle, bool *calibration); + +/** +* @brief check AHT20 measurement status +* +* @param[in] aht20_handle AHT20 device handle +* +* @param[out]busy busy in measurement if value is true +* +* @return +* - ESP_OK: successfully read AHT20 busy status +* - other error codes : failure in reading AHT20 busy status +* +*/ +esp_err_t aht20_busy_status(aht20_handle_t aht20_handle, bool *busy); + +/** +* @brief Initialize the AHT20 +* +* @param[in] aht20_handle AHT20 device handle +* +* @return +* - ESP_OK: successful initiailiztion +* - other error codes : failed to initialize +* +*/ +esp_err_t aht20_init(aht20_handle_t aht20_handle); + +/** +* @brief create an AHT20 device handle +* +* @param[in] bus_handle I2C master bus handle, with which this device will be associated +* +* @param[in] scl_speed_hz I2C communication speed used by this device +* +* @param[in] aht20_address I2C address of this device +* +* @return +* - NULL: Failed to create AHT20 device handle +* - other error codes : from the underlying i2c driver, check log +* +*/ +aht20_handle_t aht20_create(i2c_bus_handle_t bus_handle, uint8_t aht20_address); + +/** +* @brief free the resources associated with AHT20 device +* +* @param[in] aht20_handler address of + AHT20 device handle +* +*/ +void aht20_remove(aht20_handle_t *aht20_handler); + #ifdef __cplusplus } -#endif +#endif // __cplusplus + +#endif // __AHT20_H From 3719c16ef5cf79c44137038eceafdd22a00a70c3 Mon Sep 17 00:00:00 2001 From: jeetrohan Date: Fri, 2 May 2025 23:37:17 +0530 Subject: [PATCH 2/8] test(aht20): update test app for new driver APIs and configurations Updated the test application to work with the new version of the AHT20 driver (v1.1.0). Modified test code to include the new APIs for reading sensor values. Changes to be committed: modified: test_apps/CMakeLists.txt modified: test_apps/main/CMakeLists.txt modified: test_apps/main/Kconfig.projbuild new file: test_apps/main/aht20_test_app.c new file: test_apps/main/idf_component.yml modified: test_apps/pytest_aht20.py --- .../humiture/aht20/test_apps/CMakeLists.txt | 5 +- .../aht20/test_apps/main/CMakeLists.txt | 6 +- .../aht20/test_apps/main/Kconfig.projbuild | 4 +- .../aht20/test_apps/main/aht20_test_app.c | 132 ++++++++++++++++++ .../aht20/test_apps/main/idf_component.yml | 4 + .../humiture/aht20/test_apps/pytest_aht20.py | 4 +- 6 files changed, 145 insertions(+), 10 deletions(-) create mode 100644 components/sensors/humiture/aht20/test_apps/main/aht20_test_app.c create mode 100644 components/sensors/humiture/aht20/test_apps/main/idf_component.yml diff --git a/components/sensors/humiture/aht20/test_apps/CMakeLists.txt b/components/sensors/humiture/aht20/test_apps/CMakeLists.txt index 98e2ca8ee0..84f95ebc24 100644 --- a/components/sensors/humiture/aht20/test_apps/CMakeLists.txt +++ b/components/sensors/humiture/aht20/test_apps/CMakeLists.txt @@ -1,8 +1,7 @@ # The following lines of boilerplate have to be in your project's CMakeLists # in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.16) -set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components" - "../../aht20") +set(COMPONENTS main) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(aht20_test) diff --git a/components/sensors/humiture/aht20/test_apps/main/CMakeLists.txt b/components/sensors/humiture/aht20/test_apps/main/CMakeLists.txt index 66cc485579..461a305c25 100644 --- a/components/sensors/humiture/aht20/test_apps/main/CMakeLists.txt +++ b/components/sensors/humiture/aht20/test_apps/main/CMakeLists.txt @@ -1,3 +1,3 @@ -idf_component_register(SRC_DIRS "." - PRIV_INCLUDE_DIRS "." - PRIV_REQUIRES unity test_utils aht20) +idf_component_register(SRCS "aht20_test_app.c" + INCLUDE_DIRS "." + PRIV_REQUIRES unity test_utils aht20) diff --git a/components/sensors/humiture/aht20/test_apps/main/Kconfig.projbuild b/components/sensors/humiture/aht20/test_apps/main/Kconfig.projbuild index bf2baba5f8..7cd0d5ee66 100644 --- a/components/sensors/humiture/aht20/test_apps/main/Kconfig.projbuild +++ b/components/sensors/humiture/aht20/test_apps/main/Kconfig.projbuild @@ -2,13 +2,13 @@ menu "Example Configuration" config I2C_MASTER_SCL int "SCL GPIO Num" - default 40 + default 22 help GPIO number for I2C Master clock line. config I2C_MASTER_SDA int "SDA GPIO Num" - default 41 + default 21 help GPIO number for I2C Master data line. diff --git a/components/sensors/humiture/aht20/test_apps/main/aht20_test_app.c b/components/sensors/humiture/aht20/test_apps/main/aht20_test_app.c new file mode 100644 index 0000000000..47b21aecc7 --- /dev/null +++ b/components/sensors/humiture/aht20/test_apps/main/aht20_test_app.c @@ -0,0 +1,132 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +/*- + * @File: aht20_test_app.c + * + * @brief: AHT20 driver unity test app + * + * @Date: May 2, 2025 + * + * @Author: Rohan Jeet + * + */ + +#include +#include "unity.h" +#include "esp_system.h" +#include "aht20.h" + +#define TEST_MEMORY_LEAK_THRESHOLD (-400) + +// I2C config +#define I2C_MASTER_SCL_IO CONFIG_I2C_MASTER_SCL +#define I2C_MASTER_SDA_IO CONFIG_I2C_MASTER_SDA +#define I2C_MASTER_NUM I2C_NUM_0 +#define I2C_MASTER_FREQ_HZ 100000 + +// Global handles +i2c_master_bus_handle_t my_i2c_bus_handle = NULL; +aht20_handle_t aht20_handle = NULL; + +/*******************************Memory Leak Checks****************************/ + +static size_t before_free_8bit; +static size_t before_free_32bit; +static size_t after_free_8bit; +static size_t after_free_32bit; + +static void check_leak(size_t before_free, size_t after_free, const char *type) +{ + ssize_t delta = after_free - before_free; + printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta); + TEST_ASSERT_MESSAGE(delta >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak"); +} + +void setUp(void) +{ + before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); + before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); +} + +void tearDown(void) +{ + after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); + after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); +} + +/************************** Memory Leak Checks Completed********************/ + +/*******************************I2C Master Bus Initialization****************************/ +void i2c_master_init(void) +{ + i2c_master_bus_config_t i2c_mst_config = { + .clk_source = I2C_CLK_SRC_DEFAULT, + .i2c_port = I2C_MASTER_NUM, + .scl_io_num = I2C_MASTER_SCL_IO, + .sda_io_num = I2C_MASTER_SDA_IO, + .glitch_ignore_cnt = 7, + .flags.enable_internal_pullup = true, + }; + + printf("Requesting I2C bus handle\n"); + ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_mst_config, &my_i2c_bus_handle)); + printf("I2C bus handle acquired\n"); +} +/*******************************I2C Master Bus Initialization Over****************************/ + +/*******************************AHT20 Device Initialization****************************/ +esp_err_t aht20_init_test() +{ + i2c_master_init(); + aht20_handle = aht20_create(my_i2c_bus_handle, AHT20_ADDRESS_LOW); + + printf("Initializing AHT20 sensor\n"); + while (aht20_init(aht20_handle) != ESP_OK) { + vTaskDelay(100 / portTICK_PERIOD_MS); + } + printf("AHT20 initialized\n"); + + return ESP_OK; +} +/*******************************AHT20 Device Initialization Over****************************/ + +/*******************************AHT20 Device Deinitializtion****************************/ +void aht20_deinit_test(void) +{ + aht20_remove(&aht20_handle); +} +/*******************************AHT20 Device Deinitializtion Over****************************/ + +/*******************************AHT20 Read sensor****************************/ +void aht20_read_test(void) +{ + vTaskDelay(400 / portTICK_PERIOD_MS); + + TEST_ASSERT(ESP_OK == aht20_read_humiture(aht20_handle)); + + printf("Temperature = %.2f°C, Humidity = %.3f%%\n", + aht20_handle->humiture.temperature, + aht20_handle->humiture.humidity); + + vTaskDelay(1000 / portTICK_PERIOD_MS); +} +/*******************************AHT20 AHT20 Read sensor Over*****************************/ + +/*************************************Test Case**************************/ + +TEST_CASE("AHT20 Sensor", "[aht20][sensor]") +{ + aht20_init_test(); + aht20_read_test(); + aht20_deinit_test(); +} +/*************************************Test Case Over**************************/ + +void app_main(void) +{ + printf("\n=== AHT20 Sensor Test Menu ===\n"); + unity_run_menu(); // Run test selection menu in flash monitor +} diff --git a/components/sensors/humiture/aht20/test_apps/main/idf_component.yml b/components/sensors/humiture/aht20/test_apps/main/idf_component.yml new file mode 100644 index 0000000000..af73e33954 --- /dev/null +++ b/components/sensors/humiture/aht20/test_apps/main/idf_component.yml @@ -0,0 +1,4 @@ +dependencies: + aht20: + version: "*" + override_path: "../../" diff --git a/components/sensors/humiture/aht20/test_apps/pytest_aht20.py b/components/sensors/humiture/aht20/test_apps/pytest_aht20.py index 069fb610fa..8db09196d5 100644 --- a/components/sensors/humiture/aht20/test_apps/pytest_aht20.py +++ b/components/sensors/humiture/aht20/test_apps/pytest_aht20.py @@ -6,10 +6,10 @@ - Build - . ${IDF_PATH}/export.sh - pip install idf_build_apps - - python tools/build_apps.py components/sensor/radar/at581x/test_apps -t esp32s3 + - python tools/build_apps.py components/sensors/humiture/aht20/test_apps -t esp32s3 - Test - pip install -r tools/requirements/requirement.pytest.txt - - pytest components/sensor/radar/at581x/test_apps --target esp32s3 + - pytest components/sensors/humiture/aht20/test_apps --target esp32s3 ''' import pytest From 3e4c618bb7531215a65465a5c7a1a312a67097d0 Mon Sep 17 00:00:00 2001 From: jeetrohan Date: Fri, 2 May 2025 23:44:50 +0530 Subject: [PATCH 3/8] feat(aht20): add demo showcasing updated driver features Added a demo application to illustrate usage of the updated AHT20 driver (v1.1.0). Demo includes usage of new sensor read APIs. Also serves as a reference for integrating the i2c_bus component from esp-iot-solution. Changes to be committed: new file: aht20_demo/CMakeLists.txt new file: aht20_demo/README.md new file: aht20_demo/main/CMakeLists.txt new file: aht20_demo/main/aht20_demo.c new file: aht20_demo/main/idf_component.yml --- examples/sensors/aht20_demo/CMakeLists.txt | 7 ++ examples/sensors/aht20_demo/README.md | 3 + .../sensors/aht20_demo/main/CMakeLists.txt | 2 + examples/sensors/aht20_demo/main/aht20_demo.c | 80 +++++++++++++++++++ .../sensors/aht20_demo/main/idf_component.yml | 4 + 5 files changed, 96 insertions(+) create mode 100644 examples/sensors/aht20_demo/CMakeLists.txt create mode 100644 examples/sensors/aht20_demo/README.md create mode 100644 examples/sensors/aht20_demo/main/CMakeLists.txt create mode 100644 examples/sensors/aht20_demo/main/aht20_demo.c create mode 100644 examples/sensors/aht20_demo/main/idf_component.yml diff --git a/examples/sensors/aht20_demo/CMakeLists.txt b/examples/sensors/aht20_demo/CMakeLists.txt new file mode 100644 index 0000000000..614332fcbb --- /dev/null +++ b/examples/sensors/aht20_demo/CMakeLists.txt @@ -0,0 +1,7 @@ +# The following five lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.16) + +set(COMPONENT main) +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(aht20_demo) diff --git a/examples/sensors/aht20_demo/README.md b/examples/sensors/aht20_demo/README.md new file mode 100644 index 0000000000..405a4931e8 --- /dev/null +++ b/examples/sensors/aht20_demo/README.md @@ -0,0 +1,3 @@ +This is a demo which uses the aht20 driver. + +It simply reads the humiture from aht20 sensor and prints the output continuously on terminal. diff --git a/examples/sensors/aht20_demo/main/CMakeLists.txt b/examples/sensors/aht20_demo/main/CMakeLists.txt new file mode 100644 index 0000000000..ebf986e664 --- /dev/null +++ b/examples/sensors/aht20_demo/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "aht20_demo.c" + INCLUDE_DIRS ".") diff --git a/examples/sensors/aht20_demo/main/aht20_demo.c b/examples/sensors/aht20_demo/main/aht20_demo.c new file mode 100644 index 0000000000..25368cb8f1 --- /dev/null +++ b/examples/sensors/aht20_demo/main/aht20_demo.c @@ -0,0 +1,80 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +/* + * @file: aht20_demo.c + * + * @brief: A simple demo to use the AHT20 driver + * + * @date: May 2, 2025 + * + * @Author: Rohan Jeet + * + */ +#include +#include +#include +#include "aht20.h" + +//i2c configuration values +#define I2C_MASTER_SCL_IO (22) // SCL pin +#define I2C_MASTER_SDA_IO (21) // SDA pin +#define I2C_MASTER_NUM I2C_NUM_0 +#define I2C_MASTER_FREQ_HZ (400000) // I2C frequency + +i2c_bus_handle_t my_i2c_bus_handle; + +void i2c_master_init(void) +{ + const i2c_config_t conf = { + .mode = I2C_MODE_MASTER, + .sda_io_num = I2C_MASTER_SDA_IO, + .sda_pullup_en = GPIO_PULLUP_ENABLE, + .scl_io_num = I2C_MASTER_SCL_IO, + .scl_pullup_en = GPIO_PULLUP_ENABLE, + .master.clk_speed = I2C_MASTER_FREQ_HZ, + }; + printf("requesting i2c bus handle\n"); + my_i2c_bus_handle = i2c_bus_create(I2C_MASTER_NUM, &conf); + printf("i2c bus handle acquired\n"); + +} + +void read_aht20(void *my_aht20_handle) +{ + + aht20_handle_t aht20_handle = (aht20_handle_t) my_aht20_handle; //retrieve the AHT20 device handle copy + vTaskDelay(400 / portTICK_PERIOD_MS); + while (1) { + //read both humidity and temperature at once from device, using AHT20 device handle + aht20_read_humiture(aht20_handle); + //access the results stored in AHT20 device object, using the AHT20 device handle + //other apis require user to explicitly pass variable address to hold data + printf("tempertature = %.2fC humidity = %.3f \n", aht20_handle->humiture.temperature, aht20_handle->humiture.humidity); + vTaskDelay(1000 / portTICK_PERIOD_MS); + } +} + +void init_aht20() +{ + //create a AHT20 device object and receive a device handle for it + aht20_handle_t aht20_handle = aht20_create(my_i2c_bus_handle, AHT20_ADDRESS_LOW); + + printf("initializing aht20\n"); + //use the previously created AHT20 device handle for initializing the associated device + while (aht20_init(aht20_handle) != ESP_OK) { // wait until it is initialized + vTaskDelay(100 / portTICK_PERIOD_MS); + } + printf("aht20 initialized\n"); + + //creating a task to read from AHT20, passing the AHT20 device handle copy + xTaskCreate(read_aht20, "aht20_tester", 2500, aht20_handle, 5, NULL); +} + +void app_main(void) +{ + i2c_master_init(); //initialize i2c master + init_aht20(); // user defined function for aht20 initialization +} diff --git a/examples/sensors/aht20_demo/main/idf_component.yml b/examples/sensors/aht20_demo/main/idf_component.yml new file mode 100644 index 0000000000..f67432d3a6 --- /dev/null +++ b/examples/sensors/aht20_demo/main/idf_component.yml @@ -0,0 +1,4 @@ +dependencies: + aht20: + version: "*" + override_path: "../../../../components/sensors/humiture/aht20" From 4f076d734c185629bbd1e5c51e72e4b82b6ec924 Mon Sep 17 00:00:00 2001 From: jeetrohan Date: Wed, 14 May 2025 14:59:30 +0530 Subject: [PATCH 4/8] fix(aht20): fixed minor changes On branch update_aht20 Changes to be committed: modified: components/sensors/humiture/aht20/CHANGELOG.md modified: components/sensors/humiture/aht20/README.md modified: components/sensors/humiture/aht20/aht20.c modified: components/sensors/humiture/aht20/idf_component.yml modified: components/sensors/humiture/aht20/priv_include/aht20_reg.h modified: components/sensors/humiture/aht20/test_apps/CMakeLists.txt modified: components/sensors/humiture/aht20/test_apps/main/Kconfig.projbuild deleted: components/sensors/humiture/aht20/test_apps/main/aht20_test.c modified: components/sensors/humiture/aht20/test_apps/main/aht20_test_app.c modified: examples/sensors/aht20_demo/main/CMakeLists.txt --- .../sensors/humiture/aht20/CHANGELOG.md | 2 +- components/sensors/humiture/aht20/README.md | 14 +-- components/sensors/humiture/aht20/aht20.c | 10 +- .../sensors/humiture/aht20/idf_component.yml | 3 + .../humiture/aht20/priv_include/aht20_reg.h | 10 +- .../humiture/aht20/test_apps/CMakeLists.txt | 4 +- .../aht20/test_apps/main/Kconfig.projbuild | 4 +- .../aht20/test_apps/main/aht20_test.c | 104 ------------------ .../aht20/test_apps/main/aht20_test_app.c | 3 + .../sensors/aht20_demo/main/CMakeLists.txt | 3 +- 10 files changed, 27 insertions(+), 130 deletions(-) delete mode 100644 components/sensors/humiture/aht20/test_apps/main/aht20_test.c diff --git a/components/sensors/humiture/aht20/CHANGELOG.md b/components/sensors/humiture/aht20/CHANGELOG.md index 00b545fc60..153cd9f984 100644 --- a/components/sensors/humiture/aht20/CHANGELOG.md +++ b/components/sensors/humiture/aht20/CHANGELOG.md @@ -1,7 +1,7 @@ # ChangeLog ## v1.1.1 (2025-05-02) -* replace the i2c interface with i2c_bus +* Replace the i2c interface with i2c_bus ## v1.0.0 (2024-08-09) diff --git a/components/sensors/humiture/aht20/README.md b/components/sensors/humiture/aht20/README.md index 8af2cff2e5..0c30cac2e3 100644 --- a/components/sensors/humiture/aht20/README.md +++ b/components/sensors/humiture/aht20/README.md @@ -1,4 +1,4 @@ -# aht20 +# Component: AHT20 I2C driver for Aosong AHT20 humidity and temperature sensor using esp-idf. Tested with AHT20 using ESP32 and ESP32-S3 devkits. @@ -47,23 +47,23 @@ Tested with AHT20 using ESP32 and ESP32-S3 devkits. This driver includes a demo example project. - Follow the example to learn how to initialize the driver and read the sensor data. +Follow the example to learn how to initialize the driver and read the sensor data. - All public APIs are documented in aht20.h. +All public APIs are documented in aht20.h. -However, following are the general guiedlines. +Following are the general guidelines. ```c //create a AHT20 device object and receive a device handle for it // my_i2c_bus_handle is a preintialized i2c_bus_handle_t object - aht20_handle_t aht20_handle = aht20_create( my_i2c_bus_handle, AHT20_ADDRESS_LOW ); //addresses are in aht.h + aht20_handle_t aht20_handle = aht20_create( my_i2c_bus_handle, AHT20_ADDRESS_LOW ); //addresses are in aht20.h //use the previously created AHT20 device handle for initializing the associated device aht20_init(aht20_handle); //read both humidity and temperature at once from device, using AHT20 device handle - aht20_read_humiture(aht20_handle); //Other public APIs are documented in AHT20.h. + aht20_read_humiture(aht20_handle); //Other public APIs are documented in aht20.h. //access the results stored in AHT20 device object, using the AHT20 device handle //other apis require user to explicitly pass variable address to hold data @@ -77,7 +77,7 @@ However, following are the general guiedlines. # How to Configure CRC and I2C clock speed -Additionally, select in menuconfig under Component Config → AHT20,; to use CRC(default is not used) +Additionally, select in menuconfig under Component Config → AHT20; to use CRC(default is not used) or change the clock speed of device (default is 100KHz). Note : It is recommended to use clock speeds in upper ranges of 100kHz to 200kHz. diff --git a/components/sensors/humiture/aht20/aht20.c b/components/sensors/humiture/aht20/aht20.c index 1318a5da01..8b0b49d92c 100644 --- a/components/sensors/humiture/aht20/aht20.c +++ b/components/sensors/humiture/aht20/aht20.c @@ -59,7 +59,7 @@ static esp_err_t aht20_Start_Init(aht20_handle_t aht20_handle); * @param[in] addr AHT20 internal register, undocumented in datasheet * */ -static esp_err_t aht_JH_Reset_REG(aht20_handle_t aht20_handle, uint8_t addr); +static esp_err_t aht20_JH_Reset_REG(aht20_handle_t aht20_handle, uint8_t addr); /** * @brief check crc validity of response received @@ -217,7 +217,7 @@ esp_err_t aht20_busy_status(aht20_handle_t aht20_handle, bool *busy) return ESP_OK; } -static esp_err_t aht_JH_Reset_REG(aht20_handle_t aht20_handle, uint8_t addr) +static esp_err_t aht20_JH_Reset_REG(aht20_handle_t aht20_handle, uint8_t addr) { uint8_t reset_cmd[] = {addr, 0x00, 0x00}, read_bytes[3]; @@ -241,9 +241,9 @@ static esp_err_t aht_JH_Reset_REG(aht20_handle_t aht20_handle, uint8_t addr) static esp_err_t aht20_Start_Init(aht20_handle_t aht20_handle) { - ESP_RETURN_ON_ERROR(aht_JH_Reset_REG(aht20_handle, 0x1b), "", ""); - ESP_RETURN_ON_ERROR(aht_JH_Reset_REG(aht20_handle, 0x1c), "", ""); - ESP_RETURN_ON_ERROR(aht_JH_Reset_REG(aht20_handle, 0x1e), "", ""); + ESP_RETURN_ON_ERROR(aht20_JH_Reset_REG(aht20_handle, 0x1b), "", ""); + ESP_RETURN_ON_ERROR(aht20_JH_Reset_REG(aht20_handle, 0x1c), "", ""); + ESP_RETURN_ON_ERROR(aht20_JH_Reset_REG(aht20_handle, 0x1e), "", ""); return ESP_OK; } diff --git a/components/sensors/humiture/aht20/idf_component.yml b/components/sensors/humiture/aht20/idf_component.yml index a372920461..5b17436a5b 100644 --- a/components/sensors/humiture/aht20/idf_component.yml +++ b/components/sensors/humiture/aht20/idf_component.yml @@ -20,6 +20,9 @@ dependencies: idf: ">=4.0" +examples: + - path: ../../../examples/sensors/aht20_demo + targets: - esp32 - esp32c2 diff --git a/components/sensors/humiture/aht20/priv_include/aht20_reg.h b/components/sensors/humiture/aht20/priv_include/aht20_reg.h index 478c5148a0..2ba160e278 100644 --- a/components/sensors/humiture/aht20/priv_include/aht20_reg.h +++ b/components/sensors/humiture/aht20/priv_include/aht20_reg.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -14,11 +14,3 @@ #define SUPPLY_VOLTAGE_MAX (5.5f) /**< chip max supply voltage */ #define TEMPERATURE_MIN (-40.0f) /**< chip min operating temperature */ #define TEMPERATURE_MAX (125.0f) /**< chip max operating temperature */ - -#define AHT20_START_MEASURMENT_CMD 0xAC /* start measurement command */ - -#define AT581X_STATUS_CMP_INT (2) /* 1 --Out threshold range; 0 --In threshold range */ -#define AT581X_STATUS_Calibration_Enable (3) /* 1 --Calibration enable; 0 --Calibration disable */ -#define AT581X_STATUS_CRC_FLAG (4) /* 1 --CRC ok; 0 --CRC failed */ -#define AT581X_STATUS_MODE_STATUS (5) /* 00 -NOR mode; 01 -CYC mode; 1x --CMD mode */ -#define AT581X_STATUS_BUSY_INDICATION (7) /* 1 --Equipment is busy; 0 --Equipment is idle */ diff --git a/components/sensors/humiture/aht20/test_apps/CMakeLists.txt b/components/sensors/humiture/aht20/test_apps/CMakeLists.txt index 84f95ebc24..b5cebea280 100644 --- a/components/sensors/humiture/aht20/test_apps/CMakeLists.txt +++ b/components/sensors/humiture/aht20/test_apps/CMakeLists.txt @@ -1,7 +1,9 @@ # The following lines of boilerplate have to be in your project's CMakeLists # in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.16) +cmake_minimum_required(VERSION 3.5) set(COMPONENTS main) +set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components") + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(aht20_test) diff --git a/components/sensors/humiture/aht20/test_apps/main/Kconfig.projbuild b/components/sensors/humiture/aht20/test_apps/main/Kconfig.projbuild index 7cd0d5ee66..bf2baba5f8 100644 --- a/components/sensors/humiture/aht20/test_apps/main/Kconfig.projbuild +++ b/components/sensors/humiture/aht20/test_apps/main/Kconfig.projbuild @@ -2,13 +2,13 @@ menu "Example Configuration" config I2C_MASTER_SCL int "SCL GPIO Num" - default 22 + default 40 help GPIO number for I2C Master clock line. config I2C_MASTER_SDA int "SDA GPIO Num" - default 21 + default 41 help GPIO number for I2C Master data line. diff --git a/components/sensors/humiture/aht20/test_apps/main/aht20_test.c b/components/sensors/humiture/aht20/test_apps/main/aht20_test.c deleted file mode 100644 index 68f76a3c80..0000000000 --- a/components/sensors/humiture/aht20/test_apps/main/aht20_test.c +++ /dev/null @@ -1,104 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include "unity.h" -#include "driver/i2c.h" -#include "aht20.h" -#include "esp_system.h" -#include "esp_log.h" - -static const char *TAG = "aht20 test"; - -#define TEST_MEMORY_LEAK_THRESHOLD (-400) - -#define I2C_MASTER_SCL_IO CONFIG_I2C_MASTER_SCL /*!< gpio number for I2C master clock */ -#define I2C_MASTER_SDA_IO CONFIG_I2C_MASTER_SDA /*!< gpio number for I2C master data */ -#define I2C_MASTER_NUM I2C_NUM_0 /*!< I2C port number for master dev */ -#define I2C_MASTER_FREQ_HZ 100000 /*!< I2C master clock frequency */ - -static aht20_dev_handle_t aht20 = NULL; - -/** - * @brief i2c master initialization - */ -static void i2c_bus_init(void) -{ - const i2c_config_t i2c_conf = { - .mode = I2C_MODE_MASTER, - .sda_io_num = (gpio_num_t)I2C_MASTER_SDA_IO, - .sda_pullup_en = GPIO_PULLUP_DISABLE, - .scl_io_num = (gpio_num_t)I2C_MASTER_SCL_IO, - .scl_pullup_en = GPIO_PULLUP_DISABLE, - .master.clk_speed = I2C_MASTER_FREQ_HZ - }; - esp_err_t ret = i2c_param_config(I2C_MASTER_NUM, &i2c_conf); - TEST_ASSERT_EQUAL_MESSAGE(ESP_OK, ret, "I2C config returned error"); - - ret = i2c_driver_install(I2C_MASTER_NUM, i2c_conf.mode, 0, 0, 0); - TEST_ASSERT_EQUAL_MESSAGE(ESP_OK, ret, "I2C install returned error"); -} - -static void i2c_sensor_ath20_init(void) -{ - aht20_i2c_config_t i2c_conf = { - .i2c_port = I2C_MASTER_NUM, - .i2c_addr = AHT20_ADDRRES_0, - }; - - i2c_bus_init(); - aht20_new_sensor(&i2c_conf, &aht20); - TEST_ASSERT_NOT_NULL_MESSAGE(aht20, "AHT20 create returned NULL"); -} - -TEST_CASE("sensor aht20 test", "[aht20][iot][sensor]") -{ - esp_err_t ret = ESP_OK; - uint32_t temperature_raw; - uint32_t humidity_raw; - float temperature; - float humidity; - - i2c_sensor_ath20_init(); - - TEST_ASSERT(ESP_OK == aht20_read_temperature_humidity(aht20, &temperature_raw, &temperature, &humidity_raw, &humidity)); - ESP_LOGI(TAG, "%-20s: %2.2f %%", "humidity is", humidity); - ESP_LOGI(TAG, "%-20s: %2.2f degC", "temperature is", temperature); - - aht20_del_sensor(aht20); - ret = i2c_driver_delete(I2C_MASTER_NUM); - TEST_ASSERT_EQUAL(ESP_OK, ret); -} - -static size_t before_free_8bit; -static size_t before_free_32bit; - -static void check_leak(size_t before_free, size_t after_free, const char *type) -{ - ssize_t delta = after_free - before_free; - printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta); - TEST_ASSERT_MESSAGE(delta >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak"); -} - -void setUp(void) -{ - before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); - before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); -} - -void tearDown(void) -{ - size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); - size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); - check_leak(before_free_8bit, after_free_8bit, "8BIT"); - check_leak(before_free_32bit, after_free_32bit, "32BIT"); -} - -void app_main(void) -{ - printf("AHT20 TEST \n"); - unity_run_menu(); -} diff --git a/components/sensors/humiture/aht20/test_apps/main/aht20_test_app.c b/components/sensors/humiture/aht20/test_apps/main/aht20_test_app.c index 47b21aecc7..5f8b48bc99 100644 --- a/components/sensors/humiture/aht20/test_apps/main/aht20_test_app.c +++ b/components/sensors/humiture/aht20/test_apps/main/aht20_test_app.c @@ -55,6 +55,8 @@ void tearDown(void) { after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); + check_leak(before_free_8bit, after_free_8bit, "8BIT"); + check_leak(before_free_32bit, after_free_32bit, "32BIT"); } /************************** Memory Leak Checks Completed********************/ @@ -97,6 +99,7 @@ esp_err_t aht20_init_test() void aht20_deinit_test(void) { aht20_remove(&aht20_handle); + i2c_del_master_bus(my_i2c_bus_handle); } /*******************************AHT20 Device Deinitializtion Over****************************/ diff --git a/examples/sensors/aht20_demo/main/CMakeLists.txt b/examples/sensors/aht20_demo/main/CMakeLists.txt index ebf986e664..a8c3fc61e0 100644 --- a/examples/sensors/aht20_demo/main/CMakeLists.txt +++ b/examples/sensors/aht20_demo/main/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "aht20_demo.c" - INCLUDE_DIRS ".") + INCLUDE_DIRS "." + REQUIRES "aht20") From 75b9060c5f34eeaaf5ba4e23b83916d2939f1014 Mon Sep 17 00:00:00 2001 From: jeetrohan Date: Fri, 23 May 2025 17:22:23 +0530 Subject: [PATCH 5/8] feat(aht20): add Sensor Hub support, new test app, and example Updated AHT20 driver to support Sensor Hub integration. Added a dedicated test application to verify functionality with Sensor Hub Humiture Sensor APIs. Included a new example demonstrating how to use AHT20 through the Sensor Hub interface. On branch update_aht20 Changes to be committed: modified: components/sensors/humiture/aht20/CMakeLists.txt modified: components/sensors/humiture/aht20/aht20.c modified: components/sensors/humiture/aht20/idf_component.yml modified: components/sensors/humiture/aht20/include/aht20.h renamed: components/sensors/humiture/aht20/test_apps/CMakeLists.txt -> components/sensors/humiture/aht20/test_apps/aht20_driver_test/CMakeLists.txt renamed: components/sensors/humiture/aht20/test_apps/main/CMakeLists.txt -> components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/CMakeLists.txt renamed: components/sensors/humiture/aht20/test_apps/main/Kconfig.projbuild -> components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/Kconfig.projbuild renamed: components/sensors/humiture/aht20/test_apps/main/aht20_test_app.c -> components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/aht20_test_app.c renamed: components/sensors/humiture/aht20/test_apps/main/idf_component.yml -> components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/idf_component.yml renamed: components/sensors/humiture/aht20/test_apps/pytest_aht20.py -> components/sensors/humiture/aht20/test_apps/aht20_driver_test/pytest_aht20.py renamed: components/sensors/humiture/aht20/test_apps/sdkconfig.defaults -> components/sensors/humiture/aht20/test_apps/aht20_driver_test/sdkconfig.defaults new file: components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/CMakeLists.txt new file: components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/CMakeLists.txt new file: components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/Kconfig.projbuild new file: components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/aht20_Sensor_Hub_Humiture.c new file: components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/idf_component.yml new file: components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/pytest_aht20.py new file: components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/sdkconfig.defaults new file: examples/sensors/aht20_Sensor_Hub/CMakeLists.txt new file: examples/sensors/aht20_Sensor_Hub/README.md new file: examples/sensors/aht20_Sensor_Hub/main/CMakeLists.txt new file: examples/sensors/aht20_Sensor_Hub/main/Kconfig.projbuild new file: examples/sensors/aht20_Sensor_Hub/main/aht20_demo_sensor_hub.c new file: examples/sensors/aht20_Sensor_Hub/main/idf_component.yml modified: examples/sensors/aht20_demo/CMakeLists.txt modified: examples/sensors/aht20_demo/main/aht20_demo.c --- .../sensors/humiture/aht20/CMakeLists.txt | 8 ++ components/sensors/humiture/aht20/aht20.c | 100 ++++++++++++- .../sensors/humiture/aht20/idf_component.yml | 5 + .../sensors/humiture/aht20/include/aht20.h | 8 +- .../{ => aht20_driver_test}/CMakeLists.txt | 0 .../main/CMakeLists.txt | 0 .../main/Kconfig.projbuild | 0 .../main/aht20_test_app.c | 18 +-- .../main/idf_component.yml | 2 +- .../{ => aht20_driver_test}/pytest_aht20.py | 0 .../sdkconfig.defaults | 0 .../aht20_sensor_hub_humiture/CMakeLists.txt | 9 ++ .../main/CMakeLists.txt | 3 + .../main/Kconfig.projbuild | 15 ++ .../main/aht20_Sensor_Hub_Humiture.c | 134 ++++++++++++++++++ .../main/idf_component.yml | 7 + .../aht20_sensor_hub_humiture/pytest_aht20.py | 29 ++++ .../sdkconfig.defaults | 7 + .../sensors/aht20_Sensor_Hub/CMakeLists.txt | 7 + examples/sensors/aht20_Sensor_Hub/README.md | 3 + .../aht20_Sensor_Hub/main/CMakeLists.txt | 3 + .../aht20_Sensor_Hub/main/Kconfig.projbuild | 22 +++ .../main/aht20_demo_sensor_hub.c | 120 ++++++++++++++++ .../aht20_Sensor_Hub/main/idf_component.yml | 7 + examples/sensors/aht20_demo/CMakeLists.txt | 4 +- examples/sensors/aht20_demo/main/aht20_demo.c | 10 +- 26 files changed, 503 insertions(+), 18 deletions(-) rename components/sensors/humiture/aht20/test_apps/{ => aht20_driver_test}/CMakeLists.txt (100%) rename components/sensors/humiture/aht20/test_apps/{ => aht20_driver_test}/main/CMakeLists.txt (100%) rename components/sensors/humiture/aht20/test_apps/{ => aht20_driver_test}/main/Kconfig.projbuild (100%) rename components/sensors/humiture/aht20/test_apps/{ => aht20_driver_test}/main/aht20_test_app.c (90%) rename components/sensors/humiture/aht20/test_apps/{ => aht20_driver_test}/main/idf_component.yml (56%) rename components/sensors/humiture/aht20/test_apps/{ => aht20_driver_test}/pytest_aht20.py (100%) rename components/sensors/humiture/aht20/test_apps/{ => aht20_driver_test}/sdkconfig.defaults (100%) create mode 100644 components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/CMakeLists.txt create mode 100644 components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/CMakeLists.txt create mode 100644 components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/Kconfig.projbuild create mode 100644 components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/aht20_Sensor_Hub_Humiture.c create mode 100644 components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/idf_component.yml create mode 100644 components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/pytest_aht20.py create mode 100644 components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/sdkconfig.defaults create mode 100644 examples/sensors/aht20_Sensor_Hub/CMakeLists.txt create mode 100644 examples/sensors/aht20_Sensor_Hub/README.md create mode 100644 examples/sensors/aht20_Sensor_Hub/main/CMakeLists.txt create mode 100644 examples/sensors/aht20_Sensor_Hub/main/Kconfig.projbuild create mode 100644 examples/sensors/aht20_Sensor_Hub/main/aht20_demo_sensor_hub.c create mode 100644 examples/sensors/aht20_Sensor_Hub/main/idf_component.yml diff --git a/components/sensors/humiture/aht20/CMakeLists.txt b/components/sensors/humiture/aht20/CMakeLists.txt index 7c39296667..6e4a1594ee 100644 --- a/components/sensors/humiture/aht20/CMakeLists.txt +++ b/components/sensors/humiture/aht20/CMakeLists.txt @@ -1,2 +1,10 @@ idf_component_register(SRCS "aht20.c" INCLUDE_DIRS "include") + + +if(CONFIG_SENSOR_INCLUDED_HUMITURE) + target_link_libraries(${COMPONENT_LIB} INTERFACE "-u humiture_aht20_init") +endif() + +include(package_manager) +cu_pkg_define_version(${CMAKE_CURRENT_LIST_DIR}) diff --git a/components/sensors/humiture/aht20/aht20.c b/components/sensors/humiture/aht20/aht20.c index 8b0b49d92c..23df1ad3a6 100644 --- a/components/sensors/humiture/aht20/aht20.c +++ b/components/sensors/humiture/aht20/aht20.c @@ -15,6 +15,9 @@ */ #include + +#include "iot_sensor_hub.h" + #include "aht20.h" static const char *s_TAG = "AHT20"; @@ -300,9 +303,104 @@ aht20_handle_t aht20_create(i2c_bus_handle_t bus_handle, uint8_t aht20_address) return my_aht20_handle; } -void aht20_remove(aht20_handle_t *aht20ptr) +esp_err_t aht20_remove(aht20_handle_t *aht20ptr) { + if (*aht20ptr == NULL) { + return ESP_ERR_INVALID_ARG; + } i2c_bus_device_delete(&((*aht20ptr)->i2c_dev)); free(*aht20ptr); *aht20ptr = NULL; // now AHT20 handle is not a dangling pointer + return ESP_OK; +} + +#ifdef CONFIG_SENSOR_INCLUDED_HUMITURE + +static aht20_handle_t aht20 = NULL; +static bool is_init = false; + +esp_err_t humiture_aht20_init(i2c_bus_handle_t i2c_bus, uint8_t addr) +{ + if (is_init || !i2c_bus) { + return ESP_FAIL; + } + + aht20 = aht20_create(i2c_bus, addr); + + if (!aht20) { + return ESP_FAIL; + } + + ESP_RETURN_ON_ERROR(aht20_init(aht20), s_TAG, ""); + + is_init = true; + return ESP_OK; +} + +esp_err_t humiture_aht20_deinit(void) +{ + if (!is_init) { + return ESP_FAIL; + } + + ESP_RETURN_ON_ERROR(aht20_remove(&aht20), s_TAG, ""); + + is_init = false; + return ESP_OK; +} + +esp_err_t humiture_aht20_test(void) +{ + if (!is_init) { + return ESP_FAIL; + } + + return ESP_OK; +} + +esp_err_t humiture_aht20_acquire_humidity(float *h) +{ + if (!is_init) { + return ESP_FAIL; + } + + float humidity = 0; + esp_err_t ret = aht20_read_humidity(aht20, &humidity); + + if (ret == ESP_OK) { + *h = humidity; + return ESP_OK; + } + *h = 0; + return ESP_FAIL; } + +esp_err_t humiture_aht20_acquire_temperature(float *t) +{ + if (!is_init) { + return ESP_FAIL; + } + + float temperature = 0; + esp_err_t ret = aht20_read_temperature(aht20, &temperature); + + if (ret == ESP_OK) { + *t = temperature; + return ESP_OK; + } + + *t = 0; + return ESP_FAIL; +} + +static humiture_impl_t aht20_impl = { + .init = humiture_aht20_init, + .deinit = humiture_aht20_deinit, + .test = humiture_aht20_test, + .acquire_humidity = humiture_aht20_acquire_humidity, + .acquire_temperature = humiture_aht20_acquire_temperature, +}; + +SENSOR_HUB_DETECT_FN(HUMITURE_ID, aht20, &aht20_impl); + +#endif diff --git a/components/sensors/humiture/aht20/idf_component.yml b/components/sensors/humiture/aht20/idf_component.yml index 5b17436a5b..ee05baad82 100644 --- a/components/sensors/humiture/aht20/idf_component.yml +++ b/components/sensors/humiture/aht20/idf_component.yml @@ -18,10 +18,15 @@ dependencies: public: true override_path: "../../../i2c_bus" + sensor_hub: + public: true + override_path: "../../sensor_hub" + idf: ">=4.0" examples: - path: ../../../examples/sensors/aht20_demo + - path: ../../../examples/sensors/aht20_Sensor_Hub targets: - esp32 diff --git a/components/sensors/humiture/aht20/include/aht20.h b/components/sensors/humiture/aht20/include/aht20.h index 575d61da47..9cd474953c 100644 --- a/components/sensors/humiture/aht20/include/aht20.h +++ b/components/sensors/humiture/aht20/include/aht20.h @@ -196,11 +196,13 @@ aht20_handle_t aht20_create(i2c_bus_handle_t bus_handle, uint8_t aht20_address); /** * @brief free the resources associated with AHT20 device * -* @param[in] aht20_handler address of - AHT20 device handle +* @param[in] aht20_handler address of AHT20 device handle * +* @return +* - ESP_ERR_INVALID_ARG: Invalid argument +* - ESP_OK : successful */ -void aht20_remove(aht20_handle_t *aht20_handler); +esp_err_t aht20_remove(aht20_handle_t *aht20_handler); #ifdef __cplusplus } diff --git a/components/sensors/humiture/aht20/test_apps/CMakeLists.txt b/components/sensors/humiture/aht20/test_apps/aht20_driver_test/CMakeLists.txt similarity index 100% rename from components/sensors/humiture/aht20/test_apps/CMakeLists.txt rename to components/sensors/humiture/aht20/test_apps/aht20_driver_test/CMakeLists.txt diff --git a/components/sensors/humiture/aht20/test_apps/main/CMakeLists.txt b/components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/CMakeLists.txt similarity index 100% rename from components/sensors/humiture/aht20/test_apps/main/CMakeLists.txt rename to components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/CMakeLists.txt diff --git a/components/sensors/humiture/aht20/test_apps/main/Kconfig.projbuild b/components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/Kconfig.projbuild similarity index 100% rename from components/sensors/humiture/aht20/test_apps/main/Kconfig.projbuild rename to components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/Kconfig.projbuild diff --git a/components/sensors/humiture/aht20/test_apps/main/aht20_test_app.c b/components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/aht20_test_app.c similarity index 90% rename from components/sensors/humiture/aht20/test_apps/main/aht20_test_app.c rename to components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/aht20_test_app.c index 5f8b48bc99..811887f66a 100644 --- a/components/sensors/humiture/aht20/test_apps/main/aht20_test_app.c +++ b/components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/aht20_test_app.c @@ -28,7 +28,7 @@ #define I2C_MASTER_FREQ_HZ 100000 // Global handles -i2c_master_bus_handle_t my_i2c_bus_handle = NULL; +i2c_bus_handle_t my_i2c_bus_handle = NULL; aht20_handle_t aht20_handle = NULL; /*******************************Memory Leak Checks****************************/ @@ -64,17 +64,17 @@ void tearDown(void) /*******************************I2C Master Bus Initialization****************************/ void i2c_master_init(void) { - i2c_master_bus_config_t i2c_mst_config = { - .clk_source = I2C_CLK_SRC_DEFAULT, - .i2c_port = I2C_MASTER_NUM, - .scl_io_num = I2C_MASTER_SCL_IO, + const i2c_config_t conf = { + .mode = I2C_MODE_MASTER, .sda_io_num = I2C_MASTER_SDA_IO, - .glitch_ignore_cnt = 7, - .flags.enable_internal_pullup = true, + .sda_pullup_en = GPIO_PULLUP_ENABLE, + .scl_io_num = I2C_MASTER_SCL_IO, + .scl_pullup_en = GPIO_PULLUP_ENABLE, + .master.clk_speed = I2C_MASTER_FREQ_HZ, }; printf("Requesting I2C bus handle\n"); - ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_mst_config, &my_i2c_bus_handle)); + my_i2c_bus_handle = i2c_bus_create(I2C_MASTER_NUM, &conf); printf("I2C bus handle acquired\n"); } /*******************************I2C Master Bus Initialization Over****************************/ @@ -99,7 +99,7 @@ esp_err_t aht20_init_test() void aht20_deinit_test(void) { aht20_remove(&aht20_handle); - i2c_del_master_bus(my_i2c_bus_handle); + i2c_bus_delete(&my_i2c_bus_handle); } /*******************************AHT20 Device Deinitializtion Over****************************/ diff --git a/components/sensors/humiture/aht20/test_apps/main/idf_component.yml b/components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/idf_component.yml similarity index 56% rename from components/sensors/humiture/aht20/test_apps/main/idf_component.yml rename to components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/idf_component.yml index af73e33954..133580640b 100644 --- a/components/sensors/humiture/aht20/test_apps/main/idf_component.yml +++ b/components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/idf_component.yml @@ -1,4 +1,4 @@ dependencies: aht20: version: "*" - override_path: "../../" + override_path: "../../../" diff --git a/components/sensors/humiture/aht20/test_apps/pytest_aht20.py b/components/sensors/humiture/aht20/test_apps/aht20_driver_test/pytest_aht20.py similarity index 100% rename from components/sensors/humiture/aht20/test_apps/pytest_aht20.py rename to components/sensors/humiture/aht20/test_apps/aht20_driver_test/pytest_aht20.py diff --git a/components/sensors/humiture/aht20/test_apps/sdkconfig.defaults b/components/sensors/humiture/aht20/test_apps/aht20_driver_test/sdkconfig.defaults similarity index 100% rename from components/sensors/humiture/aht20/test_apps/sdkconfig.defaults rename to components/sensors/humiture/aht20/test_apps/aht20_driver_test/sdkconfig.defaults diff --git a/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/CMakeLists.txt b/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/CMakeLists.txt new file mode 100644 index 0000000000..abf2334198 --- /dev/null +++ b/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/CMakeLists.txt @@ -0,0 +1,9 @@ +# The following lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +#et(COMPONENTS main) +set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components") + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(aht20_sensor_hub_humiture) diff --git a/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/CMakeLists.txt b/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/CMakeLists.txt new file mode 100644 index 0000000000..20c3c5c281 --- /dev/null +++ b/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "aht20_Sensor_Hub_Humiture.c" + INCLUDE_DIRS "." + PRIV_REQUIRES unity test_utils aht20 sensor_hub) diff --git a/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/Kconfig.projbuild b/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/Kconfig.projbuild new file mode 100644 index 0000000000..7cd0d5ee66 --- /dev/null +++ b/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/Kconfig.projbuild @@ -0,0 +1,15 @@ +menu "Example Configuration" + + config I2C_MASTER_SCL + int "SCL GPIO Num" + default 22 + help + GPIO number for I2C Master clock line. + + config I2C_MASTER_SDA + int "SDA GPIO Num" + default 21 + help + GPIO number for I2C Master data line. + +endmenu diff --git a/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/aht20_Sensor_Hub_Humiture.c b/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/aht20_Sensor_Hub_Humiture.c new file mode 100644 index 0000000000..6b8ad437f5 --- /dev/null +++ b/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/aht20_Sensor_Hub_Humiture.c @@ -0,0 +1,134 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +/*- + * @File: aht20_test_app.c + * + * @brief: AHT20 driver unity test app + * + * @Date: May 2, 2025 + * + * @Author: Rohan Jeet + * + */ + +#include +#include "unity.h" +#include "esp_system.h" + +#include "iot_sensor_hub.h" + +#include "aht20.h" + +#define TEST_MEMORY_LEAK_THRESHOLD (-400) + +// I2C config +#define I2C_MASTER_SCL_IO CONFIG_I2C_MASTER_SCL +#define I2C_MASTER_SDA_IO CONFIG_I2C_MASTER_SDA +#define I2C_MASTER_NUM I2C_NUM_0 +#define I2C_MASTER_FREQ_HZ 100000 + +// Global handles +i2c_bus_handle_t my_i2c_bus_handle = NULL; +sensor_humiture_handle_t aht20 = NULL; + +/******************************* Memory Leak Checks ****************************/ + +static size_t before_free_8bit; +static size_t before_free_32bit; +static size_t after_free_8bit; +static size_t after_free_32bit; + +static void check_leak(size_t before_free, size_t after_free, const char *type) +{ + ssize_t delta = after_free - before_free; + printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta); + TEST_ASSERT_MESSAGE(delta >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak"); +} + +void setUp(void) +{ + before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); + before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); +} + +void tearDown(void) +{ + after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); + after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); + check_leak(before_free_8bit, after_free_8bit, "8BIT"); + check_leak(before_free_32bit, after_free_32bit, "32BIT"); +} + +/************************** Memory Leak Checks Completed ********************/ + +/******************************* I2C Master Bus Initialization ****************************/ +void i2c_master_init(void) +{ + const i2c_config_t conf = { + .mode = I2C_MODE_MASTER, + .sda_io_num = I2C_MASTER_SDA_IO, + .sda_pullup_en = GPIO_PULLUP_ENABLE, + .scl_io_num = I2C_MASTER_SCL_IO, + .scl_pullup_en = GPIO_PULLUP_ENABLE, + .master.clk_speed = I2C_MASTER_FREQ_HZ, + }; + printf("requesting i2c bus handle\n"); + my_i2c_bus_handle = i2c_bus_create(I2C_MASTER_NUM, &conf); + printf("i2c bus handle acquired\n"); +} +/******************************* I2C Master Bus Initialization Over ****************************/ + +/******************************* AHT20 Device Initialization ****************************/ +esp_err_t aht20_init_test() +{ + i2c_master_init(); + + aht20 = humiture_create(my_i2c_bus_handle, "aht20", AHT20_ADDRESS_LOW); + + return ESP_OK; +} +/******************************* AHT20 Device Initialization Over ****************************/ + +/******************************* AHT20 Device Deinitializtion ****************************/ +void aht20_deinit_test(void) +{ + humiture_delete(&aht20); + i2c_bus_delete(&my_i2c_bus_handle); +} +/******************************* AHT20 Device Deinitializtion Over ****************************/ + +/******************************* AHT20 Read sensor ****************************/ +void aht20_read_test(void) +{ + vTaskDelay(400 / portTICK_PERIOD_MS); + + float humidity, temperature; + + humiture_acquire_temperature(aht20, &temperature); + vTaskDelay(2000 / portTICK_PERIOD_MS); + humiture_acquire_humidity(aht20, &humidity); + + printf("Temperature = %.2f°C, Humidity = %.3f%%\n", + temperature, + humidity); +} +/******************************* AHT20 AHT20 Read sensor Over *****************************/ + +/************************************* Test Case **************************/ + +TEST_CASE("AHT20 Sensor", "[aht20][sensor]") +{ + aht20_init_test(); + aht20_read_test(); + aht20_deinit_test(); +} +/************************************* Test Case Over **************************/ + +void app_main(void) +{ + printf("\n=== AHT20 Sensor Test Menu ===\n"); + unity_run_menu(); // Run test selection menu in flash monitor +} diff --git a/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/idf_component.yml b/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/idf_component.yml new file mode 100644 index 0000000000..b8f9e88376 --- /dev/null +++ b/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/idf_component.yml @@ -0,0 +1,7 @@ +dependencies: + aht20: + version: "*" + override_path: "../../../" + sensor_hub: + public: true + override_path: "../../../../../sensor_hub" diff --git a/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/pytest_aht20.py b/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/pytest_aht20.py new file mode 100644 index 0000000000..8db09196d5 --- /dev/null +++ b/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/pytest_aht20.py @@ -0,0 +1,29 @@ +# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Apache-2.0 + +''' +Steps to run these cases: +- Build + - . ${IDF_PATH}/export.sh + - pip install idf_build_apps + - python tools/build_apps.py components/sensors/humiture/aht20/test_apps -t esp32s3 +- Test + - pip install -r tools/requirements/requirement.pytest.txt + - pytest components/sensors/humiture/aht20/test_apps --target esp32s3 +''' + +import pytest +from pytest_embedded import Dut + +@pytest.mark.target('esp32s3') +@pytest.mark.env('esp-box-3') +@pytest.mark.parametrize( + 'config', + [ + 'defaults', + ], +) +def test_aht20(dut: Dut)-> None: + dut.expect_exact('Press ENTER to see the list of tests.') + dut.write('[sensor]') + dut.expect_unity_test_output(timeout = 10000) diff --git a/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/sdkconfig.defaults b/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/sdkconfig.defaults new file mode 100644 index 0000000000..c6cf34620a --- /dev/null +++ b/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/sdkconfig.defaults @@ -0,0 +1,7 @@ +# For IDF 5.0 +CONFIG_ESP_TASK_WDT_EN=n + +# For IDF4.4 +CONFIG_ESP_TASK_WDT=n + +CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG=y diff --git a/examples/sensors/aht20_Sensor_Hub/CMakeLists.txt b/examples/sensors/aht20_Sensor_Hub/CMakeLists.txt new file mode 100644 index 0000000000..36142ed100 --- /dev/null +++ b/examples/sensors/aht20_Sensor_Hub/CMakeLists.txt @@ -0,0 +1,7 @@ +# The following five lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.16) + +set(COMPONENT main) +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(aht20_Sensor_Hub) diff --git a/examples/sensors/aht20_Sensor_Hub/README.md b/examples/sensors/aht20_Sensor_Hub/README.md new file mode 100644 index 0000000000..405a4931e8 --- /dev/null +++ b/examples/sensors/aht20_Sensor_Hub/README.md @@ -0,0 +1,3 @@ +This is a demo which uses the aht20 driver. + +It simply reads the humiture from aht20 sensor and prints the output continuously on terminal. diff --git a/examples/sensors/aht20_Sensor_Hub/main/CMakeLists.txt b/examples/sensors/aht20_Sensor_Hub/main/CMakeLists.txt new file mode 100644 index 0000000000..37d1f861be --- /dev/null +++ b/examples/sensors/aht20_Sensor_Hub/main/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "aht20_demo_sensor_hub.c" + INCLUDE_DIRS "." + REQUIRES "aht20") diff --git a/examples/sensors/aht20_Sensor_Hub/main/Kconfig.projbuild b/examples/sensors/aht20_Sensor_Hub/main/Kconfig.projbuild new file mode 100644 index 0000000000..3018cc2187 --- /dev/null +++ b/examples/sensors/aht20_Sensor_Hub/main/Kconfig.projbuild @@ -0,0 +1,22 @@ +menu "Example Configuration" + + config I2C_MASTER_SCL + int "SCL GPIO Num" + default 22 + help + GPIO number for I2C Master clock line. + + config I2C_MASTER_SDA + int "SDA GPIO Num" + default 21 + help + GPIO number for I2C Master data line. + + config AHT20_SAMPLE_PERIOD + int "AHT20 sampling time(ms)" + default 2000 + range 2000 1000000 + help + Sampling period for AHT20, in ms. + +endmenu diff --git a/examples/sensors/aht20_Sensor_Hub/main/aht20_demo_sensor_hub.c b/examples/sensors/aht20_Sensor_Hub/main/aht20_demo_sensor_hub.c new file mode 100644 index 0000000000..88e02d2264 --- /dev/null +++ b/examples/sensors/aht20_Sensor_Hub/main/aht20_demo_sensor_hub.c @@ -0,0 +1,120 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +/* + * @file: aht20_demo.c + * + * @brief: A simple demo to use the AHT20 driver + * + * @date: May 22, 2025 + * + * @Author: Rohan Jeet + * + */ +#include +#include +#include + +#include "iot_sensor_hub.h" + +#include "aht20.h" + +//i2c configuration values +#define I2C_MASTER_SCL_IO CONFIG_I2C_MASTER_SCL +#define I2C_MASTER_SDA_IO CONFIG_I2C_MASTER_SDA +#define I2C_MASTER_NUM I2C_NUM_0 +#define I2C_MASTER_FREQ_HZ (400000) // I2C frequency + +#define TAG "AHT20_SENSOR_HUB" + +#define SENSOR_PERIOD CONFIG_AHT20_SAMPLE_PERIOD + +i2c_bus_handle_t my_i2c_bus_handle; + +static void sensor_event_handler(void *handler_args, esp_event_base_t base, int32_t id, void *event_data) +{ + sensor_data_t *sensor_data = (sensor_data_t *)event_data; + + switch (id) { + case SENSOR_STARTED: + ESP_LOGI(TAG, "Timestamp = %llu - %s_0x%x STARTED", + sensor_data->timestamp, + sensor_data->sensor_name, + sensor_data->sensor_addr); + break; + case SENSOR_STOPED: + ESP_LOGI(TAG, "Timestamp = %llu - %s_0x%x STOPPED", + sensor_data->timestamp, + sensor_data->sensor_name, + sensor_data->sensor_addr); + break; + case SENSOR_TEMP_DATA_READY: + ESP_LOGI(TAG, "Timestamp = %llu - %s_0x%x TEMP_DATA_READY - " + "temperature=%.2f\n", + sensor_data->timestamp, + sensor_data->sensor_name, + sensor_data->sensor_addr, + sensor_data->temperature); + break; + case SENSOR_HUMI_DATA_READY: + ESP_LOGI(TAG, "Timestamp = %llu - %s_0x%x TEMP_DATA_READY - " + "humidity=%.2f\n", + sensor_data->timestamp, + sensor_data->sensor_name, + sensor_data->sensor_addr, + sensor_data->humidity); + break; + default: + ESP_LOGI(TAG, "Timestamp = %" PRIi64 " - event id = %" PRIi32, sensor_data->timestamp, id); + break; + } +} + +void i2c_master_init(void) +{ + const i2c_config_t conf = { + .mode = I2C_MODE_MASTER, + .sda_io_num = I2C_MASTER_SDA_IO, + .sda_pullup_en = GPIO_PULLUP_ENABLE, + .scl_io_num = I2C_MASTER_SCL_IO, + .scl_pullup_en = GPIO_PULLUP_ENABLE, + .master.clk_speed = I2C_MASTER_FREQ_HZ, + }; + printf("requesting i2c bus handle\n"); + my_i2c_bus_handle = i2c_bus_create(I2C_MASTER_NUM, &conf); + printf("i2c bus handle acquired\n"); + +} + +void init_aht20() +{ + /*create sensor based on sensorID*/ + sensor_handle_t sensor_handle = NULL; + sensor_config_t sensor_config = { + .bus = my_i2c_bus_handle, /*which bus sensors will connect to*/ + .type = HUMITURE_ID, /*sensor type*/ + .addr = AHT20_ADDRESS_LOW, /*sensor addr*/ + .mode = MODE_POLLING, /*data acquire mode*/ + .min_delay = SENSOR_PERIOD /*data acquire period*/ + }; + + iot_sensor_create("aht20", &sensor_config, &sensor_handle); /*create a sensor with specific sensor_id and configurations*/ + + /*register handler with sensor's handle*/ + iot_sensor_handler_register(sensor_handle, sensor_event_handler, NULL); + + iot_sensor_start(sensor_handle); /*start a sensor, data ready events will be posted once data acquired successfully*/ + +} + +void app_main(void) +{ + i2c_master_init(); //initialize i2c master + init_aht20(); // user defined function for aht20 initialization + + while (1) { + vTaskDelay(1000); + } +} diff --git a/examples/sensors/aht20_Sensor_Hub/main/idf_component.yml b/examples/sensors/aht20_Sensor_Hub/main/idf_component.yml new file mode 100644 index 0000000000..210dd57859 --- /dev/null +++ b/examples/sensors/aht20_Sensor_Hub/main/idf_component.yml @@ -0,0 +1,7 @@ +dependencies: + aht20: + version: "*" + override_path: "../../../../components/sensors/humiture/aht20" + sensor_hub: + public: true + override_path: "../../../../components/sensors/sensor_hub" diff --git a/examples/sensors/aht20_demo/CMakeLists.txt b/examples/sensors/aht20_demo/CMakeLists.txt index 614332fcbb..33d7ca6731 100644 --- a/examples/sensors/aht20_demo/CMakeLists.txt +++ b/examples/sensors/aht20_demo/CMakeLists.txt @@ -1,7 +1,9 @@ # The following five lines of boilerplate have to be in your project's # CMakeLists in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.16) +cmake_minimum_required(VERSION 3.5) set(COMPONENT main) +set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components") + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(aht20_demo) diff --git a/examples/sensors/aht20_demo/main/aht20_demo.c b/examples/sensors/aht20_demo/main/aht20_demo.c index 25368cb8f1..2f53cf4f57 100644 --- a/examples/sensors/aht20_demo/main/aht20_demo.c +++ b/examples/sensors/aht20_demo/main/aht20_demo.c @@ -47,14 +47,18 @@ void read_aht20(void *my_aht20_handle) aht20_handle_t aht20_handle = (aht20_handle_t) my_aht20_handle; //retrieve the AHT20 device handle copy vTaskDelay(400 / portTICK_PERIOD_MS); - while (1) { + for (int i = 0; i < 5; i++) { //read both humidity and temperature at once from device, using AHT20 device handle aht20_read_humiture(aht20_handle); //access the results stored in AHT20 device object, using the AHT20 device handle //other apis require user to explicitly pass variable address to hold data printf("tempertature = %.2fC humidity = %.3f \n", aht20_handle->humiture.temperature, aht20_handle->humiture.humidity); - vTaskDelay(1000 / portTICK_PERIOD_MS); + vTaskDelay(2000 / portTICK_PERIOD_MS); } + printf("end of demo\n"); + aht20_remove(&aht20_handle); + i2c_bus_delete(&my_i2c_bus_handle); + vTaskDelete(NULL); } void init_aht20() @@ -67,7 +71,7 @@ void init_aht20() while (aht20_init(aht20_handle) != ESP_OK) { // wait until it is initialized vTaskDelay(100 / portTICK_PERIOD_MS); } - printf("aht20 initialized\n"); + printf("\naht20 initialized\n"); //creating a task to read from AHT20, passing the AHT20 device handle copy xTaskCreate(read_aht20, "aht20_tester", 2500, aht20_handle, 5, NULL); From e216318caa2be3b6ad2b41b1c8ccb11b6fc3d023 Mon Sep 17 00:00:00 2001 From: jeetrohan Date: Fri, 30 May 2025 12:33:59 +0530 Subject: [PATCH 6/8] docs(aht20): improve documentation and update changelog - Expanded documentation with additional details - Fixed a typo in `main.c` of the associated example - Updated `CHANGELOG.md` to reflect recent updates On branch update_aht20 Changes to be committed: modified: components/sensors/humiture/aht20/CHANGELOG.md modified: components/sensors/humiture/aht20/README.md deleted: components/sensors/humiture/aht20/priv_include/aht20_reg.h modified: examples/sensors/aht20_Sensor_Hub/README.md modified: examples/sensors/aht20_Sensor_Hub/main/aht20_demo_sensor_hub.c --- .../sensors/humiture/aht20/CHANGELOG.md | 5 ++-- components/sensors/humiture/aht20/README.md | 25 +++++++++++++++---- .../humiture/aht20/priv_include/aht20_reg.h | 16 ------------ examples/sensors/aht20_Sensor_Hub/README.md | 2 +- .../main/aht20_demo_sensor_hub.c | 15 +++++++---- 5 files changed, 34 insertions(+), 29 deletions(-) delete mode 100644 components/sensors/humiture/aht20/priv_include/aht20_reg.h diff --git a/components/sensors/humiture/aht20/CHANGELOG.md b/components/sensors/humiture/aht20/CHANGELOG.md index 153cd9f984..a2b4eb24cb 100644 --- a/components/sensors/humiture/aht20/CHANGELOG.md +++ b/components/sensors/humiture/aht20/CHANGELOG.md @@ -1,7 +1,8 @@ # ChangeLog -## v1.1.1 (2025-05-02) -* Replace the i2c interface with i2c_bus +## v1.1.1 (2025-05-28) +* Replace the i2c interface with i2c_bus. +* Added support for Sensor Hub. ## v1.0.0 (2024-08-09) diff --git a/components/sensors/humiture/aht20/README.md b/components/sensors/humiture/aht20/README.md index 0c30cac2e3..1d472ee171 100644 --- a/components/sensors/humiture/aht20/README.md +++ b/components/sensors/humiture/aht20/README.md @@ -1,5 +1,5 @@ # Component: AHT20 -I2C driver for Aosong AHT20 humidity and temperature sensor using esp-idf. +I2C driver with Sensor Hub support for Aosong AHT20 humidity and temperature sensor using esp-idf. Tested with AHT20 using ESP32 and ESP32-S3 devkits. # Features @@ -45,13 +45,13 @@ Tested with AHT20 using ESP32 and ESP32-S3 devkits. # How To Use -This driver includes a demo example project. +This driver includes demo examples. -Follow the example to learn how to initialize the driver and read the sensor data. +Follow the examples to learn how to initialize the driver or use Sensor Hub and read the sensor data. All public APIs are documented in aht20.h. - +## Driver Following are the general guidelines. ```c @@ -75,8 +75,23 @@ Following are the general guidelines. printf("tempertature = %uC humidity = %u \n", raw_value.temperature, raw_value.humidity); ``` +## Senosr Hub + +Following are the general guidelines. The sensor config and event handler used are in example. +``` + + /*create a sensor with specific sensor_id and configurations*/ + iot_sensor_create("aht20", &sensor_config, &sensor_handle); + + /*register handler with sensor's handle*/ + iot_sensor_handler_register(sensor_handle, sensor_event_handler, NULL); + + /*start the sensor, data ready events will be posted once data is acquired successfully*/ + iot_sensor_start(sensor_handle); + +``` -# How to Configure CRC and I2C clock speed +## How to Configure CRC and I2C clock speed Additionally, select in menuconfig under Component Config → AHT20; to use CRC(default is not used) or change the clock speed of device (default is 100KHz). diff --git a/components/sensors/humiture/aht20/priv_include/aht20_reg.h b/components/sensors/humiture/aht20/priv_include/aht20_reg.h deleted file mode 100644 index 2ba160e278..0000000000 --- a/components/sensors/humiture/aht20/priv_include/aht20_reg.h +++ /dev/null @@ -1,16 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#pragma once - -/** - * @brief chip information definition - */ -#define CHIP_NAME "ASAIR AHT20" /**< chip name */ -#define SUPPLY_VOLTAGE_MIN (2.2f) /**< chip min supply voltage */ -#define SUPPLY_VOLTAGE_MAX (5.5f) /**< chip max supply voltage */ -#define TEMPERATURE_MIN (-40.0f) /**< chip min operating temperature */ -#define TEMPERATURE_MAX (125.0f) /**< chip max operating temperature */ diff --git a/examples/sensors/aht20_Sensor_Hub/README.md b/examples/sensors/aht20_Sensor_Hub/README.md index 405a4931e8..bbba280329 100644 --- a/examples/sensors/aht20_Sensor_Hub/README.md +++ b/examples/sensors/aht20_Sensor_Hub/README.md @@ -1,3 +1,3 @@ -This is a demo which uses the aht20 driver. +This is a demo which uses the Sensor Hub. It simply reads the humiture from aht20 sensor and prints the output continuously on terminal. diff --git a/examples/sensors/aht20_Sensor_Hub/main/aht20_demo_sensor_hub.c b/examples/sensors/aht20_Sensor_Hub/main/aht20_demo_sensor_hub.c index 88e02d2264..e948bb8380 100644 --- a/examples/sensors/aht20_Sensor_Hub/main/aht20_demo_sensor_hub.c +++ b/examples/sensors/aht20_Sensor_Hub/main/aht20_demo_sensor_hub.c @@ -59,7 +59,7 @@ static void sensor_event_handler(void *handler_args, esp_event_base_t base, int3 sensor_data->temperature); break; case SENSOR_HUMI_DATA_READY: - ESP_LOGI(TAG, "Timestamp = %llu - %s_0x%x TEMP_DATA_READY - " + ESP_LOGI(TAG, "Timestamp = %llu - %s_0x%x HUMIDITY_DATA_READY - " "humidity=%.2f\n", sensor_data->timestamp, sensor_data->sensor_name, @@ -100,19 +100,24 @@ void init_aht20() .min_delay = SENSOR_PERIOD /*data acquire period*/ }; - iot_sensor_create("aht20", &sensor_config, &sensor_handle); /*create a sensor with specific sensor_id and configurations*/ + /*create a sensor with specific sensor_id and configurations*/ + iot_sensor_create("aht20", &sensor_config, &sensor_handle); /*register handler with sensor's handle*/ iot_sensor_handler_register(sensor_handle, sensor_event_handler, NULL); - iot_sensor_start(sensor_handle); /*start a sensor, data ready events will be posted once data acquired successfully*/ + /*start a sensor, data ready events will be posted once data acquired successfully*/ + iot_sensor_start(sensor_handle); } void app_main(void) { - i2c_master_init(); //initialize i2c master - init_aht20(); // user defined function for aht20 initialization + //initialize i2c master + i2c_master_init(); + + // user defined function for aht20 initialization + init_aht20(); while (1) { vTaskDelay(1000); From 57caca267a430e72c3e81da71b361bb3c3f451ff Mon Sep 17 00:00:00 2001 From: jeetrohan Date: Mon, 2 Jun 2025 13:17:40 +0530 Subject: [PATCH 7/8] refactor(aht20): update to use i2c_bus, add Kconfig options, bump to v1.1.0 - Replaced legacy I2C operations with esp-iot-solution's i2c_bus component - Added Kconfig options for CRC check and I2C clock speed configuration - Updated driver version from 1.0.0 to 1.1.0 - Updated documentation and test application accordingly Changes to be committed: --- .../sensors/humiture/aht20/CHANGELOG.md | 1 - components/sensors/humiture/aht20/README.md | 34 ++-- components/sensors/humiture/aht20/aht20.c | 181 +++++++++--------- .../sensors/humiture/aht20/idf_component.yml | 3 - .../sensors/humiture/aht20/include/aht20.h | 72 +------ .../aht20_driver_test/main/aht20_test_app.c | 27 ++- .../aht20_sensor_hub_humiture/CMakeLists.txt | 9 - .../main/CMakeLists.txt | 3 - .../main/Kconfig.projbuild | 15 -- .../main/aht20_Sensor_Hub_Humiture.c | 134 ------------- .../main/idf_component.yml | 7 - .../aht20_sensor_hub_humiture/pytest_aht20.py | 29 --- .../sdkconfig.defaults | 7 - .../sensors/aht20_Sensor_Hub/CMakeLists.txt | 7 - examples/sensors/aht20_Sensor_Hub/README.md | 3 - .../aht20_Sensor_Hub/main/CMakeLists.txt | 3 - .../aht20_Sensor_Hub/main/Kconfig.projbuild | 22 --- .../main/aht20_demo_sensor_hub.c | 125 ------------ .../aht20_Sensor_Hub/main/idf_component.yml | 7 - examples/sensors/aht20_demo/CMakeLists.txt | 9 - examples/sensors/aht20_demo/README.md | 3 - .../sensors/aht20_demo/main/CMakeLists.txt | 3 - examples/sensors/aht20_demo/main/aht20_demo.c | 84 -------- .../sensors/aht20_demo/main/idf_component.yml | 4 - 24 files changed, 123 insertions(+), 669 deletions(-) delete mode 100644 components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/CMakeLists.txt delete mode 100644 components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/CMakeLists.txt delete mode 100644 components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/Kconfig.projbuild delete mode 100644 components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/aht20_Sensor_Hub_Humiture.c delete mode 100644 components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/idf_component.yml delete mode 100644 components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/pytest_aht20.py delete mode 100644 components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/sdkconfig.defaults delete mode 100644 examples/sensors/aht20_Sensor_Hub/CMakeLists.txt delete mode 100644 examples/sensors/aht20_Sensor_Hub/README.md delete mode 100644 examples/sensors/aht20_Sensor_Hub/main/CMakeLists.txt delete mode 100644 examples/sensors/aht20_Sensor_Hub/main/Kconfig.projbuild delete mode 100644 examples/sensors/aht20_Sensor_Hub/main/aht20_demo_sensor_hub.c delete mode 100644 examples/sensors/aht20_Sensor_Hub/main/idf_component.yml delete mode 100644 examples/sensors/aht20_demo/CMakeLists.txt delete mode 100644 examples/sensors/aht20_demo/README.md delete mode 100644 examples/sensors/aht20_demo/main/CMakeLists.txt delete mode 100644 examples/sensors/aht20_demo/main/aht20_demo.c delete mode 100644 examples/sensors/aht20_demo/main/idf_component.yml diff --git a/components/sensors/humiture/aht20/CHANGELOG.md b/components/sensors/humiture/aht20/CHANGELOG.md index a2b4eb24cb..d24bde1193 100644 --- a/components/sensors/humiture/aht20/CHANGELOG.md +++ b/components/sensors/humiture/aht20/CHANGELOG.md @@ -2,7 +2,6 @@ ## v1.1.1 (2025-05-28) * Replace the i2c interface with i2c_bus. -* Added support for Sensor Hub. ## v1.0.0 (2024-08-09) diff --git a/components/sensors/humiture/aht20/README.md b/components/sensors/humiture/aht20/README.md index 1d472ee171..dc6c59b456 100644 --- a/components/sensors/humiture/aht20/README.md +++ b/components/sensors/humiture/aht20/README.md @@ -8,9 +8,9 @@ Tested with AHT20 using ESP32 and ESP32-S3 devkits. Thread-safe via esp-i2c-driver - CRC checksum verification (optional via menuconfig) + CRC checksum verification (optional, only via menuconfig) - Configurable I2C clock speed (pre-compilation, not runtime,via menuconfig)) + Configurable I2C clock speed (pre-compilation, not runtime, only via menuconfig) Unit tested @@ -45,10 +45,6 @@ Tested with AHT20 using ESP32 and ESP32-S3 devkits. # How To Use -This driver includes demo examples. - -Follow the examples to learn how to initialize the driver or use Sensor Hub and read the sensor data. - All public APIs are documented in aht20.h. ## Driver @@ -56,28 +52,30 @@ All public APIs are documented in aht20.h. Following are the general guidelines. ```c //create a AHT20 device object and receive a device handle for it - // my_i2c_bus_handle is a preintialized i2c_bus_handle_t object + // my_i2c_bus_handle here is a preintialized i2c_bus_handle_t i2c_bus object aht20_handle_t aht20_handle = aht20_create( my_i2c_bus_handle, AHT20_ADDRESS_LOW ); //addresses are in aht20.h - //use the previously created AHT20 device handle for initializing the associated device + //use the previously created AHT20 device handle for initializing the AHT20 aht20_init(aht20_handle); - //read both humidity and temperature at once from device, using AHT20 device handle - aht20_read_humiture(aht20_handle); //Other public APIs are documented in aht20.h. + float_t temperature; - //access the results stored in AHT20 device object, using the AHT20 device handle - //other apis require user to explicitly pass variable address to hold data - printf("tempertature = %.2fC humidity = %.3f \n", aht20_handle->humiture.temperature, aht20_handle->humiture.humidity); + aht20_read_temperature( aht20_handle, &temperature); - //to get reaw values create a object of following data type - aht20_raw_reading_t raw_value; - aht20_read_raw( aht20_handle, &raw_value); - printf("tempertature = %uC humidity = %u \n", raw_value.temperature, raw_value.humidity); + printf("Temperature = %.2f°C\n", temperature); + + vTaskDelay(pdMS_TO_TICKS(2000)); + + float_t temperature; + + aht20_read_temperature( aht20_handle, &temperature); + + printf("Temperature = %.2f°C\n", temperature); ``` ## Senosr Hub -Following are the general guidelines. The sensor config and event handler used are in example. +Following are the general guidelines. ``` /*create a sensor with specific sensor_id and configurations*/ diff --git a/components/sensors/humiture/aht20/aht20.c b/components/sensors/humiture/aht20/aht20.c index 23df1ad3a6..dc633c751b 100644 --- a/components/sensors/humiture/aht20/aht20.c +++ b/components/sensors/humiture/aht20/aht20.c @@ -20,8 +20,18 @@ #include "aht20.h" +/******************************************** Private *********************************************/ + static const char *s_TAG = "AHT20"; +/** + *@brief AHT20 raw result + */ +typedef struct { + uint32_t humidity; /*!< raw humidity reading. */ + uint32_t temperature; /*!< raw temperature reading. */ +} aht20_raw_reading_t; + /** * @brief a function used to handle resetting of registers of the device, if not found calibrated when initialized * @@ -77,6 +87,20 @@ static esp_err_t aht20_JH_Reset_REG(aht20_handle_t aht20_handle, uint8_t addr); */ static uint8_t calc_CRC8(uint8_t *message, uint8_t Num); +/** +* @brief check AHT20 measurement status +* +* @param[in] aht20_handle AHT20 device handle +* +* @param[out]busy busy in measurement if value is true +* +* @return +* - ESP_OK: successfully read AHT20 busy status +* - other error codes : failure in reading AHT20 busy status +* +*/ +static esp_err_t aht20_busy_status(aht20_handle_t aht20_handle, bool *busy); + static esp_err_t aht20_read_reg(aht20_handle_t sensor, uint8_t * read_buffer, uint8_t read_size) { ESP_RETURN_ON_ERROR(i2c_bus_read_bytes(sensor->i2c_dev, NULL_I2C_MEM_ADDR, read_size, read_buffer), @@ -93,6 +117,57 @@ static esp_err_t aht20_write_reg(aht20_handle_t sensor, uint8_t * cmd, uint8_t w return ESP_OK; } +static esp_err_t aht20_busy_status(aht20_handle_t aht20_handle, bool *busy) +{ + ESP_RETURN_ON_FALSE((aht20_handle != NULL), ESP_ERR_INVALID_ARG, + s_TAG, "empty handle, initialize AHT20 handle"); + + ESP_RETURN_ON_FALSE((busy != NULL), ESP_ERR_INVALID_ARG, + s_TAG, "provide a variable to store status value"); + uint8_t read_status; + ESP_RETURN_ON_ERROR(aht20_read_reg(aht20_handle, &read_status, sizeof(read_status)), + s_TAG, "unable to read status"); + + if (read_status & BIT7) { + *busy = true; + } else { + *busy = false; + } + + return ESP_OK; +} + +static esp_err_t aht20_JH_Reset_REG(aht20_handle_t aht20_handle, uint8_t addr) +{ + + uint8_t reset_cmd[] = {addr, 0x00, 0x00}, read_bytes[3]; + + ESP_RETURN_ON_ERROR(aht20_write_reg(aht20_handle, reset_cmd, sizeof(reset_cmd)), + s_TAG, "unable to reset, check log"); + + ESP_RETURN_ON_ERROR(aht20_read_reg(aht20_handle, read_bytes, sizeof(read_bytes)), + s_TAG, "unable to reset, check log"); + + vTaskDelay(10 / portTICK_PERIOD_MS); + reset_cmd[0] = 0xB0 | addr; + reset_cmd[1] = read_bytes[1]; + reset_cmd[2] = read_bytes[2]; + + ESP_RETURN_ON_ERROR(aht20_write_reg(aht20_handle, reset_cmd, sizeof(reset_cmd)), + s_TAG, "unable to reset, check log"); + + return ESP_OK; +} + +static esp_err_t aht20_Start_Init(aht20_handle_t aht20_handle) +{ + ESP_RETURN_ON_ERROR(aht20_JH_Reset_REG(aht20_handle, 0x1b), "", ""); + ESP_RETURN_ON_ERROR(aht20_JH_Reset_REG(aht20_handle, 0x1c), "", ""); + ESP_RETURN_ON_ERROR(aht20_JH_Reset_REG(aht20_handle, 0x1e), "", ""); + + return ESP_OK; +} + static uint8_t calc_CRC8(uint8_t *message, uint8_t Num) { uint8_t i; @@ -111,6 +186,10 @@ static uint8_t calc_CRC8(uint8_t *message, uint8_t Num) return crc; } +/**************************************************************************************************/ + +/******************************************** Public *********************************************/ + esp_err_t aht20_read_raw(aht20_handle_t aht20_handle, aht20_raw_reading_t *raw_read) { ESP_RETURN_ON_FALSE((aht20_handle != NULL), ESP_ERR_INVALID_ARG, s_TAG, "empty handle, provide a valid AHT20 handle"); @@ -145,23 +224,10 @@ esp_err_t aht20_read_raw(aht20_handle_t aht20_handle, aht20_raw_reading_t *raw_r return ESP_OK; } -esp_err_t aht20_read_humiture(aht20_handle_t aht20_handle) -{ - aht20_raw_reading_t raw_read; - ESP_RETURN_ON_ERROR(aht20_read_raw(aht20_handle, &raw_read), - "", ""); - - aht20_handle->humiture.humidity = raw_read.humidity * 100.0 / 1024 / 1024; //Calculated humidity value - aht20_handle->humiture.temperature = (raw_read.temperature * 200.0 / 1024 / 1024) - 50; //Calculated temperature value - - return ESP_OK; -} - esp_err_t aht20_read_humidity(aht20_handle_t aht20_handle, float_t *humidity) { aht20_raw_reading_t raw_read; - ESP_RETURN_ON_ERROR(aht20_read_raw(aht20_handle, &raw_read), - "", ""); + ESP_RETURN_ON_ERROR(aht20_read_raw(aht20_handle, &raw_read), "", ""); *humidity = raw_read.humidity * 100.0 / 1024 / 1024; //Calculated humidity value return ESP_OK; @@ -170,87 +236,13 @@ esp_err_t aht20_read_humidity(aht20_handle_t aht20_handle, float_t *humidity) esp_err_t aht20_read_temperature(aht20_handle_t aht20_handle, float_t *temperature) { aht20_raw_reading_t raw_read; - ESP_RETURN_ON_ERROR(aht20_read_raw(aht20_handle, &raw_read), - "", ""); + ESP_RETURN_ON_ERROR(aht20_read_raw(aht20_handle, &raw_read), "", ""); *temperature = (raw_read.temperature * 200.0 / 1024 / 1024) - 50; //Calculated temperature value return ESP_OK; } -esp_err_t aht20_calibration_status(aht20_handle_t aht20_handle, bool *calibration) -{ - ESP_RETURN_ON_FALSE((aht20_handle != NULL), ESP_ERR_INVALID_ARG, - s_TAG, "empty handle, initialize AHT20 handle"); - - ESP_RETURN_ON_FALSE((calibration != NULL), ESP_ERR_INVALID_ARG, - s_TAG, "provide a variable to store status value"); - - uint8_t read_status; - - ESP_RETURN_ON_ERROR(aht20_read_reg(aht20_handle, &read_status, sizeof(read_status)), - s_TAG, "unable to read status"); - - if (read_status & BIT3) { - *calibration = true; - } else { - *calibration = false; - } - - return ESP_OK; -} - -esp_err_t aht20_busy_status(aht20_handle_t aht20_handle, bool *busy) -{ - ESP_RETURN_ON_FALSE((aht20_handle != NULL), ESP_ERR_INVALID_ARG, - s_TAG, "empty handle, initialize AHT20 handle"); - - ESP_RETURN_ON_FALSE((busy != NULL), ESP_ERR_INVALID_ARG, - s_TAG, "provide a variable to store status value"); - uint8_t read_status; - ESP_RETURN_ON_ERROR(aht20_read_reg(aht20_handle, &read_status, sizeof(read_status)), - s_TAG, "unable to read status"); - - if (read_status & BIT7) { - *busy = true; - } else { - *busy = false; - } - - return ESP_OK; -} - -static esp_err_t aht20_JH_Reset_REG(aht20_handle_t aht20_handle, uint8_t addr) -{ - - uint8_t reset_cmd[] = {addr, 0x00, 0x00}, read_bytes[3]; - - ESP_RETURN_ON_ERROR(aht20_write_reg(aht20_handle, reset_cmd, sizeof(reset_cmd)), - s_TAG, "unable to reset, check log"); - - ESP_RETURN_ON_ERROR(aht20_read_reg(aht20_handle, read_bytes, sizeof(read_bytes)), - s_TAG, "unable to reset, check log"); - - vTaskDelay(10 / portTICK_PERIOD_MS); - reset_cmd[0] = 0xB0 | addr; - reset_cmd[1] = read_bytes[1]; - reset_cmd[2] = read_bytes[2]; - - ESP_RETURN_ON_ERROR(aht20_write_reg(aht20_handle, reset_cmd, sizeof(reset_cmd)), - s_TAG, "unable to reset, check log"); - - return ESP_OK; -} - -static esp_err_t aht20_Start_Init(aht20_handle_t aht20_handle) -{ - ESP_RETURN_ON_ERROR(aht20_JH_Reset_REG(aht20_handle, 0x1b), "", ""); - ESP_RETURN_ON_ERROR(aht20_JH_Reset_REG(aht20_handle, 0x1c), "", ""); - ESP_RETURN_ON_ERROR(aht20_JH_Reset_REG(aht20_handle, 0x1e), "", ""); - - return ESP_OK; -} - esp_err_t aht20_init(aht20_handle_t aht20_handle) { ESP_RETURN_ON_FALSE((aht20_handle != NULL), ESP_ERR_INVALID_ARG, s_TAG, "empty handle, initialize AHT20 handle"); @@ -290,14 +282,12 @@ aht20_handle_t aht20_create(i2c_bus_handle_t bus_handle, uint8_t aht20_address) { ESP_LOGI(s_TAG, "adding aht20 as device to bus\n"); i2c_bus_device_handle_t dev_handle = i2c_bus_device_create(bus_handle, aht20_address, CONFIG_AHT20_I2C_CLK_SPEED); - ESP_RETURN_ON_FALSE((dev_handle != NULL), NULL, - s_TAG, "unable to create device\n"); + ESP_RETURN_ON_FALSE((dev_handle != NULL), NULL, s_TAG, "unable to create device\n"); ESP_LOGI(s_TAG, "device added to bus\n"); aht20_handle_t my_aht20_handle = malloc(sizeof(aht20_dev_config_t)); - ESP_RETURN_ON_FALSE((my_aht20_handle != NULL), NULL, - s_TAG, "unable to allocate memory to initialize aht20 handle"); + ESP_RETURN_ON_FALSE((my_aht20_handle != NULL), NULL, s_TAG, "unable to allocate memory to initialize aht20 handle"); my_aht20_handle->i2c_dev = dev_handle; return my_aht20_handle; @@ -314,6 +304,9 @@ esp_err_t aht20_remove(aht20_handle_t *aht20ptr) return ESP_OK; } +/*************************************************************************************************/ + +/******************************************** Sensor Hub *********************************************/ #ifdef CONFIG_SENSOR_INCLUDED_HUMITURE static aht20_handle_t aht20 = NULL; @@ -404,3 +397,5 @@ static humiture_impl_t aht20_impl = { SENSOR_HUB_DETECT_FN(HUMITURE_ID, aht20, &aht20_impl); #endif + +/**********************************************************************************************/ diff --git a/components/sensors/humiture/aht20/idf_component.yml b/components/sensors/humiture/aht20/idf_component.yml index ee05baad82..28813f2d4e 100644 --- a/components/sensors/humiture/aht20/idf_component.yml +++ b/components/sensors/humiture/aht20/idf_component.yml @@ -24,9 +24,6 @@ dependencies: idf: ">=4.0" -examples: - - path: ../../../examples/sensors/aht20_demo - - path: ../../../examples/sensors/aht20_Sensor_Hub targets: - esp32 diff --git a/components/sensors/humiture/aht20/include/aht20.h b/components/sensors/humiture/aht20/include/aht20.h index 9cd474953c..2db90fc557 100644 --- a/components/sensors/humiture/aht20/include/aht20.h +++ b/components/sensors/humiture/aht20/include/aht20.h @@ -42,28 +42,11 @@ extern "C" { #define AHT20_INIT_REG 0XBE /*!< initialize the AHT20 */ #define AHT20_MEASURE_CYC 0xAC /*!< trigger measurement in cycle mode */ -/** - *@brief AHT20 result - */ -typedef struct { - float_t humidity; /*!< humidity reading. */ - float_t temperature; /*!< temperature reading. */ -} aht20_reading_t; - -/** - *@brief AHT20 raw result - */ -typedef struct { - uint32_t humidity; /*!< raw humidity reading. */ - uint32_t temperature; /*!< raw temperature reading. */ -} aht20_raw_reading_t; - /** * @brief AHT20 device object */ typedef struct { i2c_bus_device_handle_t i2c_dev; /*!< i2c device handle. */ - aht20_reading_t humiture; /*!< AHT20 reading. */ } aht20_dev_config_t; /** @@ -83,32 +66,6 @@ typedef aht20_dev_config_t *aht20_handle_t; */ esp_err_t aht20_reset(aht20_handle_t aht20_handle); -/** -* @brief get AHT20 sensor raw readings -*- -* @param[in] aht20_handle AHT20 device handle -* -* @param[out] raw_read raw reading -* -* @return -* - ESP_OK: successful read -* - other error codes : failed to read -* -*/ -esp_err_t aht20_read_raw(aht20_handle_t aht20_handle, aht20_raw_reading_t *raw_read); - -/** -* @brief get AHT20 sensor readings -*- -* @param[in] aht20_handle AHT20 device handle -* -* @return -* - ESP_OK: successful read -* - other error codes : failed to read -* -*/ -esp_err_t aht20_read_humiture(aht20_handle_t aht20_handle); - /** * @brief get AHT20 humidity readings * @@ -121,6 +78,7 @@ esp_err_t aht20_read_humiture(aht20_handle_t aht20_handle); * - other error codes : failed to read * */ + esp_err_t aht20_read_humidity(aht20_handle_t aht20_handle, float_t *humidity); /** @@ -137,34 +95,6 @@ esp_err_t aht20_read_humidity(aht20_handle_t aht20_handle, float_t *humidity); */ esp_err_t aht20_read_temperature(aht20_handle_t aht20_handle, float_t *temperature); -/** -* @brief check AHT20 calibration status -* -* @param[in] aht20_handle AHT20 device handle -* -* @param[out] calibration calibrated if value is true -* -* @return -* - ESP_OK: successfully read AHT20 calibration status -* - other error codes : failure in reading AHT20 calibration status -* -*/ -esp_err_t aht20_calibration_status(aht20_handle_t aht20_handle, bool *calibration); - -/** -* @brief check AHT20 measurement status -* -* @param[in] aht20_handle AHT20 device handle -* -* @param[out]busy busy in measurement if value is true -* -* @return -* - ESP_OK: successfully read AHT20 busy status -* - other error codes : failure in reading AHT20 busy status -* -*/ -esp_err_t aht20_busy_status(aht20_handle_t aht20_handle, bool *busy); - /** * @brief Initialize the AHT20 * diff --git a/components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/aht20_test_app.c b/components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/aht20_test_app.c index 811887f66a..e16d5b211c 100644 --- a/components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/aht20_test_app.c +++ b/components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/aht20_test_app.c @@ -87,7 +87,7 @@ esp_err_t aht20_init_test() printf("Initializing AHT20 sensor\n"); while (aht20_init(aht20_handle) != ESP_OK) { - vTaskDelay(100 / portTICK_PERIOD_MS); + vTaskDelay(pdMS_TO_TICKS(100)); } printf("AHT20 initialized\n"); @@ -104,17 +104,24 @@ void aht20_deinit_test(void) /*******************************AHT20 Device Deinitializtion Over****************************/ /*******************************AHT20 Read sensor****************************/ -void aht20_read_test(void) +void aht20_check_humidity(void) { - vTaskDelay(400 / portTICK_PERIOD_MS); + float_t humidity; - TEST_ASSERT(ESP_OK == aht20_read_humiture(aht20_handle)); + TEST_ASSERT(ESP_OK == aht20_read_humidity(aht20_handle, &humidity)); - printf("Temperature = %.2f°C, Humidity = %.3f%%\n", - aht20_handle->humiture.temperature, - aht20_handle->humiture.humidity); + printf("Humidity = %.2f%%\n", humidity); + +} + +void aht20_check_temprature(void) +{ + float_t temperature; + + TEST_ASSERT(ESP_OK == aht20_read_temperature(aht20_handle, &temperature)); + + printf("Temperature = %.2f°C\n", temperature); - vTaskDelay(1000 / portTICK_PERIOD_MS); } /*******************************AHT20 AHT20 Read sensor Over*****************************/ @@ -123,7 +130,9 @@ void aht20_read_test(void) TEST_CASE("AHT20 Sensor", "[aht20][sensor]") { aht20_init_test(); - aht20_read_test(); + aht20_check_humidity(); + vTaskDelay(pdMS_TO_TICKS(2000)); + aht20_check_temprature(); aht20_deinit_test(); } /*************************************Test Case Over**************************/ diff --git a/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/CMakeLists.txt b/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/CMakeLists.txt deleted file mode 100644 index abf2334198..0000000000 --- a/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -# The following lines of boilerplate have to be in your project's CMakeLists -# in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.5) - -#et(COMPONENTS main) -set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components") - -include($ENV{IDF_PATH}/tools/cmake/project.cmake) -project(aht20_sensor_hub_humiture) diff --git a/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/CMakeLists.txt b/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/CMakeLists.txt deleted file mode 100644 index 20c3c5c281..0000000000 --- a/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -idf_component_register(SRCS "aht20_Sensor_Hub_Humiture.c" - INCLUDE_DIRS "." - PRIV_REQUIRES unity test_utils aht20 sensor_hub) diff --git a/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/Kconfig.projbuild b/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/Kconfig.projbuild deleted file mode 100644 index 7cd0d5ee66..0000000000 --- a/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/Kconfig.projbuild +++ /dev/null @@ -1,15 +0,0 @@ -menu "Example Configuration" - - config I2C_MASTER_SCL - int "SCL GPIO Num" - default 22 - help - GPIO number for I2C Master clock line. - - config I2C_MASTER_SDA - int "SDA GPIO Num" - default 21 - help - GPIO number for I2C Master data line. - -endmenu diff --git a/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/aht20_Sensor_Hub_Humiture.c b/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/aht20_Sensor_Hub_Humiture.c deleted file mode 100644 index 6b8ad437f5..0000000000 --- a/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/aht20_Sensor_Hub_Humiture.c +++ /dev/null @@ -1,134 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -/*- - * @File: aht20_test_app.c - * - * @brief: AHT20 driver unity test app - * - * @Date: May 2, 2025 - * - * @Author: Rohan Jeet - * - */ - -#include -#include "unity.h" -#include "esp_system.h" - -#include "iot_sensor_hub.h" - -#include "aht20.h" - -#define TEST_MEMORY_LEAK_THRESHOLD (-400) - -// I2C config -#define I2C_MASTER_SCL_IO CONFIG_I2C_MASTER_SCL -#define I2C_MASTER_SDA_IO CONFIG_I2C_MASTER_SDA -#define I2C_MASTER_NUM I2C_NUM_0 -#define I2C_MASTER_FREQ_HZ 100000 - -// Global handles -i2c_bus_handle_t my_i2c_bus_handle = NULL; -sensor_humiture_handle_t aht20 = NULL; - -/******************************* Memory Leak Checks ****************************/ - -static size_t before_free_8bit; -static size_t before_free_32bit; -static size_t after_free_8bit; -static size_t after_free_32bit; - -static void check_leak(size_t before_free, size_t after_free, const char *type) -{ - ssize_t delta = after_free - before_free; - printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta); - TEST_ASSERT_MESSAGE(delta >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak"); -} - -void setUp(void) -{ - before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); - before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); -} - -void tearDown(void) -{ - after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT); - after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT); - check_leak(before_free_8bit, after_free_8bit, "8BIT"); - check_leak(before_free_32bit, after_free_32bit, "32BIT"); -} - -/************************** Memory Leak Checks Completed ********************/ - -/******************************* I2C Master Bus Initialization ****************************/ -void i2c_master_init(void) -{ - const i2c_config_t conf = { - .mode = I2C_MODE_MASTER, - .sda_io_num = I2C_MASTER_SDA_IO, - .sda_pullup_en = GPIO_PULLUP_ENABLE, - .scl_io_num = I2C_MASTER_SCL_IO, - .scl_pullup_en = GPIO_PULLUP_ENABLE, - .master.clk_speed = I2C_MASTER_FREQ_HZ, - }; - printf("requesting i2c bus handle\n"); - my_i2c_bus_handle = i2c_bus_create(I2C_MASTER_NUM, &conf); - printf("i2c bus handle acquired\n"); -} -/******************************* I2C Master Bus Initialization Over ****************************/ - -/******************************* AHT20 Device Initialization ****************************/ -esp_err_t aht20_init_test() -{ - i2c_master_init(); - - aht20 = humiture_create(my_i2c_bus_handle, "aht20", AHT20_ADDRESS_LOW); - - return ESP_OK; -} -/******************************* AHT20 Device Initialization Over ****************************/ - -/******************************* AHT20 Device Deinitializtion ****************************/ -void aht20_deinit_test(void) -{ - humiture_delete(&aht20); - i2c_bus_delete(&my_i2c_bus_handle); -} -/******************************* AHT20 Device Deinitializtion Over ****************************/ - -/******************************* AHT20 Read sensor ****************************/ -void aht20_read_test(void) -{ - vTaskDelay(400 / portTICK_PERIOD_MS); - - float humidity, temperature; - - humiture_acquire_temperature(aht20, &temperature); - vTaskDelay(2000 / portTICK_PERIOD_MS); - humiture_acquire_humidity(aht20, &humidity); - - printf("Temperature = %.2f°C, Humidity = %.3f%%\n", - temperature, - humidity); -} -/******************************* AHT20 AHT20 Read sensor Over *****************************/ - -/************************************* Test Case **************************/ - -TEST_CASE("AHT20 Sensor", "[aht20][sensor]") -{ - aht20_init_test(); - aht20_read_test(); - aht20_deinit_test(); -} -/************************************* Test Case Over **************************/ - -void app_main(void) -{ - printf("\n=== AHT20 Sensor Test Menu ===\n"); - unity_run_menu(); // Run test selection menu in flash monitor -} diff --git a/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/idf_component.yml b/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/idf_component.yml deleted file mode 100644 index b8f9e88376..0000000000 --- a/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/main/idf_component.yml +++ /dev/null @@ -1,7 +0,0 @@ -dependencies: - aht20: - version: "*" - override_path: "../../../" - sensor_hub: - public: true - override_path: "../../../../../sensor_hub" diff --git a/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/pytest_aht20.py b/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/pytest_aht20.py deleted file mode 100644 index 8db09196d5..0000000000 --- a/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/pytest_aht20.py +++ /dev/null @@ -1,29 +0,0 @@ -# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD -# SPDX-License-Identifier: Apache-2.0 - -''' -Steps to run these cases: -- Build - - . ${IDF_PATH}/export.sh - - pip install idf_build_apps - - python tools/build_apps.py components/sensors/humiture/aht20/test_apps -t esp32s3 -- Test - - pip install -r tools/requirements/requirement.pytest.txt - - pytest components/sensors/humiture/aht20/test_apps --target esp32s3 -''' - -import pytest -from pytest_embedded import Dut - -@pytest.mark.target('esp32s3') -@pytest.mark.env('esp-box-3') -@pytest.mark.parametrize( - 'config', - [ - 'defaults', - ], -) -def test_aht20(dut: Dut)-> None: - dut.expect_exact('Press ENTER to see the list of tests.') - dut.write('[sensor]') - dut.expect_unity_test_output(timeout = 10000) diff --git a/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/sdkconfig.defaults b/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/sdkconfig.defaults deleted file mode 100644 index c6cf34620a..0000000000 --- a/components/sensors/humiture/aht20/test_apps/aht20_sensor_hub_humiture/sdkconfig.defaults +++ /dev/null @@ -1,7 +0,0 @@ -# For IDF 5.0 -CONFIG_ESP_TASK_WDT_EN=n - -# For IDF4.4 -CONFIG_ESP_TASK_WDT=n - -CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG=y diff --git a/examples/sensors/aht20_Sensor_Hub/CMakeLists.txt b/examples/sensors/aht20_Sensor_Hub/CMakeLists.txt deleted file mode 100644 index 36142ed100..0000000000 --- a/examples/sensors/aht20_Sensor_Hub/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -# The following five lines of boilerplate have to be in your project's -# CMakeLists in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.16) - -set(COMPONENT main) -include($ENV{IDF_PATH}/tools/cmake/project.cmake) -project(aht20_Sensor_Hub) diff --git a/examples/sensors/aht20_Sensor_Hub/README.md b/examples/sensors/aht20_Sensor_Hub/README.md deleted file mode 100644 index bbba280329..0000000000 --- a/examples/sensors/aht20_Sensor_Hub/README.md +++ /dev/null @@ -1,3 +0,0 @@ -This is a demo which uses the Sensor Hub. - -It simply reads the humiture from aht20 sensor and prints the output continuously on terminal. diff --git a/examples/sensors/aht20_Sensor_Hub/main/CMakeLists.txt b/examples/sensors/aht20_Sensor_Hub/main/CMakeLists.txt deleted file mode 100644 index 37d1f861be..0000000000 --- a/examples/sensors/aht20_Sensor_Hub/main/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -idf_component_register(SRCS "aht20_demo_sensor_hub.c" - INCLUDE_DIRS "." - REQUIRES "aht20") diff --git a/examples/sensors/aht20_Sensor_Hub/main/Kconfig.projbuild b/examples/sensors/aht20_Sensor_Hub/main/Kconfig.projbuild deleted file mode 100644 index 3018cc2187..0000000000 --- a/examples/sensors/aht20_Sensor_Hub/main/Kconfig.projbuild +++ /dev/null @@ -1,22 +0,0 @@ -menu "Example Configuration" - - config I2C_MASTER_SCL - int "SCL GPIO Num" - default 22 - help - GPIO number for I2C Master clock line. - - config I2C_MASTER_SDA - int "SDA GPIO Num" - default 21 - help - GPIO number for I2C Master data line. - - config AHT20_SAMPLE_PERIOD - int "AHT20 sampling time(ms)" - default 2000 - range 2000 1000000 - help - Sampling period for AHT20, in ms. - -endmenu diff --git a/examples/sensors/aht20_Sensor_Hub/main/aht20_demo_sensor_hub.c b/examples/sensors/aht20_Sensor_Hub/main/aht20_demo_sensor_hub.c deleted file mode 100644 index e948bb8380..0000000000 --- a/examples/sensors/aht20_Sensor_Hub/main/aht20_demo_sensor_hub.c +++ /dev/null @@ -1,125 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -/* - * @file: aht20_demo.c - * - * @brief: A simple demo to use the AHT20 driver - * - * @date: May 22, 2025 - * - * @Author: Rohan Jeet - * - */ -#include -#include -#include - -#include "iot_sensor_hub.h" - -#include "aht20.h" - -//i2c configuration values -#define I2C_MASTER_SCL_IO CONFIG_I2C_MASTER_SCL -#define I2C_MASTER_SDA_IO CONFIG_I2C_MASTER_SDA -#define I2C_MASTER_NUM I2C_NUM_0 -#define I2C_MASTER_FREQ_HZ (400000) // I2C frequency - -#define TAG "AHT20_SENSOR_HUB" - -#define SENSOR_PERIOD CONFIG_AHT20_SAMPLE_PERIOD - -i2c_bus_handle_t my_i2c_bus_handle; - -static void sensor_event_handler(void *handler_args, esp_event_base_t base, int32_t id, void *event_data) -{ - sensor_data_t *sensor_data = (sensor_data_t *)event_data; - - switch (id) { - case SENSOR_STARTED: - ESP_LOGI(TAG, "Timestamp = %llu - %s_0x%x STARTED", - sensor_data->timestamp, - sensor_data->sensor_name, - sensor_data->sensor_addr); - break; - case SENSOR_STOPED: - ESP_LOGI(TAG, "Timestamp = %llu - %s_0x%x STOPPED", - sensor_data->timestamp, - sensor_data->sensor_name, - sensor_data->sensor_addr); - break; - case SENSOR_TEMP_DATA_READY: - ESP_LOGI(TAG, "Timestamp = %llu - %s_0x%x TEMP_DATA_READY - " - "temperature=%.2f\n", - sensor_data->timestamp, - sensor_data->sensor_name, - sensor_data->sensor_addr, - sensor_data->temperature); - break; - case SENSOR_HUMI_DATA_READY: - ESP_LOGI(TAG, "Timestamp = %llu - %s_0x%x HUMIDITY_DATA_READY - " - "humidity=%.2f\n", - sensor_data->timestamp, - sensor_data->sensor_name, - sensor_data->sensor_addr, - sensor_data->humidity); - break; - default: - ESP_LOGI(TAG, "Timestamp = %" PRIi64 " - event id = %" PRIi32, sensor_data->timestamp, id); - break; - } -} - -void i2c_master_init(void) -{ - const i2c_config_t conf = { - .mode = I2C_MODE_MASTER, - .sda_io_num = I2C_MASTER_SDA_IO, - .sda_pullup_en = GPIO_PULLUP_ENABLE, - .scl_io_num = I2C_MASTER_SCL_IO, - .scl_pullup_en = GPIO_PULLUP_ENABLE, - .master.clk_speed = I2C_MASTER_FREQ_HZ, - }; - printf("requesting i2c bus handle\n"); - my_i2c_bus_handle = i2c_bus_create(I2C_MASTER_NUM, &conf); - printf("i2c bus handle acquired\n"); - -} - -void init_aht20() -{ - /*create sensor based on sensorID*/ - sensor_handle_t sensor_handle = NULL; - sensor_config_t sensor_config = { - .bus = my_i2c_bus_handle, /*which bus sensors will connect to*/ - .type = HUMITURE_ID, /*sensor type*/ - .addr = AHT20_ADDRESS_LOW, /*sensor addr*/ - .mode = MODE_POLLING, /*data acquire mode*/ - .min_delay = SENSOR_PERIOD /*data acquire period*/ - }; - - /*create a sensor with specific sensor_id and configurations*/ - iot_sensor_create("aht20", &sensor_config, &sensor_handle); - - /*register handler with sensor's handle*/ - iot_sensor_handler_register(sensor_handle, sensor_event_handler, NULL); - - /*start a sensor, data ready events will be posted once data acquired successfully*/ - iot_sensor_start(sensor_handle); - -} - -void app_main(void) -{ - //initialize i2c master - i2c_master_init(); - - // user defined function for aht20 initialization - init_aht20(); - - while (1) { - vTaskDelay(1000); - } -} diff --git a/examples/sensors/aht20_Sensor_Hub/main/idf_component.yml b/examples/sensors/aht20_Sensor_Hub/main/idf_component.yml deleted file mode 100644 index 210dd57859..0000000000 --- a/examples/sensors/aht20_Sensor_Hub/main/idf_component.yml +++ /dev/null @@ -1,7 +0,0 @@ -dependencies: - aht20: - version: "*" - override_path: "../../../../components/sensors/humiture/aht20" - sensor_hub: - public: true - override_path: "../../../../components/sensors/sensor_hub" diff --git a/examples/sensors/aht20_demo/CMakeLists.txt b/examples/sensors/aht20_demo/CMakeLists.txt deleted file mode 100644 index 33d7ca6731..0000000000 --- a/examples/sensors/aht20_demo/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -# The following five lines of boilerplate have to be in your project's -# CMakeLists in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.5) - -set(COMPONENT main) -set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components") - -include($ENV{IDF_PATH}/tools/cmake/project.cmake) -project(aht20_demo) diff --git a/examples/sensors/aht20_demo/README.md b/examples/sensors/aht20_demo/README.md deleted file mode 100644 index 405a4931e8..0000000000 --- a/examples/sensors/aht20_demo/README.md +++ /dev/null @@ -1,3 +0,0 @@ -This is a demo which uses the aht20 driver. - -It simply reads the humiture from aht20 sensor and prints the output continuously on terminal. diff --git a/examples/sensors/aht20_demo/main/CMakeLists.txt b/examples/sensors/aht20_demo/main/CMakeLists.txt deleted file mode 100644 index a8c3fc61e0..0000000000 --- a/examples/sensors/aht20_demo/main/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -idf_component_register(SRCS "aht20_demo.c" - INCLUDE_DIRS "." - REQUIRES "aht20") diff --git a/examples/sensors/aht20_demo/main/aht20_demo.c b/examples/sensors/aht20_demo/main/aht20_demo.c deleted file mode 100644 index 2f53cf4f57..0000000000 --- a/examples/sensors/aht20_demo/main/aht20_demo.c +++ /dev/null @@ -1,84 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -/* - * @file: aht20_demo.c - * - * @brief: A simple demo to use the AHT20 driver - * - * @date: May 2, 2025 - * - * @Author: Rohan Jeet - * - */ -#include -#include -#include -#include "aht20.h" - -//i2c configuration values -#define I2C_MASTER_SCL_IO (22) // SCL pin -#define I2C_MASTER_SDA_IO (21) // SDA pin -#define I2C_MASTER_NUM I2C_NUM_0 -#define I2C_MASTER_FREQ_HZ (400000) // I2C frequency - -i2c_bus_handle_t my_i2c_bus_handle; - -void i2c_master_init(void) -{ - const i2c_config_t conf = { - .mode = I2C_MODE_MASTER, - .sda_io_num = I2C_MASTER_SDA_IO, - .sda_pullup_en = GPIO_PULLUP_ENABLE, - .scl_io_num = I2C_MASTER_SCL_IO, - .scl_pullup_en = GPIO_PULLUP_ENABLE, - .master.clk_speed = I2C_MASTER_FREQ_HZ, - }; - printf("requesting i2c bus handle\n"); - my_i2c_bus_handle = i2c_bus_create(I2C_MASTER_NUM, &conf); - printf("i2c bus handle acquired\n"); - -} - -void read_aht20(void *my_aht20_handle) -{ - - aht20_handle_t aht20_handle = (aht20_handle_t) my_aht20_handle; //retrieve the AHT20 device handle copy - vTaskDelay(400 / portTICK_PERIOD_MS); - for (int i = 0; i < 5; i++) { - //read both humidity and temperature at once from device, using AHT20 device handle - aht20_read_humiture(aht20_handle); - //access the results stored in AHT20 device object, using the AHT20 device handle - //other apis require user to explicitly pass variable address to hold data - printf("tempertature = %.2fC humidity = %.3f \n", aht20_handle->humiture.temperature, aht20_handle->humiture.humidity); - vTaskDelay(2000 / portTICK_PERIOD_MS); - } - printf("end of demo\n"); - aht20_remove(&aht20_handle); - i2c_bus_delete(&my_i2c_bus_handle); - vTaskDelete(NULL); -} - -void init_aht20() -{ - //create a AHT20 device object and receive a device handle for it - aht20_handle_t aht20_handle = aht20_create(my_i2c_bus_handle, AHT20_ADDRESS_LOW); - - printf("initializing aht20\n"); - //use the previously created AHT20 device handle for initializing the associated device - while (aht20_init(aht20_handle) != ESP_OK) { // wait until it is initialized - vTaskDelay(100 / portTICK_PERIOD_MS); - } - printf("\naht20 initialized\n"); - - //creating a task to read from AHT20, passing the AHT20 device handle copy - xTaskCreate(read_aht20, "aht20_tester", 2500, aht20_handle, 5, NULL); -} - -void app_main(void) -{ - i2c_master_init(); //initialize i2c master - init_aht20(); // user defined function for aht20 initialization -} diff --git a/examples/sensors/aht20_demo/main/idf_component.yml b/examples/sensors/aht20_demo/main/idf_component.yml deleted file mode 100644 index f67432d3a6..0000000000 --- a/examples/sensors/aht20_demo/main/idf_component.yml +++ /dev/null @@ -1,4 +0,0 @@ -dependencies: - aht20: - version: "*" - override_path: "../../../../components/sensors/humiture/aht20" From e9727a735fd5079853bb014bdb85134d6b348809 Mon Sep 17 00:00:00 2001 From: jeetrohan Date: Tue, 3 Jun 2025 23:01:56 +0530 Subject: [PATCH 8/8] refactor(aht20): remove I2C clock Kconfig option and encapsulate sensor handle - Removed `CONFIG_AHT20_I2C_CLK_SPEED` Kconfig option - Replaced with i2c_bus_get_current_clk_speed() API - Ensures better consistency with bus-level configuration - Refactored AHT20 handle to a void pointer to encapsulate internal structure - Moved internal handle definition to implementation file Changes to be committed: modified: Kconfig modified: README.md modified: aht20.c modified: include/aht20.h modified: test_apps/aht20_driver_test/CMakeLists.txt modified: test_apps/aht20_driver_test/main/aht20_test_app.c deleted: test_apps/aht20_driver_test/main/idf_component.yml --- components/sensors/humiture/aht20/Kconfig | 9 --- components/sensors/humiture/aht20/README.md | 22 +----- components/sensors/humiture/aht20/aht20.c | 74 ++++++++++--------- .../sensors/humiture/aht20/include/aht20.h | 20 +---- .../aht20_driver_test/CMakeLists.txt | 3 +- .../aht20_driver_test/main/aht20_test_app.c | 10 --- .../aht20_driver_test/main/idf_component.yml | 4 - 7 files changed, 49 insertions(+), 93 deletions(-) delete mode 100644 components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/idf_component.yml diff --git a/components/sensors/humiture/aht20/Kconfig b/components/sensors/humiture/aht20/Kconfig index 2c216155c2..d3a98e5245 100644 --- a/components/sensors/humiture/aht20/Kconfig +++ b/components/sensors/humiture/aht20/Kconfig @@ -6,13 +6,4 @@ menu "AHT20 : CONFIGURATION" CRC check to be performed on results or not?. default n - - config AHT20_I2C_CLK_SPEED - int "I2C clock speed" - default 100000 - range 1 400000 - help - Clock speed used for i2c communication by AHT20 device. - Limited to maximum of 400KHZ. - endmenu diff --git a/components/sensors/humiture/aht20/README.md b/components/sensors/humiture/aht20/README.md index dc6c59b456..8669c48b94 100644 --- a/components/sensors/humiture/aht20/README.md +++ b/components/sensors/humiture/aht20/README.md @@ -73,31 +73,17 @@ Following are the general guidelines. printf("Temperature = %.2f°C\n", temperature); ``` -## Senosr Hub -Following are the general guidelines. -``` - - /*create a sensor with specific sensor_id and configurations*/ - iot_sensor_create("aht20", &sensor_config, &sensor_handle); - - /*register handler with sensor's handle*/ - iot_sensor_handler_register(sensor_handle, sensor_event_handler, NULL); - - /*start the sensor, data ready events will be posted once data is acquired successfully*/ - iot_sensor_start(sensor_handle); - -``` - -## How to Configure CRC and I2C clock speed +## How to Configure CRC Additionally, select in menuconfig under Component Config → AHT20; to use CRC(default is not used) or change the clock speed of device (default is 100KHz). Note : It is recommended to use clock speeds in upper ranges of 100kHz to 200kHz. Higher clock speeds may cause occasional data inconsistencies depending on your board layout and wiring. -![image](https://github.com/user-attachments/assets/fc8680fb-1567-477c-92f8-52dd126e6f9d) +![image](https://github.com/user-attachments/assets/58a07cc9-5d87-4afe-9675-637b3e776faa) + or In sdkconfig under Component Config → AHT20, -![image](https://github.com/user-attachments/assets/1f9612df-8d73-4ad1-bec7-75cbe6ed327a) + diff --git a/components/sensors/humiture/aht20/aht20.c b/components/sensors/humiture/aht20/aht20.c index dc633c751b..04941d78a3 100644 --- a/components/sensors/humiture/aht20/aht20.c +++ b/components/sensors/humiture/aht20/aht20.c @@ -3,16 +3,6 @@ * * SPDX-License-Identifier: Apache-2.0 */ -/* - * @File: aht20.c - * - * @brief: AHT20 driver function definitions - * - * @Date: May 2, 2025 - * - * @Author: Rohan Jeet - * - */ #include @@ -24,6 +14,13 @@ static const char *s_TAG = "AHT20"; +/** + * @brief AHT20 device object + */ +typedef struct { + i2c_bus_device_handle_t i2c_dev; /*!< i2c device handle. */ +} aht20_dev_config_t; + /** *@brief AHT20 raw result */ @@ -42,7 +39,7 @@ typedef struct { * @param[in] read_size data size to read * */ -static esp_err_t aht20_read_reg(aht20_handle_t sensor, uint8_t * read_buffer, uint8_t read_size); +static esp_err_t aht20_read_reg(aht20_dev_config_t * aht20_handle, uint8_t * read_buffer, uint8_t read_size); /** * @brief a function used to handle resetting of registers of the device, if not found calibrated when initialized @@ -54,7 +51,7 @@ static esp_err_t aht20_read_reg(aht20_handle_t sensor, uint8_t * read_buffer, ui * @param[in] write_size data size to write * */ -static esp_err_t aht20_write_reg(aht20_handle_t sensor, uint8_t * cmd, uint8_t write_size); +static esp_err_t aht20_write_reg(aht20_dev_config_t * aht20_handle, uint8_t * cmd, uint8_t write_size); /** * @brief a function used to handle reinitialization of registers of the device, if not found calibrated when initialized @@ -62,7 +59,7 @@ static esp_err_t aht20_write_reg(aht20_handle_t sensor, uint8_t * cmd, uint8_t w * @param[in] aht20_handle AHT20 device handle * */ -static esp_err_t aht20_Start_Init(aht20_handle_t aht20_handle); +static esp_err_t aht20_Start_Init(aht20_dev_config_t * aht20_handle); /** * @brief a function used to handle resetting of registers of the device, if not found calibrated when initialized @@ -72,7 +69,7 @@ static esp_err_t aht20_Start_Init(aht20_handle_t aht20_handle); * @param[in] addr AHT20 internal register, undocumented in datasheet * */ -static esp_err_t aht20_JH_Reset_REG(aht20_handle_t aht20_handle, uint8_t addr); +static esp_err_t aht20_JH_Reset_REG(aht20_dev_config_t * aht20_handle, uint8_t addr); /** * @brief check crc validity of response received @@ -99,25 +96,26 @@ static uint8_t calc_CRC8(uint8_t *message, uint8_t Num); * - other error codes : failure in reading AHT20 busy status * */ -static esp_err_t aht20_busy_status(aht20_handle_t aht20_handle, bool *busy); +static esp_err_t aht20_busy_status(aht20_dev_config_t * aht20_handle, bool *busy); -static esp_err_t aht20_read_reg(aht20_handle_t sensor, uint8_t * read_buffer, uint8_t read_size) +static esp_err_t aht20_read_reg(aht20_dev_config_t * aht20_handle, uint8_t * read_buffer, uint8_t read_size) { - ESP_RETURN_ON_ERROR(i2c_bus_read_bytes(sensor->i2c_dev, NULL_I2C_MEM_ADDR, read_size, read_buffer), + + ESP_RETURN_ON_ERROR(i2c_bus_read_bytes(aht20_handle->i2c_dev, NULL_I2C_MEM_ADDR, read_size, read_buffer), s_TAG, "unable to read from aht20"); return ESP_OK; } -static esp_err_t aht20_write_reg(aht20_handle_t sensor, uint8_t * cmd, uint8_t write_size) +static esp_err_t aht20_write_reg(aht20_dev_config_t * aht20_handle, uint8_t * cmd, uint8_t write_size) { - ESP_RETURN_ON_ERROR(i2c_bus_write_bytes(sensor->i2c_dev, NULL_I2C_MEM_ADDR, write_size, cmd), + ESP_RETURN_ON_ERROR(i2c_bus_write_bytes(aht20_handle->i2c_dev, NULL_I2C_MEM_ADDR, write_size, cmd), s_TAG, "unable to set mode for AHT20\n"); return ESP_OK; } -static esp_err_t aht20_busy_status(aht20_handle_t aht20_handle, bool *busy) +static esp_err_t aht20_busy_status(aht20_dev_config_t * aht20_handle, bool *busy) { ESP_RETURN_ON_FALSE((aht20_handle != NULL), ESP_ERR_INVALID_ARG, s_TAG, "empty handle, initialize AHT20 handle"); @@ -137,7 +135,7 @@ static esp_err_t aht20_busy_status(aht20_handle_t aht20_handle, bool *busy) return ESP_OK; } -static esp_err_t aht20_JH_Reset_REG(aht20_handle_t aht20_handle, uint8_t addr) +static esp_err_t aht20_JH_Reset_REG(aht20_dev_config_t * aht20_handle, uint8_t addr) { uint8_t reset_cmd[] = {addr, 0x00, 0x00}, read_bytes[3]; @@ -159,7 +157,7 @@ static esp_err_t aht20_JH_Reset_REG(aht20_handle_t aht20_handle, uint8_t addr) return ESP_OK; } -static esp_err_t aht20_Start_Init(aht20_handle_t aht20_handle) +static esp_err_t aht20_Start_Init(aht20_dev_config_t * aht20_handle) { ESP_RETURN_ON_ERROR(aht20_JH_Reset_REG(aht20_handle, 0x1b), "", ""); ESP_RETURN_ON_ERROR(aht20_JH_Reset_REG(aht20_handle, 0x1c), "", ""); @@ -190,7 +188,7 @@ static uint8_t calc_CRC8(uint8_t *message, uint8_t Num) /******************************************** Public *********************************************/ -esp_err_t aht20_read_raw(aht20_handle_t aht20_handle, aht20_raw_reading_t *raw_read) +static esp_err_t aht20_read_raw(aht20_dev_config_t * aht20_handle, aht20_raw_reading_t *raw_read) { ESP_RETURN_ON_FALSE((aht20_handle != NULL), ESP_ERR_INVALID_ARG, s_TAG, "empty handle, provide a valid AHT20 handle"); @@ -224,8 +222,12 @@ esp_err_t aht20_read_raw(aht20_handle_t aht20_handle, aht20_raw_reading_t *raw_r return ESP_OK; } -esp_err_t aht20_read_humidity(aht20_handle_t aht20_handle, float_t *humidity) +esp_err_t aht20_read_humidity(aht20_handle_t aht20, float_t *humidity) { + ESP_RETURN_ON_FALSE((aht20 != NULL), ESP_ERR_INVALID_ARG, s_TAG, "empty handle, initialize AHT20 handle"); + + aht20_dev_config_t * aht20_handle = (aht20_dev_config_t *) aht20; + aht20_raw_reading_t raw_read; ESP_RETURN_ON_ERROR(aht20_read_raw(aht20_handle, &raw_read), "", ""); @@ -233,8 +235,12 @@ esp_err_t aht20_read_humidity(aht20_handle_t aht20_handle, float_t *humidity) return ESP_OK; } -esp_err_t aht20_read_temperature(aht20_handle_t aht20_handle, float_t *temperature) +esp_err_t aht20_read_temperature(aht20_handle_t aht20, float_t *temperature) { + ESP_RETURN_ON_FALSE((aht20 != NULL), ESP_ERR_INVALID_ARG, s_TAG, "empty handle, initialize AHT20 handle"); + + aht20_dev_config_t * aht20_handle = (aht20_dev_config_t *) aht20; + aht20_raw_reading_t raw_read; ESP_RETURN_ON_ERROR(aht20_read_raw(aht20_handle, &raw_read), "", ""); @@ -243,10 +249,11 @@ esp_err_t aht20_read_temperature(aht20_handle_t aht20_handle, float_t *temperatu return ESP_OK; } -esp_err_t aht20_init(aht20_handle_t aht20_handle) +esp_err_t aht20_init(aht20_handle_t aht20) { - ESP_RETURN_ON_FALSE((aht20_handle != NULL), ESP_ERR_INVALID_ARG, s_TAG, "empty handle, initialize AHT20 handle"); + ESP_RETURN_ON_FALSE((aht20 != NULL), ESP_ERR_INVALID_ARG, s_TAG, "empty handle, initialize AHT20 handle"); + aht20_dev_config_t * aht20_handle = (aht20_dev_config_t *) aht20; vTaskDelay(20 / portTICK_PERIOD_MS); //time for AHT20 SCL to stabilize /***********************************************************************************/ @@ -281,16 +288,16 @@ esp_err_t aht20_init(aht20_handle_t aht20_handle) aht20_handle_t aht20_create(i2c_bus_handle_t bus_handle, uint8_t aht20_address) { ESP_LOGI(s_TAG, "adding aht20 as device to bus\n"); - i2c_bus_device_handle_t dev_handle = i2c_bus_device_create(bus_handle, aht20_address, CONFIG_AHT20_I2C_CLK_SPEED); + i2c_bus_device_handle_t dev_handle = i2c_bus_device_create(bus_handle, aht20_address, i2c_bus_get_current_clk_speed(bus_handle)); ESP_RETURN_ON_FALSE((dev_handle != NULL), NULL, s_TAG, "unable to create device\n"); ESP_LOGI(s_TAG, "device added to bus\n"); - aht20_handle_t my_aht20_handle = malloc(sizeof(aht20_dev_config_t)); + aht20_dev_config_t * my_aht20_handle = malloc(sizeof(aht20_dev_config_t)); ESP_RETURN_ON_FALSE((my_aht20_handle != NULL), NULL, s_TAG, "unable to allocate memory to initialize aht20 handle"); my_aht20_handle->i2c_dev = dev_handle; - return my_aht20_handle; + return (aht20_handle_t) my_aht20_handle; } esp_err_t aht20_remove(aht20_handle_t *aht20ptr) @@ -298,9 +305,10 @@ esp_err_t aht20_remove(aht20_handle_t *aht20ptr) if (*aht20ptr == NULL) { return ESP_ERR_INVALID_ARG; } - i2c_bus_device_delete(&((*aht20ptr)->i2c_dev)); - free(*aht20ptr); - *aht20ptr = NULL; // now AHT20 handle is not a dangling pointer + aht20_dev_config_t ** aht20_handle = (aht20_dev_config_t **) aht20ptr; + i2c_bus_device_delete(&((*aht20_handle)->i2c_dev)); + free(*aht20_handle); + *aht20_handle = NULL; // now AHT20 handle is not a dangling pointer return ESP_OK; } diff --git a/components/sensors/humiture/aht20/include/aht20.h b/components/sensors/humiture/aht20/include/aht20.h index 2db90fc557..981260f6c7 100644 --- a/components/sensors/humiture/aht20/include/aht20.h +++ b/components/sensors/humiture/aht20/include/aht20.h @@ -3,16 +3,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -/* - * @File: aht20.h - * - * @brief: AHT20 driver function declarations and typedefinitons - * - * @Date: May 2, 2025 - * - * @Author: Rohan Jeet - * - */ + #ifndef AHT20_H #define AHT20_H @@ -42,17 +33,10 @@ extern "C" { #define AHT20_INIT_REG 0XBE /*!< initialize the AHT20 */ #define AHT20_MEASURE_CYC 0xAC /*!< trigger measurement in cycle mode */ -/** - * @brief AHT20 device object - */ -typedef struct { - i2c_bus_device_handle_t i2c_dev; /*!< i2c device handle. */ -} aht20_dev_config_t; - /** * @brief AHT20 device handle */ -typedef aht20_dev_config_t *aht20_handle_t; +typedef void * aht20_handle_t; /** * @brief soft reset AHT20 diff --git a/components/sensors/humiture/aht20/test_apps/aht20_driver_test/CMakeLists.txt b/components/sensors/humiture/aht20/test_apps/aht20_driver_test/CMakeLists.txt index b5cebea280..fe5722940f 100644 --- a/components/sensors/humiture/aht20/test_apps/aht20_driver_test/CMakeLists.txt +++ b/components/sensors/humiture/aht20/test_apps/aht20_driver_test/CMakeLists.txt @@ -3,7 +3,8 @@ cmake_minimum_required(VERSION 3.5) set(COMPONENTS main) -set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components") +set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components" + "../../../aht20") include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(aht20_test) diff --git a/components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/aht20_test_app.c b/components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/aht20_test_app.c index e16d5b211c..78bfdfc8bc 100644 --- a/components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/aht20_test_app.c +++ b/components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/aht20_test_app.c @@ -3,16 +3,6 @@ * * SPDX-License-Identifier: Apache-2.0 */ -/*- - * @File: aht20_test_app.c - * - * @brief: AHT20 driver unity test app - * - * @Date: May 2, 2025 - * - * @Author: Rohan Jeet - * - */ #include #include "unity.h" diff --git a/components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/idf_component.yml b/components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/idf_component.yml deleted file mode 100644 index 133580640b..0000000000 --- a/components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/idf_component.yml +++ /dev/null @@ -1,4 +0,0 @@ -dependencies: - aht20: - version: "*" - override_path: "../../../"