Skip to main content
antonius
Associate III
March 29, 2017
Question

Switching between thread on RTOS ?

  • March 29, 2017
  • 6 replies
  • 5290 views
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
This topic has been closed for replies.

6 replies

valentin
Associate III
March 29, 2017
Posted on March 29, 2017 at 07:57

A suspended thread can't start another one

antonius
antoniusAuthor
Associate III
March 29, 2017
Posted on March 29, 2017 at 10:38

So do you know what the solution is ?

valentin
Associate III
March 29, 2017
Posted on March 29, 2017 at 21:09

Yes, I do.

Have a look at your code here:

/* Suspend Thread 1 */

  osThreadSuspend(NULL);

/* Resume Thread 2*/

  osThreadResume(secondTaskHandle);

And think about what's going on there

john doe
Senior III
March 29, 2017
Posted on March 30, 2017 at 01:43

i think he's telling you to suspend the first thread AFTER resuming the second because if you tell the current thread to halt, it can't tell the second thread to resume.

but i could be wrong

antonius
antoniusAuthor
Associate III
March 30, 2017
Posted on March 30, 2017 at 04:16

We need a delay between thread ? wait until the first thread finish and

continue to the other one 2000 ms ?

It's roughly to flip between thread.

I want to know how it works.

valentin
Associate III
March 30, 2017
Posted on March 30, 2017 at 06:08

Okay so what I meant is this:

Assuming the program starts running thread1 only, it executes all the commands until it reaches

osThreadSuspend(NULL);

-> this line stops the current thread (itself) and puts it into suspended mode (halted).

So the next line osThreadResume(secondTaskHandle); is not executed any more because neither thread 1 nor thread 2 are active at the time, halting the complete program.

If you swap the two lines it should work fine -> first resume the secondTask and THEN stop the first one.

antonius
antoniusAuthor
Associate III
March 30, 2017
Posted on March 30, 2017 at 08:41

So it will be :

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

/* 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();

  • /* Resume Thread 2/*

    • osThreadResume(secondTaskHandle);**

    • /* Suspend Thread 1 /*

    • osThreadSuspend(NULL);**

***

/*

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);*

}

?

valentin
Associate III
March 30, 2017
Posted on March 30, 2017 at 21:02

your task init looks alright.

this is getting a bit confusing now though and I can't really do the programming for you.

All I can say is that osThreadSuspend(NULL); halts the current thread (the one with that line in) until it gets resumed from another.

It seems to me you always manage to end up with all threads suspended, hence nothing happening any more. Your 2nd task for example is only run once, doesn't have any loop.

How about you draw a flow chart for your program and have a good look at it. then you should figure out where it stops and why.

antonius
antoniusAuthor
Associate III
March 31, 2017
Posted on March 31, 2017 at 00:23

yes it's confusing now, suppose to be simple, only I don't know yet

how to do it ? May be you know and someone else here does ?

I tried to run the second thread after the first thread finish, and from

the second thread back to the first one, and do it endlessly, flip flop

from the first to the second.

So every thread has to have an endless loop ? and will be suspended

by osThreadSuspend(NULL); , how can I start it ( what's the opposite

command API for it )?

Ok, I'll draw it and post, thanks

This is what I can learn from the STM3210CEval example, do I need those

endless loops ?

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

static void LED_Thread1(void const *argument)

{

uint32_t count = 0;

(void) argument;

  • for (;;) <=== do I need this forever loop on each thread ?*

{

count = osKernelSysTick() + 5000;

/* Toggle LED1 every 200 ms for 5 s */

  • while (count >= osKernelSysTick()) <==internal LED timing loop*

{

BSP_LED_Toggle(LED1);

osDelay(200);

}

/* Turn off LED1 */

BSP_LED_Off(LED1);

/* Suspend Thread 1 */

osThreadSuspend(NULL);

count = osKernelSysTick() + 5000;

/* Toggle LED1 every 400 ms for 5 s */

while (count >= osKernelSysTick()) <==internal LED timing loop

{

BSP_LED_Toggle(LED1);

osDelay(400);

}

/* Resume Thread 2*/

osThreadResume(LEDThread2Handle);

}

}

/**

  • @brief Toggle LED2 thread

  • @param argument not used

  • @retval None

*/

static void LED_Thread2(void const *argument)

{

uint32_t count;

(void) argument;

for (;;) <=== do I need this forever loop on each thread ?

{

count = osKernelSysTick() + 10000;

/* Toggle LED2 every 500 ms for 10 s */

while (count >= osKernelSysTick()) <==internal LED timing loop

{

BSP_LED_Toggle(LED2);

osDelay(500);

}

/* Turn off LED2 */

BSP_LED_Off(LED2);

/* Resume Thread 1 */

osThreadResume(LEDThread1Handle);

/* Suspend Thread 2 */

osThreadSuspend(NULL);

}

}

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

Hey there!

Is your code stacked at the printf??    

Vishnuvardhan
Visitor II
November 4, 2018

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