STM32CubeIDE builds an .elf by default. A .bin or .hex file is just machine
code with no source, comments, or symbols, so you can hand it to someone to flash
without sharing your project.
| File | What it is |
|---|---|
.elf |
The default build output used for debugging because it keeps symbols. |
.hex |
A shareable image that already contains its load address, so it is the easiest one to give away. |
.bin |
A shareable raw image that you must write to address 0x08000000. |
- Right-click the project in Project Explorer and choose Properties.
- Go to C/C++ Build > Settings > Tool Settings > MCU Post build outputs.
- Check Convert to binary file (-O binary). Also check Convert to Intel Hex if you want a
.hex. - Click Apply and Close, then run Clean Project and Build Project.
The files appear next to the .elf inside the active build folder.
YourProject/Debug/YourProject.bin # or Release/
YourProject/Debug/YourProject.hex
Build the Release configuration for distribution rather than Debug. You switch it with Project > Build Configurations > Set Active > Release.
This step stops other people from copying your program back off the board. A
.bin or .hex hides your source code, but the compiled program still sits inside
the chip flash, and anyone who plugs in a debugger can read it out. Turning on
Read-Out Protection blocks that read-back. Do this in STM32CubeProgrammer.
- Plug the board into USB and open STM32CubeProgrammer.
- In the top-right corner, select ST-LINK in the dropdown and click Connect.
- On the left toolbar, click the OB icon, which opens the Option Bytes panel.
- In the panel, expand the Read Out Protection section and find the row named RDP.
- Open the RDP dropdown and select Level 1, which is shown as the value BB.
- Click the Apply button at the top of the Option Bytes panel.
- Click Disconnect when it finishes.
After this a debugger can no longer read the flash. If you ever need to remove the protection you set RDP back to Level 0, but the chip erases all of its flash during that change, so nobody can recover your firmware. Do not use Level 2, because it locks the debug port forever and cannot be undone.
- If the MCU Post build outputs page is missing, you either opened the wrong Properties or the project is not an STM32 project, so right-click the project to open it.
- If no
.binappears after building, the incremental build skipped it, so run Clean Project and then Build Project. - If the recipient's
.binwill not run, remember that a.binhas no address, so give them the.hexor tell them to flash it to0x08000000.