Jonathan Thomson's web journal

Getting Started with the STM32F4DISCOVERY in Linux November 17, 2011

Filed under: Electronics — jethomson @ 8:58 pm

Building an ARM toolchain
To compile code for the STM32F4DISCOVERY you’ll need an ARM toolchain that supports the Cortex-M3. I used the build script summon-arm-toolchain to build one. Before running the script you need to install several dependencies:

$ su -c "apt-get install flex bison libgmp3-dev libmpfr-dev libncurses5-dev libmpc-dev autoconf texinfo build-essential libftdi-dev"

You might also need to run:

$ su -c "apt-get build-dep gcc-4.5"

Now make a directory to hold the files you’ll need to work with the STM32F4DISCOVERY:

$ mkdir ~/stm32f4discovery_root
$ cd ~/stm32f4discovery_root

Next clone the summon-arm-toolchain, read the README, and begin the build:

$ git clone https://github.com/esden/summon-arm-toolchain.git
$ cd summon-arm-toolchain
$ ./summon-arm-toolchain

 

Setting up the software to communicate with the STLINK
While the toolchain is compiling you can set up the software required to communicate with the STLINK, which is the on-board JTAG programmer/debugger on the lefthand side of the board. From a new shell run:

$ su -c "apt-get install libusb-1.0-0-dev"
$ cd ~/stm32f4discovery_root
$ git clone git://github.com/texane/stlink.git stlink_texane
$ cd ~/stm32f4discovery_root/stlink_texane
$ make

Now you should install the udev rules 49-stm32l-discovery.rules and restart udev. These steps will create a symbolic link called /dev/stm32l_stlinkN and give a normal user permission to access it.

$ su
# cp 49-stm32l-discovery.rules /etc/udev/rules.d/
# /etc/init.d/udev restart
# exit

 

Example projects and makefiles
Next, download the STM32F4DISCOVERY firmware package and an archive of makefiles for building some of the example projects found in the firmware package.

$ cd ~/stm32f4discovery_root
$ wget http://www.st.com/internet/com/SOFTWARE_RESOURCES/SW_COMPONENT/FIRMWARE/stm32f4discovery_fw.zip
$ unzip stm32f4discovery_fw.zip
$ wget http://jthomson.towhee.org/stm32f4discovery_files/STM32F4DISCOVERY_Makefiles.zip
$ unzip STM32F4DISCOVERY_Makefiles.zip
$ cd ~/stm32f4discovery_root/STM32F4DISCOVERY_Makefiles
$ cp Makefile.IO_Toggle ~/stm32f4discovery_root/STM32F4-Discovery_FW_V1.1.0/Project/Peripheral_Examples/IO_Toggle/
$ cd ~/stm32f4discovery_root/STM32F4-Discovery_FW_V1.1.0/Project/Peripheral_Examples/IO_Toggle

If the toolchain installation still hasn’t finished, then familiarize yourself with the example code and makefiles or take a break.

 

Compiling and Flashing
Once summon-arm-toolchain has finished building your ARM toolchain you can try it out on the IO_Toggle example by using the following commands.

$ cd ~/stm32f4discovery_root/STM32F4-Discovery_FW_V1.1.0/Project/Peripheral_Examples/IO_Toggle
$ make -f Makefile.IO_Toggle

If the build finished without error, then you’re ready to try downloading it to the board. Plug in the board and use stlink to write the blinking LED example, IO_Toggle.bin, to the STM32F4DISCOVERY’s flash memory.

$ PATH=~/stm32f4discovery_root/stlink_texane/flash:$PATH
$ flash write IO_Toggle.bin 0x8000000

Power cycle the board by removing then reinserting the USB plug. If the download was successful, the LEDs should light up in a clockwise pattern and pressing the pushbutton will not result in the board entering test mode.

You can easily modify one of the sample makefiles to work with another example by examining the .uvproj file (found in the example’s MDK-ARM directory) to determine which defines (-D), includes (-I), source files, etc. to add to the new makefile. Just open the .uvproj file in a text editor and look for lines like these:

<Define>USE_STDPERIPH_DRIVER,STM32F4XX</Define>
<IncludePath>
..\;
..\..\..\..\Libraries\CMSIS\Include;
..\..\..\..\Libraries\CMSIS\ST\STM32F4xx\Include;
..\..\..\..\Libraries\STM32F4xx_StdPeriph_Driver\inc;
..\..\..\..\Utilities\STM32F4-Discovery
</IncludePath>

