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