|
25 | 25 | * THE SOFTWARE.
|
26 | 26 | */
|
27 | 27 |
|
28 |
| -// This is a copy of the file micropython:ports/nrf/modules/uos/microbitfs.c with |
29 |
| -// a call to `microbit_file_opened_for_writing` added in `microbit_file_open`. |
| 28 | +// This is a copy of the file micropython:ports/nrf/modules/uos/microbitfs.c with: |
| 29 | +// - a call to `microbit_file_opened_for_writing` added in `microbit_file_open` |
| 30 | +// - a fix to `find_chunk_and_erase` to sweep the filesystem if any free chunks are found |
30 | 31 |
|
31 | 32 | #include <string.h>
|
32 | 33 | #include <stdio.h>
|
|
83 | 84 | /** Must be such that sizeof(file_header) < DATA_PER_CHUNK */
|
84 | 85 | #define MAX_FILENAME_LENGTH 120
|
85 | 86 |
|
86 |
| -//Minimum number of free chunks to justify sweeping. |
87 |
| -//If this is too low it may cause excessive wear |
88 |
| -#define MIN_CHUNKS_FOR_SWEEP (FLASH_PAGESIZE / CHUNK_SIZE) |
89 |
| - |
90 | 87 | #define FILE_NOT_FOUND ((uint8_t)-1)
|
91 | 88 |
|
92 | 89 | /** Maximum number of chunks allowed in filesystem. 252 chunks is 31.5kB */
|
@@ -271,9 +268,9 @@ STATIC uint8_t microbit_find_file(const char *name, int name_len) {
|
271 | 268 | // Search the chunks:
|
272 | 269 | // 1 If an UNUSED chunk is found, then return that.
|
273 | 270 | // 2. If an entire page of FREED chunks is found, then erase the page and return the first chunk
|
274 |
| -// 3. If the number of FREED chunks is >= MIN_CHUNKS_FOR_SWEEP, then |
| 271 | +// 3. If the number of FREED chunks is > 0, then |
275 | 272 | // 3a. Sweep the filesystem and restart.
|
276 |
| -// 3b. Fail and return FILE_NOT_FOUND |
| 273 | +// 3b. Otherwise, fail and return FILE_NOT_FOUND. |
277 | 274 | //
|
278 | 275 | STATIC uint8_t find_chunk_and_erase(void) {
|
279 | 276 | // Start search at a random chunk to spread the wear more evenly.
|
@@ -314,7 +311,7 @@ STATIC uint8_t find_chunk_and_erase(void) {
|
314 | 311 | if (index == chunks_in_file_system+1) index = 1;
|
315 | 312 | } while (index != start_index);
|
316 | 313 | DEBUG(("FILE DEBUG: %lu free chunks\r\n", freed_chunks));
|
317 |
| - if (freed_chunks < MIN_CHUNKS_FOR_SWEEP) { |
| 314 | + if (freed_chunks == 0) { |
318 | 315 | return FILE_NOT_FOUND;
|
319 | 316 | }
|
320 | 317 | // No freed pages, so sweep file system.
|
|
0 commit comments