34
34
35
35
try :
36
36
from io import TextIOWrapper
37
- from typing import Any , Dict , Optional
37
+ from typing import Any , Dict , Optional , Tuple
38
38
39
39
from _typeshed import FileDescriptorOrPath
40
40
from busio import SPI
41
41
except ImportError :
42
42
pass
43
43
44
+ from math import floor
45
+
44
46
from digitalio import DigitalInOut , Direction
45
47
46
48
_SLOW_CLOCK : int = 100000
@@ -324,14 +326,14 @@ def begin(self, clock: int = _FAST_CLOCK) -> None:
324
326
self ._spi .configure (baudrate = clock )
325
327
self ._transaction ((0xAC , 0x53 , 0 , 0 ))
326
328
327
- def end (self ):
329
+ def end (self ) -> None :
328
330
"""
329
331
End programming mode: SPI is released, and reset pin set high.
330
332
"""
331
333
self ._spi .unlock ()
332
334
self ._rst .value = True
333
335
334
- def read_signature (self ):
336
+ def read_signature (self ) -> list :
335
337
"""
336
338
Read and return the signature of the chip as two bytes in an array.
337
339
Requires calling begin() beforehand to put in programming mode.
@@ -342,7 +344,7 @@ def read_signature(self):
342
344
sig .append (self ._transaction ((0x30 , 0 , i , 0 ))[2 ])
343
345
return sig
344
346
345
- def read (self , addr , read_buffer ) :
347
+ def read (self , addr : int , read_buffer : bytearray ) -> None :
346
348
"""
347
349
Read a chunk of memory from address 'addr'. The amount read is the
348
350
same as the size of the bytearray 'read_buffer'. Data read is placed
@@ -366,13 +368,15 @@ def read(self, addr, read_buffer):
366
368
last_addr = read_addr
367
369
368
370
#################### Low level
369
- def _flash_word (self , addr , low , high ) :
371
+ def _flash_word (self , addr : int , low : int , high : int ) -> None :
370
372
self ._transaction ((0x40 , addr >> 8 , addr , low ))
371
373
self ._transaction ((0x48 , addr >> 8 , addr , high ))
372
374
373
- def _flash_page (self , page_buffer , page_addr , page_size ):
375
+ def _flash_page (
376
+ self , page_buffer : bytearray , page_addr : int , page_size : int
377
+ ) -> None :
374
378
page_addr //= 2 # address is by 'words' not bytes!
375
- for i in range (page_size / 2 ): # page indexed by words, not bytes
379
+ for i in range (floor ( page_size / 2 ) ): # page indexed by words, not bytes
376
380
lo_byte , hi_byte = page_buffer [2 * i : 2 * i + 2 ]
377
381
self ._flash_word (i , lo_byte , hi_byte )
378
382
@@ -384,23 +388,25 @@ def _flash_page(self, page_buffer, page_addr, page_size):
384
388
raise RuntimeError ("Failed to commit page to flash" )
385
389
self ._busy_wait ()
386
390
387
- def _transaction (self , command ) :
391
+ def _transaction (self , command : Tuple [ int , int , int , int ]) -> bytes :
388
392
reply = bytearray (4 )
389
- command = bytearray ([i & 0xFF for i in command ])
393
+ command_bytes = bytearray ([i & 0xFF for i in command ])
390
394
391
- self ._spi .write_readinto (command , reply )
392
- # s = [hex(i) for i in command ]
393
- # print("Sending %s reply %s" % ([hex(i) for i in command ], [hex(i) for i in reply]))
394
- if reply [2 ] != command [1 ]:
395
+ self ._spi .write_readinto (command_bytes , reply )
396
+ # s = [hex(i) for i in command_bytes ]
397
+ # print("Sending %s reply %s" % ([hex(i) for i in command_bytes ], [hex(i) for i in reply]))
398
+ if reply [2 ] != command_bytes [1 ]:
395
399
raise RuntimeError ("SPI transaction failed" )
396
400
return reply [1 :] # first byte is ignored
397
401
398
- def _busy_wait (self ):
402
+ def _busy_wait (self ) -> None :
399
403
while self ._transaction ((0xF0 , 0 , 0 , 0 ))[2 ] & 0x01 :
400
404
pass
401
405
402
406
403
- def read_hex_page (file_state , page_addr , page_size , page_buffer ):
407
+ def read_hex_page (
408
+ file_state : Dict [str , Any ], page_addr : int , page_size : int , page_buffer : bytearray
409
+ ) -> bool :
404
410
# pylint: disable=too-many-branches
405
411
"""
406
412
Helper function that does the Intel Hex parsing. Takes in a dictionary
0 commit comments