3

I am building a firmware updater for an STM32 MCU. I have so far programmed bootloader software on the device, separate from the main application in FLASH.

What I need to do is generate a binary file which will be the replacement code for the main application in FLASH. This means I can transfer the file over UART and overwrite the main application. How do I go about producing such a file?

The code was programmed using the stm32CubeIDE which generates an .elf file after building. I will add a header to this binary code before transmitting over UART.

Thank you very much in advance for your help,

Harry

6
  • How do I go about producing such a file? - the same way you would go producing any other firmware. Write source files, compile and link. have so far programmed bootloader software - what/which software?
    – KamilCuk
    Jul 13, 2019 at 9:15
  • I guess what I'm asking is that after compilation and linking, an executable file is usually generated. However, in this case a .elf file is generated as it's going to the MCU. Is this .elf file the equivalence of the .exe file such that if the binary was transferred to the MCU and loaded, the program would run?
    – arry_h
    Jul 13, 2019 at 9:41
  • 2
    No. .elf is ELF, .exe is executable for windows binary file is binary and what you want is dependent on what the "programmed bootloader software" expects to get. And most probably it wants a hex file, but it depends on what bootloader software you programmed. And what protocol the software you programmed is using. And most probably you need some program that will work with that bootloader you programmed.
    – KamilCuk
    Jul 13, 2019 at 9:43
  • 2
    Well, you will have to have a serious talk with your linker to persuade it to generate an image that you can load into flash. Yes, it's painful, with all those areas and segments and sections, but it has to be done:( Jul 13, 2019 at 10:31
  • 1
    Or if you use gcc and linux, just arm-none-eabi-objcopy -o ihex input.elf output.elf, ie. how to convert elf to hex. Usually bootloaders are plain simple, they expose an api to program and erase memory regions. So you need a program on your PC that will understand ex. hex file format and communicate with your bootloader. STill, you didn't say what "bootloader software" you used.
    – KamilCuk
    Jul 13, 2019 at 10:47

3 Answers 3

9

In CubeIDE go to Project Settings -> "C/C++ Build" group -> Settings -> "Tool Settings" tab -> MCU Post build outputs -> "Convert to Intel Hex file" check box Hex file option in CubeIDE (If you do not see those options, you may need to restart the IDE - such a bug still exists)

This will make the IDE convert the output into HEX-file, which is easily parsable. You can find the format description in Wikipedia. You can parse it before sending to the bootloader.

Or, you can set the checkbox "Convert to binary file", which will make a raw binary file. But it may give some troubles if your code starts not from zero address.

1
  • 1
    Thank you so much, this is just what I was looking for!
    – arry_h
    Jul 15, 2019 at 9:29
2

You will want to use the objcopy.exe that comes with your IDE. The following works with Atollic TrueStudio, the predecessor to STM32CubeIDE. This step is usually added as a post build step

arm-atollic-eabi-objcopy.exe -O ihex "${BuildArtifactFileBaseName}.elf" "${BuildArtifactFileBaseName}.hex"

For more info on objcopy: https://sourceware.org/binutils/docs/binutils/objcopy.html

0

For STM32CubeIDE's internal builder this works just fine (as post-build step):

arm-none-eabi-objcopy -O ihex ${ProjName}.elf ${ProjName}.hex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.