From 26b71a3f14088180311a7a70b3a6c9c1d0e04dce Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Thu, 1 Aug 2019 09:48:29 +0200 Subject: [PATCH 1/4] cpu/esp32: removes additional spaces in freertos --- cpu/esp32/include/freertos/portmacro.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpu/esp32/include/freertos/portmacro.h b/cpu/esp32/include/freertos/portmacro.h index 9e94a2ae9b..00c9b75b17 100644 --- a/cpu/esp32/include/freertos/portmacro.h +++ b/cpu/esp32/include/freertos/portmacro.h @@ -39,9 +39,9 @@ extern "C" { #define portEXIT_CRITICAL_ISR(mux) vTaskExitCritical(mux) #define taskENTER_CRITICAL(mux) portENTER_CRITICAL(mux) -#define taskENTER_CRITICAL_ISR(mux) portENTER_CRITICAL_ISR(mux) +#define taskENTER_CRITICAL_ISR(mux) portENTER_CRITICAL_ISR(mux) #define taskEXIT_CRITICAL(mux) portEXIT_CRITICAL(mux) -#define taskEXIT_CRITICAL_ISR(mux) portEXIT_CRITICAL_ISR(mux) +#define taskEXIT_CRITICAL_ISR(mux) portEXIT_CRITICAL_ISR(mux) #define portYIELD_FROM_ISR thread_yield_higher #define portNUM_PROCESSORS 2 From 1b041083ab8f3908ea1d68240e9fb8a896b61236 Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Thu, 1 Aug 2019 09:49:24 +0200 Subject: [PATCH 2/4] cpu/esp32: change of critical section handling in freertos Using a mutex for critical section handling with portENTER_CRITICAL and portEXIT_CRITICAL does not work for RIOT, as this function can also be called in the interrupt context. Therefore, the given mutex is not used. Instead, the basic default FreeRTOS mechanism for critical sections is used by simply disabling interrupts. Since context switches for the ESP32 are also based on interrupts, there is no possibility that another thread will enter the critical section once the interrupts are disabled. --- cpu/esp32/freertos/task.c | 17 ++++++++++++----- cpu/esp32/include/freertos/portmacro.h | 7 +++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/cpu/esp32/freertos/task.c b/cpu/esp32/freertos/task.c index e5d4a1bac9..11f754aee6 100644 --- a/cpu/esp32/freertos/task.c +++ b/cpu/esp32/freertos/task.c @@ -10,7 +10,7 @@ #ifndef DOXYGEN -#define ENABLE_DEBUG 0 +#define ENABLE_DEBUG (0) #include "debug.h" #include @@ -139,8 +139,14 @@ void vTaskEnterCritical( portMUX_TYPE *mux ) /* disable interrupts */ uint32_t state = irq_disable(); - /* aquire the mutex with interrupts disabled */ - mutex_lock(mux); /* TODO should be only a spin lock */ + /* Locking the given mutex does not work here, as this function can also + be called in the interrupt context. Therefore, the given mutex is not + used. Instead, the basic default FreeRTOS mechanism for critical + sections is used by simply disabling interrupts. Since context + switches for the ESP32 are also based on interrupts, there is no + possibility that another thread will enter the critical section + once the interrupts are disabled. */ + /* mutex_lock(mux); */ /* TODO should be only a spin lock */ /* increment nesting counter and save old interrupt level */ threads_arch_exts[my_pid].critical_nesting++; @@ -157,8 +163,9 @@ void vTaskExitCritical( portMUX_TYPE *mux ) DEBUG("%s pid=%d prio=%d mux=%p\n", __func__, my_pid, sched_threads[my_pid]->priority, mux); - /* release the mutex with interrupts disabled */ - mutex_unlock(mux); /* TODO should be only a spin lock */ + /* The given mutex is not used (see vTaskEnterCritical) and has not to + be unlocked here. */ + /* mutex_unlock(mux); */ /* TODO should be only a spin lock */ /* decrement nesting counter and restore old interrupt level */ if (threads_arch_exts[my_pid].critical_nesting) { diff --git a/cpu/esp32/include/freertos/portmacro.h b/cpu/esp32/include/freertos/portmacro.h index 00c9b75b17..f4f6f4b70d 100644 --- a/cpu/esp32/include/freertos/portmacro.h +++ b/cpu/esp32/include/freertos/portmacro.h @@ -30,8 +30,8 @@ extern "C" { #define portMUX_TYPE mutex_t #define portMUX_INITIALIZER_UNLOCKED MUTEX_INIT -#define portENTER_CRITICAL(pm) mutex_lock(pm) -#define portEXIT_CRITICAL(pm) mutex_unlock(pm) +#define portENTER_CRITICAL(mux) vTaskEnterCritical(mux) +#define portEXIT_CRITICAL(mux) vTaskExitCritical(mux) #define portENTER_CRITICAL_NESTED irq_disable #define portEXIT_CRITICAL_NESTED irq_restore @@ -48,6 +48,9 @@ extern "C" { #define xPortGetCoreID() PRO_CPU_NUM +extern void vTaskEnterCritical(portMUX_TYPE *mux); +extern void vTaskExitCritical(portMUX_TYPE *mux); + #ifdef __cplusplus } #endif From d2de4858e219a38e3bfa54db1690858186662c27 Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Mon, 5 Aug 2019 14:18:22 +0200 Subject: [PATCH 3/4] cpu/esp32: compile riot_freertos only if required --- cpu/esp32/Makefile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cpu/esp32/Makefile b/cpu/esp32/Makefile index 1294cc435c..84cd1400e6 100644 --- a/cpu/esp32/Makefile +++ b/cpu/esp32/Makefile @@ -4,7 +4,6 @@ MODULE = cpu # Add a list of subdirectories, that should also be built: DIRS += $(RIOTCPU)/esp_common DIRS += periph -DIRS += freertos DIRS += vendor ifneq (, $(filter esp_can, $(USEMODULE))) @@ -19,4 +18,8 @@ ifneq (, $(filter esp_wifi, $(USEMODULE))) DIRS += esp-wifi endif +ifneq (, $(filter riot_freertos, $(USEMODULE))) + DIRS += freertos +endif + include $(RIOTBASE)/Makefile.base From 8c5de9b714280b4cd2ee6bd373fdd8b72c7c8787 Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Mon, 5 Aug 2019 14:55:00 +0200 Subject: [PATCH 4/4] cpu/esp32: module riot_freertos is always required --- cpu/esp32/Makefile.include | 1 + 1 file changed, 1 insertion(+) diff --git a/cpu/esp32/Makefile.include b/cpu/esp32/Makefile.include index 850a724022..375702eae8 100644 --- a/cpu/esp32/Makefile.include +++ b/cpu/esp32/Makefile.include @@ -75,6 +75,7 @@ USEMODULE += periph_hwrng USEMODULE += periph_flash USEMODULE += periph_rtc USEMODULE += periph_uart +USEMODULE += riot_freertos USEMODULE += random USEMODULE += stdio_uart USEMODULE += xtensa