cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0 RTOS create task causing hardfault

Jemegen
Associate III

This is my main program:

 

int main(void)
{
  HAL_Init();

  /* USER CODE BEGIN Init */
  TaskHandle_t task1_handler;
  TaskHandle_t task2_handler;

  BaseType_t status;
  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  MX_GPIO_Init();
  MX_RTC_Init();
  //MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  status = xTaskCreate(task1_handler,"Task1",150,"Hello world From task1",2,&task1_handler);
  configASSERT( status == pdPASS);
  status = xTaskCreate(task2_handler,"Task2",150,"Hello world From task2",2,&task2_handler);
  configASSERT( status == pdPASS);

  vTaskStartScheduler();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  }
  /* USER CODE END 3 */
}

 

 

And this is my tasks' code:

 

static void task1_handler(void * parameters)
{

  while(1)
  {
    GPIOC->BSRR |= (uint32_t)1<<13;
     //vTaskDelay(pdMS_TO_TICKS(100));
    taskYIELD();
  }

}

static void task2_handler(void * parameters)
{
  while(1)
  {
    GPIOC->BSRR = (uint32_t)1<<29;
    //vTaskDelay(pdMS_TO_TICKS(100));
    taskYIELD();

  }
}

 

 

It seems that xTaskCreate() is causing hardfault. Do you have any idea?

1 ACCEPTED SOLUTION

Accepted Solutions

It seemed to be a heap size issue, when I lowered the heap size for the two task allocations hardfault resolved. Why would two 150 heap cause a stack with a total size of 1000 to overflow? Anyway thanks for the suggestions guys!

View solution in original post

6 REPLIES 6
Jemegen
Associate III

And here is my FreeRTOSConfig.h:

#define configUSE_PREEMPTION			1
#define configUSE_IDLE_HOOK				0
#define configUSE_TICK_HOOK				0
#define configCPU_CLOCK_HZ				( SystemCoreClock )
#define configTICK_RATE_HZ				( ( TickType_t ) 500 )
#define configMAX_PRIORITIES			( 5 )
#define configMINIMAL_STACK_SIZE		( ( unsigned short ) 60 )
#define configTOTAL_HEAP_SIZE			( ( size_t ) ( 1000 ) )
#define configMAX_TASK_NAME_LEN			( 5 )
#define configUSE_TRACE_FACILITY		1
#define configUSE_16_BIT_TICKS			0
#define configIDLE_SHOULD_YIELD			1
#define configUSE_MUTEXES				1
#define configQUEUE_REGISTRY_SIZE		8
#define configCHECK_FOR_STACK_OVERFLOW	0
#define configUSE_RECURSIVE_MUTEXES		1
#define configUSE_MALLOC_FAILED_HOOK	0
#define configUSE_APPLICATION_TASK_TAG	0
#define configUSE_COUNTING_SEMAPHORES	1
#define configGENERATE_RUN_TIME_STATS	0

/* 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		5
#define configTIMER_TASK_STACK_DEPTH	( 80 )

/* 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	1
#define INCLUDE_vTaskSuspend			1
#define INCLUDE_vTaskDelayUntil			1
#define INCLUDE_vTaskDelay				1

Debug it like any other Hard Fault, look at the code that's faulting and the registers. 

Clear the local/auto variables

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
SofLit
ST Employee

Hello,

From the code snippet you shared l can see code generated by CubeMx for peripherals init but I can’t see cmsis os for RTOS APIs calls. Did you integrate freertos lib manually to your project?

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

YES. I'm actually learning FreeRTOS based on an Udemy course. I'm sorry to ask such a *** question here, my debugger tells me that the CPU has been inside vTaskStartScheduler(), but I'm not sure where it's stuck because all that seems to be executing after stepping into the debug is some systick related code, so I can't pinpoint the the exact problem.

I don't recommend to add manually FreeRTOS as you are a beginner. I suggest you to enable FreeRTOS in CubeMx.

See this video on Youtube.

 

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

It seemed to be a heap size issue, when I lowered the heap size for the two task allocations hardfault resolved. Why would two 150 heap cause a stack with a total size of 1000 to overflow? Anyway thanks for the suggestions guys!