Skip to content

Commit 6168cb1

Browse files
authored
Avoid erasing all pages at once (takes looong time on some platforms) (#190)
Change page erasing strategy for nRF52 devices
1 parent 28a22f0 commit 6168cb1

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/InternalStorage.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ extern "C" {
6363
while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
6464
#endif
6565
}
66-
66+
6767
#if defined(__SAMD51__)
6868
// Invalidate all CMCC cache entries if CMCC cache is enabled.
6969
__attribute__ ((long_call, noinline, section (".data#")))
@@ -94,7 +94,7 @@ extern "C" {
9494
for (int i = 0; i < length; i += rowSize) {
9595
NVMCTRL->ADDR.reg = ((uint32_t)(address + i)) / 2;
9696
NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY | NVMCTRL_CTRLA_CMD_ER;
97-
97+
9898
waitForReady();
9999
}
100100
#elif defined(ARDUINO_ARCH_NRF5)
@@ -113,7 +113,7 @@ extern "C" {
113113
waitForReady();
114114
NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
115115
waitForReady();
116-
#endif
116+
#endif
117117
}
118118

119119
__attribute__ ((long_call, noinline, section (".data#")))
@@ -156,7 +156,10 @@ int InternalStorageClass::open(int length)
156156
NVMCTRL->CTRLB.bit.MANW = 0;
157157
#endif
158158

159+
#if !defined(ARDUINO_ARCH_NRF5)
160+
// Erase all pages
159161
eraseFlash(STORAGE_START_ADDRESS, pageAlignedLength, PAGE_SIZE);
162+
#endif
160163

161164
return 1;
162165
}
@@ -169,6 +172,13 @@ size_t InternalStorageClass::write(uint8_t b)
169172
if (_writeIndex == 4) {
170173
_writeIndex = 0;
171174

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+
172182
*_writeAddress = _addressData.u32;
173183

174184
_writeAddress++;
@@ -184,6 +194,10 @@ void InternalStorageClass::close()
184194
while ((int)_writeAddress % PAGE_SIZE) {
185195
write(0xff);
186196
}
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);
187201
}
188202

189203
void InternalStorageClass::clear()

0 commit comments

Comments
 (0)