GPT time between events

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderators: RoccoMarco, barthess

tsm
Posts: 16
Joined: Tue Mar 03, 2020 9:12 pm
Has thanked: 1 time
Been thanked: 1 time

GPT time between events

Postby tsm » Sun Sep 11, 2022 9:37 pm

Hello,

On a STM32F767zi I'm experimenting with GPT by starting/stopping a timer and querying the time it has passed ever since it started in milliseconds. My endgame is to start the timer when a certain event occurs (button press, signal edge, etc), but for now I wrote the simpler example bellow to experiment with:

Code: Select all

static const GPTConfig gpt5cfg = {
  1000000, /* 1MHz timer clock.*/
  nullptr,     /* Timer callback.*/
  0,
  0
};

// (...)
  halInit();
  chSysInit();

  gptStart(&GPTD5, &gpt5cfg);

  while (!chThdShouldTerminateX())
  {
    // configure so we get a trigger every millisecond
    gptStartContinuous(&GPTD5, 1000);
    chThdSleepMilliseconds(250);
    log_print("%u\r\n", gptGetCounterX(&GPTD5));

    chThdSleepMilliseconds(250);
    log_print("%u\r\n", gptGetCounterX(&GPTD5));

    chThdSleepMilliseconds(250);
    log_print("%u\r\n", gptGetCounterX(&GPTD5));

    chThdSleepMilliseconds(250);
    log_print"%u\r\n\n", gptGetCounterX(&GPTD5));

    gptStopTimer(&GPTD5);
  }


I was expecting to see ~250, 500, 750 and then 1000. But I get 982 on all prints. I'm either configuring something wrong or misinterpreting the timers and their APIs?

As a followup question if I want to do the same thing but in microseconds how could I setup TIM5 as 32bit timer?

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: GPT time between events

Postby Giovanni » Mon Sep 12, 2022 2:48 am

Hi,

You configured the timer with a 1MHz clock and a 1000 count limit, this means that it resets after one millisecond. Probably you read the same value because the counting is synchronous with the other timer used for system sleep functions.

Giovanni

tsm
Posts: 16
Joined: Tue Mar 03, 2020 9:12 pm
Has thanked: 1 time
Been thanked: 1 time

Re: GPT time between events

Postby tsm » Mon Sep 12, 2022 10:29 am

Giovanni wrote:Probably you read the same value because the counting is synchronous with the other timer used for system sleep functions.


Is this stm32 specific or chibios? On the reference manual I see that TIM2/3/4/5 are supposed to be independent.

Giovanni wrote:Hi,

You configured the timer with a 1MHz clock and a 1000 count limit, this means that it resets after one millisecond.


Yeah I completely misunderstood the functionality, I assumed it would count up every millisecond not reset. So now I've changed to TIM5 and see the count value change. Now I'm testing continuous mode with a period of 1000000 with a callback.

Code: Select all

gptStartContinuous(&GPTD4, 1000000);


This callback just toggles some LEDs. With this setup now I was expecting to get LEDs toggling either every 65ms if the timer is configured to16-bit or every second if 32-bit? Nonetheless I believe I'm getting a toggle every 17ms

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: GPT time between events

Postby Giovanni » Mon Sep 12, 2022 1:21 pm

"1000000" is not a valid value for a 16 bits timer like TIM4.

You need to consider that the driver is a very thin layer around a physical timers, you need to consider the timer capabilities. Also, enable assertions, those can catch many errors.

Giovanni


Return to “STM32 Support”

Who is online

Users browsing this forum: No registered users and 23 guests