cancel
Showing results for 
Search instead for 
Did you mean: 

DMA problem - SD Card with FatFs and MSD USB

niemiecarkadiusz
Associate
Posted on May 04, 2016 at 19:29

I am implementing FatFs and MSD on SD Card using HAL libraries from STM32CubeMX.Everything is working perfectly but only if I not set up a DMA for SD card. It get stuck in while loop:

/**
* @brief SD DMA transfer complete Tx callback.
* @param hdma: pointer to a DMA_HandleTypeDef structure that contains
* the configuration information for the specified DMA module.
* @retval None
*/
static
void
SD_DMA_TxCplt(DMA_HandleTypeDef *hdma)
{
SD_HandleTypeDef *hsd = (SD_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
/* DMA transfer is complete */
hsd->DmaTransferCplt = 1;
/* Wait until SD transfer is complete */
while
(hsd->SdTransferCplt == 0)
{
}
/* Disable the DMA channel */
HAL_DMA_Abort(hdma);
/* Transfer complete user callback */
HAL_SD_DMA_TxCpltCallback(hsd->hdmatx); 
}

To make it work with FatFs all I do is changing read/write functions to:

/**
* @brief Reads Sector(s)
* @param lun : not used
* @param *buff: Data buffer to store read data
* @param sector: Sector address (LBA)
* @param count: Number of sectors to read (1..128)
* @retval DRESULT: Operation result
*/
DRESULT SD_read(BYTE lun, BYTE *buff, DWORD sector, UINT count)
{
DRESULT res = RES_OK;
if
(BSP_SD_ReadBlocks_DMA((uint32_t*)buff, 
(uint64_t) (sector * BLOCK_SIZE), 
BLOCK_SIZE, 
count) != MSD_OK)
{
res = RES_ERROR;
}
return
res;
}
/**
* @brief Writes Sector(s)
* @param lun : not used
* @param *buff: Data to be written
* @param sector: Sector address (LBA)
* @param count: Number of sectors to write (1..128)
* @retval DRESULT: Operation result
*/
#if _USE_WRITE == 1
DRESULT SD_write(BYTE lun, 
const
BYTE *buff, DWORD sector, UINT count)
{
DRESULT res = RES_OK;
if
(BSP_SD_WriteBlocks_DMA((uint32_t*)buff, 
(uint64_t)(sector * BLOCK_SIZE), 
BLOCK_SIZE, count) != MSD_OK)
{
res = RES_ERROR;
}
return
res;
}

DMA settings:

void
HAL_SD_MspInit(SD_HandleTypeDef* hsd)
{
GPIO_InitTypeDef GPIO_InitStruct;
if
(hsd->Instance==SDMMC1)
{
/* USER CODE BEGIN SDMMC1_MspInit 0 */
/* USER CODE END SDMMC1_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_SDMMC1_CLK_ENABLE();
/**SDMMC1 GPIO Configuration 
PC8 ------> SDMMC1_D0
PC9 ------> SDMMC1_D1
PC10 ------> SDMMC1_D2
PC11 ------> SDMMC1_D3
PC12 ------> SDMMC1_CK
PD2 ------> SDMMC1_CMD 
*/
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11 
|GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF12_SDMMC1;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF12_SDMMC1;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/* Peripheral DMA init*/
hdma_sdmmc1_rx.Instance = DMA2_Channel4;
hdma_sdmmc1_rx.Init.Request = DMA_REQUEST_7;
hdma_sdmmc1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_sdmmc1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_sdmmc1_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_sdmmc1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_sdmmc1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma_sdmmc1_rx.Init.Mode = DMA_NORMAL;
hdma_sdmmc1_rx.Init.Priority = DMA_PRIORITY_LOW;
HAL_DMA_Init(&hdma_sdmmc1_rx);
__HAL_LINKDMA(hsd,hdmarx,hdma_sdmmc1_rx);
hdma_sdmmc1_tx.Instance = DMA2_Channel5;
hdma_sdmmc1_tx.Init.Request = DMA_REQUEST_7;
hdma_sdmmc1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_sdmmc1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_sdmmc1_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_sdmmc1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_sdmmc1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma_sdmmc1_tx.Init.Mode = DMA_NORMAL;
hdma_sdmmc1_tx.Init.Priority = DMA_PRIORITY_LOW;
HAL_DMA_Init(&hdma_sdmmc1_tx);
__HAL_LINKDMA(hsd,hdmatx,hdma_sdmmc1_tx);
/* USER CODE BEGIN SDMMC1_MspInit 1 */
/* USER CODE END SDMMC1_MspInit 1 */
}
}

What's wrong with it? To be clear: everything works perfect if I use non-DMA functions. #!stm32-!cubemx
2 REPLIES 2
niemiecarkadiusz
Associate
Posted on May 05, 2016 at 13:35

I made the DMA work with FatFS by chaning settigs from 2 channels (for TX and RX) to 1 channel for SDMMC1. Now it crashes in debug mode when executing MX_USB_DEVICE_Init().

If I don't use DMA with FatFs then USB initialize correctly but code timeouts in the HAL_SD_CheckRead/WriteOperation() function.

What's going on?

Posted on April 20, 2017 at 13:28

Hi niemiec.arkadiusz

Did you solve this problem? I am having exactly the same problem. Can you tell me how you fixed it, please.

Thanks,

Geoff