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

Subversion Repositories plasma

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

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

powered by: WebSVN 2.1.0

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