Skip to content

Commit a388a59

Browse files
committed
rp2040: fix audio glitch at soft-reload
The internal flash cache wasn't being properly used, because `write_blocks` unconditionally performed the flash write. Fixing this so that the write's not done until `internal_flash_flush` fixes the problem in my test program with i2sout & synthio. as a future optimization, `flash_read_blocks` could learn to read out of the cache, but that's probably not super important.
1 parent e23e7d3 commit a388a59

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

ports/raspberrypi/supervisor/internal_flash.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,16 @@ void port_internal_flash_flush(void) {
9595
if (_cache_lba == NO_CACHE) {
9696
return;
9797
}
98+
// Make sure we don't have an interrupt while we do flash operations.
9899
common_hal_mcu_disable_interrupts();
99100
flash_range_erase(CIRCUITPY_CIRCUITPY_DRIVE_START_ADDR + _cache_lba, SECTOR_SIZE);
100101
flash_range_program(CIRCUITPY_CIRCUITPY_DRIVE_START_ADDR + _cache_lba, _cache, SECTOR_SIZE);
101-
common_hal_mcu_enable_interrupts();
102102
_cache_lba = NO_CACHE;
103+
common_hal_mcu_enable_interrupts();
103104
}
104105

105106
mp_uint_t supervisor_flash_read_blocks(uint8_t *dest, uint32_t block, uint32_t num_blocks) {
107+
port_internal_flash_flush(); // we never read out of the cache, so we have to write it if dirty
106108
memcpy(dest,
107109
(void *)(XIP_BASE + CIRCUITPY_CIRCUITPY_DRIVE_START_ADDR + block * FILESYSTEM_BLOCK_SIZE),
108110
num_blocks * FILESYSTEM_BLOCK_SIZE);
@@ -118,6 +120,7 @@ mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t lba, uint32
118120
uint8_t block_offset = block_address % blocks_per_sector;
119121

120122
if (_cache_lba != block_address) {
123+
port_internal_flash_flush();
121124
memcpy(_cache,
122125
(void *)(XIP_BASE + CIRCUITPY_CIRCUITPY_DRIVE_START_ADDR + sector_offset),
123126
SECTOR_SIZE);
@@ -133,11 +136,6 @@ mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t lba, uint32
133136
FILESYSTEM_BLOCK_SIZE);
134137
block++;
135138
}
136-
// Make sure we don't have an interrupt while we do flash operations.
137-
common_hal_mcu_disable_interrupts();
138-
flash_range_erase(CIRCUITPY_CIRCUITPY_DRIVE_START_ADDR + sector_offset, SECTOR_SIZE);
139-
flash_range_program(CIRCUITPY_CIRCUITPY_DRIVE_START_ADDR + sector_offset, _cache, SECTOR_SIZE);
140-
common_hal_mcu_enable_interrupts();
141139
}
142140

143141
return 0; // success

0 commit comments

Comments
 (0)