Skip to main content
Graduate
August 22, 2025
Question

STM32F411 FatFS DMA F_write issue

  • August 22, 2025
  • 1 reply
  • 320 views

Hello, 

I have implemented a sdcard socket on my board based on the STM32F411, 

I use SDIO & fatfs r0.15c p2, 

The SDIO DMA NVIC configuration is : 

 
 /* DMA2_Stream3_IRQn interrupt configuration */
 HAL_NVIC_SetPriority(DMA2_Stream3_IRQn, 6, 0);
 HAL_NVIC_EnableIRQ(DMA2_Stream3_IRQn);
 /* DMA2_Stream6_IRQn interrupt configuration */
 HAL_NVIC_SetPriority(DMA2_Stream6_IRQn, 0, 0);
 HAL_NVIC_EnableIRQ(DMA2_Stream6_IRQn);

DMA 

/* SDIO DMA Init */
 /* SDIO_RX Init */
 hdma_sdio_rx.Instance = DMA2_Stream3;
 hdma_sdio_rx.Init.Channel = DMA_CHANNEL_4;
 hdma_sdio_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
 hdma_sdio_rx.Init.PeriphInc = DMA_PINC_DISABLE;
 hdma_sdio_rx.Init.MemInc = DMA_MINC_ENABLE;
 hdma_sdio_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
 hdma_sdio_rx.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
 hdma_sdio_rx.Init.Mode = DMA_PFCTRL;
 hdma_sdio_rx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
 hdma_sdio_rx.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
 hdma_sdio_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
 hdma_sdio_rx.Init.MemBurst = DMA_MBURST_INC4;
 hdma_sdio_rx.Init.PeriphBurst = DMA_PBURST_INC4;
 if (HAL_DMA_Init(&hdma_sdio_rx) != HAL_OK)
 {
 Error_Handler();
 }

 __HAL_LINKDMA(hsd,hdmarx,hdma_sdio_rx);

 /* SDIO_TX Init */
 hdma_sdio_tx.Instance = DMA2_Stream6;
 hdma_sdio_tx.Init.Channel = DMA_CHANNEL_4;
 hdma_sdio_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
 hdma_sdio_tx.Init.PeriphInc = DMA_PINC_DISABLE;
 hdma_sdio_tx.Init.MemInc = DMA_MINC_ENABLE;
 hdma_sdio_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
 hdma_sdio_tx.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
 hdma_sdio_tx.Init.Mode = DMA_PFCTRL;
 hdma_sdio_tx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
 hdma_sdio_tx.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
 hdma_sdio_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
 hdma_sdio_tx.Init.MemBurst = DMA_MBURST_INC4;
 hdma_sdio_tx.Init.PeriphBurst = DMA_PBURST_INC4;
 if (HAL_DMA_Init(&hdma_sdio_tx) != HAL_OK)
 {
 Error_Handler();
 }

 __HAL_LINKDMA(hsd,hdmatx,hdma_sdio_tx);

My PLL configuration 

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
 RCC_OscInitStruct.PLL.PLLM = 25;
 RCC_OscInitStruct.PLL.PLLN = 384;
 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
 RCC_OscInitStruct.PLL.PLLQ = 8;

SDIO 

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_1B;
 hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
 hsd.Init.ClockDiv = 1;
 /* USER CODE BEGIN SDIO_Init 2 */
 //hsd.Init.BusWide = SDIO_BUS_WIDE_1B;

 if (HAL_SD_Init(&hsd) != HAL_OK){
 log_error("MX_SDIO_SD_Init: error HAL_SD_Init code:%d",hsd.ErrorCode);
 }

 if (HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B) != HAL_OK){
 log_error("MX_SDIO_SD_Init: HAL_SD_ConfigWideBusOperation error code:%d",hsd.ErrorCode);
 }

 

 

 

With some SD Card that are not HC, but V10 I have f_write issue with fatfs return code 1 

FR_DISK_ERR
 
Code used: 
 const char string[512];
 sprintf(string,"HELLO HERE");
 unsigned int pt;
 FILINFO fno;

 /* Open or create a log file and ready to append */
 printf("mount:%d\n",f_mount(&fs, "", 1));
 printf("open: %d\n", f_open(&file, "log.txt", FA_WRITE | FA_OPEN_ALWAYS ));
 printf("stat: %d\n", f_stat("log3.txt", &fno));
 
 printf("write: %d wr: %d\n", f_write(&file, string, 512, &pt), pt);
 
 printf("flush: %d\n", f_sync(&file));
 printf("close: %d\n", f_close(&file)); /* close performs sync before close */
 
 
When I use SD Card HD I do not have any issue. 
 
I tried to lower the SDIO_CLK, increase the write block, to disable irq... but nothing seems to work 
 
all writing functions are failing like mkfs.
 
f_write is failing with 
if (clst == 0xFFFFFFFF) ABORT(fs, FR_DISK_ERR);
				fp->clust = clst;			/* Update current cluster */
 
Is there a way to get regular sd card to work ? 
 
I need a bit of help here please :)
 
Thanks 
Vincent
 
 
 

 

 

 

    This topic has been closed for replies.

    1 reply

    Super User
    August 22, 2025

    Hi,

    what format the cards have ?  same ?

    Did you enable exfat in fatfs ?

     

    vbessonAuthor
    Graduate
    August 22, 2025

    The SDCard is formatted using the same format type between the 2 SD Card => Fat32

    Exfat is not enable...

    Vincent