Skip to main content
voyvoda .
Senior
August 15, 2018
Solved

FatFs with SPI Flash porting on STM32 MCU

I would like to implement FatFs on my STM32 MCU with SPI Flash, W25Q16JV.

Is there a sample code for this? How can I port it ?

This topic has been closed for replies.
voyvoda .
voyvoda .Best answer
Senior
January 10, 2021

Hello, I am able write and read successfully.

change the line in "ffconf.h" file as below

#define _MAX_SS  4096

change the line in "spi_flash.h" file as below

#define FLASH_SECTOR_SIZE 4096

the attached file should be modified as well

20 replies

Tesla DeLorean
Guru
August 15, 2018

So take your code you have for read/write/erase of the SPI FLASH, and integrate that with the DISKIO layer of FatFs

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
voyvoda .
voyvoda .Author
Senior
August 15, 2018

Hello Clive,

I generated a sample code by CubeMX and selected fatfs as "User-Defined". There is file, "user_diskio.c" file which is generated bu CubeMX.

Will I put my code into the below function. In addition, there is read and write function available. However there is no erase function.

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 */

};

Tesla DeLorean
Guru
August 15, 2018

You have to manage the access to the flash so that FatFs can read/write 512 byte sector(s). You'd need to deal with the erase operation, and read-erase-write where a sector spanned a block on the underlying device.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
voyvoda .
voyvoda .Author
Senior
August 17, 2018

I can only erase as little as 4K sector on the flash memory, but you say 512 byte.

I will do it something like below. Do you think that it is OK.

DRESULT disk_read(

BYTE drv, /* Physical drive nmuber(0..) */

BYTE *buff, /* Data buffer to store read data */

DWORD sector, /* Sector address(LBA) */

BYTE count /* Number of sectors to read(1..255) */

)

{

int i;

for(i=0;i<count;i++)

{

W25X_Read_Sector(sector,buff);

sector ++;

buff += FLASH_SECTOR_SIZE;

}

return RES_OK;

}

DRESULT disk_write(

BYTE drv, /* Physical drive nmuber(0..) */

const BYTE *buff, /* Data to be written */

DWORD sector, /* Sector address(LBA) */

BYTE count /* Number of sectors to write(1..255) */

)

{

int i;

for(i=0;i<count;i++)

{ W25X_Erase_Sector(sector);

W25X_Write_Sector(sector,(u8*)buff);

sector ++;

buff += FLASH_SECTOR_SIZE;

}

return RES_OK;

}

voyvoda .
voyvoda .Author
Senior
August 17, 2018

FLASH_SECTOR_SIZE = 4096

Ogulcan Ariyurek
Associate III
December 13, 2018

Hi voyvoda,

I'm dealing with the same problem right now.

Did you able to achive porting the FatFS to the SPI flash through your way?

Thanks,

Ogulcan

voyvoda .
voyvoda .Author
Senior
December 14, 2018

Oğulcan Hocam Selamlar,

Ekli dosyadan faydalanabilirsin. İşini görecektir. iyi çalışmalar.

Ogulcan Ariyurek
Associate III
December 14, 2018

Teşekkürler :)

ACice
Visitor II
January 14, 2019

Açıkçası ilgili vermiş olduğunuz dosya sistemi üzerinde çalışıyorum ancak ilk initialize mkfs ve mount ile ilgili kısımları geçiyorum. hatta disk size ve free space verilerini de alıyorum. Ancak restart sonrası bu durum bozuluyor. disk büyüklüklerini okumuyor. nedenini bir türlü bulamadım. başınıza geldiyse dönebilirseniz memnun olurum. Daha önce daha eski sürümünü microsd kart sürmek için başarı ile kullanmıştım. şimdi nedense bir türlü olmadı. Teşekkürler

doganayserhatt
Associate II
July 17, 2019

Merhaba ilgili çalışmanızı tamamladınız mı?

embedded4ever
Visitor II
September 11, 2019

Merhabalar , @ACice​  , @Ogulcan Ariyurek​  , @voyvoda .​ , FatFs'i , Serial Flash ile kullanırken , yaşadığınız dezavantaj olarak nitelendirebileceğiniz bir durum oldu mu ? @ACice​bahsettiğiniz soruna karşı bir çözüm oluşturabildiniz mi?

Teşekkürler ,

Volkan

PDChauhan
Visitor II
October 16, 2020

Hello @voyvoda . (Community Member)​ , I have implemented diskio.c file for my QPSI flash of 4MBytes with 4096 sector size. f_mkfs returns OK, but f_mount returns FR_NO_FILESYSTEM.

I could not open your given attachment "stm32_spi_flash_fatfs".

May you again share your attachment ?

Also, I have doubt for

     case GET_BLOCK_SIZE :

in IOCTL.

Should the return value be "BLOCK_SIZE" (which will be in bytes) or should it be value of how many sectors erased in which case value would be '1' ? I asked this as the comment for this case is

" /* Get erase block size in unit of sector (DWORD) */"

I have read the raw data from flash for the sector #63 where f_mkfs writes and data seems to be OK (FAT header)

Thanks and Regards.