Porting of a firmware from Arduino to STM32 architecture
by Francesco Carella
The goal of this work was the porting of EasyScada firmware from the Arduino platform to the STM32 Nucleo board. This work was carried in collaboration with AGEvoluzione, an innovative small-medium company that deals with Internet of Things (IoT) devices.
IoT is a concept related to the fusion of the Internet with physical objects, so that they are able to communicate and exchange data over wireless networks. IoT sensors allow the collection of these data that will be analyzed and processed, in order to make the devices make decisions and perform operations. IoT is used in a wide range of applications such as medical field, home automation, logistics, etc.
Specifically for this work, the activity was done as a part of the development of NORBI, a pill dispenser that helps a patient or a caregiver to manage the scheduling of a prescribed pharmacological therapy. NORBI is composed by 28 cells containing pills, which are automatically dispensed at a predetermined time. It is controlled via a smartphone application and it gives vocal, visual and SMS alerts when the patient has to take the medicines.
The result of the assumption of the pills is notified via WiFi to the associated smartphone.
The implementation of NORBI is based on the EasyScada libraries.
EasyScada is a generic automation software written in C++ with the Aduino’s IDE. The original version of the code runs on Arduino platform, but it was improved using an AGEduino board with ATWINC1500 WiFi board.
Afterwards, the code has been ported to the STM32 Nucleo board with the Inventek ISM WiFi board, using the STM32duino extensions, to keep the code compatible with Arduino’s IDE.
The Arduino environment
Arduino is an open-source platform born at Ivrea Interaction design Institute in 2005. It is used to develop a wide range of projects involving different branches of computer science.
It was born to simplify the creation of prototypes for the control of various type of sensors (temperature, brightness, humidity), motors and actuators, but also for the creation of IoT embedded devices.
The Arduino’s IDE contains a text editor to write the code, an area for warnings and error messages, a text console, a series of buttons that perform common functions (such as compiling the code and uploading it to the board) and several menus. There is also a button to be able to display the serial monitor, which is a window in which we can read read the output produced by the board.
High-level firmware description
Every EasyScada device can be seen as a sort of neural network. Every device has 10 channels with the following features:
- a 32-bit value that can be transmitted to other devices;
- a name (ASCII string);
- a formula;
- a timeout and a sensibility threshold;
- an array of 5 indexes that represent the observers stored in the observer’s table;
- an array of 5 channels of the observer;
- an MQTT connection status byte.
The communication between channels is managed by the ChannelsHub, that features:
- a name;
- the message reception port;
- the IPV4 address, completed with 5 observers;
- the SSID name for the wireless network;
- the password of the wireless network.
LivingStones Protocol
LivingStones Protocol (LSP) is the communication protocol of EasyScada devices. It is a P2P request/response communication protocol based on UDP. It is used for the communication between EasyScada devices. LSP uses UDP sockets and it is based on exchange of ASCII text messages. Every message is composed by the following elements:
- an ID that identifies kind of the message;
- a channel index;
- the message text;
- the final character of the string $.
There are four main types of messages:
- channel value messages;
- channel configuration messages;
- device configuration messages;
- memory buffer transfer messages.
When a message is correctly received, an appropriate response identifier is received as acknowledgment. The “K” message is a special message used for managing the automatic sending of notifications.
In addition to UDP communication, the LSP protocol added the support for MQTT. MQTT is a lightweight publish/subscribe message transport protocol. In LSP it is used to allow the communication of the channels’ values on the internet, with an only MQTT broker. MQTT is very similar to Observer/Notifier but it is not peer-to-peer (P2P), in fact each device communicates with a broker, not with another equal level device.
Boards used during the activity
The project started from the AGEduino board. AGEduino represents an upgrade to the original Arduino platform. It features the ATMEL ATmega1284P processor at 16MHz. It is compatible with Arduino libraries and IDE using the Sanguino extensions. It feature a better memory capability with respect to Arduino: 128KB Flash, 16 KB RAM, 4 KB EEPROM.
The WiFi board used with AGEduino is the ATWINC 1500-MR210PB, that is a low power consumption 802.11 b/g/n IoT module, specifically optimized for low-power IoT applications. This module provides SPI ports to interface with a host controller.
Then the code has been moved to STM32 L152RE Nucleo board. The STM32 Nucleo board supports Arduino connectivity and it’s a flexible and expandable environment. The processor is the ARM Cortex-M3 32-bit at 32 MHz. It is a better hardware with respect to AGEduino, in particular it features: 512 KB Flash memory and 80 KB RAM.
The associated WiFi board is the Inventek ISM43362-M3G-L44 board. It is a member of eS-WiFi family of embedded wireless internet connectivity devices. It supports a Cortex-M3 microcomputer. The peculiarity of this board is that it only requires AT commands to establish connectivity with the main device.
Organization of the codebase
The code is organized in two main directories called “AGE_SCADA” and “libraries”. Every operation of the firmware is represented by a Finite State Machine (FSM), and the directory “AGE_SCADA” contains all the files regarding the automata performing the operations. Also it contains the main sketch of the program, into which all the FSMs are enabled.
Instead, the libraries
directory contains all the files related to the libraries used in the project, into which the operation performed by the FSMs are defined.
The most important libraries are the ones related to the transmission/reception of the messages sent by the EasyScada device’s channels and the managing of the channels functioning and parameters.
The libraries
directory contains also the WiFi drivers, related to the Inventek ISM WiFi board.
These drivers have been added to the code instead of the old WiFi drivers related to the ATWINC 1500 WiFi board, used with the AGEduino.
In these files, the function related to the WiFi connection and Client/server and UDP functions are defined.
To do so, AT commands have been used, as a difference with respect to the old WiFi board.
Results
As results, some statistics of the git repository into which the project was uploaded were collected using GitStats tool, to see how the porting activity went over time.
The period of time taken into consideration to extrapolate this results goes from the time of creation of the git repository to the end of the porting work, which means from 2017-10-16 to 2021-04-07, but the porting was all carried out in 2021.
- The final code has a total of 110437 lines of code (301280 added, 190843 removed).
- The final code includes 692 total files, with an average file size of 9464.84 bytes.
- During the activity, 35 commits were carried out, adding 12120 lines of code and removing 39350 lines.
Other metrics were collected using the Metrix++ tool, which analyzes different regions in the code and derives several type of data.
These metrics are the Cyclomatic Complexity, the number of lines dedicated to preprocessor, the total number of comments lines, the maximum level of indentation and the total number of magic numbers.
Finally, the messages of LSP protocol have been tested using the tool Node-RED. It is a flow-based programming tool that simplifies the creation of applications, making available a list of “nodes” (represented by blocks) that can be dragged and dropped into a web browser grid and connected between them.
The functionalities of the blocks can be tested using a dashboard, into which insert the commands and see the behavior of the blocks.
Conclusions
The goal of this work was the porting of the EasyScada firmware from AGEduino to STM32 nucleo board, in order to move to a better hardware and to abandon the Arduino environment.
This was just an intermediate step, but it was useful to have a working version of the firmware that could be tested on the final hardware.
A future work will be the migration to STM32CubeIDE, the development tool of STM32, with the code written in pure C++, in order to work with an environment compatible with the ARM processor of the STM32 Nucleo board.