cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeMX, FatFS and SDIO?

antonius
Senior
Posted on February 20, 2017 at 22:57

Everyone,

How can I use cubeMX for SDcard ? I have generated for MDK but :

this function doesn't work , from UM1721

User manual

Developing Applications on STM32Cube with FatFs page 21.

:

uint32_t wbytes; /* File write counts */

uint8_t wtext[] = 'text to write logical disk'; /* File write buffer */

if(FATFS_LinkDriver(&mynewdisk_Driver, mynewdiskPath) == 0)

{

if(f_mount(&mynewdiskFatFs, (TCHAR const*)mynewdiskPath, 0) == FR_OK)

{

if(f_open(&MyFile, 'STM32.TXT', FA_CREATE_ALWAYS | FA_WRITE) == FR_OK)

{

if(f_write(&MyFile, wtext, sizeof(wtext), (void *)&wbytes) == FR_OK);

{

f_close(&MyFile);

}

}

}

}

FATFS_UnLinkDriver(mynewdiskPath);

This variable :

&mynewdisk_Driver doesn't exist, which file is related ?

Thank you

54 REPLIES 54
Posted on April 27, 2017 at 02:32

You should be able to enable pull-ups on the pins internally.

The FatFs code will not work if the underlying SDIO code isn't functional.

What pin is being used on the socket's card detect?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on April 27, 2017 at 02:33

Lower the SDIO init speed, there's one file for doing that. I have

posted in here I reckon.

Posted on April 27, 2017 at 02:34

That's right, connect SD Detect as well, it's inside the code PB14 if

I'm not wrong..

Posted on April 27, 2017 at 03:06

Yeah, in STM32CubeMX under 'Configuration' -> 'GPIO' a 'Pin Configuration' window comes up. Under the 'SDIO' tab it shows the SDIO pins, which are defaulted as 'Alternate Function Push Pull' for 'GPIO mode', but under 'GPIO Pull-up/Pull-down' it's defaulted as 'No pull-up and no pull down' - I can change this to 'Pull-up' for just the 4 data lines and the CMD line only (so no open-drain external pull-up resistors needed - just internal). By default STM32Cube SDIO settings don't include the SD detect pin; I'll have to do that manually. I'll change the clock speed setting to what you put manually in code; but I'll have to see what this changes in the clock tree as well.

Posted on April 27, 2017 at 04:52

Yes it depends on the main clock from HSE or HSI or internal and divider,

I'm not in windows now so I can not run STM32CubeMX but I'll have a look

on my code, there're some hints on it..

Posted on April 27, 2017 at 05:05

Have a dig on the code at stm32f1xx_II_sdmmc.h ==> STM32F103, for

STM32F4xx, I reckon you need to change the name.

/* SDIO Intialization Frequency (400KHz max) */ ==> check your main

clock and divider on STM32CubeMX

&sharpdefine SDIO_INIT_CLK_DIV ((uint8_t)0xC3)

/* SDIO Data Transfer Frequency */

/* SDIO_CK = SDIOCLK / (SDIO_TRANSFER_CLK_DIV + 2) */

&sharpdefine SDIO_TRANSFER_CLK_DIV ((uint8_t)0x0F) // ==72/(15+2) = 4.23MHz

, work best for my MP3 chip syncronization speed.

Posted on April 27, 2017 at 05:22

Some debug to UART for seeing the response

/=====/

/**

  • @brief Initializes the SD card device.

  • @retval SD status

*/

uint8_t BSP_SD_Init(void)

{

uint8_t sd_state = MSD_OK;

printf('BSP_SD_Init Function!\n');

/* Check if the SD card is plugged in the slot */

if (BSP_SD_IsDetected() != SD_PRESENT)

{

return MSD_ERROR;

}

/* HAL SD initialization */

sd_state = HAL_SD_Init(&hsd, &SDCardInfo);

&sharpifdef BUS_4BITS

/* Configure SD Bus width */

if (sd_state == MSD_OK)

{

/* Enable wide operation */

if (HAL_SD_WideBusOperation_Config(&hsd, SDIO_BUS_WIDE_4B) != SD_OK)

{

sd_state = MSD_ERROR;

printf('MSD_ERROR\n');

}

else

{

sd_state = MSD_OK;

printf('MSD_OK \n');

}

}

&sharpendif

return sd_state;

}

Posted on April 27, 2017 at 05:23

&sharpdefine __SD_DETECT_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()

Posted on April 27, 2017 at 20:19

I have 

__HAL_RCC_GPIOB_CLK_ENABLE() defined and enabling the clock on that port. Are you redefining this function 

__SD_DETECT_GPIO_CLK_ENABLE() ? I don't have __SD_DETECT_GPIO_CLK_ENABLE() in my code.

Posted on April 27, 2017 at 20:29

This is the list of alternate functions for PB14 in CubeMX. Which one would be the Card Detect for SDIO mode? How would you write this in the code?