44
44
from micropython import const
45
45
from adafruit_bus_device import spi_device
46
46
47
+ try :
48
+ from typing import Union , Optional
49
+ from busio import SPI
50
+ from digitalio import DigitalInOut
51
+ from circuitpython_typing import ReadableBuffer , WriteableBuffer
52
+ except ImportError :
53
+ pass
54
+
47
55
__version__ = "0.0.0-auto.0"
48
56
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SD.git"
49
57
@@ -86,7 +94,7 @@ class SDCard:
86
94
87
95
"""
88
96
89
- def __init__ (self , spi , cs , baudrate = 1320000 ):
97
+ def __init__ (self , spi : SPI , cs : DigitalInOut , baudrate : int = 1320000 ) -> None :
90
98
# Create an SPIDevice running at a lower initialization baudrate first.
91
99
self ._spi = spi_device .SPIDevice (spi , cs , baudrate = 250000 , extra_clocks = 8 )
92
100
@@ -102,7 +110,7 @@ def __init__(self, spi, cs, baudrate=1320000):
102
110
# Create a new SPIDevice with the (probably) higher operating baudrate.
103
111
self ._spi = spi_device .SPIDevice (spi , cs , baudrate = baudrate , extra_clocks = 8 )
104
112
105
- def _init_card (self , chip_select ) :
113
+ def _init_card (self , chip_select : DigitalInOut ) -> None :
106
114
"""Initialize the card in SPI mode."""
107
115
# clock card at least 80 cycles with cs high
108
116
with self ._spi as card :
@@ -152,7 +160,7 @@ def _init_card(self, chip_select):
152
160
if self ._cmd (card , 16 , 512 , 0x15 ) != 0 :
153
161
raise OSError ("can't set 512 block size" )
154
162
155
- def _init_card_v1 (self , card ) :
163
+ def _init_card_v1 (self , card : SPI ) -> None :
156
164
"""Initialize v1 SDCards which use byte addressing."""
157
165
for _ in range (_CMD_TIMEOUT ):
158
166
self ._cmd (card , 55 , 0 , 0 )
@@ -161,7 +169,7 @@ def _init_card_v1(self, card):
161
169
return
162
170
raise OSError ("timeout waiting for v1 card" )
163
171
164
- def _init_card_v2 (self , card ) :
172
+ def _init_card_v2 (self , card : SPI ) -> None :
165
173
"""Initialize v2 SDCards which use 512-byte block addressing."""
166
174
ocr = bytearray (4 )
167
175
for _ in range (_CMD_TIMEOUT ):
@@ -180,7 +188,7 @@ def _init_card_v2(self, card):
180
188
return
181
189
raise OSError ("timeout waiting for v2 card" )
182
190
183
- def _wait_for_ready (self , card , timeout = 0.3 ):
191
+ def _wait_for_ready (self , card : SPI , timeout : float = 0.3 ) -> None :
184
192
"""
185
193
Wait for the card to clock out 0xff to indicate its ready.
186
194
@@ -196,17 +204,25 @@ def _wait_for_ready(self, card, timeout=0.3):
196
204
# pylint: disable=no-member
197
205
# no-member disable should be reconsidered when it can be tested
198
206
def _cmd (
199
- self , card , cmd , arg = 0 , crc = 0 , response_buf = None , data_block = True , wait = True
200
- ):
207
+ self ,
208
+ card : SPI ,
209
+ cmd : int ,
210
+ arg : Union [int , ReadableBuffer ] = 0 ,
211
+ crc : int = 0 ,
212
+ response_buf : Optional [WriteableBuffer ] = None ,
213
+ data_block : bool = True ,
214
+ wait : bool = True ,
215
+ ) -> int :
201
216
"""
202
217
Issue a command to the card and read an optional data response.
203
218
204
219
:param busio.SPI card: The locked SPI bus.
205
220
:param int cmd: The command number.
206
221
:param int|buf(4) arg: The command argument
207
222
:param int crc: The crc to allow the card to verify the command and argument.
208
- :param bytearray response_buf: Buffer to read a data block response into.
223
+ :param WriteableBuffer response_buf: Buffer to read a data block response into.
209
224
:param bool data_block: True if the response data is in a data block.
225
+ :param bool wait: True if the command should wait until the card is ready
210
226
"""
211
227
# create and send the command
212
228
buf = self ._cmdbuf
@@ -252,14 +268,22 @@ def _cmd(
252
268
# pylint: enable-msg=too-many-arguments
253
269
254
270
# pylint: disable-msg=too-many-arguments
255
- def _block_cmd (self , card , cmd , block , crc , response_buf = None ):
271
+ def _block_cmd (
272
+ self ,
273
+ card : SPI ,
274
+ cmd : int ,
275
+ block : int ,
276
+ crc : int ,
277
+ response_buf : Optional [WriteableBuffer ] = None ,
278
+ ) -> int :
256
279
"""
257
280
Issue a command to the card with a block argument.
258
281
259
282
:param busio.SPI card: The locked SPI bus.
260
283
:param int cmd: The command number.
261
284
:param int block: The relevant block.
262
285
:param int crc: The crc to allow the card to verify the command and argument.
286
+ :param WriteableBuffer response_buf: Buffer to read a data block response into.
263
287
"""
264
288
if self ._cdv == 1 :
265
289
return self ._cmd (card , cmd , block , crc , response_buf = response_buf )
@@ -301,12 +325,13 @@ def _block_cmd(self, card, cmd, block, crc, response_buf=None):
301
325
302
326
# pylint: enable-msg=too-many-arguments
303
327
304
- def _cmd_nodata (self , card , cmd , response = 0xFF ):
328
+ def _cmd_nodata (self , card : SPI , cmd : int , response : int = 0xFF ) -> int :
305
329
"""
306
330
Issue a command to the card with no argument.
307
331
308
332
:param busio.SPI card: The locked SPI bus.
309
333
:param int cmd: The command number.
334
+ :param int response: The expected response, default is ``0xFF``
310
335
"""
311
336
buf = self ._cmdbuf
312
337
buf [0 ] = cmd
@@ -319,12 +344,14 @@ def _cmd_nodata(self, card, cmd, response=0xFF):
319
344
return 0 # OK
320
345
return 1 # timeout
321
346
322
- def _readinto (self , card , buf , start = 0 , end = None ):
347
+ def _readinto (
348
+ self , card : SPI , buf : WriteableBuffer , start : int = 0 , end : Optional [int ] = None
349
+ ) -> None :
323
350
"""
324
351
Read a data block into buf.
325
352
326
353
:param busio.SPI card: The locked SPI bus.
327
- :param bytearray buf: The buffer to write into
354
+ :param WriteableBuffer buf: The buffer to write into
328
355
:param int start: The first index to write data at
329
356
:param int end: The index after the last byte to write to.
330
357
"""
@@ -342,13 +369,20 @@ def _readinto(self, card, buf, start=0, end=None):
342
369
card .readinto (self ._cmdbuf , end = 2 , write_value = 0xFF )
343
370
344
371
# pylint: disable-msg=too-many-arguments
345
- def _write (self , card , token , buf , start = 0 , end = None ):
372
+ def _write (
373
+ self ,
374
+ card : SPI ,
375
+ token : int ,
376
+ buf : ReadableBuffer ,
377
+ start : int = 0 ,
378
+ end : Optional [int ] = None ,
379
+ ) -> int :
346
380
"""
347
381
Write a data block to the card.
348
382
349
383
:param busio.SPI card: The locked SPI bus.
350
384
:param int token: The start token
351
- :param bytearray buf: The buffer to write from
385
+ :param ReadableBuffer buf: The buffer to write from
352
386
:param int start: The first index to read data from
353
387
:param int end: The index after the last byte to read from.
354
388
"""
@@ -386,7 +420,7 @@ def _write(self, card, token, buf, start=0, end=None):
386
420
387
421
# pylint: enable-msg=too-many-arguments
388
422
389
- def count (self ):
423
+ def count (self ) -> int :
390
424
"""
391
425
Returns the total number of sectors.
392
426
@@ -395,12 +429,12 @@ def count(self):
395
429
"""
396
430
return self ._sectors
397
431
398
- def readblocks (self , start_block , buf ) :
432
+ def readblocks (self , start_block : int , buf : WriteableBuffer ) -> int :
399
433
"""
400
434
Read one or more blocks from the card
401
435
402
436
:param int start_block: The block to start reading from
403
- :param bytearray buf: The buffer to write into. Length must be multiple of 512.
437
+ :param WriteableBuffer buf: The buffer to write into. Length must be multiple of 512.
404
438
"""
405
439
nblocks , err = divmod (len (buf ), 512 )
406
440
assert nblocks and not err , "Buffer length is invalid"
@@ -429,12 +463,12 @@ def readblocks(self, start_block, buf):
429
463
ret = self ._single_byte [0 ]
430
464
return 0
431
465
432
- def writeblocks (self , start_block , buf ) :
466
+ def writeblocks (self , start_block : int , buf : ReadableBuffer ) -> int :
433
467
"""
434
468
Write one or more blocks to the card
435
469
436
470
:param int start_block: The block to start writing to
437
- :param bytearray buf: The buffer to write into. Length must be multiple of 512.
471
+ :param ReadableBuffer buf: The buffer to write into. Length must be multiple of 512.
438
472
"""
439
473
nblocks , err = divmod (len (buf ), 512 )
440
474
assert nblocks and not err , "Buffer length is invalid"
@@ -462,7 +496,7 @@ def writeblocks(self, start_block, buf):
462
496
return 0
463
497
464
498
465
- def _calculate_crc_table ():
499
+ def _calculate_crc_table () -> bytearray :
466
500
"""Precompute the table used in calculate_crc."""
467
501
# Code converted from https://github.com/hazelnusse/crc7/blob/master/crc7.cc by devoh747
468
502
# With permission from Dale Lukas Peterson <[email protected] >
@@ -487,7 +521,7 @@ def _calculate_crc_table():
487
521
CRC_TABLE = _calculate_crc_table ()
488
522
489
523
490
- def calculate_crc (message ) :
524
+ def calculate_crc (message : ReadableBuffer ) -> int :
491
525
"""
492
526
Calculate the CRC of message[0:5], using a precomputed table in CRC_TABLE.
493
527
0 commit comments