extmod: Switch to use new event functions.

See previous commit for details of these functions.  As of this commit,
these still call the old hook macros on all ports.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
This commit is contained in:
Angus Gratton 2023-11-30 14:32:41 +11:00 committed by Damien George
parent f5be0128e4
commit df3948d3c2
7 changed files with 29 additions and 27 deletions

View File

@ -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
@ -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");
}
@ -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);
@ -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);

View File

@ -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;
}

View File

@ -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) {

View File

@ -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
@ -350,12 +351,12 @@ 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
@ -363,10 +364,15 @@ STATIC mp_uint_t poll_set_poll_until_ready_or_timeout(poll_set_t *poll_set, size
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

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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();
}
}
@ -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) {