4040static uint8_t _cache [SECTOR_SIZE ];
4141static uint32_t _cache_lba = NO_CACHE ;
4242static 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-
7247void 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
7656void 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
8065void 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
139112mp_uint_t supervisor_flash_read_blocks (uint8_t * dest , uint32_t block , uint32_t num_blocks ) {
0 commit comments