Skip to content

Commit

Permalink
Merge pull request #150 from adafruit/pb-rp2040-idle-select
Browse files Browse the repository at this point in the history
RP2040: add core-idle flag to constructor
  • Loading branch information
hathach authored Jun 16, 2023
2 parents 8a1558b + 85e5779 commit 8474944
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Adafruit SPIFlash
version=4.1.3
version=4.1.4
author=Adafruit
maintainer=Adafruit <[email protected]>
sentence=SPI Flash filesystem support for FAT and CircuitPython FS support from within Arduino
Expand Down
35 changes: 21 additions & 14 deletions src/rp2040/Adafruit_FlashTransport_RP2040.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,34 @@ extern uint8_t _FS_end;
#define MENU_FS_SIZE ((uint32_t)(&_FS_end - &_FS_start))

// CircuitPython partition scheme with
// - start address = 1 MB,
// - start address = 1 MB (Pico), 1.5 MB (Pico W)
// - size = total flash - 1 MB + 4KB (since CPY does not reserve EEPROM from
// arduino core)
#if defined(ARDUINO_RASPBERRY_PI_PICO_W)
const uint32_t Adafruit_FlashTransport_RP2040::CPY_START_ADDR = (1536 * 1024);
#else
const uint32_t Adafruit_FlashTransport_RP2040::CPY_START_ADDR =
(1 * 1024 * 1024);
#endif
const uint32_t Adafruit_FlashTransport_RP2040::CPY_SIZE =
(((uint32_t)&_FS_end) -
(XIP_BASE + Adafruit_FlashTransport_RP2040::CPY_START_ADDR) + 4096);

static inline void fl_lock(void) {
static inline void fl_lock(bool idle) {
noInterrupts();
rp2040.idleOtherCore();
if (idle)
rp2040.idleOtherCore();
}

static inline void fl_unlock(void) {
rp2040.resumeOtherCore();
static inline void fl_unlock(bool idle) {
if (idle)
rp2040.resumeOtherCore();
interrupts();
}

Adafruit_FlashTransport_RP2040::Adafruit_FlashTransport_RP2040(
uint32_t start_addr, uint32_t size) {
uint32_t start_addr, uint32_t size, bool idle)
: _idle_other_core_on_write(idle) {
_cmd_read = SFLASH_CMD_READ;
_addr_len = 3; // work with most device if not set

Expand Down Expand Up @@ -88,9 +95,9 @@ void Adafruit_FlashTransport_RP2040::begin(void) {
0,
};
uint8_t data[4];
fl_lock();
fl_lock(_idle_other_core_on_write);
flash_do_cmd(cmd, data, 5);
fl_unlock();
fl_unlock(_idle_other_core_on_write);

uint8_t *jedec_ids = data + 1;

Expand Down Expand Up @@ -128,9 +135,9 @@ bool Adafruit_FlashTransport_RP2040::runCommand(uint8_t command) {

switch (command) {
case SFLASH_CMD_ERASE_CHIP:
fl_lock();
fl_lock(_idle_other_core_on_write);
flash_range_erase(_start_addr, _size);
fl_unlock();
fl_unlock(_idle_other_core_on_write);
break;

// do nothing, mostly write enable
Expand Down Expand Up @@ -178,9 +185,9 @@ bool Adafruit_FlashTransport_RP2040::eraseCommand(uint8_t command,
return false;
}

fl_lock();
fl_lock(_idle_other_core_on_write);
flash_range_erase(_start_addr + addr, erase_sz);
fl_unlock();
fl_unlock(_idle_other_core_on_write);

return true;
}
Expand All @@ -202,9 +209,9 @@ bool Adafruit_FlashTransport_RP2040::writeMemory(uint32_t addr,
return false;
}

fl_lock();
fl_lock(_idle_other_core_on_write);
flash_range_program(_start_addr + addr, data, len);
fl_unlock();
fl_unlock(_idle_other_core_on_write);
return true;
}

Expand Down
14 changes: 11 additions & 3 deletions src/rp2040/Adafruit_FlashTransport_RP2040.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class Adafruit_FlashTransport_RP2040 : public Adafruit_FlashTransport {
// check if relative addr is valid
bool check_addr(uint32_t addr) { return addr <= _size; }

// Flag (set via constructor) indicates whether other core must pause when
// writing or erasing flash. Default state is true, pause other core. VERY
// rare that this needs changed, REQUIRES SPECIAL LINKER CONFIG to locate
// ALL functions of other core entirely in RAM, else hard crash and flash
// filesystem corruption. PicoDVI is a rare use case.
bool _idle_other_core_on_write;

public:
static const uint32_t CPY_START_ADDR;
static const uint32_t CPY_SIZE;
Expand All @@ -49,7 +56,8 @@ class Adafruit_FlashTransport_RP2040 : public Adafruit_FlashTransport {
// To be compatible with CircuitPython partition scheme (start_address = 1
// MB, size = total flash - 1 MB) use
// Adafruit_FlashTransport_RP2040(CPY_START_ADDR, CPY_SIZE)
Adafruit_FlashTransport_RP2040(uint32_t start_addr = 0, uint32_t size = 0);
Adafruit_FlashTransport_RP2040(uint32_t start_addr = 0, uint32_t size = 0,
bool idle = true);

virtual void begin(void);
virtual void end(void);
Expand All @@ -75,8 +83,8 @@ class Adafruit_FlashTransport_RP2040_CPY
: public Adafruit_FlashTransport_RP2040 {

public:
Adafruit_FlashTransport_RP2040_CPY(void)
: Adafruit_FlashTransport_RP2040(CPY_START_ADDR, CPY_SIZE) {}
Adafruit_FlashTransport_RP2040_CPY(bool idle = true)
: Adafruit_FlashTransport_RP2040(CPY_START_ADDR, CPY_SIZE, idle) {}
};

#endif

0 comments on commit 8474944

Please sign in to comment.