URL
https://opencores.org/ocsvn/plasma/plasma/trunk
Subversion Repositories plasma
Compare Revisions
- This comparison shows the changes necessary to convert path
/plasma/trunk/kernel
- from Rev 406 to Rev 407
- ↔ Reverse comparison
Rev 406 → Rev 407
/os_stubs.c
41,6 → 41,10
|
|
//Stub out RTOS functions |
#undef malloc |
#undef free |
void *OS_HeapMalloc(OS_Heap_t *heap, int bytes) {(void)heap; return malloc(bytes);} |
void OS_HeapFree(void *block) {free(block);} |
void UartPrintfCritical(const char *format, ...) {(void)format;} |
uint32 OS_AsmInterruptEnable(uint32 state) {(void)state; return 0;} |
void OS_Assert(void) {} |
/libc.c
12,7 → 12,6
#define NO_ELLIPSIS |
#include "rtos.h" |
|
|
char *strcpy(char *dst, const char *src) |
{ |
char *dstSave=dst; |
183,7 → 182,6
} |
|
|
#ifndef _LIBC |
static uint32 Rand1=0x1f2bcda3; |
int rand(void) |
{ |
403,7 → 401,6
} |
} |
} |
#endif //_LIBC |
|
|
#ifdef INCLUDE_DUMP |
/rtos_ex.c
15,19 → 15,19
|
/************** WIN32 Simulation Support *************/ |
#ifdef WIN32 |
#undef kbhit |
#undef getch |
#undef putch |
extern int kbhit(void); |
extern int getch(void); |
extern int putch(int); |
#include <conio.h> |
#define kbhit _kbhit |
#define getch _getch |
#define putch _putch |
extern void __stdcall Sleep(unsigned long value); |
|
#if OS_CPU_COUNT > 1 |
unsigned int __stdcall GetCurrentThreadId(void); |
typedef void (*LPTHREAD_START_ROUTINE)(void *lpThreadParameter); |
void * __stdcall CreateThread(void *lpsa, unsigned int dwStackSize, |
LPTHREAD_START_ROUTINE pfnThreadProc, void *pvParam, |
unsigned int dwCreationFlags, unsigned int *pdwThreadId); |
|
#if OS_CPU_COUNT > 1 |
|
static unsigned int ThreadId[OS_CPU_COUNT]; |
|
//PC simulation of multiple CPUs |
39,7 → 39,7
for(i = 1; i < OS_CPU_COUNT; ++i) |
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)OS_Start, NULL, 0, &ThreadId[i]); |
} |
#endif |
#endif //OS_CPU_COUNT > 1 |
|
static uint32 Memory[8]; |
|
109,7 → 109,8
return i; |
} |
#endif |
return 0; //0 to OS_CPU_COUNT-1; Read a CPU specific GPIO value |
//return MemoryRead(GPIO_CPU_INDEX); |
return 0; //0 to OS_CPU_COUNT-1 |
} |
|
|
122,8 → 123,8
|
cpuIndex = OS_CpuIndex(); |
state = OS_AsmInterruptEnable(0); //disable interrupts |
if(++SpinLockArray[cpuIndex] > 1) //check for nesting |
return state; |
if(SpinLockArray[cpuIndex]) |
return (uint32)-1; //already locked |
delay = (4 + cpuIndex) << 2; |
|
//Spin until only this CPU has the spin lock |
130,6 → 131,7
for(;;) |
{ |
ok = 1; |
SpinLockArray[cpuIndex] = 1; |
for(i = 0; i < OS_CPU_COUNT; ++i) |
{ |
if(i != cpuIndex && SpinLockArray[i]) |
145,7 → 147,6
if(delay < 128) |
delay <<= 1; |
state = OS_AsmInterruptEnable(0); //disable interrupts |
SpinLockArray[cpuIndex] = 1; |
} |
} |
|
154,11 → 155,10
void OS_SpinUnlock(uint32 state) |
{ |
uint32 cpuIndex; |
if(state == (uint32)-1) |
return; //nested lock call |
cpuIndex = OS_CpuIndex(); |
if(--SpinLockArray[cpuIndex] == 0) |
OS_AsmInterruptEnable(state); |
|
assert(SpinLockArray[cpuIndex] < 10); |
SpinLockArray[cpuIndex] = 0; |
} |
#endif //OS_CPU_COUNT > 1 |
|
/rtos_test.c
9,6 → 9,9
* DESCRIPTION: |
* Test Plasma Real Time Operating System |
*--------------------------------------------------------------------*/ |
#ifdef WIN32 |
#include <stdlib.h> |
#endif |
#include "plasma.h" |
#include "rtos.h" |
#include "tcpip.h" |
144,7 → 147,7
priority = OS_ThreadPriorityGet(thread); |
OS_ThreadSleep(10); |
printf("Arg=%d thread=0x%x info=0x%x priority=%d\n", |
(uint32)arg, thread, OS_ThreadInfoGet(thread, 0), priority); |
(int)arg, (int)thread, (int)OS_ThreadInfoGet(thread, 0), priority); |
OS_ThreadExit(); |
} |
|
245,6 → 248,13
OS_ThreadExit(); |
} |
|
//Test priority inversion |
static void TestMutexThread2(void *arg) |
{ |
(void)arg; |
printf("Priority inversion test thread\n"); |
} |
|
static void TestMutex(void) |
{ |
TestInfo_t info; |
253,21 → 263,25
OS_MutexPend(info.MyMutex); |
OS_MutexPend(info.MyMutex); |
OS_MutexPend(info.MyMutex); |
printf("Acquired mutexes\n"); |
|
OS_ThreadSleep(100); |
OS_ThreadCreate("TestMutex", TestMutexThread, &info, 150, 0); |
OS_ThreadCreate("TestMutex2", TestMutexThread2, &info, 110, 0); |
|
OS_ThreadCreate("TestMutex", TestMutexThread, &info, 50, 0); |
|
OS_ThreadSleep(50); |
printf("Posting mutexes at priority %d\n", |
OS_ThreadPriorityGet(OS_ThreadSelf())); |
OS_MutexPost(info.MyMutex); |
OS_MutexPost(info.MyMutex); |
OS_MutexPost(info.MyMutex); |
printf("Thread priority %d\n", OS_ThreadPriorityGet(OS_ThreadSelf())); |
OS_ThreadSleep(50); |
|
printf("Try get mutex\n"); |
OS_MutexPend(info.MyMutex); |
printf("Gotit\n"); |
printf("Got it\n"); |
|
OS_MutexDelete(info.MyMutex); |
OS_ThreadSleep(50); |
printf("Done.\n"); |
} |
|
406,11 → 420,17
{ |
int i; |
int j = 0; |
unsigned int state; |
unsigned int timeStart = OS_ThreadTime(); |
|
for(i = 0; i < 0x10000000; ++i) |
{ |
j += i; |
if((i & 0xff) == 0) |
{ |
state = OS_CriticalBegin(); |
j += i; |
OS_CriticalEnd(state); |
} |
if(OS_ThreadTime() - timeStart > 400) |
break; |
if((i & 0xfffff) == 0) |
563,4 → 583,3
} |
} |
|
|
/rtos.c
23,6 → 23,8
#define INFO_COUNT 4 |
#define HEAP_COUNT 8 |
|
#define PRINTF_DEBUG(STRING, A, B) |
//#define PRINTF_DEBUG(STRING, A, B) printf(STRING, A, B) |
|
/*************** Structures ***************/ |
#ifdef WIN32 |
98,6 → 100,7
struct OS_Mutex_s { |
OS_Semaphore_t *semaphore; |
OS_Thread_t *thread; |
uint32 priorityRestore; |
int count; |
}; |
//typedef struct OS_Mutex_s OS_Mutex_t; |
422,12 → 425,12
assert(threadCurrent->magic[0] == THREAD_MAGIC); //check stack overflow |
if(threadCurrent->state == THREAD_RUNNING) |
OS_ThreadPriorityInsert(&ThreadHead, threadCurrent); |
//printf("Pause(%d,%s) ", OS_CpuIndex(), threadCurrent->name); |
PRINTF_DEBUG("Pause(%d,%s) ", OS_CpuIndex(), threadCurrent->name); |
rc = setjmp(threadCurrent->env); //ANSI C call to save registers |
if(rc) |
{ |
//threadCurrent = ThreadCurrent[OS_CpuIndex()]; |
//printf("Resume(%d,%s) ", OS_CpuIndex(), threadCurrent->name); |
PRINTF_DEBUG("Resume(%d,%s) ", OS_CpuIndex(), |
ThreadCurrent[OS_CpuIndex()]->name); |
return; //Returned from longjmp() |
} |
} |
462,6 → 465,7
uint32 cpuIndex = OS_CpuIndex(); |
(void)arg; |
|
PRINTF_DEBUG("Starting(%d,%s) ", cpuIndex, OS_ThreadSelf()->name); |
OS_CriticalEnd(1); |
ThreadCurrent[cpuIndex]->funcPtr(ThreadCurrent[cpuIndex]->arg); |
OS_ThreadExit(); |
726,6 → 730,7
OS_Thread_t *thread; |
int returnCode=0; |
|
PRINTF_DEBUG("SemPend(%d,%s) ", OS_CpuIndex(), semaphore->name); |
assert(semaphore); |
assert(InterruptInside[OS_CpuIndex()] == 0); |
state = OS_CriticalBegin(); //Disable interrupts |
768,6 → 773,7
uint32 state; |
OS_Thread_t *thread; |
|
PRINTF_DEBUG("SemPost(%d,%s) ", OS_CpuIndex(), semaphore->name); |
assert(semaphore); |
state = OS_CriticalBegin(); |
if(++semaphore->count <= 0) |
817,6 → 823,7
void OS_MutexPend(OS_Mutex_t *mutex) |
{ |
OS_Thread_t *thread; |
uint32 state; |
|
assert(mutex); |
thread = OS_ThreadSelf(); |
825,9 → 832,17
++mutex->count; |
return; |
} |
|
state = OS_CriticalBegin(); |
//Priority inheritance to prevent priority inversion |
if(mutex->thread && mutex->thread->priority < thread->priority) |
OS_ThreadPrioritySet(mutex->thread, thread->priority); |
|
OS_SemaphorePend(mutex->semaphore, OS_WAIT_FOREVER); |
mutex->priorityRestore = thread->priority; |
mutex->thread = thread; |
mutex->count = 1; |
OS_CriticalEnd(state); |
} |
|
|
834,18 → 849,25
/******************************************/ |
void OS_MutexPost(OS_Mutex_t *mutex) |
{ |
OS_Thread_t *thread = OS_ThreadSelf(); |
uint32 state, priorityRestore; |
|
assert(mutex); |
assert(mutex->thread == OS_ThreadSelf()); |
assert(mutex->thread == thread); |
assert(mutex->count > 0); |
if(--mutex->count <= 0) |
{ |
state = OS_CriticalBegin(); |
mutex->thread = NULL; |
priorityRestore = mutex->priorityRestore; |
OS_SemaphorePost(mutex->semaphore); |
if(priorityRestore < thread->priority) |
OS_ThreadPrioritySet(thread, priorityRestore); |
OS_CriticalEnd(state); |
} |
} |
|
|
|
/***************** MQueue *****************/ |
/******************************************/ |
//Create a message queue |
1233,10 → 1255,10
/**************** Init ********************/ |
/******************************************/ |
//If there aren't any other ready to run threads then spin here |
static volatile uint32 IdleCount; |
static int SimulateIsr; |
static void OS_IdleThread(void *arg) |
{ |
uint32 IdleCount=0; |
(void)arg; |
|
//Don't block in the idle thread! |
1263,7 → 1285,7
{ |
unsigned int state; |
#ifndef WIN32 |
for(IdleCount = 0; IdleCount < 100000; ++IdleCount) ; |
for(IdleCount = 0; IdleCount < 25000; ++IdleCount) ; |
#endif |
state = OS_SpinLock(); |
OS_ThreadReschedule(1); |
1300,7 → 1322,7
if((int)OS_Init > 0x10000000) //Running from DDR? |
OS_AsmInterruptInit(); //Patch interrupt vector |
OS_InterruptMaskClear(0xffffffff); //Disable interrupts |
HeapArray[0] = OS_HeapCreate("Default", heapStorage, bytes); |
HeapArray[0] = OS_HeapCreate("Heap", heapStorage, bytes); |
HeapArray[1] = HeapArray[0]; |
SemaphoreSleep = OS_SemaphoreCreate("Sleep", 0); |
SemaphoreRelease = OS_SemaphoreCreate("Release", 1); |
/rtos.h
8,6 → 8,8
* Software 'as is' without warranty. Author liable for nothing. |
* DESCRIPTION: |
* Plasma Real Time Operating System |
* Fully pre-emptive RTOS with support for: |
* Heaps, Threads, Semaphores, Mutexes, Message Queues, and Timers. |
*--------------------------------------------------------------------*/ |
#ifndef __RTOS_H__ |
#define __RTOS_H__ |
21,14 → 23,14
typedef unsigned char uint8; |
|
// Memory Access |
#ifdef __TINYC__ |
#define WIN32 |
#endif |
#ifdef WIN32 |
#define _CRT_SECURE_NO_WARNINGS 1 |
#pragma warning(disable:4996) //atoi() |
#include <stdio.h> |
#include <stdlib.h> |
#include <assert.h> |
#define _LIBC |
extern void __stdcall Sleep(unsigned long value); |
uint32 MemoryRead(uint32 Address); |
void MemoryWrite(uint32 Address, uint32 Value); |
#else |
36,9 → 38,6
#define MemoryWrite(A,V) *(volatile uint32*)(A)=(V) |
#endif |
|
/***************** Simulation Functions ******************/ |
void OS_InitSimulation(void); |
|
/***************** LibC ******************/ |
#undef isprint |
#undef isspace |
47,6 → 46,7
#undef isupper |
#undef isalpha |
#undef isalnum |
#undef min |
#define isprint(c) (' '<=(c)&&(c)<='~') |
#define isspace(c) ((c)==' '||(c)=='\t'||(c)=='\n'||(c)=='\r') |
#define isdigit(c) ('0'<=(c)&&(c)<='9') |
54,7 → 54,6
#define isupper(c) ('A'<=(c)&&(c)<='Z') |
#define isalpha(c) (islower(c)||isupper(c)) |
#define isalnum(c) (isalpha(c)||isdigit(c)) |
#undef min |
#define min(a,b) ((a)<(b)?(a):(b)) |
#define strcpy strcpy2 //don't use intrinsic functions |
#define strncpy strncpy2 |
69,6 → 68,15
#define memcmp memcmp2 |
#define memset memset2 |
#define abs abs2 |
#define atoi atoi2 |
#define rand rand2 |
#define srand srand2 |
#define strtol strtol2 |
#define itoa itoa2 |
#define sprintf sprintf2 |
#define sscanf sscanf2 |
#define malloc(S) OS_HeapMalloc(NULL, S) |
#define free(S) OS_HeapFree(S) |
|
char *strcpy(char *dst, const char *src); |
char *strncpy(char *dst, const char *src, int count); |
83,21 → 91,10
int memcmp(const void *cs, const void *ct, unsigned long bytes); |
void *memset(void *dst, int c, unsigned long bytes); |
int abs(int n); |
|
#ifndef _LIBC |
#define assert(A) if((A)==0){OS_Assert();UartPrintfCritical("\r\nAssert %s:%d\r\n", __FILE__, __LINE__);} |
#define atoi atoi2 |
#define printf UartPrintf |
//#define printf UartPrintfPoll |
#define scanf UartScanf |
#define malloc(S) OS_HeapMalloc(NULL, S) |
#define free(S) OS_HeapFree(S) |
#define NULL (void*)0 |
|
int atoi(const char *s); |
int rand(void); |
void srand(unsigned int seed); |
long strtol(const char *s, char **end, int base); |
int atoi(const char *s); |
char *itoa(int num, char *dst, int base); |
|
#ifndef NO_ELLIPSIS |
104,6 → 101,15
int sprintf(char *s, const char *format, ...); |
int sscanf(const char *s, const char *format, ...); |
#endif |
|
#ifndef _LIBC |
#define assert(A) if((A)==0){OS_Assert();UartPrintfCritical("\r\nAssert %s:%d\r\n", __FILE__, __LINE__);} |
#define printf UartPrintf |
//#define printf UartPrintfPoll |
#define scanf UartScanf |
#define NULL (void*)0 |
#endif //_LIBC |
|
#ifdef INCLUDE_DUMP |
void dump(const unsigned char *data, int length); |
#endif |
137,7 → 143,6
void gmtimeDst(time_t dstTimeIn, time_t dstTimeOut); |
void gmtimeDstSet(time_t *tp, time_t *dstTimeIn, time_t *dstTimeOut); |
#endif |
#endif //_LIBC |
|
/***************** Assembly **************/ |
typedef uint32 jmp_buf[20]; |
149,11 → 154,11
extern void *OS_Syscall(uint32 value); |
|
/***************** Heap ******************/ |
#define HEAP_USER (void*)0 |
#define HEAP_SYSTEM (void*)1 |
#define HEAP_SMALL (void*)2 |
#define HEAP_UI (void*)3 |
typedef struct OS_Heap_s OS_Heap_t; |
#define HEAP_USER (OS_Heap_t*)0 |
#define HEAP_SYSTEM (OS_Heap_t*)1 |
#define HEAP_SMALL (OS_Heap_t*)2 |
#define HEAP_UI (OS_Heap_t*)3 |
OS_Heap_t *OS_HeapCreate(const char *name, void *memory, uint32 size); |
void OS_HeapDestroy(OS_Heap_t *heap); |
void *OS_HeapMalloc(OS_Heap_t *heap, int bytes); |
261,37 → 266,11
|
/***************** Init ******************/ |
void OS_Init(uint32 *heapStorage, uint32 bytes); |
void OS_InitSimulation(void); |
void OS_Start(void); |
void OS_Assert(void); |
void OS_DebuggerInit(void); |
void MainThread(void *Arg); |
|
/***************** MMU ******************/ |
typedef struct { |
const char *name; |
OS_FuncPtr_t funcPtr; |
void *arg; |
uint32 priority; |
uint32 stackSize; |
uint32 heapSize; |
uint32 processId; |
OS_Semaphore_t *semaphoreDone; |
uint8 *memory; //private |
OS_Heap_t *heap; //private |
OS_Thread_t *thread; //private |
} OS_Process_t; |
void OS_MMUInit(void); |
void OS_MMUMemoryRegister(uint32 processId, |
uint32 virtualAddress, |
uint32 physicalAddress, |
uint32 size, |
uint32 writable); |
OS_Process_t *OS_MMUProcessCreate(OS_Process_t *process); |
void OS_MMUProcessDelete(OS_Process_t *process); |
void OS_MMUUartPrintf(void); |
void OS_MMUUartScanf(void); |
void OS_MMUUartPrintfCritical(void); |
|
/***************** UART ******************/ |
typedef uint8* (*PacketGetFunc_t)(void); |
void UartInit(void); |