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

Subversion Repositories plasma

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

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

powered by: WebSVN 2.1.0

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