32
32
__version__ = "0.0.0+auto.0"
33
33
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_AVRprog.git"
34
34
35
- from digitalio import Direction , DigitalInOut
35
+ try :
36
+ from io import TextIOWrapper
37
+ from typing import Any , Dict , Optional
36
38
37
- _SLOW_CLOCK = 100000
38
- _FAST_CLOCK = 1000000
39
+ from _typeshed import FileDescriptorOrPath
40
+ from busio import SPI
41
+ except ImportError :
42
+ pass
43
+
44
+ from digitalio import DigitalInOut , Direction
45
+
46
+ _SLOW_CLOCK : int = 100000
47
+ _FAST_CLOCK : int = 1000000
39
48
40
49
41
50
class AVRprog :
@@ -86,10 +95,10 @@ class Boards:
86
95
"fuse_mask" : (0xFF , 0xFF , 0x07 , 0x3F ),
87
96
}
88
97
89
- _spi = None
90
- _rst = None
98
+ _spi : SPI = None
99
+ _rst : DigitalInOut = None
91
100
92
- def init (self , spi_bus , rst_pin ):
101
+ def init (self , spi_bus : SPI , rst_pin ) -> None :
93
102
"""
94
103
Initialize the programmer with an SPI port that will be used to
95
104
communicate with the chip. Make sure your SPI supports 'write_readinto'
@@ -100,7 +109,7 @@ def init(self, spi_bus, rst_pin):
100
109
self ._rst .direction = Direction .OUTPUT
101
110
self ._rst .value = True
102
111
103
- def verify_sig (self , chip , verbose = False ):
112
+ def verify_sig (self , chip : Dict [ str , Any ], verbose : bool = False ) -> bool :
104
113
"""
105
114
Verify that the chip is connected properly, responds to commands,
106
115
and has the correct signature. Returns True/False based on success
@@ -115,7 +124,13 @@ def verify_sig(self, chip, verbose=False):
115
124
return True
116
125
117
126
# pylint: disable=too-many-branches
118
- def program_file (self , chip , file_name , verbose = False , verify = True ):
127
+ def program_file (
128
+ self ,
129
+ chip : Dict [str , Any ],
130
+ file_name : FileDescriptorOrPath ,
131
+ verbose : bool = False ,
132
+ verify : bool = True ,
133
+ ) -> bool :
119
134
"""
120
135
Perform a chip erase and program from a file that
121
136
contains Intel HEX data. Returns true on verify-success, False on
@@ -132,10 +147,8 @@ def program_file(self, chip, file_name, verbose=False, verify=True):
132
147
self .begin (clock = clock_speed )
133
148
134
149
# create a file state dictionary
135
- file_state = {"line" : 0 , "ext_addr" : 0 , "eof" : False }
136
- with open (file_name , "r" ) as file_state [ # pylint: disable=unspecified-encoding
137
- "f"
138
- ]:
150
+ file_state = {"line" : 0 , "ext_addr" : 0 , "eof" : False , "f" : TextIOWrapper }
151
+ with open (file_name , "r" ) as file_state ["f" ]:
139
152
page_size = chip ["page_size" ]
140
153
141
154
for page_addr in range (0 , chip ["flash_size" ], page_size ):
@@ -183,7 +196,9 @@ def program_file(self, chip, file_name, verbose=False, verify=True):
183
196
self .end ()
184
197
return True
185
198
186
- def verify_file (self , chip , file_name , verbose = False ):
199
+ def verify_file (
200
+ self , chip : Dict [str , Any ], file_name : FileDescriptorOrPath , verbose = False
201
+ ):
187
202
"""
188
203
Perform a chip full-flash verification from a file that
189
204
contains Intel HEX data. Returns True/False on success/fail.
@@ -192,10 +207,8 @@ def verify_file(self, chip, file_name, verbose=False):
192
207
raise RuntimeError ("Signature read failure" )
193
208
194
209
# create a file state dictionary
195
- file_state = {"line" : 0 , "ext_addr" : 0 , "eof" : False }
196
- with open (file_name , "r" ) as file_name [ # pylint: disable=unspecified-encoding
197
- "f"
198
- ]:
210
+ file_state = {"line" : 0 , "ext_addr" : 0 , "eof" : False , "f" : TextIOWrapper }
211
+ with open (file_name , "r" ) as file_name ["f" ]:
199
212
page_size = chip ["page_size" ]
200
213
clock_speed = chip .get ("clock_speed" , _FAST_CLOCK )
201
214
self .begin (clock = clock_speed )
@@ -230,9 +243,9 @@ def verify_file(self, chip, file_name, verbose=False):
230
243
self .end ()
231
244
return True
232
245
233
- def read_fuses (self , chip ) :
246
+ def read_fuses (self , chip : Dict [ str , Any ]) -> tuple :
234
247
"""
235
- Read the 4 fuses and return them in a list (low, high, ext, lock)
248
+ Read the 4 fuses and return them in a tuple (low, high, ext, lock)
236
249
Each fuse is bitwise-&'s with the chip's fuse mask for simplicity
237
250
"""
238
251
mask = chip ["fuse_mask" ]
@@ -245,7 +258,14 @@ def read_fuses(self, chip):
245
258
return (low , high , ext , lock )
246
259
247
260
# pylint: disable=unused-argument,too-many-arguments
248
- def write_fuses (self , chip , low = None , high = None , ext = None , lock = None ):
261
+ def write_fuses (
262
+ self ,
263
+ chip : Dict [str , Any ],
264
+ low : Optional [int ] = None ,
265
+ high : Optional [int ] = None ,
266
+ ext : Optional [int ] = None ,
267
+ lock : Optional [int ] = None ,
268
+ ) -> None :
249
269
"""
250
270
Write any of the 4 fuses. If the kwarg low/high/ext/lock is not
251
271
passed in or is None, that fuse is skipped
@@ -260,7 +280,14 @@ def write_fuses(self, chip, low=None, high=None, ext=None, lock=None):
260
280
self .end ()
261
281
262
282
# pylint: disable=too-many-arguments
263
- def verify_fuses (self , chip , low = None , high = None , ext = None , lock = None ):
283
+ def verify_fuses (
284
+ self ,
285
+ chip : Dict [str , Any ],
286
+ low : Optional [int ] = None ,
287
+ high : Optional [int ] = None ,
288
+ ext : Optional [int ] = None ,
289
+ lock : Optional [int ] = None ,
290
+ ) -> bool :
264
291
"""
265
292
Verify the 4 fuses. If the kwarg low/high/ext/lock is not
266
293
passed in or is None, that fuse is not checked.
@@ -286,7 +313,7 @@ def erase_chip(self):
286
313
287
314
#################### Mid level
288
315
289
- def begin (self , clock = _FAST_CLOCK ):
316
+ def begin (self , clock : int = _FAST_CLOCK ) -> None :
290
317
"""
291
318
Begin programming mode: pull reset pin low, initialize SPI, and
292
319
send the initialization command to get the AVR's attention.
0 commit comments