Skip to content

Commit 1e9a063

Browse files
committed
Merge
1 parent 7413dff commit 1e9a063

File tree

5 files changed

+66
-72
lines changed

5 files changed

+66
-72
lines changed

README.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
A small tool to write files in TI's CC3200 SimpleLink (TM) filesystem.
44

5-
Copyright (C) 2016 Allterco Robotics
5+
Copyright (C) 2016-2020 Allterco Robotics
6+
7+
**NO LONGER ACTIVELY MAINTAINED HERE**
8+
9+
*Forks with CC3220 support exist, but not sure which one works best. @mon has adopted support for CC3200, go to https://github.com/mon/cc3200tool*
10+
611

712
![](https://img.shields.io/badge/license-GPL_2-green.svg "License")
813

@@ -30,7 +35,7 @@ described in TI's Application Note [CC3100/CC3200 Embedded Programming](http://w
3035

3136
## Installation
3237

33-
This runs on Python 2.7 with recent [pySerial](https://github.com/pyserial/pyserial).
38+
This runs on Python >=3.6 with recent [pySerial](https://github.com/pyserial/pyserial).
3439

3540
To install, if you have pip and want system-wide:
3641

@@ -54,7 +59,7 @@ then it's just like any other python package:
5459
## Usage
5560

5661
You need a serial port connected to the target's UART interface. For
57-
programming to work, SOP2 needs to be asserted (i.e. tied to GND) and a reset
62+
programming to work, SOP2 needs to be asserted (i.e. tied to VCC) and a reset
5863
has to be peformed to switch the chip in bootloader mode. `cc3200tool` can
5964
optionally use the RTS and DTR lines of the serial port controller to
6065
automatically perform these actions via the `--sop2` and `--reset` options.

cc3200tool/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .cc import *

cc3200tool/cc.py

+55-67
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#
22
# cc3200tool - work with TI's CC3200 SimpleLink (TM) filesystem.
3-
# Copyright (C) 2016 Allterco Robotics
3+
# Copyright (C) 2016-2020 Allterco Robotics
44
#
55
# This program is free software; you can redistribute it and/or
66
# modify it under the terms of the GNU General Public License
@@ -41,22 +41,22 @@
4141
# so separate timeout value is used
4242
ERASE_TIMEOUT = 120
4343

44-
OPCODE_START_UPLOAD = "\x21"
45-
OPCODE_FINISH_UPLOAD = "\x22"
46-
OPCODE_GET_LAST_STATUS = "\x23"
47-
OPCODE_FILE_CHUNK = "\x24"
48-
OPCODE_GET_STORAGE_LIST = "\x27"
49-
OPCODE_FORMAT_FLASH = "\x28"
50-
OPCODE_GET_FILE_INFO = "\x2A"
51-
OPCODE_READ_FILE_CHUNK = "\x2B"
52-
OPCODE_RAW_STORAGE_READ = "\x2C"
53-
OPCODE_RAW_STORAGE_WRITE = "\x2D"
54-
OPCODE_ERASE_FILE = "\x2E"
55-
OPCODE_GET_VERSION_INFO = "\x2F"
56-
OPCODE_RAW_STORAGE_ERASE = "\x30"
57-
OPCODE_GET_STORAGE_INFO = "\x31"
58-
OPCODE_EXEC_FROM_RAM = "\x32"
59-
OPCODE_SWITCH_2_APPS = "\x33"
44+
OPCODE_START_UPLOAD = b'\x21'
45+
OPCODE_FINISH_UPLOAD = b'\x22'
46+
OPCODE_GET_LAST_STATUS = b'\x23'
47+
OPCODE_FILE_CHUNK = b'\x24'
48+
OPCODE_GET_STORAGE_LIST = b'\x27'
49+
OPCODE_FORMAT_FLASH = b'\x28'
50+
OPCODE_GET_FILE_INFO = b'\x2A'
51+
OPCODE_READ_FILE_CHUNK = b'\x2B'
52+
OPCODE_RAW_STORAGE_READ = b'\x2C'
53+
OPCODE_RAW_STORAGE_WRITE = b'\x2D'
54+
OPCODE_ERASE_FILE = b'\x2E'
55+
OPCODE_GET_VERSION_INFO = b'\x2F'
56+
OPCODE_RAW_STORAGE_ERASE = b'\x30'
57+
OPCODE_GET_STORAGE_INFO = b'\x31'
58+
OPCODE_EXEC_FROM_RAM = b'\x32'
59+
OPCODE_SWITCH_2_APPS = b'\x33'
6060

6161
STORAGE_ID_SRAM = 0x0
6262
STORAGE_ID_SFLASH = 0x2
@@ -90,7 +90,7 @@
9090

9191

9292
def hexify(s):
93-
return " ".join([hex(ord(x)) for x in s])
93+
return " ".join([hex(x) for x in s])
9494

9595

9696
Pincfg = namedtuple('Pincfg', ['invert', 'pin'])
@@ -107,8 +107,7 @@ def _parse(apin):
107107
invert = True
108108
apin = apin[1:]
109109
if apin not in choices:
110-
raise argparse.ArgumentTypeError("{} not one of {}".format(
111-
apin, choices))
110+
raise argparse.ArgumentTypeError(f"{apin} not one of {choices}")
112111
return Pincfg(invert, apin)
113112

114113
return _parse
@@ -199,7 +198,7 @@ def __call__(self, string):
199198
parser_format_flash = subparsers.add_parser(
200199
"format_flash", help="Format the flash memory")
201200
parser_format_flash.add_argument(
202-
"-s", "--size", choices=SLFS_SIZE_MAP.keys(), default="1M")
201+
"-s", "--size", choices=list(SLFS_SIZE_MAP.keys()), default="1M")
203202

204203
parser_erase_file = subparsers.add_parser(
205204
"erase_file", help="Erase a file from the SL filesystem")
@@ -302,11 +301,11 @@ def is_cc3200(self):
302301

303302
@classmethod
304303
def from_packet(cls, data):
305-
bootloader = tuple(map(ord, data[0:4]))
306-
nwp = tuple(map(ord, data[4:8]))
307-
mac = tuple(map(ord, data[8:12]))
308-
phy = tuple(map(ord, data[12:16]))
309-
chip_type = tuple(map(ord, data[16:20]))
304+
bootloader = tuple(data[0:4])
305+
nwp = tuple(data[4:8])
306+
mac = tuple(data[8:12])
307+
phy = tuple(data[12:16])
308+
chip_type = tuple(data[16:20])
310309
return cls(bootloader, nwp, mac, phy, chip_type)
311310

312311
def __repr__(self):
@@ -363,7 +362,7 @@ def is_ok(self):
363362

364363
@classmethod
365364
def from_packet(cls, packet):
366-
return cls(ord(packet[3]))
365+
return cls(packet[3])
367366

368367

369368
class CC3x00FileInfo(object):
@@ -373,7 +372,7 @@ def __init__(self, exists, size=0):
373372

374373
@classmethod
375374
def from_packet(cls, data):
376-
exists = data[0] == '\x01'
375+
exists = data[0] == 0x01
377376
size = struct.unpack(">I", data[4:8])[0]
378377
return cls(exists, size)
379378

@@ -456,7 +455,7 @@ def __init__(self, fat_header, storage_info):
456455
# scan the complete FAT table (as it appears to be)
457456
meta = fat_header.fat_bytes[(i + 1) * 4:(i + 2) * 4]
458457

459-
if meta == "\xff\xff\xff\xff" or meta == struct.pack("BBBB", 0xff, i, 0xff, 0x7f):
458+
if meta == b"\xff\xff\xff\xff" or meta == struct.pack("BBBB", 0xff, i, 0xff, 0x7f):
460459
# empty entry in the middle of the FAT table
461460
continue
462461

@@ -547,7 +546,7 @@ def print_sffs_info_short(self):
547546
self.block_count - self.used_blocks)
548547

549548
def print_sffs_info_json(self):
550-
print json.dumps(self, cls=CustomJsonEncoder)
549+
print(json.dumps(self, cls=CustomJsonEncoder))
551550

552551

553552
class CustomJsonEncoder(json.JSONEncoder):
@@ -602,7 +601,7 @@ def _do_reset(self, sop2):
602601
print("Reset the device with SOP2 {}asserted and press Enter".format(
603602
'' if sop2 else 'de'
604603
))
605-
raw_input()
604+
input()
606605
return
607606

608607
in_reset = True ^ self._reset.invert
@@ -627,7 +626,7 @@ def _read_ack(self, timeout=None):
627626
ack_bytes.append(b)
628627
if len(ack_bytes) > 2:
629628
ack_bytes.pop(0)
630-
if ack_bytes == ['\x00', '\xCC']:
629+
if ack_bytes == [b'\x00', b'\xCC']:
631630
return True
632631

633632
def _read_packet(self, timeout=None):
@@ -645,36 +644,32 @@ def _read_packet(self, timeout=None):
645644
if (len(data) != data_len):
646645
raise CC3200Error("did not get entire response")
647646

648-
ccsum = 0
649-
for x in data:
650-
ccsum += ord(x)
647+
ccsum = sum(data)
651648
ccsum = ccsum & 0xff
652-
if ccsum != ord(csum_byte):
649+
if ccsum != csum_byte:
653650
raise CC3200Error("rx csum failed")
654651

655652
self._send_ack()
656653
return data
657654

658655
def _send_packet(self, data, timeout=None):
659656
assert len(data)
660-
checksum = 0
661-
for b in data:
662-
checksum += ord(b)
657+
checksum = sum(data)
663658
len_blob = struct.pack(">H", len(data) + 2)
664659
csum = struct.pack("B", checksum & 0xff)
665660
self.port.write(len_blob + csum + data)
666661
if not self._read_ack(timeout):
667662
raise CC3200Error(
668-
"No ack for packet opcode=0x{:02x}".format(ord(data[0])))
663+
f"No ack for packet opcode=0x{data[0]:02x}")
669664

