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

Subversion Repositories mlite

[/] [mlite/] [trunk/] [kernel/] [rtos_test.c] - Blame information for rev 235

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

Line No. Rev Author Line
1 138 rhoads
/*--------------------------------------------------------------------
2
 * TITLE: Test Plasma Real Time Operating System
3
 * AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
4
 * DATE CREATED: 1/1/06
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.
9
 * DESCRIPTION:
10
 *    Test Plasma Real Time Operating System
11
 *--------------------------------------------------------------------*/
12
#include "plasma.h"
13
#include "rtos.h"
14 226 rhoads
#include "tcpip.h"
15 138 rhoads
 
16 190 rhoads
/* Including mmu.h will cause all OS calls to use SYSCALL */
17
//#include "mmu.h"
18
 
19
#define SEMAPHORE_COUNT 50
20
#define TIMER_COUNT     10
21
 
22 138 rhoads
extern void TestMathFull(void);
23
 
24 190 rhoads
typedef struct {
25
   OS_Thread_t *MyThread[TIMER_COUNT];
26
   OS_Semaphore_t *MySemaphore[SEMAPHORE_COUNT];
27
   OS_Mutex_t *MyMutex;
28
   OS_Timer_t *MyTimer[TIMER_COUNT];
29
   OS_MQueue_t *MyQueue[TIMER_COUNT];
30
   int TimerDone;
31
} TestInfo_t;
32
 
33
int Global;
34
 
35 138 rhoads
//******************************************************************
36
static void TestCLib(void)
37
{
38
   char s1[80], s2[80], *ptr;
39
   int rc, v1, v2, v3;
40
 
41
   printf("TestCLib\n");
42
   strcpy(s1, "Hello ");
43
   strncpy(s2, "World wide", 5);
44
   strcat(s1, s2);
45
   strncat(s1, "!\nthing", 14);
46
   printf("%s", s1);
47
   rc = strcmp(s1, "Hello World!\n");
48
   assert(rc == 0);
49
   rc = strcmp(s1, "Hello WOrld!\n");
50
   assert(rc > 0);
51
   rc = strcmp(s1, "Hello world!\n");
52
   assert(rc < 0);
53
   rc = strncmp(s1, "Hellx", 4);
54
   assert(rc == 0);
55
   ptr = strstr(s1, "orl");
56
   assert(ptr[0] = 'o');
57
   rc = strlen(s1);
58
   assert(rc == 13);
59
   memcpy(s2, s1, rc+1);
60
   rc = memcmp(s1, s2, 8);
61
   assert(rc == 0);
62
   s2[5] = 'z';
63
   rc = memcmp(s1, s2, 8);
64
   assert(rc != 0);
65
   memset(s2, 0, 5);
66
   memset(s2, 'a', 3);
67
   rc = abs(-5);
68 154 rhoads
   itoa(1234, s1, 10);
69
   itoa(0, s1, 10);
70
   itoa(-1234, s1, 10);
71
   itoa(0xabcd, s1, 16);
72
   itoa(0x12ab, s1, 16);
73
   sprintf(s1, "test c%c d%d 0x%x s%s End\n", 'C', 1234, 0xabcd, "String");
74 138 rhoads
   printf("%s", s1);
75 154 rhoads
   sprintf(s1, "test c%c d%6d 0x%6x s%8s End\n", 'C', 1234, 0xabcd, "String");
76 138 rhoads
   printf("%s", s1);
77
   sscanf("1234 -1234 0xabcd text h", "%d %d %x %s", &v1, &v2, &v3, s1);
78
   assert(v1 == 1234 && v2 == -1234 && v3 == 0xabcd);
79
   assert(strcmp(s1, "text") == 0);
80
   //UartScanf("%d %d", &v1, &v2);
81
   //printf("v1 = %d v2 = %d\n", v1, v2);
82
   printf("Done.\n");
83
}
84
 
