summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2019-06-24 13:18:43 +1000
committerDamien George <damien.p.george@gmail.com>2019-07-01 22:53:00 +1000
commit89a23a05b3feb2f81fb00d272202b274c2a325cd (patch)
treec6b455e1eaade9b47bd848d2767df98cf1d768ec
parent999733b1fb08fb07a31846115998dbf43a913327 (diff)
esp8266: Provide custom machine_time_pulse_us that feeds soft WDT.
So that the timeout for machine.time_pulse_us() can be large. Fixes issue #2775.
-rw-r--r--extmod/machine_pulse.c2
-rw-r--r--ports/esp8266/modmachine.c29
2 files changed, 30 insertions, 1 deletions
diff --git a/extmod/machine_pulse.c b/extmod/machine_pulse.c
index 5f837479d..7178b22d7 100644
--- a/extmod/machine_pulse.c
+++ b/extmod/machine_pulse.c
@@ -30,7 +30,7 @@
#if MICROPY_PY_MACHINE_PULSE
-mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t timeout_us) {
+MP_WEAK mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t timeout_us) {
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) {
diff --git a/ports/esp8266/modmachine.c b/ports/esp8266/modmachine.c
index ccde1e5ed..e20e8cb75 100644
--- a/ports/esp8266/modmachine.c
+++ b/ports/esp8266/modmachine.c
@@ -355,6 +355,35 @@ STATIC mp_obj_t machine_enable_irq(mp_obj_t state_in) {
}
MP_DEFINE_CONST_FUN_OBJ_1(machine_enable_irq_obj, machine_enable_irq);
+// Custom version of this function that feeds system WDT if necessary
+mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t timeout_us) {
+ int nchanges = 2;
+ uint32_t start = system_get_time(); // in microseconds
+ for (;;) {
+ uint32_t dt = system_get_time() - start;
+
+ // Check if pin changed to wanted value
+ if (mp_hal_pin_read(pin) == pulse_level) {
+ if (--nchanges == 0) {
+ return dt;
+ }
+ pulse_level = 1 - pulse_level;
+ start = system_get_time();
+ continue;
+ }
+
+ // Check for timeout
+ if (dt >= timeout_us) {
+ return (mp_uint_t)-nchanges;
+ }
+
+ // Only feed WDT every now and then, to make sure edge timing is accurate
+ if ((dt & 0xffff) == 0xffff && !ets_loop_dont_feed_sw_wdt) {
+ system_soft_wdt_feed();
+ }
+ }
+}
+
STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) },
{ MP_ROM_QSTR(MP_QSTR_mem8), MP_ROM_PTR(&machine_mem8_obj) },