0
votes

i using flash to save data, but sometime error happen when Erasing Flash, i don't understand why?

Thank for your help! sorry for my terible english

my program size: 30,46 kbyte.

Here is my function:

#define FLASH_PAGE_ADDR   0x08010000

uint16_t Mydata = 2345;

void WriteData(void)
{
    FLASH_Unlock(); 
    FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR\
    |FLASH_FLAG_WRPRTERR);
    FLASH_ErasePage(FLASH_PAGE_ADDR);
    FLASH_ProgramHalfWord(FLASH_PAGE_ADDR+2, Mydata);
}
2

2 Answers

1
votes

You chip has 64kB of flash memory, so trying to erase a page that does not exist (starting 64kB after start of flash) is not the best idea. Trying to do that may as well erase the FIRST page of flash, removing interrupt vectors and part of the running application.

-1
votes

The device stm32F103C8T8 contains 64Kb of flash.

 Ordering information scheme

And the memory layout looks like this:

Memory map

For your device the flash memory will actually be 0x08000000-0x0800FFFF since you got the 64Kb variant. This memory will be our first page (aka sector) of flash memory. There are 64 pages for your device each being 1Kb in size. You can only erase full pages. See below picture for flash module organization:

flash module organization

In your example you tell the device to start erasing from 0x08010000. With the information given above, this is of course not possible (since there is no memory at this location). I suggest you change the location so that it will target a valid page (staring at the beginning of a page):

#define FLASH_PAGE_4_ADDR   0x08001000

Make sure that you are not executing from that very spot when erasing, since then, of course, your program will crash.

Also, consider looking at the return value from your functions that you use to erase with. They could tell you something important.