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

Subversion Repositories mlite

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

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

powered by: WebSVN 2.1.0

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