summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-05-03 14:11:56 +0100
committerDamien George <damien.p.george@gmail.com>2014-05-03 14:11:56 +0100
commitbff1ff28eec5611a2e963601b98044135694df57 (patch)
tree77511058916f65c9cb83e4f38217b5774c1c979b
parentc12242e9dfeefd27a1e987c32ef3cbb6b0856154 (diff)
downloadmicropython-bff1ff28eec5611a2e963601b98044135694df57.tar.gz
micropython-bff1ff28eec5611a2e963601b98044135694df57.zip
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.
-rw-r--r--stmhal/modpyb.c20
1 files changed, 12 insertions, 8 deletions
diff --git a/stmhal/modpyb.c b/stmhal/modpyb.c
index 6ffc92553a..1ffff1d17a 100644
--- a/stmhal/modpyb.c
+++ b/stmhal/modpyb.c
@@ -143,22 +143,26 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis);
/// \function delay(ms)
/// Delay for the given number of milliseconds.
-STATIC mp_obj_t pyb_delay(mp_obj_t count) {
- HAL_Delay(mp_obj_get_int(count));
+STATIC mp_obj_t pyb_delay(mp_obj_t ms_in) {
+ machine_int_t ms = mp_obj_get_int(ms_in);
+ if (ms >= 0) {
+ HAL_Delay(ms);
+ }
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_delay_obj, pyb_delay);
/// \function udelay(us)
/// Delay for the given number of microseconds.
-STATIC mp_obj_t pyb_udelay(mp_obj_t usec) {
- uint32_t count = 0;
- const uint32_t utime = (168 * mp_obj_get_int(usec) / 5);
- for (;;) {
- if (++count > utime) {
- return mp_const_none;
+STATIC mp_obj_t pyb_udelay(mp_obj_t usec_in) {
+ machine_int_t usec = mp_obj_get_int(usec_in);
+ if (usec > 0) {
+ uint32_t count = 0;
+ const uint32_t utime = (168 * usec / 4);
+ while (++count <= utime) {
}
}
+ return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_udelay_obj, pyb_udelay);