cancel
Showing results for 
Search instead for 
Did you mean: 

Timer Interrupt not working on STM32F103C8

Abdullah Baig
Associate II
Posted on April 04, 2018 at 14:51

I am using CubeMX and SW4STM32 to generate timer update interrupt on STM32F103C8. The core is running at 64 MHz, and I have verified that other functions like GPIO, PWM, ADC work perfect.

The target is to generate an interrupt after every second, so timer's prescaler is 8000 and period is 8000. When I call the function HAL_TIM_Base_Init_IT(), it hangs there and doesn't perform anything after. Any code written after this doesn't get executed. Precisely, the macro __HAL_TIM_ENABLE_IT(htim, TIM_IT_UPDATE); is where it hangs.

I have been pulling my hair for the past 3 days but can't get over it.

Please help me find the problem.

The complete code I am using is below.

&sharpinclude 'stm32f1xx_hal.h'

TIM_HandleTypeDef htim2;

TIM_HandleTypeDef htim4;

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_TIM4_Init(void);

int main(void)

{

  HAL_Init();

  /* Configure the system clock */

  SystemClock_Config();

  HAL_MspInit();

  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  MX_TIM4_Init();

  /* USER CODE BEGIN 2 */

  HAL_TIM_Base_Start_IT(&htim4);

  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET); // controller doesn't reach here

  while (1)

  {

  /* USER CODE END WHILE */

      HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);

//      HAL_Delay(1950);

      HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);

      HAL_Delay(150);

      HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);

        HAL_Delay(100);

        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);

      HAL_Delay(150);

  /* USER CODE BEGIN 3 */

  }

  /* USER CODE END 3 */

}

/** System Clock Configuration

*/

void SystemClock_Config(void)

{

  RCC_OscInitTypeDef RCC_OscInitStruct;

  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  RCC_PeriphCLKInitTypeDef PeriphClkInit;

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

  RCC_OscInitStruct.HSIState = RCC_HSI_ON;

  RCC_OscInitStruct.HSICalibrationValue = 16;

  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;

  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;

  HAL_RCC_OscConfig(&RCC_OscInitStruct);

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1;

  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;

  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);

  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;

  PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV8;

  HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);

  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */

  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

/* TIM4 init function */

void MX_TIM4_Init(void)

{

  TIM_ClockConfigTypeDef sClockSourceConfig;

  TIM_MasterConfigTypeDef sMasterConfig;

  htim4.Instance = TIM4;

  htim4.Init.Prescaler = 8000;

  htim4.Init.CounterMode = TIM_COUNTERMODE_UP;

  htim4.Init.Period = 8000;

  htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

  HAL_TIM_Base_Init(&htim4);

  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

  HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig);

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

  HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig);

}

/** Configure pins as

        * Analog

        * Input

        * Output

        * EVENT_OUT

        * EXTI

*/

void MX_GPIO_Init(void)

{

  GPIO_InitTypeDef GPIO_InitStruct;

  /* GPIO Ports Clock Enable */

  __GPIOC_CLK_ENABLE();

  __GPIOA_CLK_ENABLE();

  /*Configure GPIO pin : PC13 */

  GPIO_InitStruct.Pin = GPIO_PIN_13;

  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;

  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  /*Configure GPIO pin : PCA13 */

  GPIO_InitStruct.Pin = GPIO_PIN_13;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;

  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)

{

 if(htim_base->Instance==TIM4)

  {

  /* USER CODE BEGIN TIM4_MspInit 0 */

  /* USER CODE END TIM4_MspInit 0 */

    /* Peripheral clock enable */

    __TIM4_CLK_ENABLE();

  /* Peripheral interrupt init*/

    HAL_NVIC_SetPriority(TIM4_IRQn, 0, 1);

    HAL_NVIC_EnableIRQ(TIM4_IRQn);

  /* USER CODE BEGIN TIM4_MspInit 1 */

  /* USER CODE END TIM4_MspInit 1 */

  }

}

void TIM4_IRQHandler(void)

{

  /* USER CODE BEGIN TIM4_IRQn 0 */

  /* USER CODE END TIM4_IRQn 0 */

  HAL_TIM_IRQHandler(&htim4);

  /* USER CODE BEGIN TIM4_IRQn 1 */

  HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);

  /* USER CODE END TIM4_IRQn 1 */

}

#timer-interrupt #stm32f103-timer #stm32 #stm32f103-interrupt
4 REPLIES 4
Posted on April 04, 2018 at 15:59

You'd want to check that the vector table links up correctly, and that you have a Hard Fault handler that outputs status or is break pointed. Look at the .MAP file, review the listing file.

Use the debugger. Determine where it goes when it crashes, and problem the peripheral registers/settings.

Make sure all structures are properly initialized *before* enabling interrupts. If this isn't done there is significant potential for NULL pointers and incorrect values to cause the system to fault.

Check the name of the handler matches the one in the startup_stm32f1xx.s, and that you aren't using a .cpp file or C++ syntax/compilation settings.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Abdullah Baig
Associate II
Posted on April 05, 2018 at 07:21

When I created the project, SW4STM32 should have included the startup file for the controller I am using, i.e. STM32F103C8. However, it has included the generic STM32f startup file startup_stm32.s . When I searched for the startup file startup_stm32f103x8 in the Firmware folder, it had the startup for every other controller in the series, (stm32f103x6, xb, xc, xg) but not x8.

I have downloaded the firmware just yesterday but it still doesn't have provisions for my controller. I am thinking of hitting the STM32F103C8 board so had on the wall that it breaks into 103 pieces.

Damn you ST and AC6!!

When you have been shipping the C8 chips for some 10 years, why not take 10 minutes to make its startup file and include in the Firmware???

0690X00000604YgQAI.jpg
Posted on April 05, 2018 at 06:25

Thanks very much

Turvey.Clive

I think you have caught it.

In the startup_stms, there are only the following lines. Below these are all zeros. Where do you think the TIM4_IRQHandler should be in this list?? And can you please give more insight on why this would have been missed??

g_pfnVectors:

.word _estack

.word Reset_Handler

.word NMI_Handler

.word HardFault_Handler

.word MemManage_Handler

.word BusFault_Handler

.word UsageFault_Handler

.word 0

.word 0

.word 0

.word 0

.word SVC_Handler

.word DebugMon_Handler

.word 0

.word PendSV_Handler

.word SysTick_Handler

.word 0

.word 0

.word 0

.word 0

.word 0

.word 0

.word 0

Abdullah Baig
Associate II
Posted on April 05, 2018 at 10:54

IT’SWORKING!!

http://www.openstmorg/tiki-view_forum_thread.php?forumId=7&comments_parentId=6674&threadId=6685&thread_sort_mode=commentDate_ascIT_S_WORKING_

I just made a new project in the SW4STM32 and it includes the correct startup file with all the interrupt vectors. The firmware still doesn’t have

startup_stm32f103x8.s file and the name of the file in startup folder is

startup_stms but now it has all the interrupt vectors and other definitions.

The problem has vanished now and the interrupt is working like a charm.

http://www.openstmorg/img/smiles/icon_rolleyes.gif.pagespeed.ce.mdZqoS9ukF.gif

But I am still skeptic of why the startup file was missed in the first try. Can anyone make a guess??

The startup_stms file has the following comment in the beginning, so I am relieved.

/**

******************************************************************************

* @file startup_stms dedicated to STM32F103C8Tx device

* @author Ac6

* @version V1.0.0

* @date 2018-04-05

******************************************************************************

*/