Skip to content

A Guide to Setup Development Environment for STM32F0 Microcontrollers

Notifications You must be signed in to change notification settings

hefanbo/STM32-IDE

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 

Repository files navigation

A Guide to Setup Development Environment for STM32F0 Microcontrollers

Abstract

This article describes how to setup a development environment for STM32F0 series microcontrollers (ARM Cortex M0) manufactured by STMicroelectronics. Although there are some good commercial solutions such as IAR Embedded Workbench, MDK-ARM and TrueSTUDIO, this article tries to use free and open source software as much as possible to provide a low-cost high-quality solution.

Keywords: ARM, Cortex M0, STM32, STM32F0, IDE, Eclipse

1. Overview

1.1. System and hardware environment

The system and hardware environment used in this article is described as follows. They are not system requirements. For detailed system requirements, please refer to documents of software packages.

Windows 7 (64-bit)

The host operating system must be Windows because STM32CubeMX runs only on Windows. Nevertheless, if you don’t plan to use STM32CubeMX, other operating systems are feasible.

Java Runtime Environment 8

Java Runtime Environment (JRE, version≥7) is required by STM32CubeMX and Eclipse. Download links can be found at https://www.java.com/en/download/manual.jsp.

Hardware of host PC

CPU: 3.4 GHz / 4 Cores. RAM: 8 GB. Disk space used by all software packages is about 1 GB.

Development board

The development board in this article is NUCLEO-F091RC which features the microcontroller STM32F091RCT6 and includes an on-board ST-LINK/V2 programmer.

1.2. List of software packages to be installed

STM32 ST-LINK utility

This software includes drivers and utilities for the ST-LINK programmer.

STM32CubeMX

This handy tool lets users configure microcontrollers by GUI, and generates appropriate initialization code.

STM32CubeF0

This package provides useful code, libraries, and examples for users to write applications for STM32F0.

CMSIS

The ARM Cortex Microcontroller Software Interface Standard (CMSIS) contains useful code and libraries for Cortex-M. Although STM32CubeF0 contains a copy of CMSIS, it lacks some libraries. Nevertheless, if you are okay to compile libraries from source yourself, you can skip downloading CMSIS.

GNU Tools for ARM Embedded Processor

This is an open source toolchain to build programs for ARM processors.

GNU ARM Eclipse build tools

This open source package includes some useful programs such as make and rm which are needed during building.

OpenOCD

This open source software links gdb(the debug program) with microcontrollers. Although there is another open source project stlink which provides similar functions, this article prefers OpenOCD because it is more general and is easier to use with GNU ARM Eclipse Plug-in.

Eclipse

Eclipse is an open source platform which is the base of many embedded software development IDEs such as TrueSTUDIO, CooCox CoIDE, and Code Composer Studio.

CDT Eclipse Plug-in

This open source Eclipse plugin provides a C/C++ development environment.

GNU ARM Eclipse Plug-in

This open source Eclipse plugin provides a C/C++ development environment for ARM processors.

2. Software installation

2.1. Drivers and utilities provided by STMicroelectronics

  1. Download STM32 ST-LINK utility from http://www.st.com/web/en/catalog/tools/PF258168. Unpack and run the installer. Since this package includes drivers for ST-LINK programmers, there is no need to download and install drivers separately. After installation, connect the board to your PC via a USB cable. The LED LD1 is expected to be red (not blinking) now. Launch the STM32 ST-LINK Utility via the desktop shortcut, and select Target  Connect from the menu. After a few seconds, the software should show the memory contents of the chip, and LD1 is expected to blink fast in red and green. Finally, selecting Target  Disconnect makes LD1 green (not blinking). In case something goes wrong, more information and downloads can be found at http://www.st.com/web/en/catalog/tools/PF251168.

  2. Download STM32CubeMX from http://www.st.com/web/en/catalog/tools/PF259242. Unpack and run the installer.

  3. Downdoad STM32CubeF0 and its patch from http://www.st.com/web/en/catalog/tools/PF260612 and http://www.st.com/web/en/catalog/tools/PF261699, respectively. To install these packages, launch STM32CubeMX, and select Help  Updater Settings to set Repository Folders where the files will be extracted to. Then select Help  Install New Libraries, click From Loc…​ button, and browse to the zip files downloaded.

2.2. Build-toolchain and utilities for ARM

  1. Download GNU Tools for ARM Embedded Processor from https://launchpad.net/gcc-arm-embedded/+download. Unpack and run the installer.

  2. Download GNU ARM Eclipse build tools from http://sourceforge.net/projects/gnuarmeclipse/files/Build%20Tools/. Unpack and run the installer.

  3. Download OpenOCD 0.9.0-dev from http://www.freddiechopin.info/download/category/10-openocd-dev/. Extract the downloaded zip file. The OpenOCD project page is http://sourceforge.net/projects/openocd/. However, the latest release 0.8.0 does not work with STM32F091RCT6, and therefore users have to compile the latest source code available at http://sourceforge.net/p/openocd/code/ci/master/tree/. Fortunately, this nice guy Freddie Chopin has done the compilation and provides downloads as above.

  4. Download CMSIS from http://www.arm.com/products/processors/cortex-m/cortex-microcontroller-software-interface-standard.php.

