[STM32F0] STM32F030读SD卡,快来看看要不沉贴了

[复制链接]
4047|4
 楼主| z32437089 发表于 2014-10-13 19:31 | 显示全部楼层 |阅读模式
本帖最后由 z32437089 于 2014-10-31 15:38 编辑

使用SPI2驱动SD卡,发送CMD0命令,SD卡没任何反应,就一直卡到这里了,换V1和V2的卡都没反应,MISO引脚永远是低电平.

这个是SD卡硬件连接图

SD硬件

SD硬件


CDMO对应MCU_MOSI第28个腿PB15,CDMI对应MCU_MISO第27个腿PB14
  1. void SD_SendCmd (                /* Return value: R1 resp (bit7==1:Failed to send) */
  2.         BYTE cmd,                /* Command index */
  3.         DWORD arg,                /* Argument */
  4.         BYTE crc
  5. )
  6. {
  7. //        BYTE n, res;
  8.         /* Send command packet */
  9.         xchg_spi(0x40 | cmd);                                /* Start + command index */
  10.         xchg_spi((BYTE)(arg >> 24));                /* Argument[31..24] */
  11.         xchg_spi((BYTE)(arg >> 16));                /* Argument[23..16] */
  12.         xchg_spi((BYTE)(arg >> 8));                        /* Argument[15..8] */
  13.         xchg_spi((BYTE)arg);                                /* Argument[7..0] */
  14.         xchg_spi(crc);}
  1. SD_Error SD_GetResponse(uint8_t Response)
  2. {
  3.   uint32_t Count = 0xFFF;

  4.   /*!< Check if response is got or a timeout is happen */
  5.   while ((xchg_spi(0xFF) != Response) && Count)
  6.   {
  7.     Count--;
  8.   }
  9.   if (Count == 0)
  10.   {
  11.     /*!< After time out */
  12.     return SD_RESPONSE_FAILURE;
  13.   }
  14.   else
  15.   {
  16.     /*!< Right response got */
  17.     return SD_RESPONSE_NO_ERROR;
  18.   }
  19. }
  1. SD_Error SD_Init (void)
  2. {
  3.         uint32_t i = 0;
  4.         GPIO_InitTypeDef GPIO_InitStructure;
  5.         SPI_InitTypeDef SPI_InitStructure;

  6.         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);
  7.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);

  8.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 ;
  9.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  10.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
  11.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  12.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  13.         GPIO_Init(GPIOB, &GPIO_InitStructure);

  14.         GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_0); //SCK
  15.         GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_0); //MI
  16.         GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_0); //MO

  17.                 /* SPI CS PIN Configuration --------------------------------------------------*/
  18.                 /* Configure I/O for Flash Chip select */
  19.                 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  20.                 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  21.                 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  22.                 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  23.                 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  24.                 GPIO_Init(GPIOB, &GPIO_InitStructure);

  25.                 /* SPI configuration */
  26.                 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  27.                 SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  28.                 SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  29.                 SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  30.                 SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  31.                 SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  32.                 SPI_InitStructure.SPI_BaudRatePrescaler = SPIx_SD_BAUDRATE_SLOW; // 48000kHz/128=375kHz < 400kHz
  33.                 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  34.                 SPI_InitStructure.SPI_CRCPolynomial = 7;

  35.                 SPI_Init(SPI2, &SPI_InitStructure);
  36.                 SPI_RxFIFOThresholdConfig(SPI2, SPI_RxFIFOThreshold_QF);
  37.                 SPI_CalculateCRC(SPI2, DISABLE);
  38.                 SPI_Cmd(SPI2, ENABLE);                /* Enable SPI function */

  39.         CS_HIGH();                        /* Set CS# high */

  40.           /*!< Send dummy byte 0xFF, 10 times with CS high */
  41.           /*!< Rise CS and MOSI for 80 clocks cycles */
  42.           for (i = 0; i < 10; i++)
  43.           {
  44.                   xchg_spi(0xff);
  45.           }
  46.           /*-------Put SD in SPI mode----- 设置SD为SPI模式 -----*/
  47.           /*!< SD initialized and set to SPI mode properly */
  48.           return (SD_GoIdleState());
  49. }
  50. SD_Error SD_GoIdleState(void)
  51. {
  52.         /*!< SD chip select low */
  53.   CS_LOW();

  54.   /*!< Send CMD0 (SD_CMD_GO_IDLE_STATE) to put SD in SPI mode */
  55. do  {SD_SendCmd(SD_CMD_GO_IDLE_STATE, 0, 0x95);}
  56. while(SD_GetResponse(SD_IN_IDLE_STATE));
  57. led1_on;while(1);//can't reach here,led1 remains off

  58.   /*----------Activates the card initialization process-----------*/
  59.   do
  60.   {
  61.     /*!< SD chip select high */
  62.     CS_HIGH();
  63.     /*!< Send Dummy byte 0xFF */
  64.     xchg_spi(0xff);
  65.     /*!< SD chip select low */
  66.     CS_LOW();
  67.     /*!< Send CMD1 (Activates the card process) until response equal to 0x0 */
  68.     SD_SendCmd(SD_CMD_SEND_OP_COND, 0, 0xFF);
  69.     /*!< Wait for no error Response (R1 Format) equal to 0x00 */
  70.   }
  71.   while (SD_GetResponse(SD_RESPONSE_NO_ERROR));
  72.   /*!< SD chip select high */
  73.   CS_HIGH();
  74.   /*!< Send dummy byte 0xFF */
  75.   xchg_spi(0xff);
  76.   return SD_RESPONSE_NO_ERROR;
  77. }
airwill 发表于 2014-10-13 20:03 | 显示全部楼层
本帖最后由 airwill 于 2014-10-14 12:38 编辑

MISO引脚永远是低电平,  我看先检查一下连接和供电方面的硬件问题再调试软件,
我较对了一下,你的硬件连接图没有问题
mmuuss586 发表于 2014-10-13 20:58 | 显示全部楼层

先把SPI部分先调好;
另外楼主和我很多朋友好像同个行业;
做LED工程,灯具方面;
 楼主| z32437089 发表于 2014-10-14 10:50 | 显示全部楼层
本帖最后由 z32437089 于 2014-10-14 21:09 编辑
airwill 发表于 2014-10-13 20:03
MISO引脚永远是低电平,  我看先检查一下连接和供电方面的硬件问题再调试软件 ...

硬件图已上传,软件也已上传
 楼主| z32437089 发表于 2014-10-14 10:52 | 显示全部楼层
mmuuss586 发表于 2014-10-13 20:58
先把SPI部分先调好;
另外楼主和我很多朋友好像同个行业;
做LED工程,灯具方面; ...

SPI部分单独调试的,各项命令没问题。
这个行业人很多
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:常州诚聚光电科技有限公司,设备灯具定制,亮化工程配套,调试,灯具,简易控制器,DMX512控制系统,立志做

8

主题

210

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部