Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
added support for enabling I2C HAL locks even when system wide HAL lo…
…cks are disabled
  • Loading branch information
AlmirKadric committed Oct 23, 2025
commit efdf2bf611907ed2acf79e4334602ab549b78fb5
6 changes: 6 additions & 0 deletions Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ config DISABLE_HAL_LOCKS
or interrupt at the same time. Option is best used with Arduino enabled
and code implemented only in setup/loop and Arduino callbacks

config FORCE_I2C_HAL_LOCKS
bool "Enable mutex locks for I2C HAL even if HAL locks are disbled"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to depend on hal locks being off

default "n"
help
Enabling this option will run I2C hardware abstraction with locks.

menu "Debug Log Configuration"
choice ARDUHAL_LOG_DEFAULT_LEVEL
bool "Default log level"
Expand Down
32 changes: 17 additions & 15 deletions cores/esp32/esp32-hal-i2c-ng.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

#include "esp32-hal-i2c.h"

#define I2C_HAL_LOCKS_ENABLED (!CONFIG_DISABLE_HAL_LOCKS || CONFIG_FORCE_I2C_HAL_LOCKS)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be defined in esp32-hal-i2c.h and propagate here and all relevant files (esp32-hal-i2c.c, Wire.h and Wire.cpp)


#if SOC_I2C_SUPPORTED
#include "esp_idf_version.h"
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 4, 0)
#include "esp32-hal.h"
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
Expand All @@ -32,7 +34,7 @@
typedef volatile struct {
bool initialized;
uint32_t frequency;
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
SemaphoreHandle_t lock;
#endif
int8_t scl;
Expand Down Expand Up @@ -75,7 +77,7 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency) {
if (i2c_num >= SOC_I2C_NUM) {
return ESP_ERR_INVALID_ARG;
}
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
if (bus[i2c_num].lock == NULL) {
bus[i2c_num].lock = xSemaphoreCreateMutex();
if (bus[i2c_num].lock == NULL) {
Expand Down Expand Up @@ -147,7 +149,7 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency) {
}
if (!perimanSetPinBus(sda, ESP32_BUS_TYPE_I2C_MASTER_SDA, (void *)(i2c_num + 1), i2c_num, -1)
|| !perimanSetPinBus(scl, ESP32_BUS_TYPE_I2C_MASTER_SCL, (void *)(i2c_num + 1), i2c_num, -1)) {
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//release lock so that i2cDetachBus can execute i2cDeinit
xSemaphoreGive(bus[i2c_num].lock);
#endif
Expand All @@ -157,7 +159,7 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency) {
}

init_fail:
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//release lock
xSemaphoreGive(bus[i2c_num].lock);
#endif
Expand All @@ -169,7 +171,7 @@ esp_err_t i2cDeinit(uint8_t i2c_num) {
if (i2c_num >= SOC_I2C_NUM) {
return ESP_ERR_INVALID_ARG;
}
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//acquire lock
if (bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE) {
log_e("could not acquire lock");
Expand Down Expand Up @@ -201,7 +203,7 @@ esp_err_t i2cDeinit(uint8_t i2c_num) {
bus[i2c_num].bus_handle = NULL;
}
}
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//release lock
xSemaphoreGive(bus[i2c_num].lock);
#endif
Expand Down Expand Up @@ -241,7 +243,7 @@ esp_err_t i2cWrite(uint8_t i2c_num, uint16_t address, const uint8_t *buff, size_
log_e("Only 7bit I2C addresses are supported");
return ESP_ERR_INVALID_ARG;
}
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//acquire lock
if (bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE) {
log_e("could not acquire lock");
Expand Down Expand Up @@ -282,7 +284,7 @@ esp_err_t i2cWrite(uint8_t i2c_num, uint16_t address, const uint8_t *buff, size_
}

end:
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//release lock
xSemaphoreGive(bus[i2c_num].lock);
#endif
Expand All @@ -295,7 +297,7 @@ esp_err_t i2cRead(uint8_t i2c_num, uint16_t address, uint8_t *buff, size_t size,
if (i2c_num >= SOC_I2C_NUM) {
return ESP_ERR_INVALID_ARG;
}
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//acquire lock
if (bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE) {
log_e("could not acquire lock");
Expand Down Expand Up @@ -328,7 +330,7 @@ esp_err_t i2cRead(uint8_t i2c_num, uint16_t address, uint8_t *buff, size_t size,
*readCount = size;

end:
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//release lock
xSemaphoreGive(bus[i2c_num].lock);
#endif
Expand All @@ -343,7 +345,7 @@ esp_err_t i2cWriteReadNonStop(
if (i2c_num >= SOC_I2C_NUM) {
return ESP_ERR_INVALID_ARG;
}
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//acquire lock
if (bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE) {
log_e("could not acquire lock");
Expand Down Expand Up @@ -376,7 +378,7 @@ esp_err_t i2cWriteReadNonStop(
*readCount = rsize;

end:
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//release lock
xSemaphoreGive(bus[i2c_num].lock);
#endif
Expand All @@ -388,7 +390,7 @@ esp_err_t i2cSetClock(uint8_t i2c_num, uint32_t frequency) {
if (i2c_num >= SOC_I2C_NUM) {
return ESP_ERR_INVALID_ARG;
}
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//acquire lock
if (bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE) {
log_e("could not acquire lock");
Expand Down Expand Up @@ -429,7 +431,7 @@ esp_err_t i2cSetClock(uint8_t i2c_num, uint32_t frequency) {
}

end:
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//release lock
xSemaphoreGive(bus[i2c_num].lock);
#endif
Expand Down
16 changes: 9 additions & 7 deletions cores/esp32/esp32-hal-i2c-slave.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "soc/soc_caps.h"

#define I2C_HAL_LOCKS_ENABLED (!CONFIG_DISABLE_HAL_LOCKS || CONFIG_FORCE_I2C_HAL_LOCKS)

#if SOC_I2C_SUPPORT_SLAVE
#include <stdint.h>
#include <stdbool.h>
Expand Down Expand Up @@ -107,7 +109,7 @@ typedef struct i2c_slave_struct_t {
#endif
QueueHandle_t tx_queue;
uint32_t rx_data_count;
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
SemaphoreHandle_t lock;
#endif
} i2c_slave_struct_t;
Expand All @@ -123,22 +125,22 @@ typedef union {

static i2c_slave_struct_t _i2c_bus_array[SOC_HP_I2C_NUM] = {
{&I2C0, 0, -1, -1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
,
NULL
#endif
},
#if SOC_HP_I2C_NUM > 1
{&I2C1, 1, -1, -1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
,
NULL
#endif
}
#endif
};

#if CONFIG_DISABLE_HAL_LOCKS
#if !I2C_HAL_LOCKS_ENABLED
#define I2C_SLAVE_MUTEX_LOCK()
#define I2C_SLAVE_MUTEX_UNLOCK()
#else
Expand Down Expand Up @@ -274,7 +276,7 @@ esp_err_t i2cSlaveInit(uint8_t num, int sda, int scl, uint16_t slaveID, uint32_t
i2c_slave_struct_t *i2c = &_i2c_bus_array[num];
esp_err_t ret = ESP_OK;

#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
if (!i2c->lock) {
i2c->lock = xSemaphoreCreateMutex();
if (i2c->lock == NULL) {
Expand Down Expand Up @@ -427,7 +429,7 @@ esp_err_t i2cSlaveDeinit(uint8_t num) {
}

i2c_slave_struct_t *i2c = &_i2c_bus_array[num];
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
if (!i2c->lock) {
log_e("Lock is not initialized! Did you call i2c_slave_init()?");
return ESP_ERR_NO_MEM;
Expand All @@ -450,7 +452,7 @@ size_t i2cSlaveWrite(uint8_t num, const uint8_t *buf, uint32_t len, uint32_t tim
}
uint32_t to_queue = 0, to_fifo = 0;
i2c_slave_struct_t *i2c = &_i2c_bus_array[num];
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
if (!i2c->lock) {
log_e("Lock is not initialized! Did you call i2c_slave_init()?");
return ESP_ERR_NO_MEM;
Expand Down
32 changes: 17 additions & 15 deletions cores/esp32/esp32-hal-i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

#include "esp32-hal-i2c.h"

#define I2C_HAL_LOCKS_ENABLED (!CONFIG_DISABLE_HAL_LOCKS || CONFIG_FORCE_I2C_HAL_LOCKS)

#if SOC_I2C_SUPPORTED
#include "esp_idf_version.h"
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 0)
#include "esp32-hal.h"
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
Expand Down Expand Up @@ -55,7 +57,7 @@
typedef volatile struct {
bool initialized;
uint32_t frequency;
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
SemaphoreHandle_t lock;
#endif
int8_t scl;
Expand Down Expand Up @@ -90,7 +92,7 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency) {
if (i2c_num >= SOC_I2C_NUM) {
return ESP_ERR_INVALID_ARG;
}
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
if (bus[i2c_num].lock == NULL) {
bus[i2c_num].lock = xSemaphoreCreateMutex();
if (bus[i2c_num].lock == NULL) {
Expand Down Expand Up @@ -151,7 +153,7 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency) {
i2c_set_timeout((i2c_port_t)i2c_num, I2C_LL_MAX_TIMEOUT);
if (!perimanSetPinBus(sda, ESP32_BUS_TYPE_I2C_MASTER_SDA, (void *)(i2c_num + 1), i2c_num, -1)
|| !perimanSetPinBus(scl, ESP32_BUS_TYPE_I2C_MASTER_SCL, (void *)(i2c_num + 1), i2c_num, -1)) {
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//release lock so that i2cDetachBus can execute i2cDeinit
xSemaphoreGive(bus[i2c_num].lock);
#endif
Expand All @@ -161,7 +163,7 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency) {
}
}
init_fail:
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//release lock
xSemaphoreGive(bus[i2c_num].lock);
#endif
Expand All @@ -173,7 +175,7 @@ esp_err_t i2cDeinit(uint8_t i2c_num) {
if (i2c_num >= SOC_I2C_NUM) {
return ESP_ERR_INVALID_ARG;
}
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//acquire lock
if (bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE) {
log_e("could not acquire lock");
Expand All @@ -192,7 +194,7 @@ esp_err_t i2cDeinit(uint8_t i2c_num) {
bus[i2c_num].sda = -1;
}
}
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//release lock
xSemaphoreGive(bus[i2c_num].lock);
#endif
Expand All @@ -205,7 +207,7 @@ esp_err_t i2cWrite(uint8_t i2c_num, uint16_t address, const uint8_t *buff, size_
if (i2c_num >= SOC_I2C_NUM) {
return ESP_ERR_INVALID_ARG;
}
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//acquire lock
if (bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE) {
log_e("could not acquire lock");
Expand Down Expand Up @@ -247,7 +249,7 @@ esp_err_t i2cWrite(uint8_t i2c_num, uint16_t address, const uint8_t *buff, size_
if (cmd != NULL) {
i2c_cmd_link_delete_static(cmd);
}
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//release lock
xSemaphoreGive(bus[i2c_num].lock);
#endif
Expand All @@ -259,7 +261,7 @@ esp_err_t i2cRead(uint8_t i2c_num, uint16_t address, uint8_t *buff, size_t size,
if (i2c_num >= SOC_I2C_NUM) {
return ESP_ERR_INVALID_ARG;
}
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//acquire lock
if (bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE) {
log_e("could not acquire lock");
Expand All @@ -276,7 +278,7 @@ esp_err_t i2cRead(uint8_t i2c_num, uint16_t address, uint8_t *buff, size_t size,
*readCount = 0;
}
}
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//release lock
xSemaphoreGive(bus[i2c_num].lock);
#endif
Expand All @@ -290,7 +292,7 @@ esp_err_t i2cWriteReadNonStop(
if (i2c_num >= SOC_I2C_NUM) {
return ESP_ERR_INVALID_ARG;
}
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//acquire lock
if (bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE) {
log_e("could not acquire lock");
Expand All @@ -307,7 +309,7 @@ esp_err_t i2cWriteReadNonStop(
*readCount = 0;
}
}
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//release lock
xSemaphoreGive(bus[i2c_num].lock);
#endif
Expand All @@ -319,7 +321,7 @@ esp_err_t i2cSetClock(uint8_t i2c_num, uint32_t frequency) {
if (i2c_num >= SOC_I2C_NUM) {
return ESP_ERR_INVALID_ARG;
}
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//acquire lock
if (bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE) {
log_e("could not acquire lock");
Expand Down Expand Up @@ -412,7 +414,7 @@ esp_err_t i2cSetClock(uint8_t i2c_num, uint32_t frequency) {
}

end:
#if !CONFIG_DISABLE_HAL_LOCKS
#if I2C_HAL_LOCKS_ENABLED
//release lock
xSemaphoreGive(bus[i2c_num].lock);
#endif
Expand Down
Loading
Loading