16
16
_log = logging .getLogger ("isp_programmer" )
17
17
18
18
kTimeout = 1
19
-
20
-
21
19
BAUDRATES = (9600 , 19200 , 38400 , 57600 , 115200 , 230400 , 460800 )
22
-
23
-
24
20
NXPReturnCodes = {
25
21
"CMD_SUCCESS" : 0x0 ,
26
22
"INVALID_COMMAND" : 0x1 ,
@@ -82,7 +78,8 @@ def _raise_return_code_error(code: int, call_name: str) -> None:
82
78
83
79
@dataclass
84
80
class Settings :
85
- safe_write : bool = True # Check to see if sector is already equal to RAM, if so skip
81
+ # Check to see if sector is already equal to RAM, if so skip
82
+ safe_write : bool = True
86
83
flash_write_sleep : float = 0.01
87
84
ram_write_sleep : float = 0.01
88
85
return_code_sleep : float = 0.05
@@ -100,7 +97,7 @@ class ISPConnection:
100
97
101
98
kNewLine = "\r \n "
102
99
StatusRespLength = len (kNewLine ) + 1
103
- kWordSize = 4 # 32 bit device
100
+ kWordSize = 4
104
101
# Parity = None
105
102
# DataBits = 8
106
103
# StopBits = 1
@@ -211,7 +208,8 @@ def _get_return_code(self, command_string: str) -> int:
211
208
if resp .strip () == command_string .strip ():
212
209
_log .debug ("Command was echoed, Discarding line: %s" , resp )
213
210
resp = self ._read_line ()
214
- # if self.echo_on: # discard echo
211
+ # discard echo
212
+ # if self.echo_on:
215
213
# _log.debug("ECHO ON, Discarding line: %s", resp)
216
214
# resp = self._read_line()
217
215
except TimeoutError :
@@ -265,7 +263,7 @@ def SetEcho(self, on: bool = True):
265
263
"""
266
264
ISP echos host when enabled
267
265
"""
268
- command = f"A { on : d} "
266
+ command = f"A { on : d} "
269
267
response_code = self ._write_command (command )
270
268
_raise_return_code_error (response_code , "Set Echo" )
271
269
self .echo_on = on
@@ -284,14 +282,18 @@ def WriteToRam(self, start: int, data: bytes):
284
282
# when transfer is complete the handler sends OK<CR><LF>
285
283
response_code = self ._write_command (f"W { start } { len (data )} " )
286
284
_raise_return_code_error (response_code , function_name )
287
- self ._write (data ) # Stream data after confirmation
285
+
286
+ # Stream data after confirmation
287
+ self ._write (data )
288
288
# Ignore response, it's not reliable
289
289
290
290
def ReadMemory (self , start : int , num_bytes : int ):
291
291
"""
292
292
Send command with newline, receive response code\r \n <data>
293
293
"""
294
- assert num_bytes % self .kWordSize == 0 # On a word boundary
294
+
295
+ # On a word boundary
296
+ assert num_bytes % self .kWordSize == 0
295
297
function = "ReadMemory"
296
298
command = f"R { start } { num_bytes } "
297
299
@@ -337,9 +339,9 @@ def Go(self, address: int, thumb_mode: bool = False):
337
339
if thumb_mode :
338
340
mode = "T"
339
341
response_code = self ._write_command (f"G { address } { mode } " )
340
- if (
341
- response_code != self . ReturnCodes [ "NoStatusResponse" ]
342
- ): # Don't expect a response code from this
342
+
343
+ # Don't expect a response code from this
344
+ if response_code != self . ReturnCodes [ "NoStatusResponse" ]:
343
345
_raise_return_code_error (response_code , "Go" )
344
346
345
347
def EraseSector (self , start : int , end : int ):
@@ -467,7 +469,8 @@ def SetCrystalFrequency(self, frequency_khz: int):
467
469
verified = False
468
470
for _ in range (3 ):
469
471
try :
470
- frame_in = self ._read_line () # Should be OK\r\n
472
+ frame_in = self ._read_line ()
473
+ # Should be OK\r\n
471
474
if self .SyncVerifiedString in frame_in :
472
475
verified = True
473
476
break
@@ -526,7 +529,7 @@ def SyncConnection(self):
526
529
# self._flush()
527
530
_log .debug (f"Echoing sync string, { repr (self .SyncStringBytes )} " )
528
531
time .sleep (0.1 )
529
- self ._write (self .SyncStringBytes ) # echo SyncString
532
+ self ._write (self .SyncStringBytes )
530
533
self .write_newline ()
531
534
self .write_newline ()
532
535
# > Synchronized\n
@@ -575,7 +578,7 @@ class ChipDescription:
575
578
Wraps a chip description line and exposes it as a class
576
579
"""
577
580
578
- kWordSize = 4 # 32 bit
581
+ kWordSize = 4
579
582
kPageSizeBytes = 64
580
583
SectorSizePages = 16
581
584
CRCLocation = 0x000002FC
@@ -595,14 +598,13 @@ def __init__(self, descriptor: dict[str, typing.Any]):
595
598
self .RAMBufferSize = int (descriptor .pop ("RAMBufferSize" ))
596
599
self .SectorCount : int = int (descriptor .pop ("SectorCount" ))
597
600
self .RAMStartWrite : int = int (descriptor .pop ("RAMStartWrite" ))
598
- self .CrystalFrequency : int = 12000 # khz == 30MHz
599
- self .kCheckSumLocation : int = 7 # 0x0000001c
601
+ self .CrystalFrequency = 12000
602
+ # 0x0000001c
603
+ self .kCheckSumLocation = 7
600
604
601
605
assert self .RAMRange [0 ] > 0
602
606
assert self .RAMRange [1 ] > self .RAMRange [0 ]
603
-
604
607
assert self .FlashRange [1 ] > self .FlashRange [0 ]
605
-
606
608
assert self .SectorCount > 0
607
609
608
610
@property
@@ -644,9 +646,7 @@ def RamRangeLegal(self, address, length):
644
646
645
647
# Script tools
646
648
647
- assert (
648
- tools .calc_crc (bytes ([0xFF ] * 1024 )) == 3090874356
649
- ) # Check the software crc algorithm
649
+ assert tools .calc_crc (bytes ([0xFF ] * 1024 )) == 3090874356
650
650
651
651
652
652
def RemoveBootableCheckSum (vector_table_loc : int , image : bytes ) -> bytes :
@@ -673,7 +673,8 @@ def GetCheckSumedVectorTable(vector_table_loc: int, orig_image: bytes) -> bytes:
673
673
674
674
# calculate the checksum over the interrupt vectors
675
675
intvecs_list = list (intvecs [:vector_table_size ])
676
- intvecs_list [vector_table_loc ] = 0 # clear csum value
676
+ # clear csum value
677
+ intvecs_list [vector_table_loc ] = 0
677
678
csum = tools .CalculateCheckSum (intvecs_list )
678
679
intvecs_list [vector_table_loc ] = csum
679
680
vector_table_bytes = b""
@@ -684,7 +685,6 @@ def GetCheckSumedVectorTable(vector_table_loc: int, orig_image: bytes) -> bytes:
684
685
685
686
def MakeBootable (vector_table_loc : int , orig_image : bytes ) -> bytes :
686
687
vector_table_bytes = GetCheckSumedVectorTable (vector_table_loc , orig_image )
687
-
688
688
image = vector_table_bytes + orig_image [len (vector_table_bytes ) :]
689
689
return image
690
690
@@ -797,7 +797,8 @@ def WriteFlashSector(
797
797
def WriteSector (isp : ISPConnection , chip : ChipDescription , sector : int , data : bytes ):
798
798
assert len (data ) > 0
799
799
800
- if len (data ) != chip .sector_bytes : # Fill data buffer to match write size
800
+ # Fill data buffer to match write size
801
+ if len (data ) != chip .sector_bytes :
801
802
data += bytes ([0xFF ] * (chip .sector_bytes - len (data )))
802
803
WriteFlashSector (isp , chip , sector , data )
803
804
@@ -826,10 +827,9 @@ def WriteBinaryToFlash(
826
827
isp .Unlock ()
827
828
for sector in reversed (range (start_sector , start_sector + sector_count )):
828
829
_log .info (f"\n Writing Sector { sector } / { sector_count } " )
829
- data_chunk = image [
830
- (sector - start_sector ) * chip .sector_bytes : (sector - start_sector + 1 )
831
- * chip .sector_bytes
832
- ]
830
+ start = (sector - start_sector ) * chip .sector_bytes
831
+ end = (sector - start_sector + 1 ) * chip .sector_bytes
832
+ data_chunk = image [start :end ]
833
833
WriteSector (isp , chip , sector , data_chunk )
834
834
time .sleep (isp .settings .flash_write_sleep )
835
835
0 commit comments