- SDIO基础知识链接:https://blog.csdn.net/Little520Stone/article/details/105566883
- SDIO轮询方式读写数据:https://blog.csdn.net/Little520Stone/article/details/105961940
SDIO DMA方式
一、硬件及软件版本
- MCU型号:STM32F103ZETX
- 软件版本:STM32 Cube MX 5.4.0
- 固件版本:STM32Cube FW_F1 V1.8.0
二、STM32 Cube Mx 配置
1、配置RCC。使用的开发板外接了8MHz的晶振。
2、配置烧录。烧录使用J-link SWD的方式
3、配置SDIO。
- 注意:初次配置建议将SDIOCLK clock divide factor 设置大一些,如果使用的HCLK比较大,而SDIOCLK clock divide factor设置太小,则有可能超过SD卡支持的频率。
4、配置USART。
5、配置时钟
6、生成程序。
三、修改生成的程序
1、修改SDIO_INIT_CLK_DIV的值为0xB2。
- SD卡初始化不能超过400KHz,72MHz/(0xB2 + 2) = 400KHz.
2、在main函数前增加变量。
/* USER CODE BEGIN PTD */
#define WRITE_READ_ADDR 0x00000000
#define NUMBER_OF_BLOCKS 1
#define BLOCK_SIZE 512
/* USER CODE END PTD */
/* USER CODE BEGIN 0 */
HAL_StatusTypeDef Return_Status;
HAL_SD_CardStateTypeDef SD_Card_Status;
HAL_DMA_StateTypeDef DMA_Status;
uint8_t Buffer_Tx[512];
uint8_t Buffer_Rx[512];
/* USER CODE END 0 */
3、在main.c函数增加测试读写程序。
/* USER CODE BEGIN 2 */
printf(" Warning: this program may erase all the TF card data. \r\n");
printf("\r\n Initialize SD card successfully!\r\n\r\n");
printf(" SD card information! \r\n");
printf(" CardCapacity : %llu \r\n",((unsigned long long)hsd.SdCard.BlockSize*hsd.SdCard.BlockNbr));
printf(" CardBlockSize : %d \r\n",hsd.SdCard.BlockSize);
printf(" RCA : %d \r\n",hsd.SdCard.RelCardAdd);
printf(" CardType : %d \r\n",hsd.SdCard.CardType);
Return_Status = HAL_SD_Erase(&hsd,WRITE_READ_ADDR,WRITE_READ_ADDR+NUMBER_OF_BLOCKS*4);
printf("erase status :%d\r\n",Return_Status);
Return_Status=SDIO_ReadBlocks_DMA(&hsd,Buffer_Rx, WRITE_READ_ADDR, NUMBER_OF_BLOCKS);
printf("read status :%d\r\n",Return_Status);
for( temp = 0;temp < 512 ;temp++ )
{
printf("%4d",Buffer_Rx[temp]);
}
memset(Buffer_Tx,55,sizeof(Buffer_Tx)-1);
SDIO_WriteBlocks_DMA(&hsd,Buffer_Tx, WRITE_READ_ADDR, NUMBER_OF_BLOCKS);
printf("write status :%d\r\n",Return_Status);
Return_Status=SDIO_ReadBlocks_DMA(&hsd,Buffer_Rx, WRITE_READ_ADDR, NUMBER_OF_BLOCKS);
printf("read status :%d\r\n",Return_Status);
for( temp = 0;temp < 512 ;temp++ )
{
printf("%4d",Buffer_Rx[temp]);
}
/* USER CODE END 2 */
4、编写读写DMA程序。
HAL_StatusTypeDef SDIO_ReadBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
{
HAL_StatusTypeDef Return_Status;
HAL_SD_CardStateTypeDef SD_Card_Status;
do
{
SD_Card_Status = HAL_SD_GetCardState(hsd);
}while(SD_Card_Status != HAL_SD_CARD_TRANSFER );
/* SDIO DMA DeInit */
/* SDIO DeInit */
HAL_DMA_DeInit(&hdma_sdio);
/* SDIO DMA Init */
/* SDIO Init */
hdma_sdio.Instance = DMA2_Channel4;
hdma_sdio.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_sdio.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_sdio.Init.MemInc = DMA_MINC_ENABLE;
hdma_sdio.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_sdio.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma_sdio.Init.Mode = DMA_NORMAL;
hdma_sdio.Init.Priority = DMA_PRIORITY_LOW;
if (HAL_DMA_Init(&hdma_sdio) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA( hsd,hdmarx,hdma_sdio);
Return_Status = HAL_SD_ReadBlocks_DMA( hsd,pData, BlockAdd, NumberOfBlocks);
return Return_Status;
}
HAL_StatusTypeDef SDIO_WriteBlocks_DMA(SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
{
HAL_StatusTypeDef Return_Status;
HAL_SD_CardStateTypeDef SD_Card_Status;
do
{
SD_Card_Status = HAL_SD_GetCardState(hsd);
}while(SD_Card_Status != HAL_SD_CARD_TRANSFER );
/* SDIO DMA DeInit */
/* SDIO DeInit */
HAL_DMA_DeInit(&hdma_sdio);
/* SDIO DMA Init */
/* SDIO Init */
hdma_sdio.Instance = DMA2_Channel4;
hdma_sdio.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_sdio.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_sdio.Init.MemInc = DMA_MINC_ENABLE;
hdma_sdio.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_sdio.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma_sdio.Init.Mode = DMA_NORMAL;
hdma_sdio.Init.Priority = DMA_PRIORITY_LOW;
if (HAL_DMA_Init(&hdma_sdio) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(hsd,hdmatx,hdma_sdio);
Return_Status = HAL_SD_WriteBlocks_DMA(hsd,pData, BlockAdd, NumberOfBlocks);
return Return_Status;
}
注意:STM32F103的SDIO DMA每次由读数据变为写数据或者由写数据变为读数据时,都需要重新初始化DMA(主要是为了更改数据传输的方向)。
5、烧录测试。
6、源文件
https://download.csdn.net/download/Little520Stone/12605528