|
@@ -19,6 +19,8 @@ __version__ = "0"
|
19
|
19
|
import argparse
|
20
|
20
|
import sys
|
21
|
21
|
|
|
22
|
+import intelhex
|
|
23
|
+
|
22
|
24
|
from alpaca_isp.chips import chips
|
23
|
25
|
from alpaca_isp.exceptions import *
|
24
|
26
|
from alpaca_isp.lpc import *
|
|
@@ -45,7 +47,12 @@ def create_lpc(tty, baudrate=DEFAULT_BAUDRATE, clock=DEFAULT_CLOCK,
|
45
|
47
|
lpc.close()
|
46
|
48
|
if verbose:
|
47
|
49
|
print(" failed")
|
48
|
|
- sys.exit(1)
|
|
50
|
+ raise
|
|
51
|
+ except KeyboardInterrupt:
|
|
52
|
+ lpc.close()
|
|
53
|
+ if verbose:
|
|
54
|
+ print()
|
|
55
|
+ raise
|
49
|
56
|
if verbose:
|
50
|
57
|
print()
|
51
|
58
|
|
|
@@ -81,29 +88,46 @@ def main():
|
81
|
88
|
parser.add_argument("--try-sync", type=int,
|
82
|
89
|
help="maximum number of tries to synchronize with the "
|
83
|
90
|
"microcontroller")
|
84
|
|
- parser.add_argument("--control", action="store_true",
|
|
91
|
+ parser.add_argument("-n", "--control", action="store_true",
|
85
|
92
|
help="control RS232 lines to enter ISP mode (/RST = DTR, /ISP = "
|
86
|
93
|
"RTS)")
|
87
|
|
- parser.add_argument("--verify", action="store_true",
|
|
94
|
+ parser.add_argument("-r", "--verify", action="store_true",
|
88
|
95
|
help="verify that the data were written correctly after flashing")
|
89
|
96
|
|
90
|
97
|
# Parse arguments
|
91
|
98
|
args = parser.parse_args()
|
92
|
99
|
|
93
|
100
|
# Open the LPC
|
94
|
|
- lpc = create_lpc(args.tty, baudrate=args.baudrate, clock=args.clock_khz,
|
95
|
|
- timeout=args.timeout, control=args.control, try_sync=args.try_sync,
|
96
|
|
- verbose=True)
|
|
101
|
+ try:
|
|
102
|
+ lpc = create_lpc(args.tty, baudrate=args.baudrate,
|
|
103
|
+ clock=args.clock_khz, timeout=args.timeout,
|
|
104
|
+ control=args.control, try_sync=args.try_sync, verbose=True)
|
|
105
|
+ except (RecvTimeout, KeyboardInterrupt):
|
|
106
|
+ sys.exit(1)
|
97
|
107
|
|
98
|
108
|
# Unlock the chip
|
99
|
109
|
lpc.unlock()
|
100
|
110
|
|
101
|
111
|
# Erase the flash if we've been asked to
|
102
|
112
|
if args.erase:
|
|
113
|
+ print("Erasing all flash sectors")
|
103
|
114
|
lpc.prepare_write()
|
104
|
115
|
lpc.erase()
|
105
|
116
|
|
106
|
|
- # TODO: write the file to flash
|
|
117
|
+ # Write the files to flash
|
|
118
|
+ ih = intelhex.IntelHex()
|
|
119
|
+ for f in args.file:
|
|
120
|
+ ih.fromfile(f, format="hex")
|
|
121
|
+
|
|
122
|
+ try:
|
|
123
|
+ lpc.flash_hex(ih, verbose=True)
|
|
124
|
+ except ISPError as e:
|
|
125
|
+ if e.args[0] == ReturnCode.CODE_READ_PROTECTION_ENABLED:
|
|
126
|
+ print("Error: code read protection is enabled")
|
|
127
|
+ sys.exit(2)
|
|
128
|
+
|
|
129
|
+ # TODO: Verify that the flash has been written correctly if we've been
|
|
130
|
+ # asked to
|
107
|
131
|
|
108
|
132
|
# Start the program if we haven't been asked not to
|
109
|
133
|
if not args.no_start:
|