-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
242 lines (185 loc) · 5.2 KB
/
main.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
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
#include "main.h"
//semihosting init function
extern void initialise_monitor_handles(void);
// initialize global timers
global_timers_t global_timers = {
.timer_flags.timer = {RDY},
.timers = {0}
};
int main(void)
{
initialise_monitor_handles();
printf("Implementation of a task scheduler\n");
DISABLE_WB();
init_schedulaer_stack(SCHED_STACK_START);
init_tasks_stack();
sw_hw_init();
pwmSetDutyCycle(&pwmA, (float)50.0);
pwmSetDutyCycle(&pwmB, (float)50.0);
switch_sp_to_psp();
// start scheduler context switching
sysFlags.startScheduler = true;
task1_handler();
for(;;);
}
void task1_handler(void)
{
// retrieve raw data from imu
while(1){
led_on(&yellow_led);
//get_raw_measurements(&i2c1, false);
lock_task(IMU_RETRIEVE_RAW_DATA);
//process_raw_measurements(&mpu6050_data);
led_off(&yellow_led);
task_delay(DELAY_COUNT_1MS*5U, (uint8_t)UNLOCKED);
}
}
void task2_handler(void)
{
while(1){
requestRadioControlData(&spiHandle1);
led_on(&blue_led);
task_delay(DELAY_COUNT_500MS, (uint8_t)UNLOCKED);
led_off(&blue_led);
task_delay(DELAY_COUNT_500MS, (uint8_t)UNLOCKED);
}
}
void task3_handler(void)
{
while(1){
led_on(&red_led);
task_delay(DELAY_COUNT_250MS, (uint8_t)UNLOCKED);
led_off(&red_led);
task_delay(DELAY_COUNT_250MS, (uint8_t)UNLOCKED);
}
}
void task4_handler(void)
{
while(1){
checkEncoderStateChange(&rEncoder);
if(rEncoder.stateChanged){
}
}
}
void task5_handler(void)
{
while(1){
for(int i = 0; i < MAX_TASKS - 2; i++){
taskStackTraceDepth(&user_tasks[i]);
task_delay(DELAY_COUNT_1S, (uint8_t)UNLOCKED);
}
}
}
void task6_handler(void)
{
while(1){
__asm("nop");
}
}
__attribute__((naked)) void PendSV_Handler(void){
// when a function is given the attribute "naked" the user must use BX instruction to return back
__asm("nop");
/* Save the context of the running task
* 1. Get current running task's PSP value
* 2. Using that PSP value store stack frame 2 (R4-R11)
* 3. Save the current value of the PSP
*/
__asm volatile ("PUSH {LR}");
__asm volatile ("MRS R0, PSP");
__asm volatile ("STMDB R0!, {R4-R11}");
__asm volatile ("BL save_psp_value");
/* Retrieve the contect of the current task
* 1. Decide next task to run
* 2. get its past PSP value
* 3. Using that PSP value retrieve it's SF2 (R4-R11)
* 4. Update PSP and exit
*/
__asm volatile ("BL update_next_task");
__asm volatile ("BL get_psp_value");
__asm volatile ("LDMIA R0!, {R4-R11}");
__asm volatile ("MSR PSP, R0");
__asm volatile ("POP {LR}");
__asm volatile ("BX LR");
}
void SysTick_Handler(void){
// manage global timers
for(int i = 0; i < sizeof(global_timers.timers)/sizeof(global_timers.timers[0]); i++){
if (global_timers.timers[i] > 0){
global_timers.timers[i]--;
global_timers.timer_flags.timer[i] = IN_PROGRESS;
}
else if(global_timers.timers[i] == 0 && global_timers.timer_flags.timer[i] == IN_PROGRESS){
global_timers.timer_flags.timer[i] = EXPIRED;
}
}
// update global tick count
update_global_tick_count();
// only run schedular if flag is set
if(sysFlags.startScheduler == true){
// unblock tasks that can run
unblock_tasks();
// pend the pendsv exception
*(cortexPperiphs.pICSR) |= ( 1 << 28 );
}
}
__attribute__((naked)) void MemManage_Handler(void){
// capture the stack pointer value by using a naked function, this function removes epilogue/prologue which manipulate the stack pointer value
__asm ("MRS R0, MSP");
__asm ("B MemManage_Handler_c");
}
void MemManage_Handler_c(uint32_t *pMSP){
// read the MEM MANAGE FAULT STATUS REGISTER
uint32_t *pMMSR = (uint32_t*)0xE000ED28;
for(int i =0; i < 8; i++){
stack_frame.sf[i] = pMSP[i];
}
// read the MEM MANAGE FAULT ADDRESS REGISTER
uint32_t *pMMAR = (uint32_t*)0xE000ED34;
__asm("nop");;
}
__attribute__((naked)) void BusFault_Handler(void){
// capture the stack pointer value by using a naked function, this function removes epilogue/prologue which manipulate the stack pointer value
__asm ("MRS R0, MSP");
__asm ("B BusFault_Handler_c");
}
void BusFault_Handler_c(uint32_t *pMSP){
// read the BUS FAULT STATUS REGISTER
uint32_t pBFSR = *((uint32_t*)0xE000ED29);
for(int i =0; i < 8; i++){
stack_frame.sf[i] = pMSP[i];
}
// read the BUS FAULT ADDRESS REGISTER
uint32_t pBFAR = *((uint32_t*)0xE000ED38);
__asm("nop");
}
__attribute__((naked)) void UsageFault_Handler(void){
// capture the stack pointer value by using a naked function, this function removes epilogue/prologue which manipulate the stack pointer value
__asm ("MRS R0, MSP");
__asm ("B UsageFault_Handler_c");
}
void UsageFault_Handler_c(uint32_t *pMSP){
// read the USAGE FAULT STATUS REGISTER
uint32_t *pUFSR = (uint32_t*)0xE00ED2A;
for(int i =0; i < 8; i++){
stack_frame.sf[i] = pMSP[i];
}
__asm("nop");
}
__attribute__((naked)) void HardFault_Handler(void){
// capture the stack pointer value by using a naked function, this function removes epilogue/prologue which manipulate the stack pointer value
__asm ("MRS R0, MSP");
__asm ("B HardFault_Handler_c");
}
void HardFault_Handler_c(uint32_t *pMSP){
for(int i =0; i < 8; i++){
stack_frame.sf[i] = pMSP[i];
}
__asm("nop");
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}