help! sam3s4 can not write flash

Discussion around product based on ARM Cortex M3 core.

Moderators: nferre, ncollot

ielectron
Posts: 1
Joined: Thu Aug 18, 2011 10:43 pm

help! sam3s4 can not write flash

Tue Aug 30, 2011 9:35 pm

Hi,

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).*/
lastPageAddress = IFLASH_ADDR + IFLASH_SIZE - IFLASH_PAGE_SIZE;
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;
}
ramonf
Posts: 12
Joined: Mon Aug 08, 2011 5:59 pm

Re: help! sam3s4 can not write flash

Sun Apr 29, 2012 7:53 am

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.
incognito
Posts: 32
Joined: Tue Jul 13, 2010 12:54 pm

Re: help! sam3s4 can not write flash

Sun Apr 29, 2012 3:06 pm

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."
incognito
Posts: 32
Joined: Tue Jul 13, 2010 12:54 pm

Re: help! sam3s4 can not write flash

Sun Apr 29, 2012 3:12 pm

forum/viewtopic.php/f,24/t,20365/

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

Return to “SAM3 Cortex-M3 MCU”

Who is online

Users browsing this forum: Baidu [Spider] and 3 guests