FreeRTOS – Task Creation

This article is a continuation of the  Series on FreeRTOS and carries the discussion on FreeRTOS and its usage. The aim of this series is to provide easy and practical examples that anyone can understand. Today we are going to see FreeRTOS LPC2148 Tutorial – Task Creation.

Suggestion to read

FreeRTOS LPC2148 Tutorial – Task Creation

Introduction

FreeRTOS contains many APIs. But the very basic is Creating a Task. Because tasks are concurrently running when the system boots up. So today we will look at simple task creation.

API Used

  • xTaskCreate
  • vTaskDelay
  • vTaskStartScheduler

xTaskCreate

This FreeRTOS API is used to create a task. Using this API we can create more tasks.

portBASE_TYPE xTaskCreate  (      pdTASK_CODE pvTaskCode,

                           const signed portCHAR * const pcName,

                           unsigned portSHORT usStackDepth,

                           void *pvParameters,

                           unsigned portBASE_TYPE uxPriority,

                           xTaskHandle *pxCreatedTask );

  • pvTaskCode: a pointer to the function where the task is implemented. (Address of the function)
  • pcName: given name to the task. This is useless to FreeRTOS but is intended for debugging purposes only.
  • usStackDepth: length of the stack for this task in words. The actual size of the stack depends on the microcontroller.
  • pvParameters: a pointer to arguments given to the task.
  • uxPriority: priority given to the task, a number between 0 and MAX_PRIORITIES – 1.
  • pxCreatedTask: a pointer to an identifier that allows handling the task. If the task does not have to be handled in the future, this can be left NULL.

vTaskDelay

            We are using this API for delay purposes.

void vTaskDelay( TickType_t xTicksToDelay );

  • xTicksToDelay : The number of tick interrupts that the calling task will remain in the Blocked state before being transitioned back into the Ready state. For example, if a task called vTaskDelay( 100 ) when the tick count was 10,000, then it would immediately enter the Blocked state and remain in the Blocked state until the tick count reached 10,100. Any time that remains between vTaskDelay() being called, and the next tick interrupt occurring, counts as one complete tick period. Therefore, the highest time resolution that can be achieved when specifying a delay period is, in the worst case, equal to one complete tick interrupt period. The macro pdMS_TO_TICKS() can be used to convert milliseconds into ticks.

vTaskStartScheduler

Starts the FreeRTOS scheduler running.
Typically, before the scheduler has been started, main() (or a function called by main()) will be executing. After the scheduler has been started, only tasks and interrupts will ever execute.
Starting the scheduler causes the highest priority task that was created while the scheduler
was in the Initialization state to enter the Running state.

void vTaskStartScheduler( void );

Code

In this code, I’m creating two tasks.

  • Task 1
  • Task 2

Task 1 prints “Task1 functioning” in the serial port. Task 2 prints “Task2 functioning” in the serial port. Let’s go through the code. Here I’m adding only the main file. If you want to download the full project, please visit here (GitHub).

#include <lpc214x.h>
#include <stdlib.h>
#include "FreeRTOS.h"
#include "task.h"
#include "uart0.h"

void task1(void *q);
void task2(void *a);
void initpll(void);

int main(void)
{
	initpll();
	initserial();
	xTaskCreate(task1,"task1",128,NULL,1,NULL);
	xTaskCreate(task2,"task2",128,NULL,2,NULL);
	vTaskStartScheduler();
}
void task1(void *q)
{	
	while(1) {	
		sendsserial("Task1 functioning");
		sendsserial("\r\n");
		vTaskDelay(1000);
	}
}

void task2(void *a)
{
	while(1) {
		sendsserial("Task2 functioning");
		sendsserial("\r\n");
		vTaskDelay(1000);
	}
}

void initpll(void)
{
	PLL0CON=0x01;
	PLL0CFG=0x24;
	PLL0FEED=0xAA;
	PLL0FEED=0x55;
	while(!(PLL0STAT&1<<10));
	PLL0CON=0x03;
	PLL0FEED=0xAA;
	PLL0FEED=0x55;
	VPBDIV=0x01;
}

Output

[Download This Project]

In our next tutorial, we will see how to create a task with parameters.

You can also read the below tutorials.

Linux Device Driver TutorialsC Programming Tutorials
FreeRTOS TutorialsNuttX RTOS Tutorials
RTX RTOS TutorialsInterrupts Basics
I2C Protocol – Part 1 (Basics)I2C Protocol – Part 2 (Advanced Topics)
STM32 TutorialsLPC2148 (ARM7) Tutorials
PIC16F877A Tutorials8051 Tutorials
Unit Testing in C TutorialsESP32-IDF Tutorials
Raspberry Pi TutorialsEmbedded Interview Topics
Reset Sequence in ARM Cortex-M4BLE Basics
VIC and NVIC in ARMSPI – Serial Peripheral Interface Protocol
STM32F7 Bootloader TutorialsRaspberry PI Pico Tutorials
STM32F103 Bootloader TutorialsRT-Thread RTOS Tutorials
Zephyr RTOS Tutorials - STM32Zephyr RTOS Tutorials - ESP32
AUTOSAR TutorialsUDS Protocol Tutorials
Product ReviewsSTM32 MikroC Bootloader Tutorial
VHDL Tutorials
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Table of Contents