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:
67 STLINK should run on any system meeting the above constraints.
70 The STLINK software source code is retrieved using:\\
72 \begin{lstlisting}[frame=tb]
73 $> git clone https://github.com/texane/stlink stlink.git
78 Everything can be built from the top directory:\\
80 \begin{lstlisting}[frame=tb]
87 \item a communication library (stlink.git/libstlink.a),
88 \item a GDB server (stlink.git/gdbserver/st-util),
89 \item a flash manipulation tool (stlink.git/flash/flash).
94 \section{Building and running a program in SRAM}
96 A simple LED blinking example is provided in the example directory. It is built using:\\
98 \begin{lstlisting}[frame=tb]
99 cd stlink.git/example/blink ;
100 PATH=$TOOLCHAIN_PATH/bin:$PATH make
103 This builds three files, one for each of the Discovery boards currently
104 available, linked to run from SRAM. (So no risk of overwriting anything you didn't mean to)
105 These blink examples can safely be used to verify that:
108 \item Your installed toolchain is capable of compiling for cortex M3/M4 targets
109 \item stlink is functional
110 \item Your arm-none-eabi-gdb is functional
111 \item Your board is functional
115 A GDB server must be started to interact with the STM32. Depending on the discovery kit you
116 are using, you must run one of the 2 commands:\\
118 \begin{lstlisting}[frame=tb]
119 # STM32VL discovery kit (onboard ST-link)
120 $> ./st-util --stlinkv1
122 # STM32L or STM32F4 discovery kit (onboard ST-link/V2)
125 # Full help for other options (listen port, version)
131 Then, GDB can be used to interact with the kit:\\
133 \begin{lstlisting}[frame=tb]
134 $> $TOOLCHAIN_PATH/bin/arm-none-eabi-gdb
139 From GDB, connect to the server using:\\
141 \begin{lstlisting}[frame=tb]
142 $> target extended localhost:4242
147 By default, the program was linked such that the base address is 0x20000000. From the architecture
148 memory map, GDB knows this address belongs to SRAM. To load the program in SRAM, simply use:\\
150 \begin{lstlisting}[frame=tb]
151 $> # Choose one as appropriate for your Discovery kit
152 $> load blink_32L.elf | load blink_32VL.elf | load blink_F4.elf
157 GDB automatically set the PC register to the correct value, 0x20000000 in this case. Then, you
158 can run the program using:\\
160 \begin{lstlisting}[frame=tb]
166 All the LEDs on the board should now be blinking in time (those leds are near the user and reset buttons).
169 \section{Building and flashing a program}
171 FLASH memory reading and writing is done by a separate tool, as shown below:\\
173 \begin{lstlisting}[frame=tb]
174 # change to the flash tool directory
175 $> cd stlink.git/flash ;
177 # stlinkv1 command to read 4096 from flash into out.bin
178 $> ./flash read v1 out.bin 0x8000000 4096
181 $> ./flash read out.bin 0x8000000 4096
183 # stlinkv1 command to write the file in.bin into flash
184 $> ./flash write v1 in.bin 0x8000000
187 $> ./flash write in.bin 0x8000000
192 A LED blinking example is provided:\\
194 \begin{lstlisting}[frame=tb]
195 # build the example, resulting in blink.bin
196 $> cd stlink.git/example/blink_flash
197 $> PATH=$TOOLCHAIN_PATH:$PATH make CONFIG_STM32L_DISCOVERY=1
199 # write blink.bin into FLASH
200 $> sudo ./flash write blink.bin 0x08000000
205 Upon reset, the board LEDs should be blinking.
208 \section{Building and installing the CHIBIOS kernel}
210 CHIBIOS is an open source RTOS. More information can be found on the project website:
212 http://www.chibios.org/dokuwiki/doku.php
216 It supports several boards, including the STM32L DISCOVERY kit:
218 http://www.chibios.org/dokuwiki/doku.php?id=chibios:articles:stm32l\_discovery
222 The installation procedure is detailed below:\\
224 \begin{lstlisting}[frame=tb]
225 # checkout and build CHIBIOS for STM32L DISCOVERY kits
226 svn checkout https://chibios.svn.sourceforge.net/svnroot/chibios/trunk
227 cd chibios/trunk/demos/ARMCM3-STM32L152-DISCOVERY
228 PATH=$TOOLCHAIN_PATH:$PATH make
230 # flash the image into STM32L
231 sudo ./flash write build/ch.bin 0x08000000
238 \subsection{Disassembling THUMB code in GDB}
240 By default, the disassemble command in GDB operates in ARM mode. The programs running on CORTEX-M3
241 are compiled in THUMB mode. To correctly disassemble them under GDB, uses an odd address. For instance,
242 if you want to disassemble the code at 0x20000000, use:\\
244 \begin{lstlisting}[frame=tb]
245 $> disassemble 0x20000001
250 \subsection{libstm32l\_discovery}
252 The repository includes the STM32L discovery library source code from ST original firmware packages,
255 \begin{lstlisting}[frame=tb]
256 http://www.st.com/internet/evalboard/product/250990.jsp#FIRMWARE
263 \begin{lstlisting}[frame=tb]
264 $> cd stlink.git/example/libstm32l_discovery/build
270 An example using the library can be built using:\\
272 \begin{lstlisting}[frame=tb]
273 $> cd stlink.git/example/lcd
282 \item http://www.st.com/internet/mcu/product/248823.jsp\\
283 documentation related to the STM32L mcu
284 \item http://www.st.com/internet/evalboard/product/250990.jsp\\
285 documentation related to the STM32L discovery kit