first commit

This commit is contained in:
Wang Beihong
2026-03-14 14:19:32 +08:00
commit fcc2d0825d
68 changed files with 16382 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
idf_component_register(
SRCS "i2c_master_messager.c"
INCLUDE_DIRS "include"
REQUIRES bh1750 k0i05__esp_ahtxx esp_driver_i2c esp_driver_gpio esp_timer
)

View File

@@ -0,0 +1,38 @@
menu "I2C 传感器管理"
config I2C_MASTER_MESSAGER_ENABLE_INTERNAL_PULLUP
bool "启用 I2C 内部上拉电阻"
default y
help
启用后SCL/SDA 会使用芯片内部上拉。
如果你的硬件已经有外部上拉电阻,通常也可以关闭该选项。
config I2C_MASTER_MESSAGER_BH1750_ENABLE
bool "启用 BH1750 光照传感器"
default y
help
关闭后将不会初始化与读取 BH1750。
config I2C_MASTER_MESSAGER_BH1750_READ_PERIOD_MS
int "BH1750 采样周期 (ms)"
range 100 10000
default 500
depends on I2C_MASTER_MESSAGER_BH1750_ENABLE
help
BH1750 的轮询间隔,单位毫秒。
config I2C_MASTER_MESSAGER_AHT30_ENABLE
bool "启用 AHT30 温湿度传感器"
default y
help
关闭后将不会初始化与读取 AHT30。
config I2C_MASTER_MESSAGER_AHT30_READ_PERIOD_MS
int "AHT30 采样周期 (ms)"
range 100 10000
default 2000
depends on I2C_MASTER_MESSAGER_AHT30_ENABLE
help
AHT30 的轮询间隔,单位毫秒。
endmenu

View File

