-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMbedOS_FE.c
167 lines (156 loc) · 5.04 KB
/
MbedOS_FE.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
#include "RTOS_BE.h"
#include "cmsis_os2.h"
#include "rtx_lib.h"
#include "assert.h"
#define MAX_NUMBER_OF_TASKS 16
#define ARM_REGS_NUM 4
typedef struct{
int valid ;
void *thread ;
uint32_t* regs[ARM_REGS_NUM];
}Thread_Info;
static Thread_Info Mbed_Threads[MAX_NUMBER_OF_TASKS];
static int last_index = 0;
void Port_Init_Regs(void *task_id){
Mbed_Threads[last_index].thread = task_id;
last_index++;
}
void PortSetRegs(int index, uint32_t* param){
int i;
for(i=0;i<MAX_NUMBER_OF_TASKS;i++){
if(Mbed_Threads[i].thread == osRtxInfo.thread.run.curr){
Mbed_Threads[i].regs[index] = param;
break;
}
}
}
uint32_t** PortGetRegs(void* param){
int i;
os_thread_t* thread = (os_thread_t*)param;
for(i=0;i<MAX_NUMBER_OF_TASKS;i++){
if(Mbed_Threads[i].thread == thread){
return Mbed_Threads[i].regs;
}
}
assert(1==0);
}
void FE_Thread_Exit(){
int i;
for(i=0;i<MAX_NUMBER_OF_TASKS;i++){
if(Mbed_Threads[i].thread == osRtxInfo.thread.run.curr){
Mbed_Threads[i].thread = NULL;
break;
}
}
osThreadExit();
}
void MbedOS_Context_Switch(int *cur_idx, int *next_idx){
void *current = NULL, *next = NULL;
os_thread_t *thread;
osRtxInfo.kernel.tick++;
if(osRtxInfo.timer.tick!=NULL){
osRtxInfo.timer.tick();
}
osRtxThreadDelayTick();
current = osRtxThreadGetRunning();
assert(current != NULL);
*cur_idx = PMCU_Find_Idx(current);
osRtxThreadDispatch(NULL);
if (osRtxInfo.thread.robin.timeout != 0U) {
if (osRtxInfo.thread.robin.thread != osRtxInfo.thread.run.next) {
// Reset Round Robin
osRtxInfo.thread.robin.thread = osRtxInfo.thread.run.next;
osRtxInfo.thread.robin.tick = osRtxInfo.thread.robin.timeout;
} else {
if (osRtxInfo.thread.robin.tick != 0U) {
osRtxInfo.thread.robin.tick--;
}
if (osRtxInfo.thread.robin.tick == 0U) {
// Round Robin Timeout
if (osRtxKernelGetState() == osRtxKernelRunning) {
thread = osRtxInfo.thread.ready.thread_list;
if ((thread != NULL) && (thread->priority == osRtxInfo.thread.robin.thread->priority)) {
osRtxThreadListRemove(thread);
osRtxThreadReadyPut(osRtxInfo.thread.robin.thread);
EvrRtxThreadPreempted(osRtxInfo.thread.robin.thread);
osRtxThreadSwitch(thread);;
osRtxInfo.thread.robin.thread = thread;
osRtxInfo.thread.robin.tick = osRtxInfo.thread.robin.timeout;
}
}
}
}
}
next = osRtxInfo.thread.run.next;
*next_idx = PMCU_Find_Idx(next);
}
void TickSignalHandler(int sig){
(void)sig;
int cur_idx = -1, next_idx = -1;
if(PMCU_IsInterruptEnabled() == 1 && PMCU_IsServicingTick() != 1 && PMCU_GetSVC() == 0){
if(PMCU_SingleTryLock() == 0){
PMCU_EnterServicingTick();
MbedOS_Context_Switch(&cur_idx, &next_idx);
if(cur_idx != next_idx){
osRtxThreadSetRunning(osRtxInfo.thread.run.next);
PMCU_Schedule(cur_idx, next_idx);
}else{
PMCU_SingleUnlock();
}
PMCU_ExitServicingTick();
}else{
PMCU_SetPending();
}
}else{
PMCU_SetPending();
}
}
void MbedOS_FE_svcRtxThreadNew(void *task_id, osThreadFunc_t func, void *argument){
PMCU_CreateNewThread(task_id, func, argument, TickSignalHandler);
PMCU_Add_Task_ID(task_id);
Port_Init_Regs(task_id);
}
void StartScheduler(void){
void *FirstThread;
FirstThread = osRtxInfo.thread.run.next;
osRtxInfo.thread.run.curr = osRtxInfo.thread.run.next;
PMCU_StartScheduler(FirstThread, 1000000/osRtxConfig.tick_freq);
}
void PMCU_FE_Yield(){
int cur_idx = -1, next_idx = -1;
int i = 0;
MbedOS_Context_Switch(&cur_idx, &next_idx);
if(cur_idx != next_idx){
osRtxInfo.thread.run.curr = osRtxInfo.thread.run.next;
PMCU_Schedule(cur_idx, next_idx);
}
}
void port_svc_call_enter(void){
PMCU_SetSVC();
}
void port_svc_call_exit(uint32_t* ret){
int i = 0;
int cur_idx = -1, next_idx = -1;
if(PMCU_GetPending() == 1){
if(PMCU_IsInterruptEnabled() == 1){
MbedOS_Context_Switch(&cur_idx, &next_idx);
PMCU_ClearPending();
}
}
if(osRtxInfo.thread.run.curr!=osRtxInfo.thread.run.next){
if(ret != NULL){
for(i=0;i<MAX_NUMBER_OF_TASKS;i++){
if(Mbed_Threads[i].thread == osRtxInfo.thread.run.curr){
Mbed_Threads[i].regs[0] = ret;
break;
}
}
}
cur_idx = PMCU_Find_Idx(osRtxInfo.thread.run.curr);
next_idx = PMCU_Find_Idx(osRtxInfo.thread.run.next);
osRtxInfo.thread.run.curr = osRtxInfo.thread.run.next;
PMCU_Schedule(cur_idx, next_idx);
}else{
PMCU_ClearSVC();
}
}