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