Skip to content

Commit efdf2bf

Browse files
committed
added support for enabling I2C HAL locks even when system wide HAL locks are disabled
1 parent ee98cf3 commit efdf2bf

File tree

6 files changed

+76
-60
lines changed

6 files changed

+76
-60
lines changed

Kconfig.projbuild

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ config DISABLE_HAL_LOCKS
167167
or interrupt at the same time. Option is best used with Arduino enabled
168168
and code implemented only in setup/loop and Arduino callbacks
169169

170+
config FORCE_I2C_HAL_LOCKS
171+
bool "Enable mutex locks for I2C HAL even if HAL locks are disbled"
172+
default "n"
173+
help
174+
Enabling this option will run I2C hardware abstraction with locks.
175+
170176
menu "Debug Log Configuration"
171177
choice ARDUHAL_LOG_DEFAULT_LEVEL
172178
bool "Default log level"

cores/esp32/esp32-hal-i2c-ng.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414

1515
#include "esp32-hal-i2c.h"
1616

17+
#define I2C_HAL_LOCKS_ENABLED (!CONFIG_DISABLE_HAL_LOCKS || CONFIG_FORCE_I2C_HAL_LOCKS)
18+
1719
#if SOC_I2C_SUPPORTED
1820
#include "esp_idf_version.h"
1921
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 4, 0)
2022
#include "esp32-hal.h"
21-
#if !CONFIG_DISABLE_HAL_LOCKS
23+
#if I2C_HAL_LOCKS_ENABLED
2224
#include "freertos/FreeRTOS.h"
2325
#include "freertos/task.h"
2426
#include "freertos/semphr.h"
@@ -32,7 +34,7 @@
3234
typedef volatile struct {
3335
bool initialized;
3436
uint32_t frequency;
35-
#if !CONFIG_DISABLE_HAL_LOCKS
37+
#if I2C_HAL_LOCKS_ENABLED
3638
SemaphoreHandle_t lock;
3739
#endif
3840
int8_t scl;
@@ -75,7 +77,7 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency) {
7577
if (i2c_num >= SOC_I2C_NUM) {
7678
return ESP_ERR_INVALID_ARG;
7779
}
78-
#if !CONFIG_DISABLE_HAL_LOCKS
80+
#if I2C_HAL_LOCKS_ENABLED
7981
if (bus[i2c_num].lock == NULL) {
8082
bus[i2c_num].lock = xSemaphoreCreateMutex();
8183
if (bus[i2c_num].lock == NULL) {
@@ -147,7 +149,7 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency) {
147149
}
148150
if (!perimanSetPinBus(sda, ESP32_BUS_TYPE_I2C_MASTER_SDA, (void *)(i2c_num + 1), i2c_num, -1)
149151
|| !perimanSetPinBus(scl, ESP32_BUS_TYPE_I2C_MASTER_SCL, (void *)(i2c_num + 1), i2c_num, -1)) {
150-
#if !CONFIG_DISABLE_HAL_LOCKS
152+
#if I2C_HAL_LOCKS_ENABLED
151153
//release lock so that i2cDetachBus can execute i2cDeinit
152154
xSemaphoreGive(bus[i2c_num].lock);
153155
#endif
@@ -157,7 +159,7 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency) {
157159
}
158160

159161
init_fail:
160-
#if !CONFIG_DISABLE_HAL_LOCKS
162+
#if I2C_HAL_LOCKS_ENABLED
161163
//release lock
162164
xSemaphoreGive(bus[i2c_num].lock);
163165
#endif
@@ -169,7 +171,7 @@ esp_err_t i2cDeinit(uint8_t i2c_num) {
169171
if (i2c_num >= SOC_I2C_NUM) {
170172
return ESP_ERR_INVALID_ARG;
171173
}
172-
#if !CONFIG_DISABLE_HAL_LOCKS
174+
#if I2C_HAL_LOCKS_ENABLED
173175
//acquire lock
174176
if (bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE) {
175177
log_e("could not acquire lock");
@@ -201,7 +203,7 @@ esp_err_t i2cDeinit(uint8_t i2c_num) {
201203
bus[i2c_num].bus_handle = NULL;
202204
}
203205
}
204-
#if !CONFIG_DISABLE_HAL_LOCKS
206+
#if I2C_HAL_LOCKS_ENABLED
205207
//release lock
206208
xSemaphoreGive(bus[i2c_num].lock);
207209
#endif
@@ -241,7 +243,7 @@ esp_err_t i2cWrite(uint8_t i2c_num, uint16_t address, const uint8_t *buff, size_
241243
log_e("Only 7bit I2C addresses are supported");
242244
return ESP_ERR_INVALID_ARG;
243245
}
244-
#if !CONFIG_DISABLE_HAL_LOCKS
246+
#if I2C_HAL_LOCKS_ENABLED
245247
//acquire lock
246248
if (bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE) {
247249
log_e("could not acquire lock");
@@ -282,7 +284,7 @@ esp_err_t i2cWrite(uint8_t i2c_num, uint16_t address, const uint8_t *buff, size_
282284
}
283285

284286
end:
285-
#if !CONFIG_DISABLE_HAL_LOCKS
287+
#if I2C_HAL_LOCKS_ENABLED
286288
//release lock
287289
xSemaphoreGive(bus[i2c_num].lock);
288290
#endif
@@ -295,7 +297,7 @@ esp_err_t i2cRead(uint8_t i2c_num, uint16_t address, uint8_t *buff, size_t size,
295297
if (i2c_num >= SOC_I2C_NUM) {
296298
return ESP_ERR_INVALID_ARG;
297299
}
298-
#if !CONFIG_DISABLE_HAL_LOCKS
300+
#if I2C_HAL_LOCKS_ENABLED
299301
//acquire lock
300302
if (bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE) {
301303
log_e("could not acquire lock");
@@ -328,7 +330,7 @@ esp_err_t i2cRead(uint8_t i2c_num, uint16_t address, uint8_t *buff, size_t size,
328330
*readCount = size;
329331

330332
end:
331-
#if !CONFIG_DISABLE_HAL_LOCKS
333+
#if I2C_HAL_LOCKS_ENABLED
332334
//release lock
333335
xSemaphoreGive(bus[i2c_num].lock);
334336
#endif
@@ -343,7 +345,7 @@ esp_err_t i2cWriteReadNonStop(
343345
if (i2c_num >= SOC_I2C_NUM) {
344346
return ESP_ERR_INVALID_ARG;
345347
}
346-
#if !CONFIG_DISABLE_HAL_LOCKS
348+
#if I2C_HAL_LOCKS_ENABLED
347349
//acquire lock
348350
if (bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE) {
349351
log_e("could not acquire lock");
@@ -376,7 +378,7 @@ esp_err_t i2cWriteReadNonStop(
376378
*readCount = rsize;
377379

378380
end:
379-
#if !CONFIG_DISABLE_HAL_LOCKS
381+
#if I2C_HAL_LOCKS_ENABLED
380382
//release lock
381383
xSemaphoreGive(bus[i2c_num].lock);
382384
#endif
@@ -388,7 +390,7 @@ esp_err_t i2cSetClock(uint8_t i2c_num, uint32_t frequency) {
388390
if (i2c_num >= SOC_I2C_NUM) {
389391
return ESP_ERR_INVALID_ARG;
390392
}
391-
#if !CONFIG_DISABLE_HAL_LOCKS
393+
#if I2C_HAL_LOCKS_ENABLED
392394
//acquire lock
393395
if (bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE) {
394396
log_e("could not acquire lock");
@@ -429,7 +431,7 @@ esp_err_t i2cSetClock(uint8_t i2c_num, uint32_t frequency) {
429431
}
430432

431433
end:
432-
#if !CONFIG_DISABLE_HAL_LOCKS
434+
#if I2C_HAL_LOCKS_ENABLED
433435
//release lock
434436
xSemaphoreGive(bus[i2c_num].lock);
435437
#endif

cores/esp32/esp32-hal-i2c-slave.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "soc/soc_caps.h"
1616

17+
#define I2C_HAL_LOCKS_ENABLED (!CONFIG_DISABLE_HAL_LOCKS || CONFIG_FORCE_I2C_HAL_LOCKS)
18+
1719
#if SOC_I2C_SUPPORT_SLAVE
1820
#include <stdint.h>
1921
#include <stdbool.h>
@@ -107,7 +109,7 @@ typedef struct i2c_slave_struct_t {
107109
#endif
108110
QueueHandle_t tx_queue;
109111
uint32_t rx_data_count;
110-
#if !CONFIG_DISABLE_HAL_LOCKS
112+
#if I2C_HAL_LOCKS_ENABLED
111113
SemaphoreHandle_t lock;
112114
#endif
113115
} i2c_slave_struct_t;
@@ -123,22 +125,22 @@ typedef union {
123125

124126
static i2c_slave_struct_t _i2c_bus_array[SOC_HP_I2C_NUM] = {
125127
{&I2C0, 0, -1, -1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
126-
#if !CONFIG_DISABLE_HAL_LOCKS
128+
#if I2C_HAL_LOCKS_ENABLED
127129
,
128130
NULL
129131
#endif
130132
},
131133
#if SOC_HP_I2C_NUM > 1
132134
{&I2C1, 1, -1, -1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
133-
#if !CONFIG_DISABLE_HAL_LOCKS
135+
#if I2C_HAL_LOCKS_ENABLED
134136
,
135137
NULL
136138
#endif
137139
}
138140
#endif
139141
};
140142

141-
#if CONFIG_DISABLE_HAL_LOCKS
143+
#if !I2C_HAL_LOCKS_ENABLED
142144
#define I2C_SLAVE_MUTEX_LOCK()
143145
#define I2C_SLAVE_MUTEX_UNLOCK()
144146
#else
@@ -274,7 +276,7 @@ esp_err_t i2cSlaveInit(uint8_t num, int sda, int scl, uint16_t slaveID, uint32_t
274276
i2c_slave_struct_t *i2c = &_i2c_bus_array[num];
275277
esp_err_t ret = ESP_OK;
276278

277-
#if !CONFIG_DISABLE_HAL_LOCKS
279+
#if I2C_HAL_LOCKS_ENABLED
278280
if (!i2c->lock) {
279281
i2c->lock = xSemaphoreCreateMutex();
280282
if (i2c->lock == NULL) {
@@ -427,7 +429,7 @@ esp_err_t i2cSlaveDeinit(uint8_t num) {
427429
}
428430

429431
i2c_slave_struct_t *i2c = &_i2c_bus_array[num];
430-
#if !CONFIG_DISABLE_HAL_LOCKS
432+
#if I2C_HAL_LOCKS_ENABLED
431433
if (!i2c->lock) {
432434
log_e("Lock is not initialized! Did you call i2c_slave_init()?");
433435
return ESP_ERR_NO_MEM;
@@ -450,7 +452,7 @@ size_t i2cSlaveWrite(uint8_t num, const uint8_t *buf, uint32_t len, uint32_t tim
450452
}
451453
uint32_t to_queue = 0, to_fifo = 0;
452454
i2c_slave_struct_t *i2c = &_i2c_bus_array[num];
453-
#if !CONFIG_DISABLE_HAL_LOCKS
455+
#if I2C_HAL_LOCKS_ENABLED
454456
if (!i2c->lock) {
455457
log_e("Lock is not initialized! Did you call i2c_slave_init()?");
456458
return ESP_ERR_NO_MEM;

cores/esp32/esp32-hal-i2c.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414

1515
#include "esp32-hal-i2c.h"
1616

17+
#define I2C_HAL_LOCKS_ENABLED (!CONFIG_DISABLE_HAL_LOCKS || CONFIG_FORCE_I2C_HAL_LOCKS)
18+
1719
#if SOC_I2C_SUPPORTED
1820
#include "esp_idf_version.h"
1921
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 0)
2022
#include "esp32-hal.h"
21-
#if !CONFIG_DISABLE_HAL_LOCKS
23+
#if I2C_HAL_LOCKS_ENABLED
2224
#include "freertos/FreeRTOS.h"
2325
#include "freertos/task.h"
2426
#include "freertos/semphr.h"
@@ -55,7 +57,7 @@
5557
typedef volatile struct {
5658
bool initialized;
5759
uint32_t frequency;
58-
#if !CONFIG_DISABLE_HAL_LOCKS
60+
#if I2C_HAL_LOCKS_ENABLED
5961
SemaphoreHandle_t lock;
6062
#endif
6163
int8_t scl;
@@ -90,7 +92,7 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency) {
9092
if (i2c_num >= SOC_I2C_NUM) {
9193
return ESP_ERR_INVALID_ARG;
9294
}
93-
#if !CONFIG_DISABLE_HAL_LOCKS
95+
#if I2C_HAL_LOCKS_ENABLED
9496
if (bus[i2c_num].lock == NULL) {
9597
bus[i2c_num].lock = xSemaphoreCreateMutex();
9698
if (bus[i2c_num].lock == NULL) {
@@ -151,7 +153,7 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency) {
151153
i2c_set_timeout((i2c_port_t)i2c_num, I2C_LL_MAX_TIMEOUT);
152154
if (!perimanSetPinBus(sda, ESP32_BUS_TYPE_I2C_MASTER_SDA, (void *)(i2c_num + 1), i2c_num, -1)
153155
|| !perimanSetPinBus(scl, ESP32_BUS_TYPE_I2C_MASTER_SCL, (void *)(i2c_num + 1), i2c_num, -1)) {
154-
#if !CONFIG_DISABLE_HAL_LOCKS
156+
#if I2C_HAL_LOCKS_ENABLED
155157
//release lock so that i2cDetachBus can execute i2cDeinit
156158
xSemaphoreGive(bus[i2c_num].lock);
157159
#endif
@@ -161,7 +163,7 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency) {
161163
}
162164
}
163165
init_fail:
164-
#if !CONFIG_DISABLE_HAL_LOCKS
166+
#if I2C_HAL_LOCKS_ENABLED
165167
//release lock
166168
xSemaphoreGive(bus[i2c_num].lock);
167169
#endif
@@ -173,7 +175,7 @@ esp_err_t i2cDeinit(uint8_t i2c_num) {
173175
if (i2c_num >= SOC_I2C_NUM) {
174176
return ESP_ERR_INVALID_ARG;
175177
}
176-
#if !CONFIG_DISABLE_HAL_LOCKS
178+
#if I2C_HAL_LOCKS_ENABLED
177179
//acquire lock
178180
if (bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE) {
179181
log_e("could not acquire lock");
@@ -192,7 +194,7 @@ esp_err_t i2cDeinit(uint8_t i2c_num) {
192194
bus[i2c_num].sda = -1;
193195
}
194196
}
195-
#if !CONFIG_DISABLE_HAL_LOCKS
197+
#if I2C_HAL_LOCKS_ENABLED
196198
//release lock
197199
xSemaphoreGive(bus[i2c_num].lock);
198200
#endif
@@ -205,7 +207,7 @@ esp_err_t i2cWrite(uint8_t i2c_num, uint16_t address, const uint8_t *buff, size_
205207
if (i2c_num >= SOC_I2C_NUM) {
206208
return ESP_ERR_INVALID_ARG;
207209
}
208-
#if !CONFIG_DISABLE_HAL_LOCKS
210+
#if I2C_HAL_LOCKS_ENABLED
209211
//acquire lock
210212
if (bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE) {
211213
log_e("could not acquire lock");
@@ -247,7 +249,7 @@ esp_err_t i2cWrite(uint8_t i2c_num, uint16_t address, const uint8_t *buff, size_
247249
if (cmd != NULL) {
248250
i2c_cmd_link_delete_static(cmd);
249251
}
250-
#if !CONFIG_DISABLE_HAL_LOCKS
252+
#if I2C_HAL_LOCKS_ENABLED
251253
//release lock
252254
xSemaphoreGive(bus[i2c_num].lock);
253255
#endif
@@ -259,7 +261,7 @@ esp_err_t i2cRead(uint8_t i2c_num, uint16_t address, uint8_t *buff, size_t size,
259261
if (i2c_num >= SOC_I2C_NUM) {
260262
return ESP_ERR_INVALID_ARG;
261263
}
262-
#if !CONFIG_DISABLE_HAL_LOCKS
264+
#if I2C_HAL_LOCKS_ENABLED
263265
//acquire lock
264266
if (bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE) {
265267
log_e("could not acquire lock");
@@ -276,7 +278,7 @@ esp_err_t i2cRead(uint8_t i2c_num, uint16_t address, uint8_t *buff, size_t size,
276278
*readCount = 0;
277279
}
278280
}
279-
#if !CONFIG_DISABLE_HAL_LOCKS
281+
#if I2C_HAL_LOCKS_ENABLED
280282
//release lock
281283
xSemaphoreGive(bus[i2c_num].lock);
282284
#endif
@@ -290,7 +292,7 @@ esp_err_t i2cWriteReadNonStop(
290292
if (i2c_num >= SOC_I2C_NUM) {
291293
return ESP_ERR_INVALID_ARG;
292294
}
293-
#if !CONFIG_DISABLE_HAL_LOCKS
295+
#if I2C_HAL_LOCKS_ENABLED
294296
//acquire lock
295297
if (bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE) {
296298
log_e("could not acquire lock");
@@ -307,7 +309,7 @@ esp_err_t i2cWriteReadNonStop(
307309
*readCount = 0;
308310
}
309311
}
310-
#if !CONFIG_DISABLE_HAL_LOCKS
312+
#if I2C_HAL_LOCKS_ENABLED
311313
//release lock
312314
xSemaphoreGive(bus[i2c_num].lock);
313315
#endif
@@ -319,7 +321,7 @@ esp_err_t i2cSetClock(uint8_t i2c_num, uint32_t frequency) {
319321
if (i2c_num >= SOC_I2C_NUM) {
320322
return ESP_ERR_INVALID_ARG;
321323
}
322-
#if !CONFIG_DISABLE_HAL_LOCKS
324+
#if I2C_HAL_LOCKS_ENABLED
323325
//acquire lock
324326
if (bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE) {
325327
log_e("could not acquire lock");
@@ -412,7 +414,7 @@ esp_err_t i2cSetClock(uint8_t i2c_num, uint32_t frequency) {
412414
}
413415

414416
end:
415-
#if !CONFIG_DISABLE_HAL_LOCKS
417+
#if I2C_HAL_LOCKS_ENABLED
416418
//release lock
417419
xSemaphoreGive(bus[i2c_num].lock);
418420
#endif

0 commit comments

Comments
 (0)