1
1
#
2
2
# cc3200tool - work with TI's CC3200 SimpleLink (TM) filesystem.
3
- # Copyright (C) 2016 Allterco Robotics
3
+ # Copyright (C) 2016-2020 Allterco Robotics
4
4
#
5
5
# This program is free software; you can redistribute it and/or
6
6
# modify it under the terms of the GNU General Public License
41
41
# so separate timeout value is used
42
42
ERASE_TIMEOUT = 120
43
43
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 '
60
60
61
61
STORAGE_ID_SRAM = 0x0
62
62
STORAGE_ID_SFLASH = 0x2
90
90
91
91
92
92
def hexify (s ):
93
- return " " .join ([hex (ord ( x ) ) for x in s ])
93
+ return " " .join ([hex (x ) for x in s ])
94
94
95
95
96
96
Pincfg = namedtuple ('Pincfg' , ['invert' , 'pin' ])
@@ -107,8 +107,7 @@ def _parse(apin):
107
107
invert = True
108
108
apin = apin [1 :]
109
109
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 } " )
112
111
return Pincfg (invert , apin )
113
112
114
113
return _parse
@@ -199,7 +198,7 @@ def __call__(self, string):
199
198
parser_format_flash = subparsers .add_parser (
200
199
"format_flash" , help = "Format the flash memory" )
201
200
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" )
203
202
204
203
parser_erase_file = subparsers .add_parser (
205
204
"erase_file" , help = "Erase a file from the SL filesystem" )
@@ -302,11 +301,11 @@ def is_cc3200(self):
302
301
303
302
@classmethod
304
303
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 ])
310
309
return cls (bootloader , nwp , mac , phy , chip_type )
311
310
312
311
def __repr__ (self ):
@@ -363,7 +362,7 @@ def is_ok(self):
363
362
364
363
@classmethod
365
364
def from_packet (cls , packet ):
366
- return cls (ord ( packet [3 ]) )
365
+ return cls (packet [3 ])
367
366
368
367
369
368
class CC3x00FileInfo (object ):
@@ -373,7 +372,7 @@ def __init__(self, exists, size=0):
373
372
374
373
@classmethod
375
374
def from_packet (cls , data ):
376
- exists = data [0 ] == ' \x01 '
375
+ exists = data [0 ] == 0x01
377
376
size = struct .unpack (">I" , data [4 :8 ])[0 ]
378
377
return cls (exists , size )
379
378
@@ -456,7 +455,7 @@ def __init__(self, fat_header, storage_info):
456
455
# scan the complete FAT table (as it appears to be)
457
456
meta = fat_header .fat_bytes [(i + 1 ) * 4 :(i + 2 ) * 4 ]
458
457
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 ):
460
459
# empty entry in the middle of the FAT table
461
460
continue
462
461
@@ -547,7 +546,7 @@ def print_sffs_info_short(self):
547
546
self .block_count - self .used_blocks )
548
547
549
548
def print_sffs_info_json (self ):
550
- print json .dumps (self , cls = CustomJsonEncoder )
549
+ print ( json .dumps (self , cls = CustomJsonEncoder ) )
551
550
552
551
553
552
class CustomJsonEncoder (json .JSONEncoder ):
@@ -602,7 +601,7 @@ def _do_reset(self, sop2):
602
601
print ("Reset the device with SOP2 {}asserted and press Enter" .format (
603
602
'' if sop2 else 'de'
604
603
))
605
- raw_input ()
604
+ input ()
606
605
return
607
606
608
607
in_reset = True ^ self ._reset .invert
@@ -627,7 +626,7 @@ def _read_ack(self, timeout=None):
627
626
ack_bytes .append (b )
628
627
if len (ack_bytes ) > 2 :
629
628
ack_bytes .pop (0 )
630
- if ack_bytes == ['\x00 ' , '\xCC ' ]:
629
+ if ack_bytes == [b '\x00 ' , b '\xCC ' ]:
631
630
return True
632
631
633
632
def _read_packet (self , timeout = None ):
@@ -645,36 +644,32 @@ def _read_packet(self, timeout=None):
645
644
if (len (data ) != data_len ):
646
645
raise CC3200Error ("did not get entire response" )
647
646
648
- ccsum = 0
649
- for x in data :
650
- ccsum += ord (x )
647
+ ccsum = sum (data )
651
648
ccsum = ccsum & 0xff
652
- if ccsum != ord ( csum_byte ) :
649
+ if ccsum != csum_byte :
653
650
raise CC3200Error ("rx csum failed" )
654
651
655
652
self ._send_ack ()
656
653
return data
657
654
658
655
def _send_packet (self , data , timeout = None ):
659
656
assert len (data )
660
- checksum = 0
661
- for b in data :
662
- checksum += ord (b )
657
+ checksum = sum (data )
663
658
len_blob = struct .pack (">H" , len (data ) + 2 )
664
659
csum = struct .pack ("B" , checksum & 0xff )
665
660
self .port .write (len_blob + csum + data )
666
661
if not self ._read_ack (timeout ):
667
662
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 } " )
669
664
670
665
def _send_ack (self ):
671
- self .port .write ('\x00 \xCC ' )
666
+ self .port .write (b '\x00 \xCC ' )
672
667
673
668
def _get_last_status (self ):
674
669
self ._send_packet (OPCODE_GET_LAST_STATUS )
675
670
status = self ._read_packet ()
676
671
log .debug ("get last status got %s" , hexify (status ))
677
- return CC3x00Status (ord ( status ) )
672
+ return CC3x00Status (status [ 0 ] )
678
673
679
674
def _do_break (self , timeout ):
680
675
self .port .send_break (.2 )
@@ -691,8 +686,7 @@ def _get_version(self):
691
686
self ._send_packet (OPCODE_GET_VERSION_INFO )
692
687
version_data = self ._read_packet ()
693
688
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 )} " )
696
690
return CC3x00VersionInfo .from_packet (version_data )
697
691
698
692
def _get_storage_list (self ):
@@ -702,18 +696,17 @@ def _get_storage_list(self):
702
696
slist_byte = self .port .read (1 )
703
697
if len (slist_byte ) != 1 :
704
698
raise CC3200Error ("Did not receive storage list byte" )
705
- return CC3x00StorageList (ord ( slist_byte ) )
699
+ return CC3x00StorageList (slist_byte [ 0 ] )
706
700
707
701
def _get_storage_info (self , storage_id = STORAGE_ID_SRAM ):
708
702
log .info ("Getting storage info..." )
709
703
self ._send_packet (OPCODE_GET_STORAGE_INFO +
710
704
struct .pack (">I" , storage_id ))
711
705
sinfo = self ._read_packet ()
712
706
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" )
715
708
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 ]))
717
710
return CC3x00StorageInfo .from_packet (sinfo )
718
711
719
712
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):
733
726
if storage_id == STORAGE_ID_SRAM and not slist .sram :
734
727
raise CC3200Error ("no sram?!" )
735
728
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
-
743
729
chunk_size = 4080
744
730
sent = 0
745
731
while sent < len (data ):
@@ -789,12 +775,13 @@ def _raw_read(self, offset, size, storage_id=STORAGE_ID_SRAM, sinfo=None):
789
775
790
776
# XXX 4096 works faster, but 256 was sniffed from the uniflash
791
777
chunk_size = 4096
792
- rx_data = ''
778
+ rx_data = b ''
793
779
while size - len (rx_data ) > 0 :
794
780
rx_data += self ._read_chunk (offset + len (rx_data ),
795
781
min (chunk_size , size - len (rx_data )),
796
782
storage_id )
797
783
sys .stderr .write ('.' )
784
+ sys .stderr .flush ()
798
785
sys .stderr .write ("\n " )
799
786
return rx_data
800
787
@@ -804,7 +791,7 @@ def _exec_from_ram(self):
804
791
def _get_file_info (self , filename ):
805
792
command = OPCODE_GET_FILE_INFO \
806
793
+ struct .pack (">I" , len (filename )) \
807
- + filename
794
+ + filename . encode ()
808
795
self ._send_packet (command )
809
796
finfo = self ._read_packet ()
810
797
if len (finfo ) < 5 :
@@ -834,7 +821,7 @@ def _open_file_for_read(self, filename):
834
821
835
822
def _open_file (self , filename , slfs_flags ):
836
823
command = OPCODE_START_UPLOAD + struct .pack (">II" , slfs_flags , 0 ) + \
837
- filename + '\x00 \x00 '
824
+ filename . encode () + b '\x00 \x00 '
838
825
self ._send_packet (command )
839
826
840
827
token = self .port .read (4 )
@@ -843,13 +830,13 @@ def _open_file(self, filename, slfs_flags):
843
830
844
831
def _close_file (self , signature = None ):
845
832
if signature is None :
846
- signature = '\x46 ' * 256
833
+ signature = b '\x46 ' * 256
847
834
if len (signature ) != 256 :
848
835
raise CC3200Error ("bad signature length" )
849
836
command = OPCODE_FINISH_UPLOAD
850
- command += '\x00 ' * 63
837
+ command += b '\x00 ' * 63
851
838
command += signature
852
- command += '\x00 '
839
+ command += b '\x00 '
853
840
self ._send_packet (command )
854
841
s = self ._get_last_status ()
855
842
if not s .is_ok :
@@ -917,7 +904,7 @@ def format_slfs(self, size=None):
917
904
918
905
log .info ("Formatting flash with size=%s" , size )
919
906
command = OPCODE_FORMAT_FLASH \
920
- + struct .pack (">IIIII" , 2 , size / 4 , 0 , 0 , 2 )
907
+ + struct .pack (">IIIII" , 2 , size // 4 , 0 , 0 , 2 )
921
908
922
909
self ._send_packet (command )
923
910
@@ -934,11 +921,11 @@ def erase_file(self, filename, force=False):
934
921
935
922
log .info ("Erasing file %s..." , filename )
936
923
command = OPCODE_ERASE_FILE + struct .pack (">I" , 0 ) + \
937
- filename + '\x00 '
924
+ filename . encode () + b '\x00 '
938
925
self ._send_packet (command )
939
926
s = self ._get_last_status ()
940
927
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} " )
942
929
943
930
def write_file (self , local_file , cc_filename , sign_file = None , size = 0 , commit_flag = False ):
944
931
# 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
990
977
self ._send_packet (command )
991
978
res = self ._get_last_status ()
992
979
if not res .is_ok :
993
- raise CC3200Error ("writing at pos {} failed" . format ( pos ) )
980
+ raise CC3200Error (f "writing at pos { pos } failed" )
994
981
pos += len (chunk )
995
982
sys .stderr .write ('.' )
983
+ sys .stderr .flush ()
996
984
997
985
sys .stderr .write ("\n " )
998
986
log .debug ("Closing file ..." )
@@ -1001,7 +989,7 @@ def write_file(self, local_file, cc_filename, sign_file=None, size=0, commit_fla
1001
989
def read_file (self , cc_fname , local_file ):
1002
990
finfo = self ._get_file_info (cc_fname )
1003
991
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" )
1005
993
1006
994
log .info ("Reading file %s -> %s" , cc_fname , local_file .name )
1007
995
@@ -1177,7 +1165,7 @@ def main():
1177
1165
cc .connect ()
1178
1166
log .info ("connected to target" )
1179
1167
except (Exception , ) as e :
1180
- log .error ("Could not connect to target: {}" . format ( e ) )
1168
+ log .error (f "Could not connect to target: { e } " )
1181
1169
sys .exit (- 3 )
1182
1170
1183
1171
log .info ("Version: %s" , cc .vinfo )
0 commit comments