Align loader to 32-bit boundary
[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 \item autotools
65 \end{itemize}
66
67 \paragraph{}
68 STLINK should run on any system meeting the above constraints.
69
70 \paragraph{}
71 The STLINK software source code is retrieved using:\\
72 \begin{small}
73 \begin{lstlisting}[frame=tb]
74 $> git clone https://github.com/texane/stlink stlink.git
75 \end{lstlisting}
76 \end{small}
77
78 \paragraph{}
79 Everything can be built from the top directory:\\
80 \begin{small}
81 \begin{lstlisting}[frame=tb]
82 $> cd stlink.git
83 $> ./autogen.sh 
84 $> ./configure
85 $> make
86 \end{lstlisting}
87 \end{small}
88 It includes:
89 \begin{itemize}
90 \item a communication library (stlink.git/libstlink.a),
91 \item a GDB server (stlink.git/st-util),
92 \item a flash manipulation tool (stlink.git/st-flash).
93 \end{itemize}
94
95
96 \newpage
97 \section{Using the GDB server}
98 \paragraph{}
99 This assumes you have got the libopencm3 project downloaded in [ocm3].  The
100 libopencm3 project has some good, reliable examples for each of the Discovery boards.
101
102 Even if you don't plan on using libopencm3, the examples they provide will help you 
103 verify that:
104
105 \begin{itemize}
106 \item Your installed toolchain is capable of compiling for cortex M3/M4 targets
107 \item stlink is functional
108 \item Your arm-none-eabi-gdb is functional
109 \item Your board is functional
110 \end{itemize}
111
112 \paragraph{}
113 A GDB server must be started to interact with the STM32. Depending on the discovery kit you
114 are using, you must run one of the 2 commands:\\
115 \begin{small}
116 \begin{lstlisting}[frame=tb]
117 # STM32VL discovery kit (onboard ST-link)
118 $> ./st-util --stlinkv1
119
120 # STM32L or STM32F4 discovery kit (onboard ST-link/V2)
121 $> ./st-util 
122
123 # Full help for other options (listen port, version)
124 $> ./st-util --help
125 \end{lstlisting}
126 \end{small}
127
128 \paragraph{}
129 Then, GDB can be used to interact with the kit:\\
130 \begin{small}
131 \begin{lstlisting}[frame=tb]
132 $> $TOOLCHAIN_PATH/bin/arm-none-eabi-gdb example_file.elf
133 \end{lstlisting}
134 \end{small}
135
136 \paragraph{}
137 From GDB, connect to the server using:\\
138 \begin{small}
139 \begin{lstlisting}[frame=tb]
140 (gdb) target extended localhost:4242
141 \end{lstlisting}
142 \end{small}
143
144 \paragraph{}
145 GDB has memory maps for as many chips as it knows about, and will load your project
146 into either flash or SRAM based on how the project was linked.  Linking projects
147 to boot from SRAM is beyond the scope of this document.
148
149 Because of these built in memory maps, after specifying the .elf at the command line, now
150 we can simply "load" the target:\\
151 \begin{small}
152 \begin{lstlisting}[frame=tb]
153 (gdb) load
154 \end{lstlisting}
155 \end{small}
156
157 \paragraph{}
158 st-util will load all sections into their appropriate addresses, and "correctly" set the PC
159 register.  So, to run your freshly loaded program, simply "continue"\\
160 \begin{small}
161 \begin{lstlisting}[frame=tb]
162 (gdb) continue
163 \end{lstlisting}
164 \end{small}
165
166 \paragraph{}
167 Your program should now be running, and, if you used one of the blinking examples from
168 libopencm3, the LEDs on the board should be blinking for you.
169
170 \newpage
171 \section{Building and flashing a program}
172 \paragraph{}
173 If you want to simply flash binary files to arbitrary sections of memory, or
174 read arbitary addresses of memory out to a binary file, use the st-flash tool, 
175 as shown below:\\
176 \begin{small}
177 \begin{lstlisting}[frame=tb]
178
179 # stlinkv1 command to read 4096 from flash into out.bin
180 $> ./st-flash read v1 out.bin 0x8000000 4096
181
182 # stlinkv2 command
183 $> ./st-flash read out.bin 0x8000000 4096
184
185 # stlinkv1 command to write the file in.bin into flash
186 $> ./st-flash write v1 in.bin 0x8000000
187
188 # stlinkv2 command
189 $> ./st-flash write in.bin 0x8000000
190 \end{lstlisting}
191 \end{small}
192
193 \paragraph{}
194 Of course, you can use this instead of the gdb server, if you prefer.  Just remember
195 to use the ".bin" image, rather than the .elf file.\\
196 \begin{small}
197 \begin{lstlisting}[frame=tb]
198
199 # write blink.bin into FLASH
200 $> [sudo] ./st-flash write fancy_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{Notes}
209
210 \subsection{Disassembling THUMB code in GDB}
211 \paragraph{}
212 By default, the disassemble command in GDB operates in ARM mode. The programs running on CORTEX-M3
213 are compiled in THUMB mode. To correctly disassemble them under GDB, uses an odd address. For instance,
214 if you want to disassemble the code at 0x20000000, use:\\
215 \begin{small}
216 \begin{lstlisting}[frame=tb]
217 (gdb) disassemble 0x20000001
218 \end{lstlisting}
219 \end{small}
220
221
222 \newpage
223 \section{References}
224 \begin{itemize}
225 \item http://www.st.com/internet/mcu/product/248823.jsp\\
226   documentation related to the STM32L mcu
227 \item http://www.st.com/internet/evalboard/product/250990.jsp\\
228   documentation related to the STM32L discovery kit
229 \item http://www.libopencm3.org\\
230   libopencm3, a project providing a firmware library, with solid examples for Cortex
231   M3, M4 and M0 processors from any vendor.
232 \end{itemize}
233
234 \end{document}