Skip to content

Commit 97cc6f4

Browse files
committed
Factor enable/disable interrupts into supervisor_flash_pre_write and supervisor_flash_post_write.
Remove redundant xip cache clean, psram state preservation.
1 parent e8fc44a commit 97cc6f4

File tree

2 files changed

+15
-47
lines changed

2 files changed

+15
-47
lines changed

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,16 @@ static void write_page(uint32_t page_addr, uint32_t offset, uint32_t len, uint8_
2727
// Write a whole page to flash, buffering it first and then erasing and rewriting it
2828
// since we can only write a whole page at a time.
2929
if (offset == 0 && len == FLASH_PAGE_SIZE) {
30-
// disable interrupts to prevent core hang on rp2040
31-
common_hal_mcu_disable_interrupts();
3230
supervisor_flash_pre_write();
3331
flash_range_program(RMV_OFFSET(page_addr), bytes, FLASH_PAGE_SIZE);
3432
supervisor_flash_post_write();
35-
common_hal_mcu_enable_interrupts();
3633
} else {
3734
uint8_t buffer[FLASH_PAGE_SIZE];
3835
memcpy(buffer, (uint8_t *)page_addr, FLASH_PAGE_SIZE);
3936
memcpy(buffer + offset, bytes, len);
40-
common_hal_mcu_disable_interrupts();
4137
supervisor_flash_pre_write();
4238
flash_range_program(RMV_OFFSET(page_addr), buffer, FLASH_PAGE_SIZE);
4339
supervisor_flash_post_write();
44-
common_hal_mcu_enable_interrupts();
4540
}
4641

4742
}

ports/raspberrypi/supervisor/internal_flash.c

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,41 +40,26 @@
4040
static uint8_t _cache[SECTOR_SIZE];
4141
static uint32_t _cache_lba = NO_CACHE;
4242
static uint32_t _flash_size = 0;
43-
44-
#ifdef PICO_RP2350
45-
static uint32_t m1_rfmt;
46-
static uint32_t m1_timing;
43+
#if CIRCUITPY_AUDIOCORE
44+
static uint32_t _audio_channel_mask;
4745
#endif
4846

49-
static void __no_inline_not_in_flash_func(save_psram_settings)(void) {
50-
#ifdef PICO_RP2350
51-
// We're about to invalidate the XIP cache, clean it first to commit any dirty writes to PSRAM
52-
// From https://forums.raspberrypi.com/viewtopic.php?t=378249#p2263677
53-
// Perform clean-by-set/way on all lines
54-
for (uint32_t i = 0; i < 2048; ++i) {
55-
// Use the upper 16k of the maintenance space (0x1bffc000 through 0x1bffffff):
56-
*(volatile uint8_t *)(XIP_SRAM_BASE + (XIP_MAINTENANCE_BASE - XIP_BASE) + i * 8u + 0x1u) = 0;
57-
}
58-
59-
m1_timing = qmi_hw->m[1].timing;
60-
m1_rfmt = qmi_hw->m[1].rfmt;
61-
#endif
62-
}
63-
64-
static void __no_inline_not_in_flash_func(restore_psram_settings)(void) {
65-
#ifdef PICO_RP2350
66-
qmi_hw->m[1].timing = m1_timing;
67-
qmi_hw->m[1].rfmt = m1_rfmt;
68-
__compiler_memory_barrier();
69-
#endif
70-
}
71-
7247
void supervisor_flash_pre_write(void) {
73-
save_psram_settings();
48+
// Disable interrupts. XIP accesses will fault during flash writes.
49+
common_hal_mcu_disable_interrupts();
50+
#if CIRCUITPY_AUDIOCORE
51+
// Pause audio DMA to avoid noise while interrupts are disabled.
52+
_audio_channel_mask = audio_dma_pause_all();
53+
#endif
7454
}
7555

7656
void supervisor_flash_post_write(void) {
77-
restore_psram_settings();
57+
#if CIRCUITPY_AUDIOCORE
58+
// Unpause audio DMA.
59+
audio_dma_unpause_mask(_audio_channel_mask);
60+
#endif
61+
// Re-enable interrupts.
62+
common_hal_mcu_enable_interrupts();
7863
}
7964

8065
void supervisor_flash_init(void) {
@@ -91,11 +76,9 @@ void supervisor_flash_init(void) {
9176
// Read the RDID register to get the flash capacity.
9277
uint8_t cmd[] = {0x9f, 0, 0, 0};
9378
uint8_t data[4];
94-
common_hal_mcu_disable_interrupts();
9579
supervisor_flash_pre_write();
9680
flash_do_cmd(cmd, data, 4);
9781
supervisor_flash_post_write();
98-
common_hal_mcu_enable_interrupts();
9982
uint8_t power_of_two = FLASH_DEFAULT_POWER_OF_TWO;
10083
// Flash must be at least 2MB (1 << 21) because we use the first 1MB for the
10184
// CircuitPython core. We validate the range because Adesto Tech flash chips
@@ -119,21 +102,11 @@ void port_internal_flash_flush(void) {
119102
if (_cache_lba == NO_CACHE) {
120103
return;
121104
}
122-
// Make sure we don't have an interrupt while we do flash operations.
123-
common_hal_mcu_disable_interrupts();
124-
// and audio DMA must be paused as well
125-
#if CIRCUITPY_AUDIOCORE
126-
uint32_t channel_mask = audio_dma_pause_all();
127-
#endif
128105
supervisor_flash_pre_write();
129106
flash_range_erase(CIRCUITPY_CIRCUITPY_DRIVE_START_ADDR + _cache_lba, SECTOR_SIZE);
130107
flash_range_program(CIRCUITPY_CIRCUITPY_DRIVE_START_ADDR + _cache_lba, _cache, SECTOR_SIZE);
131-
supervisor_flash_post_write();
132108
_cache_lba = NO_CACHE;
133-
#if CIRCUITPY_AUDIOCORE
134-
audio_dma_unpause_mask(channel_mask);
135-
#endif
136-
common_hal_mcu_enable_interrupts();
109+
supervisor_flash_post_write();
137110
}
138111

139112
mp_uint_t supervisor_flash_read_blocks(uint8_t *dest, uint32_t block, uint32_t num_blocks) {

0 commit comments

Comments
 (0)