Difference between revisions of "STM32 Starter Guide"

From Eleccelerator Wiki
Jump to: navigation, search
(Using the GNU ARM Plugin)
 
(3 intermediate revisions by one user not shown)
Line 1: Line 1:
 
I use a lot of the STM32 family microcontrollers. This page will have some info on how to start using them.
 
I use a lot of the STM32 family microcontrollers. This page will have some info on how to start using them.
 +
 +
=Other Website's Tutorials=
 +
 +
* [https://www.sparkfun.com/tutorial/ARM/ARM_Cross_Development_with_Eclipse.pdf ARM Cross Development with Eclipse], sort of old
 +
* [http://gnuarmeclipse.livius.net/ Welcome to the GNU ARM Eclipse Plug-in], has some tutorials, templates, examples
 +
* [http://embeddedprogrammer.blogspot.ca/2012/09/stm32f4discovery-development-with-gcc.html STM32F4Discovery Development with GCC], from embeddedprogrammer.blogspot.ca
 +
* [http://vedder.se/2012/12/debugging-the-stm32f4-using-openocd-gdb-and-eclipse/ Debugging the STM32F4 using openocd, gdb and Eclipse], from vedder.se
 +
* [http://www.ethernut.de/en/tools/eclipse/confdebug.html Configuring Eclipse for JTAG Debugging]
  
 
=IDE=
 
=IDE=
Line 9: Line 17:
 
* Eclipse GDB Hardware Debugging Plug-in
 
* Eclipse GDB Hardware Debugging Plug-in
 
* GDB Common
 
* GDB Common
* [http://sourceforge.net/projects/gnuarmeclipse/ GNU ARM C/C++ Development Support]
+
* [http://sourceforge.net/projects/gnuarmeclipse/ GNU ARM C/C++ Development Support], also see [http://gnuarmeclipse.livius.net/ Welcome to the GNU ARM Eclipse Plug-in]
  
 
==Using the GNU ARM Plugin==
 
==Using the GNU ARM Plugin==
Line 21: Line 29:
 
Play around with the other settings.
 
Play around with the other settings.
  
=Compiler Toolchain=
+
==Debugging==
  
I use [https://launchpad.net/gcc-arm-embedded GCC ARM Embedded], because "ARM employees are maintaining this project", it is very up-to-date right now.
+
I do not use [http://openocd.sourceforge.net/ OpenOCD], but if you want to use it, go ahead. Most of the online tutorials involving GDB will also involve OpenOCD.
 +
 
 +
I use a J-Link with the J-Link GDB server, with GDB for ARM. I just plug in the J-Link and run the server, and GDB will work with it. J-Link has very wide chip support and most of the configuration is already done for you.
 +
 
 +
===Debugger===
 +
 
 +
====Discovery Boards====
 +
 
 +
ST has a family of Discovery boards, which are dirt cheap ($8 is the cheapest). Each Discovery board has a [http://www.st.com/web/catalog/tools/FM146/CL1984/SC724/SS1677/PF251168 ST Link V2] built into it, which can do SWD debugging. You can disconnect the ST Link and use it for other projects. This is a great backup tool as well.
 +
 
 +
The schematics for the Discovery boards are always available. They are a great starting point for when you need to design your own circuit with STM32 chips. There are also example code from ST written specifically for Discovery boards.
 +
 
 +
====CoLink====
 +
 
 +
[http://www.coocox.org/Colinkex.htm CoLinkEx] is also very cheap but it does not support all of the STM32 family. I support CooCox's open source efforts but I cannot recommend this debugger because some of my favorite STM32 chips are not supported yet. Once the support has been added, then I can recommend it.
 +
 
 +
====J-Link====
 +
 
 +
I own a [http://www.adafruit.com/product/1369 J-Link EDU], which is reasonably priced at around $70. This is what I use. J-Link has a GDB server application that works with the GDB plugins in Eclipse.
 +
 
 +
===Debugging Using Trace Messages===
 +
 
 +
I am talking about using "printf" for debugging.
 +
 
 +
If you want to use the serial port for debug messages, go ahead, there are certain situations when this is suitable. However, a better alternative for ARM is using SWO, which is a single wire dedicated to debug messages, it's faster than UART but it does require a debugger that uses SWD. Think of it as just a faster version of UART, really. My J-Link is great with this, is has a application called JLinkSWOViewer that is meant for this task.
 +
 
 +
This [http://forum.segger.com/index.php?page=Thread&threadID=608 forum post] (the second post) on the Segger website has some example code that uses the ITM inside ARM Cortex CPUs to output things via SWO.
 +
 
 +
=Compiler Toolchain=
  
The GNU ARM plugin for Eclipse does support it.
+
I use [https://launchpad.net/gcc-arm-embedded GCC ARM Embedded], because "ARM employees are maintaining this project", it is very up-to-date right now. The GNU ARM plugin for Eclipse does support it. It is also very similar to [http://www.yagarto.org/ YAGARTO], which is nice. YAGARTO basically got turned into GCC ARM Embedded. You can tell the plugin to use YAGARTO even if you are actually using GCC ARM Embedded.
  
 
=Tools and Documents from ST=
 
=Tools and Documents from ST=
Line 43: Line 79:
  
 
If you want to implement a custom version of the USB bootloader, ST does provide source code for the bootloader firmware (provided with their USB library), and it is compatible with DfuSe.
 
If you want to implement a custom version of the USB bootloader, ST does provide source code for the bootloader firmware (provided with their USB library), and it is compatible with DfuSe.
 +
 +
===Linux? Mac?===
 +
 +
[https://code.google.com/p/stm32flash/ stm32flash], command line utility that is similar to Flash Loader Demo.
 +
 +
[http://sourceforge.net/projects/bootstm32/ bootstm32], command line utility that is similar to Flash Loader Demo.
 +
 +
[https://gitorious.org/dfu-util dfu-util], similar to DfuSe Demo.
  
 
==Clock Configuration Tool==
 
==Clock Configuration Tool==
Line 63: Line 107:
  
 
ST provides a half decent USB library. It's not perfect but it gets the job done. Also known as [http://www.st.com/web/en/catalog/tools/FM147/CL1794/SC961/SS1743/PF257882 STSW-STM32046].
 
ST provides a half decent USB library. It's not perfect but it gets the job done. Also known as [http://www.st.com/web/en/catalog/tools/FM147/CL1794/SC961/SS1743/PF257882 STSW-STM32046].
 
=Debugger=
 
 
==Discovery Boards==
 
 
ST has a family of Discovery boards, which are dirt cheap ($8 is the cheapest). Each Discovery board has a [http://www.st.com/web/catalog/tools/FM146/CL1984/SC724/SS1677/PF251168 ST Link V2] built into it, which can do SWD debugging. You can disconnect the ST Link and use it for other projects. This is a great backup tool as well.
 
 
The schematics for the Discovery boards are always available. They are a great starting point for when you need to design your own circuit with STM32 chips. There are also example code from ST written specifically for Discovery boards.
 
 
==CoLink==
 
 
[http://www.coocox.org/Colinkex.htm CoLinkEx] is also very cheap but it does not support all of the STM32 family. I support CooCox's open source efforts but I cannot recommend this debugger because some of my favorite STM32 chips are not supported yet. Once the support has been added, then I can recommend it.
 
 
==J-Link==
 
 
I own a [http://www.adafruit.com/product/1369 J-Link EDU], which is reasonably priced at around $70. This is what I use. J-Link has a GDB server application that works with the GDB plugins in Eclipse.
 

Latest revision as of 17:32, 9 November 2013

I use a lot of the STM32 family microcontrollers. This page will have some info on how to start using them.

Contents

[edit] Other Website's Tutorials

[edit] IDE

I use Eclipse with the CDT (these help with C and C++ code). Some plugins can be installed from within Eclipse itself, some must be manually installed. Here are the list of plugins I have installed:

[edit] Using the GNU ARM Plugin

Once you start a project, the plugin gives your projects a ton of additional settings. Access them through "Properties for ProjectName" -> "C/C++ Build" -> "Settings". This gives you a GUI for the various settings available for the GNU GCC toolchain, plus the additional settings for ARM. Make sure you select the correct target processor (and whether or not you are using the THUMB instruction set). Make sure you have at least checked off "Create Flash Image".

Under the general settings for the linker, make sure you specify the linker script file (it's a *.ld file, you can find one in the ST libraries and examples). Check off "-nostartfiles".

For optimization, make sure all of the optimization options are enabled in the compiler optimization page. Use "-Os" if possible. Tell the linker general options to "remove unused sections".

Play around with the other settings.

[edit] Debugging

I do not use OpenOCD, but if you want to use it, go ahead. Most of the online tutorials involving GDB will also involve OpenOCD.

I use a J-Link with the J-Link GDB server, with GDB for ARM. I just plug in the J-Link and run the server, and GDB will work with it. J-Link has very wide chip support and most of the configuration is already done for you.

[edit] Debugger

[edit] Discovery Boards

ST has a family of Discovery boards, which are dirt cheap ($8 is the cheapest). Each Discovery board has a ST Link V2 built into it, which can do SWD debugging. You can disconnect the ST Link and use it for other projects. This is a great backup tool as well.

The schematics for the Discovery boards are always available. They are a great starting point for when you need to design your own circuit with STM32 chips. There are also example code from ST written specifically for Discovery boards.

[edit] CoLink

CoLinkEx is also very cheap but it does not support all of the STM32 family. I support CooCox's open source efforts but I cannot recommend this debugger because some of my favorite STM32 chips are not supported yet. Once the support has been added, then I can recommend it.

[edit] J-Link

I own a J-Link EDU, which is reasonably priced at around $70. This is what I use. J-Link has a GDB server application that works with the GDB plugins in Eclipse.

[edit] Debugging Using Trace Messages

I am talking about using "printf" for debugging.

If you want to use the serial port for debug messages, go ahead, there are certain situations when this is suitable. However, a better alternative for ARM is using SWO, which is a single wire dedicated to debug messages, it's faster than UART but it does require a debugger that uses SWD. Think of it as just a faster version of UART, really. My J-Link is great with this, is has a application called JLinkSWOViewer that is meant for this task.

This forum post (the second post) on the Segger website has some example code that uses the ITM inside ARM Cortex CPUs to output things via SWO.

[edit] Compiler Toolchain

I use GCC ARM Embedded, because "ARM employees are maintaining this project", it is very up-to-date right now. The GNU ARM plugin for Eclipse does support it. It is also very similar to YAGARTO, which is nice. YAGARTO basically got turned into GCC ARM Embedded. You can tell the plugin to use YAGARTO even if you are actually using GCC ARM Embedded.

[edit] Tools and Documents from ST

Remember to read the following documents for your specific chip

  • Datasheet, tells you about most of the electrical specifications, pin mappings, package details, etc
  • Reference Manual, tells you how to use the internal peripherals, etc
  • Programming Manual, not required to read but it is useful to understand how the flash memory is organized and written

[edit] Bootloader

Almost all STM32 chips can enter a factory stock bootloader that is stored in ROM (meaning it cannot be overwritten or deleted accidentally), and activated by a pin called BOOT0. To learn how to activate this bootloader mode, look for a document named STM32 microcontroller system memory boot mode. The bootloader uses UART (aka serial port) for communication, and it can also use USB if the chip has USB abilities.

This ROM bootloader usually works with a tool called "Flash Loader Demo", also known as STSW-MCU005. The protocol is also documented so that you can write your own application, but this is not required.

If you want to use the USB version of the bootloader, look for a tool called "DfuSe", also known as STSW-STM32080. The protocol is also documented, and the source code for the DfuSe tool is available as well.

If you want to implement a custom version of the USB bootloader, ST does provide source code for the bootloader firmware (provided with their USB library), and it is compatible with DfuSe.

[edit] Linux? Mac?

stm32flash, command line utility that is similar to Flash Loader Demo.

bootstm32, command line utility that is similar to Flash Loader Demo.

dfu-util, similar to DfuSe Demo.

[edit] Clock Configuration Tool

For different STM32 chips, ST provides an Excel spreadsheet with macros. You have to enter your parameters such as input crystal frequency and desired output frequency. The spreadsheet macro will use your info to generate a file that you can compile into your project. This file will setup the clock sources and PLLs for you. To find these files, you need to search for "Clock configuration tool for" on the ST website, there is a different tool for each chip.

[edit] Standard Peripheral Libraries

ST provides libraries that help you use the features of their microcontrollers. You need to find the specific library for your specific chip by searching for "Standard Peripheral Library" on the ST website.

Inside the library download, you will also find CMSIS support files, a startup file written in assembly, and linker scripts for GCC's linker. You need these files to build any project.

You may also find a clock configuration file inside the examples, but keep in mind that it might be designed for a frequency that is different from the frequency you need.

The library also comes with a ton of examples that compiles under many toolchains. If you get lost, start with an example.

ST supports Atollic TrueSTUDIO, which is an expensive IDE that is based on Eclipse and GNU GCC, so any files that works for TrueSTUDIO will also work using our GNU GCC build process.

[edit] USB Device and Host Library

ST provides a half decent USB library. It's not perfect but it gets the job done. Also known as STSW-STM32046.

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox