Monday, July 4, 2011

How to generate .hex files from Code Red's LPCXpresso to use with FlashMagic

Recently I struggled to be able to use with the LPC serial bootloader and FlashMagic with the LPCXpresso software from Code Red. All I wanted was a .hex file to load onto an LPC1759. LPCXpresso generates a binary AXF file. FlashMagic requires an ihex file. The hex file actually is just the binary file converted to an ascii file. Unfortunately, due to the rich features of LPCXpresso, you have to dig to figure out how to automatically convert the .axf into a .hex. These steps should bring you a step closer to flashing code to your LPC chip over serial.

Official Code Red and LPCXpresso Documentation

Code Red provides two links with information on how to convert your program into a usable .hex for FlashMagic.

  1. Generating srec (Motorola S format), binary, ihex (Intel Hex) files
  2. Post-processing your linked application

Changing the project settings

In order to begin, you need to open your project settings. Once the project properties are open, choose 'C/C++ Build' -> 'Settings'. On the right, select the 'Build Steps' tab.

Now, edit the 'Post-build steps' command and add the steps to create a .hex that FlashMagic can use.

Steps to go from a ELF to a FlashMagic compatible HEX file

I got these steps from a thread on NXP's user forum. They agree with the knowledge base article.

Add the following steps to your Post-build Steps -> Commands
  1. ELF=>BIN without CRC:

    arm-none-eabi-objcopy -O binary "${BuildArtifactFileName}" "${BuildArtifactFileBaseName}.bin";

  2. BIN=>BIN with CRC (this is useful for USB ISP mode with LPC13xx parts):

    checksum -v "${BuildArtifactFileBaseName}.bin";

  3. BIN with CRC => HEX with CRC:

    arm-none-eabi-objcopy -I binary "${BuildArtifactFileBaseName}.bin" -O ihex "${BuildArtifactFileBaseName}.hex";

  4. Save your project properties, and then repeat for your other Build Configurations (Debug/Release).

Now, when you rebuild your project, LPCXpresso should generate a .hex file that FlashMagic will be able to use.

Next, getting your code to actually run...


  1. It helps to add " around the filename parts, otherwise you can have problems with paths including spaces.

    1. Good call. I've always avoided spaces and special characters from my command line days so I've never run into this. I'll update my instructions to reflect it.

  2. Wonder Ful. Excellent, Cool, Good Job. Thanks.
    Succeeded to make HEX with CodeRed LPC Expresso

  3. I cant get hex file.... what is this error

    Invoking: MCU Archiver
    arm-none-eabi-ar -r "libUARThome.a" ./src/main.o ./src/serial.o
    c:\nxp\LPCXpresso_5.0.14_1109\lpcxpresso\tools\bin\arm-none-eabi-ar.exe: creating libUARThome.a
    Finished building target: libUARThome.a

    make --no-print-directory post-build
    Performing post-build steps
    arm-none-eabi-objcopy -I binary "libUARThome.bin" -O ihex "libUARThome.hex";
    c:\nxp\LPCXpresso_5.0.14_1109\lpcxpresso\tools\bin\arm-none-eabi-objcopy.exe: 'libUARThome.bin': No such file
    make[1]: [post-build] Error 1 (ignored)

    1. Sounds like you are trying to convert a library file to a HEX file. This won't work, because a library is not a valid ELF executable. You only need to convert your final executable (that has been linked to your libUARThome.a) into the HEX that you can then transfer to your target.

  4. Great..!!! It worked thanks a lot..!!! But the I found like, the CRC mismatches. It throws me error like checksum command not found. Which exe do I link the checksum..? Objcopy..?

  5. I am getting this error
    make: *** [SPI_Master.axf] Error 1
    How am I gonna resolve it????

  6. I am getting this error.
    Please help me out

    Building target: SPI_Master.axf
    Invoking: MCU Linker
    arm-none-eabi-gcc -nostdlib -L"C:\ALL_LPC1768\SPI_Workspace\CMSISv2p00_LPC17xx\Debug" -Xlinker -Map="" -Xlinker --gc-sections -mcpu=cortex-m3 -mthumb -T "SPI_Master_Debug.ld" -o "SPI_Master.axf" ./src/cr_startup_lpc176x.o ./src/main.o -lCMSISv2p00_LPC17xx
    c:/nxp/lpcxpresso_5.2.6_2137/lpcxpresso/tools/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib/armv7-m\libcr_c.a(fpprintf.o): In function `printf':
    fpprintf.c:(.text.printf+0x3a): undefined reference to `__sys_write'
    c:/nxp/lpcxpresso_5.2.6_2137/lpcxpresso/tools/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib/armv7-m\libcr_c.a(_deferredlazyseek.o): In function `__flsbuf':
    _deferredlazyseek.c:(.text.(float, long, short, bool, float __restrict)+0x8a): undefined reference to `__sys_istty'
    c:/nxp/lpcxpresso_5.2.6_2137/lpcxpresso/tools/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib/armv7-m\libcr_c.a(_writebuf.o): In function `_Cwritebuf':
    _writebuf.c:(.text._Cwritebuf+0x16): undefined reference to `__sys_flen'
    _writebuf.c:(.text._Cwritebuf+0x2a): undefined reference to `__sys_seek'
    _writebuf.c:(.text._Cwritebuf+0x52): undefined reference to `__sys_write'
    c:/nxp/lpcxpresso_5.2.6_2137/lpcxpresso/tools/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib/armv7-m\libcr_c.a(alloc.o): In function `malloc':
    alloc.c:(.text.malloc+0x70): undefined reference to `_sbrk'
    alloc.c:(.text.malloc+0x90): undefined reference to `_sbrk'
    c:/nxp/lpcxpresso_5.2.6_2137/lpcxpresso/tools/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib/armv7-m\libcr_c.a(alloc.o): In function `_Csys_alloc':
    alloc.c:(.text._Csys_alloc+0xe): undefined reference to `__sys_write'
    alloc.c:(.text._Csys_alloc+0x12): undefined reference to `__sys_appexit'
    c:/nxp/lpcxpresso_5.2.6_2137/lpcxpresso/tools/bin/../lib/gcc/arm-none-eabi/4.6.2/../../../../arm-none-eabi/lib/armv7-m\libcr_c.a(fseek.o): In function `fseek':
    fseek.c:(.text.fseek+0x1a): undefined reference to `__sys_istty'
    fseek.c:(.text.fseek+0x3c): undefined reference to `__sys_flen'
    collect2: ld returned 1 exit status
    make: *** [SPI_Master.axf] Error 1