@@ -234,7 +234,7 @@ void supervisor_flash_flush(void) {
234
234
}
235
235
}
236
236
237
- static int32_t convert_block_to_flash_addr (uint32_t block ) {
237
+ static uint32_t convert_block_to_flash_addr (uint32_t block ) {
238
238
if (0 <= block && block < INTERNAL_FLASH_FILESYSTEM_NUM_BLOCKS ) {
239
239
// a block in partition 1
240
240
return INTERNAL_FLASH_FILESYSTEM_START_ADDR + block * FILESYSTEM_BLOCK_SIZE ;
@@ -244,15 +244,30 @@ static int32_t convert_block_to_flash_addr(uint32_t block) {
244
244
}
245
245
246
246
mp_uint_t supervisor_flash_read_blocks (uint8_t * dest , uint32_t block , uint32_t num_blocks ) {
247
- // Must write out anything in cache before trying to read.
248
- supervisor_flash_flush ();
249
-
250
247
int32_t src = convert_block_to_flash_addr (block );
251
248
if (src == -1 ) {
252
249
// bad block number
253
250
return false;
254
251
}
255
- memcpy (dest , (uint8_t * ) src , FILESYSTEM_BLOCK_SIZE * num_blocks );
252
+
253
+ // Determine whether the read is contained within the sector
254
+ uint32_t sector_size ;
255
+ uint32_t sector_start_addr ;
256
+ flash_get_sector_info (src , & sector_start_addr , & sector_size );
257
+ // Count how many blocks are left in the sector
258
+ uint32_t count = (sector_size - (src - sector_start_addr ))/FILESYSTEM_BLOCK_SIZE ;
259
+ count = MIN (num_blocks , count );
260
+
261
+ if (count < num_blocks && _cache_flash_addr == sector_start_addr ) {
262
+ // Read is contained in the cache, so just read cache
263
+ memcpy (dest , (_flash_cache + (src - sector_start_addr )), FILESYSTEM_BLOCK_SIZE * num_blocks );
264
+ } else {
265
+ // The read spans multiple sectors or is in another sector
266
+ // Must write out anything in cache before trying to read.
267
+ supervisor_flash_flush ();
268
+ memcpy (dest , (uint8_t * ) src , FILESYSTEM_BLOCK_SIZE * num_blocks );
269
+ }
270
+
256
271
return 0 ; // success
257
272
}
258
273
@@ -271,7 +286,7 @@ mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t block_num,
271
286
uint32_t sector_start_addr ;
272
287
flash_get_sector_info (dest , & sector_start_addr , & sector_size );
273
288
274
- // Fail for any sector outside the 16k ones for now
289
+ // Fail for any sector outside what's supported by the cache
275
290
if (sector_size > sizeof (_flash_cache )) {
276
291
reset_into_safe_mode (FLASH_WRITE_FAIL );
277
292
}
0 commit comments