Skip to content

Commit f59833b

Browse files
committed
machine: Add time_hardware_pulse_us function.
The difference from `time_pulse_us` is that if the pin is initially equal to *pulse_level* then `time_pulse_us` counts the pulse duration immediately, but `time_hardware_pulse_us` first waits for the different *pulse_level*, then waits the equal to *pulse_level* and then counts pulse duration. Signed-off-by: IhorNehrutsa <Ihor.Nehrutsa@gmail.com>
1 parent 594670e commit f59833b

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

docs/library/machine.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,29 @@ Miscellaneous functions
198198
above. The timeout is the same for both cases and given by *timeout_us* (which
199199
is in microseconds).
200200

201+
.. function:: time_hardware_pulse_us(pin, pulse_level, timeout_us=1000000, /)
202+
203+
Time a pulse on the given *pin*, and return the duration of the pulse in
204+
microseconds. The *pulse_level* argument should be 0 to time a low pulse
205+
or 1 to time a high pulse.
206+
207+
If the pin is initially equal to *pulse_level* then first waits until
208+
the pin input becomes different from *pulse_level* (***).
209+
Then the function waits until the pin input becomes equal to *pulse_level* (**),
210+
then the function counts the duration that the pin is equal to *pulse_level* (*).
211+
212+
The function returns -3 if there was timeout waiting for condition marked (***) above.
213+
The function will return -2 if there was timeout waiting for condition marked
214+
(**) above, and -1 if there was timeout during the main measurement, marked (*)
215+
above. The timeout is the same for all cases and given by *timeout_us* (which
216+
is in microseconds).
217+
218+
The difference from `time_pulse_us` is that if the pin is initially equal to *pulse_level*
219+
then `time_pulse_us` counts the pulse duration immediately,
220+
but `time_hardware_pulse_us` first waits for the different *pulse_level*,
221+
then waits the equal to *pulse_level* and then counts pulse duration.
222+
A little bit longer, but with higher accuracy.
223+
201224
.. function:: bitstream(pin, encoding, timing, data, /)
202225

203226
Transmits *data* by bit-banging the specified *pin*. The *encoding* argument

extmod/machine_pulse.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,42 @@ static mp_obj_t machine_time_pulse_us_(size_t n_args, const mp_obj_t *args) {
6262
}
6363
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_time_pulse_us_obj, 2, 3, machine_time_pulse_us_);
6464

65+
MP_WEAK mp_uint_t machine_time_hardware_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t timeout_us) {
66+
mp_uint_t start = mp_hal_ticks_us();
67+
while (mp_hal_pin_read(pin) == pulse_level) {
68+
if ((mp_uint_t)(mp_hal_ticks_us() - start) >= timeout_us) {
69+
return (mp_uint_t)-3;
70+
}
71+
}
72+
start = mp_hal_ticks_us();
73+
while (mp_hal_pin_read(pin) != pulse_level) {
74+
if ((mp_uint_t)(mp_hal_ticks_us() - start) >= timeout_us) {
75+
return (mp_uint_t)-2;
76+
}
77+
}
78+
start = mp_hal_ticks_us();
79+
while (mp_hal_pin_read(pin) == pulse_level) {
80+
if ((mp_uint_t)(mp_hal_ticks_us() - start) >= timeout_us) {
81+
return (mp_uint_t)-1;
82+
}
83+
}
84+
return mp_hal_ticks_us() - start;
85+
}
86+
87+
static mp_obj_t machine_time_hardware_pulse_us_(size_t n_args, const mp_obj_t *args) {
88+
mp_hal_pin_obj_t pin = mp_hal_get_pin_obj(args[0]);
89+
int level = 0;
90+
if (mp_obj_is_true(args[1])) {
91+
level = 1;
92+
}
93+
mp_uint_t timeout_us = 1000000;
94+
if (n_args > 2) {
95+
timeout_us = mp_obj_get_int(args[2]);
96+
}
97+
mp_uint_t us = machine_time_hardware_pulse_us(pin, level, timeout_us);
98+
// May return -1 or -2 or -3 in case of timeout
99+
return mp_obj_new_int(us);
100+
}
101+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_time_hardware_pulse_us_obj, 2, 3, machine_time_hardware_pulse_us_);
102+
65103
#endif

extmod/modmachine.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ static const mp_rom_map_elem_t machine_module_globals_table[] = {
188188
#endif
189189
#if MICROPY_PY_MACHINE_PULSE
190190
{ MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) },
191+
{ MP_ROM_QSTR(MP_QSTR_time_hardware_pulse_us), MP_ROM_PTR(&machine_time_hardware_pulse_us_obj) },
191192
#endif
192193

193194
// Classes for PinBase and Signal.

extmod/modmachine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_deepsleep_obj);
255255
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_bootloader_obj);
256256
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_bitstream_obj);
257257
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_time_pulse_us_obj);
258+
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_time_hardware_pulse_us_obj);
258259

259260
#if MICROPY_PY_MACHINE_I2C
260261
int mp_machine_i2c_transfer_adaptor(mp_obj_base_t *self, uint16_t addr, size_t n, mp_machine_i2c_buf_t *bufs, unsigned int flags);

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