From 92a6e4362ae6c103af5028472827fcd35dd2d90b Mon Sep 17 00:00:00 2001 From: Alexey Zholtikov Date: Sun, 16 Jun 2024 08:48:52 +0300 Subject: [PATCH] WIP --- include/zh_bh1750.h | 8 ++- zh_bh1750.c | 145 ++++++++++++++++++++++++-------------------- 2 files changed, 83 insertions(+), 70 deletions(-) diff --git a/include/zh_bh1750.h b/include/zh_bh1750.h index 69b4312..636f519 100644 --- a/include/zh_bh1750.h +++ b/include/zh_bh1750.h @@ -2,11 +2,12 @@ #include "esp_err.h" #include "esp_log.h" -#include "driver/i2c.h" -#ifdef CONFIG_IDF_TARGET_ESP8266 +#include "driver/i2c_master.h" +// #include "driver/i2c.h" +// #ifdef CONFIG_IDF_TARGET_ESP8266 #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#endif +// #endif /** * @brief Default values for zh_bh1750_init_config_t structure for initial initialization of the sensor. @@ -51,6 +52,7 @@ extern "C" ONE_TIME // One time measurement. Sensor is power down after measurement. } work_mode; bool i2c_port; // I2C port. + i2c_master_bus_handle_t i2c_handle; } zh_bh1750_init_config_t; /** diff --git a/zh_bh1750.c b/zh_bh1750.c index bbb1b9c..66a6179 100644 --- a/zh_bh1750.c +++ b/zh_bh1750.c @@ -21,6 +21,7 @@ static bool _is_first_start = false; static uint8_t _time = 0; static uint8_t _sensivity = SENSITIVITY_DEFAULT; static bool _is_initialized = false; +static i2c_master_dev_handle_t _bh1750_handle; static const char *TAG = "zh_bh1750"; @@ -33,6 +34,13 @@ esp_err_t zh_bh1750_init(zh_bh1750_init_config_t *config) return ESP_ERR_INVALID_ARG; } _init_config = *config; + i2c_device_config_t bh1750_config = { + .dev_addr_length = I2C_ADDR_BIT_LEN_7, + .device_address = _init_config.i2c_address, + .scl_speed_hz = 100000, + }; + ESP_ERROR_CHECK(i2c_master_bus_add_device(_init_config.i2c_handle, &bh1750_config, &_bh1750_handle)); + ESP_ERROR_CHECK(i2c_master_probe(_init_config.i2c_handle, _init_config.i2c_address, -1)); switch (_init_config.operation_mode) { case LOW_RESOLUTION:; @@ -82,33 +90,36 @@ esp_err_t zh_bh1750_read(float *data) _is_first_start = true; } } - i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create(); - i2c_master_start(i2c_cmd_handle); - i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, true); - i2c_master_write_byte(i2c_cmd_handle, _command, true); - i2c_master_stop(i2c_cmd_handle); - esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS); - i2c_cmd_link_delete(i2c_cmd_handle); - if (esp_err != ESP_OK) - { - ESP_LOGE(TAG, "BH1750 read fail. I2C driver error."); - return esp_err; - } + ESP_ERROR_CHECK(i2c_master_transmit(_bh1750_handle, &_command, sizeof(_command), 1000 / portTICK_PERIOD_MS)); + // i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create(); + // i2c_master_start(i2c_cmd_handle); + // i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, true); + // i2c_master_write_byte(i2c_cmd_handle, _command, true); + // i2c_master_stop(i2c_cmd_handle); + // esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS); + // i2c_cmd_link_delete(i2c_cmd_handle); + // if (esp_err != ESP_OK) + // { + // ESP_LOGE(TAG, "BH1750 read fail. I2C driver error."); + // return esp_err; + // } vTaskDelay(((_sensivity / SENSITIVITY_DEFAULT) * _time) / portTICK_PERIOD_MS); READ: - i2c_cmd_handle = i2c_cmd_link_create(); - i2c_master_start(i2c_cmd_handle); - i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_READ, true); - i2c_master_read_byte(i2c_cmd_handle, &sensor_data_high, I2C_MASTER_ACK); - i2c_master_read_byte(i2c_cmd_handle, &sensor_data_low, I2C_MASTER_NACK); - i2c_master_stop(i2c_cmd_handle); - esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS); - i2c_cmd_link_delete(i2c_cmd_handle); - if (esp_err != ESP_OK) - { - ESP_LOGE(TAG, "BH1750 read fail. I2C driver error."); - return esp_err; - } + // i2c_cmd_handle = i2c_cmd_link_create(); + // i2c_master_start(i2c_cmd_handle); + // i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_READ, true); + // i2c_master_read_byte(i2c_cmd_handle, &sensor_data_high, I2C_MASTER_ACK); + // i2c_master_read_byte(i2c_cmd_handle, &sensor_data_low, I2C_MASTER_NACK); + // i2c_master_stop(i2c_cmd_handle); + // esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS); + // i2c_cmd_link_delete(i2c_cmd_handle); + // if (esp_err != ESP_OK) + // { + // ESP_LOGE(TAG, "BH1750 read fail. I2C driver error."); + // return esp_err; + // } + ESP_ERROR_CHECK(i2c_master_receive(_bh1750_handle, &sensor_data_high, 8, -1)); + ESP_ERROR_CHECK(i2c_master_receive(_bh1750_handle, &sensor_data_low, 8, -1)); if (_init_config.operation_mode == HIGH_RESOLUTION_2) { *data = (sensor_data_high << 8 | sensor_data_low) * (1 / 1.2 * (69.0 / _sensivity) / 2); @@ -123,47 +134,47 @@ READ: esp_err_t zh_bh1750_adjust(uint8_t value) { - ESP_LOGI(TAG, "BH1750 adjust begin."); - if ((value < 31 || value > 254)) - { - ESP_LOGE(TAG, "BH1750 adjust fail. Invalid argument."); - return ESP_ERR_INVALID_ARG; - } - if (_is_initialized == false) - { - ESP_LOGE(TAG, "BH1750 read fail. BH1750 not initialized."); - return ESP_ERR_NOT_FOUND; - } - esp_err_t esp_err = ESP_OK; - _sensivity = value; - i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create(); - i2c_master_start(i2c_cmd_handle); - i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, true); - i2c_master_write_byte(i2c_cmd_handle, value >> 5 | MEASUREMENT_TIME_HIGH_BIT, true); - i2c_master_stop(i2c_cmd_handle); - esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS); - i2c_cmd_link_delete(i2c_cmd_handle); - if (esp_err != ESP_OK) - { - ESP_LOGE(TAG, "BH1750 adjust fail. I2C driver error."); - return esp_err; - } - i2c_cmd_handle = i2c_cmd_link_create(); - i2c_master_start(i2c_cmd_handle); - i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, true); - i2c_master_write_byte(i2c_cmd_handle, (value & 0b00011111) | MEASUREMENT_TIME_LOW_BIT, true); - i2c_master_stop(i2c_cmd_handle); - esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS); - i2c_cmd_link_delete(i2c_cmd_handle); - if (esp_err != ESP_OK) - { - ESP_LOGE(TAG, "BH1750 adjust fail. I2C driver error."); - return esp_err; - } - if (_init_config.work_mode == CONTINUOUSLY) - { - _is_first_start = false; - } - ESP_LOGI(TAG, "BH1750 adjust success."); + // ESP_LOGI(TAG, "BH1750 adjust begin."); + // if ((value < 31 || value > 254)) + // { + // ESP_LOGE(TAG, "BH1750 adjust fail. Invalid argument."); + // return ESP_ERR_INVALID_ARG; + // } + // if (_is_initialized == false) + // { + // ESP_LOGE(TAG, "BH1750 read fail. BH1750 not initialized."); + // return ESP_ERR_NOT_FOUND; + // } + // esp_err_t esp_err = ESP_OK; + // _sensivity = value; + // i2c_cmd_handle_t i2c_cmd_handle = i2c_cmd_link_create(); + // i2c_master_start(i2c_cmd_handle); + // i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, true); + // i2c_master_write_byte(i2c_cmd_handle, value >> 5 | MEASUREMENT_TIME_HIGH_BIT, true); + // i2c_master_stop(i2c_cmd_handle); + // esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS); + // i2c_cmd_link_delete(i2c_cmd_handle); + // if (esp_err != ESP_OK) + // { + // ESP_LOGE(TAG, "BH1750 adjust fail. I2C driver error."); + // return esp_err; + // } + // i2c_cmd_handle = i2c_cmd_link_create(); + // i2c_master_start(i2c_cmd_handle); + // i2c_master_write_byte(i2c_cmd_handle, _init_config.i2c_address << 1 | I2C_MASTER_WRITE, true); + // i2c_master_write_byte(i2c_cmd_handle, (value & 0b00011111) | MEASUREMENT_TIME_LOW_BIT, true); + // i2c_master_stop(i2c_cmd_handle); + // esp_err = i2c_master_cmd_begin(_init_config.i2c_port, i2c_cmd_handle, 1000 / portTICK_PERIOD_MS); + // i2c_cmd_link_delete(i2c_cmd_handle); + // if (esp_err != ESP_OK) + // { + // ESP_LOGE(TAG, "BH1750 adjust fail. I2C driver error."); + // return esp_err; + // } + // if (_init_config.work_mode == CONTINUOUSLY) + // { + // _is_first_start = false; + // } + // ESP_LOGI(TAG, "BH1750 adjust success."); return ESP_OK; } \ No newline at end of file