HowTo ToolChain STM32 Ubuntu
HowTo ToolChain STM32 Ubuntu
HowTo ToolChain STM32 Ubuntu
Disclaimer of Warranty
THERE IS NO WARRANTY FOR THE CONTENT, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE CONTENT AS IS WITHOUT
WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF
THE CONTENT IS WITH YOU. SHOULD THE CONTENT PROVE DEFECTIVE, YOU ASSUME
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
Limitation of Liability
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR
CONVEYS THE CONTENT AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OR INABILITY TO USE THE CONTENT (INCLUDING BUT NOT
LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES
SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE
WITH ANY OTHER PROGRAM), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
Page 2 of 41
Contents
1
2
3
4
About................................................................4
Hardware..........................................................4
Software...........................................................4
Basic tools........................................................5
4.1 OpenOCD.................................................5
4.1.1 Download, build and install.............5
4.1.2 Install JTAG device..........................6
4.1.3 Configure..........................................7
4.2 Serial bootloader....................................10
4.2.1 stm32flash......................................10
4.2.2 Flash loader demonstrator..............10
4.3 GCC toolchain........................................11
4.3.1 Download.......................................11
4.3.2 Installation......................................11
5 Basic projects.................................................12
5.1 0001_Test_Blink....................................12
5.1.1 Compile..........................................12
5.1.2 Flash and run..................................14
5.1.3 Debug.............................................14
5.1.4 Make & Flash.................................15
5.2 0002_Test_Template..............................17
5.2.1 Librarys..........................................17
5.2.1.1 Install StdPeriph_Lib_V3.5.0. 17
5.2.1.2 Install USB library and
StdPeriph_Lib_V3.6.1.........................17
5.2.1.3 Content....................................18
5.2.2 Basic Makefiles..............................18
5.2.2.1 Common Makefile..................18
5.2.2.2 Libs Makefile..........................20
5.2.3 Linker Script...................................21
5.2.4 Startup Code...................................24
5.2.5 Final steps.......................................28
5.2.5.1 Source main.c.........................28
5.2.5.2 Source Makefile......................29
5.2.5.3 Final Makefile.........................30
5.2.6 Build project...................................30
Page 3 of 41
1 About
this manual describes how to install a toolchain for Cortex-M3 on Ubuntu GNU/Linux. All packages
used, except the GCC toolchain, are open source. For this part a free, unlimited and up to date version
of Sourcery CodeBench, based on the GNU tools, is used in order to ease the install and build
procedure.
Most content of this manual is based on the knowledge and the excellent how-to pages of Johan
Simonsson at http://fun-tech.se/stm32/ (1) and Geoffrey McRae at http://stm32.spacevs.com/ (2).
Consider this manual as a summary and extension of these guides. If any questions arise, please first
have a look at these pages where much more aspects are touched and explained.
For better reading of this document command inputs and outputs via a terminal window are formated
like this. The content of source files is enclosed in frames.
Hint: PDF documents do not contain tab formatting marks and empty lines. So it is not possible to
copy source code out of a PDF document by copy and paste without loss of this information.
The content of this manual may not be up to date. So before downloading and installing any package,
please check if the mentioned packages are still up to date. If newer packages exist and it is sensible to
use them please adapt the instructions to these conditions.
Much thanks and lot's of greetings to all those people developing and improving these artful tools
running on GNU/Linux.
After nearly one year of coding, using the toolset for hours most days, it has proven to be reliable,
comfortable and very satisfying. Any improvements necessary will be documented in future versions
of this manual.
Any comments welcome, please mail to: [email protected]
2 Hardware
Hardware used:
Olimex ARM-USB-OCD-H. USB ARM JTAG device with one additional RS-232 port. The
device is based on the FTDI FT2232H chip.
Olimex STM32-H103. Header board for STM32F103RBT6. The microcontroller
integrates 128KB Flash, 20KB RAM, 3xUART,
STM32F103RET6 (512KB Flash, 64KB RAM) mounted on STM32-H103 board.
The example code in this manual is adapted to STM32F103RBT6.
3 Software
The toolchain consists of following packages:
OpenOCD
stm32flash by Geoffrey McRae (2)
Sourcery CodeBench Lite Edition for ARM EABI
STM32F10x standard peripheral library
Project template and makefile by Geoffrey McRae (2)
Eclipse IDE
Git
Doxygen
Page 4 of 41
4 Basic tools
This chapter is about installing the basic toolchain.
4.1 OpenOCD
Open On-Chip Debugger is the part of software that is needed to enable the JTAG-hardware (ARMUSB-OCD-H) to flash and debug the microcontroller, it is the software interface to GDB.
OpenOCD downloads and documentation can be found at:
http://openocd.sourceforge.net/about/
http://sourceforge.net/projects/openocd/files/openocd/
Page 5 of 41
OLIMEX_ARM-USB-OCD-H.rules
check with:
dmesg | grep usb
that it is identified in a correct way, and you don't get any strange errors.
Now you should see something like this:
(Ubuntu 10.04 will not show second line)
[17611.036358] usb 1-5.1.3: new high-speed USB device number 44 using ehci_hcd
[17611.137606] usb 1-5.1.3: Ignoring serial port reserved for JTAG
[17611.140887] usb 1-5.1.3: Detected FT2232H
[17611.140889] usb 1-5.1.3: Number of endpoints 2
[17611.140891] usb 1-5.1.3: Endpoint 1 MaxPacketSize 512
[17611.140893] usb 1-5.1.3: Endpoint 2 MaxPacketSize 512
[17611.140895] usb 1-5.1.3: Setting MaxPacketSize 512
[17611.141229] usb 1-5.1.3: FTDI USB Serial Device converter now attached to ttyUSB0
Page 6 of 41
4.1.3 Configure
OpenOCD uses a configuration file called openocd.cfg on startup.
It contains information about:
1) the deamon (ports) configuration
2) the interface (JTAG) configuration
3) the board (microcontroller) configuration
4) the target (microcontroller) configuration
Cause this information may vary between projects, the configuration file should be present in the
directory of every project.
The daemon section of openocd.cfg contains following text:
#daemon configuration
telnet_port 4444
gdb_port 3333
Interface, board and target sections for the most common devices are part of the OpenOCD package.
These are present underneath the following directory:
/usr/local/share/openocd/scripts/....
The interface section for ARM-USB-OCD-H is a copy from:
/usr/local/share/openocd/scripts/interface/olimex-arm-usb-ocd-h.cfg
The board section for Olimex STM32-H103 is a copy from:
/usr/local/share/openocd/scripts/board/olimex_stm32_h103.cfg
Following supplement is necessary to define in which way OpenOCD will access the reset pin of the
MCU via the JTAG device:
# reset_config parameter (see OpenOCD manual):
# none
--> srst and trst of MCU not connected to JTAG device
# srst_only
--> only srst of MCU connected to JTAG device
# trst_only
--> only trst of MCU connected to JTAG device
# srst_and_trst
--> srst and trst of MCU connected to JTAG device
# default setting: "reset_config none" will produce a single reset via SYSRESETREQ (JTAG commands) at reset pin of MCU
reset_config none
Hardware access to trst pin (JTAG reset) of the MCU is not enabled, it can be accessed via JTAG
commands. If the reset signal (srst) of the MCU is not available at the JTAG connector and/or reset is
done via SYSRESETREQ parameter must be set to none. This setting has vital influence on the
debugger configuration.
The target section for STM32F103RBT6 is a copy from:
/usr/local/share/openocd/scripts/target/stm32f1x.cfg
The complete content of openocd.cfg is build from these 4 sections. This file must be created by use
of a text editor. Store a copy of this file in directory ~/temp for later use.
Beware to add space characters at end of lines this will cause OpenOCD to produce strange
results.
Page 7 of 41
Page 8 of 41
openocd.cfg
Page 9 of 41
4.2.1 stm32flash
Program for GNU/Linux, developed by Geoffrey McRae (2). It can be downloaded from:
http://code.google.com/p/stm32flash/
It supports raw binary and Intel HEX files for flashing. To get the source of the program, by the use of
subversion, run following commands:
cd ~/temp/
svn checkout http://stm32flash.googlecode.com/svn/trunk/ stm32flash
cd ~/temp/stm32flash/
Compile the program:
make
Install the utility into /usr/local/bin:
sudo make install
Following example command downloads file main.bin via serial port ttyS0 to the MCU:
stm32flash -w main.bin -v /dev/ttyS0
Replace ttyS0 by the port in use (ttyUSB0 for example). To find out the ports available use:
dmesg | grep tty
This command will show a list of the ports.
See the manual for information about install and use of the program.
Page 10 of 41
Alternatively GNU Tools for ARM Embedded Processors can be used. Information and downloads
can be found at:
http://launchpad.net/gcc-arm-embedded
This tool suite also includes FPU support, that is not needed when using STM32F103 devices.
Installation and usage is identical to the first mentioned compiler suite.
4.3.1 Download
Be sure to download the embedded-application binary interface (EABI) version, it is built to produce a
raw binary that will run stand-alone on the device without an operating system.
In order to make installation easy and clear download the tarball version (IA32 GNU/Linux TAR) and
make some manual settings afterwards.
Link to download area (registration will bee necessary):
http://www.codesourcery.com/sgpp/lite/arm/portal/subscription?@template=lite
4.3.2 Installation
These instructions will install version Sourcery CodeBench Lite 2012.09 for ARM EABI .
Included are:
GNU Binary Utilities
GNU C & C++ Compilers
GNU Debugger
Newlib C Library
Extract the tarball (e.g.: arm-2012.09-63-arm-none-eabi-i686-pc-linux-gnu.tar.bz2) and then copy the
extracted contents to /opt as root.
Add path e.g.: /opt/arm-2012.09/bin to your environment, therefore append following 2 lines at end
of file .profile located at your home directory. Adjust pathname to name of extracted content.
Page 11 of 41
5 Basic projects
To check whether the compiler works, we should create and compile some small programs/projects.
Therefore it it a good idea to create a directory within the home directory to store the coming
glamorous projects in:
mkdir -p ~/22_ARM-Firmware
5.1 0001_Test_Blink
This project is just for test purposes. We will show how a program can be compiled, flashed to the
device and debugged via GDB. Do not use it as sample or template for future work.
The content of this project will be downloaded from Olimex, some hints are in a file called projects.txt.
First create a directory for this project:
mkdir -p ~/22_ARM-Firmware/0001_Test_Blink
Copy your OpenOCD configuration file openocd.cfg to the project directory
cp ~/temp/openocd.cfg ~/22_ARM-Firmware/0001_Test_Blink
Now download the Olimex files and copy them to the project directory:
mkdir -p ~/temp
cd ~/temp
wget http://olimex.com/dev/soft/arm/STR/STM32-BLINK-LED-GCC-ECLIPSE-projects.rar
unrar x STM32-BLINK-LED-GCC-ECLIPSE-projects.rar
cp ~/temp/projects/projects.txt ~/22_ARM-Firmware/0001_Test_Blink
cp ~/temp/projects/stm_h103/* ~/22_ARM-Firmware/0001_Test_Blink
cd ~/22_ARM-Firmware/0001_Test_Blink
5.1.1 Compile
And now let's make a clean, recompile and see what we get.
make clean
Now you will see some messy output this project should not be used as a template.
Recompile program:
make
Again you will see some messy output and the version of the used compiler too.
The program should have compiled without generating error messages.
Let's check what happened:
ls --sort=time -1 -l
The directory is displayed, newest files first:
main.list
main.bin
main.out
stm32f10x_gpio.o
stm32f10x_rcc.o
main.o
arm_comm.h
Page 12 of 41
For a closer look at the ELF-file use command readelf from binutils:
arm-none-eabi-readelf -A main.out
Now you should see something like this:
Attribute Section: aeabi
File Attributes
Tag_CPU_name: "Cortex-M3"
Tag_CPU_arch: v7
Tag_CPU_arch_profile: Microcontroller
Tag_THUMB_ISA_use: Thumb-2
Tag_ABI_PCS_wchar_t: 4
Tag_ABI_FP_denormal: Needed
Tag_ABI_FP_exceptions: Needed
Tag_ABI_FP_number_model: IEEE 754
Tag_ABI_align_needed: 8-byte
Tag_ABI_align_preserved: 8-byte, except leaf SP
Tag_ABI_enum_size: small
Tag_ABI_optimization_goals: Aggressive Debug
Tag_CPU_unaligned_access: v6
It is shown that we produced code for Cortex-M3 and lots of other information too.
Use objdump to look at the ELF-file with the -S flag:
arm-none-eabi-objdump -S main.out
The makefile also created a file called main.list. Included is assembler and interleaved C-code
interleaved. Open file by use of an editor:
gedit main.list
This is the result of the compiling. If you want to have a closer look at the previous steps, see (1).
There you will also find some explanatory notes about the program and the associated makefile.
Page 13 of 41
Then start a telnet session, so that you can talk to the OpenOCD server:
xterm -geometry 100x16+100+350 -e "telnet localhost 4444" &
A second new terminal window, a telnet terminal, should appear:
5.1.3 Debug
Now let's check if the program can be debugged by use of GDB. Resize your terminal window to a
very big one.
At your telnet terminal enter:
reset halt
The device will stop blinking.
At you terminal window enter:
arm-none-eabi-gdbtui --eval-command="target remote localhost:3333" main.out
Page 14 of 41
Follow the instructions shown in the terminal window (You may have to press return).
Inputs to GDB have to be confirmed by pressing the enter key.
Step through the program by typing (commands always followed by enter):
s
s
do_flash.pl
Page 15 of 41
= arm-none-eabi-gcc
= arm-none-eabi-ld -v
= arm-none-eabi-ar
= arm-none-eabi-as
= arm-none-eabi-objcopy
= arm-none-eabi-objdump
makefile
Now we have an additional make command available. Close the telnet terminal window. At the
terminal window enter:
make flash
Have a look at the OpenOCD terminal window and the terminal window and see that build and flash
have been executed.
To clean up you project enter following command:
make clean
Page 16 of 41
5.2 0002_Test_Template
This project is for test purposes too. It shows how a project can be structured, which libraries should be
included and provides some makefiles to build the project. This project can be used as a template for
future projects.
First create a directory for this project:
mkdir -p ~/22_ARM-Firmware/0002_Test_Template
Copy your OpenOCD configuration file openocd.cfg to the project directory
cp ~/temp/openocd.cfg ~/22_ARM-Firmware/0002_Test_Template
5.2.1 Librarys
The STM32F10x standard peripheral library contains device drivers for all standard device
peripherals, including functions covering full peripheral functionality. The C-source is documented and
tested. It contains all defines and structures needed for coding with the STM32. The library is provided
by ST Microelectronics:
http://www.st.com/st-web-ui/static/active/en/st_prod_software_internet/resource/technical/software/firmware/stsw-stm32054.zip
This file contains version 3.5.0 of the library including example code. Documentation that comes with
the library is in CHM format, so a reader like xCHM or ChmSee has to be installed to read the
documentation.
The USB full-speed device library enables building applications including USB functionality. The
library is provided by ST Microelectronics:
http://www.st.com/st-web-ui/static/active/en/st_prod_software_internet/resource/technical/software/firmware/stsw-stm32081.zip
This file contains the USB library version 4.0.0 and the STM32F10x standard peripheral library
version 3.6.1. without example code.
5.2.1.1 Install StdPeriph_Lib_V3.5.0
Decompress the USB full-speed device library and replace the V3.5.0 content in the Libraries
directory of the STM32F10x standard peripheral library with the content included in the USB fullspeed device library. Rename path to:
"~/22_ARM-Firmware/0002_Test_Template/libs/STM32F10x_StdPeriph_Lib_V3.6.1/...".
Later on we will use the CMSIS (Cortex Microcontroller Software Interface Standard) headers and
helper functions from these libs.
If USB functionality of interest, copy the contents of the Projects and Utilities directory (USB
examples) to a separate directory.
Note: Makefiles described later on will base on StdPeriph_Lib_V3.6.1. If you use a different version
of the library you will need to update the path and name settings in the makefile Makefile.common
to reflect the change.
Page 17 of 41
5.2.1.3 Content
The package is worth to be examined in detail, there is a lot of example code and information inside.
For building working projects not using USB functionality only the following paths are needed:
Libraries/CMSIS
Libraries/STM32F10x_StdPeriph_Driver/inc
Libraries/STM32F10x_StdPeriph_Driver/src
The CMSIS directory contains the defines and data structures for every peripheral in the STM32, as
well as the defines for the configuration registers and values.
The other paths contain the helper functions that try to make programming the STM32 simple. They
add a layer of abstraction feel free to use them or not.
This makefile has to be included into all other makefiles. It contains variable setup for the build
procedure. Create a file at the top level of the project called Makefile.common and paste the
following text into it:
# include Makefile
#This file is included in the general Makefile, the libs Makefile and the src Makefile
#Different optimize settings for library and source files can be realized by using arguments
#Compiler optimize settings:
# -O0 no optimize, reduce compilation time and make debugging produce the expected results (default).
# -O1 optimize, reduce code size and execution time, without much increase of compilation time.
# -O2 optimize, reduce code execution time compared to O1, increase of compilation time.
# -O3 optimize, turns on all optimizations, further increase of compilation time.
# -Os optimize for size, enables all -O2 optimizations that do not typically increase code size and other code size optimizations.
#Recommended optimize settings for release version: -O3
#Recommended optimize settings for debug version: -O0
#Valid parameters :
# OptLIB=0 --> optimize library files using the -O0 setting
# OptLIB=1 --> optimize library files using the -O1 setting
# OptLIB=2 --> optimize library files using the -O2 setting
# OptLIB=3 --> optimize library files using the -O3 setting
# OptLIB=s --> optimize library files using the -Os setting
# OptSRC=0 --> optimize source files using the -O0 setting
# OptSRC=1 --> optimize source files using the -O1 setting
# OptSRC=2 --> optimize source files using the -O2 setting
# OptSRC=3 --> optimize source files using the -O3 setting
# OptSRC=s --> optimize source files using the -Os setting
# all --> build all
# libs --> build libs only
# src --> build src only
# clean --> clean project
# tshow --> show optimize settings
#Example:
# make OptLIB=3 OptSRC=0 all tshow
TOP=$(shell readlink -f "$(dir $(lastword $(MAKEFILE_LIST)))")
PROGRAM=main
LIBDIR=$(TOP)/libs
Page 18 of 41
Makefile.common
Page 19 of 41
The makefile will use library STM32F10x_StdPeriph_Lib_V3.6.1. Code optimization is turned off
to make debugging produce the expected results. File is configured for a STM32 Medium density
device cause the MCU STM32F103RBT6 belongs to this device class, change "STM32F10X_MD"
to another setting if another microcontroller belonging to a different class is used. Enable the correct
define setting in following file, an explanation of this define can be found there also:
/libs/STM32F10x_StdPeriph_Lib_V3.6.1/Libraries/CMSIS/Device/ST/STM32F10x/Include/stm32f10x.h
inside your current project directory.
5.2.2.2 Libs Makefile
When building the STM32 library as a static library, changes to the application do not induce a
complete re-compile of the library and this speeds up the build process. Create another Makefile
named Makefile in the libs directory with the following contents by use of a text editor:
# libs Makefile
include ../Makefile.common
LIBS+=libstm32.a
CFLAGSlib+=-c
all: libs
libs: $(LIBS)
libstm32.a:
@echo -n "Building $@ ..."
@cd $(STMLIB)/CMSIS/Device/ST/STM32F10x/Source/Templates && \
$(CC) $(CFLAGSlib) \
system_stm32f10x.c
@cd $(STMLIB)/STM32F10x_StdPeriph_Driver/src && \
$(CC) $(CFLAGSlib) \
-D"assert_param(expr)=((void)0)" \
-I../../CMSIS/Include \
-I../../CMSIS/Device/ST/STM32F10x/Include \
-I../inc \
*.c
#
@cd $(STMLIB)/STM32_USB-FS-Device_Driver/src && \
#
$(CC) $(CFLAGSlib) \
#
-D"assert_param(expr)=((void)0)" \
#
-I../../CMSIS/Include \
#
-I../../CMSIS/Device/ST/STM32F10x/Include \
#
-I../inc \
#
*.c
@$(AR) cr $(LIBDIR)/$@ \
$(STMLIB)/CMSIS/Device/ST/STM32F10x/Source/Templates/system_stm32f10x.o \
$(STMLIB)/STM32F10x_StdPeriph_Driver/src/*.o \
#
$(STMLIB)/STM32_USB-FS-Device_Driver/src/*.o
@echo "done."
.PHONY: libs clean tshow
clean:
rm -f $(STMLIB)/CMSIS/Device/ST/STM32F10x/Source/Templates/system_stm32f10x.o
rm -f $(STMLIB)/STM32F10x_StdPeriph_Driver/src/*.o
rm -f $(STMLIB)/STM32_USB-FS-Device_Driver/src/*.o
rm -f $(LIBS)
tshow:
@echo "######################################################################################################"
@echo "################# optimize settings: $(InfoTextLib), $(InfoTextSrc)"
@echo "######################################################################################################"
Makefile
To test that everything is OK, execute following commands from the libs directory:
make clean
make
Now you should see the STM32 library get compiled, and a new file called libstm32.a appear in the
current projects libs directory. If not, be sure that your cross compiler is installed properly.
Page 20 of 41
, LENGTH = 0
Page 21 of 41
KEEP (*(SORT(.fini_array.*)))
__fini_array_end = .;
*(.init .init.*)
*(.fini .fini.*)
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP (*(.preinit_array))
PROVIDE_HIDDEN (__preinit_array_end = .);
PROVIDE_HIDDEN (__init_array_start = .);
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array))
PROVIDE_HIDDEN (__init_array_end = .);
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP (*(.fini_array))
KEEP (*(SORT(.fini_array.*)))
PROVIDE_HIDDEN (__fini_array_end = .);
. = ALIGN (8);
*(.rom)
*(.rom.b)
_etext = .;
_sidata = _etext; /* exported for the startup function */
} >FLASH
/*
this data is expected by the program to be in ram
but we have to store it in the FLASH otherwise it
will get lost between resets, so the startup code
has to copy it into RAM before the program starts
*/
.data : ALIGN (8) {
_sdata = . ; /* exported for the startup function */
. = ALIGN(4);
KEEP(*(.jcr))
*(.got.plt) *(.got)
*(.shdata)
*(.data .data.*)
. = ALIGN (8);
*(.ram)
*(.ramfunc*)
. = ALIGN(4);
_edata = . ; /* exported for the startup function */
} >RAM AT>FLASH
/* This is the uninitialized data section */
.bss (NOLOAD): {
. = ALIGN(4);
_sbss = . ; /* exported for the startup function */
*(.shbss)
*(.bss .bss.*)
*(COMMON)
. = ALIGN (8);
*(.ram.b)
. = ALIGN(4);
_ebss = . ; /* exported for the startup function */
_end = .;
__end = .;
} >RAM AT>FLASH
/* ensure there is enough room for the user stack */
._usrstack (NOLOAD): {
. = ALIGN(4);
_susrstack = . ;
. = . + _min_stack ;
. = ALIGN(4);
_eusrstack = . ;
} >RAM
/* Stabs debugging sections. */
.stab
0 : { *(.stab) }
.stabstr
0 : { *(.stabstr) }
.stab.excl 0 : { *(.stab.excl) }
.stab.exclstr 0 : { *(.stab.exclstr) }
.stab.index 0 : { *(.stab.index) }
.stab.indexstr 0 : { *(.stab.indexstr) }
.comment
0 : { *(.comment) }
/* DWARF debug sections.
Symbols in the DWARF debugging sections are relative to the beginning
of the section so we begin them at 0. */
Page 22 of 41
/* DWARF 1 */
.debug
0 : { *(.debug) }
.line
0 : { *(.line) }
/* GNU DWARF 1 extensions */
.debug_srcinfo 0 : { *(.debug_srcinfo) }
.debug_sfnames 0 : { *(.debug_sfnames) }
/* DWARF 1.1 and DWARF 2 */
.debug_aranges 0 : { *(.debug_aranges) }
.debug_pubnames 0 : { *(.debug_pubnames) }
/* DWARF 2 */
.debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }
.debug_abbrev 0 : { *(.debug_abbrev) }
.debug_line 0 : { *(.debug_line) }
.debug_frame 0 : { *(.debug_frame) }
.debug_str
0 : { *(.debug_str) }
.debug_loc
0 : { *(.debug_loc) }
.debug_macinfo 0 : { *(.debug_macinfo) }
/* SGI/MIPS DWARF 2 extensions */
.debug_weaknames 0 : { *(.debug_weaknames) }
.debug_funcnames 0 : { *(.debug_funcnames) }
.debug_typenames 0 : { *(.debug_typenames) }
.debug_varnames 0 : { *(.debug_varnames) }
/* DWARF 3 */
.debug_pubtypes 0 : { *(.debug_pubtypes) }
.debug_ranges 0 : { *(.debug_ranges) }
.ARM.attributes 0 : { KEEP (*(.ARM.attributes)) KEEP (*(.gnu.attributes)) }
.note.gnu.arm.ident 0 : { KEEP (*(.note.gnu.arm.ident)) }
/DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) }
}
linker.ld
Lines at the top beginning with RAM, FLASH and EEMUL specify MCU memory.
This linker script includes examples for medium density (MD) devices with 128K flash and 20K RAM
(STM32F103RBT) and high density (HD) devices with 512K flash and 64K RAM (STM32F103RET),
it is setup for an STM32F103RBT.
2K (MD) or 4K (HD) flash memory is reserved for EEPROM emulation (2 pages x 1K/2K), see
AN2594 -EEPROM emulation for available memory size and access mechanism.
Page 23 of 41
Page 24 of 41
Page 25 of 41
I2C2_EV_IRQHandler,
/* I2C2 Event */
I2C2_ER_IRQHandler,
/* I2C2 Error */
SPI1_IRQHandler,
/* SPI1 */
SPI2_IRQHandler,
/* SPI2 */
USART1_IRQHandler,
/* USART1 */
USART2_IRQHandler,
/* USART2 */
USART3_IRQHandler,
/* USART3 */
EXTI15_10_IRQHandler,
/* EXTI Line 15..10 */
RTCAlarm_IRQHandler,
/* RTC Alarm through EXTI Line */
USBWakeUp_IRQHandler,
/* USB Wakeup from suspend */
TIM8_BRK_IRQHandler,
TIM8_UP_IRQHandler,
TIM8_TRG_COM_IRQHandler,
TIM8_CC_IRQHandler,
ADC3_IRQHandler,
FSMC_IRQHandler,
SDIO_IRQHandler,
TIM5_IRQHandler,
SPI3_IRQHandler,
UART4_IRQHandler,
UART5_IRQHandler,
TIM6_IRQHandler,
TIM7_IRQHandler,
DMA2_Channel1_IRQHandler,
DMA2_Channel2_IRQHandler,
DMA2_Channel3_IRQHandler,
DMA2_Channel4_5_IRQHandler,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0,
(intfunc)0xF1E0F85F
/* @0x1E0. This is for boot in RAM mode for STM32F10x High Density devices. */
};
void __Init_Data(void) {
unsigned long *src, *dst;
/* copy the data segment into ram */
src = &_sidata;
dst = &_sdata;
if (src != dst)
while(dst < &_edata)
*(dst++) = *(src++);
/* zero the bss segment */
dst = &_sbss;
while(dst < &_ebss)
*(dst++) = 0;
}
void Reset_Handler(void) {
__Init_Data(); /* Initialize memory, data and bss */
extern u32 _isr_vectors_offs; /* the offset to the vector table in ram */
SCB->VTOR = 0x08000000 | ((u32)&_isr_vectors_offs & (u32)0x1FFFFF80); /* set interrupt vector table address */
SystemInit(); /* configure the clock */
main(); /* start execution of the program */
while(1) {}
}
#pragma weak MMI_Handler
#pragma weak MemManage_Handler
#pragma weak BusFault_Handler
#pragma weak UsageFault_Handler
#pragma weak SVC_Handler
#pragma weak DebugMon_Handler
#pragma weak PendSV_Handler
#pragma weak SysTick_Handler
#pragma weak WWDG_IRQHandler
#pragma weak PVD_IRQHandler
#pragma weak TAMPER_IRQHandler
#pragma weak RTC_IRQHandler
#pragma weak FLASH_IRQHandler
#pragma weak RCC_IRQHandler
#pragma weak EXTI0_IRQHandler
#pragma weak EXTI1_IRQHandler
= Default_Handler
= Default_Handler
= Default_Handler
= Default_Handler
= Default_Handler
= Default_Handler
= Default_Handler
= Default_Handler
= Default_Handler
= Default_Handler
= Default_Handler
= Default_Handler
= Default_Handler
= Default_Handler
= Default_Handler
= Default_Handler
Page 26 of 41
startup.c
Page 27 of 41
Create a file called main.c in the src directory, and paste the following demo code into it:
/*************************************************************************************
* Test-program for Olimex STM32-H103, header board for STM32F103RBT6.
* After program start green LED (LED_E) will blink.
*
* Program has to be compiled with optimizer setting "-O0".
* Otherwise delay via while-loop will not work correctly.
*************************************************************************************/
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
int main(int argc, char *argv[])
{
GPIO_InitTypeDef GPIO_InitStructure;
u32 delay;
/* GPIOC Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
/* Configure PC12 to mode: slow rise-time, pushpull output */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; // GPIO No. 12
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; // slow rise time
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // push-pull output
GPIO_Init(GPIOC, &GPIO_InitStructure); // GPIOC init
while(1)
{
/* make some float calculations */
float x = 42, y = 23, z = 7;
int i = 0;
for ( i = 0; i < 6; i++ )
{
z = (x*y)/z;
};
/* GPIO PC12 set, pin=high, LED_E off */
GPIOC->BSRR = GPIO_BSRR_BS12;
/*GPIO_WriteBit(GPIOC,GPIO_Pin_12,Bit_SET);*/
/* delay --> compiler optimizer settings must be "-O0" */
delay=500000;
while(delay)
delay--;
/* GPIO PC12 reset, pin=low, LED_E on */
GPIOC->BSRR = GPIO_BSRR_BR12;
/*GPIO_WriteBit(GPIOC,GPIO_Pin_12,Bit_RESET);*/
/* delay --> compiler optimizer settings must be "-O0" */
delay=500000;
while(delay)
delay--;
}
}
main.c
This code is just sample code for test purposes, for playing around with compiler options and for
debugger tests.
Page 28 of 41
Makefile
Page 29 of 41
Create a file called Makefile in your projects top level directory and paste the following text into it:
# general Makefile
include Makefile.common
LDFLAGS=$(COMMONFLAGS) -fno-exceptions -ffunction-sections -fdata-sections -L$(LIBDIR) -nostartfiles -Wl,--gc-sections,-Tlinker.ld
LDLIBS+=-lm
LDLIBS+=-lstm32
STARTUP=startup.c
all: libs src
$(CC) -o $(PROGRAM).elf $(LDFLAGS) \
-Wl,--whole-archive \
src/app.a \
-Wl,--no-whole-archive \
$(LDLIBS)
$(OBJCOPY) -O ihex $(PROGRAM).elf $(PROGRAM).hex
$(OBJCOPY) -O binary $(PROGRAM).elf $(PROGRAM).bin
#Extract info contained in ELF to readable text-files:
arm-none-eabi-readelf -a $(PROGRAM).elf > $(PROGRAM).info_elf
arm-none-eabi-size -d -B -t $(PROGRAM).elf > $(PROGRAM).info_size
arm-none-eabi-objdump -S $(PROGRAM).elf > $(PROGRAM).info_code
arm-none-eabi-nm -t d -S --size-sort -s $(PROGRAM).elf > $(PROGRAM).info_symbol
.PHONY: libs src clean tshow
libs:
$(MAKE) -C libs $@
src:
$(MAKE) -C src $@
clean:
$(MAKE) -C src $@
$(MAKE) -C libs $@
rm -f $(PROGRAM).elf $(PROGRAM).hex $(PROGRAM).bin $(PROGRAM).info_elf $(PROGRAM).info_size
rm -f $(PROGRAM).info_code
rm -f $(PROGRAM).info_symbol
tshow:
@echo "######################################################################################################"
@echo "################# optimize settings: $(InfoTextLib), $(InfoTextSrc)"
@echo "######################################################################################################"
Makefile
Page 30 of 41
6 Additional Tools
There exist some smart tools that ease programmers task after some practice. The tools and it's
documentation can be installed via the package manager of your GNU/Linux system. This may not
install the latest versions, but these versions should be usable and easy to maintain.
The use of these tools is recommended.
6.1 Doxygen
A tool that can generate documentation from source code in HTML, hyper-linked PDF and some other
formats.
Postulate is that some documentation is done when the code is created - at the point of time you now
your code best and this kind of work takes least time...
6.2 Git
A distributed revision control system not dependent on network access or a central server.
The use of a revision control system:
facilitates to keep an overview about changes and revisions of software projects
stores the sources and changes in a data base called repository
enables teams to work on software projects
Introduction about revision control and Git:
http://tom.preston-werner.com/2009/05/19/the-git-parable.html
Documentation:
http://git-scm.com/documentation
Tutorial:
http://schacon.github.com/git/gittutorial.html
Pro Git:
http://progit.org/book/
run_picocom.sh
To run terminal emulation call script. Adjust picocom parameters to needs if necessary. Device name
ttyACM0 refers to a STM32 VCP (Virtual Com Port) device.
Page 31 of 41
7 IDE
An Integrated Development Environments to handle all those tools described before may be:
installed and configured.
or
build on our own by making scripts and by arranging terminal windows and favorite programs
on the screen.
It is just a matter of taste and habit. Below see about how to install and configure a popular IDE.
7.1 Eclipse
Eclipse has lots of features, all previous mentioned packages can be integrated. After some
familiarization the user interface will appear well-arranged.
Writing and debugging C-code and simultaneously having a look at the assembler level and registers is
possible, even the ability to set breakpoints at assembler level.
Other IDE's like Codeblocks , Codelite or Geany at the moment (january 2012) do not have all these
abilities. KDevelop and Anjuta were not tested because the installation of a somewhat up to date
system was not possible on Ubuntu with reasonable expense.
If debugging via a discrete frontend to GDB is an option, KDbg or DDD may also be a alternative in
alliance with any of the above mentioned IDE's.
7.1.1 Install
For installing the IDE a Java Runtime Environment (JRE) has to exist on the system.
Only settings deviating from defaults are mentioned, for install user privileges are sufficient.
Three compressed files must be downloaded (filenames given may be out of date, the download of the
Doxygen frontend is for archive purposes only):
Eclipse IDE for C/C++ Developers
eclipse-cpp-juno-SR2-linux-gtk.tar.gz
available at: http://www.eclipse.org/downloads/
Eclipse CDT (C/C++ Development Tooling)
cdt-master-8.1.2.zip
available at: http://eclipse.org/cdt/
Doxygen frontend plug-in for eclipse
eclox_0.8.0.zip
available at: http://home.gna.org/eclox/
First step install Eclipse IDE (from file):
1. Unzip file containing compressed Eclipse IDE to an empty directory and copy it's content to
~/eclipse.
2. Create a shortcut to ~\eclipse\eclipse or add program to the GNOME-menue.
3. Start eclipse
4. Eclipse will ask for a working directory. Enter your projects directory ~/22_ARM-Firmware.
Second step install some extensions available via Eclipse CDT (from file):
5. Help Install New Software Add
6. Enter CDT in field Name: and path to Eclipse CDT archive in field Location:.
7. Set checkbox Hide items that are already installed and open selection trees.
8. Set checkbox C/C++ Debugger Services Framework (DSF) Examples.
9. Set checkbox C/C++ GDB Hardware Debugging.
10. Set checkbox C99 LR Parser SDK.
11. Set checkbox Eclipse Debugger for C/C++.
Page 32 of 41
Start eclipse
Select Workbench icon left mouse-click
Window Open Perspective Other C/C++
File New C Project
Project name: 0003_Test_Eclipse, Project Type: Makefile.. Empty.., Toolchains: other..
Finish
Page 33 of 41
This is the recommended setting. It should be used when the reset signal of the MCU is available at
the JTAG connector of the device in development (OpenOCD setting reset_config srst_only). The
device is reset via hardware signaling or SYSRESETREQ interrupt.
When reset is done via hardware use following settings:
1.
2.
3.
4.
5.
6.
7.
8.
9.
Page 34 of 41
13. At Initialization Commands clear checkbox Reset and Delay..: and clear checkbox Halt,
in text box enter following line:
monitor reset init
14. At Run Commands: in text box enter following line (optional):
break main
15. Select Common tab at input area Display in favorites.. set checkbox Debug.
16. Apply
Note (install step 11.): when trying to set JTAG Device: to OpenOCD (via pipe) debugging was
not possible, searching the web yielded no result. So this may be a topic for future improvements.
Duplicate previous debug configuration by right click on this configuration and selecting Duplicate.
a) Select Name: Enter 0003_Flash+Debug
b) Select the Startup tab
c) At Initialization Commands in text box enter following 4 lines:
monitor reset init
monitor flash probe 0
No EEPROM emulation: monitor stm32f1x mass_erase 0
EEPROM emulation (2 KB) on STM32F103RBT: monitor flash erase_sector 0 0 125
EEPROM emulation (4 KB) on STM32F103RET: monitor flash erase_sector 0 0 253
monitor flash write_bank 0 main.bin 0
(Using erase_sector prevents erase of emulated EEPROM memory during flash erase.)
d) Apply Close
7.1.6.2 Software reset
This setting is not recommended. It should be used when the reset signal of the MCU is not available
at the JTAG connector of the device in development and the SYSRESETREQ interrupt also is not
used. The device is reset via JTAG commands.
When reset is done via JTAG commands replace steps 13. and c) in previous section:
13. At Initialization Commands clear checkbox Reset and Delay..: and clear checkbox Halt,
in text box enter following line:
monitor soft_reset_halt
c) At Initialization Commands, in text box enter following 4 lines:
monitor soft_reset_halt
monitor flash probe 0
No EEPROM emulation: monitor stm32f1x mass_erase 0
EEPROM emulation (2 KB) on STM32F103RBT: monitor flash erase_sector 0 0 125
EEPROM emulation (4 KB) on STM32F103RET: monitor flash erase_sector 0 0 253
monitor flash write_bank 0 main.bin 0
(Using erase_sector prevents erase of emulated EEPROM memory during flash erase.)
Page 35 of 41
7.1.9 Hints
There exists a lot of documentation and hints about Eclipse and it seems to be necessary. This
chapter intends to contain useful know-how about first and standard steps. Hope it's content will
increase by time and with successful usage of the IDE.
Debuggen mit GDB (Gnu DeBugger) unter Eclipse :
http://homepages.thm.de/~bbdw58/anleit/debuggen.pdf
EGit/User Guide:
http://wiki.eclipse.org/EGit/User_Guide
Page 36 of 41
2.
3.
4.
5.
according to the target STM32 device (e.g. MD or HD) used in the application. A high density
device for example needs the definition: #define STM32F10X_HD.
In Makefile.common set TypeOfMCU accordingly to step 1.
In file linker.ld adapt the lines specifying RAM, FLASH and EEMUL to target.
In file openocd.cfg adapt WORKAREASIZE to target.
Configure eclipse debugger to correct chip or page erase behavior, see 7.1.6 Configure
debugger.
Page 37 of 41
9.3 OpenOCD
9.3.1 STM32F103RET
Supply for STM32F103RET was implemented using OpenOCD version 0.5.0. Flash+Debug did not
work correctly (Target request failed: ) when compiling was done without optimization and code
size was > 128KB Chip erase and download worked but debug did not start. Debug worked
correctly. Updating to OpenOCD 0.6.1 solved the problem.
9.4 MCU
9.4.1 I2C peripheral
I2Cx configuration and use is somewhat tricky, cause:
1. I2C interface of STM32F10xxx has some severe bugs and sometimes tends to hang, see errata
sheet.
Supervision of the I2C interface by software watchdog is indispensable
To leave hanging state a reconfiguration of the interface is mandatory
The errata sheet and the associated sample program itself consists severe errors.
2. I2C peripheral device may hang and freeze the SDA line low for infinity (or till power loss)
Supervision of the I2C peripheral device by software watchdog is indispensable
Toggle the SCL line in GPIO mode during configuration to unfreeze the SDA line is
mandatory
3. Almost the whole I2C functionality and it's configuration must run in interrupt mode in order to
not slow down the system by use of wait or while() loops.
Proper functionality of the I2C interface can be achieved by implementing a state machine for I2C
configuration and watchdogs monitoring the functionality.
10 To do's
The big challenge now is to fill main.c with some useful code and improve the toolchain
when things are not satisfying or buggy.
Make files should be improved. Changes in header files do not lead to an automatic recompilation of the c-files including the headers.
Page 38 of 41
12 Revision history
Document revision history:
Date
Revision Changes
2012-01-27
0.7.2
Initial release.
2012-02-16
0.7.3
2012-02-28
0.7.4
2012-08-16
0.7.5
2012-08-29
0.7.6
2012-11-09
0.8.1
2013-01-18
0.8.2
2013-04-06
0.8.3
2013-04-15
0.8.4
Page 39 of 41
13 Appendix
13.1 Cortex-M3
Collection of Cortex-M3 related documents.
13.1.1 Intro's
The Insider's Guide To The STM32 ARM Based Microcontroller
http://www.hitex.com/fileadmin/pdf/insiders-guides/stm32/isg-stm32-v18d-scr.pdf
13.1.2 Architecture
Cortex-M3 Technical Reference Manual
http://infocenter.arm.com/help/topic/com.arm.doc.ddi0337i/DDI0337I_cortexm3_r2p1_trm.pdf
13.1.3 MCU
Datasheet STM32F103RB - DS5319
http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/CD00161566.pdf
Page 40 of 41
13.2 Links
Coding Style - how the boss likes the C code in the kernel to look
http://www.kernel.org/doc/Documentation/CodingStyle
Eclipse example project for ST STM32F103RB blinking LED, simplified printf_() function
http://www.freddiechopin.info/index.php/en/download/
www.mikrocontroller.net
http://www.mikrocontroller.net/articles/STM32F10x_Standard_Peripherals_Library
http://www.mikrocontroller.net/articles/STM32
About OpenOCD
http://elk.informatik.fh-augsburg.de/pub/epjournal-1/oocd.html
Page 41 of 41