-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsbutil.py
149 lines (131 loc) · 6.08 KB
/
sbutil.py
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
146
147
148
149
#!/usr/bin/env python3
"""
This is a command line tool for programming Laird "SmartBASIC" devices.
- Compile and download smartBASIC applications
- Download firmware images over the uart
Original works by:
blutil.py
Copyright (C)2014 Angus Gratton, released under BSD license as per the LICENSE file.
Subsequently enhanced by:
Dimitrios Siganos
Oli Solomons
Mahendra Tailor
"""
##########################################################################################
# Copyright (C)2014 Angus Gratton, released under BSD license as per the LICENSE file.
##########################################################################################
#-----------------------------------------------------------------------------
# constants
#-----------------------------------------------------------------------------
VERBOSELEVEL=0
DEFAULT_MODULE='BL654'
#-----------------------------------------------------------------------------
# Module imports
#-----------------------------------------------------------------------------
import blutilc
import uwfloader
import os
import sys
import serial
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
def setup_arg_parser():
parser = blutilc.argparse.ArgumentParser(
description=
"""Perform smartBASIC Application or Firmware operations with a Laird module.
Module type can be: BL654 | BL654IG | BL652 | BL653 | RM1XX | BT900 | GENERIC
""")
parser.add_argument('-p', '--port', help="Serial port to connect to",required=True)
parser.add_argument('-b', '--baud', type=int, default=blutilc.SERIAL_DEF_BAUD, help=f"Baud rate, default={blutilc.SERIAL_DEF_BAUD}")
parser.add_argument('-v','--verbose', action="store_true", help="verbose mode", default=False)
parser.add_argument('-n','--no-break', action="store_true", help="Do not reset with DTR deasserted")
parser.add_argument('-t', '--timeout',
help="Timeout for commands like --send", default=blutilc.SERIAL_TIMEOUT,type=float,
metavar="TIMEOUT")
parser.add_argument('-m', '--module', default=DEFAULT_MODULE, help=f"Module type, default={DEFAULT_MODULE}")
cmd_arg = parser.add_mutually_exclusive_group(required=True)
cmd_arg.add_argument('-f', '--firmware', help="Download a .uwf firmware file to device", metavar="UWF_FILE")
cmd_arg.add_argument('-c', '--compile', help="Compile specified smartBasic file to a .uwc file.", metavar="SBFILE")
cmd_arg.add_argument('-l', '--load',
help="Upload specified smartBasic file to device (if argument is a .sb file it will be compiled first.)",
metavar="FILE")
cmd_arg.add_argument('-r', '--run',
help="Execute specified smartBasic file on device (if argument is a .sb file it will be compiled and uploaded first, if argument is a .uwc file it will be uploaded first.)",
metavar="FILE")
cmd_arg.add_argument('-s', '--send',
help="Send the string CMD (\\r will be auto appended) and listen for {SERIAL_TIMEOUT} seconds",
metavar="CMD")
cmd_arg.add_argument('--ls', action="store_true", help="List all files uploaded to the device")
cmd_arg.add_argument('--rm', metavar="FILE", help="Remove specified file from the device")
cmd_arg.add_argument('--format', action="store_true", help="Erase all stored files from the device")
cmd_arg.add_argument('--listen', action="store_true",
help="Listen over serial for incoming messages, e.g. from print statements in a running program")
return parser
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
def main():
parser=setup_arg_parser()
global args
args = parser.parse_args()
if args.firmware is None:
#make the args visible to blutilc
blutilc.args=args
#create an instance of a smartBASIC device as per the class in blutilc.py
device = blutilc.BLDevice(args)
# Preload any .sb or .uwc file
if args.run is not None:
split = os.path.splitext(args.run)
if split[1] == ".uwc" or split[1] == ".sb":
args.load = args.run
# Precompile any .sb file
if args.load is not None:
split = os.path.splitext(args.load)
if split[1] == ".sb":
args.compile = args.load
ops = []
if args.compile:
ops += ["compile"]
if args.load:
ops += ["load"]
if args.run:
ops += ["run"]
#if break into command mode via reset/urt_break then do so
if not args.send:
if not args.no_break:
device.reset_into_cmd_mode()
if args.compile:
device.detect_model()
if len(ops) > 0:
print("Performing %s for %s..." % (", ".join(ops), sys.argv[-1]))
if args.ls:
device.list()
if args.rm:
device.delete(args.rm)
if args.format:
device.format()
if args.compile:
device.compile(args.compile)
if args.load:
device.upload(args.load)
if args.run:
device.run(args.run)
if args.send:
cmdstr=f"{args.send}\r"
print(device.writerawcmd(cmdstr, timeout=args.timeout))
print("Command completed")
if args.listen:
device.listen()
else:
#download firmware
uwfloader.loadfirmware(args.port,args.baud,args.firmware,args.module)
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
if __name__ == "__main__":
try:
main()
except RuntimeError as e:
print(e)
sys.exit(2)
except serial.SerialException as e:
print(e)
sys.exit(3)