3737_TOKEN_STOP_TRAN = const (0xFD )
3838_TOKEN_DATA = const (0xFE )
3939
40+ ## This function computes crc7 for SDcard commands
41+ ## It is not optimized, so do not call it where performance is critical
42+ ## You should usually be precomputing these values and hardcode them
43+ #def crc7( cmd:bytes ):
44+ # crc = 0
45+ # for value in cmd:
46+ # for i in range(8):
47+ # crc <<= 1
48+ # if (value&0x80)^(crc&0x80):
49+ # crc ^= 0x09
50+ # value <<= 1
51+ # return ((crc<<1)|1)&0xFF
52+ #def cmd_args_to_bytes( cmd:int, args:int ):
53+ # return bytes( [cmd|0x40,(args>>24)&0xFF,(args>>16)&0xFF,(args>>8)&0xFF,args&0xFF] )
4054
4155class SDCard :
4256def __init__ (self ,spi ,cs ,baudrate = 1320000 ):
@@ -92,7 +106,7 @@ def init_card(self, baudrate):
92106
93107# get the number of sectors
94108# CMD9: response R2 (R1 byte + 16-byte block read)
95- if self .cmd (9 ,0 ,0 ,0 ,False )!= 0 :
109+ if self .cmd (9 ,0 ,0xAF ,0 ,False )!= 0 :
96110raise OSError ("no response from SD card" )
97111csd = bytearray (16 )
98112self .readinto (csd )
@@ -109,7 +123,7 @@ def init_card(self, baudrate):
109123# print('sectors', self.sectors)
110124
111125# CMD16: set block length to 512 bytes
112- if self .cmd (16 ,512 ,0 )!= 0 :
126+ if self .cmd (16 ,512 ,0x15 )!= 0 :
113127raise OSError ("can't set 512 block size" )
114128
115129# set to high data rate now that it's initialised
@@ -118,8 +132,8 @@ def init_card(self, baudrate):
118132def init_card_v1 (self ):
119133for i in range (_CMD_TIMEOUT ):
120134time .sleep_ms (50 )
121- self .cmd (55 ,0 ,0 )
122- if self .cmd (41 ,0 ,0 )== 0 :
135+ self .cmd (55 ,0 ,0x65 )
136+ if self .cmd (41 ,0 ,0xE5 )== 0 :
123137# SDSC card, uses byte addressing in read/write/erase commands
124138self .cdv = 512
125139# print("[SDCard] v1 card")
@@ -129,10 +143,10 @@ def init_card_v1(self):
129143def init_card_v2 (self ):
130144for i in range (_CMD_TIMEOUT ):
131145time .sleep_ms (50 )
132- self .cmd (58 ,0 ,0 ,4 )
133- self .cmd (55 ,0 ,0 )
134- if self .cmd (41 ,0x40000000 ,0 )== 0 :
135- self .cmd (58 ,0 ,0 ,- 4 )# 4-byte response, negative means keep the first byte
146+ self .cmd (58 ,0 ,0xFD ,4 )
147+ self .cmd (55 ,0 ,0x65 )
148+ if self .cmd (41 ,0x40000000 ,0x77 )== 0 :
149+ self .cmd (58 ,0 ,0xFD ,- 4 )# 4-byte response, negative means keep the first byte
136150ocr = self .tokenbuf [0 ]# get first byte of response, which is OCR
137151if not ocr & 0x40 :
138152# SDSC card, uses byte addressing in read/write/erase commands
@@ -250,15 +264,15 @@ def readblocks(self, block_num, buf):
250264assert nblocks and not len (buf )% 512 ,"Buffer length is invalid"
251265if nblocks == 1 :
252266# CMD17: set read address for single block
253- if self .cmd (17 ,block_num * self .cdv ,0 ,release = False )!= 0 :
267+ if self .cmd (17 ,block_num * self .cdv ,0x7F ,release = False )!= 0 :
254268# release the card
255269self .cs (1 )
256270raise OSError (5 )# EIO
257271# receive the data and release card
258272self .readinto (buf )
259273else :
260274# CMD18: set read address for multiple blocks
261- if self .cmd (18 ,block_num * self .cdv ,0 ,release = False )!= 0 :
275+ if self .cmd (18 ,block_num * self .cdv ,0x7F ,release = False )!= 0 :
262276# release the card
263277self .cs (1 )
264278raise OSError (5 )# EIO
@@ -269,7 +283,7 @@ def readblocks(self, block_num, buf):
269283self .readinto (mv [offset :offset + 512 ])
270284offset += 512
271285nblocks -= 1
272- if self .cmd (12 ,0 ,0xFF ,skip1 = True ):
286+ if self .cmd (12 ,0 ,0x61 ,skip1 = True ):
273287raise OSError (5 )# EIO
274288
275289def writeblocks (self ,block_num ,buf ):