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

Subversion Repositories plasma

[/] [plasma/] [trunk/] [kernel/] [rtos_test.c] - Blame information for rev 411

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

powered by: WebSVN 2.1.0

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