cancel
Showing results for 
Search instead for 
Did you mean: 

How to store user data in flash memory(stm32f207ig)

shetty_siddharth
Associate II
Posted on June 05, 2012 at 16:28

Hello All,

I have been trying to write into the flash(certain user data), but when i debug it i see the values being written elsewhere in other locations and sometimes in the location i have specified.The function i have used is as posted below.

uint32_t FLASH_Pot_StartingAddress=((uint32_t)0x0807f800);

void Flash_ADCPot_Log(void)

{

FLASH_Unlock();

FLASH_Pot_StartingAddress=FLASH_Pot_StartingAddress+2);

FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR );

FLASH_ProgramHalfWord(FLASH_Pot_StartingAddress, ADC_Result);

FLASH_Lock();

}

ADC_Result is a 16 bit value.

Any help would be appreciated, thanks
9 REPLIES 9
Posted on June 05, 2012 at 17:26

You need to make sure that the flash array is erased, and check the return/failure code from the FLASH_ProgramHalfWord()

What tool chain are you using?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
shetty_siddharth
Associate II
Posted on June 05, 2012 at 18:10

Thanks for the reply. I will erase the sector and try it out:)

BTW, i am usil Keil IDE.

shetty_siddharth
Associate II
Posted on June 06, 2012 at 15:41

Is there noway i could write data in the ROM without having to erase the page/sector? I do not want to loose the data everytime i reset the microcontroller.

I am also trying to write to the NOR flash on board the MCBSTM32F200 board. I intialize the FSMC to write in bank1 of the NOR but when i write to it (using the same function i use to write on the onboard flash) it does not seem to work.

I am a beginner so any help would be appreciated.

Thanks

Posted on June 06, 2012 at 16:26

You get to write each location once after erasing. Roll your data across the page, and use multiple pages.

The FLASH_xxx API functions are for internal flash, for NOR you'll need to review the documentation for the parts in question.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
shetty_siddharth
Associate II
Posted on June 06, 2012 at 16:40

But in the stm32f2x std peripheral library file, stm32f2xx_fsmc.c there is no API function to write to the NOR. That is why i was wondering how i could write to the NOR Flash.

Posted on June 06, 2012 at 18:46

But in the stm32f2x std peripheral library file, stm32f2xx_fsmc.c there is no API function to write to the NOR. That is why i was wondering how i could write to the NOR Flash.

 

The FSMC manifests the NOR memory device within the linear memory the Cortex-M3, once there you just read/write to the memory addresses as you would to any other device. The magic sequences of address and data required for the NOR chip would be documented in the data sheet for the part in question. There are typically commands to unlock, erase, and program, as well as spinning and waiting. for erase/program to complete. Most would use a CFI protocol, or some precursor to that.

http://en.wikipedia.org/wiki/Common_Flash_Memory_Interface

http://www.delorie.com/agenda/specs/29220404.pdf

http://www.spansion.com/Support/Application Notes/Quick_Guide_to_CFI_AN.pdf

Read the manual for the NOR Flash part you are using.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
shetty_siddharth
Associate II
Posted on June 07, 2012 at 10:23

Hello Clive, 

Basically i need to write data being collected onto some non-volatile memory and forward it via the Ethernet. what i need to do is to store the data temporarily when the Ethernet connection is not available or disconnected. Hence i was thinking of storing this on the ROM(onboard or External NOR). But the using the external NOR is such additional work and i feel the on board ROM will do the job.However on reset and before i start writing into a sector i have to erase the sector which means i loose all my data. As you said i could roll over the collected data array across a page(which i assume is  8 words?) but i still need to erase an entire sector which means i loose all the data on reset. Could you please let me know if you meant i could erase a particular page? or probably i did not get you. Sorry about this, but i somehow cant figure a way out.My function is as below

/**

  * @brief  Writes 16 bit data in the flash starting from a memory address defined by FLASH_Pot_StartingAddress variable

  * @param  None

  * @retval None

  */

void Flash_ADCPot_Log(void)

{   

static int i=1;

FLASH_Unlock();

if(i)

{  

FLASH_EraseSector (FLASH_Sector_11, VoltageRange_3);

i--;

}

FLASH_Pot_StartingAddress=FLASH_Pot_StartingAddress+((uint32_t)2);

FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR );

FLASH_ProgramHalfWord(FLASH_Pot_StartingAddress, ADC_Result);

FLASH_Lock();

}

shetty_siddharth
Associate II
Posted on June 07, 2012 at 11:43

Clive i think i found what i was looking for, it is the EEPROM emulation application note AN3390.

Thanks.

Posted on June 07, 2012 at 16:17

The address range you're using is a 128KB sector (0x08060000 - 0x0807FFFF). You should start at the beginning and use all of it, otherwise you're just wasting resources.

If you don't want to lose the data already recorded you will need to scan through the sector and find where you stopped last time. You only need to erase to recover the memory. You could use two sectors and ping/pong between them, but either way you need to come up with some intelligent strategy for writing the data, and erasing, and to limit the erase/write cycles so you don't damage the memory.

The erase time for 128KB sectors is several seconds. If you are running from FLASH, this will stall out the processor while the erase or programming occurs.

Depending on the size of you data storage requirements a small sector may be more appropriate.

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