Searching \ for '[PIC] SDHC card initialisation' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/microchip/devices.htm?key=pic
Search entire site for: 'SDHC card initialisation'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] SDHC card initialisation'
2011\08\18@050137 by IVP

face picon face
Hi all,

having the dickens of a job initialising a 4GB SDHC card. Unknown
manufacturer. And probably won't find out until I can read the card's
registers

The procedure is, from what I read,

Send > 74 clocks with /CS high and DI high
Send CMD0
Send CMD8
Send CMD55 + ACMD41, repeat until response = 0

Here's my results

http://home.clear.net.nz/pages/joecolquitt/sdhc.html

This procedure seems to be the one that generates the most "can't
get it to work" posts and the replies aren't consistent. Some say turn
this bit on, turn that bit off, repeat this bit, don't repeat etc etc

My reading includes

http://elm-chan.org/docs/mmc/mmc_e.html

which is referred to often, but IMVHO has inconsistencies with
the official SD spec pdfs he links to

Also tried the MChip forums, AVRfreaks and others

I'm at a loss to see why the card doesn't come out of initialisation
so I can move on to the next steps in the flow charts

More exact diagrams would be really really helpful. They say so
much more than lines and lines of geek speak

Any suggestions ?

TIA

Jo

2011\08\18@064150 by Nicola Perotto

picon face
part 1 2184 bytes content-type:text/plain; charset="iso-8859-1" (decoded quoted-printable)

Hi Joe,
I also found that the documentation you can find is somewhat inconsistent!
So the better source is the "Simplified specification" at SDcard association site:
https://www.sdcard.org/developers/tech/pls/

Attached my Init code (stripped from library, added some comment). It works well but is not so beautiful because I converted from the microchip library to ASM for  18F and deeply modified. If you want I can send to you the complete code, where remains some comment in italian...

I see that you dont abort if the response is "error", maybe the code is stripped...
Not all card responds to command CMD8

After a command that doesn't give a response (CMD55) or AFTER the last byte of the response you must send 0xFF for sync (also if more data is required like OCR) but not in case of command CMD8

      Nicola



On 18/08/2011 9.01, IVP wrote:
{Quote hidden}


part 2 5812 bytes content-type:text/plain; name="sdinit.asm"
(decoded base64)

;for PIC 18F
;Warning: not support MMC

sd_MediaInitialize
       bsf     SDC_SCLK                ;clock high
       bsf     SDC_CS                  ;Initialize Chip Select line
       bcf     SDC_tris_CS             ;output

       DelayOnems

;Media powers-up in the open-drain mode and cannot handle a clock faster
;than 400kHz. Initialize SPI port to slower than 400kHz
       movlw   cSSPSTAT
       movwf   SSPSTAT
       movlw   cSSPCON1 | SPI_FOSC_64  ;at 8 MHz you get a clock of 125 kHz
       movwf   SSPCON1

;let the card power on and initialize
       Delay100ms                      ;100ms

       movlw   .10                     ;Media requires >=74 clock cycles to startup 10*8=80
       movwf   sd_timeout+0
mi20
       movlw   0xff
       call    WriteSPI
       decfsz  sd_timeout+0,f
       bra     mi20

       DelayOnems

       bcf     SDC_CS                  ;Chip Select = 0

       clrf    sd_address+0
       clrf    sd_address+1
       clrf    sd_address+2
       clrf    sd_address+3
       movlw   GO_IDLE_STATE           ;Send CMD0 to reset the media
       call    sd_SendSDCCmd

       movlw   SDC_BAD_RESPONSE        ;0xFF
       cpfseq  sd_response+0           ;we have not got anything back from the card
       bra     mi30

       movlw   sdcCardInitCommFailure
       bra     InitError
mi30                                    ;See if the device is ready
       movlw   0x01                    ;0x01=No Err&Busy Initializing
       subwf   sd_response+0,w
       btfsc   STATUS,Z
       bra     mi40                    ;=1

       movlw   sdcCardNotInitFailure
       bra     InitError

