aboutsummaryrefslogtreecommitdiff
path: root/buildroot/share/vscode/create_custom_upload_command_CDC.py
blob: acfd1787fb24f862ce3131d404138f3252dd85de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python
#
#  Builds custom upload command
#    1) Run platformio as a subprocess to find a COM port
#    2) Build the upload command
#    3) Exit and let upload tool do the work
#
#  This script runs between completion of the library/dependencies installation and compilation.
#
#  Will continue on if a COM port isn't found so that the compilation can be done.
#

from __future__ import print_function
from __future__ import division

import subprocess
import os
import sys
from SCons.Script import DefaultEnvironment
import platform
current_OS = platform.system()

env = DefaultEnvironment()

build_type = os.environ.get("BUILD_TYPE", 'Not Set')


if not(build_type == 'upload' or build_type == 'traceback' or build_type == 'Not Set') :
  env.Replace(UPLOAD_PROTOCOL = 'teensy-gui')  # run normal Teensy2 scripts
else:
  com_first = ''
  com_last = ''
  com_CDC = ''
  description_first = ''
  description_last = ''
  description_CDC = ''

  #
  # grab the first com port that pops up unless we find one we know for sure
  # is a CDC device
  #
  def get_com_port(com_search_text, descr_search_text, start):

      global com_first
      global com_last
      global com_CDC
      global description_first
      global description_last
      global description_CDC


      print('\nLooking for Serial Port\n')

    # stream output from subprocess and split it into lines
      pio_subprocess = subprocess.Popen(['platformio', 'device', 'list'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

      looking_for_description = False
      for line in iter(pio_subprocess.stdout.readline, ''):
          if 0 <= line.find(com_search_text):
            looking_for_description = True
            com_last = line.replace('\n', '')
            if com_first == '':
              com_first = com_last
          if 0 <= line.find(descr_search_text) and looking_for_description:
            looking_for_description = False
            description_last = line[ start : ]
            if description_first == '':
              description_first = description_last
            if 0 <= description_last.find('CDC'):
              com_CDC = com_last
              description_CDC = description_last

      if  com_CDC == '' and com_first != '':
          com_CDC = com_first
          description_CDC = description_first
      elif com_CDC == '':
            com_CDC = 'COM_PORT_NOT_FOUND'

      while 0 <= com_CDC.find('\n'):
        com_CDC = com_CDC.replace('\n', '')
      while 0 <= com_CDC.find('\r'):
        com_CDC = com_CDC.replace('\r', '')

      if com_CDC == 'COM_PORT_NOT_FOUND':
          print(com_CDC, '\n')
      else:
          print('FOUND: ', com_CDC)
          print('DESCRIPTION: ', description_CDC, '\n')

  if current_OS == 'Windows':

      get_com_port('COM', 'Hardware ID:', 13)

  #    avrdude_conf_path =  env.get("PIOHOME_DIR") + '\\packages\\toolchain-atmelavr\\etc\\avrdude.conf'
      avrdude_conf_path =  'buildroot\\share\\atom\\avrdude.conf'

      avrdude_exe_path =  'buildroot\\share\\atom\\avrdude_5.10.exe'

  #    source_path = env.get("PROJECTBUILD_DIR") + '\\' + env.get("PIOENV") + '\\firmware.hex'
      source_path =  '.pio\\build\\' + env.get("PIOENV") + '\\firmware.hex'

      upload_string = avrdude_exe_path + ' -p usb1286 -c avr109 -P ' + com_CDC + ' -U flash:w:' + source_path + ':i'


  if current_OS == 'Darwin':  # MAC

      get_com_port('usbmodem', 'Description:', 13)

#      avrdude_conf_path =  env.get("PIOHOME_DIR") + '/packages/toolchain-atmelavr/etc/avrdude.conf'
      avrdude_conf_path =  'buildroot/share/vscode/avrdude_macOS.conf'


      avrdude_exe_path =  'buildroot/share/vscode/avrdude_5.10_macOS'

#      source_path = env.get("PROJECTBUILD_DIR") + '/' + env.get("PIOENV") + '/firmware.hex'
      source_path = '.pio/build/' + env.get("PIOENV") + '/firmware.hex'


#      upload_string = 'avrdude -p usb1286 -c avr109 -P ' + com_CDC + ' -U flash:w:' + source_path + ':i'
      upload_string = avrdude_exe_path + ' -p usb1286 -c avr109 -P ' + com_CDC + ' -C ' + avrdude_conf_path  + ' -U flash:w:' + source_path + ':i'
      print('upload_string: ', upload_string)



  if current_OS == 'Linux':

      get_com_port('/dev/tty', 'Description:', 13)

#      avrdude_conf_path =  env.get("PIOHOME_DIR") + '/packages/toolchain-atmelavr/etc/avrdude.conf'
      avrdude_conf_path =  'buildroot/share/vscode/avrdude_linux.conf'


      avrdude_exe_path =  'buildroot/share/vscode/avrdude_5.10_linux'
#      source_path = env.get("PROJECTBUILD_DIR") + '/' + env.get("PIOENV") + '/firmware.hex'
      source_path = '.pio/build/' + env.get("PIOENV") + '/firmware.hex'

#      upload_string = 'avrdude -p usb1286 -c avr109 -P ' + com_CDC + ' -U flash:w:' + source_path + ':i'
      upload_string = avrdude_exe_path + ' -p usb1286 -c avr109 -P ' + com_CDC + ' -C ' + avrdude_conf_path  + ' -U flash:w:' + source_path + ':i'


  env.Replace(
       UPLOADCMD = upload_string,
       MAXIMUM_RAM_SIZE = 8192,
       MAXIMUM_SIZE = 130048
  )