cancel
Showing results for 
Search instead for 
Did you mean: 

System tick timer

BoboyeOkeya
Associate III

Hi All,

Presently my systick timer is set to 1ms. Does anyone know how to configure that to be set to 1us? Also can It be set such that it increments by 0.1us?

This is the present clock configuration

0693W00000FCdR0QAL.png 

Here is the present system clock configuration code that I have

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
 
  /** Initializes the CPU, AHB and APB busses clocks
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSI14;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSI14State = RCC_HSI14_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.HSI14CalibrationValue = 16;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

3 REPLIES 3
KnarfB
Principal III

Technically, you can use

SysTick_Config(SystemCoreClock/10000);

to change the SysTick after its initialization.The parameter is the number of SysClocks sys tick, in the above setting the freq. would be 0.1ms.

But: I wouldn't do that.

First: blocking HAL driver functions and others (FreeRTOS if used,...) are using SysTick and assuming that it runs at 1kHz. E.g. HAL_Delay(1000) will no longer be one second.

Second: The default implementation issues an interrupt after each tick (SysTick_Handler). The MCU load increases with more interrupts per second, and at 48 MHz core clock, a period of 1µs corresponds to only 48 assembler instructions , so the interrupt will consume 100% of the MCU.

This all can be changed and managed, but you better find an unused peripheral timer an program it to your needs.

hth

KnarfB

TDK
Guru

If you need a high resolution timer, create a free-running, preferably 32-bit, timer that runs at the system clock frequency, (or at 1us or 0.1us increments) and doesn't require use of the CPU. Other families can use the CYCCNT counter for this, but it's not available on the STM32F0.

Since SysTick requires the cpu to increment variables, having interrupts at 1 MHz or 10 MHz is not workable.

If you feel a post has answered your question, please click "Accept as Solution".

>>Does anyone know how to configure that to be set to 1us? Also can It be set such that it increments by 0.1us?

NOT VIABLE

You're approaching the challenge from entirely the wrong direction.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..