670665
def _send_ack(self):
671-
self.port.write('\x00\xCC')
666+
self.port.write(b'\x00\xCC')
672667

673668
def _get_last_status(self):
674669
self._send_packet(OPCODE_GET_LAST_STATUS)
675670
status = self._read_packet()
676671
log.debug("get last status got %s", hexify(status))
677-
return CC3x00Status(ord(status))
672+
return CC3x00Status(status[0])
678673

679674
def _do_break(self, timeout):
680675
self.port.send_break(.2)
@@ -691,8 +686,7 @@ def _get_version(self):
691686
self._send_packet(OPCODE_GET_VERSION_INFO)
692687
version_data = self._read_packet()
693688
if len(version_data) != 28:
694-
raise CC3200Error("Version info should be 28 bytes, got {}"
695-
.format(len(version_data)))
689+
raise CC3200Error(f"Version info should be 28 bytes, got {len(version_data)}")
696690
return CC3x00VersionInfo.from_packet(version_data)
697691

698692
def _get_storage_list(self):
@@ -702,18 +696,17 @@ def _get_storage_list(self):
702696
slist_byte = self.port.read(1)
703697
if len(slist_byte) != 1:
704698
raise CC3200Error("Did not receive storage list byte")
705-
return CC3x00StorageList(ord(slist_byte))
699+
return CC3x00StorageList(slist_byte[0])
706700

