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 February 21, 2017 at 01:13

Dear  h.rick,

What kind of STM32-mcus or dev-boards are you using?

Many problems occurs by hardware(especially unstable wirings).

Best regards,

Nemui.
antonius
Senior
Posted on February 21, 2017 at 11:47

I'm using my own stm32F103VC board..

how can I initialize SD card with STM32 cube mx ?

Posted on February 21, 2017 at 13:37

If you are using CubeMX, press F1 for HELP, UM1718.pdf will open. Got to chapter 7 and start reading ...

This is a step by step Manual for FatFS and SDIO.

Best regards,

Markus.

Posted on February 21, 2017 at 23:03

Ok, I've been reading it, how can I port it to different STM32 for example F103 and F407 ? Which file is main key ?

I have created my MDK, please let me know which file is playing the key for disk initialization?

Thanks

Posted on February 22, 2017 at 01:24

Dear  h.rick,

There is no different F103 from F407 on upper-layer,lower layer difference  is taken in HAL.

But STM32F4 have Core-Coupled SRAM region which cannot DMA.

If you migrate F103 to F407,plz don't forget to put DMA-buffer on SRAM1 or SRAM2 region.

Posted on February 22, 2017 at 12:58

Ok,

So the SD init sequence will be here :

HAL_Init(); on example page 194 UM1718

I have this from STM32CubeMX generated :

int main(void)

{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

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

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

  HAL_Init();

  /* Configure the system clock */

  SystemClock_Config();

  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  MX_SDIO_SD_Init();

  MX_FATFS_Init();

I can add

this code straight away ?

/*♯♯-0- Turn all LEDs off(red, green, orange and blue) */

 

HAL_GPIO_WritePin(GPIOG, (GPIO_PIN_10 | GPIO_PIN_6 | GPIO_PIN_7 |

 

GPIO_PIN_12), GPIO_PIN_SET);

 

/*♯♯-1- FatFS: Link the SD disk I/O driver ♯♯♯♯♯♯♯♯♯♯*/

 

if(retSD == 0){

 

/* success: set the orange LED on */

 

HAL_GPIO_WritePin(GPIOG, GPIO_PIN_7, GPIO_PIN_RESET);

 

/*♯♯-2- Register the file system object to the FatFs module ♯♯♯*/

 

if(f_mount(&SDFatFs, (TCHAR const*)SD_Path, 0) != FR_OK){

 

/* FatFs Initialization Error : set the red LED on */

 

HAL_GPIO_WritePin(GPIOG, GPIO_PIN_10, GPIO_PIN_RESET);

 

while(1);

 

} else {

 

/*♯♯-3- Create a FAT file system (format) on the logical drive♯*/

 

/* WARNING: Formatting the uSD card will delete all content on the

 

device */

 

if(f_mkfs((TCHAR const*)SD_Path, 0, 0) != FR_OK){

 

/* FatFs Format Error : set the red LED on */

 

HAL_GPIO_WritePin(GPIOG, GPIO_PIN_10, GPIO_PIN_RESET);

.......

Thanks

Posted on February 22, 2017 at 23:06

How can I use DMA-buffer on the code ? or you're talking about hardware buffering ?

nichtgedacht
Senior
Posted on February 24, 2017 at 20:49

Hi

Look at generated MX_FATFS_Init();

In fatfs.c:

void MX_FATFS_Init(void) 
{
 /*## FatFS: Link the USER driver ###########################*/
 retUSER = FATFS_LinkDriver(&USER_Driver, USER_Path);
 /* USER CODE BEGIN Init */
 /* additional user code for init */ 
 /* USER CODE END Init */
}
�?�?�?�?�?�?�?�?�?

In ff_gen_drv.c:

uint8_t FATFS_LinkDriver(Diskio_drvTypeDef *drv, char *path)
{
 return FATFS_LinkDriverEx(drv, path, 0);
}
�?�?�?�?

In user_diskio.c you have to implement the generated skeleton functions using your own IO functions:

/* Private function prototypes -----------------------------------------------*/
 
DSTATUS USER_initialize (BYTE pdrv);
DSTATUS USER_status (BYTE pdrv);
DRESULT USER_read (BYTE pdrv, BYTE *buff, DWORD sector, UINT count);
#if _USE_WRITE == 1
 DRESULT USER_write (BYTE pdrv, const BYTE *buff, DWORD sector, UINT count); 
#endif /* _USE_WRITE == 1 */
#if _USE_IOCTL == 1
 DRESULT USER_ioctl (BYTE pdrv, BYTE cmd, void *buff);
#endif /* _USE_IOCTL == 1 */
Diskio_drvTypeDef USER_Driver =
{
 USER_initialize,
 USER_status,
 USER_read, 
#if _USE_WRITE
 USER_write,
#endif /* _USE_WRITE == 1 */ 
#if _USE_IOCTL == 1
 USER_ioctl,
#endif /* _USE_IOCTL == 1 */
};
// Your implementation goes here
�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

Dieter

Posted on February 25, 2017 at 03:37

From I can read in UM1718 page 240 :

.......

B.3.4 FatFs

FatFs is a generic FAT/exFAT file system solution well suited for small embedded systems.

FatFs configuration is available in ffconf.h generated file.

The initialization of the SDIO peripheral for the FatFs SD Card mode and of the FMC peripheral for the FatFs External SDRAM and External SRAM modes are kept in the main.c file.

Some files need to be modified by the user to match user board specificities (BSP drivers in STM32Cube embedded software package can be used as example):

• bsp_driver_sd.c/.h generated files when using FatFs SD Card mode

• bsp_driver_sram.c/.h generated files when using FatFs External SRAM mode

• bsp_driver_sdram.c/.h generated files when using FatFs External SDRAM mode.

Multi-drive FatFs is supported, which means that multiple logical drives can be used by the application (External SDRAM, External SRAM, SD Card, USB Disk, User defined). However support for multiple instances of a given logical drive is not available (e.g. FatFs using two instances of USB hosts or several RAM disks).

.......

So I need to put all my SD card initialization in it, how can  I call it ? or it's already handled by FATFs init ?

Thanks