forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add initial storage extension support
- Loading branch information
Showing
1 changed file
with
43 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,36 +42,52 @@ | |
#include "supervisor/flash.h" | ||
#include "supervisor/usb.h" | ||
|
||
STATIC const esp_partition_t *_partition; | ||
STATIC const esp_partition_t *_partition[2]; | ||
|
||
// TODO: Split the caching out of supervisor/shared/external_flash so we can use it. | ||
#define SECTOR_SIZE 4096 | ||
STATIC uint8_t _cache[SECTOR_SIZE]; | ||
STATIC uint32_t _cache_lba = 0xffffffff; | ||
|
||
void supervisor_flash_init(void) { | ||
_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, | ||
_partition[0] = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, | ||
ESP_PARTITION_SUBTYPE_DATA_FAT, | ||
NULL); | ||
_partition[1] = esp_partition_find_first(ESP_PARTITION_TYPE_APP, | ||
This comment has been minimized.
Sorry, something went wrong. |
||
ESP_PARTITION_SUBTYPE_APP_OTA_1, | ||
NULL); | ||
} | ||
|
||
uint32_t supervisor_flash_get_block_size(void) { | ||
return FILESYSTEM_BLOCK_SIZE; | ||
} | ||
|
||
uint32_t supervisor_flash_get_block_count(void) { | ||
return _partition->size / FILESYSTEM_BLOCK_SIZE; | ||
return (_partition[0]->size + _partition[1]->size) / FILESYSTEM_BLOCK_SIZE; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
|
||
void port_internal_flash_flush(void) { | ||
|
||
} | ||
|
||
mp_uint_t supervisor_flash_read_blocks(uint8_t *dest, uint32_t block, uint32_t num_blocks) { | ||
esp_partition_read(_partition, | ||
block * FILESYSTEM_BLOCK_SIZE, | ||
dest, | ||
num_blocks * FILESYSTEM_BLOCK_SIZE); | ||
uint32_t offset = block * FILESYSTEM_BLOCK_SIZE; | ||
uint32_t read_total = num_blocks * FILESYSTEM_BLOCK_SIZE; | ||
|
||
if (offset > _partition[0]->size) { | ||
// only read from partition 1 | ||
esp_partition_read(_partition[1], (offset - _partition[0]->size), dest, read_total); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} else if ((offset + read_total) > _partition[0]->size) { | ||
// first read from partition 0, then partition 1 | ||
uint32_t read_0 = _partition[0]->size - offset; | ||
uint32_t read_1 = read_total - read_0; | ||
esp_partition_read(_partition[0], offset, dest, read_0); | ||
esp_partition_read(_partition[1], 0, (dest + read_0), read_1); | ||
This comment has been minimized.
Sorry, something went wrong.
dhalbert
Collaborator
|
||
} else { | ||
// only read from partition 0 | ||
esp_partition_read(_partition[0], offset, dest, read_total); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
|
@@ -84,12 +100,10 @@ mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t lba, uint32 | |
uint8_t block_offset = block_address % blocks_per_sector; | ||
|
||
if (_cache_lba != block_address) { | ||
esp_partition_read(_partition, | ||
sector_offset, | ||
_cache, | ||
SECTOR_SIZE); | ||
supervisor_flash_read_blocks(_cache, sector_offset / FILESYSTEM_BLOCK_SIZE, blocks_per_sector); | ||
_cache_lba = sector_offset; | ||
} | ||
|
||
for (uint8_t b = block_offset; b < blocks_per_sector; b++) { | ||
// Stop copying after the last block. | ||
if (block >= num_blocks) { | ||
|
@@ -100,11 +114,24 @@ mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t lba, uint32 | |
FILESYSTEM_BLOCK_SIZE); | ||
block++; | ||
} | ||
esp_partition_erase_range(_partition, sector_offset, SECTOR_SIZE); | ||
esp_partition_write(_partition, | ||
sector_offset, | ||
_cache, | ||
SECTOR_SIZE); | ||
|
||
if (sector_offset > _partition[0]->size) { | ||
// only write to partition 1 | ||
esp_partition_erase_range(_partition[1], sector_offset - _partition[0]->size, SECTOR_SIZE); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
esp_partition_write(_partition[1], sector_offset - _partition[0]->size, _cache, SECTOR_SIZE); | ||
} else if ((sector_offset + SECTOR_SIZE) > _partition[0]->size) { | ||
// first write to partition 0, then partition 1 | ||
uint32_t write_0 = _partition[0]->size - sector_offset; | ||
uint32_t write_1 = SECTOR_SIZE - write_0; | ||
esp_partition_erase_range(_partition[0], sector_offset, write_0); | ||
esp_partition_write(_partition[0], sector_offset, _cache, write_0); | ||
esp_partition_erase_range(_partition[1], 0, write_1); | ||
esp_partition_write(_partition[1], 0, _cache + write_0, write_1); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} else { | ||
// only write to partition 0 | ||
esp_partition_erase_range(_partition[0], sector_offset, SECTOR_SIZE); | ||
esp_partition_write(_partition[0], sector_offset, _cache, SECTOR_SIZE); | ||
} | ||
} | ||
|
||
return 0; // success | ||
|
_partition[1]
could be NULL, as we discussed in discord.