cancel
Showing results for 
Search instead for 
Did you mean: 

sdcard: f_mount returns FR_NOT_READY in CubeMX-generated project

Alex Farber
Associate III
Posted on June 17, 2018 at 08:44

Environment:

    STM32469I-EVAL

    Windows 10 x64

    STM32CubeMX v. 4.25.0

    STM32Cube FW_F4 V1.21.0

    System Workbench for STM32 Version: 1.15.0.201708311556

SD card inserted, code sample Projects\STM32469I_EVAL\Applications\FatFs\FatFs_uSD_RTOS is working.

 

Steps to reproduce the problem:

 

CubeMX, create new STM32469I-EVAL project.

Pinout.

    SDIO: SD 1 bit

    SYS, Timebase Source: TIM1

    FATFS: SD card

    FreeRTOS: enabled

Configuration.

   FREERTOS, default task stack size 1024

 

Generate SW4STM32 source code in CubeMX. Open generated project in STM32 Workbench and make the following changes in the code:

 

void _Error_Handler(char *file, int line)

{

  /* USER CODE BEGIN Error_Handler_Debug */

    HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, GPIO_PIN_RESET);

  while(1)

  {

  }

  /* USER CODE END Error_Handler_Debug */

}

 

void StartDefaultTask(void const * argument)

{

    /* init code for FATFS */

    MX_FATFS_Init();

 

    /* USER CODE BEGIN 5 */

 

    // Turn off all LEDs

    HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);

    HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET);

    HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, GPIO_PIN_SET);

    HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, GPIO_PIN_SET);

 

    // Check result of MX_FATFS_Init

    if (retSD != 0)

    {

        _Error_Handler(__FILE__, __LINE__);

    }

 

    FRESULT fresult;

    FATFS SDFatFs;

 

    fresult = f_mount(&SDFatFs, (TCHAR const*)SDPath, 1);   // runs several seconds, returns 3

 

    // Checked in the debugger:

    // resSD = 0, SDPATH = '0:/', fresut = 3 (FR_NOT_READY)

 

    if( fresult != FR_OK )

    {

        _Error_Handler(__FILE__, __LINE__);

    }

 

    HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);

 

    for(;;)

    {

      osDelay(1);

    }

    /* USER CODE END 5 */

}

f_mount returns FR_NOT_READY. The same code was working in one of previous STM32CubeMX and firmware package versions.

 

ioc project and main.c files are attached.

#fatfs #sdcard #sdio #stm32469i-eval
8 REPLIES 8
Posted on June 17, 2018 at 11:51

Make sure the stack is big enough, and that the local/auto allocation is cleared

FATFS SDFatFs = {0};

Sorry, don't have a personal interest in debugging broken CubeMX output.

Your options here are to instrument the DISKIO and SDIO layers, and make sure the IRQ handlers are named and function properly.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 17, 2018 at 12:24

Thanks. Increasing the stack size to 2048 and initializing

SDFatFs

to 0 didn't help, still the same result.
Alex Farber
Associate III
Posted on June 18, 2018 at 11:51

After some investigation I found two functions in sd_diskio.c: BSP_SD_WriteCpltCallback and BSP_SD_ReadCpltCallback. They are never called, so all SD card calls fail with timeout. Solution: add the following code to the end of sd_diskio.c

/* USER CODE BEGIN lastSection */

void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)

{

    osMessagePut(SDQueueID, READ_CPLT_MSG, osWaitForever);

}

void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)

{

    osMessagePut(SDQueueID, WRITE_CPLT_MSG, osWaitForever);

}

/* USER CODE END lastSection */

This solution may be applied to CubeMX version 4.25.0, firmware package V1.21.0.

CubeMX, SDIO: global interrupt enabled, RX and TX channels are added to DMA.

Update: Installed CubeMX 4.26.0, finally no bugs in SD card support, so all tricks from this and may other posts are not needed.

Adam Pyka
Associate

Hello,

I get nearly the same problem with SD card - f_mount() returns FR_NOT_READY.

I created new project using Nucleo f401re with external card slot. The D0..D3 data lines and CMD line are pull-uped internally, clock for SD is 24MHz, DMA enabled, IRQ enabled, no RTOS.

The trick from above does not resolve the issue, any ideas? I also followed the tutorial: http://mkwsolutions.com/Blog/STM32F4_SDIO still without success.

Software: CubeMX version 5.0.1 (freshest), Atollic TrueStudio vestion 9.2.0 (freshest).

Best,

Adam

Should probably trace back to where the not ready is coming from. Check if it expects a Card Detect signal to be connected to the socket.

Review socket and wiring.

Validate HW with known working SW.

Instrument SW to understand what it is doing, and how it is failing.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
GrantW
Associate II

Same problem with CUBEMX 5.1.0. The failure is occuring trying to read the boot sector. Perhaps there is no boot sector on the sd card? I don't know how to inspect the format of the drive. I formatted it on Win 10 as exFAT.

ooztu.1139
Associate II

Could you find our why reading the boot sector fails?

I have the exact same problem. Everything is fine until reading the boot sector fails.