Skip to content

Commit dc38120

Browse files
committed
Move filesystem funcs higher, use env.Replace
1 parent 1e709a2 commit dc38120

File tree

1 file changed

+76
-79
lines changed

1 file changed

+76
-79
lines changed

builder/main.py

Lines changed: 76 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,80 @@
2525
from SCons.Script import (ARGUMENTS, COMMAND_LINE_TARGETS, AlwaysBuild,
2626
Builder, Default, DefaultEnvironment)
2727

28+
def convert_size_expression_to_int(expression):
29+
conversion_factors = {
30+
"M": 1024*1024,
31+
"MB": 1024*1024,
32+
"K": 1024,
33+
"KB": 1024,
34+
"B": 1,
35+
"": 1 # giving no conversion factor is factor 1.
36+
}
37+
# match <floating pointer number><conversion factor>.
38+
extract_regex = r'^((?:[0-9]*[.])?[0-9]+)([mkbMKB]*)$'
39+
res = re.findall(extract_regex, expression)
40+
# unparsable expression? Warning.
41+
if len(res) == 0:
42+
sys.stderr.write(
43+
"Error: Could not parse filesystem size expression '%s'."
44+
" Will treat as size = 0.\n" % str(expression))
45+
return 0
46+
# access first result
47+
number, factor = res[0]
48+
number = float(number)
49+
number *= conversion_factors[factor.upper()]
50+
return int(number)
51+
52+
def fetch_fs_size(env):
53+
# follow generation formulas from makeboards.py for Earle Philhower core
54+
# given the total flash size, a user can specify
55+
# the amount for the filesystem (0MB, 2MB, 4MB, 8MB, 16MB)
56+
# via board_build.filesystem_size,
57+
# and we will calculate the flash size and eeprom size from that.
58+
flash_size = board.get("upload.maximum_size")
59+
filesystem_size = board.get("build.filesystem_size", "0MB")
60+
filesystem_size_int = convert_size_expression_to_int(filesystem_size)
61+
# last 4K are allocated for EEPROM emulation in flash.
62+
# see https://github.com/earlephilhower/arduino-pico/blob/3414b73172d307e9dc901f7fee83b41112f73457/libraries/EEPROM/EEPROM.cpp#L43-L46
63+
eeprom_size = 4096
64+
65+
maximum_sketch_size = flash_size - eeprom_size - filesystem_size_int
66+
67+
print("Flash size: %.2fMB" % (flash_size / 1024.0 / 1024.0))
68+
print("Sketch size: %.2fMB" % (maximum_sketch_size / 1024.0 / 1024.0))
69+
print("Filesystem size: %.2fMB" % (filesystem_size_int / 1024.0 / 1024.0))
70+
71+
eeprom_start = 0x10000000 + flash_size - eeprom_size
72+
fs_start = 0x10000000 + flash_size - eeprom_size - filesystem_size_int
73+
fs_end = 0x10000000 + flash_size - eeprom_size
74+
75+
if maximum_sketch_size <= 0:
76+
sys.stderr.write(
77+
"Error: Filesystem too large for given flash. "
78+
"Can at max be flash size - 4096 bytes. "
79+
"Available sketch size with current "
80+
"config would be %d bytes.\n" % maximum_sketch_size)
81+
sys.stderr.flush()
82+
env.Exit(1)
83+
84+
env["PICO_FLASH_LENGTH"] = maximum_sketch_size
85+
env["PICO_EEPROM_START"] = eeprom_start
86+
env["FS_START"] = fs_start
87+
env["FS_END"] = fs_end
88+
# LittleFS configuration parameters taken from
89+
# https://github.com/earlephilhower/arduino-pico-littlefs-plugin/blob/master/src/PicoLittleFS.java
90+
env["FS_PAGE"] = 256
91+
env["FS_BLOCK"] = 4096
92+
93+
print("Maximium Sketch size: %d "
94+
"EEPROM start: %s Filesystem start: %s "
95+
"Filesystem end: %s" %
96+
(maximum_sketch_size, hex(eeprom_start), hex(fs_start), hex(fs_end)))
97+
98+
99+
def __fetch_fs_size(target, source, env):
100+
fetch_fs_size(env)
101+
return (target, source)
28102

29103
def BeforeUpload(target, source, env): # pylint: disable=W0613,W0621
30104
upload_options = {}
@@ -59,6 +133,8 @@ def generate_uf2(target, source, env):
59133
board = env.BoardConfig()
60134

