Using embOS with STM32CubeMX

From SEGGER Wiki
Jump to: navigation, search

This article describes how embOS may be used with STM32CubeMX.

MadeForSTM32

MadeForSTM32 is a new quality label delivered by ST and is granted after an extended evaluation process. It helps engineers identify software solutions with the highest level of integration and quality for the STM32 microcontrollers’ ecosystem.

embOS was one of the first solutions to receive the MadeForSTM32 (v1) label in an advanced collaboration of SEGGER and ST Microelectronics. As a result, SEGGER has published the official I-CUBE-EMBOS expansion for STM32CubeMX, which may be used to generate ready-to-go embOS projects for use with ST's STM32F746G-Discovery board and various IDEs. Furthermore, it may also be used as a starting point for creating further embOS projects for different evaluation boards and devices.

For more information on MadeForSTM32 and the I-CUBE-EMBOS expansion, please visit SEGGER.com.

Creating projects from scratch

To use embOS with STM32CubeMX, customers may either

  • use the I-CUBE-EMBOS expansion for STM32CubeMX and adapt it to their desired target hardware,
  • generate a project in STM32CubeMX and subsequently add all generated HAL files and further sources to the shipped embOS start project for their desired target hardware, or
  • generate a project in STM32CubeMX and subsequently add embOS to that generated project.

This article will walk readers through the latter approach. The described configuration of the target application in STM32CubeMX, however, applies to and must therefore be followed in either approach.

Steps to perform in CubeMX

Creating the basic project

Start STM32CubeMX and create a project for any specific target hardware. For this walk-through, we create a project for the 32F429IDISCOVERY board, but chosing any different evaluation board or device will work as well. After the project was initialized, feel free to add clock configurations, GPIO settings, third-party middleware compontents and further settings as needed. The result should look similar to the screenshot below:

CubeMX InitialProject.png

When done configuring the project, begin to adapt it for embOS along the guidelines given in the following sections.

Interrupt priorities for embOS

To prepare the project for use with embOS, care needs to be taken of the required interrupt handlers for using embOS with Cortex-M devices. To do so, navigate to System Core in the menu outline and select NVIC. In the editor window, select the NVIC tab and check the Enabled checkbox for both the Pendable request for system service and System tick timer as shown below:

CubeMX InterruptPriorities.png

Please also note the column Preemption Priority. With embOS for Cortex-M, specific rules for Interrupt prioritization apply:

  • embOS utilizes the Pendable request for system service to perform context switches; it must use the lowest priority in the entire system in all cases. Hence, it gets configured for a priority of 15 in this example.
  • embOS utilizes the System tick timer to generate a time base; it may use any valid priority for an embOS interrupt. Typically, however, using the second-to-lowest priority in the entire system is advised. In this example, it therefore gets configured for a priority of 14.
  • In case your application utilizes further interrupts (that shall use embOS API), please ensure these are configured for valid embOS interrupt priorities as well.

Interrupt handler generation for embOS

STM32CubeMX may generate handlers for specific interrupt sources.

  • Since embOS includes its own handler implementation for the Pendable request for system service, that handler may not be generated by STM32CubeMX. To disable its creation, switch to the Code generation tab and uncheck the Generate IRQ handler checbox for this specific handler.
  • While embOS includes a handler implementation for the System tick timer as well, a seperate handler should be generated by STM32CubeMX. It should call HAL API functions (enable via according checkbox), which we will later use to call embOS' own handler implementation as a callback.
  • In case your application utilizes further interrupts, appropiate handlers for these may be generated as required.

CubeMX ISRGeneration.png

Steps to perform in the IDE and project files

Integrating embOS

After generating the project, open its location in the file system:

CubeMX FileSystem.png

This folder structure may be extended as needed. For the integration of embOS, for example, you may want to add a folder embOS with subfolders Inc, Lib, SEGGER, and Setup.