<FilePath>..\main.c</FilePath>
<FilePath>..\..\..\..\Utilities\STM32F4-Discovery\stm32f4_discovery.c</FilePath>
<FilePath>..\..\..\..\Libraries\STM32F4xx_StdPeriph_Driver\src\stm32f4xx_rcc.c</FilePath>
etc.

 

Final Notes
There is a lot I don’t understand about compiling for the STM32F4DISCOVERY and I’m certain that the Makefiles I’ve provided could be improved. For example, I don’t know which -m options (see the Makefile’s MCUFLAGS variable) to set, when to set them, and why. If you have any corrections or improvements to share, please leave a comment.

 

Links
The STM32F4 high-performance discovery board (STM32F4DISCOVERY) and a range of of STMicroelectronics products are available at Newark
Alternate method of programming the STM32F4DISCOVERY using the Bus Blaster and OpenOCD
A fork of the summon-arm-toolchain that builds gcc for the Cortex-M4F
A comment discussing this fork

Advertisements
 

Getting Started with the STM32VLDISCOVERY in Linux

Filed under: Electronics — jethomson @ 8:16 pm

Building an ARM toolchain
To compile code for the STM32VLDISCOVERY you’ll need an ARM toolchain that supports the Cortex-M3. I used the build script summon-arm-toolchain to build one. Before running the script you need to install several dependencies:

$ su -c "apt-get install flex bison libgmp3-dev libmpfr-dev libncurses5-dev libmpc-dev autoconf texinfo build-essential libftdi-dev"

You might also need to run:

$ su -c "apt-get build-dep gcc-4.5"

Now make a directory to hold the files you’ll need to work with the STM32VLDISCOVERY:

$ mkdir ~/stm32vldiscovery_root
$ cd ~/stm32vldiscovery_root

Next clone the summon-arm-toolchain, read the README, and begin the build:

$ git clone https://github.com/esden/summon-arm-toolchain.git
$ cd summon-arm-toolchain
$ ./summon-arm-toolchain

 

Setting up the software to communicate with the STLINK
While the toolchain is compiling you can set up the software required to communicate with the STLINK, which is the on-board JTAG programmer/debugger on the lefthand side of the board. From a new shell run:

$ cd ~/stm32vldiscovery_root
$ su -c "apt-get install libusb-1.0-0-dev"
$ wget http://arm-utilities.googlecode.com/files/arm-utilities-2011-6-28.tgz
$ tar xvzf arm-utilities-2011-6-28.tgz
$ cd ~/stm32vldiscovery_root/arm-utilities/stlink-download
$ gcc -o stlink-download stlink-download.c

Now you should install the udev rules 10-stlink.rules, add your username to the group tape, and restart udev. These steps will create a convenient symbolic link called /dev/stlink to the board’s actual /dev entry (e.g. /dev/sg2)

$ su
# cp 10-stlink.rules /etc/udev/rules.d/
# usermod -aG tape your_username_here
# /etc/init.d/udev restart
# exit

 

Example projects and makefiles
Next, download the STM32VLDISCOVERY firmware package (AN3268) and an archive of makefiles for building some of the example projects found in the firmware package.

$ cd ~/stm32vldiscovery_root
$ wget http://www.st.com/internet/com/SOFTWARE_RESOURCES/SW_COMPONENT/FIRMWARE/stm32vldiscovery_package.zip
$ unzip stm32vldiscovery_package.zip
$ wget http://jthomson.towhee.org/stm32vldiscovery_files/STM32VLDISCOVERY_Makefiles.zip
$ unzip STM32VLDISCOVERY_Makefiles.zip
$ cd ~/stm32vldiscovery_root/STM32VLDISCOVERY_Makefiles
$ cp Makefile.GPIOToggle ~/stm32vldiscovery_root/an3268/stm32vldiscovery_package/Project/Examples/GPIOToggle/
$ cd ~/stm32vldiscovery_root/an3268/stm32vldiscovery_package/Project/Examples/GPIOToggle
$ cp ../../Demo/TrueSTUDIO/DISCOVER/STM32F100RB_FLASH.ld ./

