6565_CMD_TIMEOUT = const (200 )
6666
6767_R1_IDLE_STATE = const (1 << 0 )
68- #R1_ERASE_RESET = const(1 << 1)
68+ # R1_ERASE_RESET = const(1 << 1)
6969_R1_ILLEGAL_COMMAND = const (1 << 2 )
70- #R1_COM_CRC_ERROR = const(1 << 3)
71- #R1_ERASE_SEQUENCE_ERROR = const(1 << 4)
72- #R1_ADDRESS_ERROR = const(1 << 5)
73- #R1_PARAMETER_ERROR = const(1 << 6)
74- _TOKEN_CMD25 = const (0xfc )
75- _TOKEN_STOP_TRAN = const (0xfd )
76- _TOKEN_DATA = const (0xfe )
77-
78- #pylint: disable-msg=superfluous-parens
70+ # R1_COM_CRC_ERROR = const(1 << 3)
71+ # R1_ERASE_SEQUENCE_ERROR = const(1 << 4)
72+ # R1_ADDRESS_ERROR = const(1 << 5)
73+ # R1_PARAMETER_ERROR = const(1 << 6)
74+ _TOKEN_CMD25 = const (0xFC )
75+ _TOKEN_STOP_TRAN = const (0xFD )
76+ _TOKEN_DATA = const (0xFE )
77+
78+ # pylint: disable-msg=superfluous-parens
7979class SDCard :
8080 """Controls an SD card over SPI.
8181
@@ -100,6 +100,7 @@ class SDCard:
100100 os.listdir('/')
101101
102102 """
103+
103104 def __init__ (self , spi , cs , baudrate = 1320000 ):
104105 # This is the init baudrate.
105106 # We create a second device with the target baudrate after card initialization.
@@ -125,7 +126,7 @@ def _clock_card(self, cycles=8):
125126 self ._spi .spi .configure (baudrate = self ._spi .baudrate )
126127 self ._spi .chip_select .value = True
127128
128- self ._single_byte [0 ] = 0xff
129+ self ._single_byte [0 ] = 0xFF
129130 for _ in range (cycles // 8 + 1 ):
130131 self ._spi .spi .write (self ._single_byte )
131132 self ._spi .spi .unlock ()
@@ -145,7 +146,7 @@ def _init_card(self, baudrate):
145146
146147 # CMD8: determine card version
147148 rb7 = bytearray (4 )
148- r = self ._cmd (card , 8 , 0x01aa , 0x87 , rb7 , data_block = False )
149+ r = self ._cmd (card , 8 , 0x01AA , 0x87 , rb7 , data_block = False )
149150 if r == _R1_IDLE_STATE :
150151 self ._init_card_v2 (card )
151152 elif r == (_R1_IDLE_STATE | _R1_ILLEGAL_COMMAND ):
@@ -156,18 +157,18 @@ def _init_card(self, baudrate):
156157 # get the number of sectors
157158 # CMD9: response R2 (R1 byte + 16-byte block read)
158159 csd = bytearray (16 )
159- if self ._cmd (card , 9 , 0 , 0xaf , response_buf = csd ) != 0 :
160+ if self ._cmd (card , 9 , 0 , 0xAF , response_buf = csd ) != 0 :
160161 raise OSError ("no response from SD card" )
161- #self.readinto(csd)
162- csd_version = (csd [0 ] & 0xc0 ) >> 6
162+ # self.readinto(csd)
163+ csd_version = (csd [0 ] & 0xC0 ) >> 6
163164 if csd_version >= 2 :
164165 raise OSError ("SD card CSD format not supported" )
165166
166167 if csd_version == 1 :
167168 self ._sectors = ((csd [8 ] << 8 | csd [9 ]) + 1 ) * 1024
168169 else :
169- block_length = 2 ** (csd [5 ] & 0xf )
170- c_size = ((csd [6 ] & 0x3 ) << 10 ) | (csd [7 ] << 2 ) | ((csd [8 ] & 0xc ) >> 6 )
170+ block_length = 2 ** (csd [5 ] & 0xF )
171+ c_size = ((csd [6 ] & 0x3 ) << 10 ) | (csd [7 ] << 2 ) | ((csd [8 ] & 0xC ) >> 6 )
171172 mult = 2 ** (((csd [9 ] & 0x3 ) << 1 | (csd [10 ] & 0x80 ) >> 7 ) + 2 )
172173 self ._sectors = block_length // 512 * mult * (c_size + 1 )
173174
@@ -176,34 +177,35 @@ def _init_card(self, baudrate):
176177 raise OSError ("can't set 512 block size" )
177178
178179 # set to high data rate now that it's initialised
179- self ._spi = spi_device .SPIDevice (self ._spi .spi , self ._spi .chip_select ,
180- baudrate = baudrate , extra_clocks = 8 )
180+ self ._spi = spi_device .SPIDevice (
181+ self ._spi .spi , self ._spi .chip_select , baudrate = baudrate , extra_clocks = 8
182+ )
181183
182184 def _init_card_v1 (self , card ):
183185 """Initialize v1 SDCards which use byte addressing."""
184186 for _ in range (_CMD_TIMEOUT ):
185187 self ._cmd (card , 55 , 0 , 0 )
186188 if self ._cmd (card , 41 , 0 , 0 ) == 0 :
187- #print("[SDCard] v1 card")
189+ # print("[SDCard] v1 card")
188190 return
189191 raise OSError ("timeout waiting for v1 card" )
190192
191193 def _init_card_v2 (self , card ):
192194 """Initialize v2 SDCards which use 512-byte block addressing."""
193195 ocr = bytearray (4 )
194196 for _ in range (_CMD_TIMEOUT ):
195- time .sleep (.050 )
196- self ._cmd (card , 58 , 0 , 0xfd , response_buf = ocr , data_block = False )
197+ time .sleep (0 .050 )
198+ self ._cmd (card , 58 , 0 , 0xFD , response_buf = ocr , data_block = False )
197199 self ._cmd (card , 55 , 0 , 0x65 )
198200 # On non-longint builds, we cannot use 0x40000000 directly as the arg
199201 # so break it into bytes, which are interpreted by self._cmd().
200- if self ._cmd (card , 41 , b' \x40 \x00 \x00 \x00 ' , 0x77 ) == 0 :
201- self ._cmd (card , 58 , 0 , 0xfd , response_buf = ocr , data_block = False )
202+ if self ._cmd (card , 41 , b" \x40 \x00 \x00 \x00 " , 0x77 ) == 0 :
203+ self ._cmd (card , 58 , 0 , 0xFD , response_buf = ocr , data_block = False )
202204
203205 # Check for block addressing
204206 if (ocr [0 ] & 0x40 ) != 0 :
205207 self ._cdv = 1
206- #print("[SDCard] v2 card")
208+ # print("[SDCard] v2 card")
207209 return
208210 raise OSError ("timeout waiting for v2 card" )
209211
@@ -216,13 +218,15 @@ def _wait_for_ready(self, card, timeout=0.3):
216218 """
217219 start_time = time .monotonic ()
218220 self ._single_byte [0 ] = 0x00
219- while time .monotonic () - start_time < timeout and self ._single_byte [0 ] != 0xff :
220- card .readinto (self ._single_byte , write_value = 0xff )
221+ while time .monotonic () - start_time < timeout and self ._single_byte [0 ] != 0xFF :
222+ card .readinto (self ._single_byte , write_value = 0xFF )
221223
222224 # pylint: disable-msg=too-many-arguments
223225 # pylint: disable=no-member
224226 # no-member disable should be reconsidered when it can be tested
225- def _cmd (self , card , cmd , arg = 0 , crc = 0 , response_buf = None , data_block = True , wait = True ):
227+ def _cmd (
228+ self , card , cmd , arg = 0 , crc = 0 , response_buf = None , data_block = True , wait = True
229+ ):
226230 """
227231 Issue a command to the card and read an optional data response.
228232
@@ -237,17 +241,17 @@ def _cmd(self, card, cmd, arg=0, crc=0, response_buf=None, data_block=True, wait
237241 buf = self ._cmdbuf
238242 buf [0 ] = 0x40 | cmd
239243 if isinstance (arg , int ):
240- buf [1 ] = (arg >> 24 ) & 0xff
241- buf [2 ] = (arg >> 16 ) & 0xff
242- buf [3 ] = (arg >> 8 ) & 0xff
243- buf [4 ] = arg & 0xff
244+ buf [1 ] = (arg >> 24 ) & 0xFF
245+ buf [2 ] = (arg >> 16 ) & 0xFF
246+ buf [3 ] = (arg >> 8 ) & 0xFF
247+ buf [4 ] = arg & 0xFF
244248 elif len (arg ) == 4 :
245249 # arg can be a 4-byte buf
246250 buf [1 :5 ] = arg
247251 else :
248252 raise ValueError ()
249253
250- if ( crc == 0 ) :
254+ if crc == 0 :
251255 buf [5 ] = calculate_crc (buf [:- 1 ])
252256 else :
253257 buf [5 ] = crc
@@ -259,21 +263,22 @@ def _cmd(self, card, cmd, arg=0, crc=0, response_buf=None, data_block=True, wait
259263
260264 # wait for the response (response[7] == 0)
261265 for _ in range (_CMD_TIMEOUT ):
262- card .readinto (buf , end = 1 , write_value = 0xff )
266+ card .readinto (buf , end = 1 , write_value = 0xFF )
263267 if not (buf [0 ] & 0x80 ):
264268 if response_buf :
265269 if data_block :
266270 # Wait for the start block byte
267- buf [1 ] = 0xff
268- while buf [1 ] != 0xfe :
269- card .readinto (buf , start = 1 , end = 2 , write_value = 0xff )
270- card .readinto (response_buf , write_value = 0xff )
271+ buf [1 ] = 0xFF
272+ while buf [1 ] != 0xFE :
273+ card .readinto (buf , start = 1 , end = 2 , write_value = 0xFF )
274+ card .readinto (response_buf , write_value = 0xFF )
271275 if data_block :
272276 # Read the checksum
273- card .readinto (buf , start = 1 , end = 3 , write_value = 0xff )
277+ card .readinto (buf , start = 1 , end = 3 , write_value = 0xFF )
274278 return buf [0 ]
275279 return - 1
276- #pylint: enable-msg=too-many-arguments
280+
281+ # pylint: enable-msg=too-many-arguments
277282
278283 # pylint: disable-msg=too-many-arguments
279284 def _block_cmd (self , card , cmd , block , crc , response_buf = None ):
@@ -294,12 +299,12 @@ def _block_cmd(self, card, cmd, block, crc, response_buf=None):
294299 # We address by byte because cdv is 512. Instead of multiplying, shift
295300 # the data to the correct spot so that we don't risk creating a long
296301 # int.
297- buf [1 ] = (block >> 15 ) & 0xff
298- buf [2 ] = (block >> 7 ) & 0xff
299- buf [3 ] = (block << 1 ) & 0xff
302+ buf [1 ] = (block >> 15 ) & 0xFF
303+ buf [2 ] = (block >> 7 ) & 0xFF
304+ buf [3 ] = (block << 1 ) & 0xFF
300305 buf [4 ] = 0
301306
302- if ( crc == 0 ) :
307+ if crc == 0 :
303308 buf [5 ] = calculate_crc (buf [:- 1 ])
304309 else :
305310 buf [5 ] = crc
@@ -311,7 +316,7 @@ def _block_cmd(self, card, cmd, block, crc, response_buf=None):
311316
312317 # wait for the response (response[7] == 0)
313318 for _ in range (_CMD_TIMEOUT ):
314- card .readinto (buf , end = 1 , write_value = 0xff )
319+ card .readinto (buf , end = 1 , write_value = 0xFF )
315320 if not (buf [0 ] & 0x80 ):
316321 result = buf [0 ]
317322 break
@@ -322,9 +327,10 @@ def _block_cmd(self, card, cmd, block, crc, response_buf=None):
322327 self ._readinto (card , response_buf )
323328
324329 return result
330+
325331 # pylint: enable-msg=too-many-arguments
326332
327- def _cmd_nodata (self , card , cmd , response = 0xff ):
333+ def _cmd_nodata (self , card , cmd , response = 0xFF ):
328334 """
329335 Issue a command to the card with no argument.
330336
@@ -333,14 +339,14 @@ def _cmd_nodata(self, card, cmd, response=0xff):
333339 """
334340 buf = self ._cmdbuf
335341 buf [0 ] = cmd
336- buf [1 ] = 0xff
342+ buf [1 ] = 0xFF
337343
338344 card .write (buf , end = 2 )
339345 for _ in range (_CMD_TIMEOUT ):
340- card .readinto (buf , end = 1 , write_value = 0xff )
346+ card .readinto (buf , end = 1 , write_value = 0xFF )
341347 if buf [0 ] == response :
342- return 0 # OK
343- return 1 # timeout
348+ return 0 # OK
349+ return 1 # timeout
344350
345351 def _readinto (self , card , buf , start = 0 , end = None ):
346352 """
@@ -355,14 +361,14 @@ def _readinto(self, card, buf, start=0, end=None):
355361 end = len (buf )
356362
357363 # read until start byte (0xfe)
358- buf [start ] = 0xff # busy
359- while buf [start ] != 0xfe :
360- card .readinto (buf , start = start , end = start + 1 , write_value = 0xff )
364+ buf [start ] = 0xFF # busy
365+ while buf [start ] != 0xFE :
366+ card .readinto (buf , start = start , end = start + 1 , write_value = 0xFF )
361367
362- card .readinto (buf , start = start , end = end , write_value = 0xff )
368+ card .readinto (buf , start = start , end = end , write_value = 0xFF )
363369
364370 # read checksum and throw it away
365- card .readinto (self ._cmdbuf , end = 2 , write_value = 0xff )
371+ card .readinto (self ._cmdbuf , end = 2 , write_value = 0xFF )
366372
367373 # pylint: disable-msg=too-many-arguments
368374 def _write (self , card , token , buf , start = 0 , end = None ):
@@ -385,27 +391,28 @@ def _write(self, card, token, buf, start=0, end=None):
385391 cmd [0 ] = token
386392 card .write (cmd , end = 1 )
387393 card .write (buf , start = start , end = end )
388- cmd [0 ] = 0xff
389- cmd [1 ] = 0xff
394+ cmd [0 ] = 0xFF
395+ cmd [1 ] = 0xFF
390396 card .write (cmd , end = 2 )
391397
392398 # check the response
393399 # pylint: disable=no-else-return
394400 # Disable should be removed when refactor can be tested
395401 for _ in range (_CMD_TIMEOUT ):
396- card .readinto (cmd , end = 1 , write_value = 0xff )
402+ card .readinto (cmd , end = 1 , write_value = 0xFF )
397403 if not (cmd [0 ] & 0x80 ):
398- if (cmd [0 ] & 0x1f ) != 0x05 :
404+ if (cmd [0 ] & 0x1F ) != 0x05 :
399405 return - 1
400406 else :
401407 break
402408
403409 # wait for write to finish
404- card .readinto (cmd , end = 1 , write_value = 0xff )
410+ card .readinto (cmd , end = 1 , write_value = 0xFF )
405411 while cmd [0 ] == 0 :
406- card .readinto (cmd , end = 1 , write_value = 0xff )
412+ card .readinto (cmd , end = 1 , write_value = 0xFF )
413+
414+ return 0 # worked
407415
408- return 0 # worked
409416 # pylint: enable-msg=too-many-arguments
410417
411418 def count (self ):
@@ -425,7 +432,7 @@ def readblocks(self, start_block, buf):
425432 :param bytearray buf: The buffer to write into. Length must be multiple of 512.
426433 """
427434 nblocks , err = divmod (len (buf ), 512 )
428- assert nblocks and not err , ' Buffer length is invalid'
435+ assert nblocks and not err , " Buffer length is invalid"
429436 with self ._spi as card :
430437 if nblocks == 1 :
431438 # CMD17: set read address for single block
@@ -445,7 +452,7 @@ def readblocks(self, start_block, buf):
445452 ret = self ._cmd (card , 12 , 0 , 0x61 , wait = False )
446453 # return first status 0 or last before card ready (0xff)
447454 while ret != 0 :
448- card .readinto (self ._single_byte , write_value = 0xff )
455+ card .readinto (self ._single_byte , write_value = 0xFF )
449456 if self ._single_byte [0 ] & 0x80 :
450457 return ret
451458 ret = self ._single_byte [0 ]
@@ -459,7 +466,7 @@ def writeblocks(self, start_block, buf):
459466 :param bytearray buf: The buffer to write into. Length must be multiple of 512.
460467 """
461468 nblocks , err = divmod (len (buf ), 512 )
462- assert nblocks and not err , ' Buffer length is invalid'
469+ assert nblocks and not err , " Buffer length is invalid"
463470 with self ._spi as card :
464471 if nblocks == 1 :
465472 # CMD24: set write address for single block
@@ -475,12 +482,15 @@ def writeblocks(self, start_block, buf):
475482 # send the data
476483 offset = 0
477484 while nblocks :
478- self ._write (card , _TOKEN_CMD25 , buf , start = offset , end = (offset + 512 ))
485+ self ._write (
486+ card , _TOKEN_CMD25 , buf , start = offset , end = (offset + 512 )
487+ )
479488 offset += 512
480489 nblocks -= 1
481490 self ._cmd_nodata (card , _TOKEN_STOP_TRAN , 0x0 )
482491 return 0
483492
493+
484494def _calculate_crc_table ():
485495 """Precompute the table used in calculate_crc."""
486496 # Code converted from https://github.com/hazelnusse/crc7/blob/master/crc7.cc by devoh747
@@ -492,18 +502,20 @@ def _calculate_crc_table():
492502
493503 # generate a table value for all 256 possible byte values
494504 for i in range (256 ):
495- if ( i & 0x80 ) :
505+ if i & 0x80 :
496506 crc_table [i ] = i ^ crc_poly
497507 else :
498508 crc_table [i ] = i
499509 for _ in range (1 , 8 ):
500510 crc_table [i ] = crc_table [i ] << 1
501- if ( crc_table [i ] & 0x80 ) :
511+ if crc_table [i ] & 0x80 :
502512 crc_table [i ] = crc_table [i ] ^ crc_poly
503513 return crc_table
504514
515+
505516CRC_TABLE = _calculate_crc_table ()
506517
518+
507519def calculate_crc (message ):
508520 """
509521 Calculate the CRC of message[0:5], using a precomputed table in CRC_TABLE.
@@ -515,4 +527,4 @@ def calculate_crc(message):
515527 for i in range (0 , 5 ):
516528 crc = CRC_TABLE [(crc << 1 ) ^ message [i ]]
517529
518- return (( crc << 1 ) | 1 )
530+ return (crc << 1 ) | 1
0 commit comments