SD Card CMD1 error - Card not init.

Started by gigillo74 February 4, 2012
Dear all,
i try to init my SD card with ATMEGA8 and i don't reach the goal becouse the CMD1 don't return 0x00.
I attach my ASM code and i ask someone if can explain me if there is a mistake in code.
Thank You.
Luigi.
[CODE];******************************************************** ;* LIBRERIA DI FUNZIONI PER UTILIZZO DI SD CARD * ;* * ;* Nome: SD.inc * ;* Versione: 1.0 * ;* Data: 14/01/2012 * ;* Autore: Luigi Erroi * ;* * ;* Registri utilizzati: Work * ;* * ;* Variabili Utilizzate: NESSUNA * ;* * ;******************************************************** .EQU PortaSD = PORTB ; definisce porta della scheda SD .EQU CS = 0 ; Pin CS della scheda SD CMD0DATA: .db 0x40,0x00,0x00,0x00,0x00,0x95 ; SD card cmd 0 CMD1DATA: .db 0x41,0x00,0x00,0x00,0x00,0xFF ; SD card cmd 1 CMD9DATA: .db 0x49,0x00,0x00,0x00,0x00,0xFF ; SD card cmd 9 CMD8DATA: .db 0x48,0x00,0x00,0x01,0xAA,0x87 ; SD card cmd 8 CMD41DATA: .db 0x69,0x00,0x00,0x00,0x00,0xE5 ; SD card cmd 41 CMD55DATA: .db 0x77,0x00,0x00,0x00,0x00,0x65 ; SD card cmd 55 CMD58DATA: .db 0x7A,0x00,0x00,0x00,0x00,0xFD ; SD card cmd 55 ;******************************************* ; Inizializzazione della scheda SD ;******************************************* SD_Init: sbi PortaSD,CS push R23 ldi R23,10 ldi Work,0xFF SD_Init_Seq: dec R23 breq SD_Init_Seq2 rcall SPI_TX rjmp SD_Init_Seq SD_Init_Seq2: rcall CMD0_Send ldi R23,8 SD_CMD0_Loop: dec R23 breq SD_Init_Exit rcall SPI_RX cpi Work,0x01 brne SD_CMD0_Loop ldi Work,0x10 rcall Lcdlocate ldi Work,'O' rcall LcdSendData ldi Work,'K' rcall LcdSendData rcall CMD8_Send ldi R23,8 SD_CMD8_Loop: dec R23 breq SD_Init_Exit2 rcall SPI_RX cpi Work,0x05 brne SD_CMD8_Loop ldi Work,0x12 rcall Lcdlocate ldi Work,'O' rcall LcdSendData ldi Work,'K' rcall LcdSendData ldi R23,8 rcall CMD58_Send SD_CMD58_Loop: dec R23 breq SD_Init_Exit3 rcall SPI_RX cpi Work,0x01 brne SD_CMD58_Loop ldi Work,0x14 rcall Lcdlocate ldi Work,'O' rcall LcdSendData ldi Work,'K' rcall LcdSendData SD_CMD1_: ldi R23,255 rcall CMD1_Send SD_CMD1_Loop: dec R23 breq SD_Init_Exit4 rcall SPI_RX cpi Work,0x00 brne SD_CMD1_Loop ldi Work,0x16 rcall Lcdlocate ldi Work,'O' rcall LcdSendData ldi Work,'K' rcall LcdSendData pop R23 sbi PortaSD,CS ret SD_Init_Exit: pop R23 ldi Work,0x10 rcall Lcdlocate ldi Work,'E' rcall LcdSendData ldi Work,'R' rcall LcdSendData ret SD_Init_Exit2: pop R23 ldi Work,0x12 rcall Lcdlocate ldi Work,'E' rcall LcdSendData ldi Work,'R' rcall LcdSendData ret SD_Init_Exit3: pop R23 ldi Work,0x14 rcall Lcdlocate ldi Work,'E' rcall LcdSendData ldi Work,'R' rcall LcdSendData ret SD_Init_Exit4: pop R23 ldi Work,0x16 rcall Lcdlocate ldi Work,'E' rcall LcdSendData ldi Work,'R' rcall LcdSendData ret ;******************************************* ; Invio del CMD0 **** SD ;******************************************* CMD0_Send: cbi PortaSD,CS ; Seleziona CS (Attivo Basso) ldi zh,high(CMD0DATA<<1) ; send cmd9 ldi zl,low(CMD0DATA<<1) rcall Send_SD_Cmd ret ;******************************************* ; Invio del CMD0 **** SD ;******************************************* CMD1_Send: cbi PortaSD,CS ; Seleziona CS (Attivo Basso) ldi zh,high(CMD1DATA<<1) ; send cmd9 ldi zl,low(CMD1DATA<<1) rcall Send_SD_Cmd ret ;******************************************* ; Invio del CMD8 **** SD ;******************************************* CMD8_Send: cbi PortaSD,CS ; Seleziona CS (Attivo Basso) ldi zh,high(CMD8DATA<<1) ; send cmd9 ldi zl,low(CMD8DATA<<1) rcall Send_SD_Cmd ret ;******************************************* ; Invio del CMD58 **** SD ;******************************************* CMD58_Send: cbi PortaSD,CS ; Seleziona CS (Attivo Basso) ldi zh,high(CMD58DATA<<1) ; send cmd9 ldi zl,low(CMD58DATA<<1) rcall Send_SD_Cmd ret ;******************************************* ; Invio del ACMD41 **** SD ;******************************************* ACMD41_Send: cbi PortaSD,CS ; Seleziona CS (Attivo Basso) ldi zh,high(CMD55DATA<<1) ; send cmd9 ldi zl,low(CMD55DATA<<1) rcall Send_SD_Cmd ldi zh,high(CMD41DATA<<1) ; send cmd9 ldi zl,low(CMD41DATA<<1) rcall Send_SD_Cmd ret ;******************************************* ; Invio del Comando **** scheda SD su SPI ; ; Z - punta ai 6 byte del comando ;******************************************* Send_SD_Cmd: push Work push r18 ldi r18,6 ; command frames are 6 bytes SSC_Loop: lpm Work,z+ ; send them out rcall SPI_TX dec r18 ; repeat until done brne SSC_Loop pop r18 pop Work ret[/CODE]