42
42
#include "hal/clk_gate_ll.h"
43
43
#include "esp32-hal-log.h"
44
44
#include "esp32-hal-i2c-slave.h"
45
+ #include "esp32-hal-periman.h"
45
46
46
47
#define I2C_SLAVE_USE_RX_QUEUE 0 // 1: Queue, 0: RingBuffer
47
48
@@ -194,7 +195,7 @@ static bool i2c_slave_handle_rx_fifo_full(i2c_slave_struct_t * i2c, uint32_t len
194
195
static size_t i2c_slave_read_rx (i2c_slave_struct_t * i2c , uint8_t * data , size_t len );
195
196
static void i2c_slave_isr_handler (void * arg );
196
197
static void i2c_slave_task (void * pv_args );
197
-
198
+ static bool i2cSlaveDetachBus ( void * bus_i2c_num );
198
199
199
200
//=====================================================================================================================
200
201
//-------------------------------------- Public Functions -------------------------------------------------------------
@@ -231,6 +232,11 @@ esp_err_t i2cSlaveInit(uint8_t num, int sda, int scl, uint16_t slaveID, uint32_t
231
232
frequency = 1000000 ;
232
233
}
233
234
235
+ perimanSetBusDeinit (ESP32_BUS_TYPE_I2C_SLAVE , i2cSlaveDetachBus );
236
+ if (!perimanSetPinBus (sda , ESP32_BUS_TYPE_INIT , NULL ) || !perimanSetPinBus (scl , ESP32_BUS_TYPE_INIT , NULL )){
237
+ return false;
238
+ }
239
+
234
240
log_i ("Initialising I2C Slave: sda=%d scl=%d freq=%d, addr=0x%x" , sda , scl , frequency , slaveID );
235
241
236
242
i2c_slave_struct_t * i2c = & _i2c_bus_array [num ];
@@ -344,6 +350,10 @@ esp_err_t i2cSlaveInit(uint8_t num, int sda, int scl, uint16_t slaveID, uint32_t
344
350
i2c_ll_slave_enable_rx_it (i2c -> dev );
345
351
i2c_ll_set_stretch (i2c -> dev , 0x3FF );
346
352
i2c_ll_update (i2c -> dev );
353
+ if (!perimanSetPinBus (sda , ESP32_BUS_TYPE_I2C_SLAVE , (void * )(i2c -> num + 1 )) || !perimanSetPinBus (scl , ESP32_BUS_TYPE_I2C_SLAVE , (void * )(i2c -> num + 1 ))){
354
+ i2cSlaveDetachBus ((void * )(i2c -> num + 1 ));
355
+ ret = ESP_FAIL ;
356
+ }
347
357
I2C_SLAVE_MUTEX_UNLOCK ();
348
358
return ret ;
349
359
@@ -367,7 +377,11 @@ esp_err_t i2cSlaveDeinit(uint8_t num){
367
377
}
368
378
#endif
369
379
I2C_SLAVE_MUTEX_LOCK ();
380
+ int scl = i2c -> scl ;
381
+ int sda = i2c -> sda ;
370
382
i2c_slave_free_resources (i2c );
383
+ perimanSetPinBus (scl , ESP32_BUS_TYPE_INIT , NULL );
384
+ perimanSetPinBus (sda , ESP32_BUS_TYPE_INIT , NULL );
371
385
I2C_SLAVE_MUTEX_UNLOCK ();
372
386
return ESP_OK ;
373
387
}
@@ -846,3 +860,17 @@ static void i2c_slave_task(void *pv_args)
846
860
}
847
861
vTaskDelete (NULL );
848
862
}
863
+
864
+ static bool i2cSlaveDetachBus (void * bus_i2c_num ){
865
+ uint8_t num = (int )bus_i2c_num - 1 ;
866
+ i2c_slave_struct_t * i2c = & _i2c_bus_array [num ];
867
+ if (i2c -> scl == -1 && i2c -> sda == -1 ) {
868
+ return true;
869
+ }
870
+ esp_err_t err = i2cSlaveDeinit (num );
871
+ if (err != ESP_OK ){
872
+ log_e ("i2cSlaveDeinit failed with error: %d" , err );
873
+ return false;
874
+ }
875
+ return true;
876
+ }
0 commit comments