17
17
_log = logging .getLogger ("isp_programmer" )
18
18
19
19
kTimeout = 1
20
-
21
-
22
20
BAUDRATES = (9600 , 19200 , 38400 , 57600 , 115200 , 230400 , 460800 )
23
-
24
-
25
21
NXPReturnCodes = {
26
22
"CMD_SUCCESS" : 0x0 ,
27
23
"INVALID_COMMAND" : 0x1 ,
@@ -83,7 +79,8 @@ def _raise_return_code_error(code: int, call_name: str) -> None:
83
79
84
80
@dataclass
85
81
class Settings :
86
- safe_write : bool = True # Check to see if sector is already equal to RAM, if so skip
82
+ # Check to see if sector is already equal to RAM, if so skip
83
+ safe_write : bool = True
87
84
flash_write_sleep : float = 0.01
88
85
ram_write_sleep : float = 0.01
89
86
return_code_sleep : float = 0.05
@@ -101,7 +98,7 @@ class ISPConnection:
101
98
102
99
kNewLine = "\r \n "
103
100
StatusRespLength = len (kNewLine ) + 1
104
- kWordSize = 4 # 32 bit device
101
+ kWordSize = 4
105
102
# Parity = None
106
103
# DataBits = 8
107
104
# StopBits = 1
@@ -212,7 +209,8 @@ def _get_return_code(self, command_string: str) -> int:
212
209
if resp .strip () == command_string .strip ():
213
210
_log .debug ("Command was echoed, Discarding line: %s" , resp )
214
211
resp = self ._read_line ()
215
- # if self.echo_on: # discard echo
212
+ # discard echo
213
+ # if self.echo_on:
216
214
# _log.debug("ECHO ON, Discarding line: %s", resp)
217
215
# resp = self._read_line()
218
216
except TimeoutError :
@@ -262,7 +260,7 @@ def SetEcho(self, on: bool = True):
262
260
"""
263
261
ISP echos host when enabled
264
262
"""
265
- command = f"A { on : d} "
263
+ command = f"A { on : d} "
266
264
response_code = self ._write_command (command )
267
265
_raise_return_code_error (response_code , "Set Echo" )
268
266
self .echo_on = on
@@ -281,14 +279,18 @@ def WriteToRam(self, start: int, data: bytes):
281
279
# when transfer is complete the handler sends OK<CR><LF>
282
280
response_code = self ._write_command (f"W { start } { len (data )} " )
283
281
_raise_return_code_error (response_code , function_name )
284
- self ._write (data ) # Stream data after confirmation
282
+
283
+ # Stream data after confirmation
284
+ self ._write (data )
285
285
# Ignore response, it's not reliable
286
286
287
287
def ReadMemory (self , start : int , num_bytes : int ):
288
288
"""
289
289
Send command with newline, receive response code\r \n <data>
290
290
"""
291
- assert num_bytes % self .kWordSize == 0 # On a word boundary
291
+
292
+ # On a word boundary
293
+ assert num_bytes % self .kWordSize == 0
292
294
function = "ReadMemory"
293
295
command = f"R { start } { num_bytes } "
294
296
@@ -334,9 +336,9 @@ def Go(self, address: int, thumb_mode: bool = False):
334
336
if thumb_mode :
335
337
mode = "T"
336
338
response_code = self ._write_command (f"G { address } { mode } " )
337
- if (
338
- response_code != self . ReturnCodes [ "NoStatusResponse" ]
339
- ): # Don't expect a response code from this
339
+
340
+ # Don't expect a response code from this
341
+ if response_code != self . ReturnCodes [ "NoStatusResponse" ]:
340
342
_raise_return_code_error (response_code , "Go" )
341
343
342
344
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
@@ -520,7 +523,7 @@ def SyncConnection(self):
520
523
# self._flush()
521
524
_log .debug (f"Echoing sync string, { repr (self .SyncStringBytes )} " )
522
525
time .sleep (0.1 )
523
- self ._write (self .SyncStringBytes ) # echo SyncString
526
+ self ._write (self .SyncStringBytes )
524
527
self .write_newline ()
525
528
self .write_newline ()
526
529
# > Synchronized\n
@@ -569,7 +572,7 @@ class ChipDescription:
569
572
Wraps a chip description line and exposes it as a class
570
573
"""
571
574
572
- kWordSize = 4 # 32 bit
575
+ kWordSize = 4
573
576
kPageSizeBytes = 64
574
577
SectorSizePages = 16
575
578
CRCLocation = 0x000002FC
@@ -589,14 +592,13 @@ def __init__(self, descriptor: dict[str, typing.Any]):
589
592
self .RAMBufferSize = int (descriptor .pop ("RAMBufferSize" ))
590
593
self .SectorCount : int = int (descriptor .pop ("SectorCount" ))
591
594
self .RAMStartWrite : int = int (descriptor .pop ("RAMStartWrite" ))
592
- self .CrystalFrequency = 12000 # khz == 30MHz
593
- self .kCheckSumLocation = 7 # 0x0000001c
595
+ self .CrystalFrequency = 12000
596
+ # 0x0000001c
597
+ self .kCheckSumLocation = 7
594
598
595
599
assert self .RAMRange [0 ] > 0
596
600
assert self .RAMRange [1 ] > self .RAMRange [0 ]
597
-
598
601
assert self .FlashRange [1 ] > self .FlashRange [0 ]
599
-
600
602
assert self .SectorCount > 0
601
603
602
604
@property
@@ -638,9 +640,7 @@ def RamRangeLegal(self, address, length):
638
640
639
641
# Script tools
640
642
641
- assert (
642
- tools .calc_crc (bytes ([0xFF ] * 1024 )) == 3090874356
643
- ) # Check the software crc algorithm
643
+ assert tools .calc_crc (bytes ([0xFF ] * 1024 )) == 3090874356
644
644
645
645
646
646
def RemoveBootableCheckSum (vector_table_loc : int , image : bytes ) -> bytes :
@@ -667,7 +667,8 @@ def GetCheckSumedVectorTable(vector_table_loc: int, orig_image: bytes) -> bytes:
667
667
668
668
# calculate the checksum over the interrupt vectors
669
669
intvecs_list = list (intvecs [:vector_table_size ])
670
- intvecs_list [vector_table_loc ] = 0 # clear csum value
670
+ # clear csum value
671
+ intvecs_list [vector_table_loc ] = 0
671
672
csum = tools .CalculateCheckSum (intvecs_list )
672
673
intvecs_list [vector_table_loc ] = csum
673
674
vector_table_bytes = b""
@@ -678,7 +679,6 @@ def GetCheckSumedVectorTable(vector_table_loc: int, orig_image: bytes) -> bytes:
678
679
679
680
def MakeBootable (vector_table_loc : int , orig_image : bytes ) -> bytes :
680
681
vector_table_bytes = GetCheckSumedVectorTable (vector_table_loc , orig_image )
681
-
682
682
image = vector_table_bytes + orig_image [len (vector_table_bytes ) :]
683
683
return image
684
684
@@ -791,7 +791,8 @@ def WriteFlashSector(
791
791
def WriteSector (isp : ISPConnection , chip : ChipDescription , sector : int , data : bytes ):
792
792
assert len (data ) > 0
793
793
794
- if len (data ) != chip .sector_bytes : # Fill data buffer to match write size
794
+ # Fill data buffer to match write size
795
+ if len (data ) != chip .sector_bytes :
795
796
data += bytes ([0xFF ] * (chip .sector_bytes - len (data )))
796
797
WriteFlashSector (isp , chip , sector , data )
797
798
@@ -820,10 +821,9 @@ def WriteBinaryToFlash(
820
821
isp .Unlock ()
821
822
for sector in reversed (range (start_sector , start_sector + sector_count )):
822
823
_log .info (f"\n Writing Sector { sector } / { sector_count } " )
823
- data_chunk = image [
824
- (sector - start_sector ) * chip .sector_bytes : (sector - start_sector + 1 )
825
- * chip .sector_bytes
826
- ]
824
+ start = (sector - start_sector ) * chip .sector_bytes
825
+ end = (sector - start_sector + 1 ) * chip .sector_bytes
826
+ data_chunk = image [start :end ]
827
827
WriteSector (isp , chip , sector , data_chunk )
828
828
time .sleep (isp .settings .flash_write_sleep )
829
829
0 commit comments