@@ -70,12 +70,11 @@ const CHUNK_HEADER_NAME_LEN = 1;
70
70
71
71
const MAX_FILENAME_LENGTH = 120 ;
72
72
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 ;
79
78
80
79
/**
81
80
* To speed up the Intel Hex string generation with MicroPython and the
@@ -134,16 +133,28 @@ function getFreeChunks(intelHexMap: MemoryMap): number[] {
134
133
135
134
/**
136
135
* 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 .
138
137
*
139
138
* @param intelHexMap - Memory map for the MicroPython Intel Hex.
140
139
* @returns Filesystem start address
141
140
*/
142
141
function getStartAddress ( intelHexMap : MemoryMap ) : number {
143
142
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 ) {
147
158
throw new Error (
148
159
'File system start address from UICR does not align with flash page size.'
149
160
) ;
@@ -162,11 +173,12 @@ function getStartAddress(intelHexMap: MemoryMap): number {
162
173
* @returns End address for the filesystem.
163
174
*/
164
175
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 ;
170
182
}
171
183
172
184
/**
@@ -176,7 +188,8 @@ function getEndAddress(intelHexMap: MemoryMap): number {
176
188
* @returns Memory address where the last filesystem page starts.
177
189
*/
178
190
function getLastPageAddress ( intelHexMap : MemoryMap ) : number {
179
- return getEndAddress ( intelHexMap ) - FLASH_PAGE_SIZE ;
191
+ const uicrData = getHexMapUicrData ( intelHexMap ) ;
192
+ return getEndAddress ( intelHexMap ) - uicrData . flashPageSize ;
180
193
}
181
194
182
195
/**
@@ -408,11 +421,12 @@ function addIntelHexFiles(
408
421
} else {
409
422
intelHexMap = intelHex . clone ( ) ;
410
423
}
424
+ const uicrData = getHexMapUicrData ( intelHexMap ) ;
411
425
Object . keys ( files ) . forEach ( ( filename ) => {
412
426
addMemMapFile ( intelHexMap , filename , files [ filename ] ) ;
413
427
} ) ;
414
428
return returnBytes
415
- ? intelHexMap . slicePad ( 0 , FLASH_END )
429
+ ? intelHexMap . slicePad ( 0 , uicrData . flashSize )
416
430
: intelHexMap . asHexString ( ) + '\n' ;
417
431
}
418
432
@@ -561,10 +575,11 @@ function getIntelHexFiles(
561
575
* @returns Size of the filesystem in bytes.
562
576
*/
563
577
function getMemMapFsSize ( intelHexMap : MemoryMap ) : number {
564
- const startAddress : number = getStartAddress ( intelHexMap ) ;
578
+ const uicrData = getHexMapUicrData ( intelHexMap ) ;
579
+ const startAddress = getStartAddress ( intelHexMap ) ;
565
580
const endAddress = getEndAddress ( intelHexMap ) ;
566
581
// One extra page is used as persistent page
567
- return endAddress - startAddress - FLASH_PAGE_SIZE ;
582
+ return endAddress - startAddress - uicrData . flashPageSize ;
568
583
}
569
584
570
585
export {
0 commit comments