Skip to content

Commit fdf5d11

Browse files
committed
changes handling of the partition file.
Instead of doing an in place alteration of the partition file the script now generates it's own.
1 parent 82894bb commit fdf5d11

File tree

2 files changed

+117
-91
lines changed

2 files changed

+117
-91
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ ESP32-ALL
2020
Flash sizes that are able to be used are 4, 8, 16, 32, 64 and 128 across all
2121
variants of the ESP32. It is up to the user to know what their board is using.
2222

23+
--ota if you want to set the partitions so you can do an over the air update of
24+
the firmware. I do want to note that this does take up twice as much application
25+
storage space. This feature applies to any board.
26+
2327

2428
ESP32-S3
2529
2 new command line arguments.

builder/esp32.py

Lines changed: 113 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -29,91 +29,79 @@ def get_partition_file_name(otp):
2929

3030

3131
PARTITION_HEADER = '''\
32-
# Notes: the offset of the partition table itself is set in
33-
# $IDF_PATH/components/partition_table/Kconfig.projbuild.
3432
# Name, Type, SubType, Offset, Size, Flags
3533
'''
3634

35+
# OTA boards
36+
# ARDUINO_NANO_ESP32
37+
# SIL_WESP32
38+
3739

3840
class Partition:
3941

40-
def __init__(self, file_path):
41-
self.load_file_path = file_path
42+
def __init__(self):
4243
self.save_file_path = f'"{SCRIPT_DIR}/build/partitions-{flash_size}MiB.csv"'
43-
self._saved_data = None
44-
self.csv_data = self.read_csv()
45-
last_partition = self.csv_data[-1]
46-
47-
self.total_space = last_partition[-2] + last_partition[-3]
48-
49-
def get_app_size(self) -> int:
50-
for part in self.csv_data:
51-
if part[1] in ('app', 'factory'):
52-
return part[4]
44+
self.first_offset = 0x9000
45+
self.nvs = 0x6000
46+
self.phy_init = 0x1000
47+
self.factory = 1 * 1024 * 1024
5348

54-
def set_app_size(self, size):
55-
next_offset = 0
56-
app_size = 0
57-
58-
for i, part in enumerate(self.csv_data):
59-
if next_offset == 0:
60-
next_offset = part[3]
49+
if ota:
50+
self.otadata = 0x2000
51+
else:
52+
self.otadata = 0x0
6153

62-
if part[3] != next_offset:
63-
part[3] = next_offset # NOQA
54+
def build_file(self):
55+
offset = self.first_offset
56+
data = [f'nvs,data, nvs,0x{offset:X},0x{self.nvs:X}']
57+
offset += self.nvs
6458

65-
if part[1] in ('app', 'factory'):
66-
factor = ((part[4] + size) / 4096.0) + 1
67-
part[4] = int(int(factor) * 4096) # NOQA
68-
app_size += part[4]
69-
elif app_size != 0:
70-
part[4] = self.total_space - next_offset # NOQA
59+
if ota:
60+
data.append(f'otadata,data,ota,0x{offset:X},0x{self.otadata:X}')
61+
offset += self.otadata
7162

72-
next_offset += part[4]
63+
data.append(f'phy_init,data,phy,0x{offset:X},0x{self.phy_init:X}')
64+
offset += self.phy_init
7365

74-
if next_offset > self.total_space:
75-
raise RuntimeError(
76-
f'Board does not have enough space, overflow of '
77-
f'{next_offset - self.total_space} bytes ({self.load_file_path})\n'
78-
)
66+
if ota:
67+
data.append(f'ota_0,app,ota_0,0x{offset:X},0x{self.factory:X}')
68+
offset += self.factory
69+
data.append(f'ota_1,app,ota_1,0x{offset:X},0x{self.factory:X}')
70+
offset += self.factory
71+
else:
72+
data.append(f'factory,app,factory,0x{offset:X},0x{self.factory:X}')
73+
offset += self.factory
74+
75+
vfs = (flash_size * 1024 * 1024) - sum([
76+
self.first_offset,
77+
self.nvs,
78+
self.otadata,
79+
self.phy_init,
80+
self.factory
81+
])
7982

80-
def save(self):
81-
otp = []
83+
if ota:
84+
vfs -= self.factory
8285

83-
def convert_to_hex(itm):
84-
if isinstance(itm, int):
85-
itm = hex(itm)
86-
return itm
86+
if vfs < 0:
87+
raise RuntimeError('There is not enough flash to store the firmware')
8788

88-
for line in self.csv_data:
89-
otp.append(','.join(convert_to_hex(item) for item in line))
89+
data.append(f'vfs,data,fat,0x{offset:X},0x{vfs:X}')
9090

