8000 rp2: Switch rp2 and drivers to use new event functions. · micropython/micropython@2852935 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2852935

Browse files
projectgusdpgeorge
authored andcommitted
rp2: Switch rp2 and drivers to use new event functions.
This commit changes all uses in the rp2 port, and drivers that are optionally supported by that port. The old MICROPY_EVENT_POLL_HOOK and MICROPY_EVENT_POLL_HOOK_FAST macros are no longer used for rp2 builds and are removed (C user code will need to be changed to suit). Also take the opportunity to change some timeouts that used 64-bit arithmetic to 32-bit, to hopefully claw back a little code size. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent df3948d commit 2852935

File tree

8 files changed

+59
-52
lines changed

8 files changed

+59
-52
lines changed

drivers/cyw43/cywbt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ STATIC int cywbt_hci_cmd_raw(size_t len, uint8_t *buf) {
6868
mp_bluetooth_hci_uart_write((void *)buf, len);
6969
for (int c, i = 0; i < 6; ++i) {
7070
while ((c = mp_bluetooth_hc 10000 i_uart_readchar()) == -1) {
71-
MICROPY_EVENT_POLL_HOOK
71+
mp_event_wait_indefinite();
7272
}
7373
buf[i] = c;
7474
}
@@ -88,7 +88,7 @@ STATIC int cywbt_hci_cmd_raw(size_t len, uint8_t *buf) {
8888
int sz = buf[2] - 3;
8989
for (int c, i = 0; i < sz; ++i) {
9090
while ((c = mp_bluetooth_hci_uart_readchar()) == -1) {
91-
MICROPY_EVENT_POLL_HOOK
91+
mp_event_wait_indefinite();
9292
}
9393
buf[i] = c;
9494
}

drivers/ninaw10/nina_bt_hci.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,13 @@ static int nina_hci_cmd(int ogf, int ocf, size_t param_len, const uint8_t *param
7575
// Receive HCI event packet, initially reading 3 bytes (HCI Event, Event code, Plen).
7676
for (mp_uint_t start = mp_hal_ticks_ms(), size = 3, i = 0; i < size;) {
7777
while (!mp_bluetooth_hci_uart_any()) {
78-
MICROPY_EVENT_POLL_HOOK
78+
mp_uint_t elapsed = mp_hal_ticks_ms() - start;
7979
// Timeout.
80-
if ((mp_hal_ticks_ms() - start) > HCI_COMMAND_TIMEOUT) {
80+
if (elapsed > HCI_COMMAND_TIMEOUT) {
8181
error_printf("timeout waiting for HCI packet\n");
8282
return -1;
8383
}
84+
mp_event_wait_ms(HCI_COMMAND_TIMEOUT - elapsed);
8485
}
8586

8687
buf[i] = mp_bluetooth_hci_uart_readchar();

ports/rp2/cyw43_configport.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "py/mpconfig.h"
3131
#include "py/mperrno.h"
3232
#include "py/mphal.h"
33+
#include "py/runtime.h"
3334
#include "extmod/modnetwork.h"
3435
#include "pendsv.h"
3536

@@ -119,6 +120,6 @@ static inline void cyw43_delay_ms(uint32_t ms) {
119120
mp_hal_delay_ms(ms);
120121
}
121122

122-
#define CYW43_EVENT_POLL_HOOK MICROPY_EVENT_POLL_HOOK_FAST
123+
#define CYW43_EVENT_POLL_HOOK mp_event_handle_nowait()
123124

124125
#endif // MICROPY_INCLUDED_RP2_CYW43_CONFIGPORT_H

ports/rp2/machine_uart.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,8 @@ STATIC void mp_machine_uart_sendbreak(machine_uart_obj_t *self) {
454454

455455
STATIC mp_uint_t mp_machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
456456
machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
457-
uint64_t t = time_us_64() + (uint64_t)self->timeout * 1000;
458-
uint64_t timeout_char_us = (uint64_t)self->timeout_char * 1000;
457+
mp_uint_t start = mp_hal_ticks_ms();
458+
mp_uint_t timeout = self->timeout;
459459
uint8_t *dest = buf_in;
460460

461461
for (size_t i = 0; i < size; i++) {
@@ -466,26 +466,28 @@ STATIC mp_uint_t mp_machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t
466466
uart_drain_rx_fifo(self);
467467
break;
468468
}
469-
if (time_us_64() > t) { // timed out
469+
mp_uint_t elapsed = mp_hal_ticks_ms() - start;
470+
if (elapsed > timeout) { // timed out
470471
if (i <= 0) {
471472
*errcode = MP_EAGAIN;
472473
return MP_STREAM_ERROR;
473474
} else {
474475
return i;
475476
}
476477
}
477-
MICROPY_EVENT_POLL_HOOK
478+
mp_event_wait_ms(timeout - elapsed);
478479
}
479480
*dest++ = ringbuf_get(&(self->read_buffer));
480-
t = time_us_64() + timeout_char_us;
481+
start = mp_hal_ticks_ms(); // Inter-character timeout
482+
timeout = self->timeout_char;
481483
}
482484
return size;
483485
}
484486

485487
STATIC mp_uint_t mp_machine_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) {
486488
machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
487-
uint64_t t = time_us_64() + (uint64_t)self->timeout * 1000;
488-
uint64_t timeout_char_us = (uint64_t)self->timeout_char * 1000;
489+
mp_uint_t start = mp_hal_ticks_ms();
490+
mp_uint_t timeout = self->timeout;
489491
const uint8_t *src = buf_in;
490492
size_t i = 0;
491493

@@ -502,19 +504,21 @@ STATIC mp_uint_t mp_machine_uart_write(mp_obj_t self_in, const void *buf_in, mp_
502504
while (i < size) {
503505
// Wait for the first/next character to be sent.
504506
while (ringbuf_free(&(self->write_buffer)) == 0) {
505-
if (time_us_64() > t) { // timed out
507+
mp_uint_t elapsed = mp_hal_ticks_ms() - start;
508+
if (elapsed > timeout) { // timed out
506509
if (i <= 0) {
507510
*errcode = MP_EAGAIN;
508511
return MP_STREAM_ERROR;
509512
} else {
510513
return i;
511514
}
512515
}
513-
MICROPY_EVENT_POLL_HOOK
516+
mp_event_wait_ms(timeout - elapsed);
514517
}
515518
ringbuf_put(&(self->write_buffer), *src++);
516519
++i;
517-
t = time_us_64() + timeout_char_us;
520+
start = mp_hal_ticks_ms(); // Inter-character timeout
521+
timeout = self->timeout_char;
518522
uart_fill_tx_fifo(self);
519523
}
520524

@@ -539,12 +543,16 @@ STATIC mp_uint_t mp_machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, uint
539543
// Take the worst case assumptions at 13 bit symbol size times 2.
540544
uint64_t timeout = time_us_64() +
541545
(uint64_t)(33 + self->write_buffer.size) * 13000000ll * 2 / self->baudrate;
542-
do {
546+
while (1) {
543547
if (mp_machine_uart_txdone(self)) {
544548
return 0;
545549
}
546-
MICROPY_EVENT_POLL_HOOK
547-
} while (time_us_64() < timeout);
550+
uint64_t now = time_us_64();
551+
if (now >= timeout) {
552+
break;
553+
}
554+
mp_event_wait_ms((timeout - now) / 1000);
555+
}
548556
*errcode = MP_ETIMEDOUT;
549557
ret = MP_STREAM_ERROR;
550558
} else {

ports/rp2/mpconfigport.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -249,18 +249,19 @@ extern const struct _mp_obj_type_t mod_network_nic_type_wiznet5k;
249249
#define MICROPY_PY_LWIP_REENTER lwip_lock_acquire();
250250
#define MICROPY_PY_LWIP_EXIT lwip_lock_release();
251251

252-
#define MICROPY_EVENT_POLL_HOOK_FAST \
253-
do { \
254-
extern void mp_handle_pending(bool); \
255-
mp_handle_pending(true); \
252+
// Port level Wait-for-Event macro
253+
//
254+
// Do not use this macro directly, include py/runtime.h and
255+
// call mp_event_wait_indefinite() or mp_event_wait_ms(timeout)
256+
#define MICROPY_INTERNAL_WFE(TIMEOUT_MS) \
257+
do { \
258+
if ((TIMEOUT_MS) < 0) { \
259+
__wfe(); \
260+
} else { \
261+
best_effort_wfe_or_timeout(make_timeout_time_ms(TIMEOUT_MS)); \
262+
} \
256263
} while (0)
257264

258-
#define MICROPY_EVENT_POLL_HOOK \
259-
do { \
260-
MICROPY_EVENT_POLL_HOOK_FAST; \
261-
__wfe(); \
262-
} while (0);
263-
264265
#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p) | 1))
265266

266267
#define MP_SSIZE_MAX (0x7fffffff)

ports/rp2/mphalport.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,17 @@ ringbuf_t stdin_ringbuf = { stdin_ringbuf_array, sizeof(stdin_ringbuf_array) };
5959

6060
#endif
6161

62-
#if MICROPY_HW_USB_CDC
63-
// Explicitly run the USB stack in case the scheduler is locked (eg we are in an
64-
// interrupt handler) and there is in/out data pending on the USB CDC interface.
65-
#define MICROPY_EVENT_POLL_HOOK_WITH_USB \
66-
do { \
67-
MICROPY_EVENT_POLL_HOOK; \
68-
mp_usbd_task(); \
69-
} while (0)
70-
71-
#else
72-
#define MICROPY_EVENT_POLL_HOOK_WITH_USB MICROPY_EVENT_POLL_HOOK
73-
#endif
74-
7562
#if MICROPY_HW_USB_CDC
7663

7764
uint8_t cdc_itf_pending; // keep track of cdc interfaces which need attention to poll
7865

7966
void poll_cdc_interfaces(void) {
67+
if (!cdc_itf_pending) {
68+
// Explicitly run the USB stack as the scheduler may be locked (eg we are in
69+
// an interrupt handler) while there is data pending.
70+
mp_usbd_task();
71+
}
72+
8073
// any CDC interfaces left to poll?
8174
if (cdc_itf_pending && ringbuf_free(&stdin_ringbuf)) {
8275
for (uint8_t itf = 0; itf < 8; ++itf) {
@@ -153,7 +146,7 @@ int mp_hal_stdin_rx_chr(void) {
2851
153146
return dupterm_c;
154147
}
155148
#endif
156-
MICROPY_EVENT_POLL_HOOK_WITH_USB;
149+
mp_event_wait_indefinite();
157150
}
158151
}
159152

@@ -173,7 +166,11 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
173166
int timeout = 0;
174167
// Wait with a max of USC_CDC_TIMEOUT ms
175168
while (n > tud_cdc_write_available() && timeout++ < MICROPY_HW_USB_CDC_TX_TIMEOUT) {
176-
MICROPY_EVENT_POLL_HOOK_WITH_USB;
169+
mp_event_wait_ms(1);
170+
171+
// Explicitly run the USB stack as the scheduler may be locked (eg we
172+
// are in an interrupt handler), while there is data pending.
173+
mp_usbd_task();
177174
}
178175
if (timeout >= MICROPY_HW_USB_CDC_TX_TIMEOUT) {
179176
break;
@@ -193,7 +190,7 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
193190
void mp_hal_delay_ms(mp_uint_t ms) {
194191
absolute_time_t t = make_timeout_time_ms(ms);
195192
do {
196-
MICROPY_EVENT_POLL_HOOK_FAST;
193+
mp_event_handle_nowait();
197194
} while (!best_effort_wfe_or_timeout(t));
198195
}
199196

ports/rp2/rp2_flash.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,10 @@ STATIC mp_obj_t rp2_flash_readblocks(size_t n_args, const mp_obj_t *args) {
121121
offset += mp_obj_get_int(args[3]);
122122
}
123123
memcpy(bufinfo.buf, (void *)(XIP_BASE + self->flash_base + offset), bufinfo.len);
124-
// MICROPY_EVENT_POLL_HOOK_FAST is called here to avoid a fail in registering
124+
// mp_event_handle_nowait() is called here to avoid a fail in registering
125125
// USB at boot time, if the board is busy loading files or scanning the file
126-
// system. MICROPY_EVENT_POLL_HOOK_FAST calls tud_task(). As the alternative
127-
// tud_task() should be called in the USB IRQ. See discussion in PR #10423.
128-
MICROPY_EVENT_POLL_HOOK_FAST;
126+
// system. mp_event_handle_nowait() will call the TinyUSB task if needed.
127+
mp_event_handle_nowait();
129128
return mp_const_none;
130129
}
131130
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(rp2_flash_readblocks_obj, 3, 4, rp2_flash_readblocks);
@@ -140,7 +139,7 @@ STATIC mp_obj_t rp2_flash_writeblocks(size_t n_args, const mp_obj_t *args) {
140139
mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
141140
flash_range_erase(self->flash_base + offset, bufinfo.len);
142141
MICROPY_END_ATOMIC_SECTION(atomic_state);
143-
MICROPY_EVENT_POLL_HOOK_FAST;
142+
mp_event_handle_nowait();
144143
// TODO check return value
145144
} else {
146145
offset += mp_obj_get_int(args[3]);
@@ -149,7 +148,7 @@ STATIC mp_obj_t rp2_flash_writeblocks(size_t n_args, const mp_obj_t *args) {
149148
mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
150149
flash_range_program(self->flash_base + offset, bufinfo.buf, bufinfo.len);
151150
MICROPY_END_ATOMIC_SECTION(atomic_state);
152-
MICROPY_EVENT_POLL_HOOK_FAST;
151+
mp_event_handle_nowait();
153152
// TODO check return value
154153
return mp_const_none;
155154
}

ports/rp2/rp2_pio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ STATIC mp_obj_t rp2_state_machine_get(size_t n_args, const mp_obj_t *args) {
729729
for (;;) {
730730
while (pio_sm_is_rx_fifo_empty(self->pio, self->sm)) {
731731
// This delay must be fast.
732-
MICROPY_EVENT_POLL_HOOK_FAST;
732+
mp_event_handle_nowait();
733733
}
734734
uint32_t value = pio_sm_get(self->pio, self->sm) >> shift;
735735
if (dest == NULL) {
@@ -787,7 +787,7 @@ STATIC mp_obj_t rp2_state_machine_put(size_t n_args, const mp_obj_t *args) {
787787
}
788788
while (pio_sm_is_tx_fifo_full(self->pio, self->sm)) {
789789
// This delay must be fast.
790-
MICROPY_EVENT_POLL_HOOK_FAST;
790+
mp_event_handle_nowait();
791791
}
792792
pio_sm_put(self->pio, self->sm, value << shift);
793793
}

0 commit comments

Comments
 (0)
0