cancel
Showing results for 
Search instead for 
Did you mean: 

Micro SD Card Application Problem using STM32F4 Discovery Board

Ogulcan Ariyurek
Associate II
Posted on November 07, 2017 at 10:09

Hi,

I'm using STM32F4 Discovery board with its base-board extension that has a micro-sd card slot. I'm using the STMCubeMX and its current latest F4 firmware which is 

STMCubeMX FW F4 

1.17.0.

There is no SD card example for STM32F4 Discovery board but I found an example code for another discovery board which is located at the following directory:

STM32Cube_FW_F4_V1.17.0\Projects\STM32469I-Discovery\Applications\FatFs\FatFs_uSD

I've tried many different combinations of possible solutions using the libraries offered in STM32CubeMX with the help of that example project, but I have been getting errors although it mounts succesfully as the error code suggests.

Here are the important parts of my code:

/* USER CODE BEGIN 0 */

FATFS SDFatFs; /* File system object for SD disk logical drive */

FIL MyFile; /* File object */

static uint8_t buffer[_MAX_SS]; /* a work buffer for the f_mkfs() */

FRESULT res; /* FatFs function common result code */

// uint32_t byteswritten, bytesread; /* File write/read counts */

// uint8_t wtext[] = 'This is STM32 working with FatFs\n'; /* File write buffer */

// uint8_t rtext[100];

int retlink = 0xFF, retmount = 0xFF, retmkfs = 0xFF, retopen = 0xFF, retwrite = 0xFF, retclose = 0xFF;

/* USER CODE END 0 */

//...........................................

//...........................................

  /* USER CODE BEGIN 2 */

char SDPath[4];

SDPath[0]='0';

SDPath[1]=':';

SDPath[2]='/';

SDPath[3]=0;

char fn[11];

fn[0]='0'; fn[1]=':'; fn[2]='s'; fn[3]='t'; fn[4]='m'; fn[5]='3'; fn[6]='2'; fn[7]='.'; fn[8]='t'; fn[9]='x'; fn[10]='t';// fn[11]='/'; fn[12]=0;

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);

HAL_Delay(100);

retmount = f_mount(&SDFatFs, '0:', 0); /* (0) Succeeded */

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);

HAL_Delay(1000);

retopen = f_open(&MyFile, '0:stm32.txt', FA_READ);

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);

HAL_Delay(100);

retwrite = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);

HAL_Delay(100);

retclose = f_close(&MyFile);

HAL_Delay(100);

f_mount(0, '0:', 0);

Here is my SDIO init function:

/* SDIO init function */

static void MX_SDIO_SD_Init(void)

{

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 = 118;

}

While I'm debugging the project, I get following error returns:

for f_open: (3) The physical drive cannot work

for f_write: (9) The file/directory object is invalid

for f_close:  (9) The file/directory object is invalid

I'll be glad to hear for any suggestions to solve this issue.

Cheers,

Ogulcan

#stm32cubemx #stm32f4-discovery #sdcard #fatfs #sdio #fatfs-sdio #stm32cube_fw_f4_v1.17.0 #microsd #stm32f4
12 REPLIES 12

https://drive.google.com/open?id=0B7OY5pub_GfINVZnNXFvTGxaWmM

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

Thank you so much Clive Two.Zero.

Worked perfectly, now at least I know my hardware is okay, if only ST provided straightforward working examples like this it would have saved a lot of time. I will forget using Cubemx on this peripheral.

Thanks again.

Paul

I had posted a .HEX for Ahmed several year back, but I think the forum ate it, at least search is giving me nothing. I reworked this with new FATFS code last year.

I've got other stuff for the F4 which are HAL based, not sure I've circled back for the F4-DISCO.

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