1 \documentclass[a4paper, 11pt]{article}
11 \title{Using STM32 discovery kits with open source tools}
12 \author{STLINK development team}
19 \addtocontents{toc}{\protect\setcounter{tocdepth}{1}}
26 This guide details the use of STMicroelectronics STM32 discovery kits in
27 an open source environment.
32 \section{Installing a GNU toolchain}
34 Any toolchain supporting the cortex m3 should do. You can find the necessary
35 to install such a toolchain here:\\
37 \begin{lstlisting}[frame=tb]
38 https://github.com/esden/summon-arm-toolchain
43 Details for the installation are provided in the topmost README file.
44 This documentation assumes the toolchains is installed in a \$TOOLCHAIN\_PATH.
49 \section{Installing STLINK}
51 STLINK is open source software to program and debug ST's STM32 Discovery kits. Those
52 kits have an onboard chip that translates USB commands sent by the host PC into
53 JTAG/SWD commands. This chip is called STLINK, (yes, isn't that confusing? suggest a better
54 name!) and comes in 2 versions (STLINK v1 and v2). From a software
55 point of view, those versions differ only in the transport layer used to communicate
56 (v1 uses SCSI passthru commands, while v2 uses raw USB). From a user point of view, they
60 Before continuing, the following dependencies must be met:
68 STLINK should run on any system meeting the above constraints.
71 The STLINK software source code is retrieved using:\\
73 \begin{lstlisting}[frame=tb]
74 $> git clone https://github.com/texane/stlink stlink.git
79 Everything can be built from the top directory:\\
81 \begin{lstlisting}[frame=tb]
90 \item a communication library (stlink.git/libstlink.a),
91 \item a GDB server (stlink.git/st-util),
92 \item a flash manipulation tool (stlink.git/st-flash).
97 \section{Using the GDB server}
99 This assumes you have got the libopencm3 project downloaded in [ocm3]. The
100 libopencm3 project has some good, reliable examples for each of the Discovery boards.
102 Even if you don't plan on using libopencm3, the examples they provide will help you
106 \item Your installed toolchain is capable of compiling for cortex M3/M4 targets
107 \item stlink is functional
108 \item Your arm-none-eabi-gdb is functional
109 \item Your board is functional
113 A GDB server must be started to interact with the STM32. Depending on the discovery kit you
114 are using, you must run one of the 2 commands:\\
116 \begin{lstlisting}[frame=tb]
117 # STM32VL discovery kit (onboard ST-link)
118 $> ./st-util --stlinkv1
120 # STM32L or STM32F4 discovery kit (onboard ST-link/V2)
123 # Full help for other options (listen port, version)
129 Then, GDB can be used to interact with the kit:\\
131 \begin{lstlisting}[frame=tb]
132 $> $TOOLCHAIN_PATH/bin/arm-none-eabi-gdb example_file.elf
137 From GDB, connect to the server using:\\
139 \begin{lstlisting}[frame=tb]
140 (gdb) target extended localhost:4242
145 GDB has memory maps for as many chips as it knows about, and will load your project
146 into either flash or SRAM based on how the project was linked. Linking projects
147 to boot from SRAM is beyond the scope of this document.
149 Because of these built in memory maps, after specifying the .elf at the command line, now
150 we can simply "load" the target:\\
152 \begin{lstlisting}[frame=tb]
158 st-util will load all sections into their appropriate addresses, and "correctly" set the PC
159 register. So, to run your freshly loaded program, simply "continue"\\
161 \begin{lstlisting}[frame=tb]
167 Your program should now be running, and, if you used one of the blinking examples from
168 libopencm3, the LEDs on the board should be blinking for you.
171 \section{Building and flashing a program}
173 If you want to simply flash binary files to arbitrary sections of memory, or
174 read arbitary addresses of memory out to a binary file, use the st-flash tool,
177 \begin{lstlisting}[frame=tb]
179 # stlinkv1 command to read 4096 from flash into out.bin
180 $> ./st-flash read v1 out.bin 0x8000000 4096
183 $> ./st-flash read out.bin 0x8000000 4096
185 # stlinkv1 command to write the file in.bin into flash
186 $> ./st-flash write v1 in.bin 0x8000000
189 $> ./st-flash write in.bin 0x8000000
194 Of course, you can use this instead of the gdb server, if you prefer. Just remember
195 to use the ".bin" image, rather than the .elf file.\\
197 \begin{lstlisting}[frame=tb]
199 # write blink.bin into FLASH
200 $> [sudo] ./st-flash write fancy_blink.bin 0x08000000
205 Upon reset, the board LEDs should be blinking.
210 \subsection{Disassembling THUMB code in GDB}
212 By default, the disassemble command in GDB operates in ARM mode. The programs running on CORTEX-M3
213 are compiled in THUMB mode. To correctly disassemble them under GDB, uses an odd address. For instance,
214 if you want to disassemble the code at 0x20000000, use:\\
216 \begin{lstlisting}[frame=tb]
217 (gdb) disassemble 0x20000001
225 \item http://www.st.com/internet/mcu/product/248823.jsp\\
226 documentation related to the STM32L mcu
227 \item http://www.st.com/internet/evalboard/product/250990.jsp\\
228 documentation related to the STM32L discovery kit
229 \item http://www.libopencm3.org\\
230 libopencm3, a project providing a firmware library, with solid examples for Cortex
231 M3, M4 and M0 processors from any vendor.