2023-05-15 09:45:58 +08:00
|
|
|
# Copyright (c) 2006-2022, RT-Thread Development Team
|
2021-10-11 14:10:18 +08:00
|
|
|
#
|
2023-05-15 09:45:58 +08:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2021-10-11 14:10:18 +08:00
|
|
|
#
|
|
|
|
# Change Logs:
|
|
|
|
# Date Author Notes
|
|
|
|
# 2021-10-11 Meco Man First version
|
|
|
|
|
2023-05-15 09:45:58 +08:00
|
|
|
# STM32 startup assembly language file:
|
|
|
|
# 1.replace main to entry (GCC)
|
|
|
|
# 2.reduce the heap size as 0x000 (Keil IAR)
|
|
|
|
# 3.extend the GCC stack size as 0x400, which is the same as Keil and IAR startup files.
|
2021-10-11 14:10:18 +08:00
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
2023-05-15 09:45:58 +08:00
|
|
|
# replace 'bl main' to 'bl entry'
|
|
|
|
def stm32update_main2entry(path):
|
2021-10-11 14:10:18 +08:00
|
|
|
oldline = ''
|
|
|
|
newline = ''
|
|
|
|
|
2023-05-15 09:45:58 +08:00
|
|
|
for root, dirs, files in os.walk(path):
|
2021-10-11 14:10:18 +08:00
|
|
|
for file in files:
|
2023-05-15 09:45:58 +08:00
|
|
|
if os.path.splitext(file)[1] == '.s': # find .s files (Keil MDK)
|
2021-10-11 14:10:18 +08:00
|
|
|
file_path = os.path.join(root,file)
|
|
|
|
flag_need_replace = False
|
|
|
|
with open(file_path,'r+',) as f:
|
|
|
|
while True:
|
|
|
|
line = f.readline()
|
|
|
|
if line == '':
|
|
|
|
break
|
2023-05-15 09:45:58 +08:00
|
|
|
elif ('bl' in line) and ('main' in line): # find 'bl main'
|
2021-10-11 14:10:18 +08:00
|
|
|
oldline = line # bl main
|
2023-05-15 09:45:58 +08:00
|
|
|
newline = line.replace('main', 'entry') # use 'entry' to replace 'main'
|
|
|
|
flag_need_replace = True # mark that need to be replaced
|
2021-10-11 14:10:18 +08:00
|
|
|
break
|
|
|
|
|
2023-05-15 09:45:58 +08:00
|
|
|
if (flag_need_replace == True): # use 'entry' to replace 'main'
|
2021-10-11 14:10:18 +08:00
|
|
|
f.seek(0)
|
|
|
|
content = f.read()
|
|
|
|
f.seek(0)
|
|
|
|
f.truncate()
|
|
|
|
newcontent = content.replace(oldline, newline)
|
|
|
|
f.write(newcontent)
|
|
|
|
|
2023-05-15 09:45:58 +08:00
|
|
|
#reduce the heap size as 0x000
|
|
|
|
def stm32update_heap2zero(path):
|
2021-10-11 14:10:18 +08:00
|
|
|
oldline = ''
|
|
|
|
newline = ''
|
2023-05-15 09:45:58 +08:00
|
|
|
for root, dirs, files in os.walk(path):
|
2021-10-11 14:10:18 +08:00
|
|
|
for file in files:
|
|
|
|
file_path = os.path.join(root,file)
|
2023-05-15 09:45:58 +08:00
|
|
|
if os.path.splitext(file)[1] == '.s': # find .s files (Keil MDK)
|
2021-10-11 14:10:18 +08:00
|
|
|
with open(file_path,'r+',) as f:
|
|
|
|
flag_need_replace = False
|
|
|
|
while True:
|
|
|
|
line = f.readline()
|
|
|
|
if line == '':
|
|
|
|
break
|
|
|
|
|
2023-05-15 09:45:58 +08:00
|
|
|
re_result = re.match('\s*Heap_Size\s+EQU\s+0[xX][0-9a-fA-F]+', line)
|
2021-10-11 14:10:18 +08:00
|
|
|
if re_result != None:
|
|
|
|
oldline = line
|
|
|
|
newline = re.sub('0[xX][0-9a-fA-F]+','0x00000000', oldline)
|
|
|
|
flag_need_replace = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if flag_need_replace == True:
|
|
|
|
f.seek(0)
|
|
|
|
content = f.read()
|
|
|
|
f.seek(0)
|
|
|
|
f.truncate()
|
|
|
|
newcontent = content.replace(oldline, newline)
|
|
|
|
f.write(newcontent)
|
|
|
|
|
2023-05-15 09:45:58 +08:00
|
|
|
elif os.path.splitext(file)[1] == '.icf': # find .icf files (IAR)
|
2021-10-11 14:10:18 +08:00
|
|
|
with open(file_path,'r+',) as f:
|
|
|
|
flag_need_replace = False
|
|
|
|
while True:
|
|
|
|
line = f.readline()
|
|
|
|
if line == '':
|
|
|
|
break
|
|
|
|
|
2023-05-15 09:45:58 +08:00
|
|
|
re_result = re.match('\s*define\s+symbol\s+__ICFEDIT_size_heap__\s*=\s*0[xX][0-9a-fA-F]+', line)
|
2021-10-11 14:10:18 +08:00
|
|
|
if re_result != None:
|
|
|
|
oldline = line
|
|
|
|
newline = re.sub('0[xX][0-9a-fA-F]+','0x000', oldline)
|
|
|
|
flag_need_replace = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if flag_need_replace == True:
|
|
|
|
f.seek(0)
|
|
|
|
content = f.read()
|
|
|
|
f.seek(0)
|
|
|
|
f.truncate()
|
|
|
|
newcontent = content.replace(oldline, newline)
|
|
|
|
f.write(newcontent)
|
|
|
|
|
2023-05-15 09:45:58 +08:00
|
|
|
elif os.path.splitext(file)[1] == '.lds': # find .lds files (GCC)
|
2021-10-11 14:10:18 +08:00
|
|
|
with open(file_path,'r+',) as f:
|
|
|
|
flag_need_replace = False
|
|
|
|
while True:
|
|
|
|
line = f.readline()
|
|
|
|
if line == '':
|
|
|
|
break
|
|
|
|
|
2023-05-15 09:45:58 +08:00
|
|
|
re_result = re.match('\s*_system_stack_size\s*=\s*0[xX][0-9a-fA-F]+', line)
|
2021-10-11 14:10:18 +08:00
|
|
|
if re_result != None:
|
|
|
|
oldline = line
|
2021-12-03 22:57:56 +08:00
|
|
|
newline = re.sub('0[xX][0-9a-fA-F]+','0x400', oldline)
|
2021-10-11 14:10:18 +08:00
|
|
|
flag_need_replace = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if flag_need_replace == True:
|
|
|
|
f.seek(0)
|
|
|
|
content = f.read()
|
|
|
|
f.seek(0)
|
|
|
|
f.truncate()
|
|
|
|
newcontent = content.replace(oldline, newline)
|
|
|
|
f.write(newcontent)
|
|
|
|
|
2023-05-15 09:45:58 +08:00
|
|
|
|
|
|
|
def stm32_update(path):
|
|
|
|
stm32update_main2entry(path)
|
|
|
|
stm32update_heap2zero(path)
|