From 925fbb587e3be78a8db88859d8219afc4a47ea9e Mon Sep 17 00:00:00 2001 From: Jeroen Domburg Date: Mon, 22 Aug 2016 17:36:32 +0800 Subject: [PATCH 1/3] Add static initializers for muxes, add mutex init to vPortCPUAcquireMutex --- components/freertos/event_groups.c | 2 +- components/freertos/heap_regions.c | 2 +- components/freertos/include/freertos/portmacro.h | 7 +++++++ components/freertos/port.c | 3 ++- components/freertos/queue.c | 2 ++ components/freertos/tasks.c | 4 ++-- components/freertos/timers.c | 2 +- 7 files changed, 16 insertions(+), 6 deletions(-) diff --git a/components/freertos/event_groups.c b/components/freertos/event_groups.c index 1aa95e084..0eafab10a 100644 --- a/components/freertos/event_groups.c +++ b/components/freertos/event_groups.c @@ -123,7 +123,7 @@ typedef struct xEventGroupDefinition /* Again: one mux for all events. Maybe this can be made more granular. ToDo: look into that. -JD */ -static portMUX_TYPE xEventGroupMux; +static portMUX_TYPE xEventGroupMux = portMUX_INITIALIZER_UNLOCKED; static BaseType_t xMuxInitialized = pdFALSE; diff --git a/components/freertos/heap_regions.c b/components/freertos/heap_regions.c index d1ab6240b..491a1fe6e 100644 --- a/components/freertos/heap_regions.c +++ b/components/freertos/heap_regions.c @@ -155,7 +155,7 @@ typedef struct A_BLOCK_LINK } BlockLink_t; //Mux to protect the memory status data -static portMUX_TYPE xMallocMutex; +static portMUX_TYPE xMallocMutex = portMUX_INITIALIZER_UNLOCKED; /*-----------------------------------------------------------*/ diff --git a/components/freertos/include/freertos/portmacro.h b/components/freertos/include/freertos/portmacro.h index b659a8271..c698cb8cb 100644 --- a/components/freertos/include/freertos/portmacro.h +++ b/components/freertos/include/freertos/portmacro.h @@ -147,6 +147,13 @@ typedef struct { #define portMUX_VAL_MASK 0x000000FF #define portMUX_VAL_SHIFT 0 +//Keep this in sync with the portMUX_TYPE struct definition +#ifdef portMUX_DEBUG +#define portMUX_INITIALIZER_UNLOCKED { portMUX_MAGIC_VAL|portMUX_FREE_VAL } +#else +#define portMUX_INITIALIZER_UNLOCKED { portMUX_MAGIC_VAL|portMUX_FREE_VAL, "(never locked)", -1 } +#endif + /* Critical section management. NW-TODO: replace XTOS_SET_INTLEVEL with more efficient version, if any? */ // These cannot be nested. They should be used with a lot of care and cannot be called from interrupt level. #define portDISABLE_INTERRUPTS() do { XTOS_SET_INTLEVEL(XCHAL_EXCM_LEVEL); portbenchmarkINTERRUPT_DISABLE(); } while (0) diff --git a/components/freertos/port.c b/components/freertos/port.c index fd7760e01..0be29c190 100644 --- a/components/freertos/port.c +++ b/components/freertos/port.c @@ -297,7 +297,8 @@ void vPortCPUAcquireMutex(portMUX_TYPE *mux) { #ifdef portMUX_DEBUG uint32_t cnt=(1<<16); if ( (mux->mux & portMUX_MAGIC_MASK) != portMUX_MAGIC_VAL ) { - ets_printf("ERROR: vPortCPUAcquireMutex: mux %p is uninitialized (0x%X)!\n", mux, mux->mux); + ets_printf("ERROR: vPortCPUAcquireMutex: mux %p is uninitialized (0x%X)! Called from %s line %d.\n", mux, mux->mux, fnName, line); + asm("break.n 1"); mux->mux=portMUX_FREE_VAL; } #endif diff --git a/components/freertos/queue.c b/components/freertos/queue.c index 337352fca..92e182a62 100644 --- a/components/freertos/queue.c +++ b/components/freertos/queue.c @@ -460,6 +460,8 @@ int8_t *pcAllocatedBuffer; vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) ); vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) ); + vPortCPUInitializeMutex(&pxNewQueue->mux); + traceCREATE_MUTEX( pxNewQueue ); /* Start with the semaphore in the expected state. */ diff --git a/components/freertos/tasks.c b/components/freertos/tasks.c index d747708eb..f0e4fb120 100644 --- a/components/freertos/tasks.c +++ b/components/freertos/tasks.c @@ -273,8 +273,8 @@ PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended[ portNUM_PROCES PRIVILEGED_DATA static portBASE_TYPE xMutexesInitialised = pdFALSE; /* For now, we use just one mux for all the critical sections. ToDo: give evrything a bit more granularity; that could improve performance by not needlessly spinning in spinlocks for unrelated resources. */ -PRIVILEGED_DATA static portMUX_TYPE xTaskQueueMutex; -PRIVILEGED_DATA static portMUX_TYPE xTickCountMutex; +PRIVILEGED_DATA static portMUX_TYPE xTaskQueueMutex = portMUX_INITIALIZER_UNLOCKED; +PRIVILEGED_DATA static portMUX_TYPE xTickCountMutex = portMUX_INITIALIZER_UNLOCKED; #if ( configGENERATE_RUN_TIME_STATS == 1 ) diff --git a/components/freertos/timers.c b/components/freertos/timers.c index 7c0ba5bd9..92e4bf94a 100644 --- a/components/freertos/timers.c +++ b/components/freertos/timers.c @@ -170,7 +170,7 @@ PRIVILEGED_DATA static List_t *pxOverflowTimerList; PRIVILEGED_DATA static QueueHandle_t xTimerQueue = NULL; /* Mux. We use a single mux for all the timers for now. ToDo: maybe increase granularity here? */ -PRIVILEGED_DATA portMUX_TYPE xTimerMux; +PRIVILEGED_DATA portMUX_TYPE xTimerMux = portMUX_INITIALIZER_UNLOCKED; #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 ) From 609f75a8c0a2feb12e5cef750db75c23e189d482 Mon Sep 17 00:00:00 2001 From: Jeroen Domburg Date: Mon, 22 Aug 2016 17:41:55 +0800 Subject: [PATCH 2/3] Oops, left in a debugging break.n. Removed. --- components/freertos/port.c | 1 - 1 file changed, 1 deletion(-) diff --git a/components/freertos/port.c b/components/freertos/port.c index 0be29c190..8f0a617af 100644 --- a/components/freertos/port.c +++ b/components/freertos/port.c @@ -298,7 +298,6 @@ void vPortCPUAcquireMutex(portMUX_TYPE *mux) { uint32_t cnt=(1<<16); if ( (mux->mux & portMUX_MAGIC_MASK) != portMUX_MAGIC_VAL ) { ets_printf("ERROR: vPortCPUAcquireMutex: mux %p is uninitialized (0x%X)! Called from %s line %d.\n", mux, mux->mux, fnName, line); - asm("break.n 1"); mux->mux=portMUX_FREE_VAL; } #endif From f34110009ac552b4632a276c1cb1754f4013b282 Mon Sep 17 00:00:00 2001 From: Jeroen Domburg Date: Mon, 22 Aug 2016 17:45:46 +0800 Subject: [PATCH 3/3] Add C99-style named field in initialisation struct --- components/freertos/include/freertos/portmacro.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/components/freertos/include/freertos/portmacro.h b/components/freertos/include/freertos/portmacro.h index c698cb8cb..bb574a1aa 100644 --- a/components/freertos/include/freertos/portmacro.h +++ b/components/freertos/include/freertos/portmacro.h @@ -147,11 +147,17 @@ typedef struct { #define portMUX_VAL_MASK 0x000000FF #define portMUX_VAL_SHIFT 0 -//Keep this in sync with the portMUX_TYPE struct definition +//Keep this in sync with the portMUX_TYPE struct definition please. #ifdef portMUX_DEBUG -#define portMUX_INITIALIZER_UNLOCKED { portMUX_MAGIC_VAL|portMUX_FREE_VAL } +#define portMUX_INITIALIZER_UNLOCKED { \ + .mux = portMUX_MAGIC_VAL|portMUX_FREE_VAL \ + } #else -#define portMUX_INITIALIZER_UNLOCKED { portMUX_MAGIC_VAL|portMUX_FREE_VAL, "(never locked)", -1 } +#define portMUX_INITIALIZER_UNLOCKED { \ + .mux = portMUX_MAGIC_VAL|portMUX_FREE_VAL, \ + .lastLockedFn = "(never locked)", \ + .lastLockedLine = -1 \ + } #endif /* Critical section management. NW-TODO: replace XTOS_SET_INTLEVEL with more efficient version, if any? */