Page 1 of 1

help! sam3s4 can not write flash

Posted: Tue Aug 30, 2011 9:35 pm
by ielectron

I am using sam3s4 for one of my design, I need to write some data into the flash for the data backup in case of power lost. Whenever I try to write or read the flash, my code will stop working. With the debugger, I can see it always goes to a infiniti loop, HardFault_Handler. I use the atmel library to write my code, the plateform is iar for arm v5.50 + j-link v8.0.

I really appreciate for your advice.

uint8_t WriteFlash(void)
uint32_t i;
uint8_t error=0;
uint32_t lastPageAddress;
volatile uint32_t *pLastPageData;

/* Performs on last page (to avoid overriding existing program).*/
pLastPageData = (volatile uint32_t *) lastPageAddress;
/* Unlock page */
error = FLASHD_Unlock(lastPageAddress, lastPageAddress + IFLASH_PAGE_SIZE, 0, 0);

/* Write page */
error = FLASHD_Write(lastPageAddress, NVram, IFLASH_PAGE_SIZE);

/* Check page contents */
for (i=0; i < (IFLASH_PAGE_SIZE / 4); i++)
if (NVram[i] != pLastPageData[i]) return (1) ;

/* Lock page */
error = FLASHD_Lock( lastPageAddress, lastPageAddress + IFLASH_PAGE_SIZE, 0, 0 ) ;

/* Set GPNVM bit 0 (security bit) */
error = FLASHD_SetGPNVM( 0 ) ;

return error;

Re: help! sam3s4 can not write flash

Posted: Sun Apr 29, 2012 7:53 am
by ramonf
I know that that I'm answering this message after a long time.
but I had the same error, and I solved it by disabling all interrupts just before writing to flash, and enabling them again when I'm done.
I don't know why it happens, but this trick worked for me.

Re: help! sam3s4 can not write flash

Posted: Sun Apr 29, 2012 3:06 pm
by incognito
The function writing to flash should be run from SRAM, or a memory plane different to the one being programmed/written.

The datasheet states "Commands and read operations can be performed in parallel only on different memory planes.
Code can be fetched from one memory plane while a write or an erase operation is performed
on another."

Re: help! sam3s4 can not write flash

Posted: Sun Apr 29, 2012 3:12 pm
by incognito

It may be that you already have the write function running out of SRAM, but one or another function like printf for debug jumping back to flash operation before write access is finished