From df3948d3c23e3572c49d18ede03bf3ac97ee601c Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Thu, 30 Nov 2023 14:32:41 +1100 Subject: [PATCH] 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 --- extmod/btstack/modbluetooth_btstack.c | 8 ++++---- extmod/machine_i2c.c | 6 +----- extmod/modlwip.c | 6 +----- extmod/modselect.c | 16 +++++++++++----- extmod/modssl_mbedtls.c | 4 +--- extmod/network_cyw43.c | 9 +++++++-- extmod/nimble/modbluetooth_nimble.c | 7 ++++--- 7 files changed, 29 insertions(+), 27 deletions(-) diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index 0c15e93431..211214768f 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -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); diff --git a/extmod/machine_i2c.c b/extmod/machine_i2c.c index 6cc2aa5bbf..1964284ec8 100644 --- a/extmod/machine_i2c.c +++ b/extmod/machine_i2c.c @@ -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; } diff --git a/extmod/modlwip.c b/extmod/modlwip.c index 432b97e8fe..6f8703d5a7 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -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) { diff --git a/extmod/modselect.c b/extmod/modselect.c index d665a10827..07ef3d79c8 100644 --- a/extmod/modselect.c +++ b/extmod/modselect.c @@ -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 diff --git a/extmod/modssl_mbedtls.c b/extmod/modssl_mbedtls.c index 449952594c..1d7705c17f 100644 --- a/extmod/modssl_mbedtls.c +++ b/extmod/modssl_mbedtls.c @@ -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); } } diff --git a/extmod/network_cyw43.c b/extmod/network_cyw43.c index 834f09eae9..f8490d6b9a 100644 --- a/extmod/network_cyw43.c +++ b/extmod/network_cyw43.c @@ -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; diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index 3cdf4d1a78..2e1faa9b10 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -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) {