upgrade.py 5.8 KB

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