Skip to content

Commit 391f425

Browse files
upy-fs-builder: Use flash size retrieved from UICR data.
1 parent 04fe292 commit 391f425

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

src/micropython-fs-builder.ts

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,11 @@ const CHUNK_HEADER_NAME_LEN = 1;
7070

7171
const MAX_FILENAME_LENGTH = 120;
7272

73-
/** Flash values for the micro:bit nRF microcontroller. */
74-
const FLASH_PAGE_SIZE = 1024;
75-
const FLASH_END = 0x40000;
76-
77-
/** Size of pages with specific functions. */
78-
const CALIBRATION_PAGE_SIZE = FLASH_PAGE_SIZE;
73+
/**
74+
* Chunks are a double linked list with 1-byte pointers and the front marker
75+
* (previous pointer) cannot have the values listed in the ChunkMarker enum
76+
*/
77+
const MAX_NUMBER_OF_CHUNKS = 256 - 4;
7978

8079
/**
8180
* To speed up the Intel Hex string generation with MicroPython and the
@@ -134,16 +133,28 @@ function getFreeChunks(intelHexMap: MemoryMap): number[] {
134133

135134
/**
136135
* Calculates from the input Intel Hex where the MicroPython runtime ends and
137-
* return that as the start of the filesystem area.
136+
* and where the start of the filesystem would be based on that.
138137
*
139138
* @param intelHexMap - Memory map for the MicroPython Intel Hex.
140139
* @returns Filesystem start address
141140
*/
142141
function getStartAddress(intelHexMap: MemoryMap): number {
143142
const uicrData = getHexMapUicrData(intelHexMap);
144-
const startAddress = uicrData.runtimeEndAddress;
145-
// Ensure the start address aligns with the page size
146-
if (startAddress % FLASH_PAGE_SIZE) {
143+
// Calculate the maximum flash space the filesystem can possible take
144+
let fsMaxSize = CHUNK_LEN * MAX_NUMBER_OF_CHUNKS;
145+
// We need to add the persistent data which is one page aligned after fs data
146+
fsMaxSize += uicrData.flashPageSize - (fsMaxSize % uicrData.flashPageSize);
147+
fsMaxSize += uicrData.flashPageSize;
148+
149+
// Fs is placed at the end of flash, the space available from the MicroPython
150+
// end to the end of flash might be larger than the fs max possible size
151+
const fsMaxSizeStartAddress = getEndAddress(intelHexMap) - fsMaxSize;
152+
const startAddress = Math.max(
153+
uicrData.runtimeEndAddress,
154+
fsMaxSizeStartAddress
155+
);
156+
// Ensure the start address is aligned with the page size
157+
if (startAddress % uicrData.flashPageSize) {
147158
throw new Error(
148159
'File system start address from UICR does not align with flash page size.'
149160
);
@@ -162,11 +173,12 @@ function getStartAddress(intelHexMap: MemoryMap): number {
162173
* @returns End address for the filesystem.
163174
*/
164175
function getEndAddress(intelHexMap: MemoryMap): number {
165-
let endAddress = FLASH_END;
166-
if (isAppendedScriptPresent(intelHexMap)) {
167-
endAddress = AppendedBlock.StartAdd;
168-
}
169-
return endAddress - CALIBRATION_PAGE_SIZE;
176+
const uicrData = getHexMapUicrData(intelHexMap);
177+
const endAddress = isAppendedScriptPresent(intelHexMap)
178+
? AppendedBlock.StartAdd
179+
: uicrData.flashSize;
180+
// Magnetometer calibration data is one flash page
181+
return endAddress - uicrData.flashPageSize;
170182
}
171183

172184
/**
@@ -176,7 +188,8 @@ function getEndAddress(intelHexMap: MemoryMap): number {
176188
* @returns Memory address where the last filesystem page starts.
177189
*/
178190
function getLastPageAddress(intelHexMap: MemoryMap): number {
179-
return getEndAddress(intelHexMap) - FLASH_PAGE_SIZE;
191+
const uicrData = getHexMapUicrData(intelHexMap);
192+
return getEndAddress(intelHexMap) - uicrData.flashPageSize;
180193
}
181194

182195
/**
@@ -408,11 +421,12 @@ function addIntelHexFiles(
408421
} else {
409422
intelHexMap = intelHex.clone();
410423
}
424+
const uicrData = getHexMapUicrData(intelHexMap);
411425
Object.keys(files).forEach((filename) => {
412426
addMemMapFile(intelHexMap, filename, files[filename]);
413427
});
414428
return returnBytes
415-
? intelHexMap.slicePad(0, FLASH_END)
429+
? intelHexMap.slicePad(0, uicrData.flashSize)
416430
: intelHexMap.asHexString() + '\n';
417431
}
418432

@@ -561,10 +575,11 @@ function getIntelHexFiles(
561575
* @returns Size of the filesystem in bytes.
562576
*/
563577
function getMemMapFsSize(intelHexMap: MemoryMap): number {
564-
const startAddress: number = getStartAddress(intelHexMap);
578+
const uicrData = getHexMapUicrData(intelHexMap);
579+
const startAddress = getStartAddress(intelHexMap);
565580
const endAddress = getEndAddress(intelHexMap);
566581
// One extra page is used as persistent page
567-
return endAddress - startAddress - FLASH_PAGE_SIZE;
582+
return endAddress - startAddress - uicrData.flashPageSize;
568583
}
569584

570585
export {

0 commit comments

Comments
 (0)