cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to write to flash memory

sue
Associate III
Posted on August 06, 2010 at 04:00

Unable to write to flash memory

20 REPLIES 20
chikos332
Associate II
Posted on May 17, 2011 at 14:01

Hi,

The erase value for the flash is 0xFF. If you write any other value then try to write again, you will have either programming error or the value will not be correctly written.

And as far as I know, the erase can be done only by Page (or mass erase) so you have to erase the page before attempting to write anything into it.

Do you check on BUSY flag between two operations?

Are you sure you are writing in 16-bit aligned addresses?

Have you the right voltage on your board, and is it stable?

The right sequence is :

ERASE:

 - check busy flag (wait till it is reset)

 - Set PER bit to enable the erase

 - write the address of the page to be erased into FLASH_AR register (the entire page containing the address will be erased).

 - Set STRT bit to trigger the erase

 - Wait till Busy flag or STRT flag is cleared by hardware (it takes a time)

 - Read and verify that the location is erased (should have 0xFFFF)

WRITE:

 - Unlock the flash

 - Wait on BUSY

 - Set the PG bit

 - Write to destination (16-bit in a half-word aligned address)

 - Wait on BUSY

Cheers.

swhite2
Associate III
Posted on May 17, 2011 at 14:01

You can only program flash bits from 1 to 0. This is generic to flash memory technology and is not specific to the STM32.

Typically you erase the whole page (1K or 2K depending on the STM32) of flash and then write your data.

The PRGERR is probably set because the half-word you're attempting to program has a zero bit where the value you want to program has a 1 bit.

BTW are you writing to the flash registers yourself or are you using ST's

http://www.st.com/stonline/products/support/micro/files/stm32f10x_stdperiph_lib.zip

? That contains examples of flash operations.

stforum2
Associate II
Posted on May 17, 2011 at 14:01

Specific to the STM32, if ANY bit is already 0 then the write process is aborted and PGERR is set (unless writing 0X0000).

daviddavid92
Associate II
Posted on May 17, 2011 at 14:01

My advice is to model your code on the STM32 Flash examples from ST. I did a FAT filesystem in internal Flash basing the memory writes on their code, and it worked perfectly!

I used the STM32 libs as well as they hide all the nasty bit-banging that goes on, and provide a nice interface.

Chris.

sue
Associate III
Posted on May 17, 2011 at 14:01

Are you saying that EVERY time I want to change a byte of flash memory I have to erase the whole PAGE?  How then do you preserve what is already there?  Or am I missing something?

sue
Associate III
Posted on May 17, 2011 at 14:01

No wonder I'm confused.  Page 16 of the Flash Programming Manual says to write zeros, not ff's.

Yes, I am checking the BSY bit between operations.

But  I ask again, what is the point of having data in flash memory if the only way you can write some more is by erasing what is already there and that you want to keep?

Cheers

sue
Associate III
Posted on May 17, 2011 at 14:01

Stuart, thanks for your reply.

Are you saying that if I want to write 8 bits, say 10011011, and you can only turn bits off, how then do you get the pattern above into memory?  Sorry, but I am a bit

confused by this.

sue
Associate III
Posted on May 17, 2011 at 14:01

Crossware, thanks for your reply.

What you are saying seems to be the opposite of what Stuart said, which was you can only change flash memory bits from 1 to zero.  I am getting more confused!

sue
Associate III
Posted on May 17, 2011 at 14:01

Stuart, I am writing in Assembler, not C.  But I thought I would go and have a look at what the library consists of but that page on the St site doesn't work.  Probably wouldn't be of any use anyway unless the source is there.  I don't speak C but maybe I could translate.  Anyway, the page doesn't work, so back to the drawing board.

Cheers