32
32
#include "src/rp2_common/hardware_flash/include/hardware/flash.h"
33
33
34
34
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
35
38
36
39
uint32_t common_hal_nvm_bytearray_get_length (nvm_bytearray_obj_t * self ) {
37
40
return self -> len ;
38
41
}
39
42
40
43
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.
43
46
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 );
45
48
} else {
46
49
uint8_t buffer [FLASH_PAGE_SIZE ];
47
50
memcpy (buffer , (uint8_t * ) page_addr , FLASH_PAGE_SIZE );
48
51
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 );
50
53
}
51
54
}
52
55
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
+
53
71
bool common_hal_nvm_bytearray_set_bytes (nvm_bytearray_obj_t * self ,
54
72
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
+
55
83
uint32_t address = (uint32_t ) self -> start_address + start_index ;
56
84
uint32_t offset = address % FLASH_PAGE_SIZE ;
57
85
uint32_t page_addr = address - offset ;
@@ -67,8 +95,3 @@ bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t* self,
67
95
68
96
return true;
69
97
}
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