Dev Topics
posted: September 16, 2020 

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:

  • Override _read and _write functions 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 printf and scanf functions then;
  • 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_Transmit or 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.

The project is available on our GitHub page as a public repository released on Apache 2.0 license. We hope it will help you explore the LwM2M protocol and the Anjay library.

Michał Oleksy
Software Engineer

Recommended posts

back icon
Hello! We’re always open.
Let’s start a conversation.

Marketing Consent Opt In Date:
Marketing Consent Opt In Reason:
Marketing Consent Opt Out Date:
Marketing Consent Opt Out Reason:

Thank you!

Your message has been sent. Our team will contact you as soon as possible.


AVSystem sp. j., Radzikowskiego 47D, Cracow is the controller of your personal data. We will process your personal data to contact you in response to your message. If you agree to a specific communication channel, we can also contact you in the future for marketing purposes. You can withdraw your consent at any time or object to data processing. You have the right to r access your personal data, the right to rectification data, the right to erasure data, the right to demand the restriction of the processing of your personal data and the right to object to the processing of your data. More information about processing of your personal data is available here.

Message Btn

Hello!
Contact us!

This website is using cookies

We use cookies for statistical and marketing purposes and to improve the quality of our services. The information stored in cookies usually allow the identification of a specific device or user’s browser, so they may contain personal data. By continuing to use this website with setting the web browser in a way which alows the use of cookies by the website means your’s consent to the use of cookies. You can change your web browser settings at any time.

More information on the processing of personal data and cookies you can find in our Privacy and cookies policy.

Accept