Skip to content

Commit 73bd0da

Browse files
committed
scripts: generate_flash_algo.py: fixes and improvements.
- Increase default stack size to 2 KiB. - Precompute page buffers. - Fix type error for "hex" formatted data. - Don't print copyright warning for --info-only. - Include zi in SP computation. - Print all computed values before generating output. - Print warning if the flash has multiple sector sizes.
1 parent 7aedb94 commit 73bd0da

1 file changed

Lines changed: 42 additions & 10 deletions

File tree

scripts/generate_flash_algo.py

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
BLOB_HEADER = '0xe7fdbe00,'
3838
HEADER_SIZE = 4
3939

40-
STACK_SIZE = 0x200
40+
STACK_SIZE = 0x800
4141

4242
PYOCD_TEMPLATE = \
4343
"""# pyOCD debugger
@@ -78,7 +78,11 @@
7878
'page_size' : {{'0x%x' % algo.page_size}},
7979
'analyzer_supported' : False,
8080
'analyzer_address' : 0x00000000,
81-
'page_buffers' : [{{'0x%08x' % (entry + 4096)}}, {{'0x%08x' % (entry + 4096 + algo.page_size)}}], # Enable double buffering
81+
# Enable double buffering
82+
'page_buffers' : [
83+
{{'0x%08x' % (page_buffers[0])}},
84+
{{'0x%08x' % (page_buffers[1])}}
85+
],
8286
'min_program_length' : {{'0x%x' % algo.page_size}},
8387
8488
# Relative region addresses and sizes
@@ -131,7 +135,7 @@ def format_algo_data(self, spaces, group_size, fmt):
131135
blob = binascii.b2a_hex(self.algo_data)
132136
line_list = []
133137
for i in range(0, len(blob), group_size):
134-
line_list.append('"' + blob[i:i + group_size] + '"')
138+
line_list.append('"' + blob[i:i + group_size].decode() + '"')
135139
return ("\n" + padding).join(line_list)
136140
elif fmt == "c":
137141
blob = self.algo_data[:]
@@ -145,7 +149,7 @@ def format_algo_data(self, spaces, group_size, fmt):
145149
line_list.append(", ".join(group))
146150
return (",\n" + padding).join(line_list)
147151
else:
148-
raise Exception("Unsupported format %s" % fmt)
152+
raise ValueError("Unsupported format %s" % fmt)
149153

150154
def process_template(self, template_text, data_dict=None):
151155
"""
@@ -184,7 +188,7 @@ def main():
184188
parser.add_argument('-c', '--copyright', help="Set copyright owner.")
185189
args = parser.parse_args()
186190

187-
if not args.copyright:
191+
if not args.copyright and not args.info_only:
188192
print(f"{colorama.Fore.YELLOW}Warning! No copyright owner was specified. Defaulting to \"PyOCD Authors\". "
189193
f"Please set via --copyright, or edit output.{colorama.Style.RESET_ALL}")
190194

@@ -199,21 +203,49 @@ def main():
199203

200204
print(algo.flash_info)
201205

202-
if args.info_only:
203-
return
204-
205-
# Allocate stack after algo and its rw data, with top and bottom rounded to 8 bytes.
206-
stack_base = args.blob_start + HEADER_SIZE + algo.rw_start + algo.rw_size
206+
# Allocate stack after algo and its rw/zi data, with top and bottom rounded to 8 bytes.
207+
stack_base = (args.blob_start + HEADER_SIZE
208+
+ algo.rw_start + algo.rw_size # rw_start incorporates instruction size
209+
+ algo.zi_size)
207210
stack_base = (stack_base + 7) // 8 * 8
208211
sp = stack_base + args.stack_size
209212
sp = (sp + 7) // 8 * 8
210213

214+
page_buffers = [
215+
((sp + algo.page_size - 1) // algo.page_size * algo.page_size),
216+
((sp + algo.page_size - 1) // algo.page_size * algo.page_size) + algo.page_size,
217+
]
218+
219+
# Increase stack size by placing the SP at the base of first page buffer.
220+
sp = page_buffers[0]
221+
222+
print(f"load addr: {args.blob_start:#010x}")
223+
print(f"header: {HEADER_SIZE:#x} bytes")
224+
print(f"data: {len(algo.algo_data):#x} bytes")
225+
print(f"ro: {algo.ro_start:#010x} + {algo.ro_size:#x} bytes")
226+
print(f"rw: {algo.rw_start:#010x} + {algo.rw_size:#x} bytes")
227+
print(f"zi: {algo.zi_start:#010x} + {algo.zi_size:#x} bytes")
228+
print(f"stack: {stack_base:#010x} .. {sp:#010x} ({sp - stack_base:#x} bytes)")
229+
print(f"buffer[0]: {page_buffers[0]:#010x}")
230+
print(f"buffer[1]: {page_buffers[1]:#010x}")
231+
232+
print("\nSymbol offsets:")
233+
for n, v in algo.symbols.items():
234+
print(f"{n}:{' ' * (11 - len(n))} {v:#010x}")
235+
236+
if args.info_only:
237+
return
238+
239+
if len(algo.sector_sizes) > 1:
240+
print(f"{colorama.Fore.YELLOW}Warning! Flash has more than one sector size. Remember to create one flash memory region for each sector size range.{colorama.Style.RESET_ALL}")
241+
211242
data_dict = {
212243
'name': os.path.splitext(os.path.split(args.elf_path)[-1])[0],
213244
'prog_header': BLOB_HEADER,
214245
'header_size': HEADER_SIZE,
215246
'entry': args.blob_start,
216247
'stack_pointer': sp,
248+
'page_buffers': page_buffers,
217249
'year': datetime.now().year,
218250
'copyright_owner': args.copyright or "PyOCD Authors",
219251
}

0 commit comments

Comments
 (0)