diff options
| author | microDev <70126934+microDev1@users.noreply.github.com> | 2021-03-02 14:36:28 +0530 |
|---|---|---|
| committer | microDev <70126934+microDev1@users.noreply.github.com> | 2021-03-02 14:36:28 +0530 |
| commit | b02903128679d3ecbe83d19770d2f622b36fe84d (patch) | |
| tree | 76ebbe27ad4e5893ac296595abb875160a0aeb13 /ports | |
| parent | 696d212dc70e01b9b51b7a19f344338ed387d69b (diff) | |
minor structural modification
Diffstat (limited to 'ports')
| -rw-r--r-- | ports/raspberrypi/common-hal/nvm/ByteArray.c | 31 |
1 files changed, 18 insertions, 13 deletions
diff --git a/ports/raspberrypi/common-hal/nvm/ByteArray.c b/ports/raspberrypi/common-hal/nvm/ByteArray.c index 5d1147425..fdc2c6080 100644 --- a/ports/raspberrypi/common-hal/nvm/ByteArray.c +++ b/ports/raspberrypi/common-hal/nvm/ByteArray.c @@ -53,7 +53,7 @@ static void write_page(uint32_t page_addr, uint32_t offset, uint32_t len, uint8_ } } -static void write_sector(uint32_t address, uint32_t len, uint8_t* bytes) { +static void erase_and_write_sector(uint32_t address, uint32_t len, uint8_t* bytes) { // Write a whole sector to flash, buffering it first and then erasing and rewriting it // since we can only erase a whole sector at a time. uint8_t buffer[FLASH_SECTOR_SIZE]; @@ -73,24 +73,29 @@ bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t* self, uint8_t values_in[len]; common_hal_nvm_bytearray_get_bytes(self, start_index, len, values_in); + bool all_ones = true; for (uint32_t i = 0; i < len; i++) { if (values_in[i] != UINT8_MAX) { - write_sector(start_index, len, values); - return true; + all_ones = false; + break; } } - uint32_t address = (uint32_t) self->start_address + start_index; - uint32_t offset = address % FLASH_PAGE_SIZE; - uint32_t page_addr = address - offset; + if (all_ones) { + uint32_t address = (uint32_t) self->start_address + start_index; + uint32_t offset = address % FLASH_PAGE_SIZE; + uint32_t page_addr = address - offset; - while (len) { - uint32_t write_len = MIN(len, FLASH_PAGE_SIZE - offset); - write_page(page_addr, offset, write_len, values); - len -= write_len; - values += write_len; - page_addr += FLASH_PAGE_SIZE; - offset = 0; + while (len) { + uint32_t write_len = MIN(len, FLASH_PAGE_SIZE - offset); + write_page(page_addr, offset, write_len, values); + len -= write_len; + values += write_len; + page_addr += FLASH_PAGE_SIZE; + offset = 0; + } + } else { + erase_and_write_sector(start_index, len, values); } return true; |
