Getting started with Nucleo32 – STM32L031

I have this board in my stock since some months and never taken time to play with it. As I’m starting some developments with STM32 it’s time to do it. I’ve tried to do some stuff using it at reception but even if there is some kind of Arduino mentions on the package the start is not as easy as on Arduino. Not regarding coding (it is more complicated but it is not the problem) but regarding the development environment. You need to use different tools and find yourself the way to use each of them. In my point of view ST would do a good thing simplify all this stuff. So in this post I’ll describe how to get started by creating your first led blink project. Hope it will save your time.

What is this STM32L031 Arduino looking like kit ?

This kit is a STM32 ARM Cortex-M0 board with a form factor of Arduino Nano. It is also compatible at pin level (the little detail is Arduino is 5V based pin when this is 3.3V based pin… so this is great difference). To be honest : this is the only link with an Arduino.

The board have two sides : the bottom contains the powering component and the ST-Link programmer to upload and debug code. The top side hold the MCU. ST is proposing different board with different MCU.

The tested MCU is STM32L031. This is a Low-power Cortex-M0 Arm MCU with 32Kb of Flash and 8Kb of RAM running up to 32MHz. Small, low cost and quite powerful.

The official page for this board with all the related documentation is here.

Development environment

The STM32 family is supported by a large number of development environment. And I usual I have a preference for the Free of them 😉 What is interesting is as the Flash memory is 32Kb we can also use non-free environments like Keil. Because Keil free version is limited to 32Kb of code and in this case it is not a limitation.

So I’ve did some try with Keil, AC6 (SW4STM32) – based on eclipse, Mbed online. To make it short I’ve created an account on Mbed even if I do not like online platform for coding because you don’t master what this platform will become and I don’t link it when I want to maintain code for years. More over I did not liked the first step in it : finding my board to initiate a project was too long in my point of view.

So I start my first test with Keil, I know that you quickly get started with the package manager where you can easily download what you need. I had a good experience with nrf51 and Keil thanks to this, before moving to eclipse due to the 32Kb limitation. Keil works well but is raising errors related to the device selection and basically when loading a project generated by CubeMx or from the SDK you need to reconfigure the target device and the programming interface. It’s not a big deal but it is boring. I don’t like also the project organization (like the header file hide from the project tree … who has a such idea ??)

So I’ve register to web site and download AC6 environment.

Global prerequisite

STM MCU configuration can be done within your code but the easiest way is to generate this code. The STM32CubeMX allows to make a such code generation with a graphical tool. So you need this tool to initiate your project. The tool is able to update a project once completed with the custom code if you correctly inserted your code in the dedicated places (be careful the software quickly destroy your code if not in the right place). The tool generate a project for the target you want (Keil, IAR, AC6…).

The first step is to create a new project and select a device or a board. Let’s start by a device ; but the board can directly be selected.

You can select it by familly and name, but you can also search for the best device regarding the hardware function you need to use. This is an interesting functionality. As a consequence, once you select your device you can quickly see the available capabilities. For the STM32L031 board the device is a STM32L031K6T6 MCU.

Once selected you can watch your device and all the Pin. Next you can select the component you want to use in your code and match them to the component pins.

For our sample test, we want to have the led of the board blinking and the serial line reporting a “Hello World” message on boot. So we need to activate a serial line and a GPIO. We need to know how is cabled the board. The following schema will help us:

So we can see that LED1 (basically LD3 in the real world) is connected to PB_3. By reading the DevKit documentation we can see that Serial Line is connected to ST-LINK debugger on lines PA_2 (TX) and PB_15(RX).

In the device configuration we will activate the UART2 as asynchronous and change the default pin to the right one by Right-Clicking on target pin and select it as the desired function.

Then we will Right-Click on PB3 to select it as a GPIO_OUTPUT.

Now we can switch to the Configuration tab to set detail elements on each of the selected components (certain chip provider have tool offering all the configuration in the same screen, and I think it is better, but it’s a question of habits).

So at this point we can click on the USART2 device and access to the configuration detail. We will change the WordLength to 8bits (who uses 7bits in 2018 as a default ??).

By clicking on GPIO device you can change the GPIO configuration. Basically the default configuration will work for our demo.

Now we can generate the project by clicking on this strange icon. A windows will popup and ask you for project directory/name and you can select the IDE targeted. You can directly open the project in the corresponding IDE by clicking on “open-project” button. This is useful. SW4STM32 is corresponding to an Eclipse environment.

Code customization

Whatever the IDE used is we will add some line of code for our example. You will make the change inside the IDE but for this post it is more easy to indicate it here.

We will modify the main.c file like this :

 char * msg = "Hello World!\r\n";
 HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg),0xFFFF);
 /* USER CODE END 2 */

 /* Infinite loop */
 while (1)

 /* USER CODE END 3 */

The added code is in Red, I do not think I need to comment.

Keil environment (MDK-ARM)

With Keil you have some prerequisites:

  • Install the ST-Link (Link004) software to be able to upload your code
  • Install the ST-Link firmware upgrade (Link007) to update the on-boarded st-link sw

Then you can install the Keil software and select the needed package : search for StMicroelectronics / STM32L031 and install the compiler and the associated DFP (SDK), the CMSIS.

Once you will have open the project created with CubeMX you will have an error message telling you the corresponding device does not exists even if the package has been installed. So you will have to close all the related windows opened and then you will have to edit the project setting and re-select the target device STM32L031K6Tx.

Then you need to configure the ST-Link:

Once you select ST-Link debugger in the list you need to click on setting to change the st-link default configuration to run the downloaded binary automatically. If you do not do this nothing will happen after firmware upload.

AC6 environment

AC6 environment is based on Java/Eclipse and uses GCC compiler to generate your binary. This environment is free but requires to register the website (I’m sure they could invest in a free Let’s Encrypt certificate …). The download page is difficult to find and the website looks really obsolete but the package is maintained. The download page is here (once registered). Once installed you can update the software with going to Help menu then Check For Update.

The package comes with all the ST-LINK prerequisite so the installation is easy but requests a reboot at end of the process. The application can be found in SystemWorkbench for STM32 once installed.

The compilation is done by clicking on the usual hammer icon. Then you can select the project name and click on Run or Debug Icon and the program will start to be compiled/downloaded and start on the board. The binary is automatically downloaded on the board.

I’m using AC6 under Windows environment and MacOs X environment with success. The platform is as modern as Eclipse based platform can be but it is doing the job correctly. CubeMx application allows to generate AC6 project natively when selecting SW4STM32 project target IDE. AC6 is embedding OpenOCD implementation to program & debug the chip over StLink.

Console debug

To see the Hello World in the console you just need to connect a Putty serial terminal to Serial line configured by Windows on device connection. See the Device Manager to see what port has been used. The Serial line is 115200bps / 8b  … standard.

One thought on “Getting started with Nucleo32 – STM32L031

  1. Pingback: Send Sigfox messages with STM32 + S2LP - - technology – technology blog

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.