Mbed on teensy31: Unstable timing when using Ticker and wait() (runs fine when compiled on mbed.com)

Hello everyone,

I ran into a bit of an odd (and also annoying) problem. I spent a couple hours trying to fix it, as I was convinced I was doing something wrong myself (that’s still very possible), but I ran out of ideas. So, here I am documenting what I’ve got and asking for help! To be clear, the problem does not occur at all when compiling on mbed.com, only through platformio. Hopefully you guys have some idea what is going on and can help me!

Environment

  • OS: Arch Linux x86-64bit
  • Python version 2.7.14 (version 3.6.4 also installed on system)
  • Platformio version 3.5.0 (possibly occurs on older versions too, did not test)
  • Toolchain versions:
  • teensy @ 2.9.0
  • framework-mbed @ 4.50701.0
  • tool-teensy @ 1.21.0
  • toolchain-gccarmnoneeabi @ 1.60301.0

platformio.ini

[env:teensy31]
platform = teensy
board = teensy31
framework = mbed

Code

#include "mbed.h"

DigitalOut myled(LED1); // Onboard LED
 
 void step(){
     myled = !(myled);
}
 
int main() {
    Ticker t;
    t.attach(&step, 0.1);
    while(1) {
        wait(1); // changing this duration seems to affect pattern of misbehaviour
    }
}

Click here for github repo, and here for mbed repo.

Expected behaviour

Regular blinking of the LED, toggling once every 100ms.

Observed behaviour using mbed online compiler

Regular blinking of the LED at 100ms, as expected.

Observed behaviour using platformio

Irregular blinking of the LED, seemingly switching between two different modes, one where the LED toggles every 100ms as expected, and one where the LED toggles at extremely high frequency (measured period of around 8µs).

Measurement from logic analyzer

Pattern visible over 10 seconds. Low frequencey square wave indicates correct behaviour, high frequency sqaure wave (so high it shows as solid gray on this zoom level) is incorrect behaviour. This pattern seems to repeat indefinitely.

Close-up of start of first period of incorrect beahaviour (high-frequency square wave).

I am guessing that is enough information for now. Hopefully somebody will know what’s up.

Thanks for your time!
Arjen

I think you must enable the rtos. In the platformio.ini add:
build_flags = -D PIO_FRAMEWORK_MBED_RTOS_PRESENT

First of all thanks for your quick response.

With regards to the RTOS inclusion. I may be mistaken, but I don’t think the Ticker is part of the mbed rtos. I decided to go ahead and try it anyway, adding -D PIO_FRAMEWORK_MBED_RTOS_PRESENT to the platformio.ini and #include “rtos.h” to the code (I set up a separate branch in my git repo here. It seems to change the behaviour but not make it go away. At first it seemed fixed, but after playing with the Ticker interval and wait() time a bit, I found it came back. Currently I only changed the Ticker interval to 0.100001(i.e. 100ms+1µs), which makes the problematic behaviour occur once briefly about 5 seconds after startup, and then not again. I am guessing there is some kind of repeating pattern, but that it is very long.

Here’s another measurement:


During the solid grey part the period appears to be around 9µs (it used to be 8µs, I am not sure what the relevance of that is).

I am not sure at this point whether including the mbed RTOS actually matters in a big way. It does change the observed behaviour, but both with and without, the behaviour is incorrect. I would guess that the RTOS has different timing code, which responds differently to whatever is wrong, but is not immune to it.

Anybody have any more ideas? Perhaps somebody is able to reproduce my problem? That would already rule out some kind of complex hardware failure, at least.

Thanks again,
Arjen

Try to add the event system, there are many internal chages of the api.
build_flags = -D PIO_FRAMEWORK_MBED_EVENTS_PRESENT

Adding -D PIO_FRAMEWORK_MBED_EVENTS_PRESENT doesn’t appear to change the behaviour of Ticker at all.

Thanks again,
Arjen

This is a bug with mbed 5.7.0. I see that they still use 5.6.6 for Teensy. We’ve reverted back Teensy to 5.6.6 in Release 2.9.1 · platformio/platform-teensy · GitHub

Please pio platform update or PIO Home > Platforms > Updates.

Awesome. Thanks for the quick fix! :+1: