|
1 | 1 | /* |
2 | | - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD |
| 2 | + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD |
3 | 3 | * |
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
6 | 6 |
|
7 | | -#include <stdio.h> |
8 | | -#include "driver/i2c.h" |
9 | 7 | #include "bh1750.h" |
| 8 | +#include "driver/i2c_master.h" |
| 9 | +#include "freertos/FreeRTOS.h" |
| 10 | +#include "freertos/projdefs.h" // for pdMS_TO_TICKS |
10 | 11 |
|
11 | 12 | #define BH_1750_MEASUREMENT_ACCURACY 1.2 /*!< the typical measurement accuracy of BH1750 sensor */ |
12 | 13 |
|
13 | 14 | #define BH1750_POWER_DOWN 0x00 /*!< Command to set Power Down*/ |
14 | 15 | #define BH1750_POWER_ON 0x01 /*!< Command to set Power On*/ |
| 16 | +#define I2C_CLK_SPEED 400000 |
15 | 17 |
|
16 | 18 | typedef struct { |
17 | | - i2c_port_t bus; |
18 | | - uint16_t dev_addr; |
| 19 | + i2c_master_dev_handle_t i2c_handle; |
19 | 20 | } bh1750_dev_t; |
20 | 21 |
|
21 | 22 | static esp_err_t bh1750_write_byte(const bh1750_dev_t *const sens, const uint8_t byte) |
22 | 23 | { |
23 | | - esp_err_t ret; |
24 | | - |
25 | | - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); |
26 | | - ret = i2c_master_start(cmd); |
27 | | - assert(ESP_OK == ret); |
28 | | - ret = i2c_master_write_byte(cmd, sens->dev_addr | I2C_MASTER_WRITE, true); |
29 | | - assert(ESP_OK == ret); |
30 | | - ret = i2c_master_write_byte(cmd, byte, true); |
31 | | - assert(ESP_OK == ret); |
32 | | - ret = i2c_master_stop(cmd); |
33 | | - assert(ESP_OK == ret); |
34 | | - ret = i2c_master_cmd_begin(sens->bus, cmd, 1000 / portTICK_PERIOD_MS); |
35 | | - i2c_cmd_link_delete(cmd); |
36 | | - |
37 | | - return ret; |
| 24 | + return i2c_master_transmit(sens->i2c_handle, &byte, 1, pdMS_TO_TICKS(1000)); |
38 | 25 | } |
39 | 26 |
|
40 | | -bh1750_handle_t bh1750_create(i2c_port_t port, const uint16_t dev_addr) |
| 27 | +esp_err_t bh1750_create(i2c_master_bus_handle_t i2c_bus, const uint8_t dev_addr, bh1750_handle_t *handle_ret) |
41 | 28 | { |
| 29 | + esp_err_t ret = ESP_OK; |
42 | 30 | bh1750_dev_t *sensor = (bh1750_dev_t *) calloc(1, sizeof(bh1750_dev_t)); |
43 | | - sensor->bus = port; |
44 | | - sensor->dev_addr = dev_addr << 1; |
45 | | - return (bh1750_handle_t) sensor; |
| 31 | + if (!sensor) { |
| 32 | + return ESP_ERR_NO_MEM; |
| 33 | + } |
| 34 | + |
| 35 | + // Add new I2C device |
| 36 | + const i2c_device_config_t i2c_dev_cfg = { |
| 37 | + .device_address = dev_addr, |
| 38 | + .scl_speed_hz = I2C_CLK_SPEED, |
| 39 | + }; |
| 40 | + ret = i2c_master_bus_add_device(i2c_bus, &i2c_dev_cfg, &sensor->i2c_handle); |
| 41 | + if (ret != ESP_OK) { |
| 42 | + free(sensor); |
| 43 | + return ret; |
| 44 | + } |
| 45 | + |
| 46 | + assert(sensor->i2c_handle); |
| 47 | + *handle_ret = sensor; |
| 48 | + return ret; |
46 | 49 | } |
47 | 50 |
|
48 | 51 | esp_err_t bh1750_delete(bh1750_handle_t sensor) |
49 | 52 | { |
50 | 53 | bh1750_dev_t *sens = (bh1750_dev_t *) sensor; |
| 54 | + if (sens->i2c_handle) { |
| 55 | + i2c_master_bus_rm_device(sens->i2c_handle); |
| 56 | + } |
51 | 57 | free(sens); |
52 | 58 | return ESP_OK; |
53 | 59 | } |
@@ -88,26 +94,12 @@ esp_err_t bh1750_set_measure_mode(bh1750_handle_t sensor, const bh1750_measure_m |
88 | 94 |
|
89 | 95 | esp_err_t bh1750_get_data(bh1750_handle_t sensor, float *const data) |
90 | 96 | { |
91 | | - esp_err_t ret; |
92 | | - uint8_t bh1750_data_h, bh1750_data_l; |
93 | 97 | bh1750_dev_t *sens = (bh1750_dev_t *) sensor; |
94 | | - |
95 | | - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); |
96 | | - ret = i2c_master_start(cmd); |
97 | | - assert(ESP_OK == ret); |
98 | | - ret = i2c_master_write_byte(cmd, sens->dev_addr | I2C_MASTER_READ, true); |
99 | | - assert(ESP_OK == ret); |
100 | | - ret = i2c_master_read_byte(cmd, &bh1750_data_h, I2C_MASTER_ACK); |
101 | | - assert(ESP_OK == ret); |
102 | | - ret = i2c_master_read_byte(cmd, &bh1750_data_l, I2C_MASTER_NACK); |
103 | | - assert(ESP_OK == ret); |
104 | | - ret = i2c_master_stop(cmd); |
105 | | - assert(ESP_OK == ret); |
106 | | - ret = i2c_master_cmd_begin(sens->bus, cmd, 1000 / portTICK_PERIOD_MS); |
107 | | - i2c_cmd_link_delete(cmd); |
| 98 | + uint8_t read_buffer[2]; |
| 99 | + esp_err_t ret = i2c_master_receive(sens->i2c_handle, read_buffer, sizeof(read_buffer), pdMS_TO_TICKS(1000)); |
108 | 100 | if (ESP_OK != ret) { |
109 | 101 | return ret; |
110 | 102 | } |
111 | | - *data = (( bh1750_data_h << 8 | bh1750_data_l ) / BH_1750_MEASUREMENT_ACCURACY); |
| 103 | + *data = (( read_buffer[0] << 8 | read_buffer[1] ) / BH_1750_MEASUREMENT_ACCURACY); |
112 | 104 | return ESP_OK; |
113 | 105 | } |
0 commit comments