Further Refactor stlink.h into correct places and files, create backend.h
[fw/stlink] / doc / tutorial.md
1 Using STM32 discovery kits with open source tools
2 ========
3
4 This guide details the use of STMicroelectronics STM32 discovery kits in an open source environment.
5
6 Installing a GNU toolchain
7 ==========================
8
9 Any toolchain supporting the cortex m3 should do. You can find the
10 necessary to install such a toolchain here:
11
12 ```
13 https://github.com/esden/summon-arm-toolchain
14 ```
15
16 Details for the installation are provided in the topmost `README` file.
17 This documentation assumes the toolchains is installed in a
18 `$TOOLCHAIN_PATH`.
19
20 Installing STLINK
21 =================
22
23 STLINK is open source software to program and debug ST’s STM32 Discovery
24 kits. Those kits have an onboard chip that translates USB commands sent
25 by the host PC into JTAG/SWD commands. This chip is called STLINK, (yes,
26 isn’t that confusing? suggest a better name!) and comes in 2 versions
27 (STLINK v1 and v2). From a software point of view, those versions differ
28 only in the transport layer used to communicate (v1 uses SCSI passthru
29 commands, while v2 uses raw USB). From a user point of view, they are
30 identical.
31
32
33 Before continuing, the following dependencies must be met:
34
35 -   libusb-1.0
36 -   pkg-config
37 -   autotools
38
39 STLINK should run on any system meeting the above constraints. 
40
41 The STLINK software source code is retrieved using:
42
43 ```
44 $> git clone https://github.com/texane/stlink stlink.git
45 ```
46
47 Everything can be built from the top directory:
48
49 ```
50 $> cd stlink.git
51 $> ./autogen.sh 
52 $> ./configure
53 $> make
54 ```
55
56 It includes:
57
58 -   a communication library (stlink.git/libstlink.a),
59 -   a GDB server (stlink.git/st-util),
60 -   a flash manipulation tool (stlink.git/st-flash).
61
62 Using the GDB server
63 ====================
64  
65
66 This assumes you have got the libopencm3 project downloaded in `ocm3`.
67 The libopencm3 project has some good, reliable examples for each of the
68 Discovery boards.
69
70 Even if you don’t plan on using libopencm3, the examples they provide
71 will help you verify that:
72
73 -   Your installed toolchain is capable of compiling for cortex M3/M4
74     targets
75 -   stlink is functional
76 -   Your arm-none-eabi-gdb is functional
77 -   Your board is functional
78
79 A GDB server must be started to interact with the STM32. Depending on
80 the discovery kit you are using, you must run one of the 2 commands:
81
82 ```
83 # STM32VL discovery kit (onboard ST-link)
84 $> ./st-util --stlinkv1
85
86 # STM32L or STM32F4 discovery kit (onboard ST-link/V2)
87 $> ./st-util 
88
89 # Full help for other options (listen port, version)
90 $> ./st-util --help
91 ``` 
92
93 Then, GDB can be used to interact with the kit:
94
95 ```
96 $> $TOOLCHAIN_PATH/bin/arm-none-eabi-gdb example_file.elf
97 ```
98
99 From GDB, connect to the server using:
100
101 ```
102 (gdb) target extended localhost:4242
103 ```
104
105 GDB has memory maps for as many chips as it knows about, and will load
106 your project into either flash or SRAM based on how the project was
107 linked. Linking projects to boot from SRAM is beyond the scope of this
108 document.
109
110 Because of these built in memory maps, after specifying the .elf at the
111 command line, now we can simply “load” the target:
112
113 ```
114 (gdb) load
115 ``` 
116
117 st-util will load all sections into their appropriate addresses, and
118 “correctly” set the PC register. So, to run your freshly loaded program,
119 simply “continue”
120
121 ```
122 (gdb) continue
123 ```
124
125 Your program should now be running, and, if you used one of the blinking
126 examples from libopencm3, the LEDs on the board should be blinking for
127 you.
128
129 Building and flashing a program
130 ===============================
131
132 If you want to simply flash binary files to arbitrary sections of
133 memory, or read arbitary addresses of memory out to a binary file, use
134 the st-flash tool, as shown below:
135
136 ```
137
138 # stlinkv1 command to read 4096 from flash into out.bin
139 $> ./st-flash read v1 out.bin 0x8000000 4096
140
141 # stlinkv2 command
142 $> ./st-flash read out.bin 0x8000000 4096
143
144 # stlinkv1 command to write the file in.bin into flash
145 $> ./st-flash write v1 in.bin 0x8000000
146
147 # stlinkv2 command
148 $> ./st-flash write in.bin 0x8000000
149 ```
150
151 #### 
152
153 Of course, you can use this instead of the gdb server, if you prefer.
154 Just remember to use the “.bin” image, rather than the .elf file.
155
156 ```
157
158 # write blink.bin into FLASH
159 $> [sudo] ./st-flash write fancy_blink.bin 0x08000000
160 ```
161
162 Upon reset, the board LEDs should be blinking.
163
164 Notes
165 =====
166
167 Disassembling THUMB code in GDB
168 -------------------------------
169
170 By default, the disassemble command in GDB operates in ARM mode. The
171 programs running on CORTEX-M3 are compiled in THUMB mode. To correctly
172 disassemble them under GDB, uses an odd address. For instance, if you
173 want to disassemble the code at 0x20000000, use:\
174
175 ```
176 (gdb) disassemble 0x20000001
177 ```
178
179 References
180 ==========
181
182 -   <http://www.st.com/internet/mcu/product/248823.jsp>
183     documentation related to the STM32L mcu
184
185 -   <http://www.st.com/internet/evalboard/product/250990.jsp>
186     documentation related to the STM32L discovery kit
187
188 -   <http://www.libopencm3.org>
189     libopencm3, a project providing a firmware library, with solid
190     examples for Cortex M3, M4 and M0 processors from any vendor.