upgrade.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. # 本文件用于在HAL库更新之后
  25. # 1.对gcc的汇编启动文件中main替换为entry函数
  26. # 2.将启动文件heap降为0(Keil IAR)
  27. # 3.将GCC的堆大小扩展到0x400,与Keil IAR保持一致
  28. #使用方法:运行脚本,将bsp/stm32的绝对路径传给脚本即可,如:C:\Users\92036\Desktop\rt-thread\bsp\stm32
  29. #特别说明:此脚本是借用RTT原BSP中STM32的,对ST的开源表示非常感谢!!!
  30. import os
  31. import re
  32. #将'bl main' 替换为 'bl entry'
  33. def main2entry(path):
  34. oldline = ''
  35. newline = ''
  36. for root, dirs, files in os.walk(path): #递归扫描里面的所有文件
  37. for file in files:
  38. if os.path.splitext(file)[1] == '.s': #找.s文件
  39. file_path = os.path.join(root,file)
  40. flag_need_replace = False
  41. with open(file_path,'r+',) as f:
  42. while True:
  43. line = f.readline()
  44. if line == '':
  45. break
  46. elif ('bl' in line) and ('main' in line): #发现'bl main'
  47. oldline = line # bl main
  48. newline = line.replace('main', 'entry') #将main替换为entry,形成新的字符串
  49. flag_need_replace = True #标记该文件需要做entry替换
  50. break
  51. if (flag_need_replace == True): #若该文件需要将main替换为entry
  52. f.seek(0)
  53. content = f.read()
  54. f.seek(0)
  55. f.truncate()
  56. newcontent = content.replace(oldline, newline)
  57. f.write(newcontent)
  58. #将启动文件的heap降为0
  59. def heap2zero(path):
  60. oldline = ''
  61. newline = ''
  62. for root, dirs, files in os.walk(path): #递归扫描里面的所有文件
  63. for file in files:
  64. file_path = os.path.join(root,file)
  65. if os.path.splitext(file)[1] == '.s': #找.s文件
  66. with open(file_path,'r+',) as f:
  67. flag_need_replace = False
  68. while True:
  69. line = f.readline()
  70. if line == '':
  71. break
  72. re_result = re.match('\s*Heap_Size\s+EQU\s+0[xX][0-9a-fA-F]+', line) #MDK的表示方法
  73. if re_result != None:
  74. oldline = line
  75. newline = re.sub('0[xX][0-9a-fA-F]+','0x00000000', oldline)
  76. flag_need_replace = True
  77. break
  78. if flag_need_replace == True:
  79. f.seek(0)
  80. content = f.read()
  81. f.seek(0)
  82. f.truncate()
  83. newcontent = content.replace(oldline, newline)
  84. f.write(newcontent)
  85. elif os.path.splitext(file)[1] == '.icf': #找.icf文件(IAR)
  86. with open(file_path,'r+',) as f:
  87. flag_need_replace = False
  88. while True:
  89. line = f.readline()
  90. if line == '':
  91. break
  92. re_result = re.match('\s*define\s+symbol\s+__ICFEDIT_size_heap__\s*=\s*0[xX][0-9a-fA-F]+', line) #IAR的表示方法
  93. if re_result != None:
  94. oldline = line
  95. newline = re.sub('0[xX][0-9a-fA-F]+','0x000', oldline)
  96. flag_need_replace = True
  97. break
  98. if flag_need_replace == True:
  99. f.seek(0)
  100. content = f.read()
  101. f.seek(0)
  102. f.truncate()
  103. newcontent = content.replace(oldline, newline)
  104. f.write(newcontent)
  105. elif os.path.splitext(file)[1] == '.lds': #找.lds文件(GCC)
  106. with open(file_path,'r+',) as f:
  107. flag_need_replace = False
  108. while True:
  109. line = f.readline()
  110. if line == '':
  111. break
  112. re_result = re.match('\s*_system_stack_size\s*=\s*0[xX][0-9a-fA-F]+', line) #GCC的表示方法, 将默认的栈大小增加到0x400
  113. if re_result != None:
  114. oldline = line
  115. newline = re.sub('0[xX][0-9a-fA-F]+','0x400', oldline)
  116. flag_need_replace = True
  117. break
  118. if flag_need_replace == True:
  119. f.seek(0)
  120. content = f.read()
  121. f.seek(0)
  122. f.truncate()
  123. newcontent = content.replace(oldline, newline)
  124. f.write(newcontent)
  125. folder_path = input('please input path:')
  126. main2entry(folder_path)
  127. heap2zero(folder_path)