AnsweredAssumed Answered

Fatfs Stack and Heap Memoty Requirement

Question asked by Zhitai Liu on Apr 24, 2017
Latest reply on Apr 25, 2017 by Zhitai Liu

Hi, there!

First time developing Fatfs with lovely STM32F091 Nucleo board. 

Have some serious memory problems here.

-----------------------------------------------------------------------------------

Hardware: STM32F091 Nucleo board.

Llbrary: Cube F0 1.7

IDE: Keil uVision 5.14

-----------------------------------------------------------------------------------

I am basically following the structure as in the example in

...\STM32Cube_FW_F0_V1.7.0\Projects\STM32F091RC-Nucleo\Applications\FatFs\FatFs_uSD

the simple functions like test f_mout, f_open, f_read, f_write were working in the beging.

As I wrote more and more codes, sometimes I have some stack or heap memory problems.

for example, 

setting 

Stack_Size EQU 0x800;

Heap_Size EQU 0x400;

in the startup file. (optimzed level 1)

As the the processing run to 

uint8_t BSP_SD_ReadBlocks(uint32_t* pData, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)

{

...

   ptr = malloc(sizeof(uint8_t)*BlockSize);
   if( ptr == NULL )
   {
      goto error;
   }
   memset(ptr, SD_DUMMY_BYTE, sizeof(uint8_t)*BlockSize);

   /* Data transfer */
   while (NumberOfBlocks--)
   {

   ...

   }

...

}

something i got a ptr with address like 0x2000005C8, however the NumberOfBlocks lies in 0x200000658, 

as memset function is called, 0x2000005C8+ 512 > 0x200000658. I got a hard fault error.

Another case is occurred if i use less stack memory, something the initialized objects are changed.

For example, I got a SPI_HandleTypeDef sd_Spi, which is initialized in the DSTATUS USER_initialize (BYTE pdrv);

As the program runs, the sd_Spi.Instance may have an incorrect value, not the original SPIx base address.

I know that this is about stack and heap. 
I would like to know that is there any simple method to assess how much heap and stack do I need?

or is there any recommendation size for fatfs?

 

Thanks for reading this post!

Outcomes