Skip to content

core,rp2,esp8266,windows, unix: Add new cross-port functions for event waiting and handling. #13096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions drivers/cyw43/cywbt.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ STATIC int cywbt_hci_cmd_raw(size_t len, uint8_t *buf) {
mp_bluetooth_hci_uart_write((void *)buf, len);
for (int c, i = 0; i < 6; ++i) {
while ((c = mp_bluetooth_hci_uart_readchar()) == -1) {
MICROPY_EVENT_POLL_HOOK
mp_event_wait_indefinite();
}
buf[i] = c;
}
Expand All @@ -88,7 +88,7 @@ STATIC int cywbt_hci_cmd_raw(size_t len, uint8_t *buf) {
int sz = buf[2] - 3;
for (int c, i = 0; i < sz; ++i) {
while ((c = mp_bluetooth_hci_uart_readchar()) == -1) {
MICROPY_EVENT_POLL_HOOK
mp_event_wait_indefinite();
}
buf[i] = c;
}
Expand Down
5 changes: 3 additions & 2 deletions drivers/ninaw10/nina_bt_hci.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ static int nina_hci_cmd(int ogf, int ocf, size_t param_len, const uint8_t *param
// Receive HCI event packet, initially reading 3 bytes (HCI Event, Event code, Plen).
for (mp_uint_t start = mp_hal_ticks_ms(), size = 3, i = 0; i < size;) {
while (!mp_bluetooth_hci_uart_any()) {
MICROPY_EVENT_POLL_HOOK
mp_uint_t elapsed = mp_hal_ticks_ms() - start;
// Timeout.
if ((mp_hal_ticks_ms() - start) > HCI_COMMAND_TIMEOUT) {
if (elapsed > HCI_COMMAND_TIMEOUT) {
error_printf("timeout waiting for HCI packet\n");
return -1;
}
mp_event_wait_ms(HCI_COMMAND_TIMEOUT - elapsed);
}

buf[i] = mp_bluetooth_hci_uart_readchar();
Expand Down
8 changes: 4 additions & 4 deletions extmod/btstack/modbluetooth_btstack.c
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ STATIC void set_random_address(void) {
volatile bool ready = false;
btstack_crypto_random_generate(&sm_crypto_random_request, static_addr, 6, &btstack_static_address_ready, (void *)&ready);
while (!ready) {
MICROPY_EVENT_POLL_HOOK
mp_event_wait_indefinite();
}

#endif // MICROPY_BLUETOOTH_USE_MP_HAL_GET_MAC_STATIC_ADDRESS
Expand All @@ -574,7 +574,7 @@ STATIC void set_random_address(void) {
break;
}

MICROPY_EVENT_POLL_HOOK
mp_event_wait_indefinite();
}
DEBUG_printf("set_random_address: Address loaded by controller\n");
}
Expand Down Expand Up @@ -654,7 +654,7 @@ int mp_bluetooth_init(void) {
// Either the HCI event will set state to ACTIVE, or the timeout will set it to TIMEOUT.
mp_bluetooth_btstack_port_start();
while (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_STARTING) {
MICROPY_EVENT_POLL_HOOK
mp_event_wait_indefinite();
}
btstack_run_loop_remove_timer(&btstack_init_deinit_timeout);

Expand Down Expand Up @@ -727,7 +727,7 @@ void mp_bluetooth_deinit(void) {
// either timeout or clean shutdown.
mp_bluetooth_btstack_port_deinit();
while (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_ACTIVE) {
MICROPY_EVENT_POLL_HOOK
mp_event_wait_indefinite();
}
btstack_run_loop_remove_timer(&btstack_init_deinit_timeout);

Expand Down
6 changes: 1 addition & 5 deletions extmod/machine_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,7 @@ STATIC mp_obj_t machine_i2c_scan(mp_obj_t self_in) {
// This scan loop may run for some time, so process any pending events/exceptions,
// or allow the port to run any necessary background tasks. But do it as fast as
// possible, in particular we are not waiting on any events.
#if defined(MICROPY_EVENT_POLL_HOOK_FAST)
MICROPY_EVENT_POLL_HOOK_FAST;
#elif defined(MICROPY_EVENT_POLL_HOOK)
MICROPY_EVENT_POLL_HOOK
#endif
mp_event_handle_nowait();
}
return list;
}
Expand Down
6 changes: 1 addition & 5 deletions extmod/modlwip.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,7 @@ typedef struct _lwip_socket_obj_t {
} lwip_socket_obj_t;

static inline void poll_sockets(void) {
#ifdef MICROPY_EVENT_POLL_HOOK
MICROPY_EVENT_POLL_HOOK;
#else
mp_hal_delay_ms(1);
#endif
mp_event_wait_ms(1);
}

STATIC struct tcp_pcb *volatile *lwip_socket_incoming_array(lwip_socket_obj_t *socket) {
Expand Down
16 changes: 11 additions & 5 deletions extmod/modselect.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ STATIC mp_uint_t poll_set_poll_once(poll_set_t *poll_set, size_t *rwx_num) {

STATIC mp_uint_t poll_set_poll_until_ready_or_timeout(poll_set_t *poll_set, size_t *rwx_num, mp_uint_t timeout) {
mp_uint_t start_ticks = mp_hal_ticks_ms();
bool has_timeout = timeout != (mp_uint_t)-1;

#if MICROPY_PY_SELECT_POSIX_OPTIMISATIONS

Expand Down Expand Up @@ -350,23 +351,28 @@ STATIC mp_uint_t poll_set_poll_until_ready_or_timeout(poll_set_t *poll_set, size
}

// Return if an object is ready, or if the timeout expired.
if (n_ready > 0 || (timeout != (mp_uint_t)-1 && mp_hal_ticks_ms() - start_ticks >= timeout)) {
if (n_ready > 0 || (has_timeout && mp_hal_ticks_ms() - start_ticks >= timeout)) {
return n_ready;
}

// This would be MICROPY_EVENT_POLL_HOOK but the call to poll() above already includes a delay.
mp_handle_pending(true);
// This would be mp_event_wait_ms() but the call to poll() above already includes a delay.
mp_event_handle_nowait();
}

#else

for (;;) {
// poll the objects
mp_uint_t n_ready = poll_set_poll_once(poll_set, rwx_num);
if (n_ready > 0 || (timeout != (mp_uint_t)-1 && mp_hal_ticks_ms() - start_ticks >= timeout)) {
uint32_t elapsed = mp_hal_ticks_ms() - start_ticks;
if (n_ready > 0 || (has_timeout && elapsed >= timeout)) {
return n_ready;
}
MICROPY_EVENT_POLL_HOOK
if (has_timeout) {
mp_event_wait_ms(timeout - elapsed);
} else {
mp_event_wait_indefinite();
}
}

#endif
Expand Down
4 changes: 1 addition & 3 deletions extmod/modssl_mbedtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,7 @@ STATIC mp_obj_t ssl_socket_make_new(mp_obj_ssl_context_t *ssl_context, mp_obj_t
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
goto cleanup;
}
#ifdef MICROPY_EVENT_POLL_HOOK
MICROPY_EVENT_POLL_HOOK
#endif
mp_event_wait_ms(1);
}
}

Expand Down
9 changes: 7 additions & 2 deletions extmod/network_cyw43.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,13 @@ STATIC mp_obj_t network_cyw43_scan(size_t n_args, const mp_obj_t *pos_args, mp_m

// Wait for scan to finish, with a 10s timeout
uint32_t start = mp_hal_ticks_ms();
while (cyw43_wifi_scan_active(self->cyw) && mp_hal_ticks_ms() - start < 10000) {
MICROPY_EVENT_POLL_HOOK
const uint32_t TIMEOUT = 10000;
while (cyw43_wifi_scan_active(self->cyw)) {
uint32_t elapsed = mp_hal_ticks_ms() - start;
if (elapsed >= TIMEOUT) {
break;
}
mp_event_wait_ms(TIMEOUT - elapsed);
}

return res;
Expand Down
7 changes: 4 additions & 3 deletions extmod/nimble/modbluetooth_nimble.c
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ void mp_bluetooth_nimble_port_shutdown(void) {
ble_hs_stop(&ble_hs_shutdown_stop_listener, ble_hs_shutdown_stop_cb, NULL);

while (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) {
MICROPY_EVENT_POLL_HOOK
mp_event_wait_indefinite();
}
}

Expand Down Expand Up @@ -636,10 +636,11 @@ int mp_bluetooth_init(void) {
// On non-ringbuffer builds (NimBLE on STM32/Unix) this will also poll the UART and run the event queue.
mp_uint_t timeout_start_ticks_ms = mp_hal_ticks_ms();
while (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE) {
if (mp_hal_ticks_ms() - timeout_start_ticks_ms > NIMBLE_STARTUP_TIMEOUT) {
uint32_t elapsed = mp_hal_ticks_ms() - timeout_start_ticks_ms;
if (elapsed > NIMBLE_STARTUP_TIMEOUT) {
break;
}
MICROPY_EVENT_POLL_HOOK
mp_event_wait_ms(NIMBLE_STARTUP_TIMEOUT - elapsed);
}

if (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE) {
Expand Down
7 changes: 1 addition & 6 deletions ports/esp8266/esp_mphal.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void mp_hal_init(void) {
void MP_FASTCODE(mp_hal_delay_us)(uint32_t us) {
uint32_t start = system_get_time();
while (system_get_time() - start < us) {
ets_event_poll();
mp_event_handle_nowait();
}
}

Expand Down Expand Up @@ -122,11 +122,6 @@ uint64_t mp_hal_time_ns(void) {
return pyb_rtc_get_us_since_epoch() * 1000ULL;
}

void ets_event_poll(void) {
ets_loop_iter();
mp_handle_pending(true);
}

void __assert_func(const char *file, int line, const char *func, const char *expr) {
printf("assert:%s:%d:%s: %s\n", file, line, func, expr);
mp_raise_msg(&mp_type_AssertionError, MP_ERROR_TEXT("C-level assert"));
Expand Down
3 changes: 0 additions & 3 deletions ports/esp8266/esp_mphal.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ void dupterm_task_init();
uint32_t esp_disable_irq(void);
void esp_enable_irq(uint32_t state);

void ets_event_poll(void);
#define ETS_POLL_WHILE(cond) { while (cond) ets_event_poll(); }

// needed for machine.I2C
#include "osapi.h"
#define mp_hal_delay_us_fast(us) os_delay_us(us)
Expand Down
2 changes: 1 addition & 1 deletion ports/esp8266/machine_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ STATIC mp_uint_t mp_machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, uint
if (mp_machine_uart_txdone(self)) {
return 0;
}
MICROPY_EVENT_POLL_HOOK
mp_event_wait_ms(1);
} while (system_get_time() < timeout);

*errcode = MP_ETIMEDOUT;
Expand Down
2 changes: 1 addition & 1 deletion ports/esp8266/modespnow.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ static int ringbuf_get_bytes_wait(ringbuf_t *r, uint8_t *data, size_t len, mp_in
int status = 0;
while (((status = ringbuf_get_bytes(r, data, len)) == -1)
&& (timeout_ms < 0 || (mp_uint_t)(mp_hal_ticks_ms() - start) < (mp_uint_t)timeout_ms)) {
MICROPY_EVENT_POLL_HOOK;
mp_event_wait_ms(1);
}
return status;
}
Expand Down
4 changes: 2 additions & 2 deletions ports/esp8266/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ STATIC mp_obj_t mp_machine_unique_id(void) {

STATIC void mp_machine_idle(void) {
asm ("waiti 0");
ets_event_poll(); // handle any events after possibly a long wait (eg feed WDT)
mp_event_handle_nowait(); // handle any events after possibly a long wait (eg feed WDT)
}

STATIC void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
Expand All @@ -106,7 +106,7 @@ STATIC void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
uint32_t wifi_mode = wifi_get_opmode();
uint32_t start = system_get_time();
while (system_get_time() - start <= max_us) {
ets_event_poll();
mp_event_handle_nowait();
if (wifi_mode == NULL_MODE) {
// Can only idle if the wifi is off
asm ("waiti 0");
Expand Down
20 changes: 17 additions & 3 deletions ports/esp8266/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// Board-specific definitions
#include "mpconfigboard.h"

#include <stdbool.h>
#include <stdint.h>

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

#define MICROPY_EVENT_POLL_HOOK {ets_event_poll();}
// No blocking wait-for-event on ESP8266, only non-blocking pump of the "OS" event
// loop
//
// TODO: When TIMEOUT_MS==-1, it may be possible to have MICROPY_INTERNAL_WFE() call the "waiti" instruction.
// See mp_machine_idle() and mp_machine_lightsleep() in esp8266/modmachine.c
//
// Note: We have to scope the declaration of ets_loop_iter() here as there are multiple incompatible
// definitions at compile time between the SDK and axTLS!
#define MICROPY_INTERNAL_WFE(TIMEOUT_MS)
#define MICROPY_INTERNAL_EVENT_HOOK \
do { \
extern bool ets_loop_iter(void); \
ets_loop_iter(); \
} while (0)

#define MICROPY_VM_HOOK_COUNT (10)
#define MICROPY_VM_HOOK_INIT static uint vm_hook_divisor = MICROPY_VM_HOOK_COUNT;
#define MICROPY_VM_HOOK_POLL if (--vm_hook_divisor == 0) { \
vm_hook_divisor = MICROPY_VM_HOOK_COUNT; \
extern void ets_loop_iter(void); \
ets_loop_iter(); \
MICROPY_INTERNAL_EVENT_HOOK; \
}
#define MICROPY_VM_HOOK_LOOP MICROPY_VM_HOOK_POLL
#define MICROPY_VM_HOOK_RETURN MICROPY_VM_HOOK_POLL
Expand Down
3 changes: 2 additions & 1 deletion ports/esp8266/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "c_types.h"
#include "user_interface.h"
#include "py/mphal.h"
#include "py/runtime.h"

// seems that this is missing in the Espressif SDK
#define FUNC_U0RXD 0
Expand Down Expand Up @@ -218,7 +219,7 @@ bool ICACHE_FLASH_ATTR uart_rx_wait(uint32_t timeout_us) {
if (system_get_time() - start >= timeout_us) {
return false; // timeout
}
ets_event_poll();
mp_event_handle_nowait();
}
}

Expand Down
3 changes: 2 additions & 1 deletion ports/rp2/cyw43_configport.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "py/mpconfig.h"
#include "py/mperrno.h"
#include "py/mphal.h"
#include "py/runtime.h"
#include "extmod/modnetwork.h"
#include "pendsv.h"

Expand Down Expand Up @@ -119,6 +120,6 @@ static inline void cyw43_delay_ms(uint32_t ms) {
mp_hal_delay_ms(ms);
}

#define CYW43_EVENT_POLL_HOOK MICROPY_EVENT_POLL_HOOK_FAST
#define CYW43_EVENT_POLL_HOOK mp_event_handle_nowait()

#endif // MICROPY_INCLUDED_RP2_CYW43_CONFIGPORT_H
Loading
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