Skip to content

Commit b2834c3

Browse files
committed
atmel-samd: Support reading from the flash cache.
1 parent 98c8f2f commit b2834c3

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

atmel-samd/spi_flash.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,12 +474,32 @@ bool spi_flash_read_block(uint8_t *dest, uint32_t block) {
474474
return true;
475475
} else {
476476
// Non-MBR block, get data from flash memory.
477-
uint32_t src = convert_block_to_flash_addr(block);
478-
if (src == -1) {
477+
uint32_t address = convert_block_to_flash_addr(block);
478+
if (address == -1) {
479479
// bad block number
480480
return false;
481481
}
482-
return read_flash(src, dest, FLASH_BLOCK_SIZE);
482+
483+
// Mask out the lower bits that designate the address within the sector.
484+
uint32_t this_sector = address & (~(sector_size - 1));
485+
uint8_t block_index = (address / FLASH_BLOCK_SIZE) % (sector_size / FLASH_BLOCK_SIZE);
486+
uint8_t mask = 1 << (block_index);
487+
// We're reading from the currently cached sector.
488+
if (current_sector == this_sector && (mask & dirty_mask) > 0) {
489+
if (ram_cache != NULL) {
490+
uint8_t pages_per_block = FLASH_BLOCK_SIZE / page_size;
491+
for (int i = 0; i < pages_per_block; i++) {
492+
memcpy(dest + i * page_size,
493+
ram_cache[block_index * pages_per_block + i],
494+
page_size);
495+
}
496+
return true;
497+
} else {
498+
uint32_t scratch_address = SCRATCH_SECTOR + block_index * FLASH_BLOCK_SIZE;
499+
return read_flash(scratch_address, dest, FLASH_BLOCK_SIZE);
500+
}
501+
}
502+
return read_flash(address, dest, FLASH_BLOCK_SIZE);
483503
}
484504
}
485505

0 commit comments

Comments
 (0)