Skip to content

Commit 696d212

Browse files
committed
fix nvm rewrite
1 parent f2d7a5f commit 696d212

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

ports/raspberrypi/common-hal/nvm/ByteArray.c

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,54 @@
3232
#include "src/rp2_common/hardware_flash/include/hardware/flash.h"
3333

3434
extern uint32_t __flash_binary_start;
35+
static const uint32_t flash_binary_start = (uint32_t) &__flash_binary_start;
36+
37+
#define RMV_OFFSET(addr) addr - flash_binary_start
3538

3639
uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t* self) {
3740
return self->len;
3841
}
3942

4043
static void write_page(uint32_t page_addr, uint32_t offset, uint32_t len, uint8_t* bytes) {
41-
// Write a whole page to flash, buffering it first and then erasing and rewriting
42-
// it since we can only clear a whole page at a time.
44+
// Write a whole page to flash, buffering it first and then erasing and rewriting it
45+
// since we can only write a whole page at a time.
4346
if (offset == 0 && len == FLASH_PAGE_SIZE) {
44-
flash_range_program(page_addr - (uint32_t) &__flash_binary_start, bytes, FLASH_PAGE_SIZE);
47+
flash_range_program(RMV_OFFSET(page_addr), bytes, FLASH_PAGE_SIZE);
4548
} else {
4649
uint8_t buffer[FLASH_PAGE_SIZE];
4750
memcpy(buffer, (uint8_t*) page_addr, FLASH_PAGE_SIZE);
4851
memcpy(buffer + offset, bytes, len);
49-
flash_range_program(page_addr - (uint32_t) &__flash_binary_start, buffer, FLASH_PAGE_SIZE);
52+
flash_range_program(RMV_OFFSET(page_addr), buffer, FLASH_PAGE_SIZE);
5053
}
5154
}
5255

56+
static void write_sector(uint32_t address, uint32_t len, uint8_t* bytes) {
57+
// Write a whole sector to flash, buffering it first and then erasing and rewriting it
58+
// since we can only erase a whole sector at a time.
59+
uint8_t buffer[FLASH_SECTOR_SIZE];
60+
memcpy(buffer, (uint8_t*) CIRCUITPY_INTERNAL_NVM_START_ADDR, FLASH_SECTOR_SIZE);
61+
memcpy(buffer + address, bytes, len);
62+
flash_range_erase(RMV_OFFSET(CIRCUITPY_INTERNAL_NVM_START_ADDR), FLASH_SECTOR_SIZE);
63+
flash_range_program(RMV_OFFSET(CIRCUITPY_INTERNAL_NVM_START_ADDR), buffer, FLASH_SECTOR_SIZE);
64+
}
65+
66+
void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t* self,
67+
uint32_t start_index, uint32_t len, uint8_t* values) {
68+
memcpy(values, self->start_address + start_index, len);
69+
}
70+
5371
bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t* self,
5472
uint32_t start_index, uint8_t* values, uint32_t len) {
73+
uint8_t values_in[len];
74+
common_hal_nvm_bytearray_get_bytes(self, start_index, len, values_in);
75+
76+
for (uint32_t i = 0; i < len; i++) {
77+
if (values_in[i] != UINT8_MAX) {
78+
write_sector(start_index, len, values);
79+
return true;
80+
}
81+
}
82+
5583
uint32_t address = (uint32_t) self->start_address + start_index;
5684
uint32_t offset = address % FLASH_PAGE_SIZE;
5785
uint32_t page_addr = address - offset;
@@ -67,8 +95,3 @@ bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t* self,
6795

6896
return true;
6997
}
70-
71-
void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t* self,
72-
uint32_t start_index, uint32_t len, uint8_t* values) {
73-
memcpy(values, self->start_address + start_index, len);
74-
}

0 commit comments

Comments
 (0)