61135
env.Replace(
136+
__fetch_fs_size=fetch_fs_size,
137+
62138
AR="arm-none-eabi-ar",
63139
AS="arm-none-eabi-as",
64140
CC="arm-none-eabi-gcc",
@@ -115,82 +191,6 @@ def generate_uf2(target, source, env):
115191
if not env.get("PIOFRAMEWORK"):
116192
env.SConscript("frameworks/_bare.py")
117193

118-
119-
def convert_size_expression_to_int(expression):
120-
conversion_factors = {
121-
"M": 1024*1024,
122-
"MB": 1024*1024,
123-
"K": 1024,
124-
"KB": 1024,
125-
"B": 1,
126-
"": 1 # giving no conversion factor is factor 1.
127-
}
128-
# match <floating pointer number><conversion factor>.
129-
extract_regex = r'^((?:[0-9]*[.])?[0-9]+)([mkbMKB]*)$'
130-
res = re.findall(extract_regex, expression)
131-
# unparsable expression? Warning.
132-
if len(res) == 0:
133-
sys.stderr.write(
134-
"Error: Could not parse filesystem size expression '%s'."
135-
" Will treat as size = 0.\n" % str(expression))
136-
return 0
137-
# access first result
138-
number, factor = res[0]
139-
number = float(number)
140-
number *= conversion_factors[factor.upper()]
141-
return int(number)
142-
143-
def fetch_fs_size(env):
144-
# follow generation formulas from makeboards.py for Earle Philhower core
145-
# given the total flash size, a user can specify
146-
# the amount for the filesystem (0MB, 2MB, 4MB, 8MB, 16MB)
147-
# via board_build.filesystem_size,
148-
# and we will calculate the flash size and eeprom size from that.
149-
flash_size = board.get("upload.maximum_size")
150-
filesystem_size = board.get("build.filesystem_size", "0MB")
151-
filesystem_size_int = convert_size_expression_to_int(filesystem_size)
152-
# last 4K are allocated for EEPROM emulation in flash.
153-
# see https://github.com/earlephilhower/arduino-pico/blob/3414b73172d307e9dc901f7fee83b41112f73457/libraries/EEPROM/EEPROM.cpp#L43-L46
154-
eeprom_size = 4096
155-
156-
maximum_sketch_size = flash_size - eeprom_size - filesystem_size_int
157-
158-
print("Flash size: %.2fMB" % (flash_size / 1024.0 / 1024.0))
159-
print("Sketch size: %.2fMB" % (maximum_sketch_size / 1024.0 / 1024.0))
160-
print("Filesystem size: %.2fMB" % (filesystem_size_int / 1024.0 / 1024.0))
161-
162-
eeprom_start = 0x10000000 + flash_size - eeprom_size
163-
fs_start = 0x10000000 + flash_size - eeprom_size - filesystem_size_int
164-
fs_end = 0x10000000 + flash_size - eeprom_size
165-
166-
if maximum_sketch_size <= 0:
167-
sys.stderr.write(
168-
"Error: Filesystem too large for given flash. "
169-
"Can at max be flash size - 4096 bytes. "
170-
"Available sketch size with current "
171-
"config would be %d bytes.\n" % maximum_sketch_size)
172-
sys.stderr.flush()
173-
env.Exit(1)
174-
175-
env["PICO_FLASH_LENGTH"] = maximum_sketch_size
176-
env["PICO_EEPROM_START"] = eeprom_start
177-
env["FS_START"] = fs_start
178-
env["FS_END"] = fs_end
179-
# LittleFS configuration parameters taken from
180-
# https://github.com/earlephilhower/arduino-pico-littlefs-plugin/blob/master/src/PicoLittleFS.java
181-
env["FS_PAGE"] = 256
182-
env["FS_BLOCK"] = 4096
183-
184-
print("Maximium Sketch size: %d "
185-
"EEPROM start: %s Filesystem start: %s "
186-
"Filesystem end: %s" %
187-
(maximum_sketch_size, hex(eeprom_start), hex(fs_start), hex(fs_end)))
188-
189-
190-
def __fetch_fs_size(target, source, env):
191-
fetch_fs_size(env)
192-
return (target, source)
193-
194194
env.Append(
195195
BUILDERS=dict(
196196
DataToBin=Builder(
@@ -209,9 +209,6 @@ def __fetch_fs_size(target, source, env):
209209
)
210210
)
211211

212-
# store function to get infno about filesystems for builder scripts.
213-
env["__fetch_fs_size"] = fetch_fs_size
214-
215212
#
216213
# Target: Build executable and linkable firmware
217214
#

0 commit comments

Comments
 (0)