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

Subversion Repositories mlite

[/] [mlite/] [trunk/] [kernel/] [rtos_test.c] - Blame information for rev 194

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

powered by: WebSVN 2.1.0

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