4 years ago.

Regarding Timers & Tickers

I am writing codes for STM32F4 Timers.

Pls explain how to start..

What are you trying to do?

posted by Andy A 02 Jan 2018

I need to have ticker for my function with 0.00004 sec. Is ticker programming related to Timers. Is there any document that relates Timer & Tickers?

posted by Rk c 02 Jan 2018

On ST platforms, the HW timer selection is written in the hal_tick.h file.

For the NUCLEO_F429ZI platform for example the file is here:

mbed-os\targets\TARGET_STM\TARGET_STM32F4\TARGET_STM32F429xI\device\hal_tick.h

posted by bco stm 10 Jan 2018

Ok. I have seen this now. If suppose the code is having TIM5, Can we change to any other?

And one more doubt is if we have "n" number of tickers, will all "n" use same TIM5.?

posted by Rk c 10 Jan 2018

1 Answer

4 years ago.

Tickers and Timeouts are basically the same thing, they both allow you to set a function to be called after a certain length of time. The only difference is that a Ticker will repeat over and over until you stop it while a Timeout will only run once. Generally documentation for one will work with the other.

Timers are different, they are for measuring time rather than triggering actions after a time..

The basic code for what you want would be

#include "mbed.h"

Ticker myTicker;

void onTick(void) {
  // code to run once every 0.00004 seconds.
  // avoid anything slow inside a Ticker (or any interrupt based code which is what this is) e.g. no waits, serial output etc...
  // If you need some form of debug output then toggle an LED or at most use putc('x'); to output a single character on serial.
}

main () {
  // set up the ticker
  myTicker.attach_us(&onTick,40); // run every 40us, Could use .attach(&onTick,0.00004) if you prefer.

  // need a while(true) or similar loop to prevent the program from ending or the ticker will stop. Can be left empty,
  while(true) {
   //  other code to run. Will be lower priority than the ticker code and run in the background. 
  }

}

Any global variables will be visible in both the main and the onTick function but you have to be careful, the main code must be written to allow for the possibility that the onTick could run at any point in it and modify any shared variables. Also any global variables modified in onTick should be declared as volatile or the compiler could over-optimize the main loop and not pick up changes.

How to verify this Ticker whether it has completed that many sec's or not.

posted by Rk c 02 Jan 2018

Have the ticker toggle an LED/IO pin and measure it with an oscilloscope. Or have the ticker count up and then at a certain value output something e.g.

volatile bool outputNow = false;
void onTick(void) {
  static int count = 0;
  count++;
  if (count == 25000) {
    outputNow  = true;
    count = 0;
  }
}

main () {
  myTicker.attach_us(&onTick,40); 
 
  while(true) {
    if (outputNow ) {
      outputNow  =  false;
      puts("1 second\r\n");
    }
  }
 
}

This isn't quite as accurate but doesn't require any equipment.

posted by Andy A 02 Jan 2018

I need to understand one thing that for a Ticker defined how we will know which is the underlying Hardware Timer working for that Ticker?

I assigned to you because, your guidance helped me a lot.

posted by Rk c 03 Jan 2018

The mbed libraries use one of the hardware timers to set up a 1 us clock which is then used for all Tickers/Timers/Timeouts. This adds a slight performance overhead but the end result is hardware independent and means you can have more Tickers/Timers than the device has hardware timers. Which specific timer is used depends on the device used, you can dig into the mbed source to find out if it matters for your code.

posted by Andy A 04 Jan 2018

I am using STM32F427 . In that I am using "three" Tickers. Also I dig entire code of mbed os.

i didn't find which hardware timer is used for each ticker.

can u help..

posted by Rk c 05 Jan 2018

Assigned to Andy A 4 years ago.

This means that the question has been accepted and is being worked on.