Skip to content

Commit 36c1e8c

Browse files
authored
Merge pull request #5663 from FoamyGuy/rp2040_nvm_fix
disable interrupts inside of ports raspberrypi common hal
2 parents d486284 + fa37ee6 commit 36c1e8c

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

ports/raspberrypi/common-hal/nvm/ByteArray.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "py/runtime.h"
3333
#include "src/rp2_common/hardware_flash/include/hardware/flash.h"
34+
#include "shared-bindings/microcontroller/__init__.h"
3435

3536
extern uint32_t __flash_binary_start;
3637
static const uint32_t flash_binary_start = (uint32_t)&__flash_binary_start;
@@ -45,13 +46,19 @@ static void write_page(uint32_t page_addr, uint32_t offset, uint32_t len, uint8_
4546
// Write a whole page to flash, buffering it first and then erasing and rewriting it
4647
// since we can only write a whole page at a time.
4748
if (offset == 0 && len == FLASH_PAGE_SIZE) {
49+
// disable interrupts to prevent core hang on rp2040
50+
common_hal_mcu_disable_interrupts();
4851
flash_range_program(RMV_OFFSET(page_addr), bytes, FLASH_PAGE_SIZE);
52+
common_hal_mcu_enable_interrupts();
4953
} else {
5054
uint8_t buffer[FLASH_PAGE_SIZE];
5155
memcpy(buffer, (uint8_t *)page_addr, FLASH_PAGE_SIZE);
5256
memcpy(buffer + offset, bytes, len);
57+
common_hal_mcu_disable_interrupts();
5358
flash_range_program(RMV_OFFSET(page_addr), buffer, FLASH_PAGE_SIZE);
59+
common_hal_mcu_enable_interrupts();
5460
}
61+
5562
}
5663

5764
static void erase_and_write_sector(uint32_t address, uint32_t len, uint8_t *bytes) {
@@ -60,8 +67,11 @@ static void erase_and_write_sector(uint32_t address, uint32_t len, uint8_t *byte
6067
uint8_t buffer[FLASH_SECTOR_SIZE];
6168
memcpy(buffer, (uint8_t *)CIRCUITPY_INTERNAL_NVM_START_ADDR, FLASH_SECTOR_SIZE);
6269
memcpy(buffer + address, bytes, len);
70+
// disable interrupts to prevent core hang on rp2040
71+
common_hal_mcu_disable_interrupts();
6372
flash_range_erase(RMV_OFFSET(CIRCUITPY_INTERNAL_NVM_START_ADDR), FLASH_SECTOR_SIZE);
6473
flash_range_program(RMV_OFFSET(CIRCUITPY_INTERNAL_NVM_START_ADDR), buffer, FLASH_SECTOR_SIZE);
74+
common_hal_mcu_enable_interrupts();
6575
}
6676

6777
void common_hal_nvm_bytearray_get_bytes(const nvm_bytearray_obj_t *self,

0 commit comments

Comments
 (0)