on
2021-12-14
12:25 AM
- edited on
2025-03-17
6:38 AM
by
Laurids_PETERSE
A mutex is basically a binary semaphore, which means that only one thread can own a mutex at a time. In addition, the same thread may perform a successful mutex get operation on an owned mutex multiple times. There are two main operations on the mutex object: tx_mutex_get and tx_mutex_put.
The get operation obtains a mutex not owned by another thread, while the put operation releases a previously obtained mutex. For a thread to release a mutex, the number of put operations must equal the number of prior get operations. ThreadX mutexes are used solely for mutual exclusion, this means it pertains to controlling the access of threads to certain application areas (also called critical sections or application resources).
The purpose of this article is to provide a brief explanation with a working example on how the mutex works. Although the example is using the NUCLEO-H723ZG, you can use the same steps for other STM32H7 based boards. The main differences are usually pinout and clock configuration.
This article will start a project from scratch, add the AzureRTOS Middleware using the Software Packs available in the STM32CubeIDE and add a few code lines. 2 Threads with the same priorities will be used to showcase the mutex, controlling the printf (acting as the resource). In this demo, both Threads will get and put the mutex. This is the demo representation:
Getting to know the functions:
UINT tx_mutex_get( TX_MUTEX *mutex_ptr, ULONG wait_option);
Parameters:
UINT tx_mutex_create( TX_MUTEX *mutex_ptr, CHAR *name_ptr, UINT priority_inherit);
Parameters:
Launch STM32CubeIDE (version used 1.7.0), select your preferred workspace, click on Launch. Once open, locate the Information Center, select to Start New STM32 Project – alternatively, click on File->New->STM32 Project. For this demo, we’ll use the NUCLEO-H723ZG as the starting point
Give your project a name, just remember to avoid space and special characters – the name given by this article is “Mutex”. A pop up asking if we should initialize the peripherals in the default mode, click yes. Then second popup to ask to open device configuration perspective, click yes again
Time to add the software pack:
Browse and locate the AzureRTOS pack in the component selection window and open the RTOS ThreadX and check the Core box and click OK
This will add the Software Pack in the lower part of the Categories:
Now, by clicking on it, you can add the RTOS ThreadX box, this will show the configuration options for the AzureRTOS application
As this demo is just meant to create small printfs to indicate which thread it is and use the mutex, the default settings are alright, but we do need to do one other modification. By default, the HAL driver will use the Systick as its primary time base, but this timer should be left to the AzureRTOS only. We can simply select a different time base for the HAL by clicking in the System Core/SYS and selecting the time base Source as TIM6:
As the USB and Ethernet are not used in this demo, we can remove it by unchecking the boxes at the Project Manager tab/ advanced settings:
All set, we can press Alt+K to generate the code and Ctrl+S to save it. The project now has a new set of folders, this is the detail of its structure:
On the second part of the article, we'll see the code needed for the demo and how to debug it.
Adding mutex to a ThreadX application consists of: