-
Notifications
You must be signed in to change notification settings - Fork 0
/
os.h
214 lines (184 loc) · 8.56 KB
/
os.h
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
#include <stdint.h>
// filename **********OS.H***********
// Real Time Operating System for Labs 2 and 3
// Jonathan W. Valvano 2/20/17, valvano@mail.utexas.edu
// EE445M/EE380L.6
// You may use, edit, run or distribute this file
// You are free to change the syntax/organization of this file
// You are required to implement the spirit of this OS
#ifndef _OS_H_
#define _OS_H_
// NVIC Defines
#define NVIC_ST_CTRL_R (*((volatile uint32_t *)0xE000E010))
#define NVIC_ST_CTRL_CLK_SRC 0x00000004 // Clock Source
#define NVIC_ST_CTRL_INTEN 0x00000002 // Interrupt enable
#define NVIC_ST_CTRL_ENABLE 0x00000001 // Counter mode
#define NVIC_ST_RELOAD_R (*((volatile uint32_t *)0xE000E014))
#define NVIC_ST_CURRENT_R (*((volatile uint32_t *)0xE000E018))
#define NVIC_INT_CTRL_R (*((volatile uint32_t *)0xE000ED04))
#define NVIC_INT_CTRL_PENDSTSET 0x04000000 // Set pending SysTick interrupt
#define NVIC_SYS_PRI3_R (*((volatile uint32_t *)0xE000ED20)) // Sys. Handlers 12 to 15 Priority
#define NVIC_EN0_INT21 0x00200000 // Interrupt 21 enable
#define NVIC_EN1_INT35 0x00000008
#define NVIC_EN2_INT70 0x00000040
#define TIMER_CFG_32_BIT_TIMER 0x00000000 // 32-bit timer configuration
#define TIMER_TAMR_TACDIR 0x00000010 // GPTM Timer A Count Direction
#define TIMER_TAMR_TAMR_PERIOD 0x00000002 // Periodic Timer mode
#define TIMER_CTL_TAEN 0x00000001 // GPTM TimerA Enable
#define TIMER_IMR_TATOIM 0x00000001 // GPTM TimerA Time-Out Interrupt
// Mask
#define TIMER_ICR_TATOCINT 0x00000001 // GPTM TimerA Time-Out Raw
// Interrupt
#define TIMER_TAILR_M 0xFFFFFFFF // GPTM Timer A Interval Load
// Register
#define GPIO_PORTF2 (*((volatile uint32_t *)0x40025010))
// edit these depending on your clock
#define TIME_1MS 80000
#define TIME_2MS (2*TIME_1MS)
#define TIME_500US (TIME_1MS/2)
#define TIME_250US (TIME_1MS/5)
// feel free to change the type of semaphore, there are lots of good solutions
struct Sema4{
long Value; // >0 means free, otherwise means busy
// add other components here, if necessary to implement blocking
};
typedef struct Sema4 Sema4Type;
// ******** OS_Init ************
// initialize operating system, disable interrupts until OS_Launch
// initialize OS controlled I/O: serial, ADC, systick, LaunchPad I/O and timers
// input: none
// output: none
void OS_Init(void);
//******** OS_Launch ***************
// start the scheduler, enable interrupts
// Inputs: number of 12.5ns clock cycles for each time slice
// you may select the units of this parameter
// Outputs: none (does not return)
// It is ok to limit the range of theTimeSlice to match the 24-bit SysTick
void OS_Launch(unsigned long theTimeSlice);
//******** OS_AddThread ***************
// add a foregound thread to the scheduler
// Inputs: pointer to a void/void foreground task
// number of bytes allocated for its stack
// priority, 0 is highest, 5 is the lowest
// Outputs: 1 if successful, 0 if this thread can not be added
// stack size must be divisable by 8 (aligned to double word boundary)
int OS_AddThread(void(*task)(void),
unsigned long stackSize, unsigned long priority);
//******** OS_Id ***************
// returns the thread ID for the currently running thread
// Inputs: none
// Outputs: Thread ID, number greater than zero
unsigned long OS_Id(void);
//******** OS_AddPeriodicThread ***************
// add a background periodic task
// typically this function receives the highest priority
// Inputs: pointer to a void/void background function
// period given in system time units (12.5ns)
// priority 0 is the highest, 5 is the lowest
// Outputs: 1 if successful, 0 if this thread can not be added
// You are free to select the time resolution for this function
// It is assumed that the user task will run to completion and return
// This task can not spin, block, loop, sleep, or kill
// This task can call OS_Signal OS_bSignal OS_AddThread
// This task does not have a Thread ID
int OS_AddPeriodicThread(void(*task)(void),
unsigned long period, unsigned long priority);
// ******** OS_InitSemaphore ************
// initialize semaphore
// input: pointer to a semaphore
// output: none
void OS_InitSemaphore(Sema4Type *semaPt, long value);
// ******** OS_Wait ************
// decrement semaphore
// input: pointer to a counting semaphore
// output: none
void OS_Wait(Sema4Type *semaPt);
// ******** OS_Signal ************
// increment semaphore
// input: pointer to a counting semaphore
// output: none
void OS_Signal(Sema4Type *semaPt);
// ******** OS_bWait ************
// input: pointer to a binary semaphore
// output: none
void OS_bWait(Sema4Type *semaPt);
// ******** OS_bSignal ************
// input: pointer to a binary semaphore
// output: none
void OS_bSignal(Sema4Type *semaPt);
// ******** OS_Sleep ************
// place this thread into a dormant state
// input: number of msec to sleep
// output: none
// You are free to select the time resolution for this function
// OS_Sleep(0) implements cooperative multitasking
void OS_Sleep(unsigned long sleepTime);
// ******** OS_Kill ************
// kill the currently running thread, release its TCB and stack
// input: none
// output: none
void OS_Kill(void);
// ******** OS_Suspend ************
// suspend execution of currently running thread
// scheduler will choose another thread to execute
// Can be used to implement cooperative multitasking
// Same function as OS_Sleep(0)
// input: none
// output: none
void OS_Suspend(void);
// ******** OS_Time ************
// return the system time
// Inputs: none
// Outputs: time in 12.5ns units, 0 to 4294967295
// The time resolution should be less than or equal to 1us, and the precision 32 bits
// It is ok to change the resolution and precision of this function as long as
// this function and OS_TimeDifference have the same resolution and precision
unsigned long OS_Time(void);
// ******** OS_TimeDifference ************
// Calculates difference between two times
// Inputs: two times measured with OS_Time
// Outputs: time difference in 12.5ns units
// The time resolution should be less than or equal to 1us, and the precision at least 12 bits
// It is ok to change the resolution and precision of this function as long as
// this function and OS_Time have the same resolution and precision
unsigned long OS_TimeDifference(unsigned long start, unsigned long stop);
// ******** OS_ClearMsTime ************
// sets the system time to zero (from Lab 1)
// Inputs: none
// Outputs: none
// You are free to change how this works
void OS_ClearMsTime(void);
// ******** OS_MsTime ************
// reads the current time in msec (from Lab 1)
// Inputs: none
// Outputs: time in ms units
// You are free to select the time resolution for this function
// It is ok to make the resolution to match the first call to OS_AddPeriodicThread
unsigned long OS_MsTime(void);
//******** OS_AddSW1Task ***************
// add a background task to run whenever the BUTTON1 (PD6) button is pushed
// Inputs: pointer to a void/void background function
// priority 0 is the highest, 5 is the lowest
// Outputs: 1 if successful, 0 if this thread can not be added
// It is assumed that the user task will run to completion and return
// This task can not spin, block, loop, sleep, or kill
// This task can call OS_Signal OS_bSignal OS_AddThread
// This task does not have a Thread ID
int OS_AddSW1Task(void(*task)(void), unsigned long priority);
//******** OS_AddSW2Task ***************
// add a background task to run whenever the BUTTON2 (PD7) button is pushed
// Inputs: pointer to a void/void background function
// priority 0 is highest, 5 is lowest
// Outputs: 1 if successful, 0 if this thread can not be added
// It is assumed user task will run to completion and return
// This task can not spin block loop sleep or kill
// This task can call issue OS_Signal, it can call OS_AddThread
// This task does not have a Thread ID
int OS_AddSW2Task(void(*task)(void), unsigned long priority);
void Scheduler(void);
void InitTimer1A(unsigned long period, uint32_t priority);
void InitTimer2A(unsigned long period);
void InitTimer3A(void);
void InitTimer4A(uint32_t period, uint32_t priority);
#endif