cancel
Showing results for 
Search instead for 
Did you mean: 

Systick timing unexpected.

stanley
Associate II
Posted on April 26, 2016 at 11:25

Hi all,

I am working with STM32F411CE.

I know that if I use below code will occurred every 1 ms, but it occurred every 0.5ms in my code.

HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);    // HAL_RCC_GetHCLKFreq = 96000000;

I have searched the keyword ''systick_config'' and others, but I still could not resolve it.

Please give some me advise to debug.

I know it will depend on the Clock configuration. I post my configuration as below.

My Code generated by STM32CubeMX.

/** System Clock Configuration

*/

void SystemClock_Config(void)

{

  RCC_OscInitTypeDef RCC_OscInitStruct;

  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  __PWR_CLK_ENABLE();

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  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;

  RCC_OscInitStruct.PLL.PLLM = 8;

  RCC_OscInitStruct.PLL.PLLN = 192;

  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;

  RCC_OscInitStruct.PLL.PLLQ = 8;

  HAL_RCC_OscConfig(&RCC_OscInitStruct);

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1

                              |RCC_CLOCKTYPE_PCLK2;

  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;

  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3);

  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  //HAL_SYSTICK_Config(SystemCoreClock/1000);    //Same result

  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */

  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

  /*---------------------------------------------------------------------------*/

  void SysTick_Handler(void) {

      HAL_IncTick();

      HAL_SYSTICK_IRQHandler();

      count++;

  }

  __weak void HAL_IncTick(void)

{

  uwTick++;

}

__weak uint32_t HAL_GetTick(void)

{

  return uwTick;

}

/* Main while loop */

 uint32_t        tickupdate;

while(1)

{

         if(HAL_GetTick() > tickupdate)

 {

  tickupdate = HAL_GetTick() + 10000;    // 10000 ms = 10 sec

  printf(''* tick every 10sec : %d \r\n'', _ticktupdate); 

 }

}

Actual Result:

I saw the message print every 5 sec. (Wrong)

Expected Result:

It should be print the message every 10 sec.

Please help to give me some suggestions.

Thanks All.

#systick
1 ACCEPTED SOLUTION

Accepted Solutions
stanley
Associate II
Posted on April 27, 2016 at 05:03

Hi Clive1,

You are right. The IRQ handler is also calling  increment function. 

It's working now when I remove it.

Many thanks, 

View solution in original post

2 REPLIES 2
Posted on April 26, 2016 at 12:25

I suspect the IRQ handler is also calling the incrementing function. This would explain why it is twice as fast. If you think the clocks are wrong internally, export via MCO pin and measure them.

The logic in your loop is also broken and fails to handle the wrapping condition.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
stanley
Associate II
Posted on April 27, 2016 at 05:03

Hi Clive1,

You are right. The IRQ handler is also calling  increment function. 

It's working now when I remove it.

Many thanks,