Self Programming Internal Flash - SAM3U4E

Discussion around product based on ARM Cortex M3 core.
This forum will be discontinued soon.

Moderators: nferre, ncollot

jkellar
Posts: 13
Joined: Tue May 28, 2013 6:29 pm

Self Programming Internal Flash - SAM3U4E

Wed Aug 21, 2013 6:00 pm

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;
		while(!status)
			status = EFC0->EEFC_FSR;
		if(EFC0->EEFC_FSR & EEFC_FSR_FCMDE) {
			; // Error writing
		}
		indicateProgramming();
		/* Increment internal flash page index for next page programming */
		ipage_idx++;
	}
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.
jkellar
Posts: 13
Joined: Tue May 28, 2013 6:29 pm

Re: Self Programming Internal Flash - SAM3U4E

Mon Aug 26, 2013 11:13 pm

Wow...I'm surprised nobody has a comment on my post. Doesn't anyone know how to program the SAM3U4E Flash without using ASF?
jharley
Posts: 238
Joined: Thu Dec 06, 2012 6:40 am

Re: Self Programming Internal Flash - SAM3U4E

Tue Aug 27, 2013 4:21 am

See the following for hints.
http://www.atmel.com/Images/Atmel-42141 ... 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.
jkellar
Posts: 13
Joined: Tue May 28, 2013 6:29 pm

Re: Self Programming Internal Flash - SAM3U4E

Wed Aug 28, 2013 1:45 pm

Excellent! Exactly what I needed to see. Glossed over that IAP paragraph numerous times. Using the IAP in ROM works perfectly. Thanks!
guicit
Posts: 2
Joined: Thu Feb 18, 2016 4:35 pm

Re: Self Programming Internal Flash - SAM3U4E

Tue Feb 23, 2016 6:43 pm

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?
Thanks

Return to “SAM3 Cortex-M3 MCU”

Who is online

Users browsing this forum: No registered users and 3 guests