cancel
Showing results for 
Search instead for 
Did you mean: 

Problem Implementing Fatfs on spi flash 1MB . hi i am trying to port Fatfs generated from cube mx on to my spi flash on stm32 board.

svii
Associate III

when creating and opening a file i get error and it exits what could be the problem below i have shared the code .

////////////////////////////////////main.c///////////////////////////////////////////

if( FATFS_LinkDriver(&USER_Driver, USERPath)== 0)

res = f_mkfs(USERPath, FM_ANY, 0, workBuffer, 4096);

if(f_mount(&USERFatFS, (TCHAR const*)USERPath, 0) == FR_OK)

 if(f_open(&USERFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) == FR_OK)

///////////////////////////////////////user_diskio.c/////////////////////////////

DRESULT USER_read (

BYTE pdrv,   /* Physical drive nmuber to identify the drive */

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

DWORD sector,  /* Sector address in LBA */

UINT count   /* Number of sectors to read */

)

{

 /* USER CODE BEGIN READ */

 //int i =0;

 SST80B_ReadSector(buff,0,sector*4096,count*4096);

return RES_OK;

 /* USER CODE END READ */

}

#if _USE_WRITE == 1

DRESULT USER_write (

BYTE pdrv,     /* Physical drive nmuber to identify the drive */

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

DWORD sector,    /* Sector address in LBA */

UINT count     /* Number of sectors to write */

)

 /* USER CODE BEGIN WRITE */

 /* USER CODE HERE */

 uint32_t sector_address = sector;

  uint8_t count_func = count;

 int eras_add = 0; 

 int loc = 0;

 for(int no=0;no<=count;no++)

 {

chipsector_erase_call(sector*no*4096);

}

   for(int i =1;i<=count_func;i++)

   for(int j =0;j<4096;j++)

int bb =0;

bb = *(buff+j);

sector_address += j;

SST80B_WriteByte((uint8_t)(*(buff+j)),sector_address); 

}

  sector_address += 4096;

  

 /* USER CODE END WRITE */

 }

return RES_OK;

 }

#if _USE_IOCTL == 1

DRESULT USER_ioctl (

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

BYTE cmd,    /* Control code */

void *buff   /* Buffer to send/receive control data */

)

{

 /* USER CODE BEGIN IOCTL */

  DRESULT res = RES_ERROR;

  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 :

  *(DWORD*)buff = 256;

  res = RES_OK;

  break;

 /* Get R/W sector size (WORD) */

 case GET_SECTOR_SIZE :

  *(WORD*)buff = 4096;

  res = RES_OK;

  break;

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

 case GET_BLOCK_SIZE :

  *(DWORD*)buff = 1;

res = RES_OK;

  break;

 default:

  res = RES_PARERR;

 }

The spi flash driver for write operation is byte transmit, however the read is sector read .

The low level spi flash driver is working .

kindly let me know what could be the problem.Thanks in advance.

4 REPLIES 4

Several of the computations in the USER_write() are broken, and sending bytes will be very slow. Check if you can at least do a page at a time.

Test the USER_write()​, and read, thoroughly BEFORE integrating into FatFs.

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

hi ,thanks for the reply , i have implemented a multi byte write (sector write ) which is working and tested.

Below is the code :

#if _USE_WRITE == 1

DRESULT USER_write (

BYTE pdrv,     /* Physical drive nmuber to identify the drive */

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

DWORD sector,    /* Sector address in LBA */

UINT count     /* Number of sectors to write */

)

 /* USER CODE BEGIN WRITE */

 /* USER CODE HERE */

 uint32_t add = sector *4096;

 uint32_t ea1 = 0;

  

 for(int no=1;no<=count;no++)

 {

ea1 = sector*no*4096;

chipsector_erase_call(ea1);

}

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

 {

  SST25Write(add,(uint8_t *)buff,4096);

buff= buff+4096;

add += 4096;

}

 /* USER CODE END WRITE */

}

DRESULT USER_read (

BYTE pdrv,   /* Physical drive nmuber to identify the drive */

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

DWORD sector,  /* Sector address in LBA */

UINT count   /* Number of sectors to read */

)

{

 /* USER CODE BEGIN READ */

 uint32_t add = sector *4096;

 int c = 0;

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

 {

 SST80B_ReadSector(buff,0,add,4096);

buff = buff+4096;

add += 4096;

}

return RES_OK;

 /* USER CODE END READ */

}

I am able to open the file using  if(f_open(&USERFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) == FR_OK)

  {

res = f_write(&USERFile, wtext, sizeof(wtext), (void *)&byteswritten);

Not able to write into it, get disk error 0xffffffff, kindly let me know ,what could be creating problems,

thanks in advance.

Erase math is broken​

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

hi, thanks for the reply it is working solved the issues:my next step would be trying to integrate this fatfs on spi flash along my  usb code (configured as memory stick).I already have an working tested code for the usb were, if plugged in the pc detects it as a memory storage device and tries to format it.

Now that the fatfs is up , i will be integrating it along with my usb code,

any pointers on how to do this will be helpful ,i have come across two files user_diskio.c, usbd_storage_if.c ,should i have to add the flash driver 

support to both these files.thanks in advance.