cancel
Showing results for 
Search instead for 
Did you mean: 

FatFs with SPI Flash porting on STM32 MCU

voyvoda .
Senior

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 ?

1 ACCEPTED SOLUTION

Accepted Solutions
voyvoda .
Senior

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

View solution in original post

20 REPLIES 20

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 Up vote any posts that you find helpful, it shows what's working..
voyvoda .
Senior

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

};

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 Up vote any posts that you find helpful, it shows what's working..
voyvoda .
Senior

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 .
Senior

FLASH_SECTOR_SIZE = 4096

Ogulcan Ariyurek
Associate II

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 .
Senior

Oğulcan Hocam Selamlar,

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

Teşekkürler 🙂

SPI flash dosyanızı izninizle ben de indirip inceledim. hatta hal kütüphanesi ile çalışacak hale getirdim ancak bir konudan emin olamadım. diskio.c dosyası içerisinde disk_ioctl fonksiyonunda switch içerisinde case CTRL_ERASE_SECTOR: durumu braketle devre dışı bırakılmış. neden devredışı yapılmış olabilir. yani veri yazmadan önce ilgili sektörün silinmesi gerekmiyor mu. Bilginiz var ise paylaşır mısınız. Teşekkürler