As described in README.TXT, you’ll need to edit Utilities/STM32vldiscovery.h to change the line: #include "STM32f10x.h" to lowercase: #include "stm32f10x.h". If the toolchain installation still hasn’t finished, then familiarize yourself with the example code and makefiles or take a break.

 

Compiling and Flashing
Once summon-arm-toolchain has finished building your ARM toolchain you can try it out on the AN3268 GPIOToggle example by using the following commands.

$ cd ~/stm32vldiscovery_root/an3268/stm32vldiscovery_package/Project/Examples/GPIOToggle
$ make -f Makefile.GPIOToggle

If the build finished without error, then you’re ready to try downloading it to the STM32VLDISCOVERY. Before plugging in the STM32VLDISCOVERY run the following command:

$ su -c "modprobe -r usb-storage && modprobe usb-storage quirks=483:3744:lrwsro"

Plug in the board and wait for Linux to finish mounting it. Although the udev 10-stlink.rules are written to prevent the board from being auto-mounted it happens on my system regardless. Once the board is mounted, you should be able to see three URL files. In my experience, you must wait for the board to be mounted before attempting to flash a program; otherwise, you will crash Linux. Finally, use stlink-download to write the blinking LED example, GPIOToggle.bin, to the STM32VLDISCOVERY’s flash memory.

$ PATH=~/stm32vldiscovery_root/arm-utilities/stlink-download:$PATH
$ stlink-download /dev/stlink program=GPIOToggle.bin
$ stlink-download /dev/stlink reset
$ stlink-download /dev/stlink run

You can easily modify one of the sample makefiles to work with another example by examining the .uvproj file (found in the example’s MDK-ARM directory) to determine which defines (-D), includes (-I), source files, etc. to add to the new makefile. Just open the .uvproj file in a text editor and look for lines like these:

<Define>STM32F10X_MD_VL, USE_STDPERIPH_DRIVER</Define>
<IncludePath>
..\;
..\..\..\..\Libraries\CMSIS\CM3\CoreSupport;
..\..\..\..\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x;
..\..\..\..\Libraries\STM32F10x_StdPeriph_Driver\inc;
..\..\..\..\Utilities\
</IncludePath>

<FilePath>..\..\..\..\Libraries\STM32F10x_StdPeriph_Driver\src\misc.c</FilePath>
<FilePath>..\..\..\..\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_exti.c</FilePath>
<FilePath>..\..\..\..\Libraries\STM32F10x_StdPeriph_Driver\src\stm32f10x_gpio.c</FilePath>
etc.

 

I originally started working with a package that builds all the examples at once, but this requires compiling and linking code that isn’t needed by every example so it is best to create a tailored makefile for each individual project. However, it’s an easy way to create all the .bin file examples so that you can quickly try them out on your board. The STM32VLDISCOVERY_firmware_build_all package is based on Geoffrey Brown’s project “to enable building for the STM32VLDISCOVERY board in a Unix environment.” I modified his project slightly to fix some case sensitivity errors, a missing syscall _exit() function, and to prevent from redistributing code in possible violation of the code’s license.

$ cd ~/stm32vldiscovery_root
$ wget http://jthomson.towhee.org/stm32vldiscovery_files/STM32VLDISCOVERY_firmware_build_all.zip
$ unzip STM32VLDISCOVERY_firmware_build_all.zip
$ cd ~/stm32vldiscovery_root/STM32VLDISCOVERY_firmware_build_all
$ make

 

Final Notes
There is another open source software project that supports downloading and debugging via the STLINK hardware known as stlink. It allows JTAG debugging with gdb and OpenOCD. Unfortunately I wasn’t able to use stlink because it crashes my computer. I believe this may be caused by a bug in libsgutils2-dev. Here’s a link to the open source stlink project.

 

Links
The STM32 Value Line Discovery Kit (STM32VLDISCOVERY) and a range of of STMicroelectronics products are available at Newark

A great site on developing for the STM32 using open source tools
Geoffrey Brown’s makefiles to build for the STM32VLDISCOVERY board in a Unix environment
stlink-download wiki page
Another guide that shows how to build the STM32VLDISCOVERY firmware package’s examples using Eclipse and CodeSourcery
A good list of the various toolchains and utilities for the STM32
Programming STM32F10x I/O port pins
Cortex M3 For Dummies – STM32 Discovery Board
A fork of stlink that may work for the STM32VLDISCOVERY