Skip to content

Commit adf61cd

Browse files
committed
esp8266: Update port to use new event functions.
This is necessary to avoid watchdog timeout in long i2c.scan(), as previously machine_i2c.c would call MICROPY_EVENT_POLL_HOOK if MICROPY_EVENT_POLL_HOOK_FAST was not available. Compared to previous implementation, this implementation removes the ets_event_poll() function and calls the SDK function ets_loop_iter() from MICROPY_INTERNAL_EVENT_HOOK instead. This allows using the port-agnostic functions in more places. There is a small behaviour change, which is that the event loop gets iterated in a few more places (i.e. anywhere that mp_event_handle_nowait() is called.) However, this looks like maybe only modselect.c - and is probably good to process Wi-Fi events in that polling loop. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent 8d67d76 commit adf61cd

File tree

7 files changed

+33
-17
lines changed

7 files changed

+33
-17
lines changed

ports/esp8266/esp_mphal.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "esp_mphal.h"
3232
#include "user_interface.h"
3333
#include "ets_alt_task.h"
34+
#include "py/event.h"
3435
#include "py/runtime.h"
3536
#include "py/stream.h"
3637
#include "extmod/misc.h"
@@ -53,7 +54,7 @@ void mp_hal_init(void) {
5354
void MP_FASTCODE(mp_hal_delay_us)(uint32_t us) {
5455
uint32_t start = system_get_time();
5556
while (system_get_time() - start < us) {
56-
ets_event_poll();
57+
mp_event_handle_nowait();
5758
}
5859
}
5960

@@ -122,11 +123,6 @@ uint64_t mp_hal_time_ns(void) {
122123
return pyb_rtc_get_us_since_epoch() * 1000ULL;
123124
}
124125

125-
void ets_event_poll(void) {
126-
ets_loop_iter();
127-
mp_handle_pending(true);
128-
}
129-
130126
void __assert_func(const char *file, int line, const char *func, const char *expr) {
131127
printf("assert:%s:%d:%s: %s\n", file, line, func, expr);
132128
mp_raise_msg(&mp_type_AssertionError, MP_ERROR_TEXT("C-level assert"));

ports/esp8266/esp_mphal.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#ifndef MICROPY_INCLUDED_ESP8266_ESP_MPHAL_H
28+
#define MICROPY_INCLUDED_ESP8266_ESP_MPHAL_H
29+
2730
#include "user_interface.h"
2831
#include "py/ringbuf.h"
2932
#include "shared/runtime/interrupt_char.h"
@@ -72,9 +75,6 @@ void dupterm_task_init();
7275
uint32_t esp_disable_irq(void);
7376
void esp_enable_irq(uint32_t state);
7477

75-
void ets_event_poll(void);
76-
#define ETS_POLL_WHILE(cond) { while (cond) ets_event_poll(); }
77-
7878
// needed for machine.I2C
7979
#include "osapi.h"
8080
#define mp_hal_delay_us_fast(us) os_delay_us(us)
@@ -111,3 +111,5 @@ void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin);
111111

112112
void *ets_get_esf_buf_ctlblk(void);
113113
int ets_esf_free_bufs(int idx);
114+
115+
#endif // MICROPY_INCLUDED_ESP8266_ESP_MPHAL_H

ports/esp8266/machine_uart.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "py/mperrno.h"
3131
#include "py/mphal.h"
32+
#include "py/event.h"
3233
#include "ets_sys.h"
3334
#include "user_interface.h"
3435
#include "uart.h"
@@ -300,7 +301,7 @@ STATIC mp_uint_t mp_machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, uint
300301
if (mp_machine_uart_txdone(self)) {
301302
return 0;
302303
}
303-
MICROPY_EVENT_POLL_HOOK
304+
mp_event_wait_ms(1);
304305
} while (system_get_time() < timeout);
305306

306307
*errcode = MP_ETIMEDOUT;

ports/esp8266/modespnow.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <stdint.h>
3232
#include <string.h>
3333

34+
#include "py/event.h"
3435
#include "py/runtime.h"
3536

