STM32CubeIDE 配置 SD模块并使用DMA 如下图, 比较简单
只用了dma2-channel4,
重写两个 __weak 函数:
BSP_SD_WriteBlocks 和 BSP_SD_ReadBlocks
(自动生成的代码sd_diskio.c 中使用的是这两个函数,)
uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout)
{
uint8_t sd_state = MSD_OK;
if (HAL_SD_ReadBlocks_DMA(&hsd, (uint8_t *)pData, ReadAddr, NumOfBlocks) != HAL_OK)
{
sd_state = MSD_ERROR;
}
return sd_state;
}
uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout)
{
uint8_t sd_state = MSD_OK;
if (HAL_SD_WriteBlocks_DMA(&hsd, (uint8_t *)pData, WriteAddr, NumOfBlocks) != HAL_OK)
{
sd_state = MSD_ERROR;
}
return sd_state;
}
编译成功后 实验发现读数据正确, 写操作数据没有真正写入。
使用中断方式进行对比,读写都没有问题, 看来还是DMA 问题。
查找源码,发现DMA配置方向有些问题,读写都是一样,那写操作就不对了。
确认问题所在就好办了:
每次操作前重新配置一下DMA参数即可, 当然需要DMA反初始化再重新初始化。
uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout)
{
uint8_t sd_state = MSD_OK;
if (hdma_sdio.Init.Direction != DMA_PERIPH_TO_MEMORY)
{
HAL_DMA_DeInit(&hdma_sdio);
hdma_sdio.Init.Direction = DMA_PERIPH_TO_MEMORY;
HAL_DMA_Init(&hdma_sdio);
}
if (HAL_SD_ReadBlocks_DMA(&hsd, (uint8_t *)pData, ReadAddr, NumOfBlocks) != HAL_OK)
{
sd_state = MSD_ERROR;
}
return sd_state;
}
uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout)
{
uint8_t sd_state = MSD_OK;
if (hdma_sdio.Init.Direction != DMA_MEMORY_TO_PERIPH)
{
HAL_DMA_DeInit(&hdma_sdio);
hdma_sdio.Init.Direction = DMA_MEMORY_TO_PERIPH;
HAL_DMA_Init(&hdma_sdio);
}
if (HAL_SD_WriteBlocks_DMA(&hsd, (uint8_t *)pData, WriteAddr, NumOfBlocks) != HAL_OK)
{
sd_state = MSD_ERROR;
}
return sd_state;
}