打印

请教!!!STM32F107VCT6 调试SPI模式SD卡初始化失败

[复制链接]
3983|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
cxy0629|  楼主 | 2011-4-24 10:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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;
}
已经弄了好长时间了,总是失败,也换过两三张卡都不行,很崩溃啊。。。还请大家帮帮忙看看,不胜感激!
沙发
dumachao| | 2012-7-19 18:16 | 只看该作者
请问你调试成功了吗

使用特权

评论回复
板凳
tao0127| | 2012-7-20 18:34 | 只看该作者
呵呵,2L真体贴。。

使用特权

评论回复
地板
8fu8| | 2013-11-25 20:16 | 只看该作者
你好,我也遇到了你一样的问题,请问解决了吗?

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

35

帖子

1

粉丝