STM32WB Bluetooth® LE – Heart Rate Sensor project Migration to Azure RTOS ThreadX OS

Revision as of 13:42, 6 April 2022 by Registered User (→‎ThreadX Important files)

1. Overview of Azure RTOS ThreadX OS

  • Picokernel, preemption-threshold, event-chaining unique features
  • Execution profiling and performance metrics
  • Totally available in source code (ANSI C and assembler)
  • Safety Certifications (TÜV, MISRA, UL)
  • Integrated with other Azure RTOS components:
    • USBX
    • NETX and NETXDUO
    • FILEX
    • LEVELX
    • TRACEX

Fully detailed description of ThreadX OS features and benefits can be found here: Microsoft AzureRTOS ThreadX Documentation

1.1. ThreadX code organization

File:ThreadXStructure.png
ThreadX folders organization

Main folders descriptions

  • cmake: original build system (not mandatory)
  • common: mcu architecture independent source code
  • common_modules: module feature (see: ThreadX Modules)
  • common_smp: Symmetric Multi Processing feature (see: ThreadX SMP)
  • docs: Just a dependency tree of Azure RTOS components
  • ports: mcu architecture dependent (M3, M4, M33,...)
  • ports_module: architecture dependent module code
  • ports_smp: architecture dependent smp code
  • samples: Microsoft example code (demo_threadx.c)
  • utility:
    • benchmarks: Thread-Metric test suite
    • execution_profile_kit: thread execution time tracker
    • low_power: Low power management files
    • rtos_compatibility_layers: adaptation layers (FreeRTOS, Posix, OSEK)








1.2. ThreadX way of working

A typical ThreadX application could be similar to this one (courtesy of Microsoft):

#include "tx_api.h"
unsigned long my_thread_counter = 0;
TX_THREAD my_thread;
main( )
{
    /* Enter the ThreadX kernel. */
    tx_kernel_enter( );
}
void tx_application_define(void *first_unused_memory)
{
    /* Create my_thread! */
    tx_thread_create(&my_thread, "My Thread",
    my_thread_entry, 0x1234, first_unused_memory, 1024,
    3, 3, TX_NO_TIME_SLICE, TX_AUTO_START);
}
void my_thread_entry(ULONG thread_input)
{
    /* Enter into a forever loop. */
    while(1)
    {
        /* Increment thread counter. */
        my_thread_counter++;
        /* Sleep for 1 tick. */
        tx_thread_sleep(1);
    }
}

tx_kernel_enter
This API is used to give the control to the OS, tx_application_define is called and the OS scheduler will be in charge to select the first thread (ready) to run.

tx_application_define
Here we have the creation of ThreadX resources (threads, semaphores, mutexes, events, queues,...). At least one thread must be created, other threads and resources could be created later. Please pay attention to how this function is called. Interrupts are disabled before (inside _tx_initialize_low_level) the call to this function and re-enabled just after (inside _tx_thread_schedule). So don't put any code relying on interrupt management inside this function.

1.3. ThreadX important files

  • tx_api.h: C header file containing all public definitions, data structures and service prototypes usable in the application.
    For a list of all available APIs, see: threadx APIs
  • tx_port.h: C header file containing all development-tool and target-specific data definitions and structures.
    For more details on target integration, see: target considerations
  • tx_user.h: Several configuration options can be set when building the ThreadX library and the application. These configurations cane be used to enable smallest code size, fastest execution, performance evaluation, error checking, quality coding rules and generally speaking extra features These options can be defined in the application source, on the command line or inside tx_user.h (in this latter case TX_INCLUDE_USER_DEFINE_FILE has to be defined). A "tx_user_sample.h" template is given for reference.
    For more info see: threadx configuration options
  • tx_initialize_low_level.S: Low-level processor initialization, including setting up interrupt-vectors, setting up a periodic timer interrupt source, saving the system stack pointer for use in ISR processing later and finding the first available RAM memory address for tx_application_define

Full description of initialization process between processor reset and the entry point of the thread scheduling loop can be found here: Initialization process

2. ST Sequencer VS ThreadX

2.1. ST Sequencer description

2.2. API mapping between ST Sequencer and ThreadX services

2.3. Idle Task and Low Power Management

3. Required modifications

3.1. ThreadX required files

3.2. OS resources declaration

3.3. Synchronization mechanism replacement

4. References