Subsequently, copy all relevant files from your shipment of embOS (or, alternatively, from an embOS evaluation version) to the created subfolders:

  1. To the Inc folder, copy all files from {SHIPMENT_ROOT}\Start\Inc\:
    1. BSP.h (optional)
    2. BSP_UART.h (optional)
    3. Global.h
    4. JLINKMEM.h
    5. OS_Config.h
    6. RTOS.h
  2. To the Lib folder, copy one appropriate library from {SHIPMENT_ROOT}\Start\Lib\, e.g.:
    1. libosT7VHLDP.a (debug & profiling build of embOS for ARMv7-M little-endian devices with FPU using the hard floating-point ABI)
    2. libosT7BR.a (release build of embOS for ARMv7-M big-endian devices without FPU)
    3. libosT6BDP.a (debug & profiling build of embOS for ARMv6-M big-endian devices)
  3. To the SEGGER folder, copy all files from an appropriate board support package, e.g. from {SHIPMENT_ROOT}\Start\BoardSupport\ST\STM32F429_STM32F429I_Discovery\SEGGER\:
    1. SEGGER.h
    2. SEGGER_RTT.c
    3. SEGGER_RTT.h
    4. SEGGER_RTT_ASM_ARMv7M.S (with ARMv7-M devices only)
    5. SEGGER_RTT_Conf.h
    6. SEGGER_RTT_printf.c (optional)
    7. SEGGER_RTT_Syscalls_GCC.c (optional; GCC-specific source file: different name with embOS for IAR and embOS for Keil MDK)
    8. SEGGER_SYSVIEW.c
    9. SEGGER_SYSVIEW.h
    10. SEGGER_SYSVIEW_Conf.h
    11. SEGGER_SYSVIEW_ConfDefaults.h
    12. SEGGER_SYSVIEW_Config_embOS.c
    13. SEGGER_SYSVIEW_embOS.c
    14. SEGGER_SYSVIEW_embOS.h
    15. SEGGER_SYSVIEW_Int.h
  4. To the Setup folder, copy the following *.c-files from an appropriate board support package, e.g. from {SHIPMENT_ROOT}\Start\BoardSupport\ST\STM32F429_STM32F429I_Discovery\Setup\:
    1. BSP.c (optional)
    2. BSP_SDRAM.c (optional)
    3. JLINKMEM_Process.c
    4. OS_Error.c
    5. OS_MallockLock.c (GCC-specific source file: different name with embOS for IAR and embOS for Keil MDK)
    6. OS_Syscalls.c (GCC-specific source file: different name with embOS for IAR and embOS for Keil MDK)
    7. RTOSInit_STM32F4xx.c

Open the project and add the folders and files to it. The result should resemble the given example below (which uses the Eclipse-based STM32CubeIDE):

CubeMX ProjectTree.png

In the next step, configure the include paths for embOS header files:

CubeMX Includes.png

Configure the linker settings for the embOS library (required with Eclipse-based IDEs, but not with e.g. IAR):

CubeMX Lib.png CubeMX LibPath.png

Configure the preprocessor options for your application (must match the chosen embOS library configuration):

CubeMX PreProc.png

After these configurations, embOS was fully integrated with the project.

Modifications to the linker file

embOS expects specific symbols in order to retrieve information about stack and heap in your project:

  • __heap_end__
  • __heap_start__
  • __stack_end__
  • __stack_start__

These symbols need to be added to the linker file, which is the generated *.ld-file in the project's root path, as shown below:

CubeMX Linker.png

Merging the embOS tick with the generated SysTick_Handler()

Since STM32CubeMX already initializes the hardware timer and the SysTick interrupt, some configurations inside RTOSInit_*.c must be removed.

Open RTOSInit_*.c and perform the following changes to OS_InitHW():

CubeMX RTOSInit InitHW.png

Furthermore, the SysTick_Handler() for embOS should now be used as a callback for the generated handler. To do so, apply the following modification to RTOSInit_*.c as well:

CubeMX RTOSInit SysTick.png

For the generated handler to actually call the callback function, add the following line to the generated SysTick_Handler() implementation, which can be found in stm32f*_it.c inside the Src folder of the generated project:

CubeMX SysTick Handler.png

Handle duplicate syscalls implementation

The files syscalls.c and sysmem.c should be excluded from build in the project, since their contents are already implemented in OS_Syscalls.c with embOS. The files may be found in the Src folder:

CubeMX Excluded Files.png

Add an embOS application

To use embOS in the project, main.c needs to be modified to include the following:

  1. Inclusion of RTOS.h
  2. Declaration of task control block and task stack
  3. A function which will be run as the task
  4. embOS kernel and task initialization:
    1. OS_Init()
    2. OS_InitHW()
    3. OS_TASK_CREATE(), with task control block, task name, priority, function name and task stack
    4. OS_Start()

The resulting modifications to main.c should therefore resemble the following example:

CubeMX Main embOS.png

Subsequently, the application may be compiled and executed in hardware. From here, feel free to modify the initial task, to add further tasks, OS objects and interrupt service routines to create your own embOS application.