mi40                                    ;***************************
       movlw   0xA5                    ;check pattern
       movwf   sd_address+0
       movlw   0x01                    ;VHS requested voltage 2.7 - 3.6V
       movwf   sd_address+1
       movlw   .0
       movwf   sd_address+2
       movlw   .0
       movwf   sd_address+3
       movlw   SEND_IF_COND
       call    sd_SendSDCCmd

       movlw   .1
       cpfseq  sd_response+0           ;Got 1: ok - jump and get next 32bits
       bra     mi50                    ;error, the card is not a SDHC

       LFSR    FSR0,sd_address
       movlw   .4
       movwf   sd_index+0
mi48
       call    ReadMedia
       movwf   POSTINC0                ;store data
       decfsz  sd_index+0,f
       bra     mi48

       movlw   0xff
       call    WriteSPI                ;Required clocking (see spec)
       bsf     SDC_CS

       movlw   0xA5
       cpfseq  sd_address+3            ;check pattern
       bra     mi69                    ;invalid voltage
       movlw   0x01                    ;VCA
       cpfseq  sd_address+2
       bra     mi69                    ;invalid voltage
       tstfsz  sd_address+1
       bra     mi69                    ;invalid voltage
       tstfsz  sd_address+0
       bra     mi69                    ;invalid voltage

mi50
       movlw   0xff
       movwf   sd_timeout+0
       movlw   0x0f
       movwf   sd_timeout+1

mi52                                    ;spec. V3: CMD8
       movlw   APP_CMD                 ;CMD55 - (address ignored)
       call    sd_SendSDCCmd
       movlw   .1
       cpfseq  sd_response+0           ;Got 1: ok
       bra     mi58                    ;error, retry

       clrf    sd_address+0
       clrf    sd_address+1
       clrf    sd_address+2
       movlw   0x40                    ;HCS = 1 support SDHC
       movwf   sd_address+3
       movlw   SD_APP_OP_COND          ;CMD41
       call    sd_SendSDCCmd
       movf    sd_response+0,f
       btfsc   STATUS,Z
       bra     mi60                    ;=0 ok!

mi58
       decfsz  sd_timeout+0,f
       bra     mi52
       decfsz  sd_timeout+1,f
       bra     mi52
       movlw   sdcCardInitTimeout      ;we have not got anything back from the card
       bra     InitError

mi60                                    ;get OCR
       call    sd_OCRRead
       movlw   sdcCardTypeInvalid
       tstfsz  sd_response+0
       bra     InitError

       btfsc   sd_OCR+3,6              ;1= SDHC o SDXC
       bsf     sdf_SDHC

       btfsc   sd_OCR+2,5              ;3.2 to 3.3V
       bra     mi70

mi69
       movlw   sdcInvalidVoltage
       bra     InitError

mi70                                    ;get the CSD register before increasing the speed
       call    sd_CSDRead
       movlw   sdcCardTypeInvalid
       tstfsz  sd_status               ;look at error code
       bra     InitError
mi80
#ifdef  USE_PLL
       bsf     OSCTUNE,PLLEN           ;enabled PLL
#endif
#ifdef USE_INTOSC
       bcf     OSCCON,SCS1
#endif
       movlw   cSSPCON1 | SPI_Speed
       movwf   SSPCON1
       movlw   0xff
       call    WriteSPI                ;sync clock

;Ok Now set the block length to 512. It should be already
       movlw   low(BLOCKLEN_512)
       movwf   sd_address+0
       movlw   high(BLOCKLEN_512)
       movwf   sd_address+1
       clrf    sd_address+2
       clrf    sd_address+3
       movlw   SET_BLOCKLEN
       call    sd_SendSDCCmd

mi90
       movlw   sdcValid
       movwf   sd_status
       return
   
InitError
       movwf   sd_status

       clrf    SSPCON1                 ;turn off MSSP
       bcf     SDC_SCLK                ;clock
       bcf     SDC_DO                  ;data
       bsf     SDC_CS                  ;deselect the devices
       bsf     SDC_tris_CS             ;input (3state)

       return
;sd_MediaInitialize

part 3 181 bytes content-type:text/plain; name="ATT00001.txt"
(decoded base64)

--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
mailman.mit.edu/mailman/listinfo/piclist

2011\08\18@071530 by IVP

face picon face

> Attached my Init code

