m
 
Line 1: Line 1:
[[category: RTOS Tutorials]]
+
#REDIRECT[[4. Understanding the task switching depending upon priorities]]
[[User:Amruta|Amruta]] ([[User talk:Amruta|talk]]) 13:43, 8 April 2015 (IST)
+
----
+
 
+
==Intro==
+
 
+
In last tutorial we saw the effect of vTaskDelay() function.
+
 
+
Now we will see how kernel does task switching depending upon the priorities.
+
 
+
If you are not familiar with what is task switching, check this.
+
 
+
==Code==
+
<syntaxhighlight>
+
/* Scheduler include files. */
+
#include "FreeRtOSConfig.h"
+
#include "FreeRTOS.h"
+
#include "task.h"
+
#include "croutine.h"
+
#include "uart.h" /* Explore Embedded UART library */
+
 
+
xTaskHandle TaskHandle_1;
+
xTaskHandle TaskHandle_2;
+
xTaskHandle TaskHandle_3;
+
xTaskHandle TaskHandle_4;
+
xTaskHandle TaskHandle_5;
+
 
+
/* Local Tasks declaration */
+
static void MyTask1(void* pvParameters);
+
static void MyTask2(void* pvParameters);
+
static void MyTask3(void* pvParameters);
+
static void MyTask4(void* pvParameters);
+
static void MyTask5(void* pvParameters);
+
static void MyIdleTask(void* pvParameters);
+
 
+
#define LED_IdleTask 0x01u
+
#define LED_Task1    0x02u
+
#define LED_Task2    0x04u
+
#define LED_Task3    0x08u
+
#define LED_Task4    0x10u
+
#define LED_Task5    0x20u
+
 
+
#define LED_PORT LPC_GPIO2->FIOPIN
+
 
+
int main(void)
+
+
SystemInit(); /* Initialize the microcontroller system */
+
LPC_GPIO2->FIODIR = 0xffffffffu;
+
UART_Init(38400); /* Initialize the Uart module */
+
 
+
/* Create the three tasks with priorities 1,2,3 */
+
xTaskCreate( MyTask1, ( signed char * )"Task1", configMINIMAL_STACK_SIZE, NULL, 1, &TaskHandle_1);
+
xTaskCreate( MyTask3, ( signed char * )"Task3", configMINIMAL_STACK_SIZE, NULL, 3, &TaskHandle_3 );
+
 
+
xTaskCreate( MyIdleTask, ( signed char * )"IdleTask", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
+
 
+
UART_Printf("\n\rIn main function, invoking scheduler");
+
+
vTaskStartScheduler(); /* Start the schedular */
+
 
+
while(1);
+
}
+
 
+
 
+
static void MyTask1(void* pvParameters)
+
{
+
LED_PORT = LED_Task1;   /* Led to indicate the execution of Task1*/
+
UART_Printf("\n\rIn Task1");
+
 
+
vTaskDelete(TaskHandle_1);
+
}
+
 
+
static void MyTask2(void* pvParameters)
+
{
+
LED_PORT = LED_Task2;   /* Led to indicate the execution of Task2*/
+
UART_Printf("\n\rIn Task2 ");
+
 
+
vTaskDelete(TaskHandle_2);
+
}
+
 
+
static void MyTask3(void* pvParameters)
+
{
+
 
+
LED_PORT = LED_Task3;   /* Led to indicate the execution of Task3*/
+
UART_Printf("\n\rTask3, creating new tasks 2");
+
 
+
    /* Create two new tasks 2, 4 */
+
xTaskCreate( MyTask2, ( signed char * )"Task2", configMINIMAL_STACK_SIZE, NULL, 2, &TaskHandle_2);
+
UART_Printf("\n\rTask3, creating new tasks 4");
+
 
+
xTaskCreate( MyTask4, ( signed char * )"Task4", configMINIMAL_STACK_SIZE, NULL, 4, &TaskHandle_4);
+
 
+
LED_PORT = LED_Task3;   /* Led to indicate the execution of Task3*/
+
UART_Printf("\n\rBack in Task3, Creating Task5");
+
 
+
xTaskCreate( MyTask5, ( signed char * )"Task5", configMINIMAL_STACK_SIZE, NULL, 5, &TaskHandle_5);
+
 
+
LED_PORT = LED_Task3;   /* Led to indicate the execution of Task3*/
+
UART_Printf("\n\rBack in Task3, Exiting task3");
+
 
+
vTaskDelete(TaskHandle_3);
+
 
+
}
+
 
+
static void MyTask4(void* pvParameters)
+
{
+
LED_PORT = LED_Task4;   /* Led to indicate the execution of Task4*/
+
UART_Printf("\n\rIn Task4");
+
 
+
vTaskDelete(TaskHandle_4);
+
}
+
 
+
static void MyTask5(void* pvParameters)
+
{
+
LED_PORT = LED_Task5;   /* Led to indicate the execution of Task4*/
+
UART_Printf("\n\rIn Task5");
+
 
+
vTaskDelete(TaskHandle_5);
+
}
+
 
+
static void MyIdleTask(void* pvParameters)
+
{
+
while(1)
+
{
+
    LED_PORT = LED_IdleTask; /* Led to indicate the execution of Idle Task*/
+
    UART_Printf("\n\rIn idle state");
+
}
+
}
+
</syntaxhighlight>
+
 
+
{{DISQUS}}
+

Latest revision as of 13:57, 8 April 2015