From 1c90cb36204c18c7f5d5e5d9f0608930ddd93891 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Mon, 11 Oct 2021 02:10:18 -0400 Subject: [PATCH] =?UTF-8?q?[tools]=20=E5=A2=9E=E5=8A=A0=E5=B0=86=E6=B1=87?= =?UTF-8?q?=E7=BC=96=E5=90=AF=E5=8A=A8=E6=96=87=E4=BB=B6=E5=85=A5=E5=8F=A3?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E7=94=B1main=E6=94=B9=E4=B8=BAentry=E7=9A=84?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=89=AB=E6=8F=8F=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bsp/stm32/tools/upgrade.py | 142 +++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 bsp/stm32/tools/upgrade.py diff --git a/bsp/stm32/tools/upgrade.py b/bsp/stm32/tools/upgrade.py new file mode 100644 index 0000000000..12e34bf7ab --- /dev/null +++ b/bsp/stm32/tools/upgrade.py @@ -0,0 +1,142 @@ +# +# File : upgrade.py +# This file is part of RT-Thread RTOS +# COPYRIGHT (C) 2006 - 2021, RT-Thread Development Team +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Change Logs: +# Date Author Notes +# 2021-10-11 Meco Man First version +# + +# 本文件用于在HAL库更新之后 +# 1.对gcc的汇编启动文件中main替换为entry函数 +# 2.将启动文件heap降为0 + + +#使用方法:运行脚本,将bsp/stm32的绝对路径传给脚本即可,如:C:\Users\92036\Desktop\rt-thread\bsp\stm32 + +import os +import re + +#将'bl main' 替换为 'bl entry' +def main2entry(path): + oldline = '' + newline = '' + + for root, dirs, files in os.walk(path): #递归扫描里面的所有文件 + for file in files: + if os.path.splitext(file)[1] == '.s': #找.s文件 + 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 + elif ('bl' in line) and ('main' in line): #发现'bl main' + oldline = line # bl main + newline = line.replace('main', 'entry') #将main替换为entry,形成新的字符串 + flag_need_replace = True #标记该文件需要做entry替换 + break + + if (flag_need_replace == True): #若该文件需要将main替换为entry + f.seek(0) + content = f.read() + f.seek(0) + f.truncate() + newcontent = content.replace(oldline, newline) + f.write(newcontent) + +#将启动文件的heap降为0 +def heap2zero(path): + oldline = '' + newline = '' + for root, dirs, files in os.walk(path): #递归扫描里面的所有文件 + for file in files: + file_path = os.path.join(root,file) + if os.path.splitext(file)[1] == '.s': #找.s文件 + with open(file_path,'r+',) as f: + flag_need_replace = False + while True: + line = f.readline() + if line == '': + break + + re_result = re.match('\s*Heap_Size\s+EQU\s+0[xX][0-9a-fA-F]+', line) #MDK的表示方法 + 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) + + elif os.path.splitext(file)[1] == '.icf': #找.icf文件(IAR) + with open(file_path,'r+',) as f: + flag_need_replace = False + while True: + line = f.readline() + if line == '': + break + + re_result = re.match('\s*define\s+symbol\s+__ICFEDIT_size_heap__\s*=\s*0[xX][0-9a-fA-F]+', line) #IAR的表示方法 + 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) + + elif os.path.splitext(file)[1] == '.lds': #找.lds文件(GCC) + with open(file_path,'r+',) as f: + flag_need_replace = False + while True: + line = f.readline() + if line == '': + break + + re_result = re.match('\s*_system_stack_size\s*=\s*0[xX][0-9a-fA-F]+', line) #GCC的表示方法 + 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) + +folder_path = input('please input path:') +main2entry(folder_path) +heap2zero(folder_path)