S70 with FreeRTOS: Writing new firmware to Flash

Discussions around product based on ARM Cortex M7 core.
This forum will be discontinued soon.

Moderator: nferre

eduardk
Posts: 3
Joined: Fri Sep 02, 2016 10:00 am

S70 with FreeRTOS: Writing new firmware to Flash

Fri Sep 02, 2016 10:24 am

Hi guys,

I'm trying to add in-field firmware update to a product running on an S70. Firmware is in the Flash and normally executes from the Flash. It downloads new image via a custom USB link into SRAM, calls a "RAMFUNC" which needs to do the diry work of erasing the flash and writing the new image to Flash before resetting.

This application does have the complication of also running FreeRTOS. When the new firmware is ready, the RAMFUNC-function will 'enter critical' and disable interrupts before proceeding.

I have found that the processor stalls during the flash erase though. Code snippets follow:

Code: Select all

RAMFUNC void UPDATER_Write_Image_to_Flash(void)
{
...
	/* Decode the image without writing to flash */
	_result = UPDATER_DecodeImage((int16_t)0);

...

	/* Disconnects the USB: */
	udc_detach();
	portable_delay_cycles(5000000);

	/* Disable Interrupts */
	taskENTER_CRITICAL();
	portable_delay_cycles(20000000);

	cpu_irq_disable();
	
	PIOD->PIO_CODR = 1<<19;	/* clear TP6 */
	PIOB->PIO_CODR = 1<<12;	/* clear TP7 */

	/* Erase the flash */
	EFC->EEFC_FCR = 0x5A000005;		/* Erase ALL */
	while ((EFC->EEFC_FSR & 1) == 0)
	{
		/* wait for Flash erase to complete */
		PIOD->PIO_CODR = 1<<10;
		portable_delay_cycles(5000000);
		PIOD->PIO_SODR = 1<<10;
		portable_delay_cycles(5000000);
	}

	PIOD->PIO_SODR = 1<<19;	/* set TP6 */

...

}
The firmware first does a dummy decode of the image (including CRC checks etc) to make sure it is able to use it. Then it disables the USB link (which now stops my easy debug :( ), followed by enterring a critical section (which I believe stops FreeRTOS from trying to do context switches) and disabling interrupts.

Next the firmware clears two test points (drive them low). This works.

Then it erases the flash (which works because the flash is perfectly erased) and toggles an LED in a loop until the erase operation is complete. The LED does toggle, but then stops.

After the loop one of the test points is driven high again. This does NOT happen.

So clearly the micro has jumped from here. It's not watchdog because the watchdog is disabled in the init code and is never serviced by the firmware. So it must be either an interrupt or an exception.

But I expect interrupts to be disabled. And what would cause an exception inside of that loop?

Please, what am I missing here?

portable_delay_cycles() is the ASF Cycle Counter function which is defined as "OPTIMIZE_HIGH" and "RAMFUNC".

cpu_irq_disable() is a #define which calls "__disable_irq()" and "__DMB()":
__disable_irq() is an inline function that executes cpsid


Thanks in advance,
Eduard
tsvoipio
Posts: 54
Joined: Wed Aug 19, 2015 9:44 pm

Re: S70 with FreeRTOS: Writing new firmware to Flash

Fri Sep 02, 2016 6:08 pm

You may be starting the ready poll so soon that the EFC has not turned the ready bit off yet. I have a EFC flash writing function on SAM4S and SAM4E. The code inserts one dsb instuction between the command register write and the poll loop.
eduardk
Posts: 3
Joined: Fri Sep 02, 2016 10:00 am

Re: S70 with FreeRTOS: Writing new firmware to Flash

Mon Sep 05, 2016 10:16 am

Thanks for the tip, tsvoipio.

I've added a "__DSB();" between the write to the EEFC_FCR and the while-loop. Unfortunately that didn't seem to make any difference - I'm still hunting for the elusive ...
tsvoipio
Posts: 54
Joined: Wed Aug 19, 2015 9:44 pm

Re: S70 with FreeRTOS: Writing new firmware to Flash

Mon Sep 05, 2016 4:32 pm

If I were you, I'd check that the generated machine code is compiled and linked/located for running in the RAM.

If you're using the GNU compiler toolkit (gcc and derivatives), the assembly listing of the module containing your function is available adding '-Wa,-ahlms=update.lst' to the gcc command line. The linked/located ELF file can be dissected by:
arm-none-eabi-objdump -D myrunfile.elf >myrunfile.dis

Substitute your GNU toolset prefix for arm-none-eabi-.
eduardk
Posts: 3
Joined: Fri Sep 02, 2016 10:00 am

Re: S70 with FreeRTOS: Writing new firmware to Flash

Tue Sep 06, 2016 9:23 am

I've previously tested that with a function hook and some assembly code - but, you're right, it is worth double checking.

I'm also now working on an alternative strategy where I don't try to write to the Flash from inside a FreeRTOS task - just in case the FreeRTOS port has messed up the core somehow.

I'll report back once I know what the problem is and/or once I have a working solution. Any other ideas in the meantime WILL be appreciated!

Return to “SAM Cortex-M7 MCU”

Who is online

Users browsing this forum: No registered users and 1 guest