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

}

?

Posted on March 30, 2017 at 08:47

It's only doing the first thread and stop forever ??

Posted on March 30, 2017 at 11:18

Is my init correct ?

===========

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 */

}

Posted on March 30, 2017 at 11:21

My declaration, please correct me ?

========

/* Variables

Posted on March 30, 2017 at 14:00

It stopped on Line 237 below, I have no idea why yet ??

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 */

printf('Start Default Task! \n');

test_fatfs();

reading_file_test();

printf('Line 237 <<== \n');

/* Suspend Thread 1 */

osThreadSuspend(NULL);

osDelay(1000);

/* Resume Thread 2*/

osThreadResume(secondTaskHandle);

/*

for(;;)

{

osDelay(1);

}

*/

/* USER CODE END StartDefaultTask */

}

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

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

}

}

Posted on March 31, 2017 at 00:25

I'm not asking you to do programming, that's my puzzle, I only ask how

to, please don't misunderstand

Posted on March 31, 2017 at 01:17

Here's the flowchart, roughly :

________________

Attachments :

lmahgjcoaocpffbh.jpg : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HyjB&d=%2Fa%2F0X0000000bBa%2FsJ2y0ya9XfSKucRQKXpeB46pmqL4gqMfBBW1OzzCCOE&asPdf=false
Posted on March 31, 2017 at 03:19

with this statement :

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

does 'secondTask' on standby already and ready to be called ? or ?