vscpyocd.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import os
  2. import sys
  3. import json
  4. def create_need_files(targetId, packpath):
  5. """
  6. Generate pyocd.yaml files
  7. """
  8. yaml_file = open('pyocd.yaml', 'w')
  9. if yaml_file:
  10. yaml_file.write('\npack:\n')
  11. yaml_file.write(' - ' + packpath + '\n')
  12. yaml_file.close()
  13. """
  14. Generate .vscode/launch.json files
  15. """
  16. vsc_launch_file = open('.vscode/launch.json', 'w')
  17. if vsc_launch_file:
  18. config_obj = {}
  19. config_obj['name'] = 'Cortex Debug'
  20. config_obj['cwd'] = '${workspaceFolder}'
  21. config_obj['executable'] = 'rt-thread.elf'
  22. config_obj['request'] = 'launch'
  23. config_obj['type'] = 'cortex-debug'
  24. config_obj['runToEntryPoint'] = 'Reset_Handler'
  25. config_obj['servertype'] = 'pyocd'
  26. if os.getenv('RTT_EXEC_PATH'):
  27. config_obj['armToolchainPath'] = os.getenv('RTT_EXEC_PATH').replace('\\', '/')
  28. else:
  29. print('env <RTT_EXEC_PATH> not set!')
  30. config_obj['toolchainPrefix'] = 'arm-none-eabi'
  31. config_obj['targetId'] = targetId
  32. json_obj = {}
  33. json_obj['version'] = '0.2.0'
  34. json_obj['configurations'] = [config_obj]
  35. vsc_launch_file.write(json.dumps(json_obj, ensure_ascii=False, indent=4))
  36. vsc_launch_file.close()
  37. """
  38. Generate .vscode/tasks.json files
  39. """
  40. vsc_tasks_file = open('.vscode/tasks.json', 'w')
  41. if vsc_tasks_file:
  42. task_build_obj = {}
  43. task_build_obj['type'] = 'shell'
  44. task_build_obj['label'] = 'Build target files'
  45. task_build_obj['command'] = 'scons'
  46. task_build_obj['args'] = ['-j12']
  47. task_build_obj['problemMatcher'] = ['$gcc']
  48. task_build_obj['group'] = 'build'
  49. task_download_obj = {}
  50. task_download_obj['type'] = 'shell'
  51. task_download_obj['label'] = 'Download code to flash memory'
  52. task_download_obj['command'] = 'python'
  53. task_download_obj['args'] = ['-m', 'pyocd', 'flash', '--erase', 'chip', '--target', \
  54. targetId, 'rt-thread.elf']
  55. task_download_obj['problemMatcher'] = ['$gcc']
  56. task_download_obj['group'] = 'build'
  57. task_build_download_obj = task_download_obj.copy()
  58. task_build_download_obj['label'] = 'Build and Download'
  59. task_build_download_obj['dependsOn'] = 'Build target files'
  60. json_obj = {}
  61. json_obj['version'] = '2.0.0'
  62. json_obj['tasks'] = [task_build_obj, task_download_obj, task_build_download_obj]
  63. vsc_tasks_file.write(json.dumps(json_obj, ensure_ascii=False, indent=4))
  64. vsc_tasks_file.close()
  65. def similar_char_num(str1, str2):
  66. lstr1 = len(str1)
  67. lstr2 = len(str2)
  68. record = [[0 for i in range(lstr2+1)] for j in range(lstr1+1)]
  69. similar_num = 0
  70. for i in range(lstr1):
  71. for j in range(lstr2):
  72. if str1[i] == str2[j]:
  73. record[i+1][j+1] = record[i][j] + 1
  74. if record[i+1][j+1] > similar_num:
  75. similar_num = record[i+1][j+1]
  76. return similar_num
  77. def get_socName_from_rtconfig():
  78. socName = None
  79. rtconfig_file = open('rtconfig.h', 'r')
  80. if rtconfig_file:
  81. for line in rtconfig_file.readlines():
  82. if 'SOC' in line and 'FAMILY' not in line and 'SERIES' not in line:
  83. socName = line.strip().split('_')[-1]
  84. rtconfig_file.close()
  85. return socName
  86. def get_pack_from_env():
  87. if os.environ.get('ENV_ROOT') == None:
  88. if sys.platform == 'win32':
  89. home_dir = os.environ['USERPROFILE']
  90. env_dir = os.path.join(home_dir, '.env')
  91. else:
  92. home_dir = os.environ['HOME']
  93. env_dir = os.path.join(home_dir, '.env')
  94. else:
  95. env_dir = os.environ.get('ENV_ROOT')
  96. pack_dir = env_dir.replace('\\', '/') + '/tools/cmsisPacks/'
  97. if not os.path.exists(pack_dir):
  98. print('<%s> does not exist, please create.' % pack_dir)
  99. return
  100. # get soc name from <rtconfig.h> file
  101. socName = get_socName_from_rtconfig()
  102. if socName == None:
  103. return
  104. # Find the pack that best matches soc name
  105. max_similar_num = 0
  106. max_similar_pack = None
  107. for file in os.listdir(pack_dir):
  108. if str(file).endswith('.pack'):
  109. similar_num = similar_char_num(socName, file)
  110. if(similar_num > max_similar_num):
  111. max_similar_num = similar_num
  112. max_similar_pack = file
  113. print('SOC<%s> match the pack is <%s>' % (socName, max_similar_pack))
  114. if max_similar_pack == None:
  115. return
  116. return pack_dir + max_similar_pack
  117. def get_trgetId_from_pack(pack):
  118. # get soc name from <rtconfig.h> file
  119. socName = get_socName_from_rtconfig()
  120. if socName == None:
  121. return
  122. # View the soc supported by the most similar cmsisPack
  123. result = os.popen('python -m pyocd json --target --pack ' + pack)
  124. pyocd_json = json.loads(result.read())
  125. if pyocd_json['status'] != 0:
  126. return
  127. # Find the targetId that best matches soc name
  128. max_similar_num = 0
  129. max_similar_targetId = None
  130. for target in pyocd_json['targets']:
  131. if (target['source'] == 'pack'):
  132. similar_num = similar_char_num(socName.lower(), target['name'])
  133. if(similar_num > max_similar_num):
  134. max_similar_num = similar_num
  135. max_similar_targetId = target['name']
  136. print('SOC<%s> match the targetId is <%s>' % (socName, max_similar_targetId))
  137. if max_similar_targetId == None:
  138. return
  139. if max_similar_num < len(socName):
  140. print('<%s> not match the <%s>' % (socName, pack))
  141. return
  142. return max_similar_targetId
  143. def GenerateVSCodePyocdConfig(pack):
  144. if pack == 'env':
  145. pack = get_pack_from_env()
  146. if pack == None:
  147. return
  148. else:
  149. # Check is exist
  150. if not os.path.exists(pack):
  151. return
  152. # Check is file
  153. if not os.path.isfile(pack):
  154. return
  155. # Check is pack
  156. if not str(pack).endswith('.pack'):
  157. return
  158. pack = pack.replace('\\', '/')
  159. targetId = get_trgetId_from_pack(pack)
  160. if targetId ==None:
  161. return
  162. create_need_files(targetId, pack)
  163. print('Pyocd Config Done!')
  164. return