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 03:20

I fail to see the need for two threads given the flow chart provided.  With that said, do you know how to use a mutex and osThreadYield?  The other key piece is that both threads need to have the same priority.

Edit: spelling

Posted on March 31, 2017 at 03:28

I don't know on how to use Mutex,

The core is, I want to learn to use FreeRTOS from the scratch,

and I don't know how to use osThreadYield.

Is it possible if we don't have to use Mutex ? or it's a must ? I want

to learn from the simplest step first.

How can I make those two threads have the same priority ?

I follow the example from STM3210C Eval RTOS project,

it seems that I missed something from it, and it's stopped working only

at the first thread and never goes into the 2nd thread.

Could it be, I missed something inside configuration ?

my current attempt :

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

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

test_fatfs(); //task thread_2 1

reading_file_test(); //task thread_2 2

  • printf('Line 242 <<== \n'); //<=======stops here and

stops forever*

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

reading_file_test(); //task on thread 2

/* Resume Thread 1 */

osThreadResume(defaultTaskHandle);

osDelay(1000);

/* Suspend Thread 2 */

osThreadSuspend(NULL);

}//thread2 endless loop end

}

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

Posted on March 31, 2017 at 04:08

Is it possible if we don't have to use Mutex ? or it's a must ? I want

to learn from the simplest step first.

Yes, to make this work reliably, you need to understand how to use some sort of proper thread synchronization primitives.

I don't know that this is the right place to try to learn multi-threaded programming.  Take a look at mbed.org.  The same CMSIS-OS facilities are available through those tools.  They are quite a bit easier to use, and it is more geared towards teaching these sort of concepts by providing a simpler coding model and lots of example code.  I use mbed.org to mock up new code all the time -- especially when I'm playing with new hardware.

Until you master these skills, writing a working embedded RTOS application that uses USB is going to be difficult.

Posted on March 31, 2017 at 04:29

Thanks for your suggestion for mbed.org.

I'm not going to use USB yet, I will learn on RTOS first, is there a

community for mbed.org ? a place to learn or it's only from the book and

example provided?

I can see the example from STM3210C-EVAL, that's the simplest I can

understand, blink the LED, I want to enter from that starting point (

two threads )

at this point I don't understand yet for 'thread syncronization', on my

mind, turn on one thread and turn off the other one and loop it..

What's API for 'thread RUNNING', 'thread READY','thread WAITING','thread

INACTIVE',

I got osThreadSuspend(NULL); ==> stop the thread

osThreadResume(LEDThread2Handle); ==> start the thread

So I call thread 2 after thread 1 is finished, and thread 2 is doing the

job, ==>finish it's calling thread 1 and I want them in endless loop,

it sounds like I'm doing on while(){} but inside FreeRTOS..

________________

Attachments :

gpdglcdfbdgonfff.jpg : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hyj6&d=%2Fa%2F0X0000000bBb%2Fh4utBHWVRGEYZFgdIvO5smrdgzw0kSme83gYI.zyPb0&asPdf=false
Posted on March 31, 2017 at 04:53

I've seen Mbed.org, but I'm not yet in that level, I want to learn

something simple from STM32CubeMX and FreeRTOS, and not making it more

complicated.

Thanks for that.

Posted on March 31, 2017 at 05:02

Start here: 

https://developer.mbed.org/forum/

 

It is a great place to learn this stuff.

Posted on March 31, 2017 at 05:09

Using mbed.org for development is a lot simpler than using STM32CubeMX.  Orders of magnitude simpler.  STM32CubeMX exposes a lot of complexity to the developer that is hidden from you in mbed.org.

mbed.org, especially with its online compiler, is designed to be a learning tool, much like the Arduino development environment.

Posted on March 31, 2017 at 05:13

Really, I have no idea about that, I'm in the middle of STM32CubeMX now,

it is another task for me switching from STM32CubeMX to Mbed, I reckon.

Posted on March 31, 2017 at 05:22

I think, I'll follow : http://www.freertos.org/, there are some books

there...

Thanks for mbed.org suggestions anyway..

Posted on March 31, 2017 at 06:51

I learn from FreeRTOS book, the basic of task, but I don't know if I

have missing configuration on my STM32CubeMX, any advice ? thanks

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

/3.2 Task Functions//

//Tasks are implemented as C functions. The only thing special about

them is their prototype,//

//which must return void and take a void pointer parameter. The

prototype is demonstrated by//

//Listing 11.//

//void ATaskFunction( void *pvParameters );//

//Listing 11. The task function prototype//

//Each task is a small program in its own right. It has an entry point,

will normally run forever//

//within an infinite loop, and will not exit. The structure of a typical

task is shown in Listing 12.//

//FreeRTOS tasks must not be allowed to return from their implementing

function in any way�?//

//they must not contain a ‘return’ statement and must not be allowed to

execute past the end of//

//the function. If a task is no longer required, it should instead be

explicitly deleted. This is also//

//demonstrated in Listing 12.//

//A single task function definition can be used to create any number of

tasks�?each created task//

//being a separate execution instance, with its own stack and its own

copy of any automatic//

//(stack) variables defined within the task itself.//

//void ATaskFunction( void *pvParameters )//

//{//

///* Variables can be declared just as per a normal function. Each

instance of a task//

//created using this example function will have its own copy of the

lVariableExample//

//variable. This would not be true if the variable was declared static –

in which case//

//only one copy of the variable would exist, and this copy would be

shared by each//

//created instance of the task. (The prefixes added to variable names

are described in//

//section 1.5, Data Types and Coding Style Guide.) *///

//int32_t lVariableExample = 0;//

///* A task will normally be implemented as an infinite loop. *///

//for( ;; )//

//{//

///* The code to implement the task functionality will go here. *///

//}//

///* Should the task implementation ever break out of the above loop,

then the task//

//must be deleted before reaching the end of its implementing function.

The NULL//

//parameter passed to the vTaskDelete() API function indicates that the

task to be//

//deleted is the calling (this) task. The convention used to name API

functions is//

//described in section 1.5, Data Types and Coding Style Guide. ///

/*/vTaskDelete( NULL );//====> osThreadSuspend(NULL); in*STM32CubeMX

API*

//}

=========

/