-
Notifications
You must be signed in to change notification settings - Fork 0
/
App2.txt
374 lines (278 loc) · 7.92 KB
/
App2.txt
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "stm32f4xx.h"
#include "system_stm32f4xx.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#define MAX_SIZE 49
static volatile uint32_t msTicks100 = 0;
struct Task {
int prior;
int delay;
void (*fncName)(void);
};
struct Queue {
int currSize;
int maxSize;
struct Task task[50];
};
static volatile uint8_t stopFlag = 0;
void SysTick_Handler(void);
void USART2_IRQHandler(void);
void EXTI0_IRQHandler(void);
static void sendUART(uint8_t * data, uint32_t length);
static uint8_t receiveUART(void);
//Tasks
void FirstTask(void);
void TaskA(void);
void TaskB(void);
void TaskC(void);
//TaskScheduler functions
void Init(void);
void QueTask(void (*task)(void));
void Dispatch(void);
void ReRunMe(int delay);
//queue functions
void insertRQueue(struct Task);
//globals
static struct Queue readyQueue;
static struct Queue delayQueue;
static struct Task currTask;
static int count;
static uint8_t msg1[] = "Scheduler is now running!\n\n";
static uint8_t msgC[] = "Current count: ";
void Init(){
//init the 2 queues
readyQueue.currSize = 0;
readyQueue.maxSize = MAX_SIZE;
delayQueue.currSize = 0;
delayQueue.maxSize = MAX_SIZE;
count = 0;
}
void SysTick_Handler(void) {
msTicks100++;
//check the delayQueue to put anything in the readyQueue
//if the queue is not empty
if (delayQueue.currSize != 0) {
//start checking the delayQueue
for (int j = delayQueue.currSize; j >= 0; j--) {
delayQueue.task[j].delay--;
//if the delay is now 0, it is now ready for the readyQueue and to
//be removed from the delayQueue
if (delayQueue.task[j].delay == 0) {
insertRQueue(delayQueue.task[j]);
delayQueue.currSize--;
}
}
}
}
void USART2_IRQHandler(void) {
/* pause/resume UART messages */
stopFlag = !stopFlag;
/* dummy read */
(void)receiveUART();
}
void EXTI0_IRQHandler(void) {
/* Clear interrupt request */
EXTI->PR |= 0x01;
}
static void sendUART(uint8_t * data, uint32_t length)
{
for (uint32_t i=0; i<length; ++i){
// add new data without messing up DR register
uint32_t value = (USART2->DR & 0x00) | data[i];
// send data
USART2->DR = value;
// busy wait for transmit complete
while(!(USART2->SR & (1 << 6)));
// delay
for(uint32_t j=0; j<1000; ++j);
}
}
static uint8_t receiveUART()
{
// extract data
uint8_t data = USART2->DR & 0xFF;
return data;
}
static void gpioInit()
{
// enable GPIOA clock, bit 0 on AHB1ENR
RCC->AHB1ENR |= (1 << 0);
// set pin modes as alternate mode 7 (pins 2 and 3)
// USART2 TX and RX pins are PA2 and PA3 respectively
GPIOA->MODER &= ~(0xFU << 4); // Reset bits 4:5 for PA2 and 6:7 for PA3
GPIOA->MODER |= (0xAU << 4); // Set bits 4:5 for PA2 and 6:7 for PA3 to alternate mode (10)
// set pin modes as high speed
GPIOA->OSPEEDR |= 0x000000A0; // Set pin 2/3 to high speed mode (0b10)
// choose AF7 for USART2 in Alternate Function registers
GPIOA->AFR[0] |= (0x7 << 8); // for pin A2
GPIOA->AFR[0] |= (0x7 << 12); // for pin A3
}
static void uartInit()
{
// enable USART2 clock, bit 17 on APB1ENR
RCC->APB1ENR |= (1 << 17);
// USART2 TX enable, TE bit 3
USART2->CR1 |= (1 << 3);
// USART2 rx enable, RE bit 2
USART2->CR1 |= (1 << 2);
// USART2 rx interrupt, RXNEIE bit 5
USART2->CR1 |= (1 << 5);
// baud rate = fCK / (8 * (2 - OVER8) * USARTDIV)
// for fCK = 16 Mhz, baud = 115200, OVER8 = 0
// USARTDIV = 16Mhz / 115200 / 16 = 8.6805
// Fraction : 16*0.6805 = 11 (multiply fraction with 16)
// Mantissa : 8
// 12-bit mantissa and 4-bit fraction
USART2->BRR |= (8 << 4);
USART2->BRR |= 11;
// enable usart2 - UE, bit 13
USART2->CR1 |= (1 << 13);
}
void FirstTask() {
sendUART(msg1, sizeof(msg1));
}
void TaskA() {
count += rand() % 3 + 1;
ReRunMe(1);
}
void TaskB() {
count -= rand() % 3 + 1;
ReRunMe(1);
}
void TaskC() {
sendUART(msgC, sizeof(msgC));
char sign;
char add;
if (count < 0) {
sign = '-';
int tempCount = count * -1;
tempCount = tempCount % 10;
add = tempCount + '0';
}
else {
sign = '+';
count = count % 10;
add = count + '0';
}
char temp[3] = {sign, add, '\n'};
sendUART((uint8_t*)temp, sizeof(temp));
ReRunMe(60);
}
void QueTask(void (*task)(void)) {
//to support 8 modes of priority
int priority = rand() % 8;
//giving taskC highest priority to be able to run when ReRunMe(0) for A and B are called
if (task == *TaskC)
priority = 7;
struct Task newTask;
newTask.delay = 0;
newTask.prior = priority;
newTask.fncName = task;
insertRQueue(newTask);
}
void insertRQueue(struct Task T) {
//if the queue is full, just return
if (readyQueue.currSize == readyQueue.maxSize) {
static uint8_t msgQFull[] = "Queue is currently full, please wait for tasks to finish!\n";
sendUART( (uint8_t*) msgQFull, sizeof(msgQFull) );
return;
}
for (int i = 0; i < readyQueue.currSize; i++) {
//if the current tasks's priority is more than what we're currently
//pointing to, we want to place it in that position
if (T.prior > readyQueue.task[i].prior) {
//start shifting everything to the right to make space
for (int j = readyQueue.currSize + 1; j > i; j--)
readyQueue.task[j] = readyQueue.task[j - 1];
readyQueue.task[i] = T;
readyQueue.currSize = readyQueue.currSize + 1;
return;
}
}
//if the priority is smaller than everything else, place it at the end.
readyQueue.task[readyQueue.currSize] = T;
readyQueue.currSize = readyQueue.currSize + 1;
}
void ReRunMe(int delay) {
struct Task rerunTask;
rerunTask.delay = delay;
rerunTask.prior = currTask.prior;
rerunTask.fncName = currTask.fncName;
//instantly put into readyQueue
if (delay == 0)
insertRQueue(rerunTask);
//negative number, then do nothing
else if (delay < 0)
return;
//needs to be put into the delayQueue
else {
//if the queue is full, do nothing
if (delayQueue.currSize == delayQueue.maxSize)
return;
for (int i = 0; i <= delayQueue.currSize; i++) {
//if the current tasks's priority is more than what we're currently
//pointing to, we want to place it in that position
if (rerunTask.delay > delayQueue.task[i].delay) {
//start shifting everything to the right to make space
for (int j = delayQueue.currSize + 1; j > i; j--)
delayQueue.task[j] = delayQueue.task[j - 1];
delayQueue.task[i] = rerunTask;
delayQueue.currSize = delayQueue.currSize + 1;
return;
}
}
//if the delay is smaller than everything else, place it at the end.
delayQueue.currSize = delayQueue.currSize + 1;
delayQueue.task[delayQueue.currSize] = rerunTask;
}
}
void Dispatch() {
//if the queue is not empty
if (readyQueue.currSize != 0) {
void (*runTask)(void) = readyQueue.task[0].fncName;
currTask = readyQueue.task[0];
(*runTask)();
//start shifting everything to the left
for (int j = 0; j < readyQueue.currSize; j++)
readyQueue.task[j] = readyQueue.task[j + 1];
readyQueue.currSize = readyQueue.currSize - 1;
}
}
int main()
{
/* startup code initialization */
SystemInit();
SystemCoreClockUpdate();
/* intialize UART */
gpioInit();
/* intialize UART */
uartInit();
/* enable SysTick timer to interrupt system every 100ms */
//it's not exactly 100ms, but the professor mentioned that it makes
//sense since this is a simulation and not real hardware
SysTick_Config(SystemCoreClock/3);
/* enable interrupt controller for USART2 external interrupt */
NVIC_EnableIRQ(USART2_IRQn);
/* Unmask External interrupt 0 */
EXTI->IMR |= 0x0001;
/* Enable rising and falling edge triggering for External interrupt 0 */
EXTI->RTSR |= 0x0001;
EXTI->FTSR |= 0x0001;
/* enable interrupt controller for External interrupt 0 */
NVIC_EnableIRQ(EXTI0_IRQn);
//initialize all
Init();
//queueing start schedule string
QueTask(FirstTask);
Dispatch();
//queueing the tasks
QueTask(TaskA);
QueTask(TaskB);
QueTask(TaskC);
while(1)
{
Dispatch();
}
}