cancel
Showing results for 
Search instead for 
Did you mean: 

Writing to internal Flash

gurnett2
Associate II
Posted on April 09, 2013 at 18:26

I'm trying to write to the internal flash of a STM32F103ZE. I've been using the following code

FLASH_Unlock();

uint32_t Start=0x8008000;

uint32_t End =0x800C000;

NbrOfPage = (End - Start) / FLASH_PAGE_SIZE;

 

/* Clear All pending flags */

FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);

 

/* Erase the FLASH pages */

for(EraseCounter=0; EraseCounter<NbrOfPage && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)

{

  FLASHStatus = FLASH_ErasePage(Start + (FLASH_PAGE_SIZE * EraseCounter));

}

uint32_t Address = Start;

while((Address < End) && (FLASHStatus == FLASH_COMPLETE))

    {

        FLASHStatus = FLASH_ProgramWord(Address, Data);

        Address = Address + 4;

    }

    FLASH_Lock();

However, this results in a visit to the hardfault_handler. Are there some other steps required to get this working? I'm using crossworks.

Thanks

-Michael
1 REPLY 1
Posted on April 09, 2013 at 20:12

Doesn't look too objectionable. Make sure you don't erase memory you are currently executing from.

Diagnose the cause of the Hard Fault, understand the registers and processor state when it faulted. If you understand where, and why, it faulted you'd be in a better position to solve the problem.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..