Skip to main content
POdom1
Visitor II
September 25, 2019
Question

TIM1 pwm and TIM17 as systick source causes initialization bug

  • September 25, 2019
  • 0 replies
  • 457 views

I configured a simple CubeMX project targeting the nucleo-f302r8, which I am using as a development board.

My project uses TIM1 CH1N (PB13) as a PWM output and TIM17 as the systick source. I chose TIM17 arbitrarily as I don't anticipate using it in the final project. FREERTOS/CMSIS-V1 middleware is enabled as well.

TIM1 and TIM17 share an interrupt vector. The auto-generated IRQHandler looks like this:

/**
 * @brief This function handles TIM1 trigger, commutation and TIM17 interrupts.
 */
void TIM1_TRG_COM_TIM17_IRQHandler(void)
{
 /* USER CODE BEGIN TIM1_TRG_COM_TIM17_IRQn 0 */
 
 /* USER CODE END TIM1_TRG_COM_TIM17_IRQn 0 */
 HAL_TIM_IRQHandler(&htim1);
 HAL_TIM_IRQHandler(&htim17);
 /* USER CODE BEGIN TIM1_TRG_COM_TIM17_IRQn 1 */
 
 /* USER CODE END TIM1_TRG_COM_TIM17_IRQn 1 */
}

The initialization sequence configures TIM17 as the systick source before configuring TIM1. This causes the interrupt handler to fire before htim1 is initialized. HAL_TIM_IRQHandler does not properly handle the case that htim1.Instance is uninitialized.

Pre-configuring htim1.Instance in main.c works around this issue:

int main(void)
{
 /* USER CODE BEGIN 1 */
 //workaround for initialization bug when using TIM1 with TIM17 as systick timebase
 htim1.Instance = TIM1;
 /* USER CODE END 1 */
 ...

This topic has been closed for replies.