Skip to content

Commit 398da22

Browse files
committed
esp8266/modmachine: Use common machine_time_pulse_us implementation.
Testing shows that for frequencies which the esp8266 can handle -- up to about 1kHz -- `machine.time_pulse_us()` now gives more accurate results. Prior to this commit it would measure on average about 1us lower, but now the average is much closer to the true value. For example a pulse that is 1000us long, it would measure between 998 and 1000us. Now it measures between 999us and 1001us. Signed-off-by: Damien George <damien@micropython.org>
1 parent ef21ade commit 398da22

File tree

3 files changed

+10
-31
lines changed

3 files changed

+10
-31
lines changed

extmod/machine_pulse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
#if MICROPY_PY_MACHINE_PULSE
3232

33-
MP_WEAK mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t timeout_us) {
33+
mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t timeout_us) {
3434
mp_uint_t nchanges = 2;
3535
mp_uint_t start = mp_hal_ticks_us();
3636
for (;;) {

ports/esp8266/esp_mphal.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,20 @@
2727
#include "user_interface.h"
2828
#include "py/ringbuf.h"
2929
#include "shared/runtime/interrupt_char.h"
30+
#include "ets_alt_task.h"
3031
#include "xtirq.h"
3132

3233
#define MICROPY_BEGIN_ATOMIC_SECTION() esp_disable_irq()
3334
#define MICROPY_END_ATOMIC_SECTION(state) esp_enable_irq(state)
3435

36+
// During machine.time_pulse_us, feed WDT every now and then.
37+
#define MICROPY_PY_MACHINE_TIME_PULSE_US_HOOK(dt) \
38+
do { \
39+
if ((dt & 0xffff) == 0xffff && !ets_loop_dont_feed_sw_wdt) { \
40+
system_soft_wdt_feed(); \
41+
} \
42+
} while (0)
43+
3544
void mp_sched_keyboard_interrupt(void);
3645

3746
struct _mp_print_t;

ports/esp8266/modmachine.c

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include "os_type.h"
3434
#include "osapi.h"
3535
#include "etshal.h"
36-
#include "ets_alt_task.h"
3736
#include "user_interface.h"
3837

3938
// #define MACHINE_WAKE_IDLE (0x01)
@@ -327,32 +326,3 @@ MP_DEFINE_CONST_OBJ_TYPE(
327326
print, esp_timer_print,
328327
locals_dict, &esp_timer_locals_dict
329328
);
330-
331-
// Custom version of this function that feeds system WDT if necessary
332-
mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t timeout_us) {
333-
int nchanges = 2;
334-
uint32_t start = system_get_time(); // in microseconds
335-
for (;;) {
336-
uint32_t dt = system_get_time() - start;
337-
338-
// Check if pin changed to wanted value
339-
if (mp_hal_pin_read(pin) == pulse_level) {
340-
if (--nchanges == 0) {
341-
return dt;
342-
}
343-
pulse_level = 1 - pulse_level;
344-
start = system_get_time();
345-
continue;
346-
}
347-
348-
// Check for timeout
349-
if (dt >= timeout_us) {
350-
return (mp_uint_t)-nchanges;
351-
}
352-
353-
// Only feed WDT every now and then, to make sure edge timing is accurate
354-
if ((dt & 0xffff) == 0xffff && !ets_loop_dont_feed_sw_wdt) {
355-
system_soft_wdt_feed();
356-
}
357-
}
358-
}

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