Skip to content

Commit

Permalink
Merge branch 'feature/add_callback_for_i2c_bus' into 'master'
Browse files Browse the repository at this point in the history
esp_peripherals: Add i2c_bus_run_cb

See merge request adf/esp-adf-internal!1293
  • Loading branch information
jason-mao committed Apr 8, 2024
2 parents 7082bde + 745bae4 commit d01f540
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
17 changes: 15 additions & 2 deletions components/esp_peripherals/driver/i2c_bus/i2c_bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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;
}
14 changes: 14 additions & 0 deletions components/esp_peripherals/driver/i2c_bus/i2c_bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d01f540

Please sign in to comment.