In my last post I’ve described long way of configuring non-proprietary toolchain for ARM programming. I was pretty satisfied with it except two of its elements: Eclipse (I’m not a fan of large IDEs) and proprietary STM32 L1 standard peripheral library. The post have also covered cherry-picking of needed files from this libray to transform STM32 F1 blinking “hello world” to STM32 L1 blinking project. Today I’ve found much faster way to start STM32 L1 programming.
This article’s toolchain consist of:
- Any text editor (I prefer Sublime Text),
- GNU ARM Embedded toolchain,
- Open source STLINK tool.
As you can see, I’ve used most of the parts from my last post. This toolchain will give you even more control and transparency on building process and of course it doesn’t have any proprietary software in it (except Sublime, but you’re able to use Atom or Vim).
You need some library to program your microcontroller (MCU) because usually you need to operate a plenty of sophisticated registers. All these registers usually have addresses like this:
0x40020C00. It’s better to operate with some human-readable code instead of all this digits, so you either need to define addresses as constants by yourself or use a library written by experienced developers. In my last post I used proprietary STM32 L1 standard peripheral library. Today I’m going to show you how to use GNU libopencm3 for that.
GNU ARM Embedded toolchain
I’m going to repeat installation steps from last article here to achieve completeness of this guide. So, usual APT commands for installing ARM cross-compilation tools (assuming you’re using some Debian-based distribution like Ubuntu):
sudo add-apt-repository ppa:team-gcc-arm-embedded/ppa
If installation finished correctly you should be able to test toolchain’s version.
Setting up the tool for hardware debugging.
sudo apt install openocd
Open source STLINK tool
libopencm3 is a GNU alternative for STM32 standard peripheral libraries. The project’s GitHub contains libopencm3 examples repository. If you clone this repo you’ll be able to build any example and flash it with simple terminal commands.
git clone https://github.com/libopencm3/libopencm3-examples.git
When I did it, building have stopped with error at building
examples/sam/d/d10-xplained-mini/miniblink/ stage (examples rev.:
9fbac7d, libopencm3 rev.:
13d4302). I don’t know what’s the reason of that but I don’t care about Atmel SAM. If you’ve got the same error ignore it, connect your STM32 L152 Discovery board through USB Mini-B cable and execute the following:
And that’s it! After that your board should start blinking with its LED.
Let’s write some simple program using libopencm3 library. For example, LCD ticker.
The screen should display some text moving it forward rapidly.
At first I recommend you to write some trivial example to make sure all your tools are working right.
Let’s show some text at our screen. It’s already done at one of libopencm3 examples. This example just displays “*HELLO” string. I’ve copied it to my repo and adopted Makefile. The link is already pointing at the
To flash the program on your board just execute this:
git clone https://github.com/quasiyoke/stm32-l152-lcd-ticker.git
lcd_display_hello function inside
main.c. This function determines the text we’re showing. Try to specify some other string. For example: “BYEBYE”
static void lcd_display_hello (void)
After your modifications, flash MCU again:
If you’ve broken something, don’t worry! Just checkout initial revision:
git checkout 1-lcd-hello -- .
Writing LCD ticker code
We’ll make LCD ticker in two steps:
- Writing function which shows arbitrary text
- Making the text move
lcd_display_hello show any string from some specified position. I think this function should have another name since that:
lcd_show. Look at the code:
This function is called from
TEXT specified at
You’re able to try this code by executing this:
git checkout 2-lcd-show -- .
Try to change offset specified at
main function to see different parts of the string on the screen or shorten the string to less than 6 characters to see it repeated.
To make text move let’s add infinite cycle to
And this is the final program as you can see it in my repository.
To flash it do the following:
git checkout master