how to use FATfs on SPI in a STM32F429

Discussions concerning TrueSTUDIO for STM32 9.0.0 and later versions.

Moderators: Markus Girdland, Mattias Norlander

Niconol
Posts: 10
Joined: Tue Dec 04, 2018 11:03 am

how to use FATfs on SPI in a STM32F429

Postby Niconol » Wed May 15, 2019 2:13 pm

Hi everybody,
I'm very bored beacause I now realize that STM32F429 not included SDcard in FATfs settings.
I'm not sure but I understand that this chip can't control an sdcard via SPI.

It's a big problem for me.

Is there any solution to force the using of SPI for read and write on sdcard with FATfs?

Best

alister
Posts: 38
Joined: Fri Jan 25, 2019 3:18 am

Re: how to use FATfs on SPI in a STM32F429

Postby alister » Wed May 15, 2019 10:54 pm

FatFs needs you provide a low-level driver to do its I/O, so yes of course it'll support SPI.
If you search the F4's Cube FW, expect you'd find an example that pretty much does it all.

Niconol
Posts: 10
Joined: Tue Dec 04, 2018 11:03 am

Re: how to use FATfs on SPI in a STM32F429

Postby Niconol » Fri May 17, 2019 12:27 pm

I was hoping to find example in F4's Cube FW, but no.

Perhaps I need to start by the begining ....
I want to use FatFs for read/write on a SD card, connecting via a specific hardware driver on a SPI (no matter which one) of the STM32F429 , which is on a Nucleo board.

In CubeMx, I see that I can't tick "Sd Card" on the FATFS's mode . I don't know why. To be more precise, I can only tick "user defined". But I don't know how to define it. I imagined that I have to specific that I want to use FTAFS lib for a SDCard connected on a specific SPI port, at least, and a lot of other things too...

I read FatFs applications, UM1721.pdf. As Alister say, F4's cube FW provide lot ok examples, for each type of memory....
In the F4's Cube FW, I don't find example FATFS + SdCard. But I find sources, in particular:
diskio.c
ff_gen_drv.c
ff.c

and in "drivers" folder, lot of templates, and in particular:
sd_diskio_template.c:

Code: Select all

/**
  ******************************************************************************
  * @file    sd_diskio_template.c
  * @author  MCD Application Team
  * @brief   SD Disk I/O template driver.This file needs to be renamed and copied
             into the application project alongside the respective header file
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "ff_gen_drv.h"
#include "sd_diskio.h"


/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* use the default SD timout as defined in the platform BSP driver*/
#if defined(SDMMC_DATATIMEOUT)
#define SD_TIMEOUT SDMMC_DATATIMEOUT
#elif defined(SD_DATATIMEOUT)
#define SD_TIMEOUT SD_DATATIMEOUT
#else
#define SD_TIMEOUT 30 * 1000
#endif

#define SD_DEFAULT_BLOCK_SIZE 512

/*
 * Depending on the usecase, the SD card initialization could be done at the
 * application level, if it is the case define the flag below to disable
 * the BSP_SD_Init() call in the SD_Initialize().
 */

/* #define DISABLE_SD_INIT */

/* Private variables ---------------------------------------------------------*/
/* Disk status */
static volatile DSTATUS Stat = STA_NOINIT;

/* Private function prototypes -----------------------------------------------*/
static DSTATUS SD_CheckStatus(BYTE lun);
DSTATUS SD_initialize (BYTE);
DSTATUS SD_status (BYTE);
DRESULT SD_read (BYTE, BYTE*, DWORD, UINT);
#if _USE_WRITE == 1
  DRESULT SD_write (BYTE, const BYTE*, DWORD, UINT);
#endif /* _USE_WRITE == 1 */
#if _USE_IOCTL == 1
  DRESULT SD_ioctl (BYTE, BYTE, void*);
#endif  /* _USE_IOCTL == 1 */

const Diskio_drvTypeDef  SD_Driver =
{
  SD_initialize,
  SD_status,
  SD_read,
#if  _USE_WRITE == 1
  SD_write,
#endif /* _USE_WRITE == 1 */

#if  _USE_IOCTL == 1
  SD_ioctl,
#endif /* _USE_IOCTL == 1 */
};

/* Private functions ---------------------------------------------------------*/
static DSTATUS SD_CheckStatus(BYTE lun)
{
  Stat = STA_NOINIT;

  if(BSP_SD_GetCardState() == MSD_OK)
  {
    Stat &= ~STA_NOINIT;
  }

  return Stat;
}

/**
  * @brief  Initializes a Drive
  * @param  lun : not used
  * @retval DSTATUS: Operation status
  */
DSTATUS SD_initialize(BYTE lun)
{
  Stat = STA_NOINIT;
#if !defined(DISABLE_SD_INIT)

  if(BSP_SD_Init() == MSD_OK)
  {
    Stat = SD_CheckStatus(lun);
  }

#else
  Stat = SD_CheckStatus(lun);
#endif
  return Stat;
}

/**
  * @brief  Gets Disk Status
  * @param  lun : not used
  * @retval DSTATUS: Operation status
  */
DSTATUS SD_status(BYTE lun)
{
  return SD_CheckStatus(lun);
}

/**
  * @brief  Reads Sector(s)
  * @param  lun : not used
  * @param  *buff: Data buffer to store read data
  * @param  sector: Sector address (LBA)
  * @param  count: Number of sectors to read (1..128)
  * @retval DRESULT: Operation result
  */
DRESULT SD_read(BYTE lun, BYTE *buff, DWORD sector, UINT count)
{
  DRESULT res = RES_ERROR;

  if(BSP_SD_ReadBlocks((uint32_t*)buff,
                       (uint32_t) (sector),
                       count, SD_TIMEOUT) == MSD_OK)
  {
    /* wait until the read operation is finished */
    while(BSP_SD_GetCardState()!= MSD_OK)
    {
    }
    res = RES_OK;
  }

  return res;
}

