Skip to content

Timer.read_us takes 8.5µs, making wait_us unable to wait for less than that on Nucleo F103RB #7397

Closed
@LaurentLouf

Description

@LaurentLouf

Description

  • Target : Nucleo F103RB
  • Toolchain : GNU ARM Embedded Toolchain 7-2017q4-major Release
  • Mbed-CLI 1.7.3 (mbed --version), mbed compile --profile release
  • Mbed-OS : #5bf483ee1
  • Code to reproduce issue
#include "mbed.h"
Timer timer;

int main() {

    timer.start();
    DigitalOut test(PA_1);
    int t1, t2, t3, t4, t5, t6, t7, t8;
    int t11, t12, t13, t14, t15, t16, t17, t18;

    while (1) {
        t1 = timer.read_us();
        wait_us(1);
        t2 = timer.read_us();
        wait_us(1);
        t3 = timer.read_us();
        wait_us(1);
        t4 = timer.read_us();
        wait_us(1);
        t5 = timer.read_us();
        wait_us(1);
        t6 = timer.read_us();
        wait_us(1);
        t7 = timer.read_us();
        wait_us(1);
        t8 = timer.read_us();

        t11 = timer.read_us();
        test = 0;
        test = 1;
        test = 0;
        test = 1;
        test = 0;
        test = 1;
        test = 0;
        test = 1;
        test = 0;
        test = 1;
        test = 0;
        test = 1;
        test = 0;
        test = 1;
        t12 = timer.read_us();
        test = 0;

        printf("%ld \t%ld \t%ld \t%ld \t%ld \t%ld \t%ld \t%ld\r\n\t%ld \t %ld\r\n", t1, t2, t3, t4,
               t5, t6, t7, t8, t11, t12);
        wait(0.5);
    }
}

Which produces

38 55 73 90 107 125 142
150 158

And more graphically ee9464984aba2d169cc824ea930dd76c.png
9a4e4eaf0f4b1d23e2317ddaa078e9b5.png

So I was investigating problems with timings, and one thing led to another, I realized that timer.read_us and wait_us seem to be quite slow on my board. Since they both use ticker_read_us, I guess the problem is somewhere there. Thing is, I've Googled and it seems this problem isn't widespread (one thing among others, it breaks OneWire timings, so there should be a few reports on that), so that may come from me, but the example code is pretty straightforward. Any idea ?

Issue request type

[ ] Question
[ ] Enhancement
[X] Bug

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions