4242#include "hal/clk_gate_ll.h"
4343#include "esp32-hal-log.h"
4444#include "esp32-hal-i2c-slave.h"
45+ #include "esp32-hal-periman.h"
4546
4647#define I2C_SLAVE_USE_RX_QUEUE 0 // 1: Queue, 0: RingBuffer
4748
@@ -194,7 +195,7 @@ static bool i2c_slave_handle_rx_fifo_full(i2c_slave_struct_t * i2c, uint32_t len
194195static size_t i2c_slave_read_rx (i2c_slave_struct_t * i2c , uint8_t * data , size_t len );
195196static void i2c_slave_isr_handler (void * arg );
196197static void i2c_slave_task (void * pv_args );
197-
198+ static bool i2cSlaveDetachBus ( void * bus_i2c_num );
198199
199200//=====================================================================================================================
200201//-------------------------------------- Public Functions -------------------------------------------------------------
@@ -231,6 +232,11 @@ esp_err_t i2cSlaveInit(uint8_t num, int sda, int scl, uint16_t slaveID, uint32_t
231232 frequency = 1000000 ;
232233 }
233234
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+
234240 log_i ("Initialising I2C Slave: sda=%d scl=%d freq=%d, addr=0x%x" , sda , scl , frequency , slaveID );
235241
236242 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
344350 i2c_ll_slave_enable_rx_it (i2c -> dev );
345351 i2c_ll_set_stretch (i2c -> dev , 0x3FF );
346352 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+ }
347357 I2C_SLAVE_MUTEX_UNLOCK ();
348358 return ret ;
349359
@@ -367,7 +377,11 @@ esp_err_t i2cSlaveDeinit(uint8_t num){
367377 }
368378#endif
369379 I2C_SLAVE_MUTEX_LOCK ();
380+ int scl = i2c -> scl ;
381+ int sda = i2c -> sda ;
370382 i2c_slave_free_resources (i2c );
383+ perimanSetPinBus (scl , ESP32_BUS_TYPE_INIT , NULL );
384+ perimanSetPinBus (sda , ESP32_BUS_TYPE_INIT , NULL );
371385 I2C_SLAVE_MUTEX_UNLOCK ();
372386 return ESP_OK ;
373387}
@@ -846,3 +860,17 @@ static void i2c_slave_task(void *pv_args)
846860 }
847861 vTaskDelete (NULL );
848862}
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