Skip to content

Commit 5e7c132

Browse files
committed
disable interrupts inside of write_page and erase_write_sector
1 parent f49271b commit 5e7c132

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ uint32_t common_hal_nvm_bytearray_get_length(const nvm_bytearray_obj_t *self) {
4343
}
4444

4545
static void write_page(uint32_t page_addr, uint32_t offset, uint32_t len, uint8_t *bytes) {
46+
// disable interrupts to prevent core hang on rp2040
47+
common_hal_mcu_disable_interrupts();
48+
4649
// Write a whole page to flash, buffering it first and then erasing and rewriting it
4750
// since we can only write a whole page at a time.
4851
if (offset == 0 && len == FLASH_PAGE_SIZE) {
@@ -53,16 +56,21 @@ static void write_page(uint32_t page_addr, uint32_t offset, uint32_t len, uint8_
5356
memcpy(buffer + offset, bytes, len);
5457
flash_range_program(RMV_OFFSET(page_addr), buffer, FLASH_PAGE_SIZE);
5558
}
59+
common_hal_mcu_enable_interrupts();
5660
}
5761

5862
static void erase_and_write_sector(uint32_t address, uint32_t len, uint8_t *bytes) {
63+
// disable interrupts to prevent core hang on rp2040
64+
common_hal_mcu_disable_interrupts();
65+
5966
// Write a whole sector to flash, buffering it first and then erasing and rewriting it
6067
// since we can only erase a whole sector at a time.
6168
uint8_t buffer[FLASH_SECTOR_SIZE];
6269
memcpy(buffer, (uint8_t *)CIRCUITPY_INTERNAL_NVM_START_ADDR, FLASH_SECTOR_SIZE);
6370
memcpy(buffer + address, bytes, len);
6471
flash_range_erase(RMV_OFFSET(CIRCUITPY_INTERNAL_NVM_START_ADDR), FLASH_SECTOR_SIZE);
6572
flash_range_program(RMV_OFFSET(CIRCUITPY_INTERNAL_NVM_START_ADDR), buffer, FLASH_SECTOR_SIZE);
73+
common_hal_mcu_enable_interrupts();
6674
}
6775

6876
void common_hal_nvm_bytearray_get_bytes(const nvm_bytearray_obj_t *self,
@@ -72,8 +80,6 @@ void common_hal_nvm_bytearray_get_bytes(const nvm_bytearray_obj_t *self,
7280

7381
bool common_hal_nvm_bytearray_set_bytes(const nvm_bytearray_obj_t *self,
7482
uint32_t start_index, uint8_t *values, uint32_t len) {
75-
// disable interrupts to prevent core hang on rp2040
76-
common_hal_mcu_disable_interrupts();
7783
uint8_t values_in[len];
7884
common_hal_nvm_bytearray_get_bytes(self, start_index, len, values_in);
7985

@@ -102,6 +108,5 @@ bool common_hal_nvm_bytearray_set_bytes(const nvm_bytearray_obj_t *self,
102108
erase_and_write_sector(start_index, len, values);
103109
}
104110

105-
common_hal_mcu_enable_interrupts();
106111
return true;
107112
}

0 commit comments

Comments
 (0)