Sem*_*uru 11 embedded microcontroller initialization spi sd-card
我已经看过堆栈溢出问题使用SPI接口初始化microSD卡并没有看到任何与我的问题匹配的答案(也就是我尚未尝试过的事情).
我有类似的问题,我试图通过微控制器的SPI接口(特别是HC908)访问SD卡.我已经尝试遵循物理层简化规范v2.00中的流程图,它似乎在Transcend 1 GB和2 GB以及AE&C 1 GB卡上正确初始化.但是我在我的相机上使用的旧卡片藏有其他三张随机卡片时遇到了问题.
我的代码都是HC908汇编程序.我确定了SPI时钟线,并且在初始化期间它运行大约350 kHz(HC908在我的低MCU时钟速度下提供的唯一速度倍增器,在100 - 400 kHz窗口内).
以下是未完成初始化例程的三张卡的结果(所有这些都是连续完成而不更改任何代码或时序参数):
Canon 16Meg card (labeled as SD):
Set card select high
Send 80 SPI clock cycles (done by writing 0xFF 10 times)
Set card select low
Send CMD0 [0x400000000095] and Loop up to 8 times waiting for high bit on response to go low
R1 = 0x01 (indicates idle)
Send CMD8 [0x48000001AA87] and Loop up to 8 times waiting for high bit on response to go low
R1 = 0x05 (idle and illegal command)
Because illegal command set local flag to indicate v1 or MMC card
Send CMD58 [0x7A00000000FD] and Loop up to 8 times waiting for high bit on response to go low
R1 = 0x05 (idle and illegal command)
because illegal command branch to error routine
Send CMD13 [0x4D000000000D] (show status buffer) and Loop up to 8 times waiting for high bit on response to go low
R1= 0x05 (idle and illegal command)
Run Code Online (Sandbox Code Playgroud)
非法指令旗是否卡住了?我应该在CMD8之后做些什么来清除那面旗帜吗?
SanDisk UltraII 256Meg
Set card select high
Send 80 SPI clock cycles (done by writing 0xFF 10 times)
Set card select low
Send CMD0 [0x400000000095] and Loop up to 8 times waiting for high bit on response to go low
R1 = 0x01 (idle)
Send CMD8 [0x48000001AA87] and Loop up to 8 times waiting for high bit on response to go low
R1 = 0x05 (idle and illegal command)
Because illegal command set local flag to indicate v1 or MMC card
Send CMD58 [0x7A00000000FD] and Loop up to 8 times waiting for high bit on response to go low
R1 = 0x01 (idle)
Send 0xFF 4 times to read OCR
OCR = 0xFFFFFFFF
Send CMD55 [0x770000000065] (1st part of ACMD41) and Loop up to 8 times waiting for high bit on response to go low
R1 = 0x01 (idle)
Send CMD41 [0x6900000000E5] (2nd part of ACMD41) and Loop up to 8 times waiting for high bit on response to go low
R1 = 0x05 (idle and illegal command)
Because illegal command, assume card is MMC
Send CMD1 [0x4100000000F9] (for MMC) and Loop up to 8 times waiting for high bit on response to go low
R1 = 0x05 (idle and illegal command)
Repeat the CMD1 50 times (my arbitrary number to wait until idle clears)
Every R1 response is 0x05 (idle and illegal command)
Run Code Online (Sandbox Code Playgroud)
为什么OCR都是F?看起来不合适.另外,为什么ACMD41和CMD1会响应非法命令?CMD1失败是因为即使使用非法命令响应,CMD55之后卡正在等待有效的ACMD吗?
SanDisk ExtremeIII 2G:
Set card select high
Send 80 SPI clock cycles (done by writing 0xFF 10 times)
Set card select low
Send CMD0 [0x400000000095] and Loop up to 8 times waiting for high bit on response to go low
R1 = 0x01 (idle)
Send CMD8 [0x40000001AA87] and Loop up to 8 times waiting for high bit on response to go low
R1 = 0x7F (??? My loop shows the responses for each iteration and I got 0xFF 0xFF 0xC1 0x7F... is the card getting out of sync?)
Send CMD58 [0x7A00000000FD] and Loop up to 8 times waiting for high bit on response to go low
R1 = 0x01 (idle and back in sync)
Send 0xFF 4 times to read OCR
OCR = 0x00FF80
Send CMD55 [0x770000000065] (1st part of ACMD41) and Loop up to 8 times waiting for high bit on response to go low
R1 = 0x5F (??? loop responses are 0xFF 0xFF 0xF0 0x5F... again out of sync?)
Send CMD41 [0x6900000000E5] (2nd part of ACMD41) and Loop up to 8 times waiting for high bit on response to go low
R1 = 0x05 (idle and illegal command, but back in sync???)
Because illegal command, assume card is MMC
Send CMD1 [0x4100000000F9] (for MMC) and Loop up to 8 times waiting for high bit on response to go low
R1 = 0x7F (??? loop responses are 0xFF 0xFF 0xC1 0x7F... again out of sync?)
Repeat CMD1 and Loop up to 8 times waiting for high bit on response to go low
R1 = 0x01 (idle)
Repeat CMD1 and Loop up to 8 times waiting for high bit on response to go low
R1 = 0x7F (??? loop responses are 0xFF 0xFF 0xC1 0x7F... again out of sync?)
Repeat CMD1 and Loop up to 8 times waiting for high bit on response to go low
R1 = 0x00 (out of idle)
Send CMD9 [0x4900000000AF] (get CSD) and Loop up to 8 times waiting for high bit on response to go low
R1 = 0x3F (??? loop responses are 0xFF 0xFF 0xC1 0x3F... again out of sync?)
Code craps out because Illegal command bit is high.
Run Code Online (Sandbox Code Playgroud)
这张卡在地球上有什么问题?
有时它是同步的,有时则不同步.(上面的模式是可重复的.)我已经确定了这个模式,我没有看到MOSI/MISO传输之间经历任何流氓时钟周期.
Sem*_*uru 23
好的......我发现了我的问题.对于遇到此问题的任何其他人,重要的是要记住0xFF在收到回复后发送额外的内容.这为卡提供了额外的8个时钟周期,以便为下一个命令做好准备.有些卡似乎不需要它(例如我正在使用的Transcends),但其他卡需要它.
我实际上在我的'write command'例程的开头放了一个简单的循环,0xFF直到它0xFF作为响应才发送,所以我不必去所有不同的地方我读取响应以确保我发出额外的发送0xFF.因为只要SD卡(通常)涉及SPI模式,如果没有时钟周期进入,则时间静止.
有一点我注意到并且尚未找到答案(但到目前为止它没有伤害任何东西),在我读取CSR的16个字节后,似乎还有另外2个字节的非0xFF出现.那是CRC16吗?很奇怪,因为CSR内置了CRC ...