Daniel Beer Atom | RSS | About

LPC54113 programming via OpenOCD

24 Sep 2021

The LPC54113 doesn’t currently have direct support for flash programming in OpenOCD, but it can be made to work purely via Tcl.

The LPC family of chips don’t expose their flash programming hardware directly. Instead, they provide a function in ROM with the signature:

IAP(const uint32_t *request, uint32_t *result);

Each argument points to a 5-element array. The offset of the function varies from chip to chip. By filling the request array with appropriate values, you can ask the ROM to erase pages or sectors, or copy blocks of RAM to flash.

Invoking the function via the debugger is easy enough. Just set up the argument registers, and set PC at the function address. LR can be set to a known breakpoint to have the chip break on return.

Unfortunately there is a slight difficulty: immediately on reset, offset 0 is remapped to point at the start of the boot ROM. If the boot ROM decides to boot from flash, then it will later remap this to point at flash. But, until this happens, it’s not possible to program sector 0 (it is still possible to do a mass erase).

Now, we could allow the chip to run for a while so that offset 0 gets remapped. But we can’t always do this. It may be that the currently installed firmware locks up and resets, or that the chip is erased, so that the boot ROM decides not to attempt a flash boot.

Our problem would be solved if we had a way to put the chip into a known state where it boots and doesn’t misbehave, and if we were able to do this without initially requiring access to sector 0. Fortunately, we do have such a way.

The LPC54113 supports an A/B update scheme via an image format it calls a dual-enhanced image. These are described in the reference manual, but the important fact about this image format for us is that a dual-enhanced image need not be located in sector 0.

Our programming procedure is therefore:

See the following Tcl script for an example of how to do this:

Comments in the script describe the procedure in more detail. Note that the final step is performed by loading the entire image into a block of SRAM and copying from there. You may need to modify this for images greater than 32kB in size.