1616_log = logging .getLogger ("isp_programmer" )
1717
1818kTimeout = 1
19-
20-
2119BAUDRATES = (9600 , 19200 , 38400 , 57600 , 115200 , 230400 , 460800 )
22-
23-
2420NXPReturnCodes = {
2521 "CMD_SUCCESS" : 0x0 ,
2622 "INVALID_COMMAND" : 0x1 ,
@@ -82,7 +78,8 @@ def _raise_return_code_error(code: int, call_name: str) -> None:
8278
8379@dataclass
8480class 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
8683 flash_write_sleep : float = 0.01
8784 ram_write_sleep : float = 0.01
8885 return_code_sleep : float = 0.05
@@ -100,7 +97,7 @@ class ISPConnection:
10097
10198 kNewLine = "\r \n "
10299 StatusRespLength = len (kNewLine ) + 1
103- kWordSize = 4 # 32 bit device
100+ kWordSize = 4
104101 # Parity = None
105102 # DataBits = 8
106103 # StopBits = 1
@@ -211,7 +208,8 @@ def _get_return_code(self, command_string: str) -> int:
211208 if resp .strip () == command_string .strip ():
212209 _log .debug ("Command was echoed, Discarding line: %s" , resp )
213210 resp = self ._read_line ()
214- # if self.echo_on: # discard echo
211+ # discard echo
212+ # if self.echo_on:
215213 # _log.debug("ECHO ON, Discarding line: %s", resp)
216214 # resp = self._read_line()
217215 except TimeoutError :
@@ -265,7 +263,7 @@ def SetEcho(self, on: bool = True):
265263 """
266264 ISP echos host when enabled
267265 """
268- command = f"A { on : d} "
266+ command = f"A { on : d} "
269267 response_code = self ._write_command (command )
270268 _raise_return_code_error (response_code , "Set Echo" )
271269 self .echo_on = on
@@ -284,14 +282,18 @@ def WriteToRam(self, start: int, data: bytes):
284282 # when transfer is complete the handler sends OK<CR><LF>
285283 response_code = self ._write_command (f"W { start } { len (data )} " )
286284 _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 )
288288 # Ignore response, it's not reliable
289289
290290 def ReadMemory (self , start : int , num_bytes : int ):
291291 """
292292 Send command with newline, receive response code\r \n <data>
293293 """
294- assert num_bytes % self .kWordSize == 0 # On a word boundary
294+
295+ # On a word boundary
296+ assert num_bytes % self .kWordSize == 0
295297 function = "ReadMemory"
296298 command = f"R { start } { num_bytes } "
297299
@@ -337,9 +339,9 @@ def Go(self, address: int, thumb_mode: bool = False):
337339 if thumb_mode :
338340 mode = "T"
339341 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" ]:
343345 _raise_return_code_error (response_code , "Go" )
344346
345347 def EraseSector (self , start : int , end : int ):
@@ -467,7 +469,8 @@ def SetCrystalFrequency(self, frequency_khz: int):
467469 verified = False
468470 for _ in range (3 ):
469471 try :
470- frame_in = self ._read_line () # Should be OK\r\n
472+ frame_in = self ._read_line ()
473+ # Should be OK\r\n
471474 if self .SyncVerifiedString in frame_in :
472475 verified = True
473476 break
@@ -526,7 +529,7 @@ def SyncConnection(self):
526529 # self._flush()
527530 _log .debug (f"Echoing sync string, { repr (self .SyncStringBytes )} " )
528531 time .sleep (0.1 )
529- self ._write (self .SyncStringBytes ) # echo SyncString
532+ self ._write (self .SyncStringBytes )
530533 self .write_newline ()
531534 self .write_newline ()
532535 # > Synchronized\n
@@ -575,7 +578,7 @@ class ChipDescription:
575578 Wraps a chip description line and exposes it as a class
576579 """
577580
578- kWordSize = 4 # 32 bit
581+ kWordSize = 4
579582 kPageSizeBytes = 64
580583 SectorSizePages = 16
581584 CRCLocation = 0x000002FC
@@ -595,14 +598,13 @@ def __init__(self, descriptor: dict[str, typing.Any]):
595598 self .RAMBufferSize = int (descriptor .pop ("RAMBufferSize" ))
596599 self .SectorCount : int = int (descriptor .pop ("SectorCount" ))
597600 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
600604
601605 assert self .RAMRange [0 ] > 0
602606 assert self .RAMRange [1 ] > self .RAMRange [0 ]
603-
604607 assert self .FlashRange [1 ] > self .FlashRange [0 ]
605-
606608 assert self .SectorCount > 0
607609
608610 @property
@@ -644,9 +646,7 @@ def RamRangeLegal(self, address, length):
644646
645647# Script tools
646648
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
650650
651651
652652def RemoveBootableCheckSum (vector_table_loc : int , image : bytes ) -> bytes :
@@ -673,7 +673,8 @@ def GetCheckSumedVectorTable(vector_table_loc: int, orig_image: bytes) -> bytes:
673673
674674 # calculate the checksum over the interrupt vectors
675675 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
677678 csum = tools .CalculateCheckSum (intvecs_list )
678679 intvecs_list [vector_table_loc ] = csum
679680 vector_table_bytes = b""
@@ -684,7 +685,6 @@ def GetCheckSumedVectorTable(vector_table_loc: int, orig_image: bytes) -> bytes:
684685
685686def MakeBootable (vector_table_loc : int , orig_image : bytes ) -> bytes :
686687 vector_table_bytes = GetCheckSumedVectorTable (vector_table_loc , orig_image )
687-
688688 image = vector_table_bytes + orig_image [len (vector_table_bytes ) :]
689689 return image
690690
@@ -797,7 +797,8 @@ def WriteFlashSector(
797797def WriteSector (isp : ISPConnection , chip : ChipDescription , sector : int , data : bytes ):
798798 assert len (data ) > 0
799799
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 :
801802 data += bytes ([0xFF ] * (chip .sector_bytes - len (data )))
802803 WriteFlashSector (isp , chip , sector , data )
803804
@@ -826,10 +827,9 @@ def WriteBinaryToFlash(
826827 isp .Unlock ()
827828 for sector in reversed (range (start_sector , start_sector + sector_count )):
828829 _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 ]
833833 WriteSector (isp , chip , sector , data_chunk )
834834 time .sleep (isp .settings .flash_write_sleep )
835835
0 commit comments