1 Using STM32 discovery kits with open source tools
4 This guide details the use of STMicroelectronics STM32 discovery kits in an open source environment.
6 Installing a GNU toolchain
7 ==========================
9 Any toolchain supporting the cortex m3 should do. You can find the
10 necessary to install such a toolchain here:
13 https://github.com/esden/summon-arm-toolchain
16 Details for the installation are provided in the topmost `README` file.
17 This documentation assumes the toolchains is installed in a
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
33 Before continuing, the following dependencies must be met:
39 STLINK should run on any system meeting the above constraints.
41 The STLINK software source code is retrieved using:
44 $> git clone https://github.com/texane/stlink stlink.git
47 Everything can be built from the top directory:
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).
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
70 Even if you don’t plan on using libopencm3, the examples they provide
71 will help you verify that:
73 - Your installed toolchain is capable of compiling for cortex M3/M4
75 - stlink is functional
76 - Your arm-none-eabi-gdb is functional
77 - Your board is functional
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:
83 # STM32VL discovery kit (onboard ST-link)
84 $> ./st-util --stlinkv1
86 # STM32L or STM32F4 discovery kit (onboard ST-link/V2)
89 # Full help for other options (listen port, version)
93 Then, GDB can be used to interact with the kit:
96 $> $TOOLCHAIN_PATH/bin/arm-none-eabi-gdb example_file.elf
99 From GDB, connect to the server using:
102 (gdb) target extended localhost:4242
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
110 Because of these built in memory maps, after specifying the .elf at the
111 command line, now we can simply “load” the target:
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,
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
129 Building and flashing a program
130 ===============================
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:
138 # stlinkv1 command to read 4096 from flash into out.bin
139 $> ./st-flash read v1 out.bin 0x8000000 4096
142 $> ./st-flash read out.bin 0x8000000 4096
144 # stlinkv1 command to write the file in.bin into flash
145 $> ./st-flash write v1 in.bin 0x8000000
148 $> ./st-flash write in.bin 0x8000000
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.
158 # write blink.bin into FLASH
159 $> [sudo] ./st-flash write fancy_blink.bin 0x08000000
162 Upon reset, the board LEDs should be blinking.
167 Disassembling THUMB code in GDB
168 -------------------------------
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:\
176 (gdb) disassemble 0x20000001
182 - <http://www.st.com/internet/mcu/product/248823.jsp>
183 documentation related to the STM32L mcu
185 - <http://www.st.com/internet/evalboard/product/250990.jsp>
186 documentation related to the STM32L discovery kit
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.