-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.c
113 lines (100 loc) · 2.64 KB
/
user.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
#include "riscv.h"
#include "os.h"
#include "clint.h"
#include "lock.h"
#include "timer.h"
#include "uart.h"
#include "syscall.h"
#define DELAY_TIME 10 // ToDo: accurate delay.
#define USE_LOCK
spin_lock_t lock = { .locked = 0 };
static void user_task0(void)
{
uart_puts("Task0: Created!\n");
/* test for compatible task_yield() */
task_yield();
uart_puts("Task0: I am back!\n");
while (1) {
#ifdef USE_LOCK
acquire_spin_lock(&lock);
// acquire_spin_lock();
uart_puts("Task0 acquired the spin-lock\n");
#endif
for (int i = 7; i>0; i--) {
uart_puts("Task0: Running...\n");
task_delay(DELAY_TIME/10);
}
#ifdef USE_LOCK
uart_puts("Task0 released the spin-lock\n");
release_spin_lock(&lock);
// release_spin_lock();
task_delay(DELAY_TIME);
#endif
}
}
static void timer_handler(void *args)
{
printf("Software timer handler handler!\n");
}
static void user_task1(void)
{
uart_puts("Task1: Created!\n");
timer_create(timer_handler, NULL, 5);
uart_puts("Task1 Timer Created!\n");
while (1) {
#ifdef USE_LOCK
acquire_spin_lock(&lock);
// acquire_spin_lock();
uart_puts("Task1 acquired the spin-lock\n");
#endif
for (int i = 7; i>0; i--) {
uart_puts("Task1: Running...\n");
task_delay(DELAY_TIME/10);
}
#ifdef USE_LOCK
uart_puts("Task1 released the spin-lock\n");
release_spin_lock(&lock);
// release_spin_lock();
task_delay(DELAY_TIME);
#endif
}
}
static void user_task2(void)
{
uart_puts("Task2: Created!\n");
timer_create(timer_handler, NULL, 8);
uart_puts("Task2 Timer Created!\n");
while (1) {
#ifdef USE_LOCK
acquire_spin_lock(&lock);
// acquire_spin_lock();
uart_puts("Task2 acquired the spin-lock\n");
#endif
// something wrong here!!!
uint32_t hid = -1;
uint32_t ret = -1;
printf("ptr of hid = 0x%x\n", &hid);
ret = gethid(&hid);
printf("hid = %d\n", hid);
// printf("ret = %d\n", ret);
for (int i = 7; i>0; i--) {
uart_puts("Task2: Running...\n");
task_delay(DELAY_TIME/10);
}
#ifdef USE_LOCK
uart_puts("Task2 released the spin-lock\n");
release_spin_lock(&lock);
// release_spin_lock();
task_delay(DELAY_TIME);
#endif
// trap_test();
// task_yield();
}
}
void os_main(void)
{
task_create(user_task0);
task_create(user_task1);
task_create(user_task2);
// trap_test(); /* comment this to avoid raising exception. */
}