@@ -53,7 +53,7 @@ static void write_page(uint32_t page_addr, uint32_t offset, uint32_t len, uint8_
53
53
}
54
54
}
55
55
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 ) {
57
57
// Write a whole sector to flash, buffering it first and then erasing and rewriting it
58
58
// since we can only erase a whole sector at a time.
59
59
uint8_t buffer [FLASH_SECTOR_SIZE ];
@@ -73,24 +73,29 @@ bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t* self,
73
73
uint8_t values_in [len ];
74
74
common_hal_nvm_bytearray_get_bytes (self , start_index , len , values_in );
75
75
76
+ bool all_ones = true;
76
77
for (uint32_t i = 0 ; i < len ; i ++ ) {
77
78
if (values_in [i ] != UINT8_MAX ) {
78
- write_sector ( start_index , len , values ) ;
79
- return true ;
79
+ all_ones = false ;
80
+ break ;
80
81
}
81
82
}
82
83
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 ;
86
88
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 );
94
99
}
95
100
96
101
return true;
0 commit comments