707701
def _get_storage_info(self, storage_id=STORAGE_ID_SRAM):
708702
log.info("Getting storage info...")
709703
self._send_packet(OPCODE_GET_STORAGE_INFO +
710704
struct.pack(">I", storage_id))
711705
sinfo = self._read_packet()
712706
if len(sinfo) < 4:
713-
raise CC3200Error("getting storage info got {} bytes"
714-
.format(len(sinfo)))
707+
raise CC3200Error(f"getting storage info got {len(sinfo)} bytes")
715708
log.info("storage #%d info bytes: %s", storage_id, ", "
716-
.join([hex(ord(x)) for x in sinfo]))
709+
.join([hex(x) for x in sinfo]))
717710
return CC3x00StorageInfo.from_packet(sinfo)
718711

719712
def _erase_blocks(self, start, count, storage_id=STORAGE_ID_SRAM):
@@ -733,13 +726,6 @@ def _raw_write(self, offset, data, storage_id=STORAGE_ID_SRAM):
733726
if storage_id == STORAGE_ID_SRAM and not slist.sram:
734727
raise CC3200Error("no sram?!")
735728

736-
sinfo = self._get_storage_info(storage_id)
737-
bs = sinfo.block_size
738-
if bs > 0:
739-
count = len(data) / bs
740-
if count % bs:
741-
count += 1
742-
743729
chunk_size = 4080
744730
sent = 0
745731
while sent < len(data):
@@ -789,12 +775,13 @@ def _raw_read(self, offset, size, storage_id=STORAGE_ID_SRAM, sinfo=None):
789775

790776
# XXX 4096 works faster, but 256 was sniffed from the uniflash
791777
chunk_size = 4096
792-
rx_data = ''
778+
rx_data = b''
793779
while size - len(rx_data) > 0:
794780
rx_data += self._read_chunk(offset + len(rx_data),
795781
min(chunk_size, size - len(rx_data)),
796782
storage_id)
797783
sys.stderr.write('.')
784+
sys.stderr.flush()
798785
sys.stderr.write("\n")
799786
return rx_data
800787

