[update] tutorial, led blinking from flash
[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 opensource 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 an opensource software to program and debug the discovery kits. Those
52 kits have an onboard chip that translates USB commands sent by the host PC into
53 JTAG commands. This chip is called STLINK, which is confusing since the software
54 has the same name. It comes into 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).
57
58 \paragraph{}
59 Before continuing, the following dependencies must be met:
60 \begin{itemize}
61 \item libusb-1.0
62 \item libsgutils2 (optionnal)
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 CONFIG_USE_LIBSG=0
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 # update the make option accordingly to your architecture
99 cd stlink.git/example/blink ;
100 PATH=$TOOLCHAIN_PATH/bin:$PATH make CONFIG_STM32L_DISCOVERY=1;
101 \end{lstlisting}
102 \end{small}
103
104 \paragraph{}
105 A GDB server must be start to interact with the STM32. Depending on the discovery kit you
106 are using, you must run one of the 2 commands:\\
107 \begin{small}
108 \begin{lstlisting}[frame=tb]
109 # STM32VL discovery kit
110 $> sudo ./st-util /dev/sg2
111
112 # STM32L discovery kit
113 # 2 dummy command line arguments needed, will be fixed soon
114 $> sudo ./st-util fu bar
115 \end{lstlisting}
116 \end{small}
117
118 \paragraph{}
119 Then, GDB can be used to interact with the kit:\\
120 \begin{small}
121 \begin{lstlisting}[frame=tb]
122 $> $TOOLCHAIN_PATH/bin/arm-none-eabi-gdb
123 \end{lstlisting}
124 \end{small}
125
126 \paragraph{}
127 From GDB, connect to the server using:\\
128 \begin{small}
129 \begin{lstlisting}[frame=tb]
130 $> target extended localhost:4242
131 \end{lstlisting}
132 \end{small}
133
134 \paragraph{}
135 By default, the program was linked such that the base address is 0x20000000. From the architecture
136 memory map, GDB knows this address belongs to SRAM. To load the program in SRAM, simply use:\\
137 \begin{small}
138 \begin{lstlisting}[frame=tb]
139 $> load blink.elf
140 \end{lstlisting}
141 \end{small}
142
143 \paragraph{}
144 GDB automatically set the PC register to the correct value, 0x20000000 in this case. Then, you
145 can run the program using:\\
146 \begin{small}
147 \begin{lstlisting}[frame=tb]
148 $> continue
149 \end{lstlisting}
150 \end{small}
151
152 \paragraph{}
153 The board BLUE and GREEN leds should be blinking (those leds are near the user and reset buttons).
154
155
156 \newpage
157 \section{Building and flashing a program}
158 \paragraph{}
159 FLASH memory reading and writing is done by a separate tool, as shown below:\\
160 \begin{small}
161 \begin{lstlisting}[frame=tb]
162 # change to the flash tool directory
163 $> cd stlink.git/flash ;
164
165 # stlinkv1 command to read 4096 from flash into out.bin
166 $> ./flash read /dev/sg2 out.bin 0x8000000 4096
167
168 # stlinkv2 command
169 $> ./flash read out.bin 0x8000000 4096
170
171 # stlinkv1 command to write the file in.bin into flash
172 $> ./flash write /dev/sg2 in.bin 0x8000000
173
174 # stlinkv2 command
175 $> ./flash write in.bin 0x8000000
176 \end{lstlisting}
177 \end{small}
178
179 \paragraph{}
180 A LED blinking example is provided:\\
181 \begin{small}
182 \begin{lstlisting}[frame=tb]
183 # build the example, resulting in blink.bin
184 $> cd stlink.git/example/blink_flash
185 $> PATH=$TOOLCHAIN_PATH:$PATH make CONFIG_STM32L_DISCOVERY=1
186
187 # write blink.bin into FLASH
188 $> sudo ./flash write blink.bin 0x08000000
189 \end{lstlisting}
190 \end{small}
191
192 \paragraph{}
193 Upon reset, the board LEDs should be blinking.
194
195 \newpage
196 \section{Building and installing the CHIBIOS kernel}
197 \paragraph{}
198 CHIBIOS is an open source RTOS. More information can be found on the project website:
199 \begin{center}
200 http://www.chibios.org/dokuwiki/doku.php
201 \end{center}
202
203 \paragraph{}
204 It supports several boards, including the STM32L DISCOVERY kit:
205 \begin{center}
206 http://www.chibios.org/dokuwiki/doku.php?id=chibios:articles:stm32l\_discovery
207 \end{center}
208
209 \paragraph{}
210 The installation procedure is detailed below:\\
211 \begin{small}
212 \begin{lstlisting}[frame=tb]
213 # checkout and build CHIBIOS for STM32L DISCOVERY kits
214 svn checkout https://chibios.svn.sourceforge.net/svnroot/chibios/trunk
215 cd chibios/trunk/demos/ARMCM3-STM32L152-DISCOVERY
216 PATH=$TOOLCHAIN_PATH:$PATH make
217
218 # flash the image into STM32L
219 sudo ./flash write build/ch.bin 0x08000000
220 \end{lstlisting}
221 \end{small}
222
223 \newpage
224 \section{Notes}
225
226 \subsection{Disassembling THUMB code in GDB}
227 \paragraph{}
228 By default, the disassemble command in GDB operates in ARM mode. The programs running on CORTEX-M3
229 are compiled in THUMB mode. To correctly disassemble them under GDB, uses an odd address. For instance,
230 if you want to disassemble the code at 0x20000000, use:\\
231 \begin{small}
232 \begin{lstlisting}[frame=tb]
233 $> disassemble 0x20000001
234 \end{lstlisting}
235 \end{small}
236
237
238 \subsection{libstm32l\_discovery}
239 \paragraph{}
240 The repository includes the STM32L discovery library source code from ST original firmware packages,
241 available here:\\
242 \begin{small}
243 \begin{lstlisting}[frame=tb]
244 http://www.st.com/internet/evalboard/product/250990.jsp#FIRMWARE
245 \end{lstlisting}
246 \end{small}
247
248 \paragraph{}
249 It is built using:\\
250 \begin{small}
251 \begin{lstlisting}[frame=tb]
252 $> cd stlink.git/example/libstm32l_discovery/build
253 $> make
254 \end{lstlisting}
255 \end{small}
256
257 \paragraph{}
258 An example using the library can be built using:\\
259 \begin{small}
260 \begin{lstlisting}[frame=tb]
261 $> cd stlink.git/example/lcd
262 $> make
263 \end{lstlisting}
264 \end{small}
265
266 \subsection{STM32VL support}
267 \paragraph{}
268 It seems support for STM32VL is quite broken. If it does not work, try build STLINK using libsg:
269 \begin{small}
270 \begin{lstlisting}[frame=tb]
271 $> cd stlink.git
272 $> make CONFIG_USE_LIBSG=1
273 \end{lstlisting}
274 \end{small}
275
276
277 \newpage
278 \section{References}
279 \begin{itemize}
280 \item http://www.st.com/internet/mcu/product/248823.jsp\\
281   documentation related to the STM32L mcu
282 \item http://www.st.com/internet/evalboard/product/250990.jsp\\
283   documentation related to the STM32L discovery kit
284 \end{itemize}
285
286 \end{document}