logo
CODEMBIT
Reliable software engineering partner
Home Blog Tools

Getting started with Zephyr and STM32F072B Discovery board

By Toni Akkala, August 15th, 2022
img

Overview

STM32F07 Discovery board includes ARM Cortex-M0 based STM32F072RB microcontroller (MCU) with 128 kilobytes of Flash and 16 kilobytes of Static Random Access Memory (SRAM). ARM Cortex-M0 is one of the smallest Arm processors available, designed to be 32-bit option with the small footprint. The discovery board includes an on-board ST-LINK/V2 for debugging, I3G4250D ST MEMS motion sensor for motion sensing, LEDs' and push buttons as well as touch sensors to interact with the board. So, basic stuff to get started with your own projects.

Zephyr is Real-Time Operating System (RTOS) with an emphasis on microcontrollers. Zephyr includes everything you need to start application development for your devices. Zephyr kernel offers extensive suite of services and its configuration scheme is inherited from Linux kernel, using Kconfig and CMake. It offers different scheduling algorithms like Earliest Deadline First (EDF), memory protection and native networking stack supporting multiple protocols like LwM2M, MQTT and BLE. The most important one is Zephyr's large number of contributors within Zephyr community.

Setup

The board can be powered from the USB. By default, the MCU clock source is high speed internal RC oscillator (8 MHz). This is used to drive Phase Locked Loop (PLL) clock to produce 72 MHz system clock.

I did not have any prior experience of the Zephyr RTOS. I started with the getting started guide from their website. At the first glance, Zephyr looked quite different compared to other RTOS versions I have used so far. There is much more to setup before starting the development. First you need to install dependencies like cmake, python and devicetree, then the Zephyr itself and finally the toolchain you want to use. Even after that if you manage to build and flash the sample applications there is still lots of things to learn.

Building

After completing the last steps of the installation, I tried to compile a blinky sample application for my STM32F0072B Discovery board as it would be a good test to see that everything is working correctly:

west build -b stm32f072b_disco samples/basic/blinky
Building sources

As we can see from the output, the build was successful and the compilation shows the board memory region sizes. Now we should be able to flash the device with command:

west flash
Flashing board

Okay, now I was able to see red LED flashing. Cool!

Why red LED was flashing?

I was compiling the blinky example application. I wanted to see how the code controlled my board LED without making any definitions. My discovery board has 4 LEDs, red, orange, green and blue. According to the schematics, those are connected to microcontroller pins PC6-PC9. main.c uses devicetree to get a structure to control the LED, led0 in this case.

/* The devicetree node identifier for the "led0" alias. */ #define LED0_NODE DT_ALIAS(led0) /* * A build error on this line means your board is unsupported. * See the sample documentation for information on how to fix this. */ static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios); ... ret = gpio_pin_toggle_dt(&led);

I had a look at the final, generated devicetree build/zephyr/zephyr.dts file. From there I could find that led0 has been assigned with the address of red_up_led_3 which is defined like this:

led0 = &red_up_led_3; red_up_led_3: led_3 { gpios = < &gpioc 0x6 0x0 >; label = "User LD3"; };
So the red LED is connected to the microcontroller pin PC6 by using devicetree which uses data structures to describe hardware components.

Conclusion

Even though there was more to configure than with other operating systems, Zephyr's good documentation saved the day. I did not have any problems with the installation or building nor flashing the development board. There are things to learn but after that you can use well structured tools to build your embedded systems.

References: