uint8_t SDcard_init() { uint8_t return_value[5]; uint8_t error_flag, index; //Buffer ? ncs = 1; for(index = 0; index < 10; index++) { SPI_transfer(0xFF, return_value); } // Send CMD0 // error_flag = trans_CMD0(return_value); // Send CMD8 // if(error_flag == NO_ERRORS) { error_flag = trans_CMD8(return_value); } // Send CMD58 // if(error_flag == NO_ERRORS) { error_flag = trans_CMD58(return_value); if(return_value[0] == 0x01) { error_flag = WRONG_RESPONCE; } } // Send ACMD41 // if(error_flag == NO_ERRORS) { error_flag = trans_ACMD41(return_value); } // Send CMD58 // if(error_flag == NO_ERRORS) { error_flag = trans_CMD58(return_value); if(return_value[1] & 0xC0) //bit 31 and 30 are set { //High Capacity } else if(return_value[1] & 0x80) // only bit 31 is set { //Standard Capacity error_flag = WRONG_SDCARD; } else { error_flag = WRONG_RESPONCE; } } if(error_flag != NO_ERRORS) // LED4 is the error light and this should also be redundent. { LED4 = 0; } ncs = 1; // should not be needed but just in case return error_flag; }
uint8_t SDcard_init() { uint8_t return_value[5]; uint8_t error_flag, index, dat; AUXR = 0x0C; //Buffer ? ncs = 1; for(index = 0; index < 10; index++) { SPI_transfer(0xFF, return_value); } // Send CMD0 // error_flag = trans_CMD0(return_value); for (index=0;index<5;index++) { dat=return_value[index]; printf("%2.2bX ",dat); } putchar(10); putchar(13); delay_ms(100); // Send CMD8 // if(error_flag == NO_ERRORS) { error_flag = trans_CMD8(return_value); } for (index=0;index<5;index++) { dat=return_value[index]; printf("%2.2bX ",dat); } putchar(10); putchar(13); delay_ms(100); // Send CMD58 // if(error_flag == NO_ERRORS) { error_flag = trans_CMD58(return_value); if(return_value[0] != 0x01) { error_flag = WRONG_RESPONSE; LED2 = 0; } } for (index=0;index<5;index++) { dat=return_value[index]; printf("%2.2bX ",dat); } putchar(10); putchar(13); delay_ms(100); // Send ACMD41 // if(error_flag == NO_ERRORS) { error_flag = trans_ACMD41(return_value); } for (index=0;index<5;index++) { dat=return_value[index]; printf("%2.2bX ",dat); } putchar(10); putchar(13); delay_ms(100); // Send CMD58 // if(error_flag == NO_ERRORS) { error_flag = trans_CMD58(return_value); if(return_value[1] & 0xC0) //bit 31 and 30 are set { //High Capacity } else if(return_value[1] & 0x80) // only bit 31 is set { //Standard Capacity error_flag = WRONG_SDCARD; } else { error_flag = WRONG_RESPONSE; } } for (index=0;index<5;index++) { dat=return_value[index]; printf("%2.2bX ",dat); } putchar(10); putchar(13); delay_ms(100); if(error_flag != NO_ERRORS) // LED4 is the error light and this should also be redundent. { LED4 = 0; } ncs = 1; // should not be needed but just in case return error_flag; }