loboris / MicroPython_ESP32_psRAM_LoBo

MicroPython for ESP32 with psRAM support
Other
829 stars 344 forks source link

sleep_us is not accurate #298

Open WilliamHarvey97 opened 5 years ago

WilliamHarvey97 commented 5 years ago

I did the following test code:

import time

init_time = time.time()
time.sleep_us(1000000)
print('exec time: ', str(time.time() - init_time))

It gaves me the following output:

exec time: 0.102143

I think it comes from mphalport.c:

void mp_hal_delay_us(uint32_t us) {
    if (us == 0) return;
    if (us > 10000) {
        // Delay greater then 10 ms, use ms delay function
        uint32_t dus = us % 10000;  // remaining micro seconds
        mp_hal_delay_ms(us/10000);
        if (dus) ets_delay_us(dus);
        return;
    }
    ets_delay_us(us);
}

In this following line:

mp_hal_delay_ms(us/10000)

Here, the delay is smaller than it should by a factor of 10, since a millisecond is 1000 microseconds.

carterw commented 5 years ago

Good catch. But you might have to just fix it in your own codebase, there haven't been any commits here since September of last year.