Getting Started with the STM32L432 Nucleo-32 using Eclipse and the GNU ARM Toolchain

Created by Austin Oltmanns, last modified on Aug 17, 2017

Introduction

This guide will cover the installation of a development environment on Windows for the STM32L4 board available at digikey.com for about $11 here: https://www.digikey.com/short/318cb0. It will take about 4 hours of time, administrator rights over the computer and 5GB of storage to complete. This board is an ARM Cortex-M4 that runs at 80MHz and has such useful peripherals like CAN, I²C, IrDA, LIN, QSPI, SAI, SPI, SWPMI, UART/USART, USB, 26 GPIO’s, 10x 12b A/D Converters and 2x 12b D/A Converters. This particular board has ST’s implementation of a debugger (the ST-LINK/V2) and can be programmed with a USB A to micro cable (available here: https://www.digikey.com/short/318cw8, just a normal phone charging cable).

There are many different ways to program ST’s dev-boards, and unfortunately, they don’t seem to be very clear in which way is recommended. This guide will cover one way to program their boards from a Windows environment, however, these tools are cross-platform and open source and also support MacOS and GNU/Linux distributions. This guide will be a condensed version of some of the pages available at https:/gnu-mcu-eclipse.github.io. All the necessary information is available at this site, however it can be difficult to navigate if you are new to setting up environments. Also, while this guide is specifically for the STM32L432 Nucleo-32, these instructions also apply to other ST boards.

There are several pieces of software that must be installed to set up the environment. Firstly, the toolchain will be installed. This includes the ARM compiler and linker and a few other tools. To accompany the toolchain, the windows build tools will be installed. Next will be the debugger, this is used to program the board and as the name suggests, debug programs. In addition to the debugger, software from SEGGER to convert the ST-LINK/V2-1 to a J-Link compatible device will be downloaded (this is so SEGGER’s gdb implementation can be used rather than an open source tool, which I found to be more difficult to configure). After that, drivers for the ST-Link/V2 will be installed. Next, the special version of eclipse that will be used for these boards will be installed along with its required software (namely: Java). After that, HAL and driver code from ST will be downloaded, along with their tool for configuring peripherals on the board. Finally, plugins for the GNU MCU Eclipse will be installed and a test program will be created, compiled and uploaded to the board to show that everything is working.

Each step in this guide will link back to the longer page from gnu-mcu-eclipse’s github page or the software provider’s page for reference, but this guide aims to be complete and standalone. So, without further introduction, these are the steps to prepare a development environment for the STM32L432 Nucleo-32 on Windows using GCC and Eclipse.

Please refrain from plugging in the dev-board until after the drivers have been installed or you will be forced to fiddle with Window’s dirty drivers.

Installing the toolchain

Here is the link for instructions from GNU-MCU-Eclipse: https://gnu-mcu-eclipse.github.io/toolchain/arm/install/

The short of it is, go to this link: https://developer.arm.com/open-source/gnu-toolchain/gnu-rm/downloads and download the most recent installer.

image

Then, run it, allowing it to make changes. It will ask some basic installer questions, like language, if you accept the GNU General Public License and default install location (very recommended to leave this as the default). At time of writing, this will take about 433MB of space. After selecting Install location, it will take a few minutes to install and it will show the “Install wizard Complete” screen. GNU-MCU-Eclipse recommends NOT adding the program path to the environment variable and also NOT launching gccvar.bat. It is probably wise to follow their advice. You may be asked if you wish to add registry information. Do this, because it will make uninstalling these tools easier if you ever need to.

image

Installing the build tools

Detailed page: https://gnu-mcu-eclipse.github.io/windows-build-tools/install/

Abridged version:

Go to this page and download the most recent installer for your machine (32bit or 64bit): https://github.com/gnu-mcu-eclipse/windows-build-tools/releases

(note: I had to right-click the link and hit “save target as…” for some reason, instead of just clicking the link to start the download.)

This installer will also ask you some questions typical of an installer like language, an MIT license, shortcuts, install location (recommended to leave as default!) and that’s it.

Installing the debugging software - choose one

