@@ -63,7 +63,7 @@ extern "C" {
63
63
while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
64
64
#endif
65
65
}
66
-
66
+
67
67
#if defined(__SAMD51__)
68
68
// Invalidate all CMCC cache entries if CMCC cache is enabled.
69
69
__attribute__ ((long_call, noinline, section (" .data#" )))
@@ -94,7 +94,7 @@ extern "C" {
94
94
for (int i = 0 ; i < length; i += rowSize) {
95
95
NVMCTRL->ADDR .reg = ((uint32_t )(address + i)) / 2 ;
96
96
NVMCTRL->CTRLA .reg = NVMCTRL_CTRLA_CMDEX_KEY | NVMCTRL_CTRLA_CMD_ER;
97
-
97
+
98
98
waitForReady ();
99
99
}
100
100
#elif defined(ARDUINO_ARCH_NRF5)
@@ -113,7 +113,7 @@ extern "C" {
113
113
waitForReady ();
114
114
NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
115
115
waitForReady ();
116
- #endif
116
+ #endif
117
117
}
118
118
119
119
__attribute__ ((long_call, noinline, section (" .data#" )))
@@ -156,7 +156,10 @@ int InternalStorageClass::open(int length)
156
156
NVMCTRL->CTRLB .bit .MANW = 0 ;
157
157
#endif
158
158
159
+ #if !defined(ARDUINO_ARCH_NRF5)
160
+ // Erase all pages
159
161
eraseFlash (STORAGE_START_ADDRESS, pageAlignedLength, PAGE_SIZE);
162
+ #endif
160
163
161
164
return 1 ;
162
165
}
@@ -169,6 +172,13 @@ size_t InternalStorageClass::write(uint8_t b)
169
172
if (_writeIndex == 4 ) {
170
173
_writeIndex = 0 ;
171
174
175
+ #if defined(ARDUINO_ARCH_NRF5)
176
+ // Erase a single page if needed
177
+ if ((int )(_writeAddress) % PAGE_SIZE == 0 ) {
178
+ eraseFlash ((int )_writeAddress, PAGE_SIZE, PAGE_SIZE);
179
+ }
180
+ #endif
181
+
172
182
*_writeAddress = _addressData.u32 ;
173
183
174
184
_writeAddress++;
@@ -184,6 +194,10 @@ void InternalStorageClass::close()
184
194
while ((int )_writeAddress % PAGE_SIZE) {
185
195
write (0xff );
186
196
}
197
+
198
+ // Re-calculate pageAlignedLength in case the actually written binary
199
+ // is smaller then the size provided in open()
200
+ pageAlignedLength = (_writeAddress - (uint32_t *)STORAGE_START_ADDRESS) * sizeof (uint32_t );
187
201
}
188
202
189
203
void InternalStorageClass::clear ()
0 commit comments