1#
2# File      : upgrade.py
3# This file is part of RT-Thread RTOS
4# COPYRIGHT (C) 2006 - 2021, RT-Thread Development Team
5#
6#  This program is free software; you can redistribute it and/or modify
7#  it under the terms of the GNU General Public License as published by
8#  the Free Software Foundation; either version 2 of the License, or
9#  (at your option) any later version.
10#
11#  This program is distributed in the hope that it will be useful,
12#  but WITHOUT ANY WARRANTY; without even the implied warranty of
13#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14#  GNU General Public License for more details.
15#
16#  You should have received a copy of the GNU General Public License along
17#  with this program; if not, write to the Free Software Foundation, Inc.,
18#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19#
20# Change Logs:
21# Date           Author       Notes
22# 2021-10-11     Meco Man     First version
23#
24
25# 本文件用于在HAL库更新之后
26# 1.对gcc的汇编启动文件中main替换为entry函数
27# 2.将启动文件heap降为0(Keil IAR)
28# 3.将GCC的堆大小扩展到0x400,与Keil IAR保持一致
29
30
31#使用方法:运行脚本,将bsp/stm32的绝对路径传给脚本即可,如:C:\Users\92036\Desktop\rt-thread\bsp\stm32
32
33#特别说明:此脚本是借用RTT原BSP中STM32的,对ST的开源表示非常感谢!!!
34
35import os
36import re
37
38#将'bl main' 替换为 'bl entry'
39def main2entry(path):
40    oldline = ''
41    newline = ''
42
43    for root, dirs, files in os.walk(path): #递归扫描里面的所有文件
44        for file in files:
45            if os.path.splitext(file)[1] == '.s': #找.s文件
46                file_path = os.path.join(root,file)
47                flag_need_replace = False
48                with open(file_path,'r+',) as f:
49                    while True:
50                        line = f.readline()
51                        if line == '':
52                            break
53                        elif ('bl' in line) and ('main' in line): #发现'bl main'
54                            oldline = line # bl main
55                            newline = line.replace('main', 'entry') #将main替换为entry,形成新的字符串
56                            flag_need_replace = True #标记该文件需要做entry替换
57                            break
58
59                    if (flag_need_replace == True): #若该文件需要将main替换为entry
60                        f.seek(0)
61                        content = f.read()
62                        f.seek(0)
63                        f.truncate()
64                        newcontent = content.replace(oldline, newline)
65                        f.write(newcontent)
66
67#将启动文件的heap降为0
68def heap2zero(path):
69    oldline = ''
70    newline = ''
71    for root, dirs, files in os.walk(path): #递归扫描里面的所有文件
72        for file in files:
73            file_path = os.path.join(root,file)
74            if os.path.splitext(file)[1] == '.s': #找.s文件
75                with open(file_path,'r+',) as f:
76                    flag_need_replace = False
77                    while True:
78                        line = f.readline()
79                        if line == '':
80                            break
81
82                        re_result = re.match('\s*Heap_Size\s+EQU\s+0[xX][0-9a-fA-F]+', line) #MDK的表示方法
83                        if re_result != None:
84                            oldline = line
85                            newline = re.sub('0[xX][0-9a-fA-F]+','0x00000000', oldline)
86                            flag_need_replace = True
87                            break
88
89                    if flag_need_replace == True:
90                        f.seek(0)
91                        content = f.read()
92                        f.seek(0)
93                        f.truncate()
94                        newcontent = content.replace(oldline, newline)
95                        f.write(newcontent)
96
97            elif os.path.splitext(file)[1] == '.icf': #找.icf文件(IAR)
98                with open(file_path,'r+',) as f:
99                    flag_need_replace = False
100                    while True:
101                        line = f.readline()
102                        if line == '':
103                            break
104
105                        re_result = re.match('\s*define\s+symbol\s+__ICFEDIT_size_heap__\s*=\s*0[xX][0-9a-fA-F]+', line) #IAR的表示方法
106                        if re_result != None:
107                            oldline = line
108                            newline = re.sub('0[xX][0-9a-fA-F]+','0x000', oldline)
109                            flag_need_replace = True
110                            break
111
112                    if flag_need_replace == True:
113                        f.seek(0)
114                        content = f.read()
115                        f.seek(0)
116                        f.truncate()
117                        newcontent = content.replace(oldline, newline)
118                        f.write(newcontent)
119
120            elif os.path.splitext(file)[1] == '.lds': #找.lds文件(GCC)
121                with open(file_path,'r+',) as f:
122                    flag_need_replace = False
123                    while True:
124                        line = f.readline()
125                        if line == '':
126                            break
127
128                        re_result = re.match('\s*_system_stack_size\s*=\s*0[xX][0-9a-fA-F]+', line) #GCC的表示方法, 将默认的栈大小增加到0x400
129                        if re_result != None:
130                            oldline = line
131                            newline = re.sub('0[xX][0-9a-fA-F]+','0x400', oldline)
132                            flag_need_replace = True
133                            break
134
135                    if flag_need_replace == True:
136                        f.seek(0)
137                        content = f.read()
138                        f.seek(0)
139                        f.truncate()
140                        newcontent = content.replace(oldline, newline)
141                        f.write(newcontent)
142
143folder_path = input('please input path:')
144main2entry(folder_path)
145heap2zero(folder_path)
146