Add pkg-config to the list of requirements.
[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 \item pkg-config
64 \end{itemize}
65
66 \paragraph{}
67 STLINK should run on any system meeting the above constraints.
68
69 \paragraph{}
70 The STLINK software source code is retrieved using:\\
71 \begin{small}
72 \begin{lstlisting}[frame=tb]
73 $> git clone https://github.com/texane/stlink stlink.git
74 \end{lstlisting}
75 \end{small}
76
77 \paragraph{}
78 Everything can be built from the top directory:\\
79 \begin{small}
80 \begin{lstlisting}[frame=tb]
81 $> cd stlink.git
82 $> make 
83 \end{lstlisting}
84 \end{small}
85 It includes:
86 \begin{itemize}
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).
90 \end{itemize}
91
92
93 \newpage
94 \section{Building and running a program in SRAM}
95 \paragraph{}
96 A simple LED blinking example is provided in the example directory. It is built using:\\
97 \begin{small}
98 \begin{lstlisting}[frame=tb]
99 cd stlink.git/example/blink ;
100 PATH=$TOOLCHAIN_PATH/bin:$PATH make
101 \end{lstlisting}
102 \end{small}
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:
106
107 \begin{itemize}
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
112 \end{itemize}
113
114 \paragraph{}
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:\\
117 \begin{small}
118 \begin{lstlisting}[frame=tb]
119 # STM32VL discovery kit (onboard ST-link)
120 $> ./st-util --stlinkv1
121
122 # STM32L or STM32F4 discovery kit (onboard ST-link/V2)
123 $> ./st-util 
124
125 # Full help for other options (listen port, version)
126 $> ./st-util --help
127 \end{lstlisting}
128 \end{small}
129
130 \paragraph{}
131 Then, GDB can be used to interact with the kit:\\
132 \begin{small}
133 \begin{lstlisting}[frame=tb]
134 $> $TOOLCHAIN_PATH/bin/arm-none-eabi-gdb
135 \end{lstlisting}
136 \end{small}
137
138 \paragraph{}
139 From GDB, connect to the server using:\\
140 \begin{small}
141 \begin{lstlisting}[frame=tb]
142 $> target extended localhost:4242
143 \end{lstlisting}
144 \end{small}
145
146 \paragraph{}
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:\\
149 \begin{small}
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
153 \end{lstlisting}
154 \end{small}
155
156 \paragraph{}
157 GDB automatically set the PC register to the correct value, 0x20000000 in this case. Then, you
158 can run the program using:\\
159 \begin{small}
160 \begin{lstlisting}[frame=tb]
161 $> continue
162 \end{lstlisting}
163 \end{small}
164
165 \paragraph{}
166 All the LEDs on the board should now be blinking in time (those leds are near the user and reset buttons).
167
168 \newpage
169 \section{Building and flashing a program}
170 \paragraph{}
171 FLASH memory reading and writing is done by a separate tool, as shown below:\\
172 \begin{small}
173 \begin{lstlisting}[frame=tb]
174 # change to the flash tool directory
175 $> cd stlink.git/flash ;
176
177 # stlinkv1 command to read 4096 from flash into out.bin
178 $> ./flash read v1 out.bin 0x8000000 4096
179
180 # stlinkv2 command
181 $> ./flash read out.bin 0x8000000 4096
182
183 # stlinkv1 command to write the file in.bin into flash
184 $> ./flash write v1 in.bin 0x8000000
185
186 # stlinkv2 command
187 $> ./flash write in.bin 0x8000000
188 \end{lstlisting}
189 \end{small}
190
191 \paragraph{}
192 A LED blinking example is provided:\\
193 \begin{small}
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
198
199 # write blink.bin into FLASH
200 $> sudo ./flash write blink.bin 0x08000000
201 \end{lstlisting}
202 \end{small}
203
204 \paragraph{}
205 Upon reset, the board LEDs should be blinking.
206
207 \newpage
208 \section{Building and installing the CHIBIOS kernel}
209 \paragraph{}
210 CHIBIOS is an open source RTOS. More information can be found on the project website:
211 \begin{center}
212 http://www.chibios.org/dokuwiki/doku.php
213 \end{center}
214
215 \paragraph{}
216 It supports several boards, including the STM32L DISCOVERY kit:
217 \begin{center}
218 http://www.chibios.org/dokuwiki/doku.php?id=chibios:articles:stm32l\_discovery
219 \end{center}
220
221 \paragraph{}
222 The installation procedure is detailed below:\\
223 \begin{small}
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
229
230 # flash the image into STM32L
231 sudo ./flash write build/ch.bin 0x08000000
232 \end{lstlisting}
233 \end{small}
234
235 \newpage
236 \section{Notes}
237
238 \subsection{Disassembling THUMB code in GDB}
239 \paragraph{}
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:\\
243 \begin{small}
244 \begin{lstlisting}[frame=tb]
245 $> disassemble 0x20000001
246 \end{lstlisting}
247 \end{small}
248
249
250 \subsection{libstm32l\_discovery}
251 \paragraph{}
252 The repository includes the STM32L discovery library source code from ST original firmware packages,
253 available here:\\
254 \begin{small}
255 \begin{lstlisting}[frame=tb]
256 http://www.st.com/internet/evalboard/product/250990.jsp#FIRMWARE
257 \end{lstlisting}
258 \end{small}
259
260 \paragraph{}
261 It is built using:\\
262 \begin{small}
263 \begin{lstlisting}[frame=tb]
264 $> cd stlink.git/example/libstm32l_discovery/build
265 $> make
266 \end{lstlisting}
267 \end{small}
268
269 \paragraph{}
270 An example using the library can be built using:\\
271 \begin{small}
272 \begin{lstlisting}[frame=tb]
273 $> cd stlink.git/example/lcd
274 $> make
275 \end{lstlisting}
276 \end{small}
277
278
279 \newpage
280 \section{References}
281 \begin{itemize}
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
286 \end{itemize}
287
288 \end{document}