1 |
138 |
rhoads |
/*--------------------------------------------------------------------
|
2 |
|
|
* TITLE: Plasma Real Time Operating System
|
3 |
|
|
* AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
|
4 |
|
|
* DATE CREATED: 12/17/05
|
5 |
|
|
* FILENAME: rtos.h
|
6 |
|
|
* PROJECT: Plasma CPU core
|
7 |
|
|
* COPYRIGHT: Software placed into the public domain by the author.
|
8 |
|
|
* Software 'as is' without warranty. Author liable for nothing.
|
9 |
|
|
* DESCRIPTION:
|
10 |
|
|
* Plasma Real Time Operating System
|
11 |
|
|
*--------------------------------------------------------------------*/
|
12 |
|
|
#ifndef __RTOS_H__
|
13 |
|
|
#define __RTOS_H__
|
14 |
|
|
|
15 |
175 |
rhoads |
// Symmetric Multi-Processing
|
16 |
|
|
#define OS_CPU_COUNT 1
|
17 |
|
|
|
18 |
|
|
// Standard C library calls
|
19 |
150 |
rhoads |
#define printf UartPrintf
|
20 |
|
|
//#define printf UartPrintfPoll
|
21 |
138 |
rhoads |
#define scanf UartScanf
|
22 |
150 |
rhoads |
#ifndef WIN32
|
23 |
175 |
rhoads |
#define malloc(S) OS_HeapMalloc(NULL, S)
|
24 |
|
|
#define free(S) OS_HeapFree(S)
|
25 |
150 |
rhoads |
#endif
|
26 |
138 |
rhoads |
|
27 |
|
|
// Typedefs
|
28 |
|
|
typedef unsigned int uint32;
|
29 |
|
|
typedef unsigned short uint16;
|
30 |
|
|
typedef unsigned char uint8;
|
31 |
|
|
|
32 |
|
|
// Memory Access
|
33 |
|
|
#ifdef WIN32
|
34 |
175 |
rhoads |
uint32 MemoryRead(uint32 Address);
|
35 |
|
|
void MemoryWrite(uint32 Address, uint32 Value);
|
36 |
|
|
#define atoi atoi2
|
37 |
138 |
rhoads |
#else
|
38 |
175 |
rhoads |
#define MemoryRead(A) (*(volatile uint32*)(A))
|
39 |
|
|
#define MemoryWrite(A,V) *(volatile uint32*)(A)=(V)
|
40 |
138 |
rhoads |
#endif
|
41 |
|
|
|
42 |
|
|
/***************** LibC ******************/
|
43 |
150 |
rhoads |
#ifndef _LIBC
|
44 |
138 |
rhoads |
#ifndef NULL
|
45 |
|
|
#define NULL (void*)0
|
46 |
|
|
#endif
|
47 |
|
|
|
48 |
|
|
#define assert(A) if((A)==0){OS_Assert();UartPrintfCritical("\r\nAssert %s:%d\r\n", __FILE__, __LINE__);}
|
49 |
|
|
|
50 |
|
|
#define isprint(c) (' '<=(c)&&(c)<='~')
|
51 |
|
|
#define isspace(c) ((c)==' '||(c)=='\t'||(c)=='\n'||(c)=='\r')
|
52 |
|
|
#define isdigit(c) ('0'<=(c)&&(c)<='9')
|
53 |
|
|
#define islower(c) ('a'<=(c)&&(c)<='z')
|
54 |
|
|
#define isupper(c) ('A'<=(c)&&(c)<='Z')
|
55 |
|
|
#define isalpha(c) (islower(c)||isupper(c))
|
56 |
|
|
#define isalnum(c) (isalpha(c)||isdigit(c))
|
57 |
|
|
#define min(a,b) ((a)<(b)?(a):(b))
|
58 |
150 |
rhoads |
#define memcpy memcpy2 //don't use built in version
|
59 |
138 |
rhoads |
|
60 |
|
|
char *strcpy(char *dst, const char *src);
|
61 |
|
|
char *strncpy(char *dst, const char *src, int count);
|
62 |
|
|
char *strcat(char *dst, const char *src);
|
63 |
|
|
char *strncat(char *dst, const char *src, int count);
|
64 |
175 |
rhoads |
int strcmp(const char *string1, const char *string2);
|
65 |
|
|
int strncmp(const char *string1, const char *string2, int count);
|
66 |
150 |
rhoads |
char *strstr(const char *string, const char *find);
|
67 |
175 |
rhoads |
int strlen(const char *string);
|
68 |
138 |
rhoads |
void *memcpy(void *dst, const void *src, unsigned long bytes);
|
69 |
144 |
rhoads |
void *memmove(void *dst, const void *src, unsigned long bytes);
|
70 |
175 |
rhoads |
int memcmp(const void *cs, const void *ct, unsigned long bytes);
|
71 |
138 |
rhoads |
void *memset(void *dst, int c, unsigned long bytes);
|
72 |
175 |
rhoads |
int abs(int n);
|
73 |
|
|
int rand(void);
|
74 |
|
|
void srand(unsigned int seed);
|
75 |
|
|
long strtol(const char *s, const char **end, int base);
|
76 |
|
|
int atoi(const char *s);
|
77 |
150 |
rhoads |
char *itoa(int num, char *dst, int base);
|
78 |
138 |
rhoads |
#ifndef NO_ELLIPSIS
|
79 |
175 |
rhoads |
int sprintf(char *s, const char *format, ...);
|
80 |
|
|
int sscanf(char *s, const char *format, ...);
|
81 |
138 |
rhoads |
#endif
|
82 |
175 |
rhoads |
#ifdef INCLUDE_DUMP
|
83 |
|
|
void dump(const unsigned char *data, int length);
|
84 |
|
|
#endif
|
85 |
|
|
#ifdef INCLUDE_QSORT
|
86 |
|
|
void qsort(void *base,
|
87 |
|
|
long n,
|
88 |
|
|
long size,
|
89 |
|
|
int (*cmp)(const void *,const void *));
|
90 |
|
|
void *bsearch(const void *key,
|
91 |
|
|
const void *base,
|
92 |
|
|
long n,
|
93 |
|
|
long size,
|
94 |
|
|
int (*cmp)(const void *,const void *));
|
95 |
|
|
#endif
|
96 |
|
|
#ifdef INCLUDE_TIMELIB
|
97 |
|
|
#define difftime(time2,time1) (time2-time1)
|
98 |
|
|
typedef unsigned long time_t; //start at 1/1/80
|
99 |
|
|
struct tm {
|
100 |
|
|
int tm_sec; //(0,59)
|
101 |
|
|
int tm_min; //(0,59)
|
102 |
|
|
int tm_hour; //(0,23)
|
103 |
|
|
int tm_mday; //(1,31)
|
104 |
|
|
int tm_mon; //(0,11)
|
105 |
|
|
int tm_year; //(0,n) from 1990
|
106 |
|
|
int tm_wday; //(0,6) calculated
|
107 |
|
|
int tm_yday; //(0,365) calculated
|
108 |
|
|
int tm_isdst; // calculated
|
109 |
|
|
};
|
110 |
|
|
time_t mktime(struct tm *tp);
|
111 |
|
|
void gmtime_r(const time_t *tp, struct tm *out);
|
112 |
|
|
#endif
|
113 |
150 |
rhoads |
#define _LIBC
|
114 |
175 |
rhoads |
#endif //_LIBC
|
115 |
138 |
rhoads |
|
116 |
|
|
/***************** Assembly **************/
|
117 |
|
|
typedef uint32 jmp_buf[20];
|
118 |
|
|
extern uint32 OS_AsmInterruptEnable(uint32 state);
|
119 |
|
|
extern void OS_AsmInterruptInit(void);
|
120 |
|
|
extern int setjmp(jmp_buf env);
|
121 |
|
|
extern void longjmp(jmp_buf env, int val);
|
122 |
|
|
extern uint32 OS_AsmMult(uint32 a, uint32 b, unsigned long *hi);
|
123 |
|
|
|
124 |
|
|
/***************** Heap ******************/
|
125 |
|
|
#define HEAP_SYSTEM (void*)0
|
126 |
|
|
#define HEAP_GENERAL (void*)1
|
127 |
|
|
#define HEAP_SMALL (void*)2
|
128 |
|
|
#define HEAP_UI (void*)3
|
129 |
|
|
typedef struct OS_Heap_s OS_Heap_t;
|
130 |
|
|
OS_Heap_t *OS_HeapCreate(const char *Name, void *Memory, uint32 Size);
|
131 |
|
|
void OS_HeapDestroy(OS_Heap_t *Heap);
|
132 |
|
|
void *OS_HeapMalloc(OS_Heap_t *Heap, int Bytes);
|
133 |
|
|
void OS_HeapFree(void *Block);
|
134 |
|
|
void OS_HeapAlternate(OS_Heap_t *Heap, OS_Heap_t *Alternate);
|
135 |
|
|
void OS_HeapRegister(void *Index, OS_Heap_t *Heap);
|
136 |
|
|
|
137 |
|
|
/***************** Thread *****************/
|
138 |
166 |
rhoads |
#if OS_CPU_COUNT <= 1
|
139 |
175 |
rhoads |
// Single CPU
|
140 |
|
|
#define OS_CpuIndex() 0
|
141 |
|
|
#define OS_CriticalBegin() OS_AsmInterruptEnable(0)
|
142 |
|
|
#define OS_CriticalEnd(S) OS_AsmInterruptEnable(S)
|
143 |
|
|
#define OS_SpinLock() 0
|
144 |
|
|
#define OS_SpinUnlock(S)
|
145 |
|
|
#define OS_SpinCountGet() 0
|
146 |
|
|
#define OS_SpinCountSet(S)
|
147 |
166 |
rhoads |
#else
|
148 |
175 |
rhoads |
// Symmetric multiprocessing
|
149 |
|
|
uint32 OS_CpuIndex(void);
|
150 |
|
|
#define OS_CriticalBegin() OS_SpinLock()
|
151 |
|
|
#define OS_CriticalEnd(S) OS_SpinUnlock(S)
|
152 |
|
|
uint32 OS_SpinLock(void);
|
153 |
|
|
void OS_SpinUnlock(uint32 state);
|
154 |
|
|
uint32 OS_SpinCountGet(void);
|
155 |
|
|
void OS_SpinCountSet(uint32 count);
|
156 |
|
|
void OS_CpuInterrupt(uint32 cpuIndex, uint32 bitfield);
|
157 |
166 |
rhoads |
#endif
|
158 |
|
|
|
159 |
138 |
rhoads |
#ifdef WIN32
|
160 |
175 |
rhoads |
#define STACK_SIZE_MINIMUM (1024*4)
|
161 |
138 |
rhoads |
#else
|
162 |
175 |
rhoads |
#define STACK_SIZE_MINIMUM (1024*1)
|
163 |
138 |
rhoads |
#endif
|
164 |
|
|
#define STACK_SIZE_DEFAULT 1024*2
|
165 |
|
|
#define THREAD_PRIORITY_IDLE 0
|
166 |
|
|
#define THREAD_PRIORITY_MAX 255
|
167 |
166 |
rhoads |
|
168 |
138 |
rhoads |
typedef void (*OS_FuncPtr_t)(void *Arg);
|
169 |
|
|
typedef struct OS_Thread_s OS_Thread_t;
|
170 |
|
|
OS_Thread_t *OS_ThreadCreate(const char *Name,
|
171 |
|
|
OS_FuncPtr_t FuncPtr,
|
172 |
|
|
void *Arg,
|
173 |
|
|
uint32 Priority,
|
174 |
|
|
uint32 StackSize);
|
175 |
|
|
void OS_ThreadExit(void);
|
176 |
|
|
OS_Thread_t *OS_ThreadSelf(void);
|
177 |
|
|
void OS_ThreadSleep(int Ticks);
|
178 |
|
|
uint32 OS_ThreadTime(void);
|
179 |
|
|
void OS_ThreadInfoSet(OS_Thread_t *Thread, void *Info);
|
180 |
|
|
void *OS_ThreadInfoGet(OS_Thread_t *Thread);
|
181 |
|
|
uint32 OS_ThreadPriorityGet(OS_Thread_t *Thread);
|
182 |
|
|
void OS_ThreadPrioritySet(OS_Thread_t *Thread, uint32 Priority);
|
183 |
|
|
void OS_ThreadTick(void *Arg);
|
184 |
175 |
rhoads |
void OS_ThreadCpuLock(OS_Thread_t *Thread, int CpuIndex);
|
185 |
138 |
rhoads |
|
186 |
|
|
/***************** Semaphore **************/
|
187 |
|
|
#define OS_SUCCESS 0
|
188 |
|
|
#define OS_ERROR -1
|
189 |
|
|
#define OS_WAIT_FOREVER -1
|
190 |
|
|
#define OS_NO_WAIT 0
|
191 |
|
|
typedef struct OS_Semaphore_s OS_Semaphore_t;
|
192 |
|
|
OS_Semaphore_t *OS_SemaphoreCreate(const char *Name, uint32 Count);
|
193 |
|
|
void OS_SemaphoreDelete(OS_Semaphore_t *Semaphore);
|
194 |
|
|
int OS_SemaphorePend(OS_Semaphore_t *Semaphore, int Ticks); //tick ~= 10ms
|
195 |
|
|
void OS_SemaphorePost(OS_Semaphore_t *Semaphore);
|
196 |
|
|
|
197 |
|
|
/***************** Mutex ******************/
|
198 |
|
|
typedef struct OS_Mutex_s OS_Mutex_t;
|
199 |
|
|
OS_Mutex_t *OS_MutexCreate(const char *Name);
|
200 |
|
|
void OS_MutexDelete(OS_Mutex_t *Semaphore);
|
201 |
|
|
void OS_MutexPend(OS_Mutex_t *Semaphore);
|
202 |
|
|
void OS_MutexPost(OS_Mutex_t *Semaphore);
|
203 |
|
|
|
204 |
|
|
/***************** MQueue *****************/
|
205 |
|
|
enum {
|
206 |
|
|
MESSAGE_TYPE_USER = 0,
|
207 |
|
|
MESSAGE_TYPE_TIMER = 5
|
208 |
|
|
};
|
209 |
|
|
typedef struct OS_MQueue_s OS_MQueue_t;
|
210 |
|
|
OS_MQueue_t *OS_MQueueCreate(const char *Name,
|
211 |
|
|
int MessageCount,
|
212 |
|
|
int MessageBytes);
|
213 |
|
|
void OS_MQueueDelete(OS_MQueue_t *MQueue);
|
214 |
|
|
int OS_MQueueSend(OS_MQueue_t *MQueue, void *Message);
|
215 |
|
|
int OS_MQueueGet(OS_MQueue_t *MQueue, void *Message, int Ticks);
|
216 |
|
|
|
217 |
|
|
/***************** Timer ******************/
|
218 |
|
|
typedef struct OS_Timer_s OS_Timer_t;
|
219 |
|
|
OS_Timer_t *OS_TimerCreate(const char *Name, OS_MQueue_t *MQueue, uint32 Info);
|
220 |
|
|
void OS_TimerDelete(OS_Timer_t *Timer);
|
221 |
|
|
void OS_TimerStart(OS_Timer_t *Timer, uint32 Ticks, uint32 TicksRestart);
|
222 |
|
|
void OS_TimerStop(OS_Timer_t *Timer);
|
223 |
|
|
|
224 |
|
|
/***************** ISR ********************/
|
225 |
175 |
rhoads |
#define STACK_EPC 88/4
|
226 |
|
|
void OS_InterruptServiceRoutine(uint32 Status, uint32 *Stack);
|
227 |
138 |
rhoads |
void OS_InterruptRegister(uint32 Mask, OS_FuncPtr_t FuncPtr);
|
228 |
|
|
uint32 OS_InterruptStatus(void);
|
229 |
|
|
uint32 OS_InterruptMaskSet(uint32 Mask);
|
230 |
|
|
uint32 OS_InterruptMaskClear(uint32 Mask);
|
231 |
|
|
|
232 |
|
|
/***************** Init ******************/
|
233 |
|
|
void OS_Init(uint32 *HeapStorage, uint32 Bytes);
|
234 |
|
|
void OS_Start(void);
|
235 |
|
|
void OS_Assert(void);
|
236 |
|
|
void MainThread(void *Arg);
|
237 |
175 |
rhoads |
void OS_DebuggerInit(void);
|
238 |
138 |
rhoads |
|
239 |
|
|
/***************** UART ******************/
|
240 |
150 |
rhoads |
typedef uint8* (*PacketGetFunc_t)(void);
|
241 |
138 |
rhoads |
void UartInit(void);
|
242 |
|
|
void UartWrite(int C);
|
243 |
|
|
uint8 UartRead(void);
|
244 |
|
|
void UartWriteData(uint8 *Data, int Length);
|
245 |
|
|
void UartReadData(uint8 *Data, int Length);
|
246 |
|
|
#ifndef NO_ELLIPSIS2
|
247 |
|
|
void UartPrintf(const char *format, ...);
|
248 |
|
|
void UartPrintfPoll(const char *format, ...);
|
249 |
|
|
void UartPrintfCritical(const char *format, ...);
|
250 |
166 |
rhoads |
void UartPrintfNull(const char *format, ...);
|
251 |
138 |
rhoads |
void UartScanf(const char *format, ...);
|
252 |
|
|
#endif
|
253 |
150 |
rhoads |
void UartPacketConfig(PacketGetFunc_t PacketGetFunc,
|
254 |
|
|
int PacketSize,
|
255 |
|
|
OS_MQueue_t *mQueue);
|
256 |
|
|
void UartPacketSend(uint8 *Data, int Bytes);
|
257 |
138 |
rhoads |
int puts(const char *string);
|
258 |
|
|
int getch(void);
|
259 |
|
|
int kbhit(void);
|
260 |
|
|
void LogWrite(int a);
|
261 |
|
|
void LogDump(void);
|
262 |
150 |
rhoads |
void Led(int value);
|
263 |
138 |
rhoads |
|
264 |
166 |
rhoads |
/***************** Keyboard **************/
|
265 |
|
|
#define KEYBOARD_RAW 0x100
|
266 |
|
|
#define KEYBOARD_E0 0x200
|
267 |
|
|
#define KEYBOARD_RELEASE 0x400
|
268 |
|
|
void KeyboardInit(void);
|
269 |
|
|
int KeyboardGetch(void);
|
270 |
150 |
rhoads |
|
271 |
138 |
rhoads |
/***************** Math ******************/
|
272 |
|
|
//IEEE single precision floating point math
|
273 |
|
|
#ifndef WIN32
|
274 |
|
|
#define FP_Neg __negsf2
|
275 |
|
|
#define FP_Add __addsf3
|
276 |
|
|
#define FP_Sub __subsf3
|
277 |
|
|
#define FP_Mult __mulsf3
|
278 |
|
|
#define FP_Div __divsf3
|
279 |
|
|
#define FP_ToLong __fixsfsi
|
280 |
|
|
#define FP_ToFloat __floatsisf
|
281 |
|
|
#define sqrt FP_Sqrt
|
282 |
|
|
#define cos FP_Cos
|
283 |
|
|
#define sin FP_Sin
|
284 |
|
|
#define atan FP_Atan
|
285 |
|
|
#define log FP_Log
|
286 |
|
|
#define exp FP_Exp
|
287 |
|
|
#endif
|
288 |
|
|
float FP_Neg(float a_fp);
|
289 |
|
|
float FP_Add(float a_fp, float b_fp);
|
290 |
|
|
float FP_Sub(float a_fp, float b_fp);
|
291 |
|
|
float FP_Mult(float a_fp, float b_fp);
|
292 |
|
|
float FP_Div(float a_fp, float b_fp);
|
293 |
|
|
long FP_ToLong(float a_fp);
|
294 |
|
|
float FP_ToFloat(long af);
|
295 |
|
|
int FP_Cmp(float a_fp, float b_fp);
|
296 |
|
|
float FP_Sqrt(float a);
|
297 |
|
|
float FP_Cos(float rad);
|
298 |
|
|
float FP_Sin(float rad);
|
299 |
|
|
float FP_Atan(float x);
|
300 |
|
|
float FP_Atan2(float y, float x);
|
301 |
|
|
float FP_Exp(float x);
|
302 |
|
|
float FP_Log(float x);
|
303 |
|
|
float FP_Pow(float x, float y);
|
304 |
|
|
|
305 |
|
|
#endif //__PLASMA_H__
|
306 |
|
|
|