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
valentin
Senior
Posted on March 29, 2017 at 07:57

A suspended thread can't start another one

Posted on March 29, 2017 at 10:38

So do you know what the solution is ?

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
Lead
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

Posted on March 30, 2017 at 00:32

What I think, osThreadSuspend = stop the thread,

osThreadResume = continune the thread,

Correct me ?

or I need a delay ?

Posted on March 30, 2017 at 01:33

Proably I need delay between thread ?

for(;;)

{

osDelay(1);

}

in between resume and suspend at thread1 and 2 ?

________________

Attachments :

minkgmcokjmmplkd.jpg : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HySB&d=%2Fa%2F0X0000000bBn%2FrX8DpJ86lhOl6sXbOPPO5USvFz7JmYOM67a6isTATrA&asPdf=false
Posted on March 30, 2017 at 01:41

From the example of STM32CubeMX, I saw some delays between thread, are

they what I missed ?

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

/**

  • @brief Toggle LED1 thread

  • @param thread not used

  • @retval None

*/

static void LED_Thread1(void const *argument)

{

uint32_t count = 0;

(void) argument;

for (;;)

{

count = osKernelSysTick() + 5000;

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

while (count >= osKernelSysTick())

{

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())

{

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

{

count = osKernelSysTick() + 10000;

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

while (count >= osKernelSysTick())

{

BSP_LED_Toggle(LED2);

osDelay(500);

}

/* Turn off LED2 */

BSP_LED_Off(LED2);

/* Resume Thread 1 */

osThreadResume(LEDThread1Handle);

/* Suspend Thread 2 */

osThreadSuspend(NULL);

}

}

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

valentin
Senior
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.

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.