OpenCores
URL https://opencores.org/ocsvn/plasma/plasma/trunk

Subversion Repositories plasma

[/] [plasma/] [trunk/] [kernel/] [rtos.h] - Blame information for rev 394

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.