Programing NXP LPC via USB as a flash disk (USB ISP MSC)

Hi all,

I have an LPC1549, that can be started in USB MSC mode by pulling the right pins up&down during reset. This all works, I see my device as an USB flash disk containing a 256K firmware.bin file. Now I want to be able to generate a firmware.bin file with platformio that can be used to program my LPC. I know platformio already generates a firmware.bin file, but that file is 32MB large, and cannot be uploaded to the flash disk. So I tried to generate my own firmware.bin file using this command: ‘objcopy.exe firmware.elf -O binary -R.heap -R.bss -R.stack_dummy firmware.bin’. Works fine, and strips the RAM parts of the firmware at adress 0x2000000 and higher, so smaller file; nice. Still it does not work when I upload the file to the USB flashdisk. When comparing a ‘working file’ (created by programming LPC via SWD) and a ‘non-working file’, it seems that at offset 0x1C to 0x1F the working file contains ‘4A 14 FF FD’, the non-working file contains only zeros there. How can I make my compiler and/or linker command to get those bytes in the right value?

Without doubt, that is the checksum missing from the image.

LPC microcontrollers have an additional checksum in the binary over the first 7 entries of the vector table. That was e.g. discussed in LPC 1768 Flashing and Debugging - #5 by valeros.

You can also extend PlatformIO with this logic. For that, add to the platformio.ini the line

extra_scripts = extra_script.py

and then in the root of the project, create a extra_script.py file with the content from lpc1768_blinky_example/extra_script.py at fe34b7b44271502b961fa4e79e1bb30180092133 · jxsl13/lpc1768_blinky_example · GitHub

But comment out all lines after # fix openocd target because that’ll re-target the upload method to OpenOCD, which you probably don’t want.

In PlatofrmIO you can also say

upload_protocol = mbed

and that is basically uploading the .bin file to the available USB disk (as for all mbed-enabled CMSIS-DAP boards)

I’ll open an issue about this, LPC11U35 does not work well? · Issue #6 · platformio/platform-nxplpc · GitHub was closed prematurely without the right fix.

Issue is open in Add LPC checksum in .bin file · Issue #25 · platformio/platform-nxplpc · GitHub.

Wow, that’s quick! Thanks for the reply! For the LPC15xx series it seems also the bin file is containing sections from the ELF that should not be there because they increase the size of the bin to 32MB. Using the flags “-R.heap -R.bss -R.stack_dummy” on objcopy fixes this, should this also be added to the issue on github? For now I have created an extra_script that uses your checksum function to create a firmware_FLASH.bin;

(only last part of extra_script):

# Custom BIN from ELF
env.AddPostAction(
        "$BUILD_DIR/${PROGNAME}.elf",
        env.VerboseAction(" ".join([
                    "$OBJCOPY",
                    "-O",
                    "binary",
                    "-R.heap",
                    "-R.bss",
                    "-R.stack_dummy",
                    "$TARGET",
                    "$BUILD_DIR/${PROGNAME}_FLASH.bin"
                ]), "Building USB-flash ready bin file from $TARGET"))
              
# fix checksum
env.AddPostAction("$BUILD_DIR/${PROGNAME}.elf", post_elf_file)

Yes please, but that must be a separate issue in that platform. There’s the code

Does it work that way with the checksum added? I see you’re creating a fixed firmware_FLASH.bin there (which will not be what’s used during the “Upload” procedure, there it will use firmware.bin – you might want to try and modify / fix the existing .bin file)

I created firmware_FLASH.bin because I didn’t know if it would break something if I would use the default firmware.bin. Will change to firmware.bin now.

1 Like

Here is the issue raised on github: Fix bin size of LPC15xx devices · Issue #26 · platformio/platform-nxplpc · GitHub

1 Like