你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【STM32f769I-DISC SDIO试验】

[复制链接]
andeyqi 发布时间:2018-8-21 17:18
本帖最后由 andeyqi 于 2018-8-22 11:04 编辑

官方的软件包已经包含了SD卡的驱动
STM32Cube_FW_F7_V1.9.0\Drivers\BSP\STM32F769I-Discovery\stm32f769i_discovery_sd.c

现对主要的函数理解以加深对SDIO的认识,为以后的使用积累经验。
sdcard.PNG
根据电路图可知硬件连接上三个数据线,一根命令传输线,一根时钟线,一根SD卡插入检测线。


  1. SD_HandleTypeDef uSdHandle;
  2. /**
  3.   * @brief  Initializes the SD card device.
  4.   * @retval SD status
  5.   */
  6. uint8_t BSP_SD_Init(void)
  7. {
  8.   uint8_t sd_state = MSD_OK;

  9.   /* PLLSAI is dedicated to LCD periph. Do not use it to get 48MHz*/

  10.   /* uSD device interface configuration */
  11.     uSdHandle.Instance = SDMMC2;
  12.     uSdHandle.Init.ClockEdge           = SDMMC_CLOCK_EDGE_RISING;
  13.     uSdHandle.Init.ClockBypass         = SDMMC_CLOCK_BYPASS_DISABLE;
  14.     uSdHandle.Init.ClockPowerSave      = SDMMC_CLOCK_POWER_SAVE_DISABLE;
  15.     uSdHandle.Init.BusWide             = SDMMC_BUS_WIDE_1B;
  16.     uSdHandle.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
  17.     uSdHandle.Init.ClockDiv            = SDMMC_TRANSFER_CLK_DIV;

  18.   /* Msp SD Detect pin initialization */
  19.   BSP_SD_Detect_MspInit(&uSdHandle, NULL);
  20.   if(BSP_SD_IsDetected() != SD_PRESENT)   /* Check if SD card is present */
  21.   {
  22.     return MSD_ERROR_SD_NOT_PRESENT;
  23.   }

  24.   /* Msp SD initialization */
  25.   BSP_SD_MspInit(&uSdHandle, NULL);

  26.   /* HAL SD initialization */
  27.   if(HAL_SD_Init(&uSdHandle) != HAL_OK)
  28.   {
  29.     sd_state = MSD_ERROR;
  30.   }

  31.   /* Configure SD Bus width */
  32.   if(sd_state == MSD_OK)
  33.   {
  34.     /* Enable wide operation */
  35.     if(HAL_SD_ConfigWideBusOperation(&uSdHandle, SDMMC_BUS_WIDE_4B) != HAL_OK)
  36.     {
  37.       sd_state = MSD_ERROR;
  38.     }
  39.     else
  40.     {
  41.       sd_state = MSD_OK;
  42.     }
  43.   }
  44.   return  sd_state;
  45. }
复制代码

代码初始化时设置数据位宽为SDMMC_BUS_WIDE_1B,之后又设置为SDMMC_BUS_WIDE_4B,为什么不一开始就设置成4位,而且硬件原理图上也是按照4位模式接的,后来在编程手册上找到如下描述,需要初始化完成之后再设置位宽度,但是还是没有想明白为什么这么设计。

By default SDMMC_D0 is used for data transfer. After initialization, the host can change the databus width.

BSP_SD_IsDetected()函数根据代码注释和内容可判断是,检测SD卡是否插入,检测到低电平是代表SD卡已插入。
  1. /**
  2. * @brief  Detects if SD card is correctly plugged in the memory slot or not.
  3. * @retval Returns if SD is detected or not
  4. */
  5. uint8_t BSP_SD_IsDetected(void)
  6. {
  7.   __IO uint8_t  status = SD_PRESENT;

  8.   /* Check SD card detect pin */
  9.   if (HAL_GPIO_ReadPin(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) == GPIO_PIN_SET)
  10.   {
  11.     status = SD_NOT_PRESENT;
  12.   }

  13.     return status;
  14. }
