diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2017-02-05 14:20:17 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2017-02-05 14:20:17 +0300 |
commit | d5e9ab6e61729f533dbed5c2b6b27307ce6c3b55 (patch) | |
tree | 727c3606486ac2722726ad9471f37e6f95f7d660 /extmod | |
parent | bd04ed3e8a4235664743fd4c452091a9ce603011 (diff) | |
download | micropython-d5e9ab6e61729f533dbed5c2b6b27307ce6c3b55.tar.gz micropython-d5e9ab6e61729f533dbed5c2b6b27307ce6c3b55.zip |
extmod/machine_pulse: Make time_pulse_us() not throw exceptions.
machine.time_pulse_us() is intended to provide very fine timing, including
while working with signal bursts, where each transition is tracked in row.
Throwing and handling an exception may take too much time and "signal loss".
So instead, in case of a timeout, just return negative value. Cases of
timeout while waiting for initial signal stabilization, and during actual
timing, are recognized.
The documentation is updated accordingly, and rewritten somewhat to clarify
the function behavior.
Diffstat (limited to 'extmod')
-rw-r--r-- | extmod/machine_pulse.c | 6 |
1 files changed, 2 insertions, 4 deletions
diff --git a/extmod/machine_pulse.c b/extmod/machine_pulse.c index b2a78d72ee..5f837479dd 100644 --- a/extmod/machine_pulse.c +++ b/extmod/machine_pulse.c @@ -34,7 +34,7 @@ mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t mp_uint_t start = mp_hal_ticks_us(); while (mp_hal_pin_read(pin) != pulse_level) { if ((mp_uint_t)(mp_hal_ticks_us() - start) >= timeout_us) { - return (mp_uint_t)-1; + return (mp_uint_t)-2; } } start = mp_hal_ticks_us(); @@ -57,9 +57,7 @@ STATIC mp_obj_t machine_time_pulse_us_(size_t n_args, const mp_obj_t *args) { timeout_us = mp_obj_get_int(args[2]); } mp_uint_t us = machine_time_pulse_us(pin, level, timeout_us); - if (us == (mp_uint_t)-1) { - mp_raise_OSError(MP_ETIMEDOUT); - } + // May return -1 or -2 in case of timeout return mp_obj_new_int(us); } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_time_pulse_us_obj, 2, 3, machine_time_pulse_us_); |