Page 1 of 1

Self Programming Internal Flash - SAM3U4E

Posted: Wed Aug 21, 2013 6:00 pm
by jkellar
I think I'm missing something fundamental here. I have code stored in an external memory and loading it page by page to be programmed into the internal flash (Flash0 in particular). I have been reviewing the ASF example and what I'm doing looks right but I'm getting nothing happening in the flash. If I step through the area where the writing actually takes place nothing gets hung up whereas full speed it jumps to a strange location in code and stops, so getting weirdness. Still nothing ever shows up as written to the flash verifying through the debugger. Incidentally the EFC Status register shows success without error when stepping through. Code snippet is below:

Code: Select all

/* Program internal Flash */
	iflash_ptr = (uint32_t *)(IFLASH0_ADDR + IFLASH0_APP_OFFSET);
	ipage_idx = IFLASH0_PAGE_BEGIN;
	for(pagecount = CODEPAGE_BEGIN; pagecount < (CODEPAGE_BEGIN + pages); pagecount++) {
		/* Clear alignment buffer */
		for(alignedcount = 0; alignedcount < (IFLASH0_PAGE_SIZE / sizeof(uint32_t)); alignedcount++) {
			aligned_buffer[alignedcount] = 0;
		/* Get next page of data and place in alignment buffer */
		memory_read(pagecount, 0, DATABUFFER_SIZE, databuffer);
		for(alignedcount = 0, bytecount = 0; alignedcount < (IFLASH0_PAGE_SIZE / sizeof(uint32_t)); alignedcount++) {
			aligned_buffer[alignedcount] = databuffer[bytecount++];
			aligned_buffer[alignedcount] |= (uint32_t)databuffer[bytecount++] << 8;
			aligned_buffer[alignedcount] |= (uint32_t)databuffer[bytecount++] << 16;
			aligned_buffer[alignedcount] |= (uint32_t)databuffer[bytecount++] << 24;
		/* Program aligned buffer into internal flash then apply write command */
		for(alignedcount = 0; alignedcount < (IFLASH0_PAGE_SIZE / sizeof(uint32_t)); alignedcount++) {
			*iflash_ptr++ = aligned_buffer[alignedcount];
		EFC0->EEFC_FCR = EEFC_FCR_FKEY(0x5a) | (ipage_idx << 8) | IFLASH_EWP;
		status = 0;
			status = EFC0->EEFC_FSR;
			; // Error writing
		/* Increment internal flash page index for next page programming */
If anyone can shed some light on this it would be much appreciated. I've read the manual up and down trying to figure out precisely how this should all work and I'm not finding anything new. Using Studio 6 for development and debug.

Re: Self Programming Internal Flash - SAM3U4E

Posted: Mon Aug 26, 2013 11:13 pm
by jkellar
Wow...I'm surprised nobody has a comment on my post. Doesn't anyone know how to program the SAM3U4E Flash without using ASF?

Re: Self Programming Internal Flash - SAM3U4E

Posted: Tue Aug 27, 2013 4:21 am
by jharley
See the following for hints. ... n-Note.pdf

Also, for the Sam3U4 this is a 2 bank flash. So your code that performs the write needs to run from RAM or the other bank so it is not writing to the same bank that code is executing from.

Alternatively you try using the IAP function/feature that is located in the ROM of the part to perform the Writes (this would allow writing to either bank) and the heavy lifting is already done for you. Search the document that is linked (or in the Datasheet) for "IAP" for an example of its use.

Re: Self Programming Internal Flash - SAM3U4E

Posted: Wed Aug 28, 2013 1:45 pm
by jkellar
Excellent! Exactly what I needed to see. Glossed over that IAP paragraph numerous times. Using the IAP in ROM works perfectly. Thanks!

Re: Self Programming Internal Flash - SAM3U4E

Posted: Tue Feb 23, 2016 6:43 pm
by guicit
Hi jkellar,
i'm trying to write a code for a sam3x8e, to perform an update of the firmware of my sam3x, reading the new firmware from a usb pen drive and store it in flash.

at the moment i read the file and i try to use the flash_write function, wrinting on the second flash bank and at the end of the process i set the gpnvm(2) bit, this to use the dual bank boot capabilities of sam3x but I'm encountering some difficulties and no way to have something working.

i cannot found any example of IAP routine

do you realize something similar for your project?

can you help me?