2.3. Eclipse and plugins

  1. Download Eclipse IDE for C/C++ Developers from http://www.eclipse.org/downloads/. Extract the zip file. If your PC has internet connection, you can follow ``the recommended way'' on http://gnuarmeclipse.livius.net/blog/plugins-install/#The_recommended_way. If you prefer off-line install, please follow the next 2 steps.

  2. Download CDT from https://www.eclipse.org/cdt/downloads.php. Launch Eclipse, select Help  Install New Software…​, click Add…​ then Archive…​, and browse to the downloaded zip file. Check the C/C++ GDB Hardware Debugging component and install. Remember to uncheck ``Contact all update sites during install to find required software'' if there is no internet connection.

  3. Download GNU ARM Eclipse Plug-in from http://sourceforge.net/projects/gnuarmeclipse/. Similar as above, install GNU ARM C/C++ Cross Compiler and GNU ARM C/C++ OpenOCD Debugging components from the downloaded package.

2.4. A quick note for Linux

Although this article is mainly based on Windows, here are the steps to setup a basic development environment (without STM32CubeMX) for STM32 on Linux.

  1. Install GNU ARM toolchain

  2. Install Eclipse with CDT and GNU ARM Eclipse plugins

  3. Compile and Install OpenOCD

3. Example projects

This example shows steps to generate initialization code with STM32CubeMX, create an Eclipse project, and compile/load/debug a program.

