cancel
Showing results for 
Search instead for 
Did you mean: 

Hard fault when implementing RTOS in driver with Interrupts

VSøby.1
Associate III

Hi.

Let me preface by saying, that this is the first time I try using RTOS on any device.

I am using a custom board with a STM32f410R8T6 MCU, where it retrieves some sensorvalues through SPI, and transmits data to another MCU, also through SPI.

Currently I have setup up the driver with bare metal programming, using some nested interrupts, to properly prioritize the flow of the driver. I have been strongly advised to use multithreading instead, as this should make my life alot easier. I've tried implementing it, following a simple guide: https://www.youtube.com/watch?v=OPrcpbKNSjU.

However, I get a hard fault when I hit the function osKernelStart(); in the main loop. When I use 'Step into' when debugging, I see that it happens, when vTaskScheduler is called from cmsis_os2.c (l.262).

I use STM32CubeIDE, and setup the RTOS as mentioned in the guide (by pretty much just enabling it and doing nothing more).

I have attached pictures of the settings below aswell:

0693W000006Exj7QAC.pngThen I go to sys, select my form of debugging (5 pin SWD) and chose another timebase source (TIM9).

0693W000006Exk5QAC.pngWhen I generate code, the IDE shows errormessages regarding my current interrupts, as these have preemption levels under 5. They should be within 5-15 due to the RTOS, but these can't be changed from the GUI.

0693W000006ExlmQAC.pngSo I navigate my way into the automatically generated code, and set the priorities myself.

  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI4_IRQn, 5, 0);
  HAL_NVIC_EnableIRQ(EXTI4_IRQn);
 
  HAL_NVIC_SetPriority(EXTI9_5_IRQn, 5, 1);
  HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);

I get my automatically generated default task function with a for-loop, where I try to turn on an LED, but I never get there, as a hard fault occurs 2 seconds after I start debugging.

Default task can be seen here:

void StartDefaultTask(void *argument)
{
  /* USER CODE BEGIN 5 */
  /* Infinite loop */
  for(;;)
  {
	HAL_GPIO_WritePin(LED_3_GPIO_Port, LED_3_Pin, GPIO_PIN_SET);
    osDelay(1);
  }
  /* USER CODE END 5 */
}

When i start debugging, and send pulses which gives the program an external interrupt, I am able to do so, and have the MCU toggle another LED for 2 seconds, before the hardfault occurs. This is because the interrupt occurs multiple times before the osKernelStart(); function is reached, and the hard fault occurs.

I want to use something like theTaskNotificationFromISR() function, to initialize my different tasks from interrupts, but I'm at a loss with this hard fault.

I am using an ST-Link v2 and running STM32CubeIDE at v1.5.0

Best regards Victor

3 REPLIES 3
Ons KOOLI
Senior III

Hi VSøby.1,

Can you please share your .ioc file ?

Best Regards,

Ons.

VSøby.1
Associate III

Hi @Ons KOOLI​

In the meantime I tried making a IOC file, where I copied all my settings, but added the Free RTOS first. Then I inserted all my old code, and I am now able to run different tasks and my interrupt service routines aswell (all with preemption priority 5). I think it got messed up because I added the RTOS to an existing project, where the interrupts were configured with a preemption priority below 5.. Weird I couldn't change it..

Ons KOOLI
Senior III

Hi VSøby.1,

I don't think so. In STM32CubeMX/STM32CubeIDE you can change priorities even if you add FreeRTOS to an existing project.

I think it is caused by another issue.

Else, if your problem is well solved please mark this question as answered.

Best Regards,

Ons.