Delay function for microseconds

Is there a way to get a better resolution than milliseconds in Core2?
I need something “delayMicroseconds( delay )” in Arduino.
As far as I understand the “delayUs( uint32_t delay )” has a minimum resolution of 500 microseconds.

Hi Michael, hFramework is based on RTOS (Real Time Operating System) that works with 2kHz system timer. Every 500us is a “system interrupt” and microcontroller starts executing other active tasks.

sys.delay(time_in_ms); method works in other way than in arduino. This method says to RTOS: “this task is now blocked for time_in_ms”, and microcontroller starts executing other tasks (if are in active state). In arduino there is a delay loop that does nothing and could look like this (in the most basic version):

void delay_us(uint32_t time_in_us) {
    volatile uint32_t cnt1 = 0;
    volatile uint32_t cnt2 = 0;
    for(cnt1 = 0; cnt < time_in_us; cnt++) {
        for(cnt2 = 0; cnt < 20; cnt++) {
            cnt++;
            cnt--;
        }
    }
}

In RTOS based systems it is not recomended to use delay functions that are based on “delay loop” as in Arduino for a few reasons:

  1. wasting computing power that can be used by other tasks - especially if you use delay funcion in task with a higher priority, becasue in this scenerio other tasks can not be executed.
  2. there is no guarantee that if you for example want delay 125 ms it will not be 150ms, 500ms, 625ms etc - if another task with a higher priority has something to do, processor can check whether 125 ms have passed after this period.

In most cases delay loop should work well, but it is rather not recommended.

1 Like

Yes I know that the RTOS way is normally better than just waiting an amount of time. But I want to reuse my (Makeblock-) Ultrasonic module which makes measurement per software, basically (for Arduino):

MePort::dWrite2(LOW);
delayMicroseconds(2);
MePort::dWrite2(HIGH);
delayMicroseconds(10);
MePort::dWrite2(LOW);
pinMode(s2, INPUT);
duration = pulseIn(s2, HIGH, timeout);

So the delays are very short. I will try to solve it with the hFramework::hSystem::getUsTimVal() method.
But in general there should be a solution for these short durations. As far as I know the STM32F4 has some timers which can be used with callback functions, maybe that’s a way to do this.

1 Like

Please take a look at a code for ultrasonic sensor hc-sr04: modules/DistanceSensor.cpp at master · husarion/modules · GitHub . It looks like makeblock sensor works the same way.

To get higher resolution than 1ms we use sys.getUsTimVal() that uses timer working with 1us resolution. In this example you doesn’t waste computing power of CPU, because all is done in the interrupt.

This is not recommended, but to make it work without interrupts, delay function would be implemented like this:

void delayMicroseconds(uint32_t time) {
    uint32_t time_start = sys.getUsTimVal();
    while(sys.getUsTimVal() < (time_start + time)) {
        ;
    }
}
1 Like

Thanks will try that in the next days. Found many examples but seems I have overseen this one.
The only difference to my sensor is that I have only one pin for trigger and echo, but it should be possible to switch the trigger pin to input an than configure the interrupt.
I will post the result.

1 Like