Skip to content

Commit 763846a

Browse files
Additional multicore fixes, BOOTSEL and PIO (earlephilhower#123)
BOOTSEL needs to be multicore protected, too. Reading BOOTSEL required disabling the flash interface, so the other core needs to be idles while this runs. Make the PIO program object multicore safe, too, so that if both cores try to load a program they won't step on each other.
1 parent 2da190f commit 763846a

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

cores/rp2040/Bootsel.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ static bool __no_inline_not_in_flash_func(get_bootsel_button)() {
2626

2727
// Must disable interrupts, as interrupt handlers may be in flash, and we
2828
// are about to temporarily disable flash access!
29-
uint32_t flags = save_and_disable_interrupts();
29+
noInterrupts();
30+
rp2040.idleOtherCore();
3031

3132
// Set chip select to Hi-Z
3233
hw_write_masked(&ioqspi_hw->io[CS_PIN_INDEX].ctrl,
@@ -46,7 +47,8 @@ static bool __no_inline_not_in_flash_func(get_bootsel_button)() {
4647
GPIO_OVERRIDE_NORMAL << IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_LSB,
4748
IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_BITS);
4849

49-
restore_interrupts(flags);
50+
interrupts();
51+
rp2040.resumeOtherCore();
5052

5153
return button_state;
5254
}

cores/rp2040/RP2040.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <hardware/pio.h>
2424
#include <pico/multicore.h>
2525
#include <pico/util/queue.h>
26+
#include <CoreMutex.h>
2627

2728
class _MFIFO {
2829
public:
@@ -132,14 +133,15 @@ class RP2040 {
132133
extern RP2040 rp2040;
133134

134135
// Wrapper class for PIO programs, abstracting common operations out
135-
// TODO - Make dualcore safe
136136
// TODO - Add unload/destructor
137137
class PIOProgram {
138138
public:
139139
PIOProgram(const pio_program_t *pgm) { _pgm = pgm; }
140140

141141
// Possibly load into a PIO and allocate a SM
142142
bool prepare(PIO *pio, int *sm, int *offset) {
143+
extern mutex_t _pioMutex;
144+
CoreMutex m(&_pioMutex);
143145
// Is there an open slot to run in, first?
144146
if (!_findFreeSM(pio, sm)) return false;
145147
// Is it loaded on that PIO?

cores/rp2040/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ RP2040 rp2040;
2626

2727
volatile bool _MFIFO::_otherIdled = false;
2828

29+
auto_init_mutex(_pioMutex);
30+
31+
2932
extern void setup();
3033
extern void loop();
3134

0 commit comments

Comments
 (0)