Hi Nicola, thanks very much. I'll try it later today and report back
with any results or questions

Jo

2011\08\18@080510 by M.L.

flavicon
face
On Thu, Aug 18, 2011 at 5:01 AM, IVP <spam_OUTjoecolquittTakeThisOuTspamclear.net.nz> wrote:
{Quote hidden}

My procedure, worked on most cards last time I messed around with it.
SD cards are my least favorite peripherals to interface with because
they are sort-of documented (mostly) and it should probably work. Get
what I'm saying?

------------------------

Notes on SD card initialization for SPI mode:

1. SPI is low speed on startup of SD card
2. Turn power on for SD card
3. Card requires 74+ cycles to initialize; send dummy bytes to toggle clock

4. Issue command: GO_IDLE_STATE (CMD0) while holding CS LOW
Precalculated CRC-7 for this command: 0x95
Get response R1:
0b00000001  [no errors, enter idle mode]
If response is >1 then RETRY; goto 4

5. Issue command SEND_IF_COND (CMD8) to check if this is ver 1/MMC or
ver2 HC card.
precalculated CRC-7 for this command: 0x87
get R1 response (1 byte)
check idle bit:
if idle bit is clear then card is not HC
else
card gave affirmative response to CMD8. It should be sending 32 bits
of info with lower 12 bits = 0x1AA

6. If data from #5 ended in 0x1AA then card is V2
Else card is not V2.

7. Issue command ACMD41. From this point on, CRC is NOT NECESSARY
because you are in SPI mode.
Aka: CMD55 + CMD41
get R1 response
send SEND_OP_COND command (Start card initialization)
get R1 response
check R1_IDLE_STATE_BIT
if set then goto 7 (repeat #7 until out of idle state)

8. Set block size to 512 bytes
send SET_BLOCKLEN command
get R1 response

9. Read OCR and check CCS (bit 30)
Aka CMD58
send READ_OCR command
get R1 response
Get 32 bit R3 response

If bit 30 of response is set then card is SDHC

10. Read bytes from card until returned byte is 0xFF. Indicates card is idle.

.... Card is now ready (maybe)

I left out some error checking.
You need to flash the CS* line before each new command.



-- Martin K

2011\08\18@084024 by IVP

face picon face

> You need to flash the CS* line before each new command.

Martin,

I haven't seen that mentioned anywhere else

> they are sort-of documented (mostly) and it should probably work.
> Get what I'm saying?

Uh-huh

This has been as frustrating as My First LCD Projec

2011\08\18@085210 by M.L.

flavicon
face
On Thu, Aug 18, 2011 at 8:39 AM, IVP <.....joecolquittKILLspamspam@spam@clear.net.nz> wrote:
>
>> You need to flash the CS* line before each new command.
>
> Martin,
>
> I haven't seen that mentioned anywhere else
>
>> they are sort-of documented (mostly) and it should probably work.
>> Get what I'm saying?
>
> Uh-huh
>
> This has been as frustrating as My First LCD Project


IMO much more complicated than an LCD.

I could be wrong with the CS*, but I know my code works and it raises
then lowers the CS line before each new command byte. That's a typical
SPI procedure.

What are you using to get your logic analyzer traces?
-- Martin K

2011\08\18@090000 by IVP

face picon face
> That's a typical SPI procedure

And that's why I like pictures and timing diagrams

> What are you using to get your logic analyzer traces?

An Acute USB analyser (those traces at 200MHz), screen capture
and colour reduction/giffing with Paint Shop Pr

2011\08\19@061627 by IVP

face picon face

> IMO much more complicated than an LCD.

Agree. There's a mountain of associated paper work for one
thing. I thought today to pop the card into the DVD player,
which has a card slot. And if it worked put the analyser on it.
'Bad Card' unfortunately. Probably accepts only SD and not
SDHC. My camera uses SDHC but would I dare take that
apart ? Hmmmm, a little guy in red on my left shoulder is
saying "Do it, do it !". I don't think so pal

MChip Support are chasing up the SPI flag problem but my
feeling is that's not related to this issue

Jo

More... (looser matching)
- Last day of these posts
- In 2011 , 2012 only
- Today
- New search...