diff --git a/README.md b/README.md index 01aa641..11c4658 100644 --- a/README.md +++ b/README.md @@ -10,15 +10,6 @@ 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). -## 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_bh1750.h b/include/zh_bh1750.h index 9837389..814f500 100644 --- a/include/zh_bh1750.h +++ b/include/zh_bh1750.h @@ -14,8 +14,8 @@ * - Error statistics tracking for diagnostics * - Thread-safe initialization and deinitialization * - * @note Supported I2C addresses: 0x23 (default), 0x5C (ADDR pin high) - * @warning Ensure the I2C bus is initialized before calling zh_bh1750_init() + * @note Supported I2C addresses: 0x23 (default), 0x5C (ADDR pin high). + * @warning Ensure the I2C bus is initialized before calling zh_bh1750_init(). */ #pragma once @@ -23,7 +23,9 @@ #include "esp_err.h" #include "esp_log.h" #include "driver/i2c_master.h" +#include "esp_heap_caps.h" #include "freertos/FreeRTOS.h" +#include "freertos/task.h" #define ZH_BH1750_INIT_CONFIG_DEFAULT() \ { \ @@ -74,8 +76,8 @@ extern "C" * Allocates a handle, validates the provided configuration, and registers * 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[out] handle Pointer to receive the allocated sensor handle (must be NULL) + * @param[in] config Pointer to the initialization configuration structure + * @param[out] handle Pointer to receive the allocated sensor handle * * @return ESP_OK on success * @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, * 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_ERR_INVALID_ARG if handle or *handle is NULL diff --git a/zh_bh1750.c b/zh_bh1750.c index c8b92ca..417a2db 100644 --- a/zh_bh1750.c +++ b/zh_bh1750.c @@ -13,6 +13,13 @@ static const char *TAG = "zh_bh1750"; 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. * @@ -83,13 +90,13 @@ esp_err_t zh_bh1750_read(zh_bh1750_handle_t **handle, float *data) ZH_LOGI("BH1750 read begin."); 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}; - 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."); 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."); *data = (sensor_data[0] << 8 | sensor_data[1]) / 1.2; - ZH_LOGI("BH1750read completed successfully."); + ZH_LOGI("BH1750 read completed successfully."); return ESP_OK; } @@ -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) { 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."); 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; 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_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."); handle->dev_handle = _dev_handle; return ESP_OK; } \ No newline at end of file