Electronic – Programming SRAM over SWD

armcortex-mcortex-m3embeddedmicrocontroller

I'm attempting to program a Cortex M3 based microcontroller over SWD, more or less from first principles. I've written an SWD interface library and have it running on a second micro.

I've largely been following this blog post: http://markdingst.blogspot.ie/2014/03/programming-internal-sram-over-swd.html

As well as this SiLabs application note:
https://www.silabs.com/Support%20Documents/TechnicalDocs/AN0062.pdf

I can halt the core and read and write SRAM. I'm wondering how to obtain the bare-metal code that I can write to SRAM. Can I just take the .hex file the compiler outputs and write that to SRAM word by word starting at 0x20000000?

And if so, what core registers do I have to configure so that the new program in SRAM will run on reset? Will setting the PC to 0x20000000 be enough?

I appreciate all and any help! Thanks!

Best Answer

If you have a linker script which links your executable to an appropriate address for ram, then you could interpret the hex file and extract the payload bytes.

But it would probably be easier to use arm-none-eabi-objcopy, ie

arm-none-eabi-objcopy -O binary myproject.elf myproject.bin

This will give you a raw binary file that you can write to the chip starting at the base address you linked for. Note that in doing things this way the base address is not preserved in the file itself - you will have to make sure you keep that consistent between the file creation and the file usage. Still, this is a fairly common choice - many simple flashing programs only understand raw binaries, with the destination address given explicitly on the command line.

To fully run from RAM, you need a chip that lets your relocate the vector table there; some do and some do not. If you can do that in a way that survives reset and have the reset vector in the table pointed to your code in RAM it may work. Or your could write a table pointing the reset vector at RAM to the usual location in flash. Keep in mind that the use of resetting to RAM is a bit limited, as there normally would be no program there to execute, unless something put it there. And the something can do a little cleanup and just branch to the starting address.

Related Topic