@@ -804,7 +791,7 @@ def _exec_from_ram(self):
804791
def _get_file_info(self, filename):
805792
command = OPCODE_GET_FILE_INFO \
806793
+ struct.pack(">I", len(filename)) \
807-
+ filename
794+
+ filename.encode()
808795
self._send_packet(command)
809796
finfo = self._read_packet()
810797
if len(finfo) < 5:
@@ -834,7 +821,7 @@ def _open_file_for_read(self, filename):
834821

835822
def _open_file(self, filename, slfs_flags):
836823
command = OPCODE_START_UPLOAD + struct.pack(">II", slfs_flags, 0) + \
837-
filename + '\x00\x00'
824+
filename.encode() + b'\x00\x00'
838825
self._send_packet(command)
839826

840827
token = self.port.read(4)
@@ -843,13 +830,13 @@ def _open_file(self, filename, slfs_flags):
843830

844831
def _close_file(self, signature=None):
845832
if signature is None:
846-
signature = '\x46' * 256
833+
signature = b'\x46' * 256
847834
if len(signature) != 256:
848835
raise CC3200Error("bad signature length")
849836
command = OPCODE_FINISH_UPLOAD
850-
command += '\x00' * 63
837+
command += b'\x00' * 63
851838
command += signature
852-
command += '\x00'
839+
command += b'\x00'
853840
self._send_packet(command)
854841
s = self._get_last_status()
855842
if not s.is_ok:
@@ -917,7 +904,7 @@ def format_slfs(self, size=None):
917904

918905
log.info("Formatting flash with size=%s", size)
919906
command = OPCODE_FORMAT_FLASH \
920-
+ struct.pack(">IIIII", 2, size/4, 0, 0, 2)
907+
+ struct.pack(">IIIII", 2, size//4, 0, 0, 2)
921908

922909
self._send_packet(command)
923910

@@ -934,11 +921,11 @@ def erase_file(self, filename, force=False):
934921

935922
log.info("Erasing file %s...", filename)
936923
command = OPCODE_ERASE_FILE + struct.pack(">I", 0) + \
937-
filename + '\x00'
924+
filename.encode() + b'\x00'
938925
self._send_packet(command)
939926
s = self._get_last_status()
940927
if not s.is_ok:
941-
raise CC3200Error("Erasing file failed: 0x{:02x}}".format(s.value))
928+
raise CC3200Error(f"Erasing file failed: 0x{s.value:02x}")
942929

943930
def write_file(self, local_file, cc_filename, sign_file=None, size=0, commit_flag=False):
944931
# size must be known in advance, so read the whole thing
@@ -990,9 +977,10 @@ def write_file(self, local_file, cc_filename, sign_file=None, size=0, commit_fla
990977
self._send_packet(command)
991978
res = self._get_last_status()
992979
if not res.is_ok:
993-
raise CC3200Error("writing at pos {} failed".format(pos))
980+
raise CC3200Error(f"writing at pos {pos} failed")
994981
pos += len(chunk)
995982
sys.stderr.write('.')
983+
sys.stderr.flush()
996984

997985
sys.stderr.write("\n")
998986
log.debug("Closing file ...")
@@ -1001,7 +989,7 @@ def write_file(self, local_file, cc_filename, sign_file=None, size=0, commit_fla
1001989
def read_file(self, cc_fname, local_file):
1002990
finfo = self._get_file_info(cc_fname)
1003991
if not finfo.exists:
1004-
raise CC3200Error("{} does not exist on target".format(cc_fname))
992+
raise CC3200Error(f"{cc_fname} does not exist on target")
1005993

1006994
log.info("Reading file %s -> %s", cc_fname, local_file.name)
1007995

@@ -1177,7 +1165,7 @@ def main():
11771165
cc.connect()
11781166
log.info("connected to target")
11791167
except (Exception, ) as e:
1180-
log.error("Could not connect to target: {}".format(e))
1168+
log.error(f"Could not connect to target: {e}")
11811169
sys.exit(-3)
11821170

11831171
log.info("Version: %s", cc.vinfo)

scripts/cc3200tool

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!python
22
#
33
# cc3200tool - work with TI's CC3200 SimpleLink (TM) filesystem.
4-
# Copyright (C) 2016 Allterco Robotics
4+
# Copyright (C) 2016-2020 Allterco Robotics
55
#
66
# This program is free software; you can redistribute it and/or
77
# modify it under the terms of the GNU General Public License

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="cc3200tool",
5-
version="0.1.0",
5+
version="1.0.0",
66
description="A tool to upload files to TI CC3200",
77
author="Kiril Zyapkov",
88
author_email="[email protected]",

0 commit comments

Comments
 (0)