STM32 Compile Applications

Compile on Ubuntu Linux

sudo add-apt-repository ppa:team-gcc-arm-embedded/ppa
sudo apt-get update
sudo apt-get install gcc-arm-none-eabi

Test the installation:

arm-none-eabi-gcc --version

Should output something similar to this:

arm-none-eabi-gcc (GNU Tools for Arm Embedded Processors 7-2018-q3-update) 7.3.1 20180622 (release) [ARM/embedded-7-branch revision 261907]
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 

cd ~
cd dev
mkdir helloworld
cd helloworld
vi main.c

paste this text:

int
main(void)
{

while (1);
}

Compile the program:

arm-none-eabi-gcc -std=gnu99 -g -O2 -Wall -mlittle-endian -mthumb -mthumb-interwork -mcpu=cortex-m0 -fsingle-precision-constant -Wdouble-promotion --specs=nosys.specs main.c -o main.elf

You will get an .elf file. It is not an .bin file and cannot be flashed.
https://stackoverflow.com/questions/49680382/objcopy-elf-to-bin-file

Check the elf file

arm-none-eabi-readelf -h main.elf

Convert the .elf file to .bin (Removes the .elf metadata and only leaves raw machine code for the microcontroller to execute)

arm-none-eabi-objcopy -O binary main.elf main.bin

You can now flash the bin file using st-link.
Connect the board using usb.
Check that the board is connected:

/home/wbi/dev/stlink/build/Release/st-info --probe

Flash the binary onto the board. Warning: Flashing a binary onto the board will override the previous content of the board without performing any backup! The previous content is lost and cannot be brought back! If you want to safe the content, first read the flash. Reading the flash is described in another article.

st-flash write main.bin 0x08000000

STM32 Reading the flash memory

Why read the flash

Before flashing my own software into the stm32, I wanted to download and store the preinstalled example program. To download the preinstalled application, it is necessary to read the 512kb flash memory to a file on disk.

Mac OS

On mac, the easiest way to read the 512KB flash is using the STM32CubeProg application which is available for Mac, Linux and Windows. It is available from here. Please do not confuse the STM32CubeProg (STM32 Cube Programmer) tool with the STM32CubeIDE (available here).

On Mac, the only problem is installing the STM32CubeProg. Clicking the .app file from the zip does nothing. Instead the only way to install the application is to use the tip from this page. The post says to execute a java command on the .exe file on mac which actually works and installs the application just fine.

sudo java -jar SetupSTM32CubeMX-4.22.0.exe

The STM32CubeProg allows to select an address to read from and an amount of bytes to read.

The flash has a size of 512 kilobyte. The hex equivalent of 512kb is 0x80000. Reading 0x80000 from the address 0x08000000 using a Data Width of 32bit, will read the flash content.

First plugin the STM32 board using a USB-Cable. When starting up the STM32CubeProg application, it says it is not connected. Click the connect button. The application will automatically detect your board and set correct parameters. It will prefill the address field with 0x08000000 and the Size with 0x400. Replace the size by 0x80000 to read 512kb. Click the Read button and wait until the application does refresh itself. Then, toggle the Read button from Read to Save As… by opening the dropdown and selecting Save As… from the options. Then click the button and store the file onto your harddrive.

Linux

On Ubuntu Linux, you can install a USB driver and the st-link application to access the flash of the STM32 Nucleo F446RE.

sudo add-apt-repository ppa:team-gcc-arm-embedded/ppa
sudo apt-get update
sudo apt-get install gcc-arm-none-eabi

Test the installation

arm-none-eabi-gcc --version

Should output something similar to this:

arm-none-eabi-gcc (GNU Tools for Arm Embedded Processors 7-2018-q3-update) 7.3.1 20180622 (release) [ARM/embedded-7-branch revision 261907]
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Install the USB driver:

sudo apt-get install libusb-1.0-0-dev

Install st link by checking out the git repository and compiling the source code:

git clone https://github.com/texane/stlink.git
cd stlink
make release
cd build/Release
Test:
/home/wbi/dev/stlink/build/Release/st-flash

/home/wbi/dev/stlink/build/Release/st-info
/home/wbi/dev/stlink/build/Release/st-info --version
/home/wbi/dev/stlink/build/Release/st-info --flash
/home/wbi/dev/stlink/build/Release/st-info --sram
/home/wbi/dev/stlink/build/Release/st-info --descr
/home/wbi/dev/stlink/build/Release/st-info --pagesize
/home/wbi/dev/stlink/build/Release/st-info --chipid
/home/wbi/dev/stlink/build/Release/st-info --serial
/home/wbi/dev/stlink/build/Release/st-info --hla-serial
/home/wbi/dev/stlink/build/Release/st-info --probe

Read the flash

https://github.com/texane/stlink/issues/644
stlinkv2 command line: ./st-flash [--debug] [--reset] [--serial <serial>] [--format <format>] [--flash=<fsize>] {read|write} <path> <addr> <size>
st-flash --format binary read mydump.bin 0x08000000 0x100
/home/wbi/dev/stlink/build/Release/st-flash --format binary read /home/wbi/temp/stmnucleof446RE/mydump.bin 0x08000000 0x200
/home/wbi/dev/stlink/build/Release/st-flash read /home/wbi/temp/stmnucleof446RE/out.bin 0x08000000 0x80000