Skip to content

Any reason why time.sleep() does not support microseconds #8415

@ZodiusInfuser

Description

@ZodiusInfuser

Hi,

I am attempting to implement half duplex uart for a feature on the upcoming Pimoroni Yukon board, and am in need of doing sub millisecond time delays. Micropython has sleep_us on their RP2040 build but CircuitPython does not, and although the regular sleep function accepts any floating point value, it is quantised to milliseconds.

Here is my analyser output from running time.sleep() with any microsecond value:
image
In it the uart TX is being routed to the half duplex line based on the direction pin. As you can see, the direction pin changes before all the data is sent, meaning it gets cut off.

Doing a 1 millisecond delay has the direction change take too long:
image

Yet, if I add in my own sleep_us function to the CircuitPython build, I can get the direction change happening exactly where it's needed with a time.sleep_us(350) delay.

STATIC mp_obj_t time_sleep_us(mp_obj_t microseconds_o) {
    mp_int_t usecs = mp_obj_get_int(microseconds_o);
    if (usecs < 0) {
        mp_raise_ValueError(translate("sleep length must be non-negative"));
    }
    common_hal_time_delay_us(usecs);
    return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_us_obj, time_sleep_us);

image

This was very easy for me to add, which makes me think there is a good reason why CircuitPython does not have this ability? My immediate thought is boards that do not support us delays, which my code above does not check for.

Perhaps someone could point me to how I could make this change just for RP2040, or maybe just for the Pimoroni Yukon, which I already have an in-progress PR for (#7440)

Thanks

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy