Commit bff1ff28 authored by Damien George's avatar Damien George
Browse files

stmhal: Fix bug where negative delay/udelay lead to huge delays.

A negative ms/us is now treated as a delay of 0 ms/us.  This patch also
improves the calibration of udelay.
parent c12242e9
...@@ -143,22 +143,26 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis); ...@@ -143,22 +143,26 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis);
/// \function delay(ms) /// \function delay(ms)
/// Delay for the given number of milliseconds. /// Delay for the given number of milliseconds.
STATIC mp_obj_t pyb_delay(mp_obj_t count) { STATIC mp_obj_t pyb_delay(mp_obj_t ms_in) {
HAL_Delay(mp_obj_get_int(count)); machine_int_t ms = mp_obj_get_int(ms_in);
if (ms >= 0) {
HAL_Delay(ms);
}
return mp_const_none; return mp_const_none;
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_delay_obj, pyb_delay); STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_delay_obj, pyb_delay);
/// \function udelay(us) /// \function udelay(us)
/// Delay for the given number of microseconds. /// Delay for the given number of microseconds.
STATIC mp_obj_t pyb_udelay(mp_obj_t usec) { STATIC mp_obj_t pyb_udelay(mp_obj_t usec_in) {
uint32_t count = 0; machine_int_t usec = mp_obj_get_int(usec_in);
const uint32_t utime = (168 * mp_obj_get_int(usec) / 5); if (usec > 0) {
for (;;) { uint32_t count = 0;
if (++count > utime) { const uint32_t utime = (168 * usec / 4);
return mp_const_none; while (++count <= utime) {
} }
} }
return mp_const_none;
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_udelay_obj, pyb_udelay); STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_udelay_obj, pyb_udelay);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment