@@ -32,7 +32,9 @@ static int iDelay = 0; //1;
3232#ifndef ARDUINO
3333#include " driver/gpio.h"
3434#include " esp_timer.h"
35-
35+ #include " driver/i2c_master.h"
36+ i2c_master_bus_handle_t bus_handle;
37+ i2c_master_dev_handle_t dev_handle;
3638// GPIO modes
3739#define memcpy_P memcpy
3840#define pgm_read_byte (a ) (*(uint8_t *)a)
@@ -273,6 +275,17 @@ int bbepI2CInit(uint8_t sda, uint8_t scl, int bb)
273275 Wire.setClock (400000 );
274276 Wire.setTimeout (100 );
275277#else
278+ #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
279+ i2c_master_bus_config_t conf;
280+
281+ conf.i2c_port = I2C_NUM_0 ;
282+ conf.sda_io_num = (gpio_num_t )sda;
283+ conf.scl_io_num = (gpio_num_t )scl;
284+ conf.clk_source = I2C_CLK_SRC_DEFAULT ;
285+ conf.glitch_ignore_cnt = 7 ;
286+ conf.flags .enable_internal_pullup = true ;
287+ ESP_ERROR_CHECK (i2c_new_master_bus (&conf, &bus_handle));
288+ #else // older esp-idff
276289 i2c_config_t conf;
277290 ESP_ERROR_CHECK (i2c_driver_delete ());
278291 conf.mode = I2C_MODE_MASTER ;
@@ -281,9 +294,10 @@ int bbepI2CInit(uint8_t sda, uint8_t scl, int bb)
281294 conf.sda_pullup_en = GPIO_PULLUP_ENABLE ;
282295 conf.scl_pullup_en = GPIO_PULLUP_ENABLE ;
283296 conf.master .clk_speed = 400000 ;
284- conf.clk_flags = 0 ;
297+ conf.clk_flags = 0 ;
285298 ESP_ERROR_CHECK (i2c_param_config (I2C_NUM_0 , &conf));
286299 ESP_ERROR_CHECK (i2c_driver_install (I2C_NUM_0 , I2C_MODE_MASTER , 0 , 0 , 0 ));
300+ #endif // older IDF
287301#endif // ARDUINO
288302 } // !BitBang
289303 return BBEP_SUCCESS ;
@@ -302,6 +316,16 @@ int bbepI2CWrite(unsigned char iAddr, unsigned char *pData, int iLen)
302316 rc = !Wire.endTransmission ();
303317 return rc;
304318#else
319+ #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
320+ 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));
325+ esp_err_t ret = i2c_master_transmit (dev_handle, pData, iLen, 1000 ); // 1-second timeout
326+ i2c_master_bus_rm_device (dev_handle);
327+ return (ret == ESP_OK );
328+ #else // older idf version
305329 i2c_cmd_handle_t cmd = i2c_cmd_link_create ();
306330 if (cmd == NULL ) {
307331 // ESP_LOGE("bb_epdiy", "insufficient memory for I2C transaction");
@@ -313,6 +337,7 @@ int bbepI2CWrite(unsigned char iAddr, unsigned char *pData, int iLen)
313337 esp_err_t ret = i2c_master_cmd_begin (I2C_NUM_0 , cmd, 1000 / portTICK_PERIOD_MS);
314338 i2c_cmd_link_delete (cmd);
315339 return (ret == ESP_OK );
340+ #endif // older esp-idf
316341#endif // ARDUINO
317342 } // !BitBang
318343}
@@ -330,6 +355,16 @@ int i = 0;
330355 }
331356#else
332357 esp_err_t ret;
358+ #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
359+ 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));
364+ ret = i2c_master_transmit_receive (dev_handle, NULL , 0 , pData, iLen, 1000 ); // 1-second timeout
365+ i2c_master_bus_rm_device (dev_handle);
366+ return (ret == ESP_OK );
367+ #else // older ESP-IDF
333368 i2c_cmd_handle_t cmd = i2c_cmd_link_create ();
334369 if (cmd == NULL ) {
335370 // ESP_LOGE("epdiy", "insufficient memory for I2C transaction");
@@ -347,6 +382,7 @@ int i = 0;
347382 i = iLen;
348383 }
349384 i2c_cmd_link_delete (cmd);
385+ #endif // older esp-idf version
350386#endif // ARDUINO
351387 } // !BitBang
352388 return i;
0 commit comments