Skip to content

Commit b029031

Browse files
committed
minor structural modification
1 parent 696d212 commit b029031

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static void write_page(uint32_t page_addr, uint32_t offset, uint32_t len, uint8_
5353
}
5454
}
5555

56-
static void write_sector(uint32_t address, uint32_t len, uint8_t* bytes) {
56+
static void erase_and_write_sector(uint32_t address, uint32_t len, uint8_t* bytes) {
5757
// Write a whole sector to flash, buffering it first and then erasing and rewriting it
5858
// since we can only erase a whole sector at a time.
5959
uint8_t buffer[FLASH_SECTOR_SIZE];
@@ -73,24 +73,29 @@ bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t* self,
7373
uint8_t values_in[len];
7474
common_hal_nvm_bytearray_get_bytes(self, start_index, len, values_in);
7575

76+
bool all_ones = true;
7677
for (uint32_t i = 0; i < len; i++) {
7778
if (values_in[i] != UINT8_MAX) {
78-
write_sector(start_index, len, values);
79-
return true;
79+
all_ones = false;
80+
break;
8081
}
8182
}
8283

83-
uint32_t address = (uint32_t) self->start_address + start_index;
84-
uint32_t offset = address % FLASH_PAGE_SIZE;
85-
uint32_t page_addr = address - offset;
84+
if (all_ones) {
85+
uint32_t address = (uint32_t) self->start_address + start_index;
86+
uint32_t offset = address % FLASH_PAGE_SIZE;
87+
uint32_t page_addr = address - offset;
8688

87-
while (len) {
88-
uint32_t write_len = MIN(len, FLASH_PAGE_SIZE - offset);
89-
write_page(page_addr, offset, write_len, values);
90-
len -= write_len;
91-
values += write_len;
92-
page_addr += FLASH_PAGE_SIZE;
93-
offset = 0;
89+
while (len) {
90+
uint32_t write_len = MIN(len, FLASH_PAGE_SIZE - offset);
91+
write_page(page_addr, offset, write_len, values);
92+
len -= write_len;
93+
values += write_len;
94+
page_addr += FLASH_PAGE_SIZE;
95+
offset = 0;
96+
}
97+
} else {
98+
erase_and_write_sector(start_index, len, values);
9499
}
95100

96101
return true;

0 commit comments

Comments
 (0)