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

Subversion Repositories plasma

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

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

powered by: WebSVN 2.1.0

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