3 years, 3 months ago.

TimeOut not working properly on NUCLEO-F103RB

Hello all,

I found a problem when I use multiple TimeOuts on a Nucleo-F103RB. When I use two (or more) TimeOuts the program sometimes suddenly seems to 'stop'. I calculate the deltaTime (the time it takes to run one loop, = stopTime loop - startTime loop) which normally takes about 23 uS to complete. After 1253 loops, the deltaTime suddenly drops to 5 uS for 20 loops. After 2253, 5388 and 9388 loops the same problem occurs. It always takes exacly 20 loops to get back to the normal deltaTime.

When I include a wait or if I change the parameters of the TimeOuts the position of the drops change.

Does anybody know what can cause this drop or how it can be fixed? Here's my code:


#include "mbed.h"

Serial pc(USBTX, USBRX); //tx rx debug
Timer       timer;
Timeout     timer1;
Timeout     timer2;

bool        timing1 = false;
bool        timing2 = false;
long        starttime;
long        endtime;
long        deltatime;

void interrupt1(){
    timing1 = false;

void interrupt2(){    
    timing2 = false;

int main(){
        starttime = timer.read_us();

        if(timing1 == false){
        timer1.attach_us(&interrupt1, 300);
        timing1 = true;}
        if(timing2 == false){
        timer2.attach_us(&interrupt2, 300);
        timing2 = true;}    
        endtime = timer.read_us();
        deltatime = endtime - starttime;
        pc.printf("%i\r\n", deltatime);

timing1 and timing2 should be volatile otherwise you could get some weird compiler optimisation issues.

posted by Andy A 09 Jun 2017

1 Answer

3 years, 3 months ago.

Hello Hein,
There are some issues with the current implementation of Timer, Ticker, Timeout and wait family of functions for STM boards that are using a 16-bit timer. See for example this link. Fortunately STM team has already a solution which is in test phase and available here. Using the new hal_tick_16.c and us_ticker_16b.c in my project fixed all the issues. I think it is worth to give it a try.

Accepted Answer