/**
  * @brief  Writes Sector(s)
  * @param  lun : not used
  * @param  *buff: Data to be written
  * @param  sector: Sector address (LBA)
  * @param  count: Number of sectors to write (1..128)
  * @retval DRESULT: Operation result
  */
#if _USE_WRITE == 1
DRESULT SD_write(BYTE lun, const BYTE *buff, DWORD sector, UINT count)
{
  DRESULT res = RES_ERROR;

  if(BSP_SD_WriteBlocks((uint32_t*)buff,
                        (uint32_t)(sector),
                        count, SD_TIMEOUT) == MSD_OK)
  {
   /* wait until the Write operation is finished */
    while(BSP_SD_GetCardState() != MSD_OK)
    {
    }
    res = RES_OK;
  }

  return res;
}
#endif /* _USE_WRITE == 1 */

/**
  * @brief  I/O control operation
  * @param  lun : not used
  * @param  cmd: Control code
  * @param  *buff: Buffer to send/receive control data
  * @retval DRESULT: Operation result
  */
#if _USE_IOCTL == 1
DRESULT SD_ioctl(BYTE lun, BYTE cmd, void *buff)
{
  DRESULT res = RES_ERROR;
  BSP_SD_CardInfo CardInfo;

  if (Stat & STA_NOINIT) return RES_NOTRDY;

  switch (cmd)
  {
  /* Make sure that no pending write process */
  case CTRL_SYNC :
    res = RES_OK;
    break;

  /* Get number of sectors on the disk (DWORD) */
  case GET_SECTOR_COUNT :
    BSP_SD_GetCardInfo(&CardInfo);
    *(DWORD*)buff = CardInfo.LogBlockNbr;
    res = RES_OK;
    break;

  /* Get R/W sector size (WORD) */
  case GET_SECTOR_SIZE :
    BSP_SD_GetCardInfo(&CardInfo);
    *(WORD*)buff = CardInfo.LogBlockSize;
    res = RES_OK;
    break;

  /* Get erase block size in unit of sector (DWORD) */
  case GET_BLOCK_SIZE :
    BSP_SD_GetCardInfo(&CardInfo);
    *(DWORD*)buff = CardInfo.LogBlockSize / SD_DEFAULT_BLOCK_SIZE;
   res = RES_OK;
    break;

  default:
    res = RES_PARERR;
  }

  return res;
}
#endif /* _USE_IOCTL == 1 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/



I imagine that all that I need is here, but I can't do something by myself with this.

Can you explain me to see clearly what I have to do, in wich order, and/or link me to a full project.

I have to precise that I work with FreeRTOS, I read that there is sepcific driver ...

Best

Niconol
Posts: 10
Joined: Tue Dec 04, 2018 11:03 am

Re: how to use FATfs on SPI in a STM32F429

Postby Niconol » Fri May 17, 2019 12:44 pm

Now I understand that i can tick "SdCard" on FTAFS's mode if I enable SDIO, for exemple SD1bit.
But hao to redirect it to one of the SPI port?

alister
Posts: 38
Joined: Fri Jan 25, 2019 3:18 am

Re: how to use FATfs on SPI in a STM32F429

Postby alister » Sun May 19, 2019 11:02 pm

> Can you explain me to see clearly what I have to do, in wich order, and/or link me to a full project.

Google "fatfs spi sd card stm32f4" without the quotes.

Niconol
Posts: 10
Joined: Tue Dec 04, 2018 11:03 am

Re: how to use FATfs on SPI in a STM32F429

Postby Niconol » Wed May 22, 2019 2:11 pm

I already search on google and other, and find a lot of project, code, tuto. Thanks

With this:

Code: Select all

http://stm32f4-discovery.net/2014/07/library-21-read-sd-card-fatfs-stm32f4xx-devices/

I loose my usual lib, incompatibility with stm32f4xx_hal lib

With this :
https://github.com/eziya/STM32_SPI_SDCARD
I can't compile, where is arm_mats.h ...

With others, no way to obtain something which works ....

In both cases, I correct one error, an other, an other, ... and, in final, I'm lost in my correction (change lib, change constant name, ...) and I can't obtain nothing good. Perhaps, one day, I'll obtain something which works. But I'll not be able to continue with my usual lib, or other tools as CubeMx.

I think it'll more useful to speak with a person who knows for understanding and after include some parts of code. And I speak poor english ...

Thanks

alister
Posts: 38
Joined: Fri Jan 25, 2019 3:18 am

Re: how to use FATfs on SPI in a STM32F429

Postby alister » Wed May 22, 2019 10:54 pm

Well neither FATFS nor SPI needs arm_maths.h nor changing any libraries. So you're borrowing the wrong pieces of code from places.

I've recently implemented FATFS on SPI for the H7 and discovered a bug in stm32_adafruit_sd.c that I'd read was fixed in L4 was missing. So the L4 FW examples are good and I suggest you look there. So use STM32CubeMX to generate your F4 project, and then find copy only the source files you need from the L4 FW into your project. Don't go changing any libraries.

Niconol
Posts: 10
Joined: Tue Dec 04, 2018 11:03 am

Re: how to use FATfs on SPI in a STM32F429

Postby Niconol » Thu May 23, 2019 9:35 am

Thanks Alister,
I have to set apart for 10 days because of too much work, but I still hope to achieve soon, with your advices

To be continued...

Best

nicolas


Return to “TrueSTUDIO for STM32 discussions”

Who is online

Users browsing this forum: No registered users and 2 guests