cancel
Showing results for 
Search instead for 
Did you mean: 

How to edit low level disk i/o for FatFS+SPI?

Emin Yagmahan
Associate II
Posted on April 08, 2018 at 20:55

Dear ST community,

I am currently working on being able to control a micro SD card via SPI and FatFS.

Now to my initial situation.

I have already generated a project via STCubeMX.

The project information can be found in the appendix ''SDCard.pdf''.

I am working with a STM32L475VG (BL475-IOT01A).

I have already tested the SPI connection with a second controller.

This works fine.

The SD card has been connected as shown below:

(CS -> NSS ?)

0690X00000604T2QAI.jpg

Now I have some difficulties with controlling this card (Samsung Micro SD card 16GB SDHC).

CubeMX shows the option ''user-defined'' under FatFs.

According to Chan's description, I have to implement the low level disk I / O functions on my own.

''The low level device control module is not part of FatFs module and it needs to be provided by implementer'' ( chapter

http://elm-chan.org/fsw/ff/00index_e.html

).

The manual of ST (UM1721 also in the appendix) gives already first hints, how the functions have to be added:

[...]

&sharpif _USE_WRITE == 1

mynewdisk_write,

&sharpendif /* _USE_WRITE == 1 */

/*------------------------ Initialize a Drive ---------------------------*/

DSTATUS mynewdisk_initialize (void)

{

Stat = STA_NOINIT;

// write your own code here to initialize the drive

Stat &= ~STA_NOINIT;

return Stat;

}

/*------------------------- Get Disk Status -----------------------------*/

DSTATUS mynewdisk_status (void)

{

Stat = STA_NOINIT;

// write your own code here

return Stat;

}

[...]

These are the functions:

  • Initialize a Drive

  • Read Sector (s)

  • Write Sector (s)

  • Get Disk Status

  • Miscellaneous Functions

Now back to my specific questions

- How exactly do I have to implement these functions?

- Does the SD card control with the commands of the SD specification take place here?

- Are there already examples of the STM32L4 series for FatFs with SPI?

Unfortunately there are none in the repository of the STM32L4.

I hope you can give me some initial ideas on how to implement this.

I have to say that i despair

Many Thanks

Best regards

#fat #fat_fs #sd-card- #sd-acces #spi-communication #stm32l4-spi
1 ACCEPTED SOLUTION

Accepted Solutions
Posted on April 08, 2018 at 21:12

The DISKIO.C layer of FATFS expects you to provide routines to read/write multiple blocks from the underlying block storage device (HD, USBSTOR/MSC, SDCard, etc). Usually the functionality for the specific interface is managed below this.

An SPI layer supporting SD, SDHC and SDXC cards can be found here

STM32Cube_FW_L4_V1.11.0\Drivers\BSP\Adafruit_Shield\stm32_adafruit_sd.c

STM32Cube_FW_L4_V1.11.0\Projects\NUCLEO-L496ZG\Demonstrations\Adafruit_LCD_1_8_SD_Joystick\Src\sd_diskio.c

There is a multi-sector bug in this code, but it is fixable. The bigger problem is the inherent slowness of such implementations.

https://community.st.com/0D50X00009XkgXzSAJ

 

For an 16 GB SDHC card the SD/MMC code would need to support the appropriate command set, so other web examples of this may not do that. The adafruit code should be good for cards >200GB

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

View solution in original post

1 REPLY 1
Posted on April 08, 2018 at 21:12

The DISKIO.C layer of FATFS expects you to provide routines to read/write multiple blocks from the underlying block storage device (HD, USBSTOR/MSC, SDCard, etc). Usually the functionality for the specific interface is managed below this.

An SPI layer supporting SD, SDHC and SDXC cards can be found here

STM32Cube_FW_L4_V1.11.0\Drivers\BSP\Adafruit_Shield\stm32_adafruit_sd.c

STM32Cube_FW_L4_V1.11.0\Projects\NUCLEO-L496ZG\Demonstrations\Adafruit_LCD_1_8_SD_Joystick\Src\sd_diskio.c

There is a multi-sector bug in this code, but it is fixable. The bigger problem is the inherent slowness of such implementations.

https://community.st.com/0D50X00009XkgXzSAJ

 

For an 16 GB SDHC card the SD/MMC code would need to support the appropriate command set, so other web examples of this may not do that. The adafruit code should be good for cards >200GB

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