Project tutorial
Preemptive Multitasking Scheduler for AVR

Preemptive Multitasking Scheduler for AVR

Multitasking on AVR

  • 1,190 views
  • 0 comments
  • 2 respects

Components and supplies

About this project

By Akash Kollipara December 19, 2016

Hey there!

Today we shall discuss about the scheduler, in this case its for AVR.

During this summer vacation I was working on a project which needed multitasking to serve UI program. So I had to design a preemptive multitasking scheduler. If you ask why preemptive, I chose preemptive because there were tasks which would never terminate (infinite loop functions).

I assume you know what multitasking is, now lets see what is scheduler and how it works...

Scheduler:

It is a program that schedules the execution of the tasks. When there are at least 3 or more tasks and there is a need for multitasking a scheduler needs to be implemented. It is usually written as ISR.

Consider 3 tasks T1, T2, T3 namely. Assuming all tasks are of equal priority, initially the scheduler allows mcu to execute T1, after a certain or specified amount of time is elapsed the timer interrupts the execution and switches over to the ISR, before the scheduler takes over the context is saved to the specified location in the stack of RAM. Now the scheduler updates the program counter of mcu with the memory location of T2 and the task T2 gets executed and same goes for T3. As the context is being saved in stack, when all tasks are executed at least one time the scheduler sets the status of task to 'running'. Now to execute T1 again the stack pointer of mcu is updated with the location of context of T1 in the stack, then context restore routine runs. When T1 runs again the T1 assumes that it is not interrupted as the registers are updated with the same values as before and T1 runs. 

The above example is also called as round-robin method of task execution. 

I have provided link below to view the partial code of scheduler I have written. 

Here is the video in which the blinking of there LED's are different tasks.

For further details visit the project website

Code

Scheduler Source FileC/C++
/*
 * main.c
 *
 * Created: 27-06-2016 09:29:09 PM
 * Author : Akash Kollipara
 */ 

uint8_t R=0, currentTaskId=0, nextTaskId=0;

typedef enum status{t_ready, t_busy} status;

typedef struct TaskControlBlock		//defines and controls a task for this scheduler
{
	uint8_t task_id;
	enum status state;
	unsigned int stack_pointer_begin;
	unsigned int stack_pointer_end;
	void (*fnctpt)(void);
} tcb;
tcb task[maxTask];

int main(void)
{
    	cli();
	//initialization 
	timer_init();				//initializes timer for scheduler
	Neutron_kernel_init();		//initializes the kernel
	
	//setting up first task parameters
	StackPointer=task[0].stack_pointer_begin;
	currentTaskId=task[0].task_id;
	if(task[0].state==t_ready)
		task[0].state=t_busy;
	sei();
	//running first task
	task[0].fnctpt();
	
    	while (1);
}

ISR(TIMER0_COMPA_vect)
{
	uint8_t q=0;
	PORTB^=(1<<PORTB5);
	cli();
	//getting the termination pointer of individual stacks
	task[currentTaskId].stack_pointer_end=StackPointer;
	for(q=0; q<maxTask; q++)
	{
		if(task[q].state==t_ready)
		{
			//setting up task parameters 
			task[q].state=t_busy;
			currentTaskId=task[q].task_id;		//sets task_id for saving context in corresponding task's stack
			StackPointer=task[q].stack_pointer_begin;
			sei();
			task[q].fnctpt();		//execute task if not priorly executed
		}
		R=q;
	}
	if(R==maxTask-1)	//checks if all the tasks are in queue
	{
		if(nextTaskId==maxTask) nextTaskId=0;		//resets the the task_id if it exceeds maxTask
		if(task[nextTaskId].state==t_busy)
		{
			//pointing stack to start context restoring
			StackPointer=task[nextTaskId].stack_pointer_end;
			currentTaskId=nextTaskId;				//when next interrupt occurs, this var is used as the task_id for getting stack end pointer
			nextTaskId=task[nextTaskId].task_id+1;	//increments the task_id for next call of scheduler
		}
	}
}

Comments

Similar projects you might like

Arduino-Based Automatic Water Tap Using IR Sensor

Project tutorial by Creatjet3D R&D Team

  • 5,649 views
  • 6 comments
  • 19 respects

Hacking Qualcomm (Quick Charge) QC 2.0/3.0 With ATtiny85

Project tutorial by Shahariar

  • 5,896 views
  • 11 comments
  • 40 respects

Smart Garbage Monitoring System Using Arduino 101

Project tutorial by Technovation

  • 21,393 views
  • 7 comments
  • 32 respects

CNC drawing machine with joysticks

Project showcase by TechnoFabrique

  • 2,145 views
  • 0 comments
  • 5 respects

Multitasking FreeRTOS for Arduino

Project showcase by Du Phạm

  • 1,514 views
  • 0 comments
  • 2 respects

Protected Switching Power Supply for Development Boards

Project showcase by Akash Kollipara

  • 1,313 views
  • 0 comments
  • 6 respects
Add projectSign up / Login