Update tutorial docs and flash writing/reading
[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
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 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{Reading and writing to flash}
169 \paragraph{}
170 Flash memory reading and writing is done by a separate tool. A binary running in flash is assumed to
171 be linked against address 0x8000000. The flash tool is then used 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
192 \newpage
193 \section{Notes}
194
195 \subsection{Disassembling THUMB code in GDB}
196 \paragraph{}
197 By default, the disassemble command in GDB operates in ARM mode. The programs running on CORTEX-M3
198 are compiled in THUMB mode. To correctly disassemble them under GDB, uses an odd address. For instance,
199 if you want to disassemble the code at 0x20000000, use:\\
200 \begin{small}
201 \begin{lstlisting}[frame=tb]
202 $> disassemble 0x20000001
203 \end{lstlisting}
204 \end{small}
205
206
207 \subsection{libstm32l\_discovery}
208 \paragraph{}
209 The repository includes the STM32L discovery library source code from ST original firmware packages,
210 available here:\\
211 \begin{small}
212 \begin{lstlisting}[frame=tb]
213 http://www.st.com/internet/evalboard/product/250990.jsp#FIRMWARE
214 \end{lstlisting}
215 \end{small}
216
217 \paragraph{}
218 It is built using:\\
219 \begin{small}
220 \begin{lstlisting}[frame=tb]
221 $> cd stlink.git/example/libstm32l_discovery/build
222 $> make
223 \end{lstlisting}
224 \end{small}
225
226 \paragraph{}
227 An example using the library can be built using:\\
228 \begin{small}
229 \begin{lstlisting}[frame=tb]
230 $> cd stlink.git/example/lcd
231 $> make
232 \end{lstlisting}
233 \end{small}
234
235
236 \newpage
237 \section{References}
238 \begin{itemize}
239 \item http://www.st.com/internet/mcu/product/248823.jsp\\
240   documentation related to the STM32L mcu
241 \item http://www.st.com/internet/evalboard/product/250990.jsp\\
242   documentation related to the STM32L discovery kit
243 \end{itemize}
244
245 \end{document}