perf: updated error checks

This commit is contained in:
aZholtikov committed 2026-10-11 20:49:00 +03:00
1 parent c5affea438
commit 81cf96aa16
3 files changed
+19 -19

No files matched your search

-9
View File
@@ -10,15 +10,6 @@
6. Sensor presence verification during initialization via I2C probe. 6. Sensor presence verification during initialization via I2C probe.
7. Support multiple sensors on one device with [zh_pca9548a](https://github.com/aZholtikov/zh_pca9548a). 7. Support multiple sensors on one device with [zh_pca9548a](https://github.com/aZholtikov/zh_pca9548a).
## Note
Enable the following settings in menuconfig:
```text
I2C_ISR_IRAM_SAFE
I2C_MASTER_ISR_HANDLER_IN_IRAM
```
## Using ## Using
In an existing project, run the following command to install the component: In an existing project, run the following command to install the component:
+7 -5
View File
@@ -14,8 +14,8 @@
* - Error statistics tracking for diagnostics * - Error statistics tracking for diagnostics
* - Thread-safe initialization and deinitialization * - Thread-safe initialization and deinitialization
* *
* @note Supported I2C addresses: 0x23 (default), 0x5C (ADDR pin high) * @note Supported I2C addresses: 0x23 (default), 0x5C (ADDR pin high).
* @warning Ensure the I2C bus is initialized before calling zh_bh1750_init() * @warning Ensure the I2C bus is initialized before calling zh_bh1750_init().
*/ */
#pragma once #pragma once
@@ -23,7 +23,9 @@
#include "esp_err.h" #include "esp_err.h"
#include "esp_log.h" #include "esp_log.h"
#include "driver/i2c_master.h" #include "driver/i2c_master.h"
#include "esp_heap_caps.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#define ZH_BH1750_INIT_CONFIG_DEFAULT() \ #define ZH_BH1750_INIT_CONFIG_DEFAULT() \
{ \ { \
@@ -74,8 +76,8 @@ extern "C"
* Allocates a handle, validates the provided configuration, and registers * Allocates a handle, validates the provided configuration, and registers
* the sensor as an I2C device. The I2C bus must already be initialized. * the sensor as an I2C device. The I2C bus must already be initialized.
* *
* @param[in] config Pointer to the initialization configuration structure (must not be NULL) * @param[in] config Pointer to the initialization configuration structure
* @param[out] handle Pointer to receive the allocated sensor handle (must be NULL) * @param[out] handle Pointer to receive the allocated sensor handle
* *
* @return ESP_OK on success * @return ESP_OK on success
* @return ESP_ERR_INVALID_ARG if config or handle is NULL, or parameters are invalid * @return ESP_ERR_INVALID_ARG if config or handle is NULL, or parameters are invalid
@@ -91,7 +93,7 @@ extern "C"
* Removes the sensor from the I2C bus, frees the allocated handle, * Removes the sensor from the I2C bus, frees the allocated handle,
* and sets the handle pointer to NULL. * and sets the handle pointer to NULL.
* *
* @param[in,out] handle Pointer to the sensor handle to deinitialize (must not be NULL) * @param handle Pointer to the sensor handle (must not be NULL)
* *
* @return ESP_OK on success * @return ESP_OK on success
* @return ESP_ERR_INVALID_ARG if handle or *handle is NULL * @return ESP_ERR_INVALID_ARG if handle or *handle is NULL
+12 -5
View File
@@ -13,6 +13,13 @@ static const char *TAG = "zh_bh1750";
return err; \ return err; \
} }
#define ZH_ERROR_CHECK_CONT(cond, cleanup, msg, ...) \
if (!(cond)) \
{ \
ZH_LOGE(msg, ESP_FAIL, ##__VA_ARGS__); \
cleanup; \
}
/** /**
* @brief Handle structure for BH1750 sensor. * @brief Handle structure for BH1750 sensor.
* *
@@ -83,10 +90,10 @@ esp_err_t zh_bh1750_read(zh_bh1750_handle_t **handle, float *data)
ZH_LOGI("BH1750 read begin."); ZH_LOGI("BH1750 read begin.");
ZH_ERROR_CHECK(handle != NULL && *handle != NULL && data != NULL, ESP_ERR_INVALID_ARG, NULL, "BH1750 read fail. Invalid argument."); ZH_ERROR_CHECK(handle != NULL && *handle != NULL && data != NULL, ESP_ERR_INVALID_ARG, NULL, "BH1750 read fail. Invalid argument.");
uint8_t sensor_data[2] = {0}; uint8_t sensor_data[2] = {0};
ZH_ERROR_CHECK(i2c_master_transmit((*handle)->dev_handle, &_bh1750_read_command, sizeof(_bh1750_read_command), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL, ZH_ERROR_CHECK(i2c_master_transmit((*handle)->dev_handle, &_bh1750_read_command, sizeof(_bh1750_read_command), 100) == ESP_OK, ESP_FAIL,
++_stats.i2c_driver_error, "BH1750 read fail. I2C driver error."); ++_stats.i2c_driver_error, "BH1750 read fail. I2C driver error.");
vTaskDelay(180 / portTICK_PERIOD_MS); vTaskDelay(180 / 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, "BH1750 read fail. I2C driver error."); ++_stats.i2c_driver_error, "BH1750 read fail. I2C driver error.");
*data = (sensor_data[0] << 8 | sensor_data[1]) / 1.2; *data = (sensor_data[0] << 8 | sensor_data[1]) / 1.2;
ZH_LOGI("BH1750 read completed successfully."); ZH_LOGI("BH1750 read completed successfully.");
@@ -108,7 +115,7 @@ void zh_bh1750_reset_stats(void)
static esp_err_t _zh_bh1750_validate_config(const zh_bh1750_init_config_t *config) static esp_err_t _zh_bh1750_validate_config(const zh_bh1750_init_config_t *config)
{ {
ZH_ERROR_CHECK(config->i2c_address == 0x23 || config->i2c_address == 0x5C, ESP_ERR_INVALID_ARG, NULL, "Invalid I2C address."); ZH_ERROR_CHECK(config->i2c_address == 0x23 || config->i2c_address == 0x5C, 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."); ZH_ERROR_CHECK(config->i2c_handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Invalid I2C handle.");
return ESP_OK; return ESP_OK;
} }
@@ -122,8 +129,8 @@ static esp_err_t _zh_bh1750_i2c_init(const zh_bh1750_init_config_t *config, zh_b
}; };
i2c_master_dev_handle_t _dev_handle = NULL; i2c_master_dev_handle_t _dev_handle = NULL;
ZH_ERROR_CHECK(i2c_master_bus_add_device(config->i2c_handle, &bh1750_config, &_dev_handle) == ESP_OK, ESP_FAIL, NULL, "Add I2C device failed."); ZH_ERROR_CHECK(i2c_master_bus_add_device(config->i2c_handle, &bh1750_config, &_dev_handle) == ESP_OK, ESP_FAIL, NULL, "Add I2C device failed.");
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_probe(config->i2c_handle, config->i2c_address, 100) == 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_CONT(i2c_master_bus_rm_device(_dev_handle) == ESP_OK, NULL, "I2C remove device failed."), "Sensor not connected or not responded.");
handle->dev_handle = _dev_handle; handle->dev_handle = _dev_handle;
return ESP_OK; return ESP_OK;
} }