stm32_update.py 4.9 KB

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