-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
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:
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:
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);
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