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

arry_h
  • 167
  • 1
  • 3
  • 12
  • `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 '19 at 09: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 '19 at 09:41
  • 2
    No. [.elf is ELF](https://en.wikipedia.org/wiki/Executable_and_Linkable_Format), [.exe is executable for windows](https://en.wikipedia.org/wiki/.exe) [binary file is binary](https://en.wikipedia.org/wiki/Binary_file) and what you want is dependent on what the "programmed bootloader software" expects to get. And most probably it wants a [hex file](https://en.wikipedia.org/wiki/Intel_HEX), 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 '19 at 09:43
  • What I would want is a hex file, of the completely compiled code such that it had the regular vector table first followed by the main application. I am not sure how to create this file from the elf file which is generated. – arry_h Jul 13 '19 at 10:05
  • 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:( – Martin James Jul 13 '19 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](https://stackoverflow.com/questions/19458031/how-to-create-a-executable-hex-from-elf-file-format). 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 '19 at 10:47

3 Answers3

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.

AterLux
  • 4,566
  • 2
  • 10
  • 13
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

abonneville
  • 334
  • 2
  • 5
0

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

arm-none-eabi-objcopy -O ihex ${ProjName}.elf ${ProjName}.hex
Unreal Qw
  • 81
  • 1
  • 2