1、硬件图
2、示例代码
根据提示配置SDCLK为72/3 = 24MHz。
static void MX_SDIO_SD_Init(void)
{
/* USER CODE BEGIN SDIO_Init 0 */
SD_InitTypeDef Init;
Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
Init.BusWide = SDIO_BUS_WIDE_1B;
Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
Init.ClockDiv = SDIO_INIT_CLK_DIV;
/* USER CODE END SDIO_Init 0 */
/* USER CODE BEGIN SDIO_Init 1 */
/* USER CODE END SDIO_Init 1 */
hsd.Instance = SDIO;
hsd.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
hsd.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
hsd.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
hsd.Init.BusWide = SDIO_BUS_WIDE_4B;
hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
hsd.Init.ClockDiv = 1;
if (HAL_SD_Init(&hsd) != HAL_OK)
{
Error_Handler();
}
SDIO_Init(hsd.Instance, Init);
if (HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SDIO_Init 2 */
/* USER CODE END SDIO_Init 2 */
}
BUG:SD_FindSCR出现死等情况。
解决:在位宽1bit切换至4Bit配置时,SDIOCLK已经由小于400KH变为24Mhz,部分SD卡是不支持的,故在初始化函数中添加 SDIO_Init(hsd.Instance, Init)降低SDIO的基准时钟,配置为4bit位宽以后,可重新调整为24Mhz。
配置完成后可在while大循环之前读取SD卡的一个block内容。
HAL_SD_ReadBlocks(&hsd, SDInfo, 0, 1, 0xffff);