Make the blink example build for all platforms.
[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
94 \section{Building and running a program}
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
101 \end{lstlisting}
102 \end{small}
103 This builds three files, one for each of the Discovery boards currently
104 available.
105
106 \paragraph{}
107 A GDB server must be started to interact with the STM32. Depending on the discovery kit you
108 are using, you must run one of the 2 commands:\\
109 \begin{small}
110 \begin{lstlisting}[frame=tb]
111 # STM32VL discovery kit (onboard ST-link)
112 $> sudo ./st-util --stlinkv1 [-d /dev/sg2]
113
114 # STM32L or STM32F4 discovery kit (onboard ST-link/V2)
115 $> sudo ./st-util 
116
117 # Full help for other options (listen port, version)
118 $> ./st-util --help
119 \end{lstlisting}
120 \end{small}
121
122 \paragraph{}
123 Then, GDB can be used to interact with the kit:\\
124 \begin{small}
125 \begin{lstlisting}[frame=tb]
126 $> $TOOLCHAIN_PATH/bin/arm-none-eabi-gdb
127 \end{lstlisting}
128 \end{small}
129
130 \paragraph{}
131 From GDB, connect to the server using:\\
132 \begin{small}
133 \begin{lstlisting}[frame=tb]
134 $> target extended localhost:4242
135 \end{lstlisting}
136 \end{small}
137
138 \paragraph{}
139 By default, the program was linked such that the base address is 0x20000000. From the architecture
140 memory map, GDB knows this address belongs to SRAM. To load the program in SRAM, simply use:\\
141 \begin{small}
142 \begin{lstlisting}[frame=tb]
143 $> load blink.elf
144 \end{lstlisting}
145 \end{small}
146
147 \paragraph{}
148 GDB automatically set the PC register to the correct value, 0x20000000 in this case. Then, you
149 can run the program using:\\
150 \begin{small}
151 \begin{lstlisting}[frame=tb]
152 $> continue
153 \end{lstlisting}
154 \end{small}
155
156 \paragraph{}
157 The board BLUE and GREEN leds should be blinking (those leds are near the user and reset buttons).
158
159
160 \newpage
161 \section{Reading and writing to flash}
162 \paragraph{}
163 Flash memory reading and writing is done by a separate tool. A binary running in flash is assumed to
164 be linked against address 0x8000000. The flash tool is then used as shown below:\\
165 \begin{small}
166 \begin{lstlisting}[frame=tb]
167 # change to the flash tool directory
168 $> cd stlink.git/flash ;
169
170 # stlinkv1 command to read 4096 from flash into out.bin
171 $> ./flash read /dev/sg2 out.bin 0x8000000 4096
172
173 # stlinkv2 command
174 $> ./flash read out.bin 0x8000000 4096
175
176 # stlinkv1 command to write the file in.bin into flash
177 $> ./flash write /dev/sg2 in.bin 0x8000000
178
179 # stlinkv2 command
180 $> ./flash write in.bin 0x8000000
181 \end{lstlisting}
182 \end{small}
183
184
185 \newpage
186 \section{Notes}
187
188 \subsection{Disassembling THUMB code in GDB}
189 \paragraph{}
190 By default, the disassemble command in GDB operates in ARM mode. The programs running on CORTEX-M3
191 are compiled in THUMB mode. To correctly disassemble them under GDB, uses an odd address. For instance,
192 if you want to disassemble the code at 0x20000000, use:\\
193 \begin{small}
194 \begin{lstlisting}[frame=tb]
195 $> disassemble 0x20000001
196 \end{lstlisting}
197 \end{small}
198
199
200 \subsection{libstm32l\_discovery}
201 \paragraph{}
202 The repository includes the STM32L discovery library source code from ST original firmware packages,
203 available here:\\
204 \begin{small}
205 \begin{lstlisting}[frame=tb]
206 http://www.st.com/internet/evalboard/product/250990.jsp#FIRMWARE
207 \end{lstlisting}
208 \end{small}
209
210 \paragraph{}
211 It is built using:\\
212 \begin{small}
213 \begin{lstlisting}[frame=tb]
214 $> cd stlink.git/example/libstm32l_discovery/build
215 $> make
216 \end{lstlisting}
217 \end{small}
218
219 \paragraph{}
220 An example using the library can be built using:\\
221 \begin{small}
222 \begin{lstlisting}[frame=tb]
223 $> cd stlink.git/example/lcd
224 $> make
225 \end{lstlisting}
226 \end{small}
227
228 \subsection{STM32VL support}
229 \paragraph{}
230 It seems support for STM32VL is quite broken. If it does not work, try build STLINK using libsg:
231 \begin{small}
232 \begin{lstlisting}[frame=tb]
233 $> cd stlink.git
234 $> make CONFIG_USE_LIBSG=1
235 \end{lstlisting}
236 \end{small}
237
238
239 \newpage
240 \section{References}
241 \begin{itemize}
242 \item http://www.st.com/internet/mcu/product/248823.jsp\\
243   documentation related to the STM32L mcu
244 \item http://www.st.com/internet/evalboard/product/250990.jsp\\
245   documentation related to the STM32L discovery kit
246 \end{itemize}
247
248 \end{document}