SEGGER provides professional debugging and device programming solutions. Granted, these normally come with professional prices (https://www.digikey.com/short/31hn8v) which can be inhibitive to certain groups of developers. Thankfully, they have provided a tool (which is completely reversible) to convert the on-board ST-Link/V2-1 device to a SEGGER J-Link compatible device which can be used with their software. This is the recommended debugging option because this same setup can be used should you decide to migrate your project to a professional level. In this step, the debugging software will be installed and then after installing the drivers for the ST-Link/V2, the conversion tool will be installed.

Note: making this guide was a long process of trying different things, and I eventually settled on J-Link’s debugger over OpenOCD. This is mainly because I could not find a proper configuration to use it with this board. However, after making this guide, I found this page https://github.com/GrumpyOldPizza/arduino-STM32L4/blob/master/variants/STM32L432KC-NUCLEO/openocd_scripts/stm32l432kc_nucleo.cfg which appears to have a working configuration (untested by me). I will leave using OpenOCD and the ST-LINK/V2-1 debugger firmware as an exercise to the reader.

[SEGGER J-Link] - recommended

GNU-MCU-Eclipse link: https://gnu-mcu-eclipse.github.io/debug/jlink/install/

Download page link: https://www.segger.com/downloads/jlink (Go to J-Link Software and Documentation Pack)

Download and run the installer, be sure to install to the default location for easy setup later.

[OpenOCD] – open source option

GNU-MCU-Eclipse link: https://gnu-mcu-eclipse.github.io/openocd/install/

Download page link: https://github.com/gnu-mcu-eclipse/openocd/releases

(find the windows installer, the setup.exe version)

Once again, you will find yourself answering installer questions. You should be pretty familiar with them by now. Pick a language, accept another GNU General Public License, leave the default options and install location and you should be set.

Installing the drivers

These instructions come from the same page as the OpenOCD instructions, but are required for either debugging setup. GNU-MCU-Eclipse link: https://gnu-mcu-eclipse.github.io/openocd/install/

Download the ST-Link/V2 USB drivers from ST themselves: http://www.st.com/en/embedded-software/stsw-link009.html

(it’s the little “Get Software” button at the bottom of the page)

This time, you get to agree to the license before downloading the software, then you will be asked for your name and email. Then you will be sent an email with the download link, so hopefully you entered a valid email. You’ll get a .zip folder which you should extract all the files to another directory from and then run their .bat file as per the instructions in the readme. This will start the driver installer, which should ask for admin rights. Allow them and then finish the installer.

It is now safe to plug in the dev-board to the computer.

Installing the ST-Link to J-Link Converter

Instructions and download link: https://www.segger.com/products/debug-probes/j-link/models/other-j-links/st-link-on-board/

This part is fairly straight forward. Download the tool, and use the tool. Be sure to read and agree to the license terms. (Note: I was able to double click the .exe to run it, but sometimes with command line utilities it is required to open a command prompt and run it from there.) The light on your board near the USB cable should change from red to green after the conversion (this is a handy indicator of which firmware you have installed at a glance).

Installing the Java Development Kit

These instructions stem from this page: https://gnu-mcu-eclipse.github.io/plugins/install/

This particular version of Eclipse requires the Java Development Kit. At the time of writing, just the runtime environment was not enough and the full JDK was required. To install this, go to http://www.oracle.com/technetwork/java/javase/downloads/index.html and click the download button next to JDK. Then you will be directed to another download page. Accept the license agreement by selecting the radio button and then download the appropriate JDK for your system. Accept the defaults and move on.

Installing Eclipse

These instructions stem from the same page as the previous section.

Take yourself to this page: https://github.com/gnu-mcu-eclipse/org.eclipse.epp.packages/releases/

Download the most recent version of your style of Windows (hopefully 64bit). The nice thing about eclipse is that you don’t actually install it in the traditional sense. You will be downloading a folder which contains the program and you can place it anywhere you like. Inside your user folder is probably a good place for it (C:\Users\you). And you may as well make a link to the executable on your desktop while you are in the installing spirit (right click the eclipse executable, Send to>Desktop (create shortcut)).

Installing the Eclipse Plugins

Still using the instructions from the last two sections.

Thankfully, GNU-MCU-Eclipse has made this process relatively painless so far. The next step is to open up the new Eclipse “installation” and going along the top menu to Help>Eclipse Marketplace. Then type “GNU MCU Eclipse” into the “Find:” field and press enter. After a little bit, the GNU MCU Eclipse solution should appear as the top result. Click the install button and then you will be presented with a selection of features to choose from. Install all of them for maximum potential, or at least make sure that the STM templates and the OpenOCD debug plugin are installed. If you receive an error that the plugin cannot be installed, click ok and just hang tight, eclipse is working in the background. After a few minutes, another dialog will appear asking you to continue your download. Run through that, and once it completes you can check to see if the plugins actually installed by going to Help>Install New Software…. Click Add… in the upper right hand corner and add GNU MCU Eclipse Plug-ins for the name and http://gnu-mcu-eclipse.netlify.com/v4-neon-updates/ for the URL. Then click the drop down on “Work with” and choose the item you just entered. You should see “GNU ARM & RISC-V C/C++ Cross Development Tools”. Click its drop down arrow, and hopefully you will see “All items are installed”.

Installing ST’s Drivers and HAL Code

Download page: http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/patch-cubefw-l4.html

Be sure to get the latest one, at the time of writing, this is 1.8.1. For some reason it’s labeled as a “patch” but includes all the data from 1.8.0. This will be a .zip that contains the drivers and HAL code for the stm32l4 series. It also includes example projects which have important files for project configuration. Put the extracted folder from inside the zip in a reasonable place, as you will be taking copies of its files whenever a new project is made.

It also comes with complete API reference help files, which are vital to development.

Installing ST’s CubeMX Peripheral Configuration Tool

Download page: http://www.st.com/en/development-tools/stm32cubemx.html

This is a tool for ST which makes configuring all the peripherals on the board much easier. It contains a GUI in which you may select peripherals and settings (like timers or communication devices) and from these selections it will generate initialization code. It also lets you know some of the files needed from the Drivers and HAL code package to include in your project.

This tool will also require an account and a valid email with ST.

Blinking the Lights

It is now finally time to put some code on the hardware. If everything has gone well up to this point, you are almost done with this guide.

Make the project in STM32CubeMX

Open up STM32CubeMX and hit “New Project”. At the top, go over to “Board Selector” and set “Type of Board” to Nucleo-32. You should now see your board in the board list. It’s the NUCLEO-L432KC, double click it. Now you will be in the project configuration page on the pinout tab. Scroll down to TIM1 and expand it. Set the clock source to “Internal Clock” and “Channel1” to “Output Compare No Output”.

Next, go to the “Configuration” tab and click on TIM1 in the control block. Somewhat arbitrarily, set “Prescaler” to “10”, “Counter Period” to “200” and “Internal Clock Division” to “Division by 2”. Then, in that same pop-up window, go to the NVIC Settings and set TIM1 capture compare interrupt to “enable”. Press “apply” and “ok” to close the pop-up.

Now, go to Project>Settings… from the top ribbon, or press Alt+P if you are running short on time. Give your project a name, like Blink1. Change the Toolchain/IDE setting to “Other Toolchain (GPDSC)”. Then go to the “Code Generator Settings” tab and change the “Firmware Library Package” setting to “Copy only the necessary library files” or else you will get some example and template files in your project that will cause it not to build. Press “ok” on this window and then press the image of the gear and pin (next to the scroll) to generate the project files. Click the folder to open project folder, but don’t get too excited just yet, there’s still a few steps left.

Make the project in Eclipse

In Eclipse, do a File>New>C++ Project. Enter a decent name, like LEDTest, Blinky or whatever you like. Choose “Hello World ARM Cortex-M C/C++ Project” as the “Project type” and “ARM Cross GCC” as the toolchain. Click next.

The Target Processor settings should be as follows:

And the folder settings:

The configurations can be left alone.

The toolchain settings should be fine, but make sure that the Toolchain Path goes to the proper bin folder (the one with the long name programs like “arm-none-eabi-gcc”). Click finish to generate the eclipse project.

Putting it all together

This part can get a little hairy, but it is one of the last steps. There are now two projects with conflicting items and the two of them combined aren’t quite enough to get a demo from. This is really because no one has gone out and made a template for this dev-board yet unlike some of the other Nucleo boards out there. But, a little copy and pasting will yield one fully functional project setup. It is recommended to have two file explorer windows open side by side for the following steps. One for your CubeMX project folder and the other for your eclipse project folder.

First set of files to copy is most of the CubeMX “Src” files. These should be “main.c”, “stm32l4xx_hal_msp.c” and “stm32l4xx_it.c” but NOT “system_stm32l4xx.c” . Paste them into the “src” folder in the root of the eclipse project. You can also go ahead and delete the original files that were in the eclipse “src” folder (Timer.cpp, main.cpp, and _write.c).

Next, go to the CubeMX project “Inc” directory. Same routine here, copy the cube files (main.h, stm32l4xx_hal_conf.h and stm32l4xx_it.h) and delete the eclipse files (Timer.h).

After that is the CubeMX “Drivers” folder. At this point, the CubeMX directory structure and the Eclipse directory structure differ slightly, so be careful. Go to CubeMXProjectName>Drivers>CMISS>Device>ST>STM32L4xx and go to the “Include” directory. In your other window, go to EclipseProjectName>system>include>cmsis. Copy these files from the CubeMX project into the Eclipse project: stm32l4xx.h, stm32l432xx.h and system_stm32l4xx.h. Be sure to overwrite the existing files that were in there and unlike the previous steps, leave the files that were already in the eclipse project alone.

Now go to the CubeMX CubeMXProjectName>Drivers>CMISS>Device>ST>STM32L4xx>Source>Templates folder and copy the system_stm32l4xx.c file (same name as the file that was not copied in the first step). Paste it into EclipseProjectName>system>src>cmsis.

Following that, go into CubeProjName>Drivers>STM32L4xx_HAL_Driver>Inc in one window and EclipseProjName>system>include>stm32l4xx in the other. Copy everything from the Cube project into the eclipse project, even the “Legacy” folder. Next, go into CubeProjName>Drivers>STM32L4xx_HAL_Driver>Src and EclipseProjName>system>src>stm32l4xx and once again, copy the Cube files into the Eclipse folder.

That covers all of the necessary files from the CubeMX project, but there are still a few steps.

You should now open up the folder where you have downloaded ST’s drivers and HAL code. The first file needed is a linker script (.ld) from one of the example projects. Search *.ld in the folder and a few should come up, as of writing they are all equal. Pick your favorite and copy it to the ldscripts folder of the eclipse project.

Next is a .s file which contains the functions which are called on a system reset and a few other things. You will want the one from STM32Cube_FW_L4_VXXXX>Drivers>CMSISST>STM32L4xxTemplates>gcc. Specifically startup_stm32l432xxx.s. Copy it to your Eclipse project at EclipseProjName>system>src>cmsis and change the .s extension to a .S (note the capital letter).

Now there are still two files which must be excluded from the builds in order to use this .S file. In the EclipseProjName>system>src>cortexm>exception_handlers.c and EclipseProjName>system>newlib>_startup.c should be renamed to have a .EXCLUDE extension (or deleted, anything to keep the compiler from finding them).

You are now on the home stretch. Open up eclipse and refresh the project to make sure all of the files and changes are active. Along the top of the window, go to Project>Properties. Go to C/C++ Build>Settings>Tool Settings>GNU ARM Cross C++ Linker>General and remove “mem.ld”, “libs.ld” and “sections.ld”. Now add the STM32L432KCUx_FLASH.ld script from the workspace ldscripts folder. Also be sure to uncheck “Do not use standard start files (-nostartfiles)” or you will get a very cryptic error later. Click apply and OK.

Next, add the following lines of code to the application in the while(1) loop in main.c

HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
for(unsigned int num1=0; num1 <40;num1++) for (unsigned int num=0; num <60000;num++);

This will yield a blinky light (at about 13Hz, should be visibly strobing).

Next, from eclipse, go to the system>include>cmsis>stm32l4xx.h file and uncomment

#define STM32L432

After adding those two lines and editing that header file you may press the build hammer to generate a flurry of warnings and an intel .hex file. If you get an error, be sure to check over the instructions to make sure every file has been copied and all the right buttons have been clicked.

It’s now time to upload and debug. Click the drop down next to the bug icon and click “Debug Configurations…”. Double click “GDB SEGGER J-Link Debugging” to create a new debugging configuration. Add the word Jlink to the beginning of the name so it is easy to differentiate it from the default configuration (which will not work for our purposes). Make sure the settings match these images:

Then click Apply and then Debug.

If the correct debugging configuration was launched (sometimes eclipse likes to grab the default one, even if it said it was going to launch the Jlink one) you should get a license agreement to appear. Click accept to upload the program. If it does not appear, accept whatever error has occurred and then try clicking the drop down arrow and selecting the Jlink debugging configuration that was just made. Also, try closing any other programs you may have open if it keeps giving you a timeout error.

If all has gone well, you can press the little green and yellow play/pause button at the top bar to start the program. You may now start and stop the program at will, set breakpoints, step through the code line by line and otherwise use the debugger to its full potential.

Blinky, Round II

For a second exercise/example, add the following code to user code area 0:

void HAL_TIM_OC_DelayElapsedCallback ( TIM_HandleTypeDef * htim )
{
    UNUSED(htim);
    HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
}

And this line to user code area 2:

HAL_TIM_OC_Start_IT(&htim1, TIM_CHANNEL_1);

And comment out the code in the while(1) loop that was added (but not the loop itself). This code will toggle the light very precisely based on the TIM1 output compare. This will require an O-scope to view properly (or a buzzer/speaker, it’s about 7.15 KHz). Also, note that stepping through this code with the debugger might be problematic because the debugger and the timer peripherals can have trouble working together.

Conclusion

Here is the project file achieved during this exercise. Blink66.zip (7.9 MB)

At this point, you should consider reading some datasheets and manuals. Specifically, these:

Nucleo Users manual

STM32L432KC data sheet

Also browse through the documentation that came with the ST’s drivers and HAL code to see some of the different things that can be done with the peripherals.

You should now be set to start developing with the STM32L432 Nucleo-32 Development board!