40
40
static uint8_t _cache [SECTOR_SIZE ];
41
41
static uint32_t _cache_lba = NO_CACHE ;
42
42
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 ;
47
45
#endif
48
46
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
-
72
47
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
74
54
}
75
55
76
56
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 ();
78
63
}
79
64
80
65
void supervisor_flash_init (void ) {
@@ -91,11 +76,9 @@ void supervisor_flash_init(void) {
91
76
// Read the RDID register to get the flash capacity.
92
77
uint8_t cmd [] = {0x9f , 0 , 0 , 0 };
93
78
uint8_t data [4 ];
94
- common_hal_mcu_disable_interrupts ();
95
79
supervisor_flash_pre_write ();
96
80
flash_do_cmd (cmd , data , 4 );
97
81
supervisor_flash_post_write ();
98
- common_hal_mcu_enable_interrupts ();
99
82
uint8_t power_of_two = FLASH_DEFAULT_POWER_OF_TWO ;
100
83
// Flash must be at least 2MB (1 << 21) because we use the first 1MB for the
101
84
// CircuitPython core. We validate the range because Adesto Tech flash chips
@@ -119,21 +102,11 @@ void port_internal_flash_flush(void) {
119
102
if (_cache_lba == NO_CACHE ) {
120
103
return ;
121
104
}
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
128
105
supervisor_flash_pre_write ();
129
106
flash_range_erase (CIRCUITPY_CIRCUITPY_DRIVE_START_ADDR + _cache_lba , SECTOR_SIZE );
130
107
flash_range_program (CIRCUITPY_CIRCUITPY_DRIVE_START_ADDR + _cache_lba , _cache , SECTOR_SIZE );
131
- supervisor_flash_post_write ();
132
108
_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 ();
137
110
}
138
111
139
112
mp_uint_t supervisor_flash_read_blocks (uint8_t * dest , uint32_t block , uint32_t num_blocks ) {
0 commit comments