Skip to content

Commit 23fb171

Browse files
projectgusdpgeorge
authored andcommitted
rp2/mpnetworkport: Refactor out cyw43_has_pending global variable.
A better indication of whether a cyw43 event is pending is the actual flag in the PendSV handler table. (If this fails, could also use the GPIO interrupt enabled register bit). This commit was needed of a previous version of the fix in the parent commit, but it turned out not strictly necessary for the current version. However, it's still a good clean up. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
1 parent 6fa498c commit 23fb171

File tree

5 files changed

+11
-7
lines changed

5 files changed

+11
-7
lines changed

ports/rp2/cyw43_configport.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,12 @@ uint cyw43_get_pin_wl(cyw43_pin_index_t pin_id);
140140
#endif
141141

142142
void cyw43_post_poll_hook(void);
143-
extern volatile int cyw43_has_pending;
143+
static inline bool cyw43_poll_is_pending(void) {
144+
return pendsv_is_pending(PENDSV_DISPATCH_CYW43);
145+
}
144146

145147
static inline void cyw43_yield(void) {
146-
if (!cyw43_has_pending) {
148+
if (!cyw43_poll_is_pending()) {
147149
best_effort_wfe_or_timeout(make_timeout_time_ms(1));
148150
}
149151
}

ports/rp2/modmachine.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
145145

146146
uint32_t my_interrupts = MICROPY_BEGIN_ATOMIC_SECTION();
147147
#if MICROPY_PY_NETWORK_CYW43
148-
if (cyw43_has_pending && cyw43_poll != NULL) {
148+
if (cyw43_poll_is_pending()) {
149149
MICROPY_END_ATOMIC_SECTION(my_interrupts);
150150
return;
151151
}

ports/rp2/mpnetworkport.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ static soft_timer_entry_t mp_network_soft_timer;
5757
#define CYW43_IRQ_LEVEL GPIO_IRQ_LEVEL_HIGH
5858
#define CYW43_SHARED_IRQ_HANDLER_PRIORITY PICO_SHARED_IRQ_HANDLER_HIGHEST_ORDER_PRIORITY
5959

60-
volatile int cyw43_has_pending = 0;
6160

6261
// The Pico SDK only lets us set GPIO wake on the current running CPU, but the
6362
// hardware doesn't have this limit. We need to always enable/disable the pin
@@ -89,9 +88,8 @@ static void gpio_irq_handler(void) {
8988
// cyw43_poll(). It is re-enabled in cyw43_post_poll_hook(), implemented
9089
// below.
9190
gpio_set_cpu0_host_wake_irq_enabled(false);
92-
cyw43_has_pending = 1;
93-
__sev();
9491
pendsv_schedule_dispatch(PENDSV_DISPATCH_CYW43, cyw43_poll);
92+
__sev();
9593
CYW43_STAT_INC(IRQ_COUNT);
9694
}
9795
}
@@ -106,7 +104,6 @@ void cyw43_irq_init(void) {
106104

107105
// This hook will run on whichever CPU serviced the PendSV interrupt
108106
void cyw43_post_poll_hook(void) {
109-
cyw43_has_pending = 0;
110107
gpio_set_cpu0_host_wake_irq_enabled(true);
111108
}
112109

ports/rp2/pendsv.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ static inline int pendsv_suspend_count(void) {
9999

100100
#endif
101101

102+
bool pendsv_is_pending(size_t slot) {
103+
return pendsv_dispatch_table[slot] != NULL;
104+
}
105+
102106
static inline void pendsv_resume_run_dispatch(void) {
103107
// Run pendsv if needed. Find an entry with a dispatch and call pendsv dispatch
104108
// with it. If pendsv runs it will service all slots.

ports/rp2/pendsv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ void pendsv_init(void);
5151
void pendsv_suspend(void);
5252
void pendsv_resume(void);
5353
void pendsv_schedule_dispatch(size_t slot, pendsv_dispatch_t f);
54+
bool pendsv_is_pending(size_t slot);
5455

5556
#endif // MICROPY_INCLUDED_RP2_PENDSV_H

0 commit comments

Comments
 (0)