program_cc430.py 3.6 KB
Newer Older
Reto Da Forno's avatar
initial  
Reto Da Forno committed
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
#!/usr/bin/env python3
'''

Flash a hex file onto the CC430 MCU via the bootloader.

This routing only works for the DPP2 DevBoard.
The solder jumpers J502/J503 on the back of the DevBoard need to be closed.
The script will automatically pick the first available serial port that is
identified as an FTDI Dual RS232 device.

last update: 2018-07-24
author:      rdaforno

'''

import sys
import os.path
import serial
import serial.tools.list_ports
import subprocess
import time


resetAfterProgram = True


Reto Da Forno's avatar
Reto Da Forno committed
27
28
29
30
31
32
33
34
def listAvailablePorts():
  print("available serial ports:")
  ports = [p for p in serial.tools.list_ports.comports()]
  for p in sorted(ports):
    print("%s" % p)


def getDPPDevBoardPort(printPorts):
Reto Da Forno's avatar
initial  
Reto Da Forno committed
35
36
37
38
39
40
41
42
43
  ports = [p for p in serial.tools.list_ports.comports() if "Dual RS232" in p[1]]
  if printPorts:
    for p in sorted(ports):
      print("%s" % p)
  if ports is None or len(ports) == 0:
    return None
  return sorted(ports)[1][0]


Reto Da Forno's avatar
Reto Da Forno committed
44
45
46
47
48
49
50
51
52
53
def getMSPFETPort(printPorts):
  ports = [p for p in serial.tools.list_ports.comports() if "MSP Debug" in p[1]]
  if printPorts:
    for p in sorted(ports):
      print("%s" % p)
  if ports is None or len(ports) == 0:
    return None
  return sorted(ports)[0][0]


Reto Da Forno's avatar
initial  
Reto Da Forno committed
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
def checkSerialPort(serialPort):
  try:
    ser = serial.Serial(port=serialPort, baudrate=115200)
    if ser.is_open:
      ser.close()
      return True
  except:
    print("can't connect to serial port " + serialPort)
  return False


def resetMCU(serialPort):
  try:
    ser = serial.Serial(port=serialPort, baudrate=115200, xonxoff=0, rtscts=0)
    if ser.is_open:
      ser.setDTR(True)        # pull reset line
      ser.setRTS(True)        # pull TEST / BSL entry line
      time.sleep(0.1)
      ser.setDTR(False)       # release reset line
      ser.close()
      print("target reset")
  except:
    print("failed to connect to serial port " + serialPort)


Reto Da Forno's avatar
Reto Da Forno committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
def programWithMSPFET(serialPort, fileName):
  if "LD_LIBRARY_PATH" in os.environ:
    cmd = "mspdebug tilib 'prog " + fileName + "' --allow-fw-update"
    if serialPort:
      cmd += " -d " + serialPort
    ret = subprocess.call(cmd, shell=True)
    if ret == 0:
      return True
    print("failed")
  else:
    print("can't use MSP-FET, LD_LIBRARY_PATH not defined")
  return False


def programWithBSL(serialPort, fileName):
  print("connecting to serial port %s" % serialPort)
  ret = subprocess.call(['python', '-m', 'msp430.bsl5.uart', '-p', serialPort, '--invert-reset', '-e', '-S', '-s', '115200' , '-P', fileName, '-V'])
  if ret == 0:
    return True
  print("failed")
  return False


Reto Da Forno's avatar
initial  
Reto Da Forno committed
102
if __name__ == "__main__":
Reto Da Forno's avatar
Reto Da Forno committed
103
  # check arguments
Reto Da Forno's avatar
initial  
Reto Da Forno committed
104
105
106
107
108
109
110
  if len(sys.argv) < 2:
    print("no filename provided\r\nusage:  ./" + os.path.basename(__file__) + " [filename] [port (optional)]")
    sys.exit()
  fileName = sys.argv[1]
  if not os.path.isfile(fileName):
    print("file '%s' not found" % fileName)
    sys.exit()
Reto Da Forno's avatar
Reto Da Forno committed
111
112

  # determine the serial port
Reto Da Forno's avatar
initial  
Reto Da Forno committed
113
114
115
116
  if len(sys.argv) > 2:
    # 2nd argument is supposed to be the serial port
    serialPort = sys.argv[2]
  else:
Reto Da Forno's avatar
Reto Da Forno committed
117
118
    # prefer the MSP-FET
    serialPort = getMSPFETPort(False)
Reto Da Forno's avatar
Reto Da Forno committed
119
    if serialPort is None:
Reto Da Forno's avatar
Reto Da Forno committed
120
121
122
123
124
125
126
127
128
129
130
131
132
      # no MSP-FET found, try to find a DPP DevBoard
      serialPort = getDPPDevBoardPort(False)
      if serialPort is None:
        print("no connected device found")
        listAvailablePorts()
        sys.exit()  # abort

  # do the target programming
  if "USB" in serialPort:
    if checkSerialPort(serialPort):
      if not programWithBSL(serialPort, fileName):
        listAvailablePorts()
      # reset the target
Reto Da Forno's avatar
initial  
Reto Da Forno committed
133
134
  if resetAfterProgram:
    resetMCU(serialPort)
Reto Da Forno's avatar
Reto Da Forno committed
135
136
137
  else:
    if not programWithMSPFET(serialPort, fileName):
      listAvailablePorts()