33
33
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_AVRprog.git"
34
34
35
35
try :
36
+ from typing import Any , Dict , List , Optional , Tuple , Union , TypedDict
37
+ from typing_extensions import TypeAlias
36
38
from os import PathLike
37
- from typing import Any , Dict , List , Optional , Tuple , TypeAlias , Union
39
+ from busio import SPI
40
+ from microcontroller import Pin
38
41
39
42
# Technically this type should come from: from _typeshed import FileDescriptorOrPath
40
43
# Unfortunately _typeshed is only in the standard library in newer releases of Python, e.g. 3.11
41
44
# Thus have to define a placeholder
42
- FileDescriptorOrPath : TypeAlias = (
43
- int | str | bytes | PathLike [str ] | PathLike [bytes ]
44
- )
45
+ FileDescriptorOrPath : TypeAlias = Union [
46
+ int , str , bytes , PathLike [str ], PathLike [bytes ]
47
+ ]
48
+
45
49
from io import TextIOWrapper
46
50
51
+ class ChipDictionary (TypedDict ):
52
+ """
53
+ Dictionary representing a specific target chip type
54
+ """
55
+
56
+ name : str
57
+ sig : List [int ]
58
+ flash_size : int
59
+ page_size : int
60
+ fuse_mask : Tuple [int ]
61
+
62
+ class FileState (TypedDict ):
63
+ """
64
+ Dictionary representing a File State
65
+ """
66
+
67
+ # pylint: disable=invalid-name
68
+ line : int
69
+ ext_addr : int
70
+ eof : bool
71
+ f : Optional [TextIOWrapper ]
72
+
47
73
except ImportError :
48
74
pass
49
75
50
- from math import floor
51
76
52
- from busio import I2C , SPI
53
77
from digitalio import DigitalInOut , Direction
54
78
55
79
_SLOW_CLOCK : int = 100000
@@ -104,10 +128,10 @@ class Boards:
104
128
"fuse_mask" : (0xFF , 0xFF , 0x07 , 0x3F ),
105
129
}
106
130
107
- _spi : SPI = None
108
- _rst : DigitalInOut = None
131
+ _spi : Optional [ SPI ] = None
132
+ _rst : Optional [ DigitalInOut ] = None
109
133
110
- def init (self , spi_bus : SPI , rst_pin : Union [ SPI , I2C ] ) -> None :
134
+ def init (self , spi_bus : SPI , rst_pin : Pin ) -> None :
111
135
"""
112
136
Initialize the programmer with an SPI port that will be used to
113
137
communicate with the chip. Make sure your SPI supports 'write_readinto'
@@ -118,7 +142,7 @@ def init(self, spi_bus: SPI, rst_pin: Union[SPI, I2C]) -> None:
118
142
self ._rst .direction = Direction .OUTPUT
119
143
self ._rst .value = True
120
144
121
- def verify_sig (self , chip : Dict [ str , Any ] , verbose : bool = False ) -> bool :
145
+ def verify_sig (self , chip : ChipDictionary , verbose : bool = False ) -> bool :
122
146
"""
123
147
Verify that the chip is connected properly, responds to commands,
124
148
and has the correct signature. Returns True/False based on success
@@ -156,7 +180,7 @@ def program_file(
156
180
self .begin (clock = clock_speed )
157
181
158
182
# create a file state dictionary
159
- file_state = {"line" : 0 , "ext_addr" : 0 , "eof" : False , "f" : TextIOWrapper }
183
+ file_state = {"line" : 0 , "ext_addr" : 0 , "eof" : False , "f" : None }
160
184
with open (file_name , "r" ) as file_state ["f" ]:
161
185
page_size = chip ["page_size" ]
162
186
@@ -219,7 +243,7 @@ def verify_file(
219
243
raise RuntimeError ("Signature read failure" )
220
244
221
245
# create a file state dictionary
222
- file_state = {"line" : 0 , "ext_addr" : 0 , "eof" : False , "f" : TextIOWrapper }
246
+ file_state = {"line" : 0 , "ext_addr" : 0 , "eof" : False , "f" : None }
223
247
with open (file_name , "r" ) as file_state ["f" ]:
224
248
page_size = chip ["page_size" ]
225
249
clock_speed = chip .get ("clock_speed" , _FAST_CLOCK )
@@ -386,7 +410,7 @@ def _flash_page(
386
410
self , page_buffer : bytearray , page_addr : int , page_size : int
387
411
) -> None :
388
412
page_addr //= 2 # address is by 'words' not bytes!
389
- for i in range (floor ( page_size / 2 ) ): # page indexed by words, not bytes
413
+ for i in range (page_size / 2 ): # page indexed by words, not bytes
390
414
lo_byte , hi_byte = page_buffer [2 * i : 2 * i + 2 ]
391
415
self ._flash_word (i , lo_byte , hi_byte )
392
416
0 commit comments