-1

I know very well how to setup timer with hal API in stm32cubemx but I'm new to keil-rtx. Unfortunately STM32CubeMX does not support CMSIS-RTOS2.

My question is I wanna have a timer with less than 1 ms interval.

I want to know how to use hardware timer alongside RTOS2 with HAL functions or is it possible to setup 0.9ms timer with RTOS2 driver?

I'm using stm32f407 microcontroller with 168MHz clock frequency.

I've already increased kernel tick frequency and configured RTOS timer but it's not a good solution.

  • Take a look at the HAL functions and see what they do. Alternatively, STM provides a "LL" (low-level) driver interface that is very lightweight, which probably does everything you need. – pmacfarlane Feb 05 '23 at 14:19
  • Thanks a lot. I created an empty project and then configured a timer with interrupt. By inspecting the hal functions I completely understood what I should do. – Amin Mollaei Feb 06 '23 at 06:26

2 Answers2

1

Here's an example of using the timer on the STM32F407 microcontroller with a 1 millisecond period:

#include "stm32f4xx.h"

void TIM3_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
    // Code to be executed every 1 millisecond
  }
}

void TIM3_Config(void)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  NVIC_InitTypeDef NVIC_InitStructure;

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 999;
  TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

  /* TIM IT enable */
  TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);

  /* Enable the TIM3 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* TIM3 enable counter */
  TIM_Cmd(TIM3, ENABLE);
}

int main(void)
{
  /* TIM3 configuration */
  TIM3_Config();

  while (1)
  {
    // main loop code
  }
}

This code sets up TIM3 as a timer with a 1 millisecond period. The interrupt service routine (ISR) TIM3_IRQHandler will be called every 1 millisecond and execute the code inside it.

Dorin
  • 1,016
  • 1
  • 11
  • 15
0

You can use any timer you want.

Read the reference manual of your STM and you can program it bare metal or using STM-supplied libraries.

0___________
  • 60,014
  • 4
  • 34
  • 74