Skip to content

Commit bff1ff2

Browse files
committed
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.
1 parent c12242e commit bff1ff2

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

stmhal/modpyb.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,22 +143,26 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis);
143143

144144
/// \function delay(ms)
145145
/// Delay for the given number of milliseconds.
146-
STATIC mp_obj_t pyb_delay(mp_obj_t count) {
147-
HAL_Delay(mp_obj_get_int(count));
146+
STATIC mp_obj_t pyb_delay(mp_obj_t ms_in) {
147+
machine_int_t ms = mp_obj_get_int(ms_in);
148+
if (ms >= 0) {
149+
HAL_Delay(ms);
150+
}
148151
return mp_const_none;
149152
}
150153
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_delay_obj, pyb_delay);
151154

152155
/// \function udelay(us)
153156
/// Delay for the given number of microseconds.
154-
STATIC mp_obj_t pyb_udelay(mp_obj_t usec) {
155-
uint32_t count = 0;
156-
const uint32_t utime = (168 * mp_obj_get_int(usec) / 5);
157-
for (;;) {
158-
if (++count > utime) {
159-
return mp_const_none;
157+
STATIC mp_obj_t pyb_udelay(mp_obj_t usec_in) {
158+
machine_int_t usec = mp_obj_get_int(usec_in);
159+
if (usec > 0) {
160+
uint32_t count = 0;
161+
const uint32_t utime = (168 * usec / 4);
162+
while (++count <= utime) {
160163
}
161164
}
165+
return mp_const_none;
162166
}
163167
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_udelay_obj, pyb_udelay);
164168

0 commit comments

Comments
 (0)