Skip to content

Commit 892596b

Browse files
committed
Improved IDF v6 i2c code
1 parent c94266e commit 892596b

2 files changed

Lines changed: 32 additions & 15 deletions

File tree

examples/Arduino/LilyGoT5_Shutdown/LilyGoT5_Shutdown.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ FASTEPD epaper;
99

1010
#define PWRMGMT_ADDR 0x6b
1111
#define REG09 9
12-
#define SDA_PIN 6
13-
#define SCL_PIN 5
12+
#define SDA_PIN 39
13+
#define SCL_PIN 40
1414
extern "C" {
1515
void s3_prep_diff(uint8_t *pCurr, uint8_t *pPrev, uint8_t *pDest, int iWidth);
1616
}
@@ -66,7 +66,8 @@ void setup()
6666
delay(3000);
6767
Serial.println("Starting...");
6868
// TestSIMD();
69-
epaper.initPanel(BB_PANEL_LILYGO_T5PRO);
69+
epaper.initPanel(BB_PANEL_EPDIY_V7);
70+
epaper.setPanelSize(960, 540);
7071
epaper.clearWhite();
7172
epaper.setFont(FONT_12x16);
7273
epaper.println("Power off in 5 seconds...");

src/arduino_io.inl

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ 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;
37-
i2c_master_dev_handle_t dev_handle;
36+
i2c_master_bus_handle_t bus_handle = 0;
37+
i2c_master_dev_handle_t dev_handle = 0;
38+
uint8_t u8Address = 0xff;
3839
// GPIO modes
3940
#define memcpy_P memcpy
4041
#define pgm_read_byte(a) (*(uint8_t *)a)
@@ -278,6 +279,8 @@ int bbepI2CInit(uint8_t sda, uint8_t scl, int bb)
278279
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
279280
i2c_master_bus_config_t conf;
280281

282+
if (bus_handle) return BBEP_SUCCESS; // already initialized
283+
281284
conf.i2c_port = I2C_NUM_0;
282285
conf.sda_io_num = (gpio_num_t)sda;
283286
conf.scl_io_num = (gpio_num_t)scl;
@@ -318,12 +321,19 @@ int bbepI2CWrite(unsigned char iAddr, unsigned char *pData, int iLen)
318321
#else
319322
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
320323
i2c_device_config_t dev_config;
321-
dev_config.dev_addr_length = I2C_ADDR_BIT_LEN_7;
322-
dev_config.device_address = iAddr;
323-
dev_config.scl_speed_hz = 400000;
324-
ESP_ERROR_CHECK(i2c_master_bus_add_device(bus_handle, &dev_config, &dev_handle));
324+
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+
}
330+
dev_config.dev_addr_length = I2C_ADDR_BIT_LEN_7;
331+
dev_config.device_address = iAddr;
332+
dev_config.scl_speed_hz = 400000;
333+
ESP_ERROR_CHECK(i2c_master_bus_add_device(bus_handle, &dev_config, &dev_handle));
334+
u8Address = iAddr;
335+
}
325336
esp_err_t ret = i2c_master_transmit(dev_handle, pData, iLen, 1000); // 1-second timeout
326-
i2c_master_bus_rm_device(dev_handle);
327337
return (ret == ESP_OK);
328338
#else // older idf version
329339
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
@@ -357,12 +367,18 @@ int i = 0;
357367
esp_err_t ret;
358368
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
359369
i2c_device_config_t dev_config;
360-
dev_config.dev_addr_length = I2C_ADDR_BIT_LEN_7;
361-
dev_config.device_address = iAddr;
362-
dev_config.scl_speed_hz = 400000;
363-
ESP_ERROR_CHECK(i2c_master_bus_add_device(bus_handle, &dev_config, &dev_handle));
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+
}
375+
dev_config.dev_addr_length = I2C_ADDR_BIT_LEN_7;
376+
dev_config.device_address = iAddr;
377+
dev_config.scl_speed_hz = 400000;
378+
ESP_ERROR_CHECK(i2c_master_bus_add_device(bus_handle, &dev_config, &dev_handle));
379+
u8Address = iAddr;
380+
}
364381
ret = i2c_master_receive(dev_handle, pData, iLen, 1000); // 1-second timeout
365-
i2c_master_bus_rm_device(dev_handle);
366382
return (ret == ESP_OK);
367383
#else // older ESP-IDF
368384
i2c_cmd_handle_t cmd = i2c_cmd_link_create();

0 commit comments

Comments
 (0)