1 |
400 |
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 |
|
|
// Symmetric Multi-Processing
|
16 |
|
|
#define OS_CPU_COUNT 1
|
17 |
|
|
|
18 |
|
|
// Typedefs
|
19 |
|
|
typedef unsigned int uint32;
|
20 |
|
|
typedef unsigned short uint16;
|
21 |
|
|
typedef unsigned char uint8;
|
22 |
|
|
|
23 |
|
|
// Memory Access
|
24 |
|
|
#ifdef WIN32
|
25 |
|
|
#define _CRT_SECURE_NO_WARNINGS 1
|
26 |
|
|
#pragma warning(disable:4996) //atoi()
|
27 |
|
|
#include <stdio.h>
|
28 |
|
|
#include <stdlib.h>
|
29 |
|
|
#include <assert.h>
|
30 |
|
|
#define _LIBC
|
31 |
402 |
rhoads |
extern void __stdcall Sleep(unsigned long value);
|
32 |
400 |
rhoads |
uint32 MemoryRead(uint32 Address);
|
33 |
|
|
void MemoryWrite(uint32 Address, uint32 Value);
|
34 |
|
|
#else
|
35 |
|
|
#define MemoryRead(A) (*(volatile uint32*)(A))
|
36 |
|
|
#define MemoryWrite(A,V) *(volatile uint32*)(A)=(V)
|
37 |
|
|
#endif
|
38 |
|
|
|
39 |
402 |
rhoads |
/***************** Simulation Functions ******************/
|
40 |
|
|
void OS_InitSimulation(void);
|
41 |
|
|
|
42 |
400 |
rhoads |
/***************** LibC ******************/
|
43 |
402 |
rhoads |
#undef isprint
|
44 |
|
|
#undef isspace
|
45 |
|
|
#undef isdigit
|
46 |
|
|
#undef islower
|
47 |
|
|
#undef isupper
|
48 |
|
|
#undef isalpha
|
49 |
|
|
#undef isalnum
|
50 |
400 |
rhoads |
#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 |
|
|
#undef min
|
58 |
|
|
#define min(a,b) ((a)<(b)?(a):(b))
|
59 |
|
|
#define strcpy strcpy2 //don't use intrinsic functions
|
60 |
402 |
rhoads |
#define strncpy strncpy2
|
61 |
400 |
rhoads |
#define strcat strcat2
|
62 |
|
|
#define strncat strncat2
|
63 |
|
|
#define strcmp strcmp2
|
64 |
402 |
rhoads |
#define strncmp strncmp2
|
65 |
400 |
rhoads |
#define strstr strstr2
|
66 |
|
|
#define strlen strlen2
|
67 |
|
|
#define memcpy memcpy2
|
68 |
402 |
rhoads |
#define memmove memmove2
|
69 |
400 |
rhoads |
#define memcmp memcmp2
|
70 |
|
|
#define memset memset2
|
71 |
|
|
#define abs abs2
|
72 |
|
|
|
73 |
|
|
char *strcpy(char *dst, const char *src);
|
74 |
|
|
char *strncpy(char *dst, const char *src, int count);
|
75 |
|
|
char *strcat(char *dst, const char *src);
|
76 |
|
|
char *strncat(char *dst, const char *src, int count);
|
77 |
|
|
int strcmp(const char *string1, const char *string2);
|
78 |
|
|
int strncmp(const char *string1, const char *string2, int count);
|
79 |
|
|
char *strstr(const char *string, const char *find);
|
80 |
|
|
int strlen(const char *string);
|
81 |
|
|
void *memcpy(void *dst, const void *src, unsigned long bytes);
|
82 |
|
|
void *memmove(void *dst, const void *src, unsigned long bytes);
|
83 |
|
|
int memcmp(const void *cs, const void *ct, unsigned long bytes);
|
84 |
|
|
void *memset(void *dst, int c, unsigned long bytes);
|
85 |
|
|
int abs(int n);
|
86 |
|
|
|
87 |
|
|
#ifndef _LIBC
|
88 |
|
|
#define assert(A) if((A)==0){OS_Assert();UartPrintfCritical("\r\nAssert %s:%d\r\n", __FILE__, __LINE__);}
|
89 |
|
|
#define atoi atoi2
|
90 |
|
|
#define printf UartPrintf
|
91 |
|
|
//#define printf UartPrintfPoll
|
92 |
|
|
#define scanf UartScanf
|
93 |
|
|
#define malloc(S) OS_HeapMalloc(NULL, S)
|
94 |
|
|
#define free(S) OS_HeapFree(S)
|
95 |
|
|
#define NULL (void*)0
|
96 |
|
|
|
97 |
|
|
int rand(void);
|
98 |
|
|
void srand(unsigned int seed);
|
99 |
|
|
long strtol(const char *s, char **end, int base);
|
100 |
|
|
int atoi(const char *s);
|
101 |
|
|
char *itoa(int num, char *dst, int base);
|
102 |
|
|
|
103 |
|
|
#ifndef NO_ELLIPSIS
|
104 |
|
|
int sprintf(char *s, const char *format, ...);
|
105 |
|
|
int sscanf(const char *s, const char *format, ...);
|
106 |
|
|
#endif
|
107 |
|
|
#ifdef INCLUDE_DUMP
|
108 |
|
|
void dump(const unsigned char *data, int length);
|
109 |
|
|
#endif
|
110 |
|
|
#ifdef INCLUDE_QSORT
|
111 |
|
|
void qsort(void *base,
|
112 |
|
|
long n,
|
113 |
|
|
long size,
|
114 |
|
|
int (*cmp)(const void *,const void *));
|
115 |
|
|
void *bsearch(const void *key,
|
116 |
|
|
const void *base,
|
117 |
|
|
long n,
|
118 |
|
|
long size,
|
119 |
|
|
int (*cmp)(const void *,const void *));
|
120 |
|
|
#endif
|
121 |
|
|
#ifdef INCLUDE_TIMELIB
|
122 |
|
|
#define difftime(time2,time1) (time2-time1)
|
123 |
|
|
typedef unsigned long time_t; //start at 1/1/80
|
124 |
|
|
struct tm {
|
125 |
|
|
int tm_sec; //(0,59)
|
126 |
|
|
int tm_min; //(0,59)
|
127 |
|
|
int tm_hour; //(0,23)
|
128 |
|
|
int tm_mday; //(1,31)
|
129 |
|
|
int tm_mon; //(0,11)
|
130 |
|
|
int tm_year; //(0,n) from 1900
|
131 |
|
|
int tm_wday; //(0,6) calculated
|
132 |
|
|
int tm_yday; //(0,365) calculated
|
133 |
|
|
int tm_isdst; // calculated
|
134 |
|
|
};
|
135 |
|
|
time_t mktime(struct tm *tp);
|
136 |
|
|
void gmtime_r(const time_t *tp, struct tm *out);
|
137 |
|
|
void gmtimeDst(time_t dstTimeIn, time_t dstTimeOut);
|
138 |
|
|
void gmtimeDstSet(time_t *tp, time_t *dstTimeIn, time_t *dstTimeOut);
|
139 |
|
|
#endif
|
140 |
|
|
#endif //_LIBC
|
141 |
|
|
|
142 |
|
|
/***************** Assembly **************/
|
143 |
|
|
typedef uint32 jmp_buf[20];
|
144 |
|
|
extern uint32 OS_AsmInterruptEnable(uint32 state);
|
145 |
|
|
extern void OS_AsmInterruptInit(void);
|
146 |
|
|
extern int setjmp(jmp_buf env);
|
147 |
|
|
extern void longjmp(jmp_buf env, int val);
|
148 |
|
|
extern uint32 OS_AsmMult(uint32 a, uint32 b, unsigned long *hi);
|
149 |
|
|
extern void *OS_Syscall(uint32 value);
|
150 |
|
|
|
151 |
|
|
/***************** Heap ******************/
|
152 |
|
|
#define HEAP_USER (void*)0
|
153 |
|
|
#define HEAP_SYSTEM (void*)1
|
154 |
|
|
#define HEAP_SMALL (void*)2
|
155 |
|
|
#define HEAP_UI (void*)3
|
156 |
|
|
typedef struct OS_Heap_s OS_Heap_t;
|
157 |
|
|
OS_Heap_t *OS_HeapCreate(const char *name, void *memory, uint32 size);
|
158 |
|
|
void OS_HeapDestroy(OS_Heap_t *heap);
|
159 |
|
|
void *OS_HeapMalloc(OS_Heap_t *heap, int bytes);
|
160 |
|
|
void OS_HeapFree(void *block);
|
161 |
|
|
void OS_HeapAlternate(OS_Heap_t *heap, OS_Heap_t *alternate);
|
162 |
|
|
void OS_HeapRegister(void *index, OS_Heap_t *heap);
|
163 |
|
|
|
164 |
|
|
/***************** Critical Sections *****************/
|
165 |
|
|
#if OS_CPU_COUNT <= 1
|
166 |
|
|
// Single CPU
|
167 |
|
|
#define OS_CpuIndex() 0
|
168 |
|
|
#define OS_CriticalBegin() OS_AsmInterruptEnable(0)
|
169 |
|
|
#define OS_CriticalEnd(S) OS_AsmInterruptEnable(S)
|
170 |
|
|
#define OS_SpinLock() 0
|
171 |
|
|
#define OS_SpinUnlock(S)
|
172 |
|
|
#else
|
173 |
|
|
// Symmetric multiprocessing
|
174 |
|
|
uint32 OS_CpuIndex(void);
|
175 |
|
|
#define OS_CriticalBegin() OS_SpinLock()
|
176 |
|
|
#define OS_CriticalEnd(S) OS_SpinUnlock(S)
|
177 |
|
|
uint32 OS_SpinLock(void);
|
178 |
|
|
void OS_SpinUnlock(uint32 state);
|
179 |
|
|
#endif
|
180 |
|
|
|
181 |
|
|
/***************** Thread *****************/
|
182 |
|
|
#ifdef WIN32
|
183 |
402 |
rhoads |
#define STACK_SIZE_MINIMUM (1024*8)
|
184 |
400 |
rhoads |
#else
|
185 |
|
|
#define STACK_SIZE_MINIMUM (1024*1)
|
186 |
|
|
#endif
|
187 |
|
|
#define STACK_SIZE_DEFAULT 1024*2
|
188 |
|
|
#undef THREAD_PRIORITY_IDLE
|
189 |
|
|
#define THREAD_PRIORITY_IDLE 0
|
190 |
|
|
#define THREAD_PRIORITY_MAX 255
|
191 |
|
|
|
192 |
|
|
typedef void (*OS_FuncPtr_t)(void *arg);
|
193 |
|
|
typedef struct OS_Thread_s OS_Thread_t;
|
194 |
|
|
OS_Thread_t *OS_ThreadCreate(const char *name,
|
195 |
|
|
OS_FuncPtr_t funcPtr,
|
196 |
|
|
void *arg,
|
197 |
|
|
uint32 priority,
|
198 |
|
|
uint32 stackSize);
|
199 |
|
|
void OS_ThreadExit(void);
|
200 |
|
|
OS_Thread_t *OS_ThreadSelf(void);
|
201 |
|
|
void OS_ThreadSleep(int ticks);
|
202 |
|
|
uint32 OS_ThreadTime(void);
|
203 |
|
|
void OS_ThreadInfoSet(OS_Thread_t *thread, uint32 index, void *info);
|
204 |
|
|
void *OS_ThreadInfoGet(OS_Thread_t *thread, uint32 index);
|
205 |
|
|
uint32 OS_ThreadPriorityGet(OS_Thread_t *thread);
|
206 |
|
|
void OS_ThreadPrioritySet(OS_Thread_t *thread, uint32 priority);
|
207 |
|
|
void OS_ThreadProcessId(OS_Thread_t *thread, uint32 processId, OS_Heap_t *heap);
|
208 |
|
|
void OS_ThreadCpuLock(OS_Thread_t *thread, int cpuIndex);
|
209 |
|
|
|
210 |
|
|
/***************** Semaphore **************/
|
211 |
|
|
#define OS_SUCCESS 0
|
212 |
|
|
#define OS_ERROR -1
|
213 |
|
|
#define OS_WAIT_FOREVER -1
|
214 |
|
|
#define OS_NO_WAIT 0
|
215 |
|
|
typedef struct OS_Semaphore_s OS_Semaphore_t;
|
216 |
|
|
OS_Semaphore_t *OS_SemaphoreCreate(const char *name, uint32 count);
|
217 |
|
|
void OS_SemaphoreDelete(OS_Semaphore_t *semaphore);
|
218 |
|
|
int OS_SemaphorePend(OS_Semaphore_t *semaphore, int ticks); //tick ~= 10ms
|
219 |
|
|
void OS_SemaphorePost(OS_Semaphore_t *semaphore);
|
220 |
|
|
|
221 |
|
|
/***************** Mutex ******************/
|
222 |
|
|
typedef struct OS_Mutex_s OS_Mutex_t;
|
223 |
|
|
OS_Mutex_t *OS_MutexCreate(const char *name);
|
224 |
|
|
void OS_MutexDelete(OS_Mutex_t *semaphore);
|
225 |
|
|
void OS_MutexPend(OS_Mutex_t *semaphore);
|
226 |
|
|
void OS_MutexPost(OS_Mutex_t *semaphore);
|
227 |
|
|
|
228 |
|
|
/***************** MQueue *****************/
|
229 |
|
|
enum {
|
230 |
|
|
MESSAGE_TYPE_USER = 0,
|
231 |
|
|
MESSAGE_TYPE_TIMER = 5
|
232 |
|
|
};
|
233 |
|
|
typedef struct OS_MQueue_s OS_MQueue_t;
|
234 |
|
|
OS_MQueue_t *OS_MQueueCreate(const char *name,
|
235 |
|
|
int messageCount,
|
236 |
|
|
int messageBytes);
|
237 |
|
|
void OS_MQueueDelete(OS_MQueue_t *mQueue);
|
238 |
|
|
int OS_MQueueSend(OS_MQueue_t *mQueue, void *message);
|
239 |
|
|
int OS_MQueueGet(OS_MQueue_t *mQueue, void *message, int ticks);
|
240 |
|
|
|
241 |
|
|
/***************** Job ********************/
|
242 |
|
|
typedef void (*JobFunc_t)(void *a0, void *a1, void *a2);
|
243 |
|
|
void OS_Job(JobFunc_t funcPtr, void *arg0, void *arg1, void *arg2);
|
244 |
|
|
|
245 |
|
|
/***************** Timer ******************/
|
246 |
|
|
typedef struct OS_Timer_s OS_Timer_t;
|
247 |
|
|
typedef void (*OS_TimerFuncPtr_t)(OS_Timer_t *timer, uint32 info);
|
248 |
|
|
OS_Timer_t *OS_TimerCreate(const char *name, OS_MQueue_t *mQueue, uint32 info);
|
249 |
|
|
void OS_TimerDelete(OS_Timer_t *timer);
|
250 |
|
|
void OS_TimerCallback(OS_Timer_t *timer, OS_TimerFuncPtr_t callback);
|
251 |
|
|
void OS_TimerStart(OS_Timer_t *timer, uint32 ticks, uint32 ticksRestart);
|
252 |
|
|
void OS_TimerStop(OS_Timer_t *timer);
|
253 |
|
|
|
254 |
|
|
/***************** ISR ********************/
|
255 |
|
|
#define STACK_EPC 88/4
|
256 |
|
|
void OS_InterruptServiceRoutine(uint32 status, uint32 *stack);
|
257 |
|
|
void OS_InterruptRegister(uint32 mask, OS_FuncPtr_t funcPtr);
|
258 |
|
|
uint32 OS_InterruptStatus(void);
|
259 |
|
|
uint32 OS_InterruptMaskSet(uint32 mask);
|
260 |
|
|
uint32 OS_InterruptMaskClear(uint32 mask);
|
261 |
|
|
|
262 |
|
|
/***************** Init ******************/
|
263 |
|
|
void OS_Init(uint32 *heapStorage, uint32 bytes);
|
264 |
|
|
void OS_Start(void);
|
265 |
|
|
void OS_Assert(void);
|
266 |
|
|
void OS_DebuggerInit(void);
|
267 |
|
|
void MainThread(void *Arg);
|
268 |
|
|
|
269 |
|
|
/***************** MMU ******************/
|
270 |
|
|
typedef struct {
|
271 |
|
|
const char *name;
|
272 |
|
|
OS_FuncPtr_t funcPtr;
|
273 |
|
|
void *arg;
|
274 |
|
|
uint32 priority;
|
275 |
|
|
uint32 stackSize;
|
276 |
|
|
uint32 heapSize;
|
277 |
|
|
uint32 processId;
|
278 |
|
|
OS_Semaphore_t *semaphoreDone;
|
279 |
|
|
uint8 *memory; //private
|
280 |
|
|
OS_Heap_t *heap; //private
|
281 |
|
|
OS_Thread_t *thread; //private
|
282 |
|
|
} OS_Process_t;
|
283 |
|
|
void OS_MMUInit(void);
|
284 |
|
|
void OS_MMUMemoryRegister(uint32 processId,
|
285 |
|
|
uint32 virtualAddress,
|
286 |
|
|
uint32 physicalAddress,
|
287 |
|
|
uint32 size,
|
288 |
|
|
uint32 writable);
|
289 |
|
|
OS_Process_t *OS_MMUProcessCreate(OS_Process_t *process);
|
290 |
|
|
void OS_MMUProcessDelete(OS_Process_t *process);
|
291 |
|
|
void OS_MMUUartPrintf(void);
|
292 |
|
|
void OS_MMUUartScanf(void);
|
293 |
|
|
void OS_MMUUartPrintfCritical(void);
|
294 |
|
|
|
295 |
|
|
/***************** UART ******************/
|
296 |
|
|
typedef uint8* (*PacketGetFunc_t)(void);
|
297 |
|
|
void UartInit(void);
|
298 |
|
|
void UartWrite(int ch);
|
299 |
|
|
uint8 UartRead(void);
|
300 |
|
|
void UartWriteData(uint8 *data, int length);
|
301 |
|
|
void UartReadData(uint8 *data, int length);
|
302 |
|
|
#ifndef NO_ELLIPSIS2
|
303 |
|
|
void UartPrintf(const char *format, ...);
|
304 |
|
|
void UartPrintfPoll(const char *format, ...);
|
305 |
|
|
void UartPrintfCritical(const char *format, ...);
|
306 |
|
|
void UartPrintfNull(const char *format, ...);
|
307 |
|
|
void UartScanf(const char *format, ...);
|
308 |
|
|
#endif
|
309 |
|
|
void UartPacketConfig(PacketGetFunc_t packetGetFunc,
|
310 |
|
|
int packetSize,
|
311 |
|
|
OS_MQueue_t *mQueue);
|
312 |
|
|
void UartPacketSend(uint8 *data, int bytes);
|
313 |
402 |
rhoads |
int OS_puts(const char *string);
|
314 |
|
|
int OS_getch(void);
|
315 |
|
|
int OS_kbhit(void);
|
316 |
400 |
rhoads |
void LogWrite(int a);
|
317 |
|
|
void LogDump(void);
|
318 |
|
|
void Led(int mask, int value);
|
319 |
|
|
|
320 |
|
|
/***************** Keyboard **************/
|
321 |
|
|
#define KEYBOARD_RAW 0x100
|
322 |
|
|
#define KEYBOARD_E0 0x200
|
323 |
|
|
#define KEYBOARD_RELEASE 0x400
|
324 |
|
|
void KeyboardInit(void);
|
325 |
|
|
int KeyboardGetch(void);
|
326 |
|
|
|
327 |
|
|
/***************** Math ******************/
|
328 |
|
|
//IEEE single precision floating point math
|
329 |
|
|
#ifndef WIN32
|
330 |
|
|
#define FP_Neg __negsf2
|
331 |
|
|
#define FP_Add __addsf3
|
332 |
|
|
#define FP_Sub __subsf3
|
333 |
|
|
#define FP_Mult __mulsf3
|
334 |
|
|
#define FP_Div __divsf3
|
335 |
|
|
#define FP_ToLong __fixsfsi
|
336 |
|
|
#define FP_ToFloat __floatsisf
|
337 |
|
|
#define sqrt FP_Sqrt
|
338 |
|
|
#define cos FP_Cos
|
339 |
|
|
#define sin FP_Sin
|
340 |
|
|
#define atan FP_Atan
|
341 |
|
|
#define log FP_Log
|
342 |
|
|
#define exp FP_Exp
|
343 |
|
|
#endif
|
344 |
|
|
float FP_Neg(float a_fp);
|
345 |
|
|
float FP_Add(float a_fp, float b_fp);
|
346 |
|
|
float FP_Sub(float a_fp, float b_fp);
|
347 |
|
|
float FP_Mult(float a_fp, float b_fp);
|
348 |
|
|
float FP_Div(float a_fp, float b_fp);
|
349 |
|
|
long FP_ToLong(float a_fp);
|
350 |
|
|
float FP_ToFloat(long af);
|
351 |
|
|
int FP_Cmp(float a_fp, float b_fp);
|
352 |
|
|
float FP_Sqrt(float a);
|
353 |
|
|
float FP_Cos(float rad);
|
354 |
|
|
float FP_Sin(float rad);
|
355 |
|
|
float FP_Atan(float x);
|
356 |
|
|
float FP_Atan2(float y, float x);
|
357 |
|
|
float FP_Exp(float x);
|
358 |
|
|
float FP_Log(float x);
|
359 |
|
|
float FP_Pow(float x, float y);
|
360 |
|
|
|
361 |
|
|
/***************** Filesys ******************/
|
362 |
|
|
#ifdef INCLUDE_FILESYS
|
363 |
|
|
#define FILE OS_FILE
|
364 |
|
|
#define fopen OS_fopen
|
365 |
|
|
#define fclose OS_fclose
|
366 |
|
|
#define fread OS_fread
|
367 |
|
|
#define fwrite OS_fwrite
|
368 |
|
|
#define fseek OS_fseek
|
369 |
|
|
#endif
|
370 |
|
|
#define _FILESYS_
|
371 |
|
|
typedef struct OS_FILE_s OS_FILE;
|
372 |
|
|
OS_FILE *OS_fopen(char *name, char *mode);
|
373 |
|
|
void OS_fclose(OS_FILE *file);
|
374 |
|
|
int OS_fread(void *buffer, int size, int count, OS_FILE *file);
|
375 |
|
|
int OS_fwrite(void *buffer, int size, int count, OS_FILE *file);
|
376 |
|
|
int OS_fseek(OS_FILE *file, int offset, int mode);
|
377 |
|
|
int OS_fmkdir(char *name);
|
378 |
|
|
int OS_fdir(OS_FILE *dir, char name[64]);
|
379 |
|
|
void OS_fdelete(char *name);
|
380 |
|
|
int OS_flength(char *entry);
|
381 |
|
|
|
382 |
|
|
/***************** Flash ******************/
|
383 |
|
|
void FlashRead(uint16 *dst, uint32 byteOffset, int bytes);
|
384 |
|
|
void FlashWrite(uint16 *src, uint32 byteOffset, int bytes);
|
385 |
|
|
void FlashErase(uint32 byteOffset);
|
386 |
|
|
|
387 |
|
|
#endif //__RTOS_H__
|
388 |
|
|
|