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

Subversion Repositories plasma

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

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
#ifndef WIN32
404
static void MySyscall(void *arg)
405
{
406
   uint32 *stack = arg;
407
   stack[STACK_EPC] += 4;  //skip over SYSCALL
408
   printf("Inside MySyscall %d\n", stack[28/4]);
409
}
410
 
411
void TestSyscall(void)
412
{
413
   OS_InterruptRegister((uint32)(1<<31), MySyscall);
414
   OS_Syscall(57);
415
   OS_ThreadSleep(1);
416
   printf("Done\n");
417
}
418
#endif
419
 
420 190 rhoads
#ifdef __MMU_ENUM_H__
421
void TestProcess(void)
422
{
423
   OS_Process_t *process;
424
   process = (OS_Process_t*)OS_HeapMalloc(NULL, sizeof(OS_Process_t));
425
   process->name = "test";
426
   process->funcPtr = MainThread;
427
   process->arg = NULL;
428
   process->priority = 200;
429
   process->stackSize = 1024*32;
430
   process->heapSize = 1024*128;
431
   process->processId = 1;
432
   process->semaphoreDone = OS_SemaphoreCreate("processDone", 0);
433
   printf("Creating process\n");
434
   OS_MMUProcessCreate(process);
435
   OS_SemaphorePend(process->semaphoreDone, OS_WAIT_FOREVER);
436
   printf("Process done\n");
437
   OS_MMUProcessDelete(process);
438
}
439
#endif
440 138 rhoads
 
441 190 rhoads
 
442 138 rhoads
//******************************************************************
443 190 rhoads
void MMUTest(void);
444 221 rhoads
void HtmlThread(void *arg);
445
void ConsoleInit(void);
446 336 rhoads
uint8 macAddress[] =  {0x00, 0x10, 0xdd, 0xce, 0x15, 0xd4};
447 221 rhoads
 
448 298 rhoads
 
449 138 rhoads
void MainThread(void *Arg)
450
{
451 283 rhoads
   int ch, i, display=1;
452 138 rhoads
   (void)Arg;
453 190 rhoads
#ifdef __MMU_ENUM_H__
454
   OS_MMUInit();
455
#endif
456 138 rhoads
 
457 298 rhoads
#ifdef INCLUDE_ETH
458
   EthernetInit(macAddress);
459
   IPInit(EthernetTransmit, macAddress, "plasma");
460
   HtmlInit(1);
461
#endif
462
 
463 221 rhoads
#ifdef INCLUDE_HTML
464 298 rhoads
   IPInit(NULL, macAddress, "plasma");
465 235 rhoads
   HtmlInit(1);
466 221 rhoads
#endif
467
 
468
#ifdef INCLUDE_CONSOLE
469
   ConsoleInit();
470
#endif
471
 
472 138 rhoads
   for(;;)
473
   {
474 283 rhoads
      if(display)
475
      {
476
         printf("\n");
477
         printf("1 CLib\n");
478
         printf("2 Heap\n");
479
         printf("3 Thread\n");
480
         printf("4 Semaphore\n");
481
         printf("5 Mutex\n");
482
         printf("6 MQueue\n");
483
         printf("7 Timer\n");
484
         printf("8 Math\n");
485
         printf("9 Syscall\n");
486 190 rhoads
#ifdef __MMU_ENUM_H__
487 283 rhoads
         printf("p MMU Process\n");
488 190 rhoads
#endif
489 283 rhoads
      }
490 138 rhoads
      printf("> ");
491 283 rhoads
      display = 1;
492 138 rhoads
      ch = UartRead();
493
      printf("%c\n", ch);
494
      switch(ch)
495
      {
496 255 rhoads
#ifdef WIN32
497
      case '0': exit(0);
498
#endif
499 138 rhoads
      case '1': TestCLib(); break;
500
      case '2': TestHeap(); break;
501
      case '3': TestThread(); break;
502
      case '4': TestSemaphore(); break;
503
      case '5': TestMutex(); break;
504
      case '6': TestMQueue(); break;
505
      case '7': TestTimer(); break;
506
      case '8': TestMath(); break;
507 190 rhoads
#ifndef WIN32
508 194 rhoads
      case '9': TestSyscall(); break;
509 190 rhoads
#endif
510
#ifdef __MMU_ENUM_H__
511
      case 'p': TestProcess(); break;
512
#endif
513 138 rhoads
#ifdef WIN32
514
      case 'm': TestMathFull(); break;
515
#endif
516 190 rhoads
      case 'g': printf("Global=%d\n", ++Global); break;
517 235 rhoads
      default:
518 283 rhoads
         printf("E");
519
         display = 0;
520 235 rhoads
         for(i = 0; i < 30; ++i)
521
         {
522 275 rhoads
            while(kbhit())
523 235 rhoads
               ch = UartRead();
524 275 rhoads
            OS_ThreadSleep(1);
525 235 rhoads
         }
526
         break;
527 138 rhoads
      }
528
   }
529
}
530
 
531
 

powered by: WebSVN 2.1.0

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