cancel
Showing results for 
Search instead for 
Did you mean: 

f_mkfs returns fa_not_ready

Mr ZEE
Associate II

Hi all

I'm new in implementing SD-Card, however i've managed to run the example from the repository. I am using STM32429i-Eval Board with cubeMX and CubeIDE.

Question is that when I run the example code on the board it works fine but when I create a new project (code generated by cubeMX) and copy the example code in it, the function f_mkfs() returns FA_NOT_READY error. All the hardware pin configurations are according to the schematic provided by the Eval-Board. Have a look at my code please.

#include "main.h"

#include "fatfs.h"

/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/

/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/

/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/

/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

SD_HandleTypeDef hsd;

/* USER CODE BEGIN PV */

static uint8_t buffer[_MAX_SS];

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_SDIO_SD_Init(void);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**

 * @brief The application entry point.

 * @retval int

 */

int main(void)

{

 /* USER CODE BEGIN 1 */

  uint8_t wtext[] = "this is test program";

uint32_t byteswritten;

FRESULT res;

 /* USER CODE END 1 */

  

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 HAL_Init();

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */

 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_SDIO_SD_Init();

 MX_FATFS_Init();

 /* USER CODE BEGIN 2 */

 if(f_mount(&SDFatFS, (TCHAR const*)SDPath, 0) == FR_OK)

  {

   res = f_mkfs((TCHAR const*)SDPath, FM_ANY, 0, buffer, sizeof(buffer));

   if(res == FR_OK)

   {

   if(f_open(&SDFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE))

   {

   if(f_write(&SDFile, wtext, sizeof(wtext), (void *)&byteswritten) == FR_OK)

   {

   f_close(&SDFile);

   }

   }

   }

  }

  FATFS_UnLinkDriver(SDPath);

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

4 REPLIES 4
Ozone
Lead

Looking at the implementation (ff.c in the FatFS sources), it says:

FRESULT f_mkfs ( ...
...
   /* Get disk statics */
   stat = disk_initialize(drv);
   if (stat & STA_NOINIT) return FR_NOT_READY;

Seems something is not initialized properly.

I don't do Cube code, though.

Mr ZEE
Associate II

well actually that is the main problem when I was debugging the code. The thing that surprises me is that cubeMX selects PB2 pin as GPIO Input detect for SD card but in actual case GPIO pin PI8 is connected to the IO Expander that gives you the interrupt for SD-Card detection.

If you don't change the pin PB2 to PI8, f_mkfs() gives you FR_DISK_ERR then

Ozone
Lead

Needless to say, the configuration must match that of the actual schematic, i.e. your eval board (which I don't have).

Cube isn't quite famous for being robust and error free - meaning, you cannot really trust the auto-generated code.

I implemented a SD card interrface some while ago on a F3 discovery, using the SPL.

dbgarasiya
Senior II

f_mkfs is just not necessary to use all time

you can use f_mount then you can open and write data to sd card