20
20
DefaultEnvironment )
21
21
22
22
23
+ #
24
+ # Helpers
25
+ #
26
+
23
27
def _get_board_f_flash (env ):
24
28
frequency = env .subst ("$BOARD_F_FLASH" )
25
29
frequency = str (frequency ).replace ("L" , "" )
@@ -34,30 +38,73 @@ def _get_board_flash_mode(env):
34
38
return "dout"
35
39
return mode
36
40
41
+
42
+ def _parse_size (value ):
43
+ if value .isdigit ():
44
+ return int (value )
45
+ elif value .startswith ("0x" ):
46
+ return int (value , 16 )
47
+ elif value [- 1 ] in ("K" , "M" ):
48
+ base = 1024 if value [- 1 ] == "K" else 1024 * 1024
49
+ return int (value [:- 1 ]) * base
50
+ return value
51
+
52
+
53
+ def _parse_partitions (env ):
54
+ partitions_csv = env .subst ("$PARTITIONS_TABLE_CSV" )
55
+ if not isfile (partitions_csv ):
56
+ sys .stderr .write ("Could not find the file %s with partitions "
57
+ "table.\n " % partitions_csv )
58
+ env .Exit (1 )
59
+
60
+ result = []
61
+ with open (partitions_csv ) as fp :
62
+ for line in fp .readlines ():
63
+ line = line .strip ()
64
+ if not line or line .startswith ("#" ):
65
+ continue
66
+ tokens = [t .strip () for t in line .split ("," ) if t .strip ()]
67
+ if len (tokens ) < 5 :
68
+ continue
69
+ result .append ({
70
+ "name" : tokens [0 ],
71
+ "type" : tokens [1 ],
72
+ "subtype" : tokens [2 ],
73
+ "offset" : tokens [3 ],
74
+ "size" : tokens [4 ],
75
+ "flags" : tokens [5 ] if len (tokens ) > 5 else None
76
+ })
77
+ return result
78
+
79
+
80
+ def _update_max_upload_size (env ):
81
+ if not env .get ("PARTITIONS_TABLE_CSV" ):
82
+ return
83
+ sizes = [
84
+ _parse_size (p ['size' ]) for p in _parse_partitions (env )
85
+ if p ['type' ] in ("0" , "app" )
86
+ ]
87
+ if sizes :
88
+ env .BoardConfig ().update ("upload.maximum_size" , max (sizes ))
89
+
90
+
37
91
#
38
92
# SPIFFS helpers
39
93
#
40
94
41
95
def fetch_spiffs_size (env ):
42
- path_to_patition_table = env .get ("PARTITION_TABLE_CSV" )
43
- if not isfile (path_to_patition_table ):
44
- sys .stderr .write ("Could not find the file %s with paritions table." %
45
- path_to_patition_table )
46
- env .Exit (1 )
47
-
48
- with open (path_to_patition_table ) as fp :
49
- for l in fp .readlines ():
50
- if l .startswith ("spiffs" ):
51
- spiffs_config = [s .strip () for s in l .split ("," )]
52
- env ["SPIFFS_START" ] = spiffs_config [3 ]
53
- env ["SPIFFS_SIZE" ] = spiffs_config [4 ]
54
- env ["SPIFFS_PAGE" ] = "0x100"
55
- env ["SPIFFS_BLOCK" ] = "0x1000"
56
- return
57
-
58
- sys .stderr .write ("Could not find the spiffs section in the paritions "
59
- "file %s" % path_to_patition_table )
60
- env .Exit (1 )
96
+ spiffs = None
97
+ for p in _parse_partitions (env ):
98
+ if p ['type' ] == "data" and p ['subtype' ] == "spiffs" :
99
+ spiffs = p
100
+ if not spiffs :
101
+ sys .stderr .write (
102
+ env .subst ("Could not find the `spiffs` section in the partitions "
103
+ "table $PARTITIONS_TABLE_CSV\n " ))
104
+ env ["SPIFFS_START" ] = _parse_size (spiffs ['offset' ])
105
+ env ["SPIFFS_SIZE" ] = _parse_size (spiffs ['size' ])
106
+ env ["SPIFFS_PAGE" ] = int ("0x100" , 16 )
107
+ env ["SPIFFS_BLOCK" ] = int ("0x1000" , 16 )
61
108
62
109
63
110
def __fetch_spiffs_size (target , source , env ):
@@ -157,9 +204,9 @@ def __fetch_spiffs_size(target, source, env):
157
204
action = env .VerboseAction (" " .join ([
158
205
'"$MKSPIFFSTOOL"' ,
159
206
"-c" , "$SOURCES" ,
160
- "-p" , "${int( SPIFFS_PAGE, 16)} " ,
161
- "-b" , "${int( SPIFFS_BLOCK, 16)} " ,
162
- "-s" , "${int( SPIFFS_SIZE, 16)} " ,
207
+ "-p" , "$SPIFFS_PAGE" ,
208
+ "-b" , "$SPIFFS_BLOCK" ,
209
+ "-s" , "$SPIFFS_SIZE" ,
163
210
"$TARGET"
164
211
]), "Building SPIFFS image from '$SOURCES' directory to $TARGET" ),
165
212
emitter = __fetch_spiffs_size ,
@@ -194,6 +241,10 @@ def __fetch_spiffs_size(target, source, env):
194
241
AlwaysBuild (env .Alias ("nobuild" , target_firm ))
195
242
target_buildprog = env .Alias ("buildprog" , target_firm , target_firm )
196
243
244
+ # update max upload size based on CSV file
245
+ if "upload" in COMMAND_LINE_TARGETS :
246
+ _update_max_upload_size (env )
247
+
197
248
#
198
249
# Target: Print binary size
199
250
#
@@ -263,7 +314,7 @@ def __fetch_spiffs_size(target, source, env):
263
314
"write_flash" , "-z" ,
264
315
"--flash_mode" , "$BOARD_FLASH_MODE" ,
265
316
"--flash_size" , "detect" ,
266
- "${int( SPIFFS_START, 16)} "
317
+ "$SPIFFS_START"
267
318
],
268
319
UPLOADCMD = '"$PYTHONEXE" "$UPLOADER" $UPLOADERFLAGS $SOURCE' ,
269
320
)
0 commit comments