@@ -0,0 +1,109 @@
# i2c_master_messager
`i2c_master_messager` 用于统一管理工程中的 I2C 传感器。
当前已接入:
- BH1750 光照传感器(使用驱动默认地址)
- AHT30 温湿度传感器(使用驱动默认地址)
设计目标:
- 提供统一的数据结构,方便其他模块读取
- 提供独立采集任务,周期性更新数据
- 为后续新增其他 I2C 传感器预留扩展位置
- 各传感器驱动相互独立,管理层只做调度与数据汇总
- 支持每个传感器独立采样周期(例如光照快采、温湿度慢采)
## 驱动分层
- `k0i05__esp_ahtxx`AHT30 驱动组件(通过组件管理器引入)
- `bh1750`:使用 ESP 组件管理器驱动
- `i2c_master_messager.c`:统一总线管理、任务轮询、线程安全数据缓存
## 对外数据结构
头文件:`include/i2c_master_messager.h`
- `i2c_master_messager_data_t`
- `bh1750.lux`光照强度lx
- `bh1750.valid`:当前数据是否有效
- `bh1750.last_update_ms`:最后一次成功更新时间(毫秒)
- `bh1750.last_error`:最后一次采集错误码
- `aht30.temperature_c`:温度(摄氏度)
- `aht30.humidity_rh`:湿度(%RH
- `aht30.valid`:当前数据是否有效
- `aht30.last_update_ms`:最后一次成功更新时间(毫秒)
- `aht30.last_error`:最后一次采集错误码
后续新增传感器时,建议继续在 `i2c_master_messager_data_t` 中增加对应字段。
## API
- `esp_err_t i2c_master_messager_init(const i2c_master_messager_config_t *config);`
- 初始化 I2C 总线(传感器驱动在任务中懒初始化)
- `esp_err_t i2c_master_messager_start(void);`
- 启动循环采集任务
- `esp_err_t i2c_master_messager_stop(void);`
- 停止采集任务
- `esp_err_t i2c_master_messager_get_data(i2c_master_messager_data_t *out_data);`
- 读取当前缓存数据(线程安全)
- `esp_err_t i2c_master_messager_deinit(void);`
- 释放总线和传感器资源
## menuconfig 配置
路径:`Component config -> I2C Master Messager`
- `启用 BH1750 光照传感器`:开关 BH1750
- `启用 I2C 内部上拉电阻`:控制是否打开芯片内部上拉
- `BH1750 采样周期 (ms)`:光照采样周期
- `启用 AHT30 温湿度传感器`:开关 AHT30
- `AHT30 采样周期 (ms)`:温湿度采样周期
这两个周期相互独立,可按业务需求分别调优。
## 使用示例
```c
#include "esp_check.h"
#include "i2c_master_messager.h"
void app_main(void)
{
i2c_master_messager_config_t cfg = {
.i2c_port = I2C_NUM_0,
.scl_io_num = GPIO_NUM_5,
.sda_io_num = GPIO_NUM_4,
.i2c_enable_internal_pullup = true,
.bh1750_enable = true,
.aht30_enable = true,
.bh1750_read_period_ms = 500,
.aht30_read_period_ms = 2000,
.bh1750_mode = BH1750_CONTINUE_1LX_RES,
};
ESP_ERROR_CHECK(i2c_master_messager_init(&cfg));
ESP_ERROR_CHECK(i2c_master_messager_start());
while (1) {
i2c_master_messager_data_t data;
if (i2c_master_messager_get_data(&data) == ESP_OK && data.bh1750.valid && data.aht30.valid) {
printf("BH1750: %.2f lx, AHT30: %.2f C %.2f %%RH\n",
data.bh1750.lux,
data.aht30.temperature_c,
data.aht30.humidity_rh);
}
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
```
## 扩展建议
新增其他传感器时,建议按以下步骤扩展:
1.`i2c_master_messager_data_t` 中增加该传感器的数据结构
2.`i2c_master_messager_config_t` 中增加该传感器配置项
3. 在任务循环中按需完成驱动初始化与重试
4. 在采集任务中加入周期读取逻辑并更新共享数据
5.`deinit()` 中释放对应资源

View File

@@ -0,0 +1,356 @@
#include <string.h>
#include "driver/i2c_master.h"
#include "ahtxx.h"
#include "esp_check.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/task.h"
#include "i2c_master_messager.h"
static const char *TAG = "i2c_master_messager";
#define BH1750_REINIT_INTERVAL_MS (3000)
#define AHT30_REINIT_INTERVAL_MS (3000)
typedef struct {
bool initialized;
bool owns_i2c_bus;
bool bh1750_ready;
bool aht30_ready;
i2c_master_messager_config_t config;
i2c_master_bus_handle_t i2c_bus;
bh1750_handle_t bh1750;
ahtxx_handle_t aht30;
i2c_master_messager_data_t data;
SemaphoreHandle_t lock;
TaskHandle_t task_handle;
} i2c_master_messager_ctx_t;
static i2c_master_messager_ctx_t g_ctx;
static esp_err_t i2c_master_messager_try_init_bh1750(void)
{
if (!g_ctx.config.bh1750_enable) {
return ESP_ERR_NOT_SUPPORTED;
}
if (g_ctx.bh1750 != NULL) {
bh1750_delete(g_ctx.bh1750);
g_ctx.bh1750 = NULL;
}
esp_err_t ret = bh1750_create(g_ctx.i2c_bus, BH1750_I2C_ADDRESS_DEFAULT, &g_ctx.bh1750);
if (ret != ESP_OK) {
return ret;
}
ret = bh1750_power_on(g_ctx.bh1750);
if (ret != ESP_OK) {
bh1750_delete(g_ctx.bh1750);
g_ctx.bh1750 = NULL;
return ret;
}
ret = bh1750_set_measure_mode(g_ctx.bh1750, g_ctx.config.bh1750_mode);
if (ret != ESP_OK) {
bh1750_delete(g_ctx.bh1750);
g_ctx.bh1750 = NULL;
return ret;
}
g_ctx.bh1750_ready = true;
return ESP_OK;
}
static esp_err_t i2c_master_messager_try_init_aht30(void)
{
if (!g_ctx.config.aht30_enable) {
return ESP_ERR_NOT_SUPPORTED;
}
if (g_ctx.aht30 != NULL) {
ahtxx_delete(g_ctx.aht30);
g_ctx.aht30 = NULL;
}
ahtxx_config_t aht_cfg = I2C_AHT30_CONFIG_DEFAULT;
esp_err_t ret = ahtxx_init(g_ctx.i2c_bus, &aht_cfg, &g_ctx.aht30);
if (ret != ESP_OK) {
return ret;
}
g_ctx.aht30_ready = true;
return ESP_OK;
}
static void i2c_master_messager_task(void *arg)
{
(void)arg;
int64_t last_bh1750_reinit_ms = 0;
int64_t last_bh1750_read_ms = 0;
int64_t last_aht30_read_ms = 0;
int64_t last_aht30_reinit_ms = 0;
while (1) {
int64_t now_ms = esp_timer_get_time() / 1000;
if (g_ctx.config.bh1750_enable && !g_ctx.bh1750_ready &&
(now_ms - last_bh1750_reinit_ms) >= BH1750_REINIT_INTERVAL_MS) {
last_bh1750_reinit_ms = now_ms;
esp_err_t init_ret = i2c_master_messager_try_init_bh1750();
if (init_ret == ESP_OK) {
ESP_LOGI(TAG, "BH1750 reinit success");
} else {
ESP_LOGW(TAG, "BH1750 reinit failed: %s", esp_err_to_name(init_ret));
if (xSemaphoreTake(g_ctx.lock, pdMS_TO_TICKS(100)) == pdTRUE) {
g_ctx.data.bh1750.valid = false;
g_ctx.data.bh1750.last_error = init_ret;
xSemaphoreGive(g_ctx.lock);
}
}
}
if (g_ctx.bh1750_ready && g_ctx.bh1750 != NULL &&
(now_ms - last_bh1750_read_ms) >= g_ctx.config.bh1750_read_period_ms) {
float lux = 0.0f;
esp_err_t bh1750_ret = bh1750_get_data(g_ctx.bh1750, &lux);
last_bh1750_read_ms = now_ms;
if (xSemaphoreTake(g_ctx.lock, pdMS_TO_TICKS(100)) == pdTRUE) {
g_ctx.data.bh1750.valid = (bh1750_ret == ESP_OK);
g_ctx.data.bh1750.last_error = bh1750_ret;
if (bh1750_ret == ESP_OK) {
g_ctx.data.bh1750.lux = lux;
g_ctx.data.bh1750.last_update_ms = now_ms;
}
xSemaphoreGive(g_ctx.lock);
}
if (bh1750_ret != ESP_OK) {
ESP_LOGW(TAG, "bh1750_get_data failed: %s", esp_err_to_name(bh1750_ret));
}
}
if (g_ctx.config.aht30_enable && !g_ctx.aht30_ready &&
(now_ms - last_aht30_reinit_ms) >= AHT30_REINIT_INTERVAL_MS) {
last_aht30_reinit_ms = now_ms;
esp_err_t init_ret = i2c_master_messager_try_init_aht30();
if (init_ret == ESP_OK) {
ESP_LOGI(TAG, "AHT30 reinit success");
} else {
ESP_LOGW(TAG, "AHT30 reinit failed: %s", esp_err_to_name(init_ret));
if (xSemaphoreTake(g_ctx.lock, pdMS_TO_TICKS(100)) == pdTRUE) {
g_ctx.data.aht30.valid = false;
g_ctx.data.aht30.last_error = init_ret;
xSemaphoreGive(g_ctx.lock);
}
}
}
if (g_ctx.aht30_ready && g_ctx.aht30 != NULL &&
(now_ms - last_aht30_read_ms) >= g_ctx.config.aht30_read_period_ms) {
float temperature_c = 0.0f;
float humidity_rh = 0.0f;
esp_err_t aht30_ret = ahtxx_get_measurement(g_ctx.aht30, &temperature_c, &humidity_rh);
last_aht30_read_ms = now_ms;
if (xSemaphoreTake(g_ctx.lock, pdMS_TO_TICKS(100)) == pdTRUE) {
g_ctx.data.aht30.valid = (aht30_ret == ESP_OK);
g_ctx.data.aht30.last_error = aht30_ret;
if (aht30_ret == ESP_OK) {
g_ctx.data.aht30.temperature_c = temperature_c;
g_ctx.data.aht30.humidity_rh = humidity_rh;
g_ctx.data.aht30.last_update_ms = now_ms;
}
xSemaphoreGive(g_ctx.lock);
}
if (aht30_ret != ESP_OK) {
ESP_LOGW(TAG, "ahtxx_get_measurement failed: %s", esp_err_to_name(aht30_ret));
if (aht30_ret == ESP_ERR_INVALID_STATE || aht30_ret == ESP_ERR_TIMEOUT) {
if (g_ctx.aht30 != NULL) {
ahtxx_delete(g_ctx.aht30);
g_ctx.aht30 = NULL;
}
g_ctx.aht30_ready = false;
}
}
}
vTaskDelay(pdMS_TO_TICKS(I2C_MASTER_MESSAGER_MIN_PERIOD_MS));
}
}
esp_err_t i2c_master_messager_init(const i2c_master_messager_config_t *config)
{
ESP_RETURN_ON_FALSE(config != NULL, ESP_ERR_INVALID_ARG, TAG, "config is null");
ESP_RETURN_ON_FALSE(config->bh1750_enable || config->aht30_enable,
ESP_ERR_INVALID_ARG,
TAG,
"at least one sensor must be enabled");
ESP_RETURN_ON_FALSE(!config->bh1750_enable ||
config->bh1750_read_period_ms >= I2C_MASTER_MESSAGER_MIN_PERIOD_MS,
ESP_ERR_INVALID_ARG,
TAG,
"bh1750_read_period_ms too small");
ESP_RETURN_ON_FALSE(!config->aht30_enable ||
config->aht30_read_period_ms >= I2C_MASTER_MESSAGER_MIN_PERIOD_MS,
ESP_ERR_INVALID_ARG,
TAG,
"aht30_read_period_ms too small");
if (g_ctx.initialized) {
return ESP_ERR_INVALID_STATE;
}
memset(&g_ctx, 0, sizeof(g_ctx));
g_ctx.config = *config;
g_ctx.lock = xSemaphoreCreateMutex();
ESP_RETURN_ON_FALSE(g_ctx.lock != NULL, ESP_ERR_NO_MEM, TAG, "failed to create mutex");
const i2c_master_bus_config_t bus_cfg = {
.i2c_port = config->i2c_port,
.sda_io_num = config->sda_io_num,
.scl_io_num = config->scl_io_num,
.clk_source = I2C_CLK_SRC_DEFAULT,
.glitch_ignore_cnt = 7,
.flags.enable_internal_pullup = config->i2c_enable_internal_pullup,
};
esp_err_t ret = i2c_new_master_bus(&bus_cfg, &g_ctx.i2c_bus);
if (ret != ESP_OK) {
if (ret == ESP_ERR_INVALID_STATE) {
ESP_LOGW(TAG,
"i2c port %d already initialized, trying to reuse existing master bus",
config->i2c_port);
ret = i2c_master_get_bus_handle(config->i2c_port, &g_ctx.i2c_bus);
if (ret != ESP_OK || g_ctx.i2c_bus == NULL) {
ESP_LOGE(TAG,
"failed to reuse i2c bus on port %d: %s",
config->i2c_port,
esp_err_to_name(ret));
vSemaphoreDelete(g_ctx.lock);
g_ctx.lock = NULL;
return (ret == ESP_OK) ? ESP_ERR_INVALID_STATE : ret;
}
g_ctx.owns_i2c_bus = false;
} else {
ESP_LOGE(TAG, "i2c_new_master_bus failed: %s", esp_err_to_name(ret));
vSemaphoreDelete(g_ctx.lock);
g_ctx.lock = NULL;
return ret;
}
} else {
g_ctx.owns_i2c_bus = true;
}
if (!config->bh1750_enable) {
g_ctx.data.bh1750.valid = false;
g_ctx.data.bh1750.last_error = ESP_ERR_NOT_SUPPORTED;
} else {
g_ctx.data.bh1750.valid = false;
g_ctx.data.bh1750.last_error = ESP_ERR_INVALID_STATE;
}
if (!config->aht30_enable) {
g_ctx.data.aht30.valid = false;
g_ctx.data.aht30.last_error = ESP_ERR_NOT_SUPPORTED;
} else {
g_ctx.data.aht30.valid = false;
g_ctx.data.aht30.last_error = ESP_ERR_INVALID_STATE;
}
g_ctx.initialized = true;
ESP_LOGI(TAG,
"initialized: port=%d scl=%d sda=%d pullup_int=%d bh1750(en=%d,ready=%d,default,%ums) aht30(en=%d,ready=%d,default,%ums)",
config->i2c_port,
config->scl_io_num,
config->sda_io_num,
config->i2c_enable_internal_pullup,
config->bh1750_enable,
g_ctx.bh1750_ready,
config->bh1750_read_period_ms,
config->aht30_enable,
g_ctx.aht30_ready,
config->aht30_read_period_ms);
ESP_LOGI(TAG,
"i2c bus is ready; sensor drivers will initialize lazily in task loop");
return ESP_OK;
}
esp_err_t i2c_master_messager_start(void)
{
ESP_RETURN_ON_FALSE(g_ctx.initialized, ESP_ERR_INVALID_STATE, TAG, "not initialized");
if (g_ctx.task_handle != NULL) {
return ESP_OK;
}
BaseType_t ok = xTaskCreate(i2c_master_messager_task,
"i2c_msg_task",
4096,
NULL,
5,
&g_ctx.task_handle);
ESP_RETURN_ON_FALSE(ok == pdPASS, ESP_ERR_NO_MEM, TAG, "failed to create task");
return ESP_OK;
}
esp_err_t i2c_master_messager_stop(void)
{
ESP_RETURN_ON_FALSE(g_ctx.initialized, ESP_ERR_INVALID_STATE, TAG, "not initialized");
if (g_ctx.task_handle != NULL) {
vTaskDelete(g_ctx.task_handle);
g_ctx.task_handle = NULL;
}
return ESP_OK;
}
esp_err_t i2c_master_messager_get_data(i2c_master_messager_data_t *out_data)
{
ESP_RETURN_ON_FALSE(g_ctx.initialized, ESP_ERR_INVALID_STATE, TAG, "not initialized");
ESP_RETURN_ON_FALSE(out_data != NULL, ESP_ERR_INVALID_ARG, TAG, "out_data is null");
ESP_RETURN_ON_FALSE(g_ctx.lock != NULL, ESP_ERR_INVALID_STATE, TAG, "lock not ready");
ESP_RETURN_ON_FALSE(xSemaphoreTake(g_ctx.lock, pdMS_TO_TICKS(100)) == pdTRUE,
ESP_ERR_TIMEOUT,
TAG,
"failed to lock shared data");
*out_data = g_ctx.data;
xSemaphoreGive(g_ctx.lock);
return ESP_OK;
}
esp_err_t i2c_master_messager_deinit(void)
{
if (!g_ctx.initialized) {
return ESP_OK;
}
i2c_master_messager_stop();
if (g_ctx.bh1750 != NULL) {
bh1750_delete(g_ctx.bh1750);
g_ctx.bh1750 = NULL;
}
if (g_ctx.aht30 != NULL) {
ahtxx_delete(g_ctx.aht30);
g_ctx.aht30 = NULL;
}
if (g_ctx.i2c_bus != NULL && g_ctx.owns_i2c_bus) {
i2c_del_master_bus(g_ctx.i2c_bus);
}
g_ctx.i2c_bus = NULL;
g_ctx.owns_i2c_bus = false;
if (g_ctx.lock != NULL) {
vSemaphoreDelete(g_ctx.lock);
g_ctx.lock = NULL;
}
memset(&g_ctx, 0, sizeof(g_ctx));
return ESP_OK;
}

View File

@@ -0,0 +1,57 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include "bh1750.h"
#include "driver/gpio.h"
#include "driver/i2c_types.h"
#include "esp_err.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
i2c_port_num_t i2c_port;
gpio_num_t scl_io_num;
gpio_num_t sda_io_num;
bool i2c_enable_internal_pullup;
bool bh1750_enable;
bool aht30_enable;
uint16_t bh1750_read_period_ms;
uint16_t aht30_read_period_ms;
bh1750_measure_mode_t bh1750_mode;
} i2c_master_messager_config_t;
typedef struct {
float lux;
bool valid;
int64_t last_update_ms;
esp_err_t last_error;
} i2c_bh1750_data_t;
typedef struct {
float temperature_c;
float humidity_rh;
bool valid;
int64_t last_update_ms;
esp_err_t last_error;
} i2c_aht30_data_t;
typedef struct {
i2c_bh1750_data_t bh1750;
i2c_aht30_data_t aht30;
} i2c_master_messager_data_t;
#define I2C_MASTER_MESSAGER_MIN_PERIOD_MS (100)
esp_err_t i2c_master_messager_init(const i2c_master_messager_config_t *config);
esp_err_t i2c_master_messager_start(void);
esp_err_t i2c_master_messager_stop(void);
esp_err_t i2c_master_messager_get_data(i2c_master_messager_data_t *out_data);
esp_err_t i2c_master_messager_deinit(void);
#ifdef __cplusplus
}
#endif