Skip to content

Commit d5e9ab6

Browse files
committed
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.
1 parent bd04ed3 commit d5e9ab6

File tree

5 files changed

+15
-21
lines changed

5 files changed

+15
-21
lines changed

docs/library/machine.rst

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,15 @@ Miscellaneous functions
118118
microseconds. The `pulse_level` argument should be 0 to time a low pulse
119119
or 1 to time a high pulse.
120120

121-
The function first waits while the pin input is different to the `pulse_level`
122-
parameter, then times the duration that the pin is equal to `pulse_level`.
121+
If the current input value of the pin is different to `pulse_level`,
122+
the function first (*) waits until the pin input becomes equal to `pulse_level`,
123+
then (**) times the duration that the pin is equal to `pulse_level`.
123124
If the pin is already equal to `pulse_level` then timing starts straight away.
124125

125-
The function will raise an OSError with ETIMEDOUT if either of the waits is
126-
longer than the given timeout value (which is in microseconds).
126+
The function will return -2 if there was timeout waiting for condition marked
127+
(*) above, and -1 if there was timeout during the main measurement, marked (**)
128+
above. The timeout is the same for both cases and given by `timeout_us` (which
129+
is in microseconds).
127130

128131
.. _machine_constants:
129132

drivers/dht/dht.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ STATIC mp_obj_t dht_readinto(mp_obj_t pin_in, mp_obj_t buf_in) {
6565

6666
// time pulse, should be 80us
6767
ticks = machine_time_pulse_us(pin, 1, 150);
68-
if (ticks == (mp_uint_t)-1) {
68+
if ((mp_int_t)ticks < 0) {
6969
goto timeout;
7070
}
7171

7272
// time 40 pulses for data (either 26us or 70us)
7373
uint8_t *buf = bufinfo.buf;
7474
for (int i = 0; i < 40; ++i) {
7575
ticks = machine_time_pulse_us(pin, 1, 100);
76-
if (ticks == (mp_uint_t)-1) {
76+
if ((mp_int_t)ticks < 0) {
7777
goto timeout;
7878
}
7979
buf[i / 8] = (buf[i / 8] << 1) | (ticks > 48);

extmod/machine_pulse.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t
3434
mp_uint_t start = mp_hal_ticks_us();
3535
while (mp_hal_pin_read(pin) != pulse_level) {
3636
if ((mp_uint_t)(mp_hal_ticks_us() - start) >= timeout_us) {
37-
return (mp_uint_t)-1;
37+
return (mp_uint_t)-2;
3838
}
3939
}
4040
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) {
5757
timeout_us = mp_obj_get_int(args[2]);
5858
}
5959
mp_uint_t us = machine_time_pulse_us(pin, level, timeout_us);
60-
if (us == (mp_uint_t)-1) {
61-
mp_raise_OSError(MP_ETIMEDOUT);
62-
}
60+
// May return -1 or -2 in case of timeout
6361
return mp_obj_new_int(us);
6462
}
6563
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_time_pulse_us_obj, 2, 3, machine_time_pulse_us_);

tests/extmod/machine_pulse.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,5 @@ def value(self, v=None):
4343
print(type(t))
4444

4545
p = ConstPin(0)
46-
try:
47-
machine.time_pulse_us(p, 1, 10)
48-
except OSError:
49-
print("OSError")
50-
51-
try:
52-
machine.time_pulse_us(p, 0, 10)
53-
except OSError:
54-
print("OSError")
46+
print(machine.time_pulse_us(p, 1, 10))
47+
print(machine.time_pulse_us(p, 0, 10))

tests/extmod/machine_pulse.py.exp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ value: 1
55
value: 0
66
value: 1
77
<class 'int'>
8-
OSError
9-
OSError
8+
-2
9+
-1

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy