Skip to content

Commit 0edbd1b

Browse files
committed
Added code to allow sharing of the esp-idf I2C bus handle with other libraries
1 parent 892596b commit 0edbd1b

2 files changed

Lines changed: 21 additions & 17 deletions

File tree

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=FastEPD
2-
version=2.0.0
2+
version=2.0.1
33
author=Larry Bank
44
maintainer=Larry Bank
55
sentence=A frustration-free e-paper library for parallel eink panels.

src/arduino_io.inl

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ static int iDelay = 0; //1;
3333
#include "driver/gpio.h"
3434
#include "esp_timer.h"
3535
#include "driver/i2c_master.h"
36-
i2c_master_bus_handle_t bus_handle = 0;
37-
i2c_master_dev_handle_t dev_handle = 0;
36+
i2c_master_bus_handle_t my_bus_handle = 0;
37+
i2c_master_dev_handle_t my_dev_handle = 0;
3838
uint8_t u8Address = 0xff;
3939
// GPIO modes
4040
#define memcpy_P memcpy
@@ -279,15 +279,19 @@ int bbepI2CInit(uint8_t sda, uint8_t scl, int bb)
279279
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
280280
i2c_master_bus_config_t conf;
281281

282-
if (bus_handle) return BBEP_SUCCESS; // already initialized
282+
if (my_bus_handle) return BBEP_SUCCESS; // already initialized
283283

284+
// Try to get existing bus (initialized elsewhere)
285+
if (i2c_master_get_bus_handle(I2C_NUM_0, &my_bus_handle) == ESP_OK) {
286+
return BBEP_SUCCESS;
287+
}
284288
conf.i2c_port = I2C_NUM_0;
285289
conf.sda_io_num = (gpio_num_t)sda;
286290
conf.scl_io_num = (gpio_num_t)scl;
287291
conf.clk_source = I2C_CLK_SRC_DEFAULT;
288292
conf.glitch_ignore_cnt = 7;
289293
conf.flags.enable_internal_pullup = true;
290-
ESP_ERROR_CHECK(i2c_new_master_bus(&conf, &bus_handle));
294+
ESP_ERROR_CHECK(i2c_new_master_bus(&conf, &my_bus_handle));
291295
#else // older esp-idff
292296
i2c_config_t conf;
293297
ESP_ERROR_CHECK(i2c_driver_delete());
@@ -322,18 +326,18 @@ int bbepI2CWrite(unsigned char iAddr, unsigned char *pData, int iLen)
322326
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
323327
i2c_device_config_t dev_config;
324328

325-
if (dev_handle || u8Address != iAddr) {
326-
if (dev_handle) {
327-
ESP_ERROR_CHECK(i2c_master_bus_rm_device(dev_handle));
328-
dev_handle = 0;
329+
if (my_dev_handle || u8Address != iAddr) {
330+
if (my_dev_handle) {
331+
ESP_ERROR_CHECK(i2c_master_bus_rm_device(my_dev_handle));
332+
my_dev_handle = 0;
329333
}
330334
dev_config.dev_addr_length = I2C_ADDR_BIT_LEN_7;
331335
dev_config.device_address = iAddr;
332336
dev_config.scl_speed_hz = 400000;
333-
ESP_ERROR_CHECK(i2c_master_bus_add_device(bus_handle, &dev_config, &dev_handle));
337+
ESP_ERROR_CHECK(i2c_master_bus_add_device(my_bus_handle, &dev_config, &my_dev_handle));
334338
u8Address = iAddr;
335339
}
336-
esp_err_t ret = i2c_master_transmit(dev_handle, pData, iLen, 1000); // 1-second timeout
340+
esp_err_t ret = i2c_master_transmit(my_dev_handle, pData, iLen, 1000); // 1-second timeout
337341
return (ret == ESP_OK);
338342
#else // older idf version
339343
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
@@ -367,18 +371,18 @@ int i = 0;
367371
esp_err_t ret;
368372
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
369373
i2c_device_config_t dev_config;
370-
if (dev_handle || u8Address != iAddr) {
371-
if (dev_handle) {
372-
ESP_ERROR_CHECK(i2c_master_bus_rm_device(dev_handle));
373-
dev_handle = 0;
374+
if (my_dev_handle || u8Address != iAddr) {
375+
if (my_dev_handle) {
376+
ESP_ERROR_CHECK(i2c_master_bus_rm_device(my_dev_handle));
377+
my_dev_handle = 0;
374378
}
375379
dev_config.dev_addr_length = I2C_ADDR_BIT_LEN_7;
376380
dev_config.device_address = iAddr;
377381
dev_config.scl_speed_hz = 400000;
378-
ESP_ERROR_CHECK(i2c_master_bus_add_device(bus_handle, &dev_config, &dev_handle));
382+
ESP_ERROR_CHECK(i2c_master_bus_add_device(my_bus_handle, &dev_config, &my_dev_handle));
379383
u8Address = iAddr;
380384
}
381-
ret = i2c_master_receive(dev_handle, pData, iLen, 1000); // 1-second timeout
385+
ret = i2c_master_receive(my_dev_handle, pData, iLen, 1000); // 1-second timeout
382386
return (ret == ESP_OK);
383387
#else // older ESP-IDF
384388
i2c_cmd_handle_t cmd = i2c_cmd_link_create();

0 commit comments

Comments
 (0)