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

Subversion Repositories plasma

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

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

powered by: WebSVN 2.1.0

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