stm32_update.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # Copyright (c) 2006-2022, RT-Thread Development Team
  2. #
  3. # SPDX-License-Identifier: Apache-2.0
  4. #
  5. # Change Logs:
  6. # Date Author Notes
  7. # 2021-10-11 Meco Man First version
  8. # This file is suggested to use under Linux environment.
  9. # > python stm32_update.py
  10. # update STM32 startup assembly language file:
  11. # 1.replace main to entry (GCC)
  12. # 2.reduce the heap size as 0x000 (Keil IAR)
  13. # 3.extend the GCC stack size as 0x400, which is the same as Keil and IAR startup files.
  14. import os
  15. import re
  16. # replace 'bl main' to 'bl entry'
  17. def stm32update_main2entry(path):
  18. oldline = ''
  19. newline = ''
  20. for root, dirs, files in os.walk(path):
  21. for file in files:
  22. if os.path.splitext(file)[1] == '.s': # find .s files (Keil MDK)
  23. file_path = os.path.join(root,file)
  24. flag_need_replace = False
  25. with open(file_path,'r+',) as f:
  26. while True:
  27. line = f.readline()
  28. if line == '':
  29. break
  30. elif ('bl' in line) and ('main' in line): # find 'bl main'
  31. oldline = line # bl main
  32. newline = line.replace('main', 'entry') # use 'entry' to replace 'main'
  33. flag_need_replace = True # mark that need to be replaced
  34. break
  35. if (flag_need_replace == True): # use 'entry' to replace 'main'
  36. f.seek(0)
  37. content = f.read()
  38. f.seek(0)
  39. f.truncate()
  40. newcontent = content.replace(oldline, newline)
  41. f.write(newcontent)
  42. #reduce the heap size as 0x000
  43. def stm32update_heap2zero(path):
  44. oldline = ''
  45. newline = ''
  46. for root, dirs, files in os.walk(path):
  47. for file in files:
  48. file_path = os.path.join(root,file)
  49. if os.path.splitext(file)[1] == '.s': # find .s files (Keil MDK)
  50. with open(file_path,'r+',) as f:
  51. flag_need_replace = False
  52. while True:
  53. line = f.readline()
  54. if line == '':
  55. break
  56. re_result = re.match('\\s*Heap_Size\\s+EQU\\s+0[xX][0-9a-fA-F]+', line)
  57. if re_result != None:
  58. oldline = line
  59. newline = re.sub('0[xX][0-9a-fA-F]+','0x00000000', oldline)
  60. flag_need_replace = True
  61. break
  62. if flag_need_replace == True:
  63. f.seek(0)
  64. content = f.read()
  65. f.seek(0)
  66. f.truncate()
  67. newcontent = content.replace(oldline, newline)
  68. f.write(newcontent)
  69. elif os.path.splitext(file)[1] == '.icf': # find .icf files (IAR)
  70. with open(file_path,'r+',) as f:
  71. flag_need_replace = False
  72. while True:
  73. line = f.readline()
  74. if line == '':
  75. break
  76. re_result = re.match('\\s*define\\s+symbol\\s+__ICFEDIT_size_heap__\\s*=\\s*0[xX][0-9a-fA-F]+', line)
  77. if re_result != None:
  78. oldline = line
  79. newline = re.sub('0[xX][0-9a-fA-F]+','0x000', oldline)
  80. flag_need_replace = True
  81. break
  82. if flag_need_replace == True:
  83. f.seek(0)
  84. content = f.read()
  85. f.seek(0)
  86. f.truncate()
  87. newcontent = content.replace(oldline, newline)
  88. f.write(newcontent)
  89. elif os.path.splitext(file)[1] == '.lds': # find .lds files (GCC)
  90. with open(file_path,'r+',) as f:
  91. flag_need_replace = False
  92. while True:
  93. line = f.readline()
  94. if line == '':
  95. break
  96. re_result = re.match('\\s*_system_stack_size\\s*=\\s*0[xX][0-9a-fA-F]+', line)
  97. if re_result != None:
  98. oldline = line
  99. newline = re.sub('0[xX][0-9a-fA-F]+','0x400', oldline)
  100. flag_need_replace = True
  101. break
  102. if flag_need_replace == True:
  103. f.seek(0)
  104. content = f.read()
  105. f.seek(0)
  106. f.truncate()
  107. newcontent = content.replace(oldline, newline)
  108. f.write(newcontent)
  109. def stm32_update(path):
  110. stm32update_main2entry(path)
  111. stm32update_heap2zero(path)
  112. if __name__ == "__main__":
  113. stm32_update(os.getcwd())
  114. print("STM32 startup assembly language file update successfully!")