From 2da2d309ceda24b3ff9ec5ab036fe800822b869b Mon Sep 17 00:00:00 2001 From: Alexey Zholtikov Date: Sun, 11 Oct 2026 20:13:39 +0300 Subject: [PATCH] perf: updated error checks --- README.md | 9 --------- include/zh_aht.h | 7 +++---- zh_aht.c | 49 ++++++++++++++++++++++++++++++++++++------------ 3 files changed, 40 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 37e3704..3b18ece 100644 --- a/README.md +++ b/README.md @@ -7,15 +7,6 @@ 3. CRC8 data integrity verification. 4. Error statistics tracking. -## Note - -Enable the following settings in menuconfig: - -```text -I2C_ISR_IRAM_SAFE -I2C_MASTER_ISR_HANDLER_IN_IRAM -``` - ## Using In an existing project, run the following command to install the component: diff --git a/include/zh_aht.h b/include/zh_aht.h index af7167f..7a04876 100644 --- a/include/zh_aht.h +++ b/include/zh_aht.h @@ -11,9 +11,6 @@ * - Automatic sensor initialization and probe detection * - CRC8 data integrity verification * - Error statistics tracking - * - * @note Requires ESP-IDF v5.0+ with I2C master driver - * @note Enable I2C_ISR_IRAM_SAFE and I2C_MASTER_ISR_HANDLER_IN_IRAM in menuconfig */ #pragma once @@ -22,6 +19,8 @@ #include "esp_log.h" #include "driver/i2c_master.h" #include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_heap_caps.h" #define ZH_AHT_INIT_CONFIG_DEFAULT() \ { \ @@ -37,7 +36,7 @@ extern "C" * @brief Opaque handle for AHT sensor driver. * * Incomplete type that encapsulates internal sensor state. - * Users interact with this handle exclusively through the public API functions.c + * Users interact with this handle exclusively through the public API functions. */ typedef struct _zh_aht_handle_t zh_aht_handle_t; diff --git a/zh_aht.c b/zh_aht.c index f52be69..f195681 100644 --- a/zh_aht.c +++ b/zh_aht.c @@ -13,6 +13,13 @@ static const char *TAG = "zh_aht"; return err; \ } +#define ZH_ERROR_CHECK_CONT(cond, cleanup, msg, ...) \ + if (!(cond)) \ + { \ + ZH_LOGE(msg, ESP_FAIL, ##__VA_ARGS__); \ + cleanup; \ + } + /** * @brief Handle structure for AHT sensor. * @@ -25,7 +32,7 @@ struct _zh_aht_handle_t static const uint8_t _aht_reset_command = 0xBA; /*!< Reset command for AHT sensor */ static const uint8_t _aht_read_command[] = {0xAC, 0x33, 0x00}; /*!< Command sequence to initiate sensor data read */ -static const uint8_t _aht_init_command[] = {0xBE, 0x08, 0x00}; /*!< Command sequence to configure sensor for normal operation */ +static const uint8_t _aht_init_command[] = {0xBE, 0x08, 0x00}; /*!< Init command */ static zh_aht_stats_t _stats = {0}; /*!< Global statistics structure for error tracking */ /** @@ -95,11 +102,12 @@ esp_err_t zh_aht_read(zh_aht_handle_t **handle, float *humidity, float *temperat ZH_LOGI("AHT read begin."); ZH_ERROR_CHECK(handle != NULL && *handle != NULL && humidity != NULL && temperature != NULL, ESP_ERR_INVALID_ARG, NULL, "AHT read fail. Invalid argument."); uint8_t sensor_data[7] = {0}; - ZH_ERROR_CHECK(i2c_master_transmit((*handle)->dev_handle, _aht_read_command, sizeof(_aht_read_command), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL, + ZH_ERROR_CHECK(i2c_master_transmit((*handle)->dev_handle, _aht_read_command, sizeof(_aht_read_command), 100) == ESP_OK, ESP_FAIL, ++_stats.i2c_driver_error, "AHT read fail. I2C driver error."); vTaskDelay(80 / portTICK_PERIOD_MS); - ZH_ERROR_CHECK(i2c_master_receive((*handle)->dev_handle, sensor_data, sizeof(sensor_data), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL, + ZH_ERROR_CHECK(i2c_master_receive((*handle)->dev_handle, sensor_data, sizeof(sensor_data), 100) == ESP_OK, ESP_FAIL, ++_stats.i2c_driver_error, "AHT read fail. I2C driver error."); + ZH_ERROR_CHECK((sensor_data[0] & 0x08) != 0, ESP_ERR_INVALID_STATE, NULL, "AHT read fail. Sensor not calibrated."); ZH_ERROR_CHECK((sensor_data[0] & 0x80) == 0, ESP_ERR_TIMEOUT, NULL, "AHT read fail. Timeout exceeded."); ZH_ERROR_CHECK(_zh_aht_calc_crc(sensor_data, 6) == sensor_data[6], ESP_ERR_INVALID_CRC, NULL, "AHT read fail. Invalid CRC."); *humidity = (((((uint32_t)sensor_data[1]) << 16) | (((uint32_t)sensor_data[2]) << 8) | (((uint32_t)sensor_data[3]) << 0)) >> 4) / 1048576.0 * 100.0; @@ -112,9 +120,22 @@ esp_err_t zh_aht_reset(zh_aht_handle_t **handle) { ZH_LOGI("AHT reset begin."); ZH_ERROR_CHECK(handle != NULL && *handle != NULL, ESP_ERR_INVALID_ARG, NULL, "AHT reset fail. Invalid argument."); - ZH_ERROR_CHECK(i2c_master_transmit((*handle)->dev_handle, &_aht_reset_command, sizeof(_aht_reset_command), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL, - ++_stats.i2c_driver_error, "AHT reset fail. I2C driver error."); + ZH_ERROR_CHECK(i2c_master_transmit((*handle)->dev_handle, &_aht_reset_command, sizeof(_aht_reset_command), 100) == ESP_OK, + ESP_FAIL, ++_stats.i2c_driver_error, "AHT reset fail. I2C driver error (reset cmd)."); vTaskDelay(20 / portTICK_PERIOD_MS); + uint8_t status_cmd = 0x71; + uint8_t status = 0; + ZH_ERROR_CHECK(i2c_master_transmit((*handle)->dev_handle, &status_cmd, sizeof(status_cmd), 100) == ESP_OK, ESP_FAIL, + ++_stats.i2c_driver_error, "AHT reset fail. I2C driver error (status cmd)."); + vTaskDelay(10 / portTICK_PERIOD_MS); + ZH_ERROR_CHECK(i2c_master_receive((*handle)->dev_handle, &status, sizeof(status), 100) == ESP_OK, ESP_FAIL, + ++_stats.i2c_driver_error, "AHT reset fail. I2C driver error (status read)."); + if ((status & 0x08) == 0) + { + ZH_ERROR_CHECK(i2c_master_transmit((*handle)->dev_handle, _aht_init_command, sizeof(_aht_init_command), 100) == ESP_OK, ESP_FAIL, + ++_stats.i2c_driver_error, "AHT reset fail. I2C driver error (init cmd AHT20+)."); + vTaskDelay(20 / portTICK_PERIOD_MS); + } ZH_LOGI("AHT reset completed successfully."); return ESP_OK; } @@ -134,7 +155,7 @@ void zh_aht_reset_stats(void) static esp_err_t _zh_aht_validate_config(const zh_aht_init_config_t *config) { ZH_ERROR_CHECK(config->i2c_address == 0x38 || config->i2c_address == 0x39 || config->i2c_address == 0x44, ESP_ERR_INVALID_ARG, NULL, "Invalid I2C address."); - ZH_ERROR_CHECK(config->i2c_frequency <= 400000, ESP_ERR_INVALID_ARG, NULL, "Invalid I2C frequency."); + ZH_ERROR_CHECK(config->i2c_frequency <= 400000 && config->i2c_frequency > 0, ESP_ERR_INVALID_ARG, NULL, "Invalid I2C frequency."); ZH_ERROR_CHECK(config->i2c_handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Invalid I2C handle."); return ESP_OK; } @@ -149,15 +170,19 @@ static esp_err_t _zh_aht_i2c_init(const zh_aht_init_config_t *config, zh_aht_han i2c_master_dev_handle_t _dev_handle = NULL; ZH_ERROR_CHECK(i2c_master_bus_add_device(config->i2c_handle, &aht_config, &_dev_handle) == ESP_OK, ESP_FAIL, NULL, "Add I2C device failed."); vTaskDelay(100 / portTICK_PERIOD_MS); - ZH_ERROR_CHECK(i2c_master_probe(config->i2c_handle, config->i2c_address, 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_ERR_NOT_FOUND, - {ZH_ERROR_CHECK(i2c_master_bus_rm_device(_dev_handle) == ESP_OK, ESP_FAIL, NULL, "I2C remove device failed.")}, "Sensor not connected or not responded."); + ZH_ERROR_CHECK(i2c_master_probe(config->i2c_handle, config->i2c_address, 100) == ESP_OK, ESP_ERR_NOT_FOUND, + {ZH_ERROR_CHECK_CONT(i2c_master_bus_rm_device(_dev_handle) == ESP_OK, NULL, "I2C remove device failed.")}, "Sensor not connected or not responded."); + uint8_t status_cmd = 0x71; uint8_t sensor_data = 0; - ZH_ERROR_CHECK(i2c_master_receive(_dev_handle, &sensor_data, sizeof(sensor_data), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL, - {ZH_ERROR_CHECK(i2c_master_bus_rm_device(_dev_handle) == ESP_OK, ESP_FAIL, NULL, "I2C remove device failed.")}, "I2C driver error."); + ZH_ERROR_CHECK(i2c_master_transmit(_dev_handle, &status_cmd, sizeof(status_cmd), 100) == ESP_OK, ESP_FAIL, + {ZH_ERROR_CHECK_CONT(i2c_master_bus_rm_device(_dev_handle) == ESP_OK, NULL, "I2C remove device failed.")}, "I2C driver error (status cmd)."); + vTaskDelay(10 / portTICK_PERIOD_MS); + ZH_ERROR_CHECK(i2c_master_receive(_dev_handle, &sensor_data, sizeof(sensor_data), 100) == ESP_OK, ESP_FAIL, + {ZH_ERROR_CHECK_CONT(i2c_master_bus_rm_device(_dev_handle) == ESP_OK, NULL, "I2C remove device failed.")}, "I2C driver error (status read)."); if ((sensor_data & 0x08) == 0) { - ZH_ERROR_CHECK(i2c_master_transmit(_dev_handle, _aht_init_command, sizeof(_aht_init_command), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL, - {ZH_ERROR_CHECK(i2c_master_bus_rm_device(_dev_handle) == ESP_OK, ESP_FAIL, NULL, "I2C remove device failed.")}, "I2C driver error."); + ZH_ERROR_CHECK(i2c_master_transmit(_dev_handle, _aht_init_command, sizeof(_aht_init_command), 100) == ESP_OK, ESP_FAIL, + {ZH_ERROR_CHECK_CONT(i2c_master_bus_rm_device(_dev_handle) == ESP_OK, NULL, "I2C remove device failed.")}, "I2C driver error (init cmd)."); vTaskDelay(20 / portTICK_PERIOD_MS); } handle->dev_handle = _dev_handle;