3637
#if MICROPY_PY_ESPNOW
@@ -285,7 +286,7 @@ static int ringbuf_get_bytes_wait(ringbuf_t *r, uint8_t *data, size_t len, mp_in
285286
int status = 0;
286287
while (((status = ringbuf_get_bytes(r, data, len)) == -1)
287288
&& (timeout_ms < 0 || (mp_uint_t)(mp_hal_ticks_ms() - start) < (mp_uint_t)timeout_ms)) {
288-
MICROPY_EVENT_POLL_HOOK;
289+
mp_event_wait_ms(1);
289290
}
290291
return status;
291292
}

ports/esp8266/modmachine.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "etshal.h"
3636
#include "ets_alt_task.h"
3737
#include "user_interface.h"
38+
#include "py/event.h"
3839

3940
// #define MACHINE_WAKE_IDLE (0x01)
4041
// #define MACHINE_WAKE_SLEEP (0x02)
@@ -91,7 +92,7 @@ STATIC mp_obj_t mp_machine_unique_id(void) {
9192

9293
STATIC void mp_machine_idle(void) {
9394
asm ("waiti 0");
94-
ets_event_poll(); // handle any events after possibly a long wait (eg feed WDT)
95+
mp_event_handle_nowait(); // handle any events after possibly a long wait (eg feed WDT)
9596
}
9697

9798
STATIC void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
@@ -106,7 +107,7 @@ STATIC void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
106107
uint32_t wifi_mode = wifi_get_opmode();
107108
uint32_t start = system_get_time();
108109
while (system_get_time() - start <= max_us) {
109-
ets_event_poll();
110+
mp_event_handle_nowait();
110111
if (wifi_mode == NULL_MODE) {
111112
// Can only idle if the wifi is off
112113
asm ("waiti 0");

ports/esp8266/mpconfigport.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// Board-specific definitions
88
#include "mpconfigboard.h"
99

10+
#include <stdbool.h>
1011
#include <stdint.h>
1112

1213
// Set the rom feature level.
@@ -116,13 +117,26 @@
116117
#define MICROPY_FATFS_LFN_CODE_PAGE 437 /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */
117118
#define MICROPY_ESP8266_APA102 (1)
118119

119-
#define MICROPY_EVENT_POLL_HOOK {ets_event_poll();}
120+
// No blocking wait-for-event on ESP8266, only non-blocking pump of the "OS" event
121+
// loop
122+
//
123+
// TODO: When TIMEOUT_MS==-1, it may be possible to have MICROPY_INTERNAL_WFE() call the "waiti" instruction.
124+
// See mp_machine_idle() and mp_machine_lightsleep() in esp8266/modmachine.c
125+
//
126+
// Note: We have to scope the declaration of ets_loop_iter() here as there are multiple incompatible
127+
// definitions at compile time between the SDK and axTLS!
128+
#define MICROPY_INTERNAL_WFE(TIMEOUT_MS)
129+
#define MICROPY_INTERNAL_EVENT_HOOK \
130+
do { \
131+
extern bool ets_loop_iter(void); \
132+
ets_loop_iter(); \
133+
} while (0)
134+
120135
#define MICROPY_VM_HOOK_COUNT (10)
121136
#define MICROPY_VM_HOOK_INIT static uint vm_hook_divisor = MICROPY_VM_HOOK_COUNT;
122137
#define MICROPY_VM_HOOK_POLL if (--vm_hook_divisor == 0) { \
123138
vm_hook_divisor = MICROPY_VM_HOOK_COUNT; \
124-
extern void ets_loop_iter(void); \
125-
ets_loop_iter(); \
139+
MICROPY_INTERNAL_EVENT_HOOK; \
126140
}
127141
#define MICROPY_VM_HOOK_LOOP MICROPY_VM_HOOK_POLL
128142
#define MICROPY_VM_HOOK_RETURN MICROPY_VM_HOOK_POLL

ports/esp8266/uart.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "c_types.h"
1919
#include "user_interface.h"
2020
#include "esp_mphal.h"
21+
#include "py/event.h"
2122

2223
// seems that this is missing in the Espressif SDK
2324
#define FUNC_U0RXD 0
@@ -218,7 +219,7 @@ bool ICACHE_FLASH_ATTR uart_rx_wait(uint32_t timeout_us) {
218219
if (system_get_time() - start >= timeout_us) {
219220
return false; // timeout
220221
}
221-
ets_event_poll();
222+
mp_event_handle_nowait();
222223
}
223224
}
224225

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