Dynamically allocated binary semaphore fails randomly on a task restart

Hi I am running four tasks on an STM32H7 they are:

monitorTask - runs with a vTaskDelay 0f 250 ms Prio. = osPriorityNormal2
posRecordTask - runs with a vTaskDelay 0f 2 ms Prio. = osPriorityBelowNormal
wiFiStartupTask - runs once then deletes itself - Prio. = osPriorityNormal1
wiFiTask - runs continuously - Prio = osPriorityNormal

All the tasks are static, the semaphores are dynamic.

All tasks have large stacks at 2048 words.

I have the ICACHE and DCACHE enabled, but the semaphore handle is in the DTCM memory which isn’t cached.

It is the interaction between the startWiFiTask and wiFiTask that causes the issue.

At startup the startWiFiTask runs first to set up a WiFiModem using “AT” commands over the SPI bus. On completion of the startup this task starts the wiFiTask then deletes itself.

The wiFiTask looks after the data transport of the modem. If the connection is lost during the listening process the wiFiTask restarts the startWiFiTask and then deletes itself. This cycle will repeat successfully up until a point where upon entry to the startWiFiTask and on executing
the xSemaphoreTake(wiFiReadySemHandle, (uint32_t)0U); statement the config assert of the semaphore handle fails and the program stops in the freeRTOSConfigAssert module; vAssertCalled.

I have stack checking 2 enabled and upon entry to the vAssert all stacks are OK, no overflow.

I changed the semaphore wiFiReadySemHandle to a static implementation and then the system runs without error.

I have checked all of my interrupts to ensure they are => configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5

I checked I wasn’t calling non ISR FreeRTOS functions from within interrupt routine. That was OK.

The functions that initialize and restart the wiFiStartup task are:

#define STACK_SIZE_START_WIFI_TASK 2048

StackType_t 	__attribute__((section (".DTCM_MISC"))) startWiFiTaskStack[STACK_SIZE_START_WIFI_TASK];
StaticTask_t 	__attribute__((section (".DTCM_MISC"))) startWiFiTskBuffer;

#define STACK_SIZE_WIFI_TASK 2048

StackType_t 	__attribute__((section (".DTCM_MISC"))) wiFiTaskStack[STACK_SIZE_WIFI_TASK];
StaticTask_t 	__attribute__((section (".DTCM_MISC"))) wiFiTskBuffer;


TaskHandle_t 			__attribute__((section (".DTCM_MISC"))) startWiFiTskHandle = NULL;
TaskHandle_t 			__attribute__((section (".DTCM_MISC"))) wiFiTskHandle = NULL;

SemaphoreHandle_t	__attribute__((section (".DTCM_MISC"))) wiFiReadySemHandle = {0};

/*
 **************************************************************************
 * Start the WiFi Initialisation task and Network Time Task if defined 1
 *
 **************************************************************************
 */
WIFI_StatusTypeDef startWiFiSystem(wifiParsTypeDef* wiFiPars)
{
  wiFiReadySemHandle = xSemaphoreCreateBinary();
  startWiFiTskHandle = xTaskCreateStatic(wiFiStartupTask,
                                           "wifiStartup",
                                         STACK_SIZE_START_WIFI_TASK,
                                         wiFiPars,
                                         osPriorityNormal1,
                                         startWiFiTaskStack,
                                         &startWiFiTskBuffer);

  if(startWiFiTskHandle == NULL)
  {
    return WIFI_STARTUP_TASK_FAILED;
  }
  return WIFI_OK;
}

/**************************************************************************
*    CREATE AND START WIFI TASK
***************************************************************************
*/
WIFI_StatusTypeDef startwiFiTask(wifiParsTypeDef* wiFiPars)
{
  wiFiTskHandle = xTaskCreateStatic( wiFiTask,
                                     "wifiTask",
                                     STACK_SIZE_WIFI_TASK,
                                     wiFiPars,
                                     osPriorityNormal,
                                     wiFiTaskStack,
                                     &wiFiTskBuffer);
  if(wiFiTskHandle == NULL)
  {
    return WIFI_XPORT_TASK_FAILED;
  }
  return WIFI_OK;
}

/**************************************************************************
*    RESTART WIFI
***************************************************************************
*/

void restartWiFiTask(void)
{
  startWiFiSystem(&wiFiPars);
  vTaskDelete(wiFiTskHandle);
}

The failure of the asset occurs at the first semaphore take in the startWiFiTask:

