This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

STM32 TIM2

Dear Sir

I have a freertos running on the stm32 microncontroller(stm32f101VD),I use a trace function from the API of freertos to record ticks and corresponding tasks

Now the problem is that the tick resolution is too high for me and this means that for the same tick I have manay task recorde, and this because a switch contest can occur betwenn two tick freertos.

So I tried to implement a timer and use the count value in in this trace function API from freertos.

I proceeded like that (from the example code you give ):


void TIM2_Init (void) {

  RCC->APB2ENR |= RCC_APB1ENR_TIM2EN;             /* enable clock for TIM4    */

  TIM2->PSC   = ( 1 - 1);                      /* set prescaler   = 10KHz  */
  TIM2->ARR   = 0xFFFF;                      /* set auto-reload = 250 ms */
  TIM2->RCR   =  0;                              /* set repetition counter   */

  TIM2->DIER = TIM_DIER_UIE;                      /* Update Interrupt enable  */
  NVIC_EnableIRQ(TIM2_IRQn);                   /* TIM2   Interrupt enable  */

  TIM2->CR1  |= TIM_CR1_CEN;                      /* timer enable             */
}

void TIM2_IRQHandler  (void) {

        trace_counter++; // this variable change value actually but cant have the count more
                         //precise ,have same problem as before

  if (TIM2->SR & (TIM_SR_UIF)) {                  /* UIF set?                 */



    TIM2->SR &= ~(TIM_SR_UIF);                    /* clear UIF flag           */
  }

Can you please tell me how can I implement this counter?

Parents
  • Do you really need a timer interrupt?

    Doesn't the processor have any suitable timer that can be free-running that you can just read out the current counter value from?

    With the NXP LPC2xxx chips, I normally use a 32-bit counter ticking at 1MHz - so it turns round every 4000 seconds, but allows events to be recorded with us resolution.

Reply
  • Do you really need a timer interrupt?

    Doesn't the processor have any suitable timer that can be free-running that you can just read out the current counter value from?

    With the NXP LPC2xxx chips, I normally use a 32-bit counter ticking at 1MHz - so it turns round every 4000 seconds, but allows events to be recorded with us resolution.

Children
  • dear Sir

    It is not necessary to implement IRQ ,it is just done to be sure that time counter will be incremented without interruption,but I think resolution that this way I read the count value every switch context which give me result like this

    Time count Task
    0 7
    0 3
    0 2
    1 4
    1 3
    1 5
    3 2
    3 1
    4 4
    4 7
    7 5
    7 4

    something like that ,if the timer has more precision thant tick OS then why I have for two tasks the same counter value wich mean interruption doesnt occur between switch context ,right ?
    So how to impelement a precise timer that will really be more precise than switch context?and this way allow recording tasks in real time?

  • "So how to impelement a precise timer that will really be more precise than switch context?and this way allow recording tasks in real time?"

    Did you consider what I wrote, and checked if the timers in your specific processor have the required capabilities to implement the variant I suggested?