SAM4S: Booting from 2nd flash bank

Discussion around product based on ARM Cortex M4 core.

Moderators: nferre, ncollot

Posts: 2
Joined: Wed Sep 24, 2014 10:03 am

SAM4S: Booting from 2nd flash bank

Wed Sep 24, 2014 12:46 pm

Hi all,

as this is my first post, I'd first like to thank all of the contributors here whose posts greatly helped me wrap my MCU newbie head around the ins & outs of the SAM4 platform and MCUs in general :).
I'm working with a SAM4S Xplained Pro with the intention of developing a USB device capable of updating its own firmware. I implemented a vendor USB device which is capable of (among other things) receiving a firmware image which then gets written to the second flash bank.
So far everything works great, except for the reboot.
I've tried two approaches after having written the firmware (using AS 6.2 with ASF 3.17.0):

Code: Select all

        flash_set_gpnvm(1);  // boot from flash
        flash_set_gpnvm(2);  // switch to FLASH_1
This ends in a crash for the currently running firmware after setting GPNVM 2 (boot from FLASH_1), which does not seem too surprising, but a hard reset does not boot, neither from FLASH_0 nor from FLASH_1.
Second approach after unsetting GPNVM2 (I know that on hard reset the firmware on FLASH_0 will boot, but I'd expect this piece of code to jump to the FW image on FLASH_1):

Code: Select all

	if (*((uint32_t*)IFLASH1_ADDR) == 0xffffffff)
		printf("No firmware present.");
	void (*app_entry)(void);
	__set_MSP(*(uint32_t*)IFLASH1_ADDR); // reset SP
	SCB->VTOR = ((uint32_t) IFLASH1_ADDR & SCB_VTOR_TBLOFF_Msk); // relocate vector table
	app_entry = (void(*)(void)) (unsigned*)(*(unsigned*)IFLASH1_ADDR+4);
The call to app_entry() crashes to the Dummy handler (even when I make app_entry global). Not happy.
From what I understand, the first approach should be the most straightforward as setting GPNVM bit 2 just remaps FLASH_1 to 0x400000, so a reboot with GPNVM 1 and 2 set should jump to the start of FLASH_1 and execute whatever is there. The second one should have the same effect, replacing the effect of remapping the flash planes by resetting the stack pointer and vector table to the current address of FLASH_1. Only it does not, as stated above :?.
Am I missing something? And if so, what am I missing?

Confusedly yet hopefully,

Posts: 2
Joined: Wed Sep 24, 2014 10:03 am

Re: SAM4S: Booting from 2nd flash bank

Tue Sep 30, 2014 1:03 pm

Okay, I should probably have been less verbose here ;). My issue is really with the non-working boot after switching flash banks, to which I don't seem to find a fitting answer in any of the online or offline sources.

What I don't understand is that the same firmware image which boots from FLASH_0 won't boot from FLASH_1 after remapping the flash regions via GPNVM bit 2 (assuming GPNVM 1 is set for boot from flash, of course).

Setting GPNVM 2 in the SAM4SD32C maps the FLASH_1 start address to 0x400000, according to the documentation.
So the same firmware image as resides in FLASH_0 should be bootable from FLASH_1 as soon as GPNVM 2 is set and the system reset. It shouldn't be necessary to change the linker script as the address space does not change, it's only a different memory that gets mapped there. Why doesn't it boot from the second flash bank, then, <mild expletive/>?
Posts: 1
Joined: Tue Feb 10, 2015 11:09 am

Re: SAM4S: Booting from 2nd flash bank

Tue Feb 10, 2015 11:16 am

Hi All,

I got the same Problem. Is there any example Code available?
So far I couldn't find any help in the web. I'm also using the SAM4sd32c. Some ideas?

Return to “SAM4 Cortex-M4 MCU”

Who is online

Users browsing this forum: No registered users and 3 guests