/*
******************************************************************************
* File Name    : wiFiStartupTask.c
* Created On   : 23 Dec. 2021
* Description  :
* Author	     : R J Garnett - Admin
******************************************************************************
*/
#pragma GCC optimize ("Ofast") /* rjg todo */
//#pragma GCC optimize ("O0") /* rjg todo */

/*
 ***************************************************************************
 * Start Up Task
 *
 ***************************************************************************
 * This does the following:
 * - Hardware resets the WiFi module via WIFI_RST_Pin => PI4
 * - Connects the modem as a Station (STA) to the designated SSID
...
 */
void wiFiStartupTask(void *argument)
{
	WIFI_START_LBL:
	vTaskDelay(20); /* Allow wiFiTask to complete and close */
	noWiFiStarts++;
	printfdma("%05d WiFi Startup Start\r\n", noWiFiStarts);

	wifiParsTypeDef *wf;
	wf = argument;

	HAL_GPIO_WritePin(USER_LED2_GPIO_Port, USER_LED2_Pin, GPIO_PIN_SET);

  __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_5);

  wiFiErrorState = WIFI_OK;
  wiFiRdyState = HAL_GPIO_ReadPin(WIFI_DATRDY_GPIO_Port, (uint16_t)WIFI_DATRDY_Pin);

  /* Clear semaphores to prevent nastiness */
	xSemaphoreTake(wiFiReadySemHandle, (uint32_t)0U); /* Assert raised here */

	/* Main task loop */
	while(1)
	{
		/************ 1 Send a reset to the Wi Fi module ***********/
		wiFiModuleReset();

		if (xSemaphoreTake(wiFiReadySemHandle, (uint32_t)WI_FI_RESET_TIMEOUT) == pdFAIL)
		{ ...
      /* Long list of AT commands sent to the modem */
                  ...
    #endif
    /* Delete the startup task */
    printfdma("%05d WiFi Startup End\r\n", testCounterWiFiTask);
    vTaskDelete(startWiFiTskHandle);
    wiFiErrorState = WIFI_OK;
  }
}

My FreeRTOS Config is:

...
#ifndef CMSIS_device_header
#define CMSIS_device_header "stm32h7xx.h"
#endif /* CMSIS_device_header */

#define configENABLE_FPU                         0
#define configENABLE_MPU                         0

#define configUSE_PREEMPTION                     1
#define configSUPPORT_STATIC_ALLOCATION          1
#define configSUPPORT_DYNAMIC_ALLOCATION         1
#define configUSE_IDLE_HOOK                      0
#define configUSE_TICK_HOOK                      0
#define configCPU_CLOCK_HZ                       ( SystemCoreClock )
#define configTICK_RATE_HZ                       ((TickType_t)1000)
#define configMAX_PRIORITIES                     ( 56 )
#define configMINIMAL_STACK_SIZE                 ((uint16_t)128)
#define configTOTAL_HEAP_SIZE                    ((size_t)15360)
#define configMAX_TASK_NAME_LEN                  ( 16 )
#define configUSE_TRACE_FACILITY                 1
#define configUSE_16_BIT_TICKS                   0
#define configUSE_MUTEXES                        1
#define configQUEUE_REGISTRY_SIZE                8
#define configUSE_RECURSIVE_MUTEXES              1
#define configUSE_COUNTING_SEMAPHORES            1
#define configUSE_PORT_OPTIMISED_TASK_SELECTION  0
#define configCHECK_FOR_STACK_OVERFLOW 					 1
#define configUSE_TASK_NOTIFICATIONS						 1
/* USER CODE BEGIN MESSAGE_BUFFER_LENGTH_TYPE */
/* Defaults to size_t for backward compatibility, but can be changed
   if lengths will always be less than the number of bytes in a size_t. */
#define configMESSAGE_BUFFER_LENGTH_TYPE         size_t
/* USER CODE END MESSAGE_BUFFER_LENGTH_TYPE */

/* Co-routine definitions. */
#define configUSE_CO_ROUTINES                    0
#define configMAX_CO_ROUTINE_PRIORITIES          ( 2 )

/* Software timer definitions. */
#define configUSE_TIMERS                         1
#define configTIMER_TASK_PRIORITY                ( 2 )
#define configTIMER_QUEUE_LENGTH                 10
#define configTIMER_TASK_STACK_DEPTH             256

/* The following flag must be enabled only when using newlib */
#define configUSE_NEWLIB_REENTRANT           1

/* CMSIS-RTOS V2 flags */
#define configUSE_OS2_THREAD_SUSPEND_RESUME  1
#define configUSE_OS2_THREAD_ENUMERATE       1
#define configUSE_OS2_EVENTFLAGS_FROM_ISR    1
#define configUSE_OS2_THREAD_FLAGS           1
#define configUSE_OS2_TIMER                  1
#define configUSE_OS2_MUTEX                  1

