How to use the mutex RTOS .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-08-29 10:23 PM
Hi,
I want to synchronize the my threads execution using the mutex.
here is my code
/*----------------------------------------------------------------------------
* CMSIS-RTOS 'main' function template
*---------------------------------------------------------------------------*/
#include "RTE_Components.h"
#include CMSIS_device_header
#include "cmsis_os2.h"
#include "stm32f2xx_hal.h"
#include "stm32f2xx_hal_gpio.h"
#include "main.h"
//osMutexId_t mutex_id;
/*----------------------------------------------------------------------------
* Application main thread
*---------------------------------------------------------------------------*/
__NO_RETURN void thread1 (void *argument) {
(void)argument;
uint32_t ii=0;
osMutexId_t mutex_id1;
__HAL_RCC_GPIOF_CLK_ENABLE(); // enabling the peripheral clock
// gpio structure initialization
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Mode= GPIO_MODE_OUTPUT_PP ;
GPIO_InitStructure.Pin= GPIO_PIN_0;
GPIO_InitStructure.Pull= GPIO_PULLUP;
GPIO_InitStructure.Speed= GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOF, &GPIO_InitStructure);
mutex_id1 = osMutexNew (NULL);
while(1)
{
for(ii=0;ii<100;ii++)
{
osMutexAcquire (mutex_id1, 100);
HAL_GPIO_TogglePin(GPIOF, GPIO_PIN_0);
osDelay(100);
osMutexRelease (mutex_id1);
}
ii=0;
}
}
/*----------------------------------------------------------------------------
* Application main thread
*---------------------------------------------------------------------------*/
__NO_RETURN void thread2 (void *argument) {
(void)argument;
osMutexId_t mutex_id2;
uint32_t jj=0;
__HAL_RCC_GPIOF_CLK_ENABLE(); // enabling the peripheral clock
// gpio structure initialization
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Mode= GPIO_MODE_OUTPUT_PP ;
GPIO_InitStructure.Pin= GPIO_PIN_0;
GPIO_InitStructure.Pull= GPIO_PULLUP;
GPIO_InitStructure.Speed= GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOF, &GPIO_InitStructure);
mutex_id2 = osMutexNew (NULL);
while(1)
{
for(jj=0;jj<100;jj++)
{
osMutexAcquire (mutex_id2,100);
HAL_GPIO_TogglePin(GPIOF, GPIO_PIN_0);
osDelay(1000);
osMutexRelease (mutex_id2);
}
jj=0;
}
}
/*----------------------------------------------------------------------------
* Application main thread
*---------------------------------------------------------------------------*/
__NO_RETURN void thread3 (void *argument) {
(void)argument;
osMutexId_t mutex_id3;
uint32_t kk=0;
__HAL_RCC_GPIOF_CLK_ENABLE(); // enabling the peripheral clock
// gpio structure initialization
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Mode= GPIO_MODE_OUTPUT_PP ;
GPIO_InitStructure.Pin= GPIO_PIN_0;
GPIO_InitStructure.Pull= GPIO_PULLUP;
GPIO_InitStructure.Speed= GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOF, &GPIO_InitStructure);
mutex_id3 = osMutexNew (NULL);
while(1)
{
for(kk=0;kk<100;kk++)
{
osMutexAcquire (mutex_id3, 100);
HAL_GPIO_TogglePin(GPIOF, GPIO_PIN_0);
osDelay(500);
osMutexRelease (mutex_id3);
}
kk=0;
}
}
/*----------------------------------------------------------------------------
* Application main thread
*---------------------------------------------------------------------------*/
__NO_RETURN static void app_main (void *argument) {
(void)argument;
osThreadNew(thread1, NULL, NULL);
osThreadNew(thread2, NULL, NULL);
osThreadNew(thread3, NULL, NULL);
while(1);
}
int main (void) {
// System Initialization
SystemCoreClockUpdate();
// ...
osKernelInitialize(); // Initialize CMSIS-RTOS
osThreadNew(app_main, NULL, NULL); // Create application main thread
osKernelStart(); // Start thread execution
while(1);
}
The output shows threads are not synchronized.
Here I am aware of linux threads(mutex) concepts.
same thing I am implemented in RTOS with little changes .
an any one help to solve this problem.
Thanks.
Solved! Go to Solution.
- Labels:
-
STM32F2 Series
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-08-29 11:24 PM
Create one mutex per ressource and not one per thread, i.e. make the mutex a global variable used in all 3 threads. See also https://www.keil.com/pack/doc/CMSIS/RTOS2/html/group__CMSIS__RTOS__MutexMgmt.html. Your code should also examine the return value of osMutexAcquire
, because it may fail.
hth
KnarfB
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-08-29 11:24 PM
Create one mutex per ressource and not one per thread, i.e. make the mutex a global variable used in all 3 threads. See also https://www.keil.com/pack/doc/CMSIS/RTOS2/html/group__CMSIS__RTOS__MutexMgmt.html. Your code should also examine the return value of osMutexAcquire
, because it may fail.
hth
KnarfB
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-08-30 12:12 AM
creating one mutex object also done but the result is same as before one.
please tell me,how to create one mutex object .
Do you have any reference code please share with me.
Thanks
Trinu.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-08-30 07:13 AM
How does your code look like, how did you test it? What did you expect and what did you got?
KnarfB