From 11cfde2d90d7abdd73bfc97cac11265d22d6e8d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Tue, 16 May 2023 15:40:47 +0200 Subject: [PATCH 1/7] i2c-master periman initial commit --- cores/esp32/esp32-hal-i2c.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/cores/esp32/esp32-hal-i2c.c b/cores/esp32/esp32-hal-i2c.c index c3f98e517bb..c0108f541a7 100644 --- a/cores/esp32/esp32-hal-i2c.c +++ b/cores/esp32/esp32-hal-i2c.c @@ -26,6 +26,7 @@ #include "hal/i2c_hal.h" #include "hal/i2c_ll.h" #include "driver/i2c.h" +#include "esp32-hal-periman.h" typedef volatile struct { bool initialized; @@ -37,6 +38,21 @@ typedef volatile struct { static i2c_bus_t bus[SOC_I2C_NUM]; +bool i2cDetachBus(void * bus_i2c_num){ + uint8_t i2c_num = (int)bus_i2c_num - 1; + if(!bus[i2c_num].initialized){ + log_d("i2cDetachBus I2C%d not initialized", i2c_num); + return true; + } + log_d("i2cDetachBus I2C%d calling i2cDeinit()", i2c_num); + esp_err_t err = i2cDeinit(i2c_num); + if(err != ESP_OK){ + log_e("i2cDeinit failed with error: %d", err); + return false; + } + return true; +} + bool i2cIsInit(uint8_t i2c_num){ if(i2c_num >= SOC_I2C_NUM){ return false; @@ -72,6 +88,12 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency){ } else if(frequency > 1000000UL){ frequency = 1000000UL; } + + if(!perimanSetPinBus(sda, ESP32_BUS_TYPE_INIT, NULL) || !perimanSetPinBus(scl, ESP32_BUS_TYPE_INIT, NULL)){ + return false; + } + perimanSetBusDeinit(ESP32_BUS_TYPE_I2C_MASTER, i2cDetachBus); + log_i("Initialising I2C Master: sda=%d scl=%d freq=%d", sda, scl, frequency); i2c_config_t conf = { }; @@ -95,6 +117,10 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency){ bus[i2c_num].frequency = frequency; //Clock Stretching Timeout: 20b:esp32, 5b:esp32-c3, 24b:esp32-s2 i2c_set_timeout((i2c_port_t)i2c_num, I2C_LL_MAX_TIMEOUT); + if(!perimanSetPinBus(sda, ESP32_BUS_TYPE_I2C_MASTER, (void *)(i2c_num+1)) || !perimanSetPinBus(scl, ESP32_BUS_TYPE_I2C_MASTER, (void *)(i2c_num+1))){ + i2cDetachBus((void *)(i2c_num+1)); + return false; + } } } #if !CONFIG_DISABLE_HAL_LOCKS @@ -122,6 +148,9 @@ esp_err_t i2cDeinit(uint8_t i2c_num){ err = i2c_driver_delete((i2c_port_t)i2c_num); if(err == ESP_OK){ bus[i2c_num].initialized = false; + /* + Add perimanSetPinBus(pin, ESP32_BUS_TYPE_INIT, NULL); for both pins -> add them to bus structure + */ } } #if !CONFIG_DISABLE_HAL_LOCKS From a65284bfd20f82b4ded1fcfa199b18c5661a94b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Wed, 17 May 2023 13:50:36 +0200 Subject: [PATCH 2/7] i2c-master make detachbus static + comment remove --- cores/esp32/esp32-hal-i2c.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/cores/esp32/esp32-hal-i2c.c b/cores/esp32/esp32-hal-i2c.c index c0108f541a7..a4037abe4d3 100644 --- a/cores/esp32/esp32-hal-i2c.c +++ b/cores/esp32/esp32-hal-i2c.c @@ -38,7 +38,7 @@ typedef volatile struct { static i2c_bus_t bus[SOC_I2C_NUM]; -bool i2cDetachBus(void * bus_i2c_num){ +static bool i2cDetachBus(void * bus_i2c_num){ uint8_t i2c_num = (int)bus_i2c_num - 1; if(!bus[i2c_num].initialized){ log_d("i2cDetachBus I2C%d not initialized", i2c_num); @@ -89,10 +89,10 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency){ frequency = 1000000UL; } + perimanSetBusDeinit(ESP32_BUS_TYPE_I2C_MASTER, i2cDetachBus); if(!perimanSetPinBus(sda, ESP32_BUS_TYPE_INIT, NULL) || !perimanSetPinBus(scl, ESP32_BUS_TYPE_INIT, NULL)){ return false; } - perimanSetBusDeinit(ESP32_BUS_TYPE_I2C_MASTER, i2cDetachBus); log_i("Initialising I2C Master: sda=%d scl=%d freq=%d", sda, scl, frequency); @@ -148,9 +148,6 @@ esp_err_t i2cDeinit(uint8_t i2c_num){ err = i2c_driver_delete((i2c_port_t)i2c_num); if(err == ESP_OK){ bus[i2c_num].initialized = false; - /* - Add perimanSetPinBus(pin, ESP32_BUS_TYPE_INIT, NULL); for both pins -> add them to bus structure - */ } } #if !CONFIG_DISABLE_HAL_LOCKS From 0e4ae9619cb55ecca5251e9360c57c1c26995df8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Wed, 17 May 2023 13:54:10 +0200 Subject: [PATCH 3/7] i2c-slave periman implementation --- cores/esp32/esp32-hal-i2c-slave.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/cores/esp32/esp32-hal-i2c-slave.c b/cores/esp32/esp32-hal-i2c-slave.c index 9b54cce9b1d..f361efb274f 100644 --- a/cores/esp32/esp32-hal-i2c-slave.c +++ b/cores/esp32/esp32-hal-i2c-slave.c @@ -42,6 +42,7 @@ #include "hal/clk_gate_ll.h" #include "esp32-hal-log.h" #include "esp32-hal-i2c-slave.h" +#include "esp32-hal-periman.h" #define I2C_SLAVE_USE_RX_QUEUE 0 // 1: Queue, 0: RingBuffer @@ -194,7 +195,7 @@ static bool i2c_slave_handle_rx_fifo_full(i2c_slave_struct_t * i2c, uint32_t len static size_t i2c_slave_read_rx(i2c_slave_struct_t * i2c, uint8_t * data, size_t len); static void i2c_slave_isr_handler(void* arg); static void i2c_slave_task(void *pv_args); - +static bool i2cSlaveDetachBus(void * bus_i2c_num); //===================================================================================================================== //-------------------------------------- Public Functions ------------------------------------------------------------- @@ -231,6 +232,11 @@ esp_err_t i2cSlaveInit(uint8_t num, int sda, int scl, uint16_t slaveID, uint32_t frequency = 1000000; } + perimanSetBusDeinit(ESP32_BUS_TYPE_I2C_SLAVE, i2cSlaveDetachBus); + if(!perimanSetPinBus(sda, ESP32_BUS_TYPE_INIT, NULL) || !perimanSetPinBus(scl, ESP32_BUS_TYPE_INIT, NULL)){ + return false; + } + log_i("Initialising I2C Slave: sda=%d scl=%d freq=%d, addr=0x%x", sda, scl, frequency, slaveID); 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 i2c_ll_slave_enable_rx_it(i2c->dev); i2c_ll_set_stretch(i2c->dev, 0x3FF); i2c_ll_update(i2c->dev); + if(!perimanSetPinBus(sda, ESP32_BUS_TYPE_I2C_SLAVE, (void *)(i2c_num+1)) || !perimanSetPinBus(scl, ESP32_BUS_TYPE_I2C_SLAVE, (void *)(i2c_num+1))){ + i2cSlaveDetachBus((void *)(i2c_num+1)); + return false; + } I2C_SLAVE_MUTEX_UNLOCK(); return ret; @@ -846,3 +856,19 @@ static void i2c_slave_task(void *pv_args) } vTaskDelete(NULL); } + +static bool i2cSlaveDetachBus(void * bus_i2c_num){ + uint8_t num = (int)bus_i2c_num - 1; + i2c_slave_struct_t * i2c = &_i2c_bus_array[num]; + if (i2c->scl == -1 && i2c->sda == -1) { + log_d("i2cSlaveDetachBus: I2C%d not initialized", num); + return true; + } + log_d("i2cSlaveDetachBus: I2C%d calling i2cSlaveDeinit()", num); + esp_err_t err = i2cSlaveDeinit(num); + if(err != ESP_OK){ + log_e("i2cSlaveDeinit failed with error: %d", err); + return false; + } + return true; +} From 8446fa320705573a6978915b835a70379b538d84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Wed, 17 May 2023 15:29:55 +0200 Subject: [PATCH 4/7] SetPinBus to INIT on i2cDeinits --- cores/esp32/esp32-hal-i2c-slave.c | 6 ++++-- cores/esp32/esp32-hal-i2c.c | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/cores/esp32/esp32-hal-i2c-slave.c b/cores/esp32/esp32-hal-i2c-slave.c index f361efb274f..007ea35120f 100644 --- a/cores/esp32/esp32-hal-i2c-slave.c +++ b/cores/esp32/esp32-hal-i2c-slave.c @@ -350,8 +350,8 @@ esp_err_t i2cSlaveInit(uint8_t num, int sda, int scl, uint16_t slaveID, uint32_t i2c_ll_slave_enable_rx_it(i2c->dev); i2c_ll_set_stretch(i2c->dev, 0x3FF); i2c_ll_update(i2c->dev); - if(!perimanSetPinBus(sda, ESP32_BUS_TYPE_I2C_SLAVE, (void *)(i2c_num+1)) || !perimanSetPinBus(scl, ESP32_BUS_TYPE_I2C_SLAVE, (void *)(i2c_num+1))){ - i2cSlaveDetachBus((void *)(i2c_num+1)); + if(!perimanSetPinBus(sda, ESP32_BUS_TYPE_I2C_SLAVE, (void *)(i2c->num+1)) || !perimanSetPinBus(scl, ESP32_BUS_TYPE_I2C_SLAVE, (void *)(i2c->num+1))){ + i2cSlaveDetachBus((void *)(i2c->num+1)); return false; } I2C_SLAVE_MUTEX_UNLOCK(); @@ -377,6 +377,8 @@ esp_err_t i2cSlaveDeinit(uint8_t num){ } #endif I2C_SLAVE_MUTEX_LOCK(); + perimanSetPinBus(i2c->scl, ESP32_BUS_TYPE_INIT, NULL); + perimanSetPinBus(i2c->sda, ESP32_BUS_TYPE_INIT, NULL); i2c_slave_free_resources(i2c); I2C_SLAVE_MUTEX_UNLOCK(); return ESP_OK; diff --git a/cores/esp32/esp32-hal-i2c.c b/cores/esp32/esp32-hal-i2c.c index a4037abe4d3..a3fbadefbea 100644 --- a/cores/esp32/esp32-hal-i2c.c +++ b/cores/esp32/esp32-hal-i2c.c @@ -33,6 +33,8 @@ typedef volatile struct { uint32_t frequency; #if !CONFIG_DISABLE_HAL_LOCKS SemaphoreHandle_t lock; + int8_t scl; + int8_t sda; #endif } i2c_bus_t; @@ -115,6 +117,8 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency){ } else { bus[i2c_num].initialized = true; bus[i2c_num].frequency = frequency; + bus[i2c_num].scl = scl; + bus[i2c_num].sda = sda; //Clock Stretching Timeout: 20b:esp32, 5b:esp32-c3, 24b:esp32-s2 i2c_set_timeout((i2c_port_t)i2c_num, I2C_LL_MAX_TIMEOUT); if(!perimanSetPinBus(sda, ESP32_BUS_TYPE_I2C_MASTER, (void *)(i2c_num+1)) || !perimanSetPinBus(scl, ESP32_BUS_TYPE_I2C_MASTER, (void *)(i2c_num+1))){ @@ -148,6 +152,10 @@ esp_err_t i2cDeinit(uint8_t i2c_num){ err = i2c_driver_delete((i2c_port_t)i2c_num); if(err == ESP_OK){ bus[i2c_num].initialized = false; + perimanSetPinBus(bus[i2c_num].scl, ESP32_BUS_TYPE_INIT, NULL); + perimanSetPinBus(bus[i2c_num].sda, ESP32_BUS_TYPE_INIT, NULL); + bus[i2c_num].scl = -1; + bus[i2c_num].sda = -1; } } #if !CONFIG_DISABLE_HAL_LOCKS From eef101a6abe6a7ce3e2d7c9525a8b04af96253ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Wed, 17 May 2023 15:52:07 +0200 Subject: [PATCH 5/7] Fix slave pins deinit --- cores/esp32/esp32-hal-i2c-slave.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cores/esp32/esp32-hal-i2c-slave.c b/cores/esp32/esp32-hal-i2c-slave.c index 007ea35120f..16e90312616 100644 --- a/cores/esp32/esp32-hal-i2c-slave.c +++ b/cores/esp32/esp32-hal-i2c-slave.c @@ -377,9 +377,11 @@ esp_err_t i2cSlaveDeinit(uint8_t num){ } #endif I2C_SLAVE_MUTEX_LOCK(); - perimanSetPinBus(i2c->scl, ESP32_BUS_TYPE_INIT, NULL); - perimanSetPinBus(i2c->sda, ESP32_BUS_TYPE_INIT, NULL); + int scl = i2c->scl; + int sda = i2c->sda; i2c_slave_free_resources(i2c); + perimanSetPinBus(scl, ESP32_BUS_TYPE_INIT, NULL); + perimanSetPinBus(sda, ESP32_BUS_TYPE_INIT, NULL); I2C_SLAVE_MUTEX_UNLOCK(); return ESP_OK; } From e911566154e10e96d69e2e6da45a9ea3452aa397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Wed, 17 May 2023 15:57:30 +0200 Subject: [PATCH 6/7] remove dbg logs --- cores/esp32/esp32-hal-i2c-slave.c | 2 -- cores/esp32/esp32-hal-i2c.c | 2 -- 2 files changed, 4 deletions(-) diff --git a/cores/esp32/esp32-hal-i2c-slave.c b/cores/esp32/esp32-hal-i2c-slave.c index 16e90312616..436422a6ad4 100644 --- a/cores/esp32/esp32-hal-i2c-slave.c +++ b/cores/esp32/esp32-hal-i2c-slave.c @@ -865,10 +865,8 @@ static bool i2cSlaveDetachBus(void * bus_i2c_num){ uint8_t num = (int)bus_i2c_num - 1; i2c_slave_struct_t * i2c = &_i2c_bus_array[num]; if (i2c->scl == -1 && i2c->sda == -1) { - log_d("i2cSlaveDetachBus: I2C%d not initialized", num); return true; } - log_d("i2cSlaveDetachBus: I2C%d calling i2cSlaveDeinit()", num); esp_err_t err = i2cSlaveDeinit(num); if(err != ESP_OK){ log_e("i2cSlaveDeinit failed with error: %d", err); diff --git a/cores/esp32/esp32-hal-i2c.c b/cores/esp32/esp32-hal-i2c.c index a3fbadefbea..cd1ac0cdadd 100644 --- a/cores/esp32/esp32-hal-i2c.c +++ b/cores/esp32/esp32-hal-i2c.c @@ -43,10 +43,8 @@ static i2c_bus_t bus[SOC_I2C_NUM]; static bool i2cDetachBus(void * bus_i2c_num){ uint8_t i2c_num = (int)bus_i2c_num - 1; if(!bus[i2c_num].initialized){ - log_d("i2cDetachBus I2C%d not initialized", i2c_num); return true; } - log_d("i2cDetachBus I2C%d calling i2cDeinit()", i2c_num); esp_err_t err = i2cDeinit(i2c_num); if(err != ESP_OK){ log_e("i2cDeinit failed with error: %d", err); From f53e6d522123f39971d4ddb1f5fee6b2da1bb39e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Wed, 31 May 2023 14:02:33 +0200 Subject: [PATCH 7/7] set ret to ESP_FAIL instead of returning --- cores/esp32/esp32-hal-i2c-slave.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/esp32-hal-i2c-slave.c b/cores/esp32/esp32-hal-i2c-slave.c index 436422a6ad4..7e64b4d817d 100644 --- a/cores/esp32/esp32-hal-i2c-slave.c +++ b/cores/esp32/esp32-hal-i2c-slave.c @@ -352,7 +352,7 @@ esp_err_t i2cSlaveInit(uint8_t num, int sda, int scl, uint16_t slaveID, uint32_t i2c_ll_update(i2c->dev); if(!perimanSetPinBus(sda, ESP32_BUS_TYPE_I2C_SLAVE, (void *)(i2c->num+1)) || !perimanSetPinBus(scl, ESP32_BUS_TYPE_I2C_SLAVE, (void *)(i2c->num+1))){ i2cSlaveDetachBus((void *)(i2c->num+1)); - return false; + ret = ESP_FAIL; } I2C_SLAVE_MUTEX_UNLOCK(); return ret;