1 /*--------------------------------------------------------------------
2 * TITLE: Test Plasma Real Time Operating System
3 * AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
5 * FILENAME: rtos_test.c
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.
10 * Test Plasma Real Time Operating System
11 *--------------------------------------------------------------------*/
16 /* Including mmu.h will cause all OS calls to use SYSCALL */
23 #define SEMAPHORE_COUNT 50
24 #define TIMER_COUNT 10
26 extern void TestMathFull(void);
29 OS_Thread_t *MyThread[TIMER_COUNT];
30 OS_Semaphore_t *MySemaphore[SEMAPHORE_COUNT];
32 OS_Timer_t *MyTimer[TIMER_COUNT];
33 OS_MQueue_t *MyQueue[TIMER_COUNT];
39 //******************************************************************
40 static void TestCLib(void)
42 char s1[80], s2[80], *ptr;
47 memset(s2, 0, sizeof(s2));
48 strncpy(s2, "World wide", 5);
50 strncat(s1, "!\nthing", 14);
52 rc = strcmp(s1, "Hello World!\n");
54 rc = strcmp(s1, "Hello WOrld!\n");
56 rc = strcmp(s1, "Hello world!\n");
58 rc = strncmp(s1, "Hellx", 4);
60 ptr = strstr(s1, "orl");
65 rc = memcmp(s1, s2, 8);
68 rc = memcmp(s1, s2, 8);
78 sprintf(s1, "test c%c d%d 0x%x s%s End\n", 'C', 1234, 0xabcd, "String");
80 sprintf(s1, "test c%c d%6d 0x%6x s%8s End\n", 'C', 1234, 0xabcd, "String");
82 sscanf("1234 -1234 0xabcd text h", "%d %d %x %s", &v1, &v2, &v3, s1);
83 assert(v1 == 1234 && v2 == -1234 && v3 == 0xabcd);
84 assert(strcmp(s1, "text") == 0);
85 //UartScanf("%d %d", &v1, &v2);
86 //printf("v1 = %d v2 = %d\n", v1, v2);
90 //******************************************************************
91 static void TestHeap(void)
93 uint8 *ptrs[256], size[256], *ptr;
97 memset(ptrs, 0, sizeof(ptrs));
98 for(i = 0; i < 1000; ++i)
105 for(k = 0; k < value; ++k)
110 OS_HeapFree(ptrs[j]);
112 size[j] = (uint8)(rand() & 255);
113 ptrs[j] = OS_HeapMalloc(NULL, size[j]);
115 printf("malloc NULL\n");
117 memset(ptrs[j], size[j], size[j]);
119 for(i = 0; i < 256; ++i)
122 OS_HeapFree(ptrs[i]);
127 //******************************************************************
128 static void MyThreadMain(void *arg)
133 thread = OS_ThreadSelf();
134 priority = OS_ThreadPriorityGet(thread);
136 printf("Arg=%d thread=0x%x info=0x%x priority=%d\n",
137 (uint32)arg, thread, OS_ThreadInfoGet(thread, 0), priority);
141 static void TestThread(void)
146 printf("TestThread\n");
147 for(i = 0; i < 32; ++i)
150 thread = OS_ThreadCreate("MyThread", MyThreadMain, (uint32*)i, priority, 0);
151 OS_ThreadInfoSet(thread, 0, (void*)(0xabcd + i));
152 //printf("Created thread 0x%x\n", thread);
155 thread = OS_ThreadSelf();
156 priority = OS_ThreadPriorityGet(thread);
157 printf("Priority = %d\n", priority);
158 OS_ThreadPrioritySet(thread, 200);
159 printf("Priority = %d\n", OS_ThreadPriorityGet(thread));
160 OS_ThreadPrioritySet(thread, priority);
162 printf("Thread time = %d\n", OS_ThreadTime());
164 printf("Thread time = %d\n", OS_ThreadTime());
167 //******************************************************************
168 static void TestSemThread(void *arg)
171 TestInfo_t *info = (TestInfo_t*)arg;
173 for(i = 0; i < SEMAPHORE_COUNT/2; ++i)
176 OS_SemaphorePend(info->MySemaphore[i], OS_WAIT_FOREVER);
177 OS_SemaphorePost(info->MySemaphore[i + SEMAPHORE_COUNT/2]);
182 static void TestSemaphore(void)
186 printf("TestSemaphore\n");
187 for(i = 0; i < SEMAPHORE_COUNT; ++i)
189 info.MySemaphore[i] = OS_SemaphoreCreate("MySem", 0);
190 //printf("sem[%d]=0x%x\n", i, MySemaphore[i]);
193 OS_ThreadCreate("TestSem", TestSemThread, &info, 50, 0);
195 for(i = 0; i < SEMAPHORE_COUNT/2; ++i)
198 OS_SemaphorePost(info.MySemaphore[i]);
199 rc = OS_SemaphorePend(info.MySemaphore[i + SEMAPHORE_COUNT/2], 500);
204 rc = OS_SemaphorePend(info.MySemaphore[0], 10);
207 OS_SemaphorePend(info.MySemaphore[0], 100);
210 for(i = 0; i < SEMAPHORE_COUNT; ++i)
211 OS_SemaphoreDelete(info.MySemaphore[i]);
216 //******************************************************************
217 static void TestMutexThread(void *arg)
219 TestInfo_t *info = (TestInfo_t*)arg;
221 printf("Waiting for mutex\n");
222 OS_MutexPend(info->MyMutex);
223 printf("Have Mutex1\n");
224 OS_MutexPend(info->MyMutex);
225 printf("Have Mutex2\n");
226 OS_MutexPend(info->MyMutex);
227 printf("Have Mutex3\n");
231 OS_MutexPost(info->MyMutex);
232 OS_MutexPost(info->MyMutex);
233 OS_MutexPost(info->MyMutex);
238 static void TestMutex(void)
241 printf("TestMutex\n");
242 info.MyMutex = OS_MutexCreate("MyMutex");
243 OS_MutexPend(info.MyMutex);
244 OS_MutexPend(info.MyMutex);
245 OS_MutexPend(info.MyMutex);
249 OS_ThreadCreate("TestMutex", TestMutexThread, &info, 50, 0);
252 OS_MutexPost(info.MyMutex);
253 OS_MutexPost(info.MyMutex);
254 OS_MutexPost(info.MyMutex);
256 printf("Try get mutex\n");
257 OS_MutexPend(info.MyMutex);
260 OS_MutexDelete(info.MyMutex);
264 //******************************************************************
265 static void TestMQueue(void)
271 printf("TestMQueue\n");
272 mqueue = OS_MQueueCreate("MyMQueue", 10, 16);
273 strcpy(data, "Test0");
274 for(i = 0; i < 16; ++i)
276 data[4] = (char)('0' + i);
277 OS_MQueueSend(mqueue, data);
279 for(i = 0; i < 16; ++i)
281 memset(data, 0, sizeof(data));
282 rc = OS_MQueueGet(mqueue, data, 20);
284 printf("message=(%s)\n", data);
289 OS_MQueueDelete(mqueue);
293 //******************************************************************
294 static void TestTimerThread(void *arg)
299 TestInfo_t *info = (TestInfo_t*)arg;
301 //printf("TestTimerThread\n");
304 index = (int)OS_ThreadInfoGet(OS_ThreadSelf(), 0);
305 //printf("index=%d\n", index);
306 OS_MQueueGet(info->MyQueue[index], data, 1000);
307 timer = (OS_Timer_t*)data[1];
308 printf("%d ", data[2]);
309 OS_MQueueGet(info->MyQueue[index], data, 1000);
310 printf("%d ", data[2]);
315 static void TestTimer(void)
320 printf("TestTimer\n");
322 for(i = 0; i < TIMER_COUNT; ++i)
324 info.MyQueue[i] = OS_MQueueCreate("MyQueue", 10, 16);
325 info.MyTimer[i] = OS_TimerCreate("MyTimer", info.MyQueue[i], i);
326 info.MyThread[i] = OS_ThreadCreate("TimerTest", TestTimerThread, &info, 50, 0);
327 OS_ThreadInfoSet(info.MyThread[i], 0, (void*)i);
328 OS_TimerStart(info.MyTimer[i], 10+i*2, 220+i);
331 while(info.TimerDone < TIMER_COUNT)
334 for(i = 0; i < TIMER_COUNT; ++i)
336 OS_MQueueDelete(info.MyQueue[i]);
337 OS_TimerDelete(info.MyTimer[i]);
343 //******************************************************************
348 float a, b, sum, diff, mult, div;
351 //Check add subtract multiply and divide
352 for(i = -4; i < 4; ++i)
354 a = (float)(i * 10 + (float)63.2);
355 b = (float)(-i * 5 + (float)3.5);
360 printf("a=%dE-3 b=%dE-3 sum=%dE-3 diff=%dE-3 mult=%dE-3 div=%dE-3\n",
361 (int)(a*(float)1000), (int)(b*(float)1000),
362 (int)(sum*(float)1000), (int)(diff*(float)1000),
363 (int)(mult*(float)1000), (int)(div*(float)1000));
369 for(i = 1; i < 4; ++i)
372 compare = (compare << 1) | (a == b);
373 compare = (compare << 1) | (a != b);
374 compare = (compare << 1) | (a < b);
375 compare = (compare << 1) | (a <= b);
376 compare = (compare << 1) | (a > b);
377 compare = (compare << 1) | (a >= b);
379 printf("Compare = %8x %s\n", compare,
380 compare==0x1c953 ? "OK" : "ERROR");
383 for(a = (float)0.0; a <= (float)(3.1415); a += (float)(3.1415/16.0))
386 printf("cos(%4dE-3) = %4dE-3\n",
387 (int)(a*(float)1000.0), (int)(b*(float)1000.0));
392 //******************************************************************
394 static void MySyscall(void *arg)
397 stack[STACK_EPC] += 4; //skip over SYSCALL
398 printf("Inside MySyscall %d\n", stack[28/4]);
401 void TestSyscall(void)
403 OS_InterruptRegister((uint32)(1<<31), MySyscall);
410 #ifdef __MMU_ENUM_H__
411 void TestProcess(void)
413 OS_Process_t *process;
414 process = (OS_Process_t*)OS_HeapMalloc(NULL, sizeof(OS_Process_t));
415 process->name = "test";
416 process->funcPtr = MainThread;
418 process->priority = 200;
419 process->stackSize = 1024*32;
420 process->heapSize = 1024*128;
421 process->processId = 1;
422 process->semaphoreDone = OS_SemaphoreCreate("processDone", 0);
423 printf("Creating process\n");
424 OS_MMUProcessCreate(process);
425 OS_SemaphorePend(process->semaphoreDone, OS_WAIT_FOREVER);
426 printf("Process done\n");
427 OS_MMUProcessDelete(process);
432 //******************************************************************
434 void HtmlThread(void *arg);
435 void ConsoleInit(void);
437 uint8 macAddress[] = {0x00, 0x10, 0xdd, 0xce, 0x15, 0xd4};
440 void MainThread(void *Arg)
442 int ch, i, display=1;
444 #ifdef __MMU_ENUM_H__
449 EthernetInit(macAddress);
450 IPInit(EthernetTransmit, macAddress, "plasma");
455 IPInit(NULL, macAddress, "plasma");
459 #ifdef INCLUDE_CONSOLE
470 printf("3 Thread\n");
471 printf("4 Semaphore\n");
473 printf("6 MQueue\n");
476 printf("9 Syscall\n");
477 #ifdef __MMU_ENUM_H__
478 printf("p MMU Process\n");
490 case '1': TestCLib(); break;
491 case '2': TestHeap(); break;
492 case '3': TestThread(); break;
493 case '4': TestSemaphore(); break;
494 case '5': TestMutex(); break;
495 case '6': TestMQueue(); break;
496 case '7': TestTimer(); break;
497 case '8': TestMath(); break;
499 case '9': TestSyscall(); break;
501 #ifdef __MMU_ENUM_H__
502 case 'p': TestProcess(); break;
505 case 'm': TestMathFull(); break;
507 case 'g': printf("Global=%d\n", ++Global); break;
511 for(i = 0; i < 30; ++i)