Skip to content

Commit 3d5b570

Browse files
authored
I2C - Peripheral manager implementation (#8220)
* i2c-master periman initial commit * i2c-master make detachbus static + comment remove * i2c-slave periman implementation * SetPinBus to INIT on i2cDeinits * Fix slave pins deinit * remove dbg logs * set ret to ESP_FAIL instead of returning
1 parent 66c88fa commit 3d5b570

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

Diff for: cores/esp32/esp32-hal-i2c-slave.c

+29-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
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
194195
static size_t i2c_slave_read_rx(i2c_slave_struct_t * i2c, uint8_t * data, size_t len);
195196
static void i2c_slave_isr_handler(void* arg);
196197
static 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+
}

Diff for: cores/esp32/esp32-hal-i2c.c

+32
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,33 @@
2626
#include "hal/i2c_hal.h"
2727
#include "hal/i2c_ll.h"
2828
#include "driver/i2c.h"
29+
#include "esp32-hal-periman.h"
2930

3031
typedef volatile struct {
3132
bool initialized;
3233
uint32_t frequency;
3334
#if !CONFIG_DISABLE_HAL_LOCKS
3435
SemaphoreHandle_t lock;
36+
int8_t scl;
37+
int8_t sda;
3538
#endif
3639
} i2c_bus_t;
3740

3841
static i2c_bus_t bus[SOC_I2C_NUM];
3942

43+
static bool i2cDetachBus(void * bus_i2c_num){
44+
uint8_t i2c_num = (int)bus_i2c_num - 1;
45+
if(!bus[i2c_num].initialized){
46+
return true;
47+
}
48+
esp_err_t err = i2cDeinit(i2c_num);
49+
if(err != ESP_OK){
50+
log_e("i2cDeinit failed with error: %d", err);
51+
return false;
52+
}
53+
return true;
54+
}
55+
4056
bool i2cIsInit(uint8_t i2c_num){
4157
if(i2c_num >= SOC_I2C_NUM){
4258
return false;
@@ -72,6 +88,12 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency){
7288
} else if(frequency > 1000000UL){
7389
frequency = 1000000UL;
7490
}
91+
92+
perimanSetBusDeinit(ESP32_BUS_TYPE_I2C_MASTER, i2cDetachBus);
93+
if(!perimanSetPinBus(sda, ESP32_BUS_TYPE_INIT, NULL) || !perimanSetPinBus(scl, ESP32_BUS_TYPE_INIT, NULL)){
94+
return false;
95+
}
96+
7597
log_i("Initialising I2C Master: sda=%d scl=%d freq=%d", sda, scl, frequency);
7698

7799
i2c_config_t conf = { };
@@ -93,8 +115,14 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency){
93115
} else {
94116
bus[i2c_num].initialized = true;
95117
bus[i2c_num].frequency = frequency;
118+
bus[i2c_num].scl = scl;
119+
bus[i2c_num].sda = sda;
96120
//Clock Stretching Timeout: 20b:esp32, 5b:esp32-c3, 24b:esp32-s2
97121
i2c_set_timeout((i2c_port_t)i2c_num, I2C_LL_MAX_TIMEOUT);
122+
if(!perimanSetPinBus(sda, ESP32_BUS_TYPE_I2C_MASTER, (void *)(i2c_num+1)) || !perimanSetPinBus(scl, ESP32_BUS_TYPE_I2C_MASTER, (void *)(i2c_num+1))){
123+
i2cDetachBus((void *)(i2c_num+1));
124+
return false;
125+
}
98126
}
99127
}
100128
#if !CONFIG_DISABLE_HAL_LOCKS
@@ -122,6 +150,10 @@ esp_err_t i2cDeinit(uint8_t i2c_num){
122150
err = i2c_driver_delete((i2c_port_t)i2c_num);
123151
if(err == ESP_OK){
124152
bus[i2c_num].initialized = false;
153+
perimanSetPinBus(bus[i2c_num].scl, ESP32_BUS_TYPE_INIT, NULL);
154+
perimanSetPinBus(bus[i2c_num].sda, ESP32_BUS_TYPE_INIT, NULL);
155+
bus[i2c_num].scl = -1;
156+
bus[i2c_num].sda = -1;
125157
}
126158
}
127159
#if !CONFIG_DISABLE_HAL_LOCKS

0 commit comments

Comments
 (0)