9191
with open(self.save_file_path, 'w') as f:
9292
f.write(PARTITION_HEADER)
93-
f.write('\n'.join(otp))
94-
95-
def read_csv(self):
96-
with open(self.load_file_path, 'r') as f:
97-
csv_data = f.read()
98-
99-
self._saved_data = csv_data
93+
f.write('\n'.join(data))
94+
f.write('\n')
10095

101-
csv_data = [
102-
line.strip()
103-
for line in csv_data.split('\n')
104-
if line.strip() and not line.startswith('#')
105-
]
106-
107-
def convert_to_int(elem):
108-
if elem.startswith('0'):
109-
elem = int(elem, 16)
110-
return elem
96+
def get_app_size(self) -> int:
97+
return self.factory
11198

112-
for j, line in enumerate(csv_data):
113-
line = [convert_to_int(item.strip()) for item in line.split(',')]
114-
csv_data[j] = line
99+
def set_app_size(self, size):
100+
factor = ((self.factory + size) / 4096.0) + 1
101+
self.factory = int(factor * 4096)
115102

116-
return csv_data
103+
def save(self):
104+
self.build_file()
117105

118106

119107
def get_espidf():
@@ -159,6 +147,8 @@ def get_espidf():
159147
disable_OTG = True
160148
onboard_mem_speed = 80
161149
flash_mode = 'QIO'
150+
optimize_size = False
151+
ota = False
162152

163153

164154
def common_args(extra_args):
@@ -170,6 +160,25 @@ def common_args(extra_args):
170160
global skip_partition_resize
171161
global partition_size
172162
global flash_size
163+
global board_variant
164+
global optimize_size
165+
global ota
166+
167+
if board == 'ARDUINO_NANO_ESP32':
168+
raise RuntimeError('Board is not currently supported')
169+
170+
if board in (
171+
'UM_NANOS3', 'ESP32_GENERIC_S3',
172+
'UM_TINYS3', 'UM_TINYWATCHS3'
173+
):
174+
def_flash_size = 8
175+
elif board in (
176+
'UM_FEATHERS2', 'SIL_WESP32',
177+
'UM_PROS3', 'UM_FEATHERS3',
178+
):
179+
def_flash_size = 16
180+
else:
181+
def_flash_size = 4
173182

174183
esp_argParser = ArgumentParser(prefix_chars='-BPd')
175184
esp_argParser.add_argument(
@@ -191,30 +200,32 @@ def common_args(extra_args):
191200
default=False,
192201
action='store_true'
193202
)
194-
195203
esp_argParser.add_argument(
196204
'--skip-partition-resize',
197205
dest='skip_partition_resize',
198206
help='clean the build',
199207
default=False,
200208
action='store_true'
201209
)
202-
203210
esp_argParser.add_argument(
204211
'--partition-size',
205212
dest='partition_size',
206213
default=-1,
207214
type=int,
208215
action='store'
209216
)
210-
217+
esp_argParser.add_argument(
218+
'--optimize-size',
219+
dest='optimize_size',
220+
default=False,
221+
action='store_true'
222+
)
211223
esp_argParser.add_argument(
212224
'--debug',
213225
dest='debug',
214226
default=False,
215227
action='store_true'
216228
)
217-
218229
esp_argParser.add_argument(
219230
'--ccache',
220231
dest='ccache',
@@ -227,10 +238,16 @@ def common_args(extra_args):
227238
dest='flash_size',
228239
help='flash size',
229240
choices=(4, 8, 16, 32, 64, 128),
230-
default=4,
241+
default=def_flash_size,
231242
type=int,
232243
action='store'
233244
)
245+
esp_argParser.add_argument(
246+
'--ota',
247+
dest='ota',
248+
default=False,
249+
action='store_true'
250+
)
234251

235252
esp_args, extra_args = esp_argParser.parse_known_args(extra_args)
236253

@@ -242,16 +259,18 @@ def common_args(extra_args):
242259
ccache = esp_args.ccache
243260
DEBUG = esp_args.debug
244261
flash_size = esp_args.flash_size
262+
optimize_size = esp_args.optimize_size
263+
ota = esp_args.ota
245264

246265
return extra_args
247266

248267

249268
def esp32_s3_args(extra_args):
250-
global board_variant
251269
global oct_flash
252270
global disable_OTG
253271
global onboard_mem_speed
254272
global flash_mode
273+
global board_variant
255274

256275
esp_argParser = ArgumentParser(prefix_chars='-B')
257276

