u8 SDC_Init(void)
{
u32 i = 0;
/* SDC chip select high */
SDC_CS_HIGH();
/* Send dummy byte 0xFF, 10 times with CS high*/
/* rise CS and MOSI for at least clocks cycles */
for (i = 0; i <= 9; i++)
{
/* Send dummy byte 0xFF,generate 80 clks*/
SDC_WriteByte(DUMMY);
}
/*------------Put SDC in SPI mode--------------*/
/* Done by the SDC_GoIdleState() */
return (SDC_GoIdleState());
}
u8 SDC_GoIdleState(void)
{
/* SDC chip select low */
SDC_CS_LOW();
/* Send CMD0 (GO_IDLE_STATE) to put SDC in SPI mode */
SDC_SendCmd(SDC_GO_IDLE_STATE, 0, 0x95);
/* Wait for In Idle State Response (R1 Format) equal to 0x01 */
if (SDC_GetResponse(SDC_IN_IDLE_STATE))
{
/* No Idle State Response: return response failue */
return SDC_RESPONSE_FAILURE;
}
/*----------Activates the card initialization process-----------*/
do
{
/* SDC chip select high */
SDC_CS_HIGH();
/* Send Dummy byte 0xFF */
SDC_WriteByte(DUMMY);
/* SDC chip select low */
SDC_CS_LOW();
/* Send CMD55 and ACMD41 (Activates the card process) until response equal to 0x0 */
SDC_SendCmd(SDC_APP_CMD,0,0xFF);
SDC_SendCmd(SDC_SEND_APP_COND_ACMD,0,0xFF);
}while(SDC_GetResponse(SDC_RESPONSE_NO_ERROR));
/* SDC chip select high */
SDC_CS_HIGH();
/* Send dummy byte 0xFF */
SDC_WriteByte(DUMMY);
return SDC_RESPONSE_NO_ERROR;
}
void SDC_SendCmd(u8 Cmd, u32 Arg, u8 Crc)
{
u32 i = 0x00;
/* A cmd consists of 6 byte each */
u8 Frame[6];
/* Construct byte1 */
Frame[0] = (Cmd | 0x40);
/* Construct byte2 */
Frame[1] = (u8)(Arg >> 24);
/* Construct byte3 */
Frame[2] = (u8)(Arg >> 16);
/* Construct byte4 */
Frame[3] = (u8)(Arg >> 8);
/* Construct byte5 */
Frame[4] = (u8)(Arg);
/* Construct CRC: byte6 */
Frame[5] = (Crc);
/* Send the Cmd bytes */
for (i = 0; i < 6; i++)
{
SDC_WriteByte(Frame);
}
}
u8 SDC_GetResponse(u8 Response)
{
u32 Count = 0xFFF;
/* Check if response is got or a timeout is happen */
while ((SDC_ReadByte() != Response) && Count)
{
Count--;
if(Count==0) break;
}
if (Count == 0)
{
/* After time out */
return SDC_RESPONSE_FAILURE;
}
else
{
/* Right response got */
return SDC_RESPONSE_NO_ERROR;
}
}
u8 SDC_ReadByte(void)
{
u8 byte_data = 0;
/* Wait until the transmit buffer is empty */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
/* Send the byte,the transmission starts by the host */
SPI_I2S_SendData(SPI1, DUMMY);
/* Wait until a data is received */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
/* Get the received data */
byte_data = SPI_I2S_ReceiveData(SPI1);初始化走到这一步就是接收不到卡给的信息,也就是COM0时收不到0x01的正确数据!硬件检查了连接是对的啊!
//USART_SendData(USART1, byte_data);
/* Return the shifted data */
return byte_data;
}
已经弄了好长时间了,总是失败,也换过两三张卡都不行,很崩溃啊。。。还请大家帮帮忙看看,不胜感激! |