2222#define __BB_EP_IO__
2323
2424// Since the Espressif I2C driver seems to corrupt memory with it's frequent allocs and frees, use bit banging
25- #define BIT_BANG
2625
27- #ifdef BIT_BANG
2826static uint8_t u8SDA_Pin, u8SCL_Pin;
27+ static uint8_t bBitBang;
2928static int iDelay = 0 ; // 1;
30- #endif
3129#include < freertos/FreeRTOS.h>
3230#include < freertos/task.h>
3331#include " rom/ets_sys.h"
@@ -91,7 +89,6 @@ void bbepPinMode(int iPin, int iMode)
9189 gpio_config (&io_conf); // configure GPIO with the given settings
9290} /* bbepPinMode() */
9391
94- #ifdef BIT_BANG
9592uint8_t SDA_READ (void )
9693{
9794 return gpio_get_level ((gpio_num_t )u8SDA_Pin);
@@ -257,24 +254,24 @@ uint8_t b = 0;
257254 i2cEnd ();
258255 return response;
259256 } /* I2CTest() */
260- #endif // BIT_BANG
261257//
262258// Initialize the Wire library on the given SDA/SCL GPIOs
263259//
264- int bbepI2CInit (uint8_t sda, uint8_t scl)
260+ int bbepI2CInit (uint8_t sda, uint8_t scl, int bb )
265261{
266- #ifdef BIT_BANG
267- u8SDA_Pin = sda;
268- u8SCL_Pin = scl;
262+ bBitBang = bb;
263+ if (bBitBang) {
264+ u8SDA_Pin = sda;
265+ u8SCL_Pin = scl;
269266// if (iSpeed >= 400000) iDelay = 1;
270267// else if (iSpeed >= 100000) iDelay = 10;
271268// else iDelay = 20;
272- # else
269+ } else {
273270#ifdef ARDUINO
274- Wire.end ();
275- Wire.begin (sda, scl);
276- Wire.setClock (400000 );
277- Wire.setTimeout (100 );
271+ Wire.end ();
272+ Wire.begin (sda, scl);
273+ Wire.setClock (400000 );
274+ Wire.setTimeout (100 );
278275#else
279276 i2c_config_t conf;
280277 ESP_ERROR_CHECK (i2c_driver_delete ());
@@ -287,17 +284,17 @@ int bbepI2CInit(uint8_t sda, uint8_t scl)
287284 conf.clk_flags = 0 ;
288285 ESP_ERROR_CHECK (i2c_param_config (I2C_NUM_0, &conf));
289286 ESP_ERROR_CHECK (i2c_driver_install (I2C_NUM_0, I2C_MODE_MASTER, 0 , 0 , 0 ));
290- #endif
291- # endif // BIT_BANG
287+ #endif // ARDUINO
288+ } // !BitBang
292289 return BBEP_SUCCESS;
293290} /* bbepI2CInit() */
294291
295292int bbepI2CWrite (unsigned char iAddr, unsigned char *pData, int iLen)
296293{
297- # ifdef BIT_BANG
298- I2CWrite (iAddr, pData, iLen);
299- return 1 ;
300- # else
294+ if (bBitBang) {
295+ I2CWrite (iAddr, pData, iLen);
296+ return 1 ;
297+ } else {
301298#ifdef ARDUINO
302299 int rc = 0 ;
303300 Wire.beginTransmission (iAddr);
@@ -316,16 +313,16 @@ int bbepI2CWrite(unsigned char iAddr, unsigned char *pData, int iLen)
316313 esp_err_t ret = i2c_master_cmd_begin (I2C_NUM_0, cmd, 1000 / portTICK_PERIOD_MS);
317314 i2c_cmd_link_delete (cmd);
318315 return (ret == ESP_OK);
319- #endif
320- # endif // BIT_BANG
316+ #endif // ARDUINO
317+ } // !BitBang
321318}
322319
323320int bbepI2CRead (unsigned char iAddr, unsigned char *pData, int iLen)
324321{
325322int i = 0 ;
326- # ifdef BIT_BANG
323+ if (bBitBang) {
327324 i = I2CRead (iAddr, pData, iLen);
328- # else
325+ } else {
329326#ifdef ARDUINO
330327 Wire.requestFrom (iAddr, (unsigned char )iLen);
331328 while (i < iLen && Wire.available ()) {
@@ -350,8 +347,8 @@ int i = 0;
350347 i = iLen;
351348 }
352349 i2c_cmd_link_delete (cmd);
353- #endif
354- # endif // BIT_BANG
350+ #endif // ARDUINO
351+ } // !BitBang
355352 return i;
356353}
357354int bbepI2CReadRegister (unsigned char iAddr, unsigned char u8Register, unsigned char *pData, int iLen)
0 commit comments