Skip to content

Commit 35d4d2d

Browse files
projectgusdpgeorge
authored andcommitted
rp2/pendsv: Account for PendSV running on both cores, and without CYW43.
Changes: - Move setting of PendSV priority to pendsv_init(). - Call pendsv_init() from CPU1 as well, to ensure priority is the same. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
1 parent 23fb171 commit 35d4d2d

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

ports/rp2/mpnetworkport.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ static soft_timer_entry_t mp_network_soft_timer;
4242
#if MICROPY_PY_NETWORK_CYW43
4343
#include "lib/cyw43-driver/src/cyw43.h"
4444
#include "lib/cyw43-driver/src/cyw43_stats.h"
45-
#include "hardware/irq.h"
4645

4746
#if !defined(__riscv)
4847
#if PICO_RP2040
@@ -97,9 +96,6 @@ static void gpio_irq_handler(void) {
9796
void cyw43_irq_init(void) {
9897
gpio_add_raw_irq_handler_with_order_priority(CYW43_PIN_WL_HOST_WAKE, gpio_irq_handler, CYW43_SHARED_IRQ_HANDLER_PRIORITY);
9998
irq_set_enabled(IO_IRQ_BANK0, true);
100-
#if !defined(__riscv)
101-
NVIC_SetPriority(PendSV_IRQn, IRQ_PRI_PENDSV);
102-
#endif
10399
}
104100

105101
// This hook will run on whichever CPU serviced the PendSV interrupt

ports/rp2/mpthreadport.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ static void core1_entry_wrapper(void) {
106106
// Allow MICROPY_BEGIN_ATOMIC_SECTION to be invoked from core0.
107107
multicore_lockout_victim_init();
108108

109+
// Set PendSV interrupt priority correctly for CPU1
110+
pendsv_init();
111+
109112
if (core1_entry) {
110113
core1_entry(core1_arg);
111114
}

ports/rp2/pendsv.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "py/mpconfig.h"
2929
#include "py/mpthread.h"
3030
#include "pendsv.h"
31+
#include "hardware/irq.h"
3132

3233
#if PICO_RP2040
3334
#include "RP2040.h"
@@ -45,6 +46,9 @@ static pendsv_dispatch_t pendsv_dispatch_table[PENDSV_DISPATCH_NUM_SLOTS];
4546

4647
static inline void pendsv_resume_run_dispatch(void);
4748

49+
// PendSV IRQ priority, to run system-level tasks that preempt the main thread.
50+
#define IRQ_PRI_PENDSV PICO_LOWEST_IRQ_PRIORITY
51+
4852
void PendSV_Handler(void);
4953

5054
#if MICROPY_PY_THREAD
@@ -53,8 +57,14 @@ void PendSV_Handler(void);
5357
// loop of mp_wfe_or_timeout(), where we don't want the CPU event bit to be set.
5458
static mp_thread_recursive_mutex_t pendsv_mutex;
5559

60+
// Called from CPU0 during boot, but may be called later when CPU1 wakes up
5661
void pendsv_init(void) {
57-
mp_thread_recursive_mutex_init(&pendsv_mutex);
62+
if (get_core_num() == 0) {
63+
mp_thread_recursive_mutex_init(&pendsv_mutex);
64+
}
65+
#if !defined(__riscv)
66+
NVIC_SetPriority(PendSV_IRQn, IRQ_PRI_PENDSV);
67+
#endif
5868
}
5969

6070
void pendsv_suspend(void) {
@@ -117,11 +127,13 @@ static inline void pendsv_resume_run_dispatch(void) {
117127

118128
void pendsv_schedule_dispatch(size_t slot, pendsv_dispatch_t f) {
119129
pendsv_dispatch_table[slot] = f;
130+
// There is a race here where other core calls pendsv_suspend() before ISR
131+
// can execute so this check fails, but dispatch will happen later when
132+
// other core calls pendsv_resume().
120133
if (pendsv_suspend_count() == 0) {
121134
#if PICO_ARM
122-
// There is a race here where other core calls pendsv_suspend() before
123-
// ISR can execute, but dispatch will happen later when other core
124-
// calls pendsv_resume().
135+
// Note this register is part of each CPU core, so setting it on CPUx
136+
// will set the IRQ and run PendSV_Handler on CPUx only.
125137
SCB->ICSR = SCB_ICSR_PENDSVSET_Msk;
126138
#elif PICO_RISCV
127139
struct timespec ts;
@@ -136,15 +148,19 @@ void pendsv_schedule_dispatch(size_t slot, pendsv_dispatch_t f) {
136148
}
137149

138150
// PendSV interrupt handler to perform background processing.
151+
//
152+
// Handler can execute on either CPU if MICROPY_PY_THREAD is set (no code on
153+
// CPU1 calls pendsv_schedule_dispatch(), but CPU1 can call pendsv_resume()
154+
// which will trigger it).
139155
void PendSV_Handler(void) {
140156

141157
#if MICROPY_PY_THREAD
142158
if (!mp_thread_recursive_mutex_lock(&pendsv_mutex, 0)) {
143-
// Failure here means core 1 holds pendsv_mutex. ISR will
144-
// run again after core 1 calls pendsv_resume().
159+
// Failure here means other core holds pendsv_mutex. ISR will
160+
// run again after that core calls pendsv_resume().
145161
return;
146162
}
147-
// Core 0 should not already have locked pendsv_mutex
163+
// This core should not already have locked pendsv_mutex
148164
assert(pendsv_mutex.mutex.enter_count == 1);
149165
#else
150166
assert(pendsv_suspend_count() == 0);

ports/rp2/pendsv.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ enum {
4242

4343
#define PENDSV_DISPATCH_NUM_SLOTS PENDSV_DISPATCH_MAX
4444

45-
// PendSV IRQ priority, to run system-level tasks that preempt the main thread.
46-
#define IRQ_PRI_PENDSV PICO_LOWEST_IRQ_PRIORITY
47-
4845
typedef void (*pendsv_dispatch_t)(void);
4946

5047
void pendsv_init(void);

0 commit comments

Comments
 (0)