cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeMX SDMMC FATFS code is broken with a buffer overrun. Here is a fix

caleb
Associate III

Hello, I have configured the NUCLEO-STM32F746ZG to use the SDMMC interface. However, the generated code has a buffer overrun in it. I found a patch to the SDIO interface on the forum, so I applied it, and it works. Here is the patch.

Please update the cubemx software to fix this code, and similar code for other families and SD drivers since it seems that same bug is copied all over different families and peripherals.

Here is the patch and where I got it from:

From 2077796b6f7489f6039667359553524bff41c1ab Mon Sep 17 00:00:00 2001

From: caleb crome <caleb@crome.org>

Date: Wed, 27 Feb 2019 09:50:14 -0800

Subject: [PATCH] buffer overrun fix for SDMMC on stm32f7xx_hal_sd.c

This I found this solution here https://community.st.com/s/question/0D50X00009XkWWHSA3/stm32f407-sdiosdcard?t=1551289670001

and modified it for the stm32f7 hal.

---

 Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_sd.c | 3 +++

 1 file changed, 3 insertions(+)

diff --git a/Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_sd.c b/Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_sd.c

index 9f25343..8ba022a 100644

--- a/Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_sd.c

+++ b/Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_hal_sd.c

@@ -752,9 +752,11 @@ HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint

   config.DPSM     = SDMMC_DPSM_ENABLE;

   SDMMC_ConfigData(hsd->Instance, &config);

   

+  uint32_t datacnt=0;    //*****Fix for overrun

   /* Write block(s) in polling mode */

   while(!__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXUNDERR | SDMMC_FLAG_DCRCFAIL | SDMMC_FLAG_DTIMEOUT | SDMMC_FLAG_DATAEND))

   {

+   if (datacnt < NumberOfBlocks * BLOCKSIZE) {//*****Fix for overrun

    if(__HAL_SD_GET_FLAG(hsd, SDMMC_FLAG_TXFIFOHE))

    {

     /* Write data to SDMMC Tx FIFO */

@@ -763,6 +765,7 @@ HAL_StatusTypeDef HAL_SD_WriteBlocks(SD_HandleTypeDef *hsd, uint8_t *pData, uint

      SDMMC_WriteFIFO(hsd->Instance, (tempbuff + count));

     }

     tempbuff += 8U;

+ datacnt+=8*4;}//*****Fix for overrun

    }

    

    if((Timeout == 0U)||((HAL_GetTick()-tickstart) >= Timeout))

-- 

2.17.1

0 REPLIES 0