复制代码

  BSP_SD_Detect_MspInit(&uSdHandle, NULL); 配置sd卡插入检测引脚为输入上拉模式
  1. /**
  2.   * @brief  Initializes the SD Detect pin MSP.
  3.   * @param  hsd: SD handle
  4.   * @param  Params : pointer on additional configuration parameters, can be NULL.  
  5.   * @retval None
  6.   */
  7. __weak void BSP_SD_Detect_MspInit(SD_HandleTypeDef *hsd, void *Params)
  8. {
  9.   GPIO_InitTypeDef  gpio_init_structure;

  10.   SD_DETECT_GPIO_CLK_ENABLE();

  11.   /* GPIO configuration in input for uSD_Detect signal */
  12.   gpio_init_structure.Pin       = SD_DETECT_PIN;
  13.   gpio_init_structure.Mode      = GPIO_MODE_INPUT;
  14.   gpio_init_structure.Pull      = GPIO_PULLUP;
  15.   gpio_init_structure.Speed     = GPIO_SPEED_HIGH;
  16.   HAL_GPIO_Init(SD_DETECT_GPIO_PORT, &gpio_init_structure);
  17. }
复制代码
  1. /**
  2.   * @brief  Initializes the SD Card.
  3.   * @param  hsd Pointer to SD handle
  4.   * @note   This function initializes the SD card. It could be used when a card
  5.             re-initialization is needed.
  6.   * @retval HAL status
  7.   */
  8. HAL_StatusTypeDef HAL_SD_InitCard(SD_HandleTypeDef *hsd)
  9. {
  10.   uint32_t errorstate = HAL_SD_ERROR_NONE;
  11.   SD_InitTypeDef Init;
  12.   
  13.   /* Default SDMMC peripheral configuration for SD card initialization */
  14.   Init.ClockEdge           = SDMMC_CLOCK_EDGE_RISING;
  15.   Init.ClockBypass         = SDMMC_CLOCK_BYPASS_DISABLE;
  16.   Init.ClockPowerSave      = SDMMC_CLOCK_POWER_SAVE_DISABLE;
  17.   Init.BusWide             = SDMMC_BUS_WIDE_1B;
  18.   Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
  19.   Init.ClockDiv            = SDMMC_INIT_CLK_DIV;

  20.   /* Initialize SDMMC peripheral interface with default configuration */
  21.   SDMMC_Init(hsd->Instance, Init);

  22.   /* Disable SDMMC Clock */
  23.   __HAL_SD_DISABLE(hsd);
  24.   
  25.   /* Set Power State to ON */
  26.   SDMMC_PowerState_ON(hsd->Instance);
  27.   
  28.   /* Enable SDMMC Clock */
  29.   __HAL_SD_ENABLE(hsd);
  30.   
  31.   /* Required power up waiting time before starting the SD initialization sequence */
  32.   HAL_Delay(2);
  33.   
  34.   /* Identify card operating voltage */
  35.   errorstate = SD_PowerON(hsd);
  36.   if(errorstate != HAL_SD_ERROR_NONE)
  37.   {
  38.     hsd->State = HAL_SD_STATE_READY;
  39.     hsd->ErrorCode |= errorstate;
  40.     return HAL_ERROR;
  41.   }

  42.   /* Card initialization */
  43.   errorstate = SD_InitCard(hsd);
  44.   if(errorstate != HAL_SD_ERROR_NONE)
  45.   {
  46.     hsd->State = HAL_SD_STATE_READY;
  47.     hsd->ErrorCode |= errorstate;
  48.     return HAL_ERROR;
  49.   }

  50.   return HAL_OK;
  51. }
复制代码



收藏 评论3 发布时间:2018-8-21 17:18

举报

3个回答
andeyqi 回答时间:2018-8-22 09:22:05
呃呃呃  没编辑完  竟然发出去了  有时间慢慢写吧
zero99 回答时间:2018-8-22 10:15:37
MrJiu 回答时间:2018-8-22 10:43:47
支持!!!

所属标签

STM32团队

意法半导体微控制器和微处理器拥有广泛的产品线,包含低成本的8位单片机和基于ARM® Cortex®-M0、M0+、M3、M4、M33、M7及A7内核并具备丰富外设选择的32位微控制器及微处理器


最新内容

相似分享

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版