[update] documentation
[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 are required:
60 \begin{itemize}
61 \item libusb-1.0
62 \item libsg2
63 \end{itemize}
64
65 \paragraph{}
66 The STLINK software source code is retrieved using:\\
67 \begin{small}
68 \begin{lstlisting}[frame=tb]
69 git clone https://github.com/texane/stlink stlink.git
70 \end{lstlisting}
71 \end{small}
72
73 \paragraph{}
74 Everything can be built from the top directory:\\
75 \begin{small}
76 \begin{lstlisting}[frame=tb]
77 $> cd stlink.git ;
78 $> make ;
79 \end{lstlisting}
80 \end{small}
81 It includes:
82 \begin{itemize}
83 \item a communication library (stlink.git/libstlink.a),
84 \item a GDB server (stlink.git/gdbserver/st-util),
85 \item a flash manipulation tool (stlink.git/flash/flash).
86 \end{itemize}
87
88
89 \newpage
90
91 \section{Building and running a program}
92 A simple LED blinking example is provided in the example directory. It is built using:\\
93 \begin{small}
94 \begin{lstlisting}[frame=tb]
95 # update the make option accordingly to your architecture
96 cd stlink.git/example/blink ;
97 PATH=$TOOLCHAIN_PATH/bin:$PATH make CONFIG_STM32L_DISCOVERY=1;
98 \end{lstlisting}
99 \end{small}
100
101 \paragraph{}
102 A GDB server must be start to interact with the STM32. Depending on the discovery kit you
103 are using, you must run one of the 2 commands:\\
104 \begin{small}
105 \begin{lstlisting}[frame=tb]
106 # STM32VL discovery kit
107 $> sudo ./st-util /dev/sg2
108
109 # STM32L discovery kit
110 $> sudo ./st-util
111 \end{lstlisting}
112 \end{small}
113
114 \paragraph{}
115 Then, GDB can be used to interact with the kit:\\
116 \begin{small}
117 \begin{lstlisting}[frame=tb]
118 $> $TOOLCHAIN_PATH/bin/arm-none-eabi-gdb
119 \end{lstlisting}
120 \end{small}
121
122 \paragraph{}
123 From GDB, connect to the server using:\\
124 \begin{small}
125 \begin{lstlisting}[frame=tb]
126 $> target extended localhost:4242
127 \end{lstlisting}
128 \end{small}
129
130 \paragraph{}
131 By default, the program was linked such that the base address is 0x20000000. From the architecture
132 memory map, GDB knows this address belongs to SRAM. To load the program in SRAM, simply use:\\
133 \begin{small}
134 \begin{lstlisting}[frame=tb]
135 $> load blink.elf
136 \end{lstlisting}
137 \end{small}
138
139 \paragraph{}
140 GDB automatically set the PC register to the correct value, 0x20000000 in this case. Then, you
141 can run the program using:\\
142 \begin{small}
143 \begin{lstlisting}[frame=tb]
144 $> continue
145 \end{lstlisting}
146 \end{small}
147
148 \paragraph{}
149 The board BLUE and GREEN leds should be blinking (those leds are near the user and reset buttons).
150
151
152 \newpage
153 \section{Reading and writing to flash}
154 \paragraph{}
155 Flash memory reading and writing is done by a separate tool. A binary running in flash is assumed to
156 be linked against address 0x8000000. The flash tool is then used as shown below:\\
157 \begin{small}
158 \begin{lstlisting}[frame=tb]
159 # change to the flash tool directory
160 $> cd stlink.git/flash ;
161
162 # stlinkv1 command to read 4096 from flash into out.bin
163 $> ./flash read /dev/sg2 out.bin 0x8000000 4096
164
165 # stlinkv2 command
166 $> ./flash read out.bin 0x8000000 4096
167
168 # stlinkv1 command to write the file in.bin into flash
169 $> ./flash write /dev/sg2 in.bin 0x8000000
170
171 # stlinkv2 command
172 $> ./flash write in.bin 0x8000000
173 \end{lstlisting}
174 \end{small}
175
176
177 \newpage
178 \section{Notes}
179
180 \subsection{Disassembling THUMB code in GDB}
181 \paragraph{}
182 By default, the disassemble command in GDB operates in ARM mode. The programs running on CORTEX-M3
183 are compiled in THUMB mode. To correctly disassemble them under GDB, uses an odd address. For instance,
184 if you want to disassemble the code at 0x20000000, use:\\
185 \begin{small}
186 \begin{lstlisting}[frame=tb]
187 $> disassemble 0x20000001
188 \end{lstlisting}
189 \end{small}
190
191
192 \subsection{libstm32l\_discovery}
193 \paragraph{}
194 The repository includes the STM32L discovery library source code from ST original firmware packages,
195 available here:\\
196 \begin{small}
197 \begin{lstlisting}[frame=tb]
198 http://www.st.com/internet/evalboard/product/250990.jsp#FIRMWARE
199 \end{lstlisting}
200 \end{small}
201
202 \paragraph{}
203 It is built using:\\
204 \begin{small}
205 \begin{lstlisting}[frame=tb]
206 $> cd stlink.git/example/libstm32l_discovery/build
207 $> make
208 \end{lstlisting}
209 \end{small}
210
211 \paragraph{}
212 An example using the library can be built using:\\
213 \begin{small}
214 \begin{lstlisting}[frame=tb]
215 $> cd stlink.git/example/lcd
216 $> make
217 \end{lstlisting}
218 \end{small}
219
220 \newpage
221 \section{References}
222 \begin{itemize}
223 \item http://www.st.com/internet/mcu/product/248823.jsp\\
224   documentation related to the STM32L mcu
225 \item http://www.st.com/internet/evalboard/product/250990.jsp\\
226   documentation related to the STM32L discovery kit
227 \end{itemize}
228
229 \end{document}