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