summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormicroDev <70126934+microDev1@users.noreply.github.com>2021-02-18 18:42:00 +0530
committermicroDev <70126934+microDev1@users.noreply.github.com>2021-02-18 18:42:00 +0530
commit696d212dc70e01b9b51b7a19f344338ed387d69b (patch)
treee0ba724cc5006ca958df6051dbf1ca4b09eee2b9
parentf2d7a5f1e1dd24c215c25e1788e359d9866c080f (diff)
fix nvm rewrite
-rw-r--r--ports/raspberrypi/common-hal/nvm/ByteArray.c41
1 files changed, 32 insertions, 9 deletions
diff --git a/ports/raspberrypi/common-hal/nvm/ByteArray.c b/ports/raspberrypi/common-hal/nvm/ByteArray.c
index 6dbd21351..5d1147425 100644
--- a/ports/raspberrypi/common-hal/nvm/ByteArray.c
+++ b/ports/raspberrypi/common-hal/nvm/ByteArray.c
@@ -32,26 +32,54 @@
#include "src/rp2_common/hardware_flash/include/hardware/flash.h"
extern uint32_t __flash_binary_start;
+static const uint32_t flash_binary_start = (uint32_t) &__flash_binary_start;
+
+#define RMV_OFFSET(addr) addr - flash_binary_start
uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t* self) {
return self->len;
}
static void write_page(uint32_t page_addr, uint32_t offset, uint32_t len, uint8_t* bytes) {
- // Write a whole page to flash, buffering it first and then erasing and rewriting
- // it since we can only clear a whole page at a time.
+ // Write a whole page to flash, buffering it first and then erasing and rewriting it
+ // since we can only write a whole page at a time.
if (offset == 0 && len == FLASH_PAGE_SIZE) {
- flash_range_program(page_addr - (uint32_t) &__flash_binary_start, bytes, FLASH_PAGE_SIZE);
+ flash_range_program(RMV_OFFSET(page_addr), bytes, FLASH_PAGE_SIZE);
} else {
uint8_t buffer[FLASH_PAGE_SIZE];
memcpy(buffer, (uint8_t*) page_addr, FLASH_PAGE_SIZE);
memcpy(buffer + offset, bytes, len);
- flash_range_program(page_addr - (uint32_t) &__flash_binary_start, buffer, FLASH_PAGE_SIZE);
+ flash_range_program(RMV_OFFSET(page_addr), buffer, FLASH_PAGE_SIZE);
}
}
+static void 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];
+ memcpy(buffer, (uint8_t*) CIRCUITPY_INTERNAL_NVM_START_ADDR, FLASH_SECTOR_SIZE);
+ memcpy(buffer + address, bytes, len);
+ flash_range_erase(RMV_OFFSET(CIRCUITPY_INTERNAL_NVM_START_ADDR), FLASH_SECTOR_SIZE);
+ flash_range_program(RMV_OFFSET(CIRCUITPY_INTERNAL_NVM_START_ADDR), buffer, FLASH_SECTOR_SIZE);
+}
+
+void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t* self,
+ uint32_t start_index, uint32_t len, uint8_t* values) {
+ memcpy(values, self->start_address + start_index, len);
+}
+
bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t* self,
uint32_t start_index, uint8_t* values, uint32_t len) {
+ uint8_t values_in[len];
+ common_hal_nvm_bytearray_get_bytes(self, start_index, len, values_in);
+
+ for (uint32_t i = 0; i < len; i++) {
+ if (values_in[i] != UINT8_MAX) {
+ write_sector(start_index, len, values);
+ return true;
+ }
+ }
+
uint32_t address = (uint32_t) self->start_address + start_index;
uint32_t offset = address % FLASH_PAGE_SIZE;
uint32_t page_addr = address - offset;
@@ -67,8 +95,3 @@ bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t* self,
return true;
}
-
-void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t* self,
- uint32_t start_index, uint32_t len, uint8_t* values) {
- memcpy(values, self->start_address + start_index, len);
-}