From 745bae4e880c281fef1bc7eb9a41c7a79e3dec74 Mon Sep 17 00:00:00 2001 From: Jason-mao Date: Mon, 8 Apr 2024 18:39:53 +0800 Subject: [PATCH] esp_peripherals: Add i2c_bus_run_cb --- .../esp_peripherals/driver/i2c_bus/i2c_bus.c | 17 +++++++++++++++-- .../esp_peripherals/driver/i2c_bus/i2c_bus.h | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/components/esp_peripherals/driver/i2c_bus/i2c_bus.c b/components/esp_peripherals/driver/i2c_bus/i2c_bus.c index 2ed80e0c8..32f3c21cb 100644 --- a/components/esp_peripherals/driver/i2c_bus/i2c_bus.c +++ b/components/esp_peripherals/driver/i2c_bus/i2c_bus.c @@ -62,8 +62,8 @@ i2c_bus_handle_t i2c_bus_create(i2c_port_t port, i2c_config_t *conf) ESP_LOGW(TAG, "I2C bus has been already created, [port:%d]", port); return i2c_bus[port]; } else { - ESP_LOGE(TAG, "Have not enough slot(%d) to create I2C bus", port); - return NULL; + ESP_LOGE(TAG, "Have not enough slot(%d) to create I2C bus", port); + return NULL; } } @@ -216,3 +216,16 @@ esp_err_t i2c_bus_probe_addr(i2c_bus_handle_t bus, uint8_t addr) /* Get probe result if ESP_OK equals to ret_val */ return ret_val; } + +esp_err_t i2c_bus_run_cb(i2c_bus_handle_t bus, i2c_run_cb_t cb, void *arg) +{ + I2C_BUS_CHECK(bus != NULL, "Handle error", ESP_FAIL); + I2C_BUS_CHECK(cb != NULL, "Invalid callback", ESP_FAIL); + + i2c_bus_t *i2c_bus = (i2c_bus_t *) bus; + mutex_lock(i2c_bus->bus_lock); + (*cb)(i2c_bus->i2c_port, arg); + mutex_unlock(i2c_bus->bus_lock); + + return ESP_OK; +} diff --git a/components/esp_peripherals/driver/i2c_bus/i2c_bus.h b/components/esp_peripherals/driver/i2c_bus/i2c_bus.h index b4835c2d8..03c0d5ac3 100644 --- a/components/esp_peripherals/driver/i2c_bus/i2c_bus.h +++ b/components/esp_peripherals/driver/i2c_bus/i2c_bus.h @@ -31,6 +31,7 @@ extern "C" { #endif typedef void *i2c_bus_handle_t; +typedef void (*i2c_run_cb_t)(i2c_port_t port, void *arg); /** * @brief Create and init I2C bus and return a I2C bus handle @@ -125,6 +126,19 @@ esp_err_t i2c_bus_cmd_begin(i2c_bus_handle_t bus, i2c_cmd_handle_t cmd, portBASE */ esp_err_t i2c_bus_probe_addr(i2c_bus_handle_t bus, uint8_t addr); +/** + * @brief Lock the I2C bus while executing the given callback + * + * @param bus I2C bus handle + * @param cb The callback to execute + * @param arg The argument for the callback + * + * @return + * - ESP_OK Done calling callback function + * - ESP_FAIL Fail + */ +esp_err_t i2c_bus_run_cb(i2c_bus_handle_t bus, i2c_run_cb_t cb, void *arg); + #ifdef __cplusplus } #endif