create_custom_upload_command_CDC.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env python
  2. #
  3. # Builds custom upload command
  4. # 1) Run platformio as a subprocess to find a COM port
  5. # 2) Build the upload command
  6. # 3) Exit and let upload tool do the work
  7. #
  8. # This script runs between completion of the library/dependencies installation and compilation.
  9. #
  10. # Will continue on if a COM port isn't found so that the compilation can be done.
  11. #
  12. from __future__ import print_function
  13. from __future__ import division
  14. import subprocess
  15. import os
  16. import sys
  17. from SCons.Script import DefaultEnvironment
  18. import platform
  19. current_OS = platform.system()
  20. env = DefaultEnvironment()
  21. build_type = os.environ.get("BUILD_TYPE", 'Not Set')
  22. if not(build_type == 'upload' or build_type == 'traceback' or build_type == 'Not Set') :
  23. env.Replace(UPLOAD_PROTOCOL = 'teensy-gui') # run normal Teensy2 scripts
  24. else:
  25. com_first = ''
  26. com_last = ''
  27. com_CDC = ''
  28. description_first = ''
  29. description_last = ''
  30. description_CDC = ''
  31. #
  32. # grab the first com port that pops up unless we find one we know for sure
  33. # is a CDC device
  34. #
  35. def get_com_port(com_search_text, descr_search_text, start):
  36. global com_first
  37. global com_last
  38. global com_CDC
  39. global description_first
  40. global description_last
  41. global description_CDC
  42. print('\nLooking for Serial Port\n')
  43. # stream output from subprocess and split it into lines
  44. pio_subprocess = subprocess.Popen(['platformio', 'device', 'list'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  45. looking_for_description = False
  46. for line in iter(pio_subprocess.stdout.readline, ''):
  47. if 0 <= line.find(com_search_text):
  48. looking_for_description = True
  49. com_last = line.replace('\n', '')
  50. if com_first == '':
  51. com_first = com_last
  52. if 0 <= line.find(descr_search_text) and looking_for_description:
  53. looking_for_description = False
  54. description_last = line[ start : ]
  55. if description_first == '':
  56. description_first = description_last
  57. if 0 <= description_last.find('CDC'):
  58. com_CDC = com_last
  59. description_CDC = description_last
  60. if com_CDC == '' and com_first != '':
  61. com_CDC = com_first
  62. description_CDC = description_first
  63. elif com_CDC == '':
  64. com_CDC = 'COM_PORT_NOT_FOUND'
  65. while 0 <= com_CDC.find('\n'):
  66. com_CDC = com_CDC.replace('\n', '')
  67. while 0 <= com_CDC.find('\r'):
  68. com_CDC = com_CDC.replace('\r', '')
  69. if com_CDC == 'COM_PORT_NOT_FOUND':
  70. print(com_CDC, '\n')
  71. else:
  72. print('FOUND: ', com_CDC)
  73. print('DESCRIPTION: ', description_CDC, '\n')
  74. if current_OS == 'Windows':
  75. get_com_port('COM', 'Hardware ID:', 13)
  76. # avrdude_conf_path = env.get("PIOHOME_DIR") + '\\packages\\toolchain-atmelavr\\etc\\avrdude.conf'
  77. avrdude_conf_path = 'buildroot\\share\\atom\\avrdude.conf'
  78. avrdude_exe_path = 'buildroot\\share\\atom\\avrdude_5.10.exe'
  79. # source_path = env.get("PROJECTBUILD_DIR") + '\\' + env.get("PIOENV") + '\\firmware.hex'
  80. source_path = '.pio\\build\\' + env.get("PIOENV") + '\\firmware.hex'
  81. upload_string = avrdude_exe_path + ' -p usb1286 -c avr109 -P ' + com_CDC + ' -U flash:w:' + source_path + ':i'
  82. if current_OS == 'Darwin': # MAC
  83. get_com_port('usbmodem', 'Description:', 13)
  84. # avrdude_conf_path = env.get("PIOHOME_DIR") + '/packages/toolchain-atmelavr/etc/avrdude.conf'
  85. avrdude_conf_path = 'buildroot/share/atom/avrdude_macOS.conf'
  86. avrdude_exe_path = 'buildroot/share/atom/avrdude_5.10_macOS'
  87. # source_path = env.get("PROJECTBUILD_DIR") + '/' + env.get("PIOENV") + '/firmware.hex'
  88. source_path = '.pio/build/' + env.get("PIOENV") + '/firmware.hex'
  89. # upload_string = 'avrdude -p usb1286 -c avr109 -P ' + com_CDC + ' -U flash:w:' + source_path + ':i'
  90. upload_string = avrdude_exe_path + ' -p usb1286 -c avr109 -P ' + com_CDC + ' -C ' + avrdude_conf_path + ' -U flash:w:' + source_path + ':i'
  91. print('upload_string: ', upload_string)
  92. if current_OS == 'Linux':
  93. get_com_port('/dev/tty', 'Description:', 13)
  94. # avrdude_conf_path = env.get("PIOHOME_DIR") + '/packages/toolchain-atmelavr/etc/avrdude.conf'
  95. avrdude_conf_path = 'buildroot/share/atom/avrdude_linux.conf'
  96. avrdude_exe_path = 'buildroot/share/atom/avrdude_5.10_linux'
  97. # source_path = env.get("PROJECTBUILD_DIR") + '/' + env.get("PIOENV") + '/firmware.hex'
  98. source_path = '.pio/build/' + env.get("PIOENV") + '/firmware.hex'
  99. # upload_string = 'avrdude -p usb1286 -c avr109 -P ' + com_CDC + ' -U flash:w:' + source_path + ':i'
  100. upload_string = avrdude_exe_path + ' -p usb1286 -c avr109 -P ' + com_CDC + ' -C ' + avrdude_conf_path + ' -U flash:w:' + source_path + ':i'
  101. env.Replace(
  102. UPLOADCMD = upload_string,
  103. MAXIMUM_RAM_SIZE = 8192,
  104. MAXIMUM_SIZE = 130048
  105. )