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

Subversion Repositories mlite

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

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 190 rhoads
#ifdef __MMU_ENUM_H__
387
void TestProcess(void)
388
{
389
   OS_Process_t *process;
390
   process = (OS_Process_t*)OS_HeapMalloc(NULL, sizeof(OS_Process_t));
391
   process->name = "test";
392
   process->funcPtr = MainThread;
393
   process->arg = NULL;
394
   process->priority = 200;
395
   process->stackSize = 1024*32;
396
   process->heapSize = 1024*128;
397
   process->processId = 1;
398
   process->semaphoreDone = OS_SemaphoreCreate("processDone", 0);
399
   printf("Creating process\n");
400
   OS_MMUProcessCreate(process);
401
   OS_SemaphorePend(process->semaphoreDone, OS_WAIT_FOREVER);
402
   printf("Process done\n");
403
   OS_MMUProcessDelete(process);
404
}
405
#endif
406 138 rhoads
 
407 190 rhoads
 
408 138 rhoads
//******************************************************************
409 190 rhoads
void MMUTest(void);
410 138 rhoads
void MainThread(void *Arg)
411
{
412
   int ch;
413
   (void)Arg;
414 190 rhoads
#ifdef __MMU_ENUM_H__
415
   OS_MMUInit();
416
#endif
417 138 rhoads
 
418
   for(;;)
419
   {
420
      printf("\n");
421
      printf("0 Exit\n");
422
      printf("1 CLib\n");
423
      printf("2 Heap\n");
424
      printf("3 Thread\n");
425
      printf("4 Semaphore\n");
426
      printf("5 Mutex\n");
427
      printf("6 MQueue\n");
428
      printf("7 Timer\n");
429
      printf("8 Math\n");
430 190 rhoads
      //printf("9 Debugger\n");
431
#ifdef __MMU_ENUM_H__
432
      printf("p MMU Process\n");
433
#endif
434 138 rhoads
      printf("> ");
435
      ch = UartRead();
436
      printf("%c\n", ch);
437
      switch(ch)
438
      {
439
      case '0':
440
#ifndef WIN32
441 190 rhoads
         //{
442
         //OS_FuncPtr_t funcPtr=NULL;
443
         //OS_CriticalBegin();
444
         //funcPtr(NULL);
445
         //}
446 138 rhoads
#endif
447
         return;
448
      case '1': TestCLib(); break;
449
      case '2': TestHeap(); break;
450
      case '3': TestThread(); break;
451
      case '4': TestSemaphore(); break;
452
      case '5': TestMutex(); break;
453
      case '6': TestMQueue(); break;
454
      case '7': TestTimer(); break;
455
      case '8': TestMath(); break;
456 190 rhoads
#ifndef WIN32
457
      //case '9': OS_DebuggerInit(); break;
458
#endif
459
#ifdef __MMU_ENUM_H__
460
      case 'p': TestProcess(); break;
461
#endif
462 138 rhoads
#ifdef WIN32
463
      case 'm': TestMathFull(); break;
464
#endif
465 190 rhoads
      case 'g': printf("Global=%d\n", ++Global); break;
466 138 rhoads
      }
467
   }
468
}
469
 
470
 

powered by: WebSVN 2.1.0

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