This repository has been archived by the owner on Aug 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathavr_sched.c
175 lines (150 loc) · 3.68 KB
/
avr_sched.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* avr_sched.c
* -->Premptive Multitasking Scheduler
*
* Created: 27-06-2016 09:29:09 PM
* Author : Akash Kollipara
*/
#define F_CPU 16000000UL
#define maxTask 3
#define StackPointer SP
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
void timer_init();
void tasks_init();
//tasks
void blink1();
void blink2();
void blink3();
uint8_t R=0, currentTaskId=0, nextTaskId=0;
/*
* -->to define the task's state these variables are used
* -->t_pause and t_quit will be used when priority is concerned, also when dynamic allocation is required
*/
typedef enum status{t_ready, t_busy, t_wait, t_quit} status;
typedef struct TaskControlBlock //defines and controls a task for this scheduler
{
uint8_t task_id;
enum status state;
uint8_t priority;
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
tasks_init(); //initializes tasks
//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;
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
}
}
}
void blink1()
{
DDRB|= (1<< PORTB4);
while(1)
{
PORTB |= (1<<PORTB4);
_delay_ms(50);
PORTB &= ~(1<<PORTB4);
_delay_ms(50);
}
}
void blink2()
{
DDRB|= (1<<PORTB3);
while(1)
{
PORTB |= (1<<PORTB3);
_delay_ms(100);
PORTB &= ~(1<<PORTB3);
_delay_ms(100);
}
}
void blink3()
{
DDRB |= (1<<PORTB2);
while(1)
{
PORTB |= (1<<PORTB2);
_delay_ms(300);
PORTB &= ~(1<<PORTB2);
_delay_ms(300);
}
}
void timer_init()
{
TCCR0A=(1<<WGM01); //enable CTC-mode
TCCR0B=(1<<CS02); //pre-scalar to /256
TIMSK0=(1<<OCIE0A); //timer0A interrupt enable
/*
* pre-scalar is set to /256
* 1.024ms for each task execution per submission
* can be increased to 32.64ms by assigning OCR0A=254 (MAX) for prescalar of 1024
*/
OCR0A =31;
}
void tasks_init()
{
//assigning task_id
task[0].task_id=0;
task[1].task_id=1;
task[2].task_id=2;
//setting up PC for tasks
task[0].fnctpt=&blink1;
task[1].fnctpt=&blink2;
task[2].fnctpt=&blink3;
//setting up stack
task[0].stack_pointer_begin=RAMEND-100;
task[1].stack_pointer_begin=RAMEND-140;
task[2].stack_pointer_begin=RAMEND-180;
//assigning priority
task[0].priority=0;
task[1].priority=0;
task[2].priority=0;
//setting up states
task[0].state=t_ready;
task[1].state=t_ready;
task[2].state=t_ready;
}