Preemptive Multitasking Scheduler for AVR
Multitasking on AVR
Components and supplies
1
Arduino UNO
1
LED (generic)
Project description
Code
Scheduler Source File
c_cpp
Comments
Only logged in users can leave comments
Components and supplies
Arduino UNO
LED (generic)
Project description
Code
Scheduler Source File
c_cpp
1/* 2 * main.c 3 * 4 * Created: 27-06-2016 09:29:09 PM 5 * Author : Akash Kollipara 6 */ 7 8uint8_t R=0, currentTaskId=0, nextTaskId=0; 9 10typedef enum status{t_ready, t_busy} status; 11 12typedef struct TaskControlBlock //defines and controls a task for this scheduler 13{ 14 uint8_t task_id; 15 enum status state; 16 unsigned int stack_pointer_begin; 17 unsigned int stack_pointer_end; 18 void (*fnctpt)(void); 19} tcb; 20tcb task[maxTask]; 21 22int main(void) 23{ 24 cli(); 25 //initialization 26 timer_init(); //initializes timer for scheduler 27 Neutron_kernel_init(); //initializes the kernel 28 29 //setting up first task parameters 30 StackPointer=task[0].stack_pointer_begin; 31 currentTaskId=task[0].task_id; 32 if(task[0].state==t_ready) 33 task[0].state=t_busy; 34 sei(); 35 //running first task 36 task[0].fnctpt(); 37 38 while (1); 39} 40 41ISR(TIMER0_COMPA_vect) 42{ 43 uint8_t q=0; 44 PORTB^=(1<<PORTB5); 45 cli(); 46 //getting the termination pointer of individual stacks 47 task[currentTaskId].stack_pointer_end=StackPointer; 48 for(q=0; q<maxTask; q++) 49 { 50 if(task[q].state==t_ready) 51 { 52 //setting up task parameters 53 task[q].state=t_busy; 54 currentTaskId=task[q].task_id; //sets task_id for saving context in corresponding task's stack 55 StackPointer=task[q].stack_pointer_begin; 56 sei(); 57 task[q].fnctpt(); //execute task if not priorly executed 58 } 59 R=q; 60 } 61 if(R==maxTask-1) //checks if all the tasks are in queue 62 { 63 if(nextTaskId==maxTask) nextTaskId=0; //resets the the task_id if it exceeds maxTask 64 if(task[nextTaskId].state==t_busy) 65 { 66 //pointing stack to start context restoring 67 StackPointer=task[nextTaskId].stack_pointer_end; 68 currentTaskId=nextTaskId; //when next interrupt occurs, this var is used as the task_id for getting stack end pointer 69 nextTaskId=task[nextTaskId].task_id+1; //increments the task_id for next call of scheduler 70 } 71 } 72} 73
Comments
Only logged in users can leave comments