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