-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkernel_functions.h
More file actions
140 lines (106 loc) · 4.24 KB
/
kernel_functions.h
File metadata and controls
140 lines (106 loc) · 4.24 KB
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
#ifndef KERNEL_FUNCTIONS_H
#define KERNEL_FUNCTIONS_H
/*********************************************************/
/** Global variables and definitions */
/** Modified on 27th Feb 2019 */
/*********************************************************/
#include <stdlib.h> /* for using the functions calloc, free */
#include <string.h> /* for using the function memcpy */
#include <limits.h> /* for using the constant UINT_MAX */
#define CONTEXT_SIZE 8 /* for the 8 registers: r4 to r11 */
#define STACK_SIZE 100 /* about enough space for the stack */
#define TRUE 1
#define FALSE !TRUE
#define RUNNING 1
#define INIT !RUNNING
#define FAIL 0
#define SUCCESS 1
#define OK 1
#define DEADLINE_REACHED 0
#define NOT_EMPTY 0
#define SENDER (+1)
#define RECEIVER (-1)
typedef int exception;
typedef int bool;
typedef unsigned int uint;
typedef int action;
struct l_obj; // Forward declaration
// Task Control Block, TCB. Modified on 24/02/2019
typedef struct {
uint *SP;
uint R4toR11[CONTEXT_SIZE];
void (*PC)();
uint SPSR;
uint StackSeg[STACK_SIZE];
uint Deadline;
} TCB;
// Message items
typedef struct msgobj {
char *pData;
exception Status;
struct l_obj *pBlock;
struct msgobj *pPrevious;
struct msgobj *pNext;
} msg;
// Mailbox structure
typedef struct {
msg *pHead;
msg *pTail;
int nDataSize;
int nMaxMessages;
int nMessages;
int nBlockedMsg;
} mailbox;
// Generic list item
typedef struct l_obj {
TCB *pTask;
uint nTCnt;
msg *pMessage;
struct l_obj *pPrevious;
struct l_obj *pNext;
} Node;
// Generic list
typedef struct _list {
Node *pHead;
Node *pTail;
} List;
extern List *ReadyList, *WaitingList, *TimerList;
extern TCB *PreviousTask, *NextTask; /* Pointers to previous and next running tasks */
extern int Ticks;
// Function prototypes
void Idle_Task(void);
// Task administration
exception init_kernel(void);
exception create_task( void (* task_body)(), uint deadline);
void terminate( void );
void run( void );
// Communication
mailbox* create_mailbox( uint nMessages, uint nDataSize );
exception remove_mailbox (mailbox* mBox);
int no_messages( mailbox* mBox );
exception send_wait( mailbox* mBox, void* pData );
exception receive_wait( mailbox* mBox, void* pData );
exception send_no_wait( mailbox* mBox, void* pData );
int receive_no_wait( mailbox* mBox, void* pData );
// Timing
exception wait( uint nTicks );
void set_ticks( uint nTicks );
uint ticks( void );
uint deadline( void );
void set_deadline( uint deadline );
//Interrupt and context switch
extern void isr_off(void);
extern void isr_on(void);
extern void SwitchContext( void );
/* Stores stack frame in stack of currently running task, and the
* remaining registers in its TCB
* Loads stack frame from stack of RunningTask, and the
* remaining registers from its TCB
*/
extern void LoadContext_In_Run( void );
/* To be used on the last line of the C function run() */
extern void switch_to_stack_of_next_task( void );
/* To be used inside the C function terminate() */
extern void LoadContext_In_Terminate( void );
/* To be used on the last line of the C function terminate() */
#endif