cancel
Showing results for 
Search instead for 
Did you mean: 

Looking for some simple timer sample code (mine seems to cause a Hard Fault)

Jim Seymour
Senior

I'm using STM32CubeMX to develop an app for the STM32F030. I've done this many times and thought I understood the various issues - but I'm having a hell of a time getting a basic timer (TIM6) to run (interrupt-driven).

The generated init code is:

  htim6.Instance = TIM6;
  htim6.Init.Prescaler = 0;
  htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim6.Init.Period = 816;
  htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
  {
    Error_Handler();
  }

Then I start it with this: (The GPIO is for debugging - see below).

HAL_GPIO_WritePin(TP5_GPIO_Port, TP5_Pin, GPIO_PIN_SET);    //!!!
HAL_GPIO_WritePin(TP5_GPIO_Port, TP5_Pin, GPIO_PIN_RESET);  //!!!
 
   // Start the system timer
   HAL_TIM_Base_Start_IT(&htim6);
 
HAL_GPIO_WritePin(TP5_GPIO_Port, TP5_Pin, GPIO_PIN_SET);    //!!!
HAL_GPIO_WritePin(TP5_GPIO_Port, TP5_Pin, GPIO_PIN_RESET);  //!!!

Finally, my interrupt handler simply asserts a GPIO test point and increments a global variable:

/**
  * @brief This function handles TIM6 global interrupt.
  */
void TIM6_IRQHandler(void)
{
  /* USER CODE BEGIN TIM6_IRQn 0 */
  HAL_GPIO_WritePin(TP5_GPIO_Port, TP5_Pin, GPIO_PIN_SET);
  swTimerTick++;
  /* USER CODE END TIM6_IRQn 0 */
  HAL_TIM_IRQHandler(&htim6);
  /* USER CODE BEGIN TIM6_IRQn 1 */
  HAL_GPIO_WritePin(TP5_GPIO_Port, TP5_Pin, GPIO_PIN_RESET);
 
  /* USER CODE END TIM6_IRQn 1 */
}

When I run this, it goes into the ditch. On the oscilloscope, I see only the first test point pulse (before the call to HAL_TIM_Base_Start_IT(). If I run it in the debugger, I can step through that _Start call seemingly without issues, but when I tell it to run it hangs. If I stop the debugger, it's looping in the HardFault_Handler.

I feel like I must be missing something obvious.... Any thoughts?

1 ACCEPTED SOLUTION

Accepted Solutions

You should go through the standard interrupt troubleshooting, part of which is check​ing whether address of the ISR is properly inserted into the vector table.

My guess is that it is not. Some 'F030 e.g. 'F030x6 officially do not have TIM6.

https://github.com/STMicroelectronics/STM32CubeF0/blob/master/Drivers/CMSIS/Device/ST/STM32F0xx/Source/Templates/gcc/startup_stm32f030x6.s

Use a different timer.

JW​

View solution in original post

6 REPLIES 6
KnarfB
Principal III

What does the fault analyzer say if you hit the hard fault in the debugger?

Have you tried to find the error by bisection, i.e. repeatedly setting breakpoints at intermediate code points?

Some init code is missing, like enabling the clock. Is HAL_TIM_Base_MspInit properly called?

hth

KnarfB

You should go through the standard interrupt troubleshooting, part of which is check​ing whether address of the ISR is properly inserted into the vector table.

My guess is that it is not. Some 'F030 e.g. 'F030x6 officially do not have TIM6.

https://github.com/STMicroelectronics/STM32CubeF0/blob/master/Drivers/CMSIS/Device/ST/STM32F0xx/Source/Templates/gcc/startup_stm32f030x6.s

Use a different timer.

JW​

Interesting... I wonder if this is a bug in CubeMX, then. Because it certainly didn't complain when I enabled that timer...

OK and is it this case at all? Which STM32F030 exactly do you use?

The 'F030 are in fact 3 different chips with slightly different set of peripherals, depending on the package. TIM6 is available only in STM32F030x8/C.

JW

Got it. Yes, I'm using the STM32F030C8T6.

Jim Seymour
Senior

I migrated my project to use SMT32CubeIDE and it seems to be working now.

Before, I was using STM32CubeMX along with Keil uVision.

I can't explain it, but I'm going to chalk it up to some obscure bug and move on.

Thanks for all the responses!