/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */
#define INCLUDE_vTaskPrioritySet             1
#define INCLUDE_uxTaskPriorityGet            1
#define INCLUDE_vTaskDelete                  1
#define INCLUDE_vTaskCleanUpResources        0
#define INCLUDE_vTaskSuspend                   
#define INCLUDE_vTaskDelayUntil                
#define INCLUDE_vTaskDelay                     
#define INCLUDE_xTaskGetSchedulerState         
#define INCLUDE_xTimerPendFunctionCall         
#define INCLUDE_xQueueGetMutexHolder           
#define INCLUDE_uxTaskGetStackHighWaterMark    
#define INCLUDE_xTaskGetCurrentTaskHandle      
#define INCLUDE_eTaskGetState                  

/*
 * The CMSIS-RTOS V2 FreeRTOS wrapper is dependent on the heap implementation used
 * by the application thus the correct define need to be enabled below
 */
#define USE_FreeRTOS_HEAP_4

/* Cortex-M specific definitions. */
#ifdef __NVIC_PRIO_BITS
 /* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */
 #define configPRIO_BITS         __NVIC_PRIO_BITS
#else
 #define configPRIO_BITS         4
#endif

/* The lowest interrupt priority that can be used in a call to a "set priority"
function. */
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY     5

#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5

#define configKERNEL_INTERRUPT_PRIORITY 		( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
/* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
#define configMAX_SYSCALL_INTERRUPT_PRIORITY 	( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )

/* Normal assert() semantics without relying on the provision of an assert.h
header file. */
/* USER CODE BEGIN    */
  extern void vAssertCalled( const char *pcFile, uint32_t ulLine );
  #define configASSERT( x )  if( ( x ) == 0 ) vAssertCalled( __FILE__, __LINE__ )
/* USER CODE END    */

/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
standard names. */
#define vPortSVCHandler    SVC_Handler
#define xPortPendSVHandler PendSV_Handler

/* IMPORTANT: After   0.3.   update, Systick_Handler comes from NVIC (if SYS timebase = systick), otherwise from cmsis_os2.c */

#define USE_CUSTOM_SYSTICK_HANDLER_IMPLEMENTATION 0

#endif /* FREERTOS_CONFIG_H */

I am at a loss as to what is going wrong. It would seem the semaphore handle is getting set to zero, but I don’t know how to determine the cause of this.

Has anyone had this problem before or has anyone any ideas on where to look for a cause. Setting the semaphore as a static to work around the problem is a satisfactory solution as it never gets deleted, but I would like to know what I am doing wrong that causes the dynamically assigned semaphore to fail.

Regards
Rob

Where are you deleting the semaphore before re-creating it. I might have missed it but I didn’t see it in your code. If its not getting deleted there will be a memory leak that could result in running out of heap.

Thanks for that Richard.

I don’t delete the semaphore - silly me, I forgot that I create it every time I restart, but I don’t delete it when I delete the task. The static one works of course, because it doesn’t use the heap. I am assuming it is the heap that is leaking?

I will create the sem separate from the task as the sem is persistant across both tasks and I will keep the sem static, because it is persistent as the network modem will be running all the time in the production system.

What a knucklehead I am! It’s hard to get good users of FreeRTOS!

Would this be a reasonable approach?

WIFI_StatusTypeDef startWiFiSystem(wifiParsTypeDef* wiFiPars)
{
	if(wiFiReadySemHandle == NULL)
	{
		wiFiReadySemHandle = xSemaphoreCreateBinaryStatic(&wiFiReadySemBuff);
	}

  startWiFiTskHandle = xTaskCreateStatic(wiFiStartupTask,
                                           "wifiStartup",
                                         STACK_SIZE_START_WIFI_TASK,
                                         wiFiPars,

One suggestion, rather than have the task delete itself, only to be recreated when you need to reconnect, just have the task block on a notification, and then loop back to its start. Then send the notification instead of re-creating the task.

Unless you are really short on Ram, and have a task that is also deleted and recreated mutually exclusive with this task, you aren’t really gaining that much deleting the task. And if you DO have that sort of situation, making one task that does both functions might simplify things (maybe a root task that calls one of two task mode functions for each operation).

Yes I like that idea. I’ve always thought that deletion - recreation of a static task was inefficient. I will do as you suggest. I’ve got plenty of ram; ~1.2 MByte on chip and 15 MByte external.

There is another lesson for me in this -Heap checking!

Regards
Rob