@@ -261,21 +280,18 @@ def esp32_s3_args(extra_args):
261280
default='',
262281
action='store'
263282
)
264-
265283
esp_argParser.add_argument(
266284
'--USB-OTG',
267285
dest='usb_otg',
268286
default=False,
269287
action='store_true'
270288
)
271-
272289
esp_argParser.add_argument(
273290
'--octal-flash',
274291
help='octal spi flash',
275292
dest='oct_flash',
276293
action='store_true'
277294
)
278-
279295
esp_argParser.add_argument(
280296
'--onboard-mem-speed',
281297
dest='onboard_mem_speed',
@@ -284,7 +300,6 @@ def esp32_s3_args(extra_args):
284300
type=int,
285301
action='store'
286302
)
287-
288303
esp_argParser.add_argument(
289304
'--flash-mode',
290305
dest='flash_mode',
@@ -324,9 +339,9 @@ def esp32_s2_args(extra_args):
324339

325340
def esp32_args(extra_args):
326341
global board_variant
342+
global ota
327343

328-
esp_argParser = ArgumentParser(prefix_chars='-B')
329-
344+
esp_argParser = ArgumentParser(prefix_chars='B')
330345
esp_argParser.add_argument(
331346
'BOARD_VARIANT',
332347
dest='board_variant',
@@ -337,8 +352,16 @@ def esp32_args(extra_args):
337352
esp_args, extra_args = esp_argParser.parse_known_args(extra_args)
338353
board_variant = esp_args.board_variant
339354

340-
return extra_args
355+
if board_variant == 'OTA':
356+
board_variant = ''
357+
ota = True
341358

359+
elif board_variant == 'D2WD':
360+
raise RuntimeError(
361+
'board variant not supported, Not enough flash capacity'
362+
)
363+
364+
return extra_args
342365

343366
def parse_args(extra_args, lv_cflags, brd):
344367
global board
@@ -655,6 +678,7 @@ def submodules():
655678

656679
def compile(): # NOQA
657680
global PORT
681+
global flash_size
658682

659683
env, cmds = setup_idf_environ()
660684

@@ -679,7 +703,10 @@ def compile(): # NOQA
679703
'CONFIG_ESPTOOLPY_FLASHSIZE_16MB=n',
680704
'CONFIG_ESPTOOLPY_FLASHSIZE_32MB=n',
681705
'CONFIG_ESPTOOLPY_FLASHSIZE_64MB=n',
682-
'CONFIG_ESPTOOLPY_FLASHSIZE_128MB=n'
706+
'CONFIG_ESPTOOLPY_FLASHSIZE_128MB=n',
707+
'CONFIG_COMPILER_OPTIMIZATION_SIZE=n',
708+
'CONFIG_COMPILER_OPTIMIZATION_PERF=n',
709+
'CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT=y'
683710
]
684711

685712
if DEBUG:
@@ -718,6 +745,11 @@ def compile(): # NOQA
718745
f'"{SCRIPT_DIR}/build/partitions-{flash_size}MiB.csv"'
719746
]))
720747

748+
if optimize_size:
749+
base_config.append('CONFIG_COMPILER_OPTIMIZATION_SIZE=y')
750+
else:
751+
base_config.append('CONFIG_COMPILER_OPTIMIZATION_PERF=y')
752+
721753
base_config.append('CONFIG_ESPTOOLPY_FLASHFREQ_{onboard_mem_speed}M=y')
722754
base_config.append('CONFIG_SPIRAM_SPEED_{onboard_mem_speed}M=y')
723755

@@ -750,6 +782,9 @@ def compile(): # NOQA
750782
'../../../../build/sdkconfig.board)'
751783
)
752784

785+
partition = Partition()
786+
partition.save()
787+
753788
if sdkconfig not in data:
754789
data += '\n' + sdkconfig + '\n'
755790

@@ -826,12 +861,6 @@ def compile(): # NOQA
826861
sys.stdout.write('\n\033[31;1m***** Resizing Partition *****\033[0m\n')
827862
sys.stdout.flush()
828863

829-
partition_file_name = get_partition_file_name(output)
830-
partition_file_name = (
831-
os.path.join('lib/micropython/ports/esp32', partition_file_name)
832-
)
833-
partition = Partition(partition_file_name)
834-
835864
if partition_size != -1:
836865
overflow_amount = partition_size - partition.get_app_size()
837866
else:
@@ -854,13 +883,6 @@ def compile(): # NOQA
854883

855884
elif not skip_partition_resize:
856885
if 'build complete' in output:
857-
partition_file_name = get_partition_file_name(output)
858-
partition_file_name = os.path.join(
859-
'lib/micropython/ports/esp32',
860-
partition_file_name
861-
)
862-
partition = Partition(partition_file_name)
863-
864886
remaining = output.rsplit('application')[-1]
865887
remaining = int(
866888
remaining.split('(', 1)[-1].split('remaining')[0].strip()

0 commit comments

Comments
 (0)