3.1. Generate initialization code

  1. [Generate code] Launch STM32CubeMX, click New Project and select Board Selector tab, choose NUCLEO-F091RC and click OK. Select Project  Settings…​ from the menu. Fill Project Location (refered as workspace hereafter) and Project Name, and select TrueSTUDIO for IDE (if you are using STM32CubeMX version 4.7+, you can also select SW4STM32). Now switch to Code Generator tab and select Copy only the necessary library files, and then click OK. Then select Project  Generate Code from the menu and a folder (refered as working folder hereafter) will be generated with initialization code inside.

  2. [Delete unnecessary project configuration] In the working folder, TrueSTUDIO can be deleted.

  3. [Delete unnecessary header files] Delete the following files (21 in total) in the working folder

    1. In Drivers\CMSIS\Device\ST\STM32F0xx\Include, delete everything except stm32f0xx.h, stm32f091xc.h, and system_stm32f0xx.h.

    2. In Drivers\CMSIS\Include, delete core_cm0plus.h, core_cm3.h, core_cm4.h, core_cm7.h, core_sc000.h, and core_sc300.h.

  4. [Copy BSP files] The package STM32CubeF0 provides some BSP(board support package) files which are not copied to the working folder by STM32CubeMX. Copy them manually to make coding easier — create a folder named BSP in <working foler>\Drivers, and copy Drivers\BSP\STM32F0xx-Nucleo in the STM32CubeF0 repository (specified when installing STM32CubeF0) into the new BSP folder.

  5. [Copy linker script] STM32CubeMX doesn’t generate the linker script correctly for TrueSTUDIO, so we have to do it manually. In the STM32CubeF0 repository, locate Projects\STM32F091RC-Nucleo\Templates\TrueSTUDIO\STM32F091RC-Nucleo, and copy STM32F091RC_FLASH.ld into Src in the working folder. If you selected SW4STM32, the correct file can be found in <working folder>\SW4STM32\<Project Name> Configuration, and after copying this file, the folder SW4STM32 can be deleted.

  6. [Fix assembly file extension] Browse to Drivers\CMSIS\Device\ST\STM32F0xx\Source\Templates\gcc in the working folder, and rename startup_stm32f091xc.s to startup_stm32f091xc.S (Upper-case `S'). Otherwise the linker will give a warning ``cannot find entry symbol Reset_Handler''.

3.2. Create Eclipse project

  1. [Set workspace] Launch Eclipse, select the forementioned workspace folder when asked for Workspace. In case another workspace is opened by default, select File  Switch Workspace  Other…​ to switch workspace.

  2. [Create project] Select File  New  C Project from the menu, choose Empty Project and Cross ARM GCC. Uncheck Use default location, and fill Location with <working folder>\EclipseProject (replace <working folder> with the actual path). Fill Project name with the same Project Name as in STM32CubeMX. Click Next button, uncheck release, and click Next again. In the toolchain path, browse to the bin folder of the installed GNU Tools for ARM Embedded Processor, and click Finish.

  3. [Add source folders] Select File  New  Folder from the menu, click Advanced > >, select Link to alternat location (Linked Folder), enter the text ..\Src and click Finish. Add ..\Inc and ..\Drivers in the same way.

  4. [Add path for tools] Select Project  Properties from the menu, select C/C++ Build→Environment from the left tree, and double click PATH in the right pane. Append the full path of the bin folder of GNU ARM Eclipse build tools after the original value. Don’t forget to add a semicolon as a separation.

  5. [Set build options] Select Project  Properties from the menu. Select C/C++ Build→Settings from the left tree.

    1. Select Target Processor on the right pane. Choose cortex-m0 for ARM family.

    2. Select Cross ARM C Compiler  Preprocessor. Click Add icon on the Defined symbols (-D) pane, and enter STM32F091xC. Do it again to add USE_HAL_DRIVER.

    3. Select Cross ARM C Compiler  Include. Click Add icon on the Include paths (-I) pane, click Workspace…​, and browse to the Inc folder in the project. Do the same thing for Drivers\STM32F0xx_HAL_Driver\Inc, Drivers\CMSIS\Include, Drivers\CMSIS\Device\ST\STM32F0xx\Include, and Drivers\BSP\STM32F0xx-Nucleo.

    4. Select Cross ARM C Linker  General. Click Add icon on the Script files (-T) pane, click Workspace…​, and browse to STM32F091RC_FLASH.ld in the Src folder.

    5. If CMSIS DSP Library is to be used in the project, the math library should be added. However, the CMSIS shipped with STM32CubeF0 does not contain libraries to use with GNU Tools for ARM Embedded Processor. To fix this:

      1. Extract CMSIS\Lib\GCC\libarm_cortexM0l_math.a from the downloaded CMSIS zip file to <working folder>\Drivers\CMSIS\Lib\GCC (create corresponding folders if necessary).

      2. Select Cross ARM C Linker  Libraries. Click Add icon on the Libraries (-l) pane, and enter the text arm_cortexM0l_math. Click Add icon on the Library search path (-L) pane, click Workspace…​, and browse to Drivers\CMSIS\Lib\GCC in the project.

      3. Select Cross ARM C Compiler  Preprocessor and add ARM_MATH_CM0 in Defined symbols.

      4. Another option is to compile the math library from source. The source code is at Drivers\CMSIS\DSP_Lib\Source in STM32CubeF0 repository. Don’t compile arm_bitreversal2.S because there will be errors.

    6. Select Cross ARM GNU Create Flash Image  General. Specify Raw binary for Output file format (-O). Although the default Intel HEX also works with STM32 ST-LINK Utility, the mbed interface only supports binary files.

  6. [Add custom code] Open main.c in Src. The function of the program is simple — light the green LED LD2 when the blue button B1 is pressed.

    1. Add #include "stm32f0xx_nucleo.h" between /* USER CODE BEGIN Includes / and / USER CODE END Includes */.

    2. Add the following code inside the while (1) loop

        if (BSP_PB_GetState(BUTTON_USER)) {
          BSP_LED_Off(LED_GREEN);
        } else {
          BSP_LED_On(LED_GREEN);
        }
Note
If there are unexpected error marks when displaying source code in Eclipse, try Project  C/C++ Index  Rebuild from the menu.

3.3. Compile and run

  1. [Compile] Select Project  Build Project from the menu of Eclipse, and a bin file will be generated in the EclipseProject\Debug folder.

  2. [Flash] There are two ways to flash bin files to the board. After flashing, the program should run as expected.

    1. Launch STM32 ST-LINK utility, select Target  Program & Verify…​, browse to the bin file, and click Start.

    2. When the board is connected to the host PC, a USB drive will appear. Just copy the bin file to the drive, and the on-board mbed interface will flash it automatically.

  3. [Debug] Follow these steps to debug with Eclipse

    1. [Set OpenOCD path] Select Window  Preferences from the menu, and then select Run/Debug  String Substitution from the left tree. Set the absolute path (e.g., <somewhere>\OpenOCD\bin-x64) and executable file name (e.g., openocd-x64-0.9.0-dev-150204220259) in the right pane.

    2. [Create debug configuration] Select Run  Debug Configurations…​ from the menu, and double click GDB OpenOCD Debugging in the left tree. In the right pane, select Debugger tab, and fill -f board/st_nucleo_f0.cfg in Config options.

    3. [Launch debug] Connect the board with a USB cable, and click Debug button. Switch to ``Debug'' perspective if asked. Now you can debug the program by setting breakpoints, executing program step by step, watching variables, etc.


About

A Guide to Setup Development Environment for STM32F0 Microcontrollers

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published