85
//******************************************************************
86
static void TestHeap(void)
87
{
88
   uint8 *ptrs[256], size[256], *ptr;
89
   int i, j, k, value;
90
 
91
   printf("TestHeap\n");
92
   memset(ptrs, 0, sizeof(ptrs));
93
   for(i = 0; i < 1000; ++i)
94
   {
95
      j = rand() & 255;
96
      if(ptrs[j])
97
      {
98
         ptr = ptrs[j];
99
         value = size[j];
100
         for(k = 0; k < value; ++k)
101
         {
102
            if(ptr[k] != value)
103
               printf("Error\n");
104
         }
105
         OS_HeapFree(ptrs[j]);
106
      }
107
      size[j] = (uint8)(rand() & 255);
108
      ptrs[j] = OS_HeapMalloc(NULL, size[j]);
109 190 rhoads
      if(ptrs[j] == NULL)
110
         printf("malloc NULL\n");
111
      else
112
         memset(ptrs[j], size[j], size[j]);
113 138 rhoads
   }
114
   for(i = 0; i < 256; ++i)
115
   {
116
      if(ptrs[i])
117
         OS_HeapFree(ptrs[i]);
118
   }
119
   printf("Done.\n");
120
}
121
 
122
//******************************************************************
123 190 rhoads
static void MyThreadMain(void *arg)
124 138 rhoads
{
125
   OS_Thread_t *thread;
126
   int priority;
127
 
128
   thread = OS_ThreadSelf();
129
   priority = OS_ThreadPriorityGet(thread);
130
   OS_ThreadSleep(10);
131
   printf("Arg=%d thread=0x%x info=0x%x priority=%d\n",
132 190 rhoads
      (uint32)arg, thread, OS_ThreadInfoGet(thread), priority);
133
   OS_ThreadExit();
134 138 rhoads
}
135
 
136
static void TestThread(void)
137
{
138
   OS_Thread_t *thread;
139
   int i, priority;
140
 
141
   printf("TestThread\n");
142
   for(i = 0; i < 32; ++i)
143
   {
144
      priority = 50 + i;
145
      thread = OS_ThreadCreate("MyThread", MyThreadMain, (uint32*)i, priority, 0);
146
      OS_ThreadInfoSet(thread, (void*)(0xabcd + i));
147
      //printf("Created thread 0x%x\n", thread);
148
   }
149
 
150
   thread = OS_ThreadSelf();
151
   priority = OS_ThreadPriorityGet(thread);
152
   printf("Priority = %d\n", priority);
153
   OS_ThreadPrioritySet(thread, 200);
154
   printf("Priority = %d\n", OS_ThreadPriorityGet(thread));
155
   OS_ThreadPrioritySet(thread, priority);
156
 
157
   printf("Thread time = %d\n", OS_ThreadTime());
158
   OS_ThreadSleep(100);
159
   printf("Thread time = %d\n", OS_ThreadTime());
160
}
161
 
162
//******************************************************************
163 190 rhoads
static void TestSemThread(void *arg)
164 138 rhoads
{
165
   int i;
166 190 rhoads
   TestInfo_t *info = (TestInfo_t*)arg;
167 138 rhoads
 
168 190 rhoads
   for(i = 0; i < SEMAPHORE_COUNT/2; ++i)
169 138 rhoads
   {
170
      printf("s");
171 190 rhoads
      OS_SemaphorePend(info->MySemaphore[i], OS_WAIT_FOREVER);
172
      OS_SemaphorePost(info->MySemaphore[i + SEMAPHORE_COUNT/2]);
173 138 rhoads
   }
174 190 rhoads
   OS_ThreadExit();
175 138 rhoads
}
176
 
