2017-03-28 07:18 PM
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 #thread2017-03-28 10:57 PM
A suspended thread can't start another one
2017-03-29 03:38 AM
So do you know what the solution is ?
2017-03-29 02:09 PM
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
2017-03-29 04:43 PM
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
2017-03-29 05:32 PM
What I think, osThreadSuspend = stop the thread,
osThreadResume = continune the thread,
Correct me ?
or I need a delay ?
2017-03-29 06:33 PM
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=false2017-03-29 06:41 PM
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);
}
}
=====================
2017-03-29 09:08 PM
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.
2017-03-29 09:16 PM
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.