cancel
Showing results for 
Search instead for 
Did you mean: 

Switching between thread on RTOS ?

antonius
Senior
Posted on March 29, 2017 at 04:18

Everybody,

I want to switch between thread on RTOS, how can I do that ?

I've written down below, but it seems that it's staying on the first thread only,

Thanks

/* StartDefaultTask function */

void StartDefaultTask(void const * argument)

{

  /* init code for FATFS */

  MX_FATFS_Init();

  /* init code for USB_DEVICE */

  MX_USB_DEVICE_Init();

  /* USER CODE BEGIN StartDefaultTask */

  /* Infinite loop */

    test_fatfs();

    reading_file_test();

     /* Suspend Thread 1 */

    osThreadSuspend(NULL);

       /* Resume Thread 2*/

    osThreadResume(secondTaskHandle);

    

    /*

  for(;;)

  {

    osDelay(1);

  }

    */

  /* USER CODE END StartDefaultTask */

}

void StartSecondTask(void const * argument)

{

    reading_file_test();

     /* Resume Thread 1 */

    osThreadResume(defaultTaskHandle);

    /* Suspend Thread 2 */

    osThreadSuspend(NULL);

}

#switch #rtos #thread
35 REPLIES 35
Posted on March 31, 2017 at 07:47

 ,

 ,

I increse heap size :

♯ define configTOTAL_HEAP_SIZE ((size_t)8192)

It's running now but not flipping yet ?

Code :

==============

void MX_FREERTOS_Init(void) {

/* USER CODE BEGIN Init */

/* USER CODE END Init */

/* USER CODE BEGIN RTOS_MUTEX */

/* add mutexes, ... */

/* USER CODE END RTOS_MUTEX */

/* USER CODE BEGIN RTOS_SEMAPHORES */

/* add semaphores, ... */

/* USER CODE END RTOS_SEMAPHORES */

/* USER CODE BEGIN RTOS_TIMERS */

/* start timers, add new ones, ... */

/* USER CODE END RTOS_TIMERS */

/* Create the thread(s) */

/* definition and creation of defaultTask */

osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 512),

defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL),

/* USER CODE BEGIN RTOS_THREADS */

/* add threads, ... */

/* definition and creation of defaultTask */

osThreadDef(secondTask, StartSecondTask, osPriorityNormal, 0, 512),

secondTaskHandle = osThreadCreate(osThread(secondTask), NULL),

/* USER CODE END RTOS_THREADS */

/* USER CODE BEGIN RTOS_QUEUES */

/* add queues, ... */

/* USER CODE END RTOS_QUEUES */

}

/* StartDefaultTask function */

static void StartDefaultTask(void const * argument)

{

uint32_t count = 0,

(void) argument,

/* init code for FATFS */

MX_FATFS_Init(),

/* init code for USB_DEVICE */

MX_USB_DEVICE_Init(),

/* USER CODE BEGIN StartDefaultTask */

/* Infinite loop */

printf('Start Default Task! \n'),*

    • for (,,)**

    • {//thread1 endless loop begin**

    • printf('Task 1, DefaultTask running! \n'),*

//test_fatfs(), //task thread_2 1

//reading_file_test(), //task thread_2 2

printf('Line 242 <,<,== \n'),

/* Suspend Thread 1 */

osThreadSuspend(NULL),

osDelay(1000),

/* Resume Thread 2*/

osThreadResume(secondTaskHandle),

}//thread1 endless loop end

/*

for(,,)

{

osDelay(1),

}

*/

/* USER CODE END StartDefaultTask */

}

static void StartSecondTask(void const * argument)

{

printf('Start Second Task! \n'),*

    • for (,,)**

    • {//thread2 endless loop begin**

    • printf('Task 2, SecondTask running! \n'),*

//reading_file_test(), //task on thread 2

/* Resume Thread 1 */

osThreadResume(defaultTaskHandle),

osDelay(1000),

/* Suspend Thread 2 */

osThreadSuspend(NULL),

}//thread2 endless loop end

}

======

Output :

*Start Second Task! **

**Task 2, SecondTask running! **

**Task 1, DefaultTask running! **

**Line 242 <,<,== *

Posted on March 31, 2017 at 07:55

I found the answer of my puzzle :

I commented out one of :

/*//osThreadSuspend(NULL); ==> in this case, it's on the first task, */

now it's flipping from task one to task two

thanks a lot guys...:)

Posted on April 03, 2017 at 00:42

Where can I find the example for those in :

\FreeRTOS Example\FreeRTOSv9.0.0\FreeRTOS\Demo\Common\Full 

mutex example ?

thanks

Posted on April 03, 2017 at 00:44

Is it this one you're talking about to solve this puzzle ?

7.3 Mutexes (and Binary Semaphores)

Huzaifah Limbada
Associate II
Posted on July 07, 2018 at 09:31

Hey there!

Is your code stacked at the printf??    

Vishnuvardhan
Associate II

What RTOS is being used? Is it FreeRTOS or something else?