c3615df7818a1dad9f4d08a35ae8639217d9b42d
[fw/stlink] / doc / tutorial / tutorial.tex
1 \documentclass[a4paper, 11pt]{article}
2
3 \usepackage{graphicx}
4 \usepackage{graphics}
5 \usepackage{verbatim}
6 \usepackage{listings}
7 \usepackage{color}
8
9 \begin{document}
10
11 \title{Using STM32 discovery kits with open source tools}
12 \author{STLINK development team}
13 \date{}
14
15 \maketitle
16
17 \newpage
18 \tableofcontents
19 \addtocontents{toc}{\protect\setcounter{tocdepth}{1}}
20
21
22 \newpage
23
24 \section{Overview}
25 \paragraph{}
26 This guide details the use of STMicroelectronics STM32 discovery kits in
27 an open source environment.
28
29
30 \newpage
31
32 \section{Installing a GNU toolchain}
33 \paragraph{}
34 Any toolchain supporting the cortex m3 should do. You can find the necessary
35 to install such a toolchain here:\\
36 \begin{small}
37 \begin{lstlisting}[frame=tb]
38 https://github.com/esden/summon-arm-toolchain
39 \end{lstlisting}
40 \end{small}
41
42 \paragraph{}
43 Details for the installation are provided in the topmost README file.
44 This documentation assumes the toolchains is installed in a \$TOOLCHAIN\_PATH.
45
46
47 \newpage
48
49 \section{Installing STLINK}
50 \paragraph{}
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 
57 are identical. 
58
59 \paragraph{}
60 Before continuing, the following dependencies must be met:
61 \begin{itemize}
62 \item libusb-1.0
63 \end{itemize}
64
65 \paragraph{}
66 STLINK should run on any system meeting the above constraints.
67
68 \paragraph{}
69 The STLINK software source code is retrieved using:\\
70 \begin{small}
71 \begin{lstlisting}[frame=tb]
72 $> git clone https://github.com/texane/stlink stlink.git
73 \end{lstlisting}
74 \end{small}
75
76 \paragraph{}
77 Everything can be built from the top directory:\\
78 \begin{small}
79 \begin{lstlisting}[frame=tb]
80 $> cd stlink.git
81 $> make 
82 \end{lstlisting}
83 \end{small}
84 It includes:
85 \begin{itemize}
86 \item a communication library (stlink.git/libstlink.a),
87 \item a GDB server (stlink.git/gdbserver/st-util),
88 \item a flash manipulation tool (stlink.git/flash/flash).
89 \end{itemize}
90
91
92 \newpage
93 \section{Building and running a program in SRAM}
94 \paragraph{}
95 A simple LED blinking example is provided in the example directory. It is built using:\\
96 \begin{small}
97 \begin{lstlisting}[frame=tb]
98 cd stlink.git/example/blink ;
99 PATH=$TOOLCHAIN_PATH/bin:$PATH make
100 \end{lstlisting}
101 \end{small}
102 This builds three files, one for each of the Discovery boards currently
103 available, linked to run from SRAM. (So no risk of overwriting anything you didn't mean to) 
104 These blink examples can safely be used to verify that:
105
106 \begin{itemize}
107 \item Your installed toolchain is capable of compiling for cortex M3/M4 targets
108 \item stlink is functional
109 \item Your arm-none-eabi-gdb is functional
110 \item Your board is functional
111 \end{itemize}
112
113 \paragraph{}
114 A GDB server must be started to interact with the STM32. Depending on the discovery kit you
115 are using, you must run one of the 2 commands:\\
116 \begin{small}
117 \begin{lstlisting}[frame=tb]
118 # STM32VL discovery kit (onboard ST-link)
119 $> ./st-util --stlinkv1
120
121 # STM32L or STM32F4 discovery kit (onboard ST-link/V2)
122 $> ./st-util 
123
124 # Full help for other options (listen port, version)
125 $> ./st-util --help
126 \end{lstlisting}
127 \end{small}
128
129 \paragraph{}
130 Then, GDB can be used to interact with the kit:\\
131 \begin{small}
132 \begin{lstlisting}[frame=tb]
133 $> $TOOLCHAIN_PATH/bin/arm-none-eabi-gdb
134 \end{lstlisting}
135 \end{small}
136
137 \paragraph{}
138 From GDB, connect to the server using:\\
139 \begin{small}
140 \begin{lstlisting}[frame=tb]
141 $> target extended localhost:4242
142 \end{lstlisting}
143 \end{small}
144
145 \paragraph{}
146 By default, the program was linked such that the base address is 0x20000000. From the architecture
147 memory map, GDB knows this address belongs to SRAM. To load the program in SRAM, simply use:\\
148 \begin{small}
149 \begin{lstlisting}[frame=tb]
150 $> # Choose one as appropriate for your Discovery kit
151 $> load blink_32L.elf | load blink_32VL.elf | load blink_F4.elf
152 \end{lstlisting}
153 \end{small}
154
155 \paragraph{}
156 GDB automatically set the PC register to the correct value, 0x20000000 in this case. Then, you
157 can run the program using:\\
158 \begin{small}
159 \begin{lstlisting}[frame=tb]
160 $> continue
161 \end{lstlisting}
162 \end{small}
163
164 \paragraph{}
165 All the LEDs on the board should now be blinking in time (those leds are near the user and reset buttons).
166
167 \newpage
168 \section{Building and flashing a program}
169 \paragraph{}
170 FLASH memory reading and writing is done by a separate tool, as shown below:\\
171 \begin{small}
172 \begin{lstlisting}[frame=tb]
173 # change to the flash tool directory
174 $> cd stlink.git/flash ;
175
176 # stlinkv1 command to read 4096 from flash into out.bin
177 $> ./flash read v1 out.bin 0x8000000 4096
178
179 # stlinkv2 command
180 $> ./flash read out.bin 0x8000000 4096
181
182 # stlinkv1 command to write the file in.bin into flash
183 $> ./flash write v1 in.bin 0x8000000
184
185 # stlinkv2 command
186 $> ./flash write in.bin 0x8000000
187 \end{lstlisting}
188 \end{small}
189
190 \paragraph{}
191 A LED blinking example is provided:\\
192 \begin{small}
193 \begin{lstlisting}[frame=tb]
194 # build the example, resulting in blink.bin
195 $> cd stlink.git/example/blink_flash
196 $> PATH=$TOOLCHAIN_PATH:$PATH make CONFIG_STM32L_DISCOVERY=1
197
198 # write blink.bin into FLASH
199 $> sudo ./flash write blink.bin 0x08000000
200 \end{lstlisting}
201 \end{small}
202
203 \paragraph{}
204 Upon reset, the board LEDs should be blinking.
205
206 \newpage
207 \section{Building and installing the CHIBIOS kernel}
208 \paragraph{}
209 CHIBIOS is an open source RTOS. More information can be found on the project website:
210 \begin{center}
211 http://www.chibios.org/dokuwiki/doku.php
212 \end{center}
213
214 \paragraph{}
215 It supports several boards, including the STM32L DISCOVERY kit:
216 \begin{center}
217 http://www.chibios.org/dokuwiki/doku.php?id=chibios:articles:stm32l\_discovery
218 \end{center}
219
220 \paragraph{}
221 The installation procedure is detailed below:\\
222 \begin{small}
223 \begin{lstlisting}[frame=tb]
224 # checkout and build CHIBIOS for STM32L DISCOVERY kits
225 svn checkout https://chibios.svn.sourceforge.net/svnroot/chibios/trunk
226 cd chibios/trunk/demos/ARMCM3-STM32L152-DISCOVERY
227 PATH=$TOOLCHAIN_PATH:$PATH make
228
229 # flash the image into STM32L
230 sudo ./flash write build/ch.bin 0x08000000
231 \end{lstlisting}
232 \end{small}
233
234 \newpage
235 \section{Notes}
236
237 \subsection{Disassembling THUMB code in GDB}
238 \paragraph{}
239 By default, the disassemble command in GDB operates in ARM mode. The programs running on CORTEX-M3
240 are compiled in THUMB mode. To correctly disassemble them under GDB, uses an odd address. For instance,
241 if you want to disassemble the code at 0x20000000, use:\\
242 \begin{small}
243 \begin{lstlisting}[frame=tb]
244 $> disassemble 0x20000001
245 \end{lstlisting}
246 \end{small}
247
248
249 \subsection{libstm32l\_discovery}
250 \paragraph{}
251 The repository includes the STM32L discovery library source code from ST original firmware packages,
252 available here:\\
253 \begin{small}
254 \begin{lstlisting}[frame=tb]
255 http://www.st.com/internet/evalboard/product/250990.jsp#FIRMWARE
256 \end{lstlisting}
257 \end{small}
258
259 \paragraph{}
260 It is built using:\\
261 \begin{small}
262 \begin{lstlisting}[frame=tb]
263 $> cd stlink.git/example/libstm32l_discovery/build
264 $> make
265 \end{lstlisting}
266 \end{small}
267
268 \paragraph{}
269 An example using the library can be built using:\\
270 \begin{small}
271 \begin{lstlisting}[frame=tb]
272 $> cd stlink.git/example/lcd
273 $> make
274 \end{lstlisting}
275 \end{small}
276
277
278 \newpage
279 \section{References}
280 \begin{itemize}
281 \item http://www.st.com/internet/mcu/product/248823.jsp\\
282   documentation related to the STM32L mcu
283 \item http://www.st.com/internet/evalboard/product/250990.jsp\\
284   documentation related to the STM32L discovery kit
285 \end{itemize}
286
287 \end{document}