177
static void TestSemaphore(void)
178
{
179
   int i, rc;
180 190 rhoads
   TestInfo_t info;
181 138 rhoads
   printf("TestSemaphore\n");
182 190 rhoads
   for(i = 0; i < SEMAPHORE_COUNT; ++i)
183 138 rhoads
   {
184 190 rhoads
      info.MySemaphore[i] = OS_SemaphoreCreate("MySem", 0);
185 138 rhoads
      //printf("sem[%d]=0x%x\n", i, MySemaphore[i]);
186
   }
187
 
188 190 rhoads
   OS_ThreadCreate("TestSem", TestSemThread, &info, 50, 0);
189 138 rhoads
 
190 190 rhoads
   for(i = 0; i < SEMAPHORE_COUNT/2; ++i)
191 138 rhoads
   {
192
      printf("S");
193 190 rhoads
      OS_SemaphorePost(info.MySemaphore[i]);
194
      rc = OS_SemaphorePend(info.MySemaphore[i + SEMAPHORE_COUNT/2], 500);
195 138 rhoads
      assert(rc == 0);
196
   }
197
 
198
   printf(":");
199 190 rhoads
   rc = OS_SemaphorePend(info.MySemaphore[0], 10);
200 138 rhoads
   assert(rc != 0);
201
   printf(":");
202 190 rhoads
   OS_SemaphorePend(info.MySemaphore[0], 100);
203 138 rhoads
   printf(":");
204
 
205 190 rhoads
   for(i = 0; i < SEMAPHORE_COUNT; ++i)
206
      OS_SemaphoreDelete(info.MySemaphore[i]);
207 138 rhoads
 
208
   printf("\nDone.\n");
209
}
210
 
211
//******************************************************************
212 190 rhoads
static void TestMutexThread(void *arg)
213 138 rhoads
{
214 190 rhoads
   TestInfo_t *info = (TestInfo_t*)arg;
215 138 rhoads
 
216
   printf("Waiting for mutex\n");
217 190 rhoads
   OS_MutexPend(info->MyMutex);
218 138 rhoads
   printf("Have Mutex1\n");
219 190 rhoads
   OS_MutexPend(info->MyMutex);
220 138 rhoads
   printf("Have Mutex2\n");
221 190 rhoads
   OS_MutexPend(info->MyMutex);
222 138 rhoads
   printf("Have Mutex3\n");
223
 
224
   OS_ThreadSleep(100);
225
 
226 190 rhoads
   OS_MutexPost(info->MyMutex);
227
   OS_MutexPost(info->MyMutex);
228
   OS_MutexPost(info->MyMutex);
229
 
230
   OS_ThreadExit();
231 138 rhoads
}
232
 
233
static void TestMutex(void)
234
{
235 190 rhoads
   TestInfo_t info;
236 138 rhoads
   printf("TestMutex\n");
237 190 rhoads
   info.MyMutex = OS_MutexCreate("MyMutex");
238
   OS_MutexPend(info.MyMutex);
239
   OS_MutexPend(info.MyMutex);
240
   OS_MutexPend(info.MyMutex);
241 138 rhoads
 
242 190 rhoads
   OS_ThreadSleep(100);
243 138 rhoads
 
244 190 rhoads
   OS_ThreadCreate("TestMutex", TestMutexThread, &info, 50, 0);
245
 
246 138 rhoads
   OS_ThreadSleep(50);
247 190 rhoads
   OS_MutexPost(info.MyMutex);
248
   OS_MutexPost(info.MyMutex);
249
   OS_MutexPost(info.MyMutex);
250 138 rhoads
 
251
   printf("Try get mutex\n");
252 190 rhoads
   OS_MutexPend(info.MyMutex);
253 138 rhoads
   printf("Gotit\n");
254
 
255 190 rhoads
   OS_MutexDelete(info.MyMutex);
256 138 rhoads
   printf("Done.\n");
257
}
258
 
259
//******************************************************************
260
static void TestMQueue(void)
261
{
262
   OS_MQueue_t *mqueue;
263
   char data[16];
264
   int i, rc;
265
 
266
   printf("TestMQueue\n");
267
   mqueue = OS_MQueueCreate("MyMQueue", 10, 16);
268
   strcpy(data, "Test0");
269
   for(i = 0; i < 16; ++i)
270
   {
271
      data[4] = (char)('0' + i);
272
      OS_MQueueSend(mqueue, data);
273
   }
274
   for(i = 0; i < 16; ++i)
275
   {
276
      memset(data, 0, sizeof(data));
277
      rc = OS_MQueueGet(mqueue, data, 20);
278
      if(rc == 0)
279
         printf("message=(%s)\n", data);
280
      else
281
         printf("timeout\n");
282
   }
283
 
284
   OS_MQueueDelete(mqueue);
285
   printf("Done.\n");
286
}
287
 
288
//******************************************************************
289 190 rhoads
static void TestTimerThread(void *arg)
290 138 rhoads
{
291 190 rhoads
   int index;
292 138 rhoads
   uint32 data[4];
293
   OS_Timer_t *timer;
294 190 rhoads
   TestInfo_t *info = (TestInfo_t*)arg;
295 138 rhoads
 
296 190 rhoads
   //printf("TestTimerThread\n");
297
 
298
   OS_ThreadSleep(1);
299
   index = (int)OS_ThreadInfoGet(OS_ThreadSelf());
300
   //printf("index=%d\n", index);
301
   OS_MQueueGet(info->MyQueue[index], data, 1000);
302 138 rhoads
   timer = (OS_Timer_t*)data[1];
303
   printf("%d ", data[2]);
304 190 rhoads
   OS_MQueueGet(info->MyQueue[index], data, 1000);
305 138 rhoads
   printf("%d ", data[2]);
306 190 rhoads
   ++info->TimerDone;
307
   OS_ThreadExit();
308 138 rhoads
}
309
 
310
static void TestTimer(void)
311
{
312
   int i;
313 190 rhoads
   TestInfo_t info;
314 138 rhoads
 
315
   printf("TestTimer\n");
316 190 rhoads
   info.TimerDone = 0;
317 138 rhoads
   for(i = 0; i < TIMER_COUNT; ++i)
318
   {
319 190 rhoads
      info.MyQueue[i] = OS_MQueueCreate("MyQueue", 10, 16);
320
      info.MyTimer[i] = OS_TimerCreate("MyTimer", info.MyQueue[i], i);
321
      info.MyThread[i] = OS_ThreadCreate("TimerTest", TestTimerThread, &info, 50, 0);
322
      OS_ThreadInfoSet(info.MyThread[i], (void*)i);
323
      OS_TimerStart(info.MyTimer[i], 10+i*2, 220+i);
324 138 rhoads
   }
325
 
326 190 rhoads
   while(info.TimerDone < TIMER_COUNT)
327 138 rhoads
      OS_ThreadSleep(10);
328
 
329
   for(i = 0; i < TIMER_COUNT; ++i)
330
   {
331 190 rhoads
      OS_MQueueDelete(info.MyQueue[i]);
332
      OS_TimerDelete(info.MyTimer[i]);
333 138 rhoads
   }
334
 
335
   printf("Done.\n");
336
}
337
 
338
//******************************************************************
339
#if 1
340
void TestMath(void)
341
{
342
   int i;
343
   float a, b, sum, diff, mult, div;
344
   uint32 compare;
345
 
346
   //Check add subtract multiply and divide
347
   for(i = -4; i < 4; ++i)
348
   {
349
      a = (float)(i * 10 + (float)63.2);
350
      b = (float)(-i * 5 + (float)3.5);
351
      sum = a + b;
352
      diff = a - b;
353
      mult = a * b;
354
      div = a / b;
355
      printf("a=%dE-3 b=%dE-3 sum=%dE-3 diff=%dE-3 mult=%dE-3 div=%dE-3\n",
356
         (int)(a*(float)1000), (int)(b*(float)1000),
357
         (int)(sum*(float)1000), (int)(diff*(float)1000),
358
         (int)(mult*(float)1000), (int)(div*(float)1000));
359
   }
360
 
361
   //Comparisons
362
   b = (float)2.0;
363
   compare = 0;
364
   for(i = 1; i < 4; ++i)
365
   {
366
      a = (float)i;
367
      compare = (compare << 1) | (a == b);
368
      compare = (compare << 1) | (a != b);
369
      compare = (compare << 1) | (a <  b);
370
      compare = (compare << 1) | (a <= b);
371
      compare = (compare << 1) | (a >  b);
372
      compare = (compare << 1) | (a >= b);
373
   }
374
   printf("Compare = %8x %s\n", compare,
375
      compare==0x1c953 ? "OK" : "ERROR");
376
 
377
   //Cosine
378
   for(a = (float)0.0; a <= (float)(3.1415); a += (float)(3.1415/16.0))
379
   {
380
      b = FP_Cos(a);
381
      printf("cos(%4dE-3) = %4dE-3\n",
382
         (int)(a*(float)1000.0), (int)(b*(float)1000.0));
383
   }
384
}
385
#endif
386
 
387 194 rhoads
//******************************************************************
388
#ifndef WIN32
389
static void MySyscall(void *arg)
390
{
391
   uint32 *stack = arg;
392
   stack[STACK_EPC] += 4;  //skip over SYSCALL
393
   printf("Inside MySyscall %d\n", stack[28/4]);
394
}
395
 
396
void TestSyscall(void)
397
{
398
   OS_InterruptRegister((uint32)(1<<31), MySyscall);
399
   OS_Syscall(57);
400
   OS_ThreadSleep(1);
401
   printf("Done\n");
402
}
403
#endif
404
 
405 190 rhoads
#ifdef __MMU_ENUM_H__
406
void TestProcess(void)
407
{
408
   OS_Process_t *process;
409
   process = (OS_Process_t*)OS_HeapMalloc(NULL, sizeof(OS_Process_t));
410
   process->name = "test";
411
   process->funcPtr = MainThread;
412
   process->arg = NULL;
413
   process->priority = 200;
414
   process->stackSize = 1024*32;
415
   process->heapSize = 1024*128;
416
   process->processId = 1;
417
   process->semaphoreDone = OS_SemaphoreCreate("processDone", 0);
418
   printf("Creating process\n");
419
   OS_MMUProcessCreate(process);
420
   OS_SemaphorePend(process->semaphoreDone, OS_WAIT_FOREVER);
421
   printf("Process done\n");
422
   OS_MMUProcessDelete(process);
423
}
424
#endif
425 138 rhoads
 
426 190 rhoads
 
427 138 rhoads
//******************************************************************
428 190 rhoads
void MMUTest(void);
429 221 rhoads
void HtmlThread(void *arg);
430
void ConsoleInit(void);
431
 
432 138 rhoads
void MainThread(void *Arg)
433
{
434 235 rhoads
   int ch, i;
435 138 rhoads
   (void)Arg;
436 190 rhoads
#ifdef __MMU_ENUM_H__
437
   OS_MMUInit();
438
#endif
439 138 rhoads
 
440 221 rhoads
#ifdef INCLUDE_HTML
441 226 rhoads
   IPInit(NULL);
442 235 rhoads
   HtmlInit(1);
443 221 rhoads
#endif
444
 
445
#ifdef INCLUDE_CONSOLE
446
   ConsoleInit();
447
#endif
448
 
449 138 rhoads
   for(;;)
450
   {
451
      printf("\n");
452
      printf("1 CLib\n");
453
      printf("2 Heap\n");
454
      printf("3 Thread\n");
455
      printf("4 Semaphore\n");
456
      printf("5 Mutex\n");
457
      printf("6 MQueue\n");
458
      printf("7 Timer\n");
459
      printf("8 Math\n");
460 194 rhoads
      printf("9 Syscall\n");
461 190 rhoads
#ifdef __MMU_ENUM_H__
462
      printf("p MMU Process\n");
463
#endif
464 138 rhoads
      printf("> ");
465
      ch = UartRead();
466
      printf("%c\n", ch);
467
      switch(ch)
468
      {
469
      case '1': TestCLib(); break;
470
      case '2': TestHeap(); break;
471
      case '3': TestThread(); break;
472
      case '4': TestSemaphore(); break;
473
      case '5': TestMutex(); break;
474
      case '6': TestMQueue(); break;
475
      case '7': TestTimer(); break;
476
      case '8': TestMath(); break;
477 190 rhoads
#ifndef WIN32
478 194 rhoads
      case '9': TestSyscall(); break;
479 190 rhoads
#endif
480
#ifdef __MMU_ENUM_H__
481
      case 'p': TestProcess(); break;
482
#endif
483 138 rhoads
#ifdef WIN32
484
      case 'm': TestMathFull(); break;
485
#endif
486 190 rhoads
      case 'g': printf("Global=%d\n", ++Global); break;
487 235 rhoads
      default:
488
         printf("Error");
489
         for(i = 0; i < 30; ++i)
490
         {
491
            if(kbhit())
492
               ch = UartRead();
493
            else
494
               OS_ThreadSleep(1);
495
         }
496
         break;
497 138 rhoads
      }
498
   }
499
}
500
 
501
 

powered by: WebSVN 2.1.0

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