Developing Anjay for FreeRTOS
FreeRTOS is a free real-time operating system distributed under MIT license. Its simplicity, accessibility and functionality live up to embedded developers' expectations perfectly. It supports the majority of microcontroller platforms you can think of. Compared to other operating systems like Mbed OS or Zephyr, FreeRTOS offers lower-level and lighter API as well as higher configurability. It has been developed and maintained by a large group of programmers as an open-source project for over 15 years. This is why we decided to use FreeRTOS for the next Anjay integration.
Our new demonstration application is based on the P-L496G-CELL02 development board with STM32L496AGI6 microcontroller. It is compatible with the X-NUCLEO-IKS01A2 expansion board providing: accelerometer, gyroscope, magnetometer, humidity, temperature and pressure sensors. Internet connection is established thanks to the Quectel BG96 module placed on a cellular expansion board with antenna.
The project was prepared in the STM32CubeIDE environment. The biggest advantage of this software is that it’s free. What is more, it offers the possibility of fast and comfortable hardware configuration. Many files were automatically generated by STMCube32MX so that we didn’t have to write the initialization code for all elements manually. Moreover, ST supplies programmers with APIs for various devices – in our case Cellular software expansion turned out to be really helpful.
After initialization of all configured peripherals the program sets up and launches three main tasks related to cellular features, client and onboard joystick. One of them allows users to enter the configuration menu and arrange the options of LwM2M connection, such as LwM2M Server URI, device endpoint name and pre-shared key. These settings are then stored in the 8-Megabyte flash memory accessed through the QUAD SPI interface. Implementation approach concerning sensor objects was similar to that from Zephyr demo – we created two generic sensor representations and reused them for all the sensors in order to reduce the application size. When it comes to joystick, we ensured software debouncing for the select button.
Since FreeRTOS is considered and built as a lightweight operating system, it does not provide utilities present in Zephyr, like the
console library – we had to implement custom functions for console interaction through UART.
You can do this in two ways:
_writefunctions denoted as
__attribute__((weak)). They should be placed in a generated syscalls.c file. If a compiler finds your version of these functions, it will compile your version and discard the weak version. You can call standard
- Write your own functions from the ground up. Using variadic function arguments may come in handy usage-wise.
We chose the latter method. In both cases you have to receive or transmit characters in some way. Thanks to the HAL APIs it is quite easy – just invoke
HAL_UART_Receive with proper arguments.
Developers writing software for FreeRTOS must beware of faulty files generated by STM32CubeIDE. Unfortunately, a problem of invalid body of _sbrk function placed in sysmem.c file, which causes issues with dynamic allocation among others, has existed for a long time and ST hasn’t solved it yet. One needs to provide correct implementation on its own.