program_msp432.py 2.29 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
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
#!/usr/bin/env python3
'''

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

This routing only works for the DPP2 DevBoard.
The solder jumpers J500/J501 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


def getFirstPort(printPorts):
  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)[0][0]


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
      time.sleep(0.1)
      ser.close()
      print("target reset")
  except:
    print("failed to connect to serial port " + serialPort)


if __name__ == "__main__":
  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()
  if len(sys.argv) > 2:
    # 2nd argument is supposed to be the serial port
    serialPort = sys.argv[2]
  else:
      serialPort = getFirstPort(False)
      if serialPort is None:
        print("no DPP2 DevBoard found")
        sys.exit()
  if not checkSerialPort(serialPort):
    sys.exit()
  print("connecting to serial port %s" % serialPort)
  res = subprocess.call(['python', '-m', 'msp430.bsl32.uart', '-p', serialPort, '--invert-reset', '-e', '-V', '-S', '-s', '115200' , '-P', fileName])
  if res is not 0:
    print("BSL locked?")
  if resetAfterProgram:
    resetMCU(serialPort)