Create a Luos project with PlatformIO
What you will learn:
You just heard about Luos, or you have already finished the Luos Get started and Trainings and you want to go further with Luos: this tutorial is for you! We will accompany you from scratch and step by step with your first project creation and Luos integration!
Part 1: Project creation
1. Introduction
Beginning with a project can be painful and a long process! New environment, new tools, ... You may sometimes want to give up before starting.
But don't worry, we will guide you through the process! You will learn how to set up a PlatformIO environment and how to make a good project structure.
This tutorial uses VSCode as an IDE. Before jumping into the code, let's set up your project with PlatformIO.
If PlatformIO is not already installed on your computer, please follow our Get started tutorial.
In this tutorial, we will use a ST NUCLEO F072RB board for a project creation with ST board, and an Arduino MKRZero board for project creation with Arduino board.
2. Create a new project
1- Create a new project
- ST
- Arduino
Choose the default location of your projet on your computer.
At this step, PlatformIO creates for you a complete project:
- Folder include: where you put the project's header files.
- Folder src: where you put the put project's source files.
- Folder lib: where you put the Luos services.
- plateformio.ini: the PlatformIO project entry point.
2- Click on finish
3- Click on Yes accept
3. Node's config files creation
Luos project uses a file, placed at the root of the folder, called node_config.h. This file allows you to configure the Luos library and match your hardware's configuration with Luos's default configuration.
- Right click on "Project name".
- New Files → node_config.h.
- Copy the code below and save.
/******************************************************************************
* @file node_config.h
* @brief This file allow you to use standard preprocessor definitions to
* configure your project, Luos and Luos HAL libraries
* @author Yourself
* @version 0.0.0
******************************************************************************/
#ifndef _NODE_CONFIG_H_
#define _NODE_CONFIG_H_
#endif /* _NODE_CONFIG_H_ */
If you don't know what node_config.h
is, please read the Luos documentation.
In this example, we will use the default configuration of Luos HAL for ST and Arduino, so nothing to change right now 😅.
4. Configure platformio.ini to use your project
platformio.ini is the project configuration file. You must fill in the file with the information relative to your project. See the file definition in the table below, according to the board you have:
- ST
- Arduino
build_unflags = -Os
build_flags =
-O1
-include node_config.h
debug_tool = stlink
upload_protocol = stlink
Defined for STM32 Frameworks:
USE_HAL_DRIVER
→ possibility to use HAL file of STM32 Framworks.USE_FULL_LL_DRIVER
→ possibility to use low Level file of STM32 Framworks.
build_unflags = -Os
build_flags =
-O1
-include node_config.h
- Modifying compilation flag
-Os
to-O1
. - Include the node_config.h file.
- Setup tools to upload or debug the program if necessary.
5. Create the main file
The main file of your project is your entry point! Let's fill in this file with our basic needs:
- ST
- Arduino
- In src → New Files → main.c
- Fill in the file main.c with the following code:
/******************************************************************************
* @file main
* @brief main file of the project
* @author Yourself
* @version 0.0.0
******************************************************************************/
#include "stm32f0xx_hal.h"
/*******************************************************************************
* Definitions
******************************************************************************/
/*******************************************************************************
* Variables
******************************************************************************/
/*******************************************************************************
* Function
******************************************************************************/
/******************************************************************************
* @brief main function
* @param None
* @return None
******************************************************************************/
int main(void)
{
// Init
HAL_Init();
// Loop
while (1)
{
}
}
- The include of stm32f0xx_hal.h must be adapted to the STM familly MCU you chose. In our case, ST NUCLEO STM32F072RB corresponds to the F0 family.
You can modify the clock frequency of you MCU. We recommand to do it with CubeMx.
In Arduino, the main file is automatically created and filled with two functions:
#include <Arduino.h>
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
6. Create interruption files handler
For many MCUs, you have acces to an IRQ handler fonction. These functions are called when an interruption happens (exception and peripherial interruption). You have to declare these functions depending on the frameworks you use.
- ST
- Arduino
- In src → New Files → stm32f0xx_it.c, add this code:
/******************************************************************************
* @file stm32f0xx_it
* @brief Interrupt Service Routines.
* @author Yourself
* @version 0.0.0
******************************************************************************/
#include "stm32f0xx_hal.h"
#include "stm32f0xx_it.h"
/*******************************************************************************
* Definitions
******************************************************************************/
/*******************************************************************************
* Variables
******************************************************************************/
/*******************************************************************************
* Function
******************************************************************************/
/******************************************************************************/
/* Cortex-M0 Processor Interruption and Exception Handlers */
/******************************************************************************/
void NMI_Handler(void)
{
}
void HardFault_Handler(void)
{
while (1)
{
}
}
void SVC_Handler(void)
{
}
void PendSV_Handler(void)
{
}
void SysTick_Handler(void)
{
HAL_IncTick();
}
/******************************************************************************/
/* STM32F0xx Peripheral Interrupt Handlers */
/* Add here the Interrupt Handlers for the used peripherals. */
/* For the available peripheral interrupt handler names, */
/* please refer to the startup file (startup_stm32f0xx.s). */
/******************************************************************************/
- In Include → New File → stm32f0xx_it.h, add this code:
/******************************************************************************
* @file stm32f0xx_it
* @brief Interrupt Service Routines.
* @author Yourself
* @version 0.0.0
******************************************************************************/
#ifndef __STM32F0XX_IT_H
#define __STM32F0XX_IT_H
#ifdef __cplusplus
extern "C" {
#endif
/*******************************************************************************
* Definitions
******************************************************************************/
/*******************************************************************************
* Variables
******************************************************************************/
/*******************************************************************************
* Function
******************************************************************************/
void NMI_Handler(void);
void HardFault_Handler(void);
void SVC_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
#ifdef __cplusplus
}
#endif
#endif /* __STM32F0XX_IT_H*/
If you choose to use the Arduino framworks, there is no need to declare the IRQ function.
7. Compile your first project
- ST
- Arduino
You now have a complete functional environment, but what about Luos? Let's find out in the next steps.