ThreadX

1 Introduction

ThreadX is a real-time operating system (RTOS), designed for embedded systems.
It offers two functional modes:

  • Common mode: It offers the common RTOS functionalities such as thread management and synchronization, memory pool management, messaging, and event handling.
  • Module mode: An advanced usage mode that allows loading and unloading of pre-linked ThreadX modules on-the-fly through a module manager.
ThreadX main blocks


The ThreadX core provides the common RTOS features, such as management of threads, semaphores, mutexes and SW timers. The bock diagram below shows the different functional blocks in the ThreadX core.

ThreadX core functional components


The Azure RTOS ThreadX library is provided with a FreeRTOS compatibility layer. This adaptation layer allows the user to develop a ThreadX application using FreeRTOS APIs.

Info.png All cores on which STM32s are based (Cortex M0, M0+, M4, M7, M33) are supported in the Azure RTOS ThreadX stack.


1.1 Unique features

ThreadX offers a set of unique features that give higher flexibility to the user:

1.2 Folder/file structure

ThreadX folder structure
  • common: contains the implementation of all ThreadX APIs. In addition it provides some utility features: SW timers, performance information and tracing features.
  • common_modules: contains the implementation of the ThreadX Module feature, allowing the dynamic loading of a prelinked ThreadX module. It is split into two folders:
  • module_lib: implements the txm_*.c API and should be used instead of tx_*.c when writing a ThreadX module.
    • module_manager: implements the functional block to be put on top of the ThreadX core to handle module loading, unloading, running, and so on.
    • ports/<core>/<compiler>: contains the implementation of the kernel scheduler, mostly in assembly language.
  • ports_module/<core>/<compiler>: contains the implementation of the ThreadX module related scheduler to fit with Module Manager requirements. The files in this directory are used instead of the port's files when the ThreadX Module is enabled.

More details about ThreadX features are available in the official documentation

2 STM32 integration

To correctly run ThreadX on STM32 based MCUs, the application should provide the 3 following user-level files:

  • tx_initialize_low_level.s: implements the tx_initialize_low_level() function called by the threadX kernel to initialize the RTOS heap base address and setup the SYSTick_Handler().

As tx_initialize_low_level.s is compiler and core dependent, a template of this file is provided for each compiler/core in the ThreadX source tree.

  • stm32XXX_hal_timebase_tim.c: Implements a timebase used by the HAL drivers, as the systick is reserved for the ThreadX kernel scheduler.
  • tx_user.h: configures the ThreadX RTOS using some of defines. A template of this file is provided in the ThreadX source tree. It is included in the ThreadX stack by the define TX_INCLUDE_USER_DEFINE_FILE.
Warning.png The FREE_MEM variable needs to be declared in the RAM region. It is used to initialize the start address of the ThreadX free memory.


Info.png The user level integration of lowpower within Azure RTOS ThreadX is to be supported in the future.


ThreadX template files

Note: STM32CubeIDE provides specific ThreadX debugging features. More information can be found in STM32CubeIDE user guide.

3 CMSIS-RTOS API Support

The CMSIS-RTOS v2 (CMSIS-RTOS2) provides generic RTOS interfaces for Arm® Cortex® processor-based devices. It provides a standardized API for software components that require RTOS functionality.

The list of features supported for ThreadX is as follows:

Feature Supported Short description
Kernel Information and control Y Provides version/system information and starts/controls the RTOS Kernel. More…
Thread Management Y Defines, creates, and controls thread functions. More…
Thread Flags N Synchronizes threads using flags. More…
Event Flags Y Synchronizes threads using event flags. More…
Generic Wait Functions Y Waits for a certain period of time. More…
Timer Management Y Creates and controls timer and timer callback functions. More…
Mutex Management Y Synchronizes resource access using Mutual Exclusion (Mutex). More…
Semaphores Y Accesses shared resources simultaneously from different threads. More…
Memory Pool N Manages thread-safe fixed-size blocks of dynamic memory. More…
Message Queue Y Exchanges messages between threads in a FIFO-like operation. More…


For more information about CMSIS-RTOS v2 APIs, please refer to the ARM manual: CMSIS-RTOS API v2.

For more information about the implementation of CMSIS-RTOS wrapper for ThreadX, please refer to its readme.

4 How to use

1. In order to initialize and configure Azure RTOS ThreadX kernel, the user needs to include the tx_api.h file.
2. Call tx_kernel_enter function from the main.

/* Start the ThreadX kernel */
tx_kernel_enter();

This function initializes the internal ThreadX data structure before calling the application’s definition function (tx_application_define) and starting the scheduler.

ThreadX application initialization

3. The user also needs to properly define the initial system resources in the tx_application_define function. Examples of system resources include threads, queues, memory pools, event flag groups, mutexes, timers and semaphores.

VOID tx_application_define(VOID *first_unused_memory)
{
  VOID *memory_ptr = first_unused_memory;
  /* USER CODE BEGIN  tx_application_define */
  /* User can add his own implementation to define the initial ThreadX system resources*/
  /* USER CODE END  tx_application_define */
}

The input parameter of tx_application_define is the first available RAM address to be used for the run-time memory allocation of thread stacks, queues and memory pools.
4. Initialize system resource initialization using ThreadX APIs :

  • Thread creation using ThreadX API :
 /* Create ThreadOne.  */
if (tx_thread_create(&ThreadOne, "Thread One", ThreadOne_Entry, 0,  
                       pointer, THREAD_ONE_STACK_SIZE, 
                       THREAD_ONE_PRIORITY, THREAD_ONE_PREEMPTION_THRESHOLD,
                       TX_NO_TIME_SLICE, TX_AUTO_START) != TX_SUCCESS)
{
  status = TX_THREAD_ERROR;
}
  • Mutex creation using ThreadX API :
/* Create the Mutex */
if (tx_mutex_create(&MutexOne, "Mutex One", TX_NO_INHERIT) != TX_SUCCESS)
{
  ret = TX_MUTEX_ERROR;
}

Note: It is also possible to create and delete system resources after starting the ThreadX kernel.
5. Define the entry function for each created thread :

void ThreadOne_Entry(ULONG thread_input)
{
  /* User can add his own implementation*/
}

5 Applications

STM32Cube firmware provides a set of functional applications covering the main features of Azure RTOS ThreadX. The ThreadX applications for STM32 MCUs are listed below.

ThreadX application Overview
Tx_Thread_Creation This application provides an example of Azure RTOS ThreadX stack usage, it shows how to develop an application using the ThreadX thread management APIs. It demonstrates how to create and destroy multiple threads using Azure RTOS ThreadX APIs. In addition, it shows how to use a preemption threshold between threads, and change priorities on-fly.

For more information please refer to the application readme file.

Tx_Thread_MsgQueue This application provides an example of Azure RTOS ThreadX stack usage, it shows how to develop an application using the ThreadX message queue APIs. It demonstrates how to send and receive messages between threads using ThreadX message queue APIs. In addition, it shows how to use the event chaining feature.

For more information please refer to the application readme file.

Tx_Thread_Sync This application provides an example of Azure RTOS ThreadX stack usage. It shows how to develop an application using the ThreadX synchronization APIs.

For more information please refer to application readme file.

Tx_FreeRTOS_Wrapper This application provides an example of Azure RTOS ThreadX stack usage. It shows how to develop an application using the FreeRTOS adaptation layer for ThreadX.

For more information please refer to application readme file.

Tx_CMSIS_Wrapper This application provides an example CMSIS RTOS adaptation layer for Azure RTOS ThreadX. It shows how to develop an application using the CMSIS-RTOS v2 APIs, and demonstrates how to create multiple threads using CMSIS-RTOS v2 for ThreadX APIs.

For more information please refer to application readme file.