diff --git a/components/esp32/freertos_hooks.c b/components/esp32/freertos_hooks.c index 599bc19620..ed88be8835 100644 --- a/components/esp32/freertos_hooks.c +++ b/components/esp32/freertos_hooks.c @@ -24,6 +24,7 @@ //an idle or tick hook. #define MAX_HOOKS 8 +static portMUX_TYPE hooks_spinlock = portMUX_INITIALIZER_UNLOCKED; static esp_freertos_idle_cb_t idle_cb[portNUM_PROCESSORS][MAX_HOOKS]={0}; static esp_freertos_tick_cb_t tick_cb[portNUM_PROCESSORS][MAX_HOOKS]={0}; @@ -61,12 +62,15 @@ esp_err_t esp_register_freertos_idle_hook_for_cpu(esp_freertos_idle_cb_t new_idl if(cpuid >= portNUM_PROCESSORS){ return ESP_ERR_INVALID_ARG; } + portENTER_CRITICAL(&hooks_spinlock); for(int n = 0; n < MAX_HOOKS; n++){ if (idle_cb[cpuid][n]==NULL) { idle_cb[cpuid][n]=new_idle_cb; + portEXIT_CRITICAL(&hooks_spinlock); return ESP_OK; } } + portEXIT_CRITICAL(&hooks_spinlock); return ESP_ERR_NO_MEM; } @@ -80,12 +84,15 @@ esp_err_t esp_register_freertos_tick_hook_for_cpu(esp_freertos_tick_cb_t new_tic if(cpuid >= portNUM_PROCESSORS){ return ESP_ERR_INVALID_ARG; } + portENTER_CRITICAL(&hooks_spinlock); for(int n = 0; n < MAX_HOOKS; n++){ if (tick_cb[cpuid][n]==NULL) { tick_cb[cpuid][n]=new_tick_cb; + portEXIT_CRITICAL(&hooks_spinlock); return ESP_OK; } } + portEXIT_CRITICAL(&hooks_spinlock); return ESP_ERR_NO_MEM; } @@ -96,19 +103,23 @@ esp_err_t esp_register_freertos_tick_hook(esp_freertos_tick_cb_t new_tick_cb) void esp_deregister_freertos_idle_hook(esp_freertos_idle_cb_t old_idle_cb) { + portENTER_CRITICAL(&hooks_spinlock); for(int m = 0; m < portNUM_PROCESSORS; m++) { for(int n = 0; n < MAX_HOOKS; n++){ if(idle_cb[m][n] == old_idle_cb) idle_cb[m][n] = NULL; } } + portEXIT_CRITICAL(&hooks_spinlock); } void esp_deregister_freertos_tick_hook(esp_freertos_tick_cb_t old_tick_cb) { + portENTER_CRITICAL(&hooks_spinlock); for(int m = 0; m < portNUM_PROCESSORS; m++){ for(int n = 0; n < MAX_HOOKS; n++){ if(tick_cb[m][n] == old_tick_cb) tick_cb[m][n] = NULL; } } + portEXIT_CRITICAL(&hooks_spinlock); }