STM32CubeIDE Tutorial

../_images/stm32_cubeide_logo.png

This section serves as a comprehensive guide to assist in creating and understanding the key points of a basic Safe DDS application using STM32CubeIDE tool.

This project has been tested on the NUCLEO-F746ZG board with STM32CubeMX v6.16.1, STM32CubeIDE v2.0.0, FreeRTOS v10.2.1 and LwIP v2.1.2. Check Supported platforms for more information.

Note

This guide might require adaptations to work with a different hardware, RTOS, network stack or STM32CubeMX/STM32CubeIDE version.

In order to get support please contact support@eProsima.com.

../_images/nucleo_f746zg.png

Note

It is recommended to first read the Getting Started section to gain a better understanding of the basic concepts and terminology used in this section.

Project Configuration

This section describes how to configure an STM32CubeIDE project that uses the Safe DDS library. The project is first configured and generated with STM32CubeMX, and then imported into STM32CubeIDE to add the Safe DDS library and build the application. Follow the steps below in the order presented to ensure the project is set up correctly.

1. Create the project in STM32CubeMX

  • Select File > New Project > Board Selector and choose the NUCLEO-F746ZG board.

  • When asked to Initialize all peripherals with their default Mode?, select Yes.

2. Configure FreeRTOS for Safe DDS

To configure FreeRTOS, set the following parameters in the STM32CubeMX configuration window:

  • Enable FreeRTOS by choosing CMSIS v2 version in Pinout & Configuration > Middleware and Software Packs > FREERTOS.

  • Configure TOTAL_HEAP_SIZE and Safe DDS task’s stack size. 30 KB for the total heap and 3k words for the default task (our Safe DDS task) shall be sufficient for Safe DDS application.

  • Enable USE_NEWLIB_REENTRANT to use newlib reentrant functions.

3. Configure LwIP for Safe DDS

Safe DDS uses its POSIX UDPv4 transport on top of the LwIP sockets API (BSD/POSIX-compatible API), not the LwIP raw API. Hence, the LwIP sockets API and its socket options must be enabled.

To configure LwIP, set the following parameters in the STM32CubeMX configuration window:

  • Enable LwIP middleware in Pinout & Configuration > Middleware and Software Packs > LWIP.

  • Configure PBUF_POOL_BUFSIZE to MTU size. For the board used in this example, the MTU size is 1500.

  • By default Safe DDS will use UDP multicast operation, so LWIP_IGMP, LWIP_MULTICAST_TX_OPTIONS and SO_REUSE shall be enabled. Some of these options are found after showing advanced parameters.

  • By default, Safe DDS might require a send operation to be blocking so LWIP_SO_SNDTIMEO shall be enabled.

  • Set MEMP_NUM_PBUF to 32, and MEM_SIZE to 4000.

  • Configure the Driver_PHY, for this board it is LAN8742A.

4. Time Base Configuration

STM32CubeMX might suggest using a HAL timebase source other than the Systick. To configure the timebase source to use the TIM1 peripheral, follow these steps:

  • Navigate to System Core > SYS > Timebase Source and select TIM1.

5. Generate the code

  • In Project Manager > Project:

    • Set the Project Name and Project Location.

    • Select STM32CubeIDE as Toolchain/IDE and check the Generate Under Root option.

  • In Project Manager > Code Generator, check Generate peripheral initialization as a pair of ‘.c/.h’ files per peripheral.

  • Click Generate Code and close STM32CubeMX once the generation has finished.

6. Import the project in STM32CubeIDE

  • Open STM32CubeIDE and select File > Import > General > Existing Projects into Workspace.

  • As root directory, select the folder containing the project generated by STM32CubeMX (the one with the .ioc file).

  • In the Project Explorer, right-click on the project and select Convert to C++.

7. Add a New Source Folder for Safe DDS

To add the Safe DDS library to the project follow these steps:

  • In the Project Explorer, right-click on the project and select New > Source Folder. Name the folder something like Safe-DDS.

  • Navigate to the STM32CubeIDE support package folder and run the create_package.sh script followed by the path to the Safe DDS source release folder. This script generates the required files for a FreeRTOS + LwIP application on an STM32CubeIDE environment.

  • Copy the Safe DDS src and include generated folders to the folder previously created in the STM32CubeIDE project.

  • Move to the project properties, navigate to C/C++ Build > Settings and select the [All configurations] configuration (it defaults to the Debug configuration).

  • Navigate to MCU/MPU G++ Compiler > Include Paths and add the path to the Safe DDS include folder.

  • Navigate to MCU/MPU GCC Compiler > Preprocessor and add the following symbols:

    • ETH_TX_DESC_CNT=12U

    • ETH_RX_DESC_CNT=12U

Safe DDS application

After the project has been set up, the next step is to create a Safe DDS application:

1. Add the application files to the project

  • Add the provided Safe DDS signature file to the Core/Src folder inside the application.

  • Navigate to the tutorial_stm32cubeide example folder.

  • Add the provided SafeDDSApp.cpp file to the Core/Src folder inside the application.

  • Add the provided SafeDDSApp.hpp and HelloWorldTypeSupport.hpp files to the Core/Inc folder inside the application.

2. Configure the Safe DDS Task

Once all necessary files are in place, modify the Safe DDS task in Core/Src/freertos.c.

  • Make the Safe DDS task wait until the device has an assigned IP address.

  • Then call safedds_app_init function, which creates a SafeDDSApp object and spins it forever.

An example of a Safe DDS entry point could be:

void safedds_app_init(uint32_t, uint32_t);
void SafeDDSTask(void *argument)
{
    // Wait until the device has an IP address
    // ...

    const uint32_t ip_addr = netif_default->ip_addr.addr;
    const uint32_t port = 8000;

    // Init Safe DDS app:
    safedds_app_init(ip_addr, port);
}

The LwIP headers might be required for the network handling code. If so, include them in the user code includes section of the same file:

#include "lwip.h"

Running the application

Select the Release build configuration, build the project and flash it to the device. Run the Getting Started example in a local machine on the same network as the device. The console output should show messages similar to the following:

[DW: 0] Message: HelloWorld with index: 4
[DW: 16777217] Message: Hello World from STM32! with index: 1
[DW: 16777217] Message: Hello World from STM32! with index: 2
[DW: 0] Message: HelloWorld with index: 5
[DW: 16777217] Message: Hello World from STM32! with index: 3
[DW: 16777217] Message: Hello World from STM32! with index: 4
[DW: 0] Message: HelloWorld with index: 6
[DW: 16777217] Message: Hello World from STM32! with index: 5
[DW: 16777217] Message: Hello World from STM32! with index: 6
[DW: 0] Message: HelloWorld with index: 7
[DW: 16777217] Message: Hello World from STM32! with index: 7
[DW: 16777217] Message: Hello World from STM32! with index: 8