Skip to content

Commit 49fc375

Browse files
Merge pull request #646 from espressif/feat/bh1750_i2c_ng
feat(bh1750): Migrate to I2C driver-NG
2 parents e235246 + c25e705 commit 49fc375

File tree

15 files changed

+160
-150
lines changed

15 files changed

+160
-150
lines changed

components/.build-test-rules.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ components/qma6100p:
4848
- if: IDF_VERSION < "5.2.0"
4949
reason: Requires I2C Driver-NG which was introduced in v5.2
5050

51+
components/bh1750:
52+
depends_filepatterns:
53+
- "components/bh1750/**"
54+
disable:
55+
- if: IDF_VERSION < "5.3.0"
56+
5157
components/lcd_touch/esp_lcd_touch_cst816s/test_apps:
5258
depends_filepatterns:
5359
- "components/lcd_touch/esp_lcd_touch_cst816s/**"

components/bh1750/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
idf_component_register(
22
SRCS "bh1750.c"
33
INCLUDE_DIRS "include"
4-
REQUIRES "driver"
4+
REQUIRES "esp_driver_i2c"
55
)

components/bh1750/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Component: BH1750
22

33
[![Component Registry](https://components.espressif.com/components/espressif/bh1750/badge.svg)](https://components.espressif.com/components/espressif/bh1750)
4-
![maintenance-status](https://img.shields.io/badge/maintenance-deprecated-red.svg)
4+
![maintenance-status](https://img.shields.io/badge/maintenance-as--is-yellow.svg)
55

6-
:warning: **BH1750 component is deprecated. Users can use BH1750 from new [Sensor HUB](https://components.espressif.com/components/espressif/sensor_hub) component.**
6+
:warning: **BH1750 component is provided as-is with no further development and compatibility maintenance**
77

8-
* This component will show you how to use I2C module read external i2c sensor data, here we use BH1750 light sensor(GY-30 module).
8+
* This component will show you how to use I2C module read external i2c sensor data, here we use BH1750 light sensor (GY-30 module).
99
* BH1750 measurement mode:
10-
* one-time mode: bh1750 just measure only one time when received the one time measurement command, so you need to send this command when you want to get intensity value every time
11-
* continuous mode: bh1750 will measure continuously when received the continuously measurement command, so you just need to send this command once, and than call `bh1750_get_data()` to get intensity value repeatedly.
10+
* one-time mode: BH1750 just measure only one time when received the one time measurement command, so you need to send this command when you want to get intensity value every time
11+
* continuous mode: BH1750 will measure continuously when received the continuously measurement command, so you just need to send this command once, and than call `bh1750_get_data()` to get intensity value repeatedly.
1212
## Notice:
13-
* Bh1750 has different measurement time in different measurement mode, and also, measurement time can be changed by call `bh1750_change_measure_time()`
13+
* BH1750 has different measurement time for each measurement mode. Measurement time can be changed by calling `bh1750_change_measure_time()`.

components/bh1750/bh1750.c

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,59 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
#include <stdio.h>
8-
#include "driver/i2c.h"
97
#include "bh1750.h"
8+
#include "driver/i2c_master.h"
9+
#include "freertos/FreeRTOS.h"
10+
#include "freertos/projdefs.h" // for pdMS_TO_TICKS
1011

1112
#define BH_1750_MEASUREMENT_ACCURACY 1.2 /*!< the typical measurement accuracy of BH1750 sensor */
1213

1314
#define BH1750_POWER_DOWN 0x00 /*!< Command to set Power Down*/
1415
#define BH1750_POWER_ON 0x01 /*!< Command to set Power On*/
16+
#define I2C_CLK_SPEED 400000
1517

1618
typedef struct {
17-
i2c_port_t bus;
18-
uint16_t dev_addr;
19+
i2c_master_dev_handle_t i2c_handle;
1920
} bh1750_dev_t;
2021

2122
static esp_err_t bh1750_write_byte(const bh1750_dev_t *const sens, const uint8_t byte)
2223
{
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));
3825
}
3926

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)
4128
{
29+
esp_err_t ret = ESP_OK;
4230
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;
4649
}
4750

4851
esp_err_t bh1750_delete(bh1750_handle_t sensor)
4952
{
5053
bh1750_dev_t *sens = (bh1750_dev_t *) sensor;
54+
if (sens->i2c_handle) {
55+
i2c_master_bus_rm_device(sens->i2c_handle);
56+
}
5157
free(sens);
5258
return ESP_OK;
5359
}
@@ -88,26 +94,12 @@ esp_err_t bh1750_set_measure_mode(bh1750_handle_t sensor, const bh1750_measure_m
8894

8995
esp_err_t bh1750_get_data(bh1750_handle_t sensor, float *const data)
9096
{
91-
esp_err_t ret;
92-
uint8_t bh1750_data_h, bh1750_data_l;
9397
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));
108100
if (ESP_OK != ret) {
109101
return ret;
110102
}
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);
112104
return ESP_OK;
113105
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
version: "1.0.3"
1+
version: "2.0.0"
22
description: I2C driver for BH1750 light sensor
33
url: https://github.com/espressif/esp-bsp/tree/master/components/bh1750
44
dependencies:
5-
idf : ">=4.0"
5+
idf : ">=5.3" # esp_driver_i2c requires IDF v5.3 or later

components/bh1750/include/bh1750.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -15,7 +15,8 @@
1515
extern "C" {
1616
#endif
1717

18-
#include "driver/i2c.h"
18+
#include "driver/i2c_types.h"
19+
#include "esp_err.h"
1920

2021
typedef enum {
2122
BH1750_CONTINUE_1LX_RES = 0x10, /*!< Command to set measure mode as Continuously H-Resolution mode*/
@@ -107,14 +108,17 @@ esp_err_t bh1750_set_measure_time(bh1750_handle_t sensor, const uint8_t measure_
107108
/**
108109
* @brief Create and init sensor object and return a sensor handle
109110
*
110-
* @param port I2C port number
111-
* @param[in] dev_addr I2C device address of sensor
111+
* @param[in] i2c_bus I2C bus handle. Obtained from i2c_new_master_bus().s
112+
* @param[in] dev_addr I2C device address of sensor. Use BH1750_I2C_ADDRESS_DEFAULT for default address.
113+
* @param[out] handle_ret Handle to created BH1750 driver object.
112114
*
113115
* @return
114-
* - NULL Fail
115-
* - Others Success
116+
* - ESP_OK Success
117+
* - ESP_ERR_NO_MEM Not enough memory for the driver
118+
* - ESP_ERR_NOT_FOUND Sensor not found on the I2C bus
119+
* - Others Error from underlying I2C driver
116120
*/
117-
bh1750_handle_t bh1750_create(i2c_port_t port, const uint16_t dev_addr);
121+
esp_err_t bh1750_create(i2c_master_bus_handle_t i2c_bus, const uint8_t dev_addr, bh1750_handle_t *handle_ret);
118122

119123
/**
120124
* @brief Delete and release a sensor object

components/bh1750/test/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

components/bh1750/test/bh1750_test.c

Lines changed: 0 additions & 85 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# The following five lines of boilerplate have to be in your project's
2+
# CMakeLists in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.22)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
project(test_apps)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
idf_component_register(SRCS "test_apps.c"
2+
INCLUDE_DIRS ".")

0 commit comments

Comments
 (0)