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

Subversion Repositories mlite

[/] [mlite/] [trunk/] [kernel/] [rtos.c] - Blame information for rev 218

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 138 rhoads
/*--------------------------------------------------------------------
2
 * TITLE: Plasma Real Time Operating System
3
 * AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
4
 * DATE CREATED: 12/17/05
5
 * FILENAME: rtos.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
 *    Plasma Real Time Operating System
11
 *    Fully pre-emptive RTOS with support for:
12
 *       Heaps, Threads, Semaphores, Mutexes, Message Queues, and Timers.
13
 *    This file tries to be hardware independent except for calls to:
14
 *       MemoryRead() and MemoryWrite() for interrupts.
15 167 rhoads
 *    Partial support for multiple CPUs using symmetric multiprocessing.
16 138 rhoads
 *--------------------------------------------------------------------*/
17
#include "plasma.h"
18
#include "rtos.h"
19
 
20
#define HEAP_MAGIC 0x1234abcd
21
#define THREAD_MAGIC 0x4321abcd
22
#define SEM_RESERVED_COUNT 2
23
#define HEAP_COUNT 8
24
 
25 167 rhoads
 
26 138 rhoads
/*************** Structures ***************/
27
#ifdef WIN32
28 189 rhoads
   #define setjmp _setjmp
29 138 rhoads
   //x86 registers
30
   typedef struct jmp_buf2 {
31
      uint32 Ebp, Ebx, Edi, Esi, sp, pc, extra[10];
32
   } jmp_buf2;
33 189 rhoads
#elif defined(ARM_CPU)
34
   //ARM registers
35
   typedef struct jmp_buf2 {
36
      uint32 r[13], sp, lr, pc, cpsr, extra[5];
37 218 rhoads
   } jmp_buf 2;
38 138 rhoads
#else  
39
   //Plasma registers
40
   typedef struct jmp_buf2 {
41
      uint32 s[9], gp, sp, pc;
42
   } jmp_buf2;
43
#endif
44
 
45
typedef struct HeapNode_s {
46
   struct HeapNode_s *next;
47
   int size;
48
} HeapNode_t;
49
 
50
struct OS_Heap_s {
51
   uint32 magic;
52
   const char *name;
53
   OS_Semaphore_t *semaphore;
54
   HeapNode_t *available;
55
   HeapNode_t base;
56
   struct OS_Heap_s *alternate;
57
};
58
//typedef struct OS_Heap_s OS_Heap_t;
59
 
60 168 rhoads
typedef enum {
61 176 rhoads
   THREAD_PEND    = 0,
62 168 rhoads
   THREAD_READY   = 1,
63
   THREAD_RUNNING = 2
64
} OS_ThreadState_e;
65
 
66 138 rhoads
struct OS_Thread_s {
67
   const char *name;
68 168 rhoads
   OS_ThreadState_e state;
69 189 rhoads
   int cpuIndex;
70 176 rhoads
   int cpuLock;
71 138 rhoads
   jmp_buf env;
72
   OS_FuncPtr_t funcPtr;
73
   void *arg;
74
   uint32 priority;
75
   uint32 ticksTimeout;
76
   void *info;
77
   OS_Semaphore_t *semaphorePending;
78
   int returnCode;
79 167 rhoads
   uint32 spinLocks;
80 189 rhoads
   uint32 processId;
81
   OS_Heap_t *heap;
82 138 rhoads
   struct OS_Thread_s *next, *prev;
83
   struct OS_Thread_s *nextTimeout, *prevTimeout;
84
   uint32 magic[1];
85
};
86
//typedef struct OS_Thread_s OS_Thread_t;
87
 
88
struct OS_Semaphore_s {
89
   const char *name;
90
   struct OS_Thread_s *threadHead;
91
   int count;
92
};
93
//typedef struct OS_Semaphore_s OS_Semaphore_t;
94
 
95
struct OS_Mutex_s {
96
   OS_Semaphore_t *semaphore;
97
   OS_Thread_t *thread;
98
   int count;
99
};
100
//typedef struct OS_Mutex_s OS_Mutex_t;
101
 
102
struct OS_MQueue_s {
103
   const char *name;
104
   OS_Semaphore_t *semaphore;
105
   int count, size, used, read, write;
106
};
107
//typedef struct OS_MQueue_s OS_MQueue_t;
108
 
109
struct OS_Timer_s {
110
   const char *name;
111
   struct OS_Timer_s *next, *prev;
112
   uint32 ticksTimeout;
113
   uint32 ticksRestart;
114
   int active;
115 218 rhoads
   OS_TimerFuncPtr_t callback;
116 138 rhoads
   OS_MQueue_t *mqueue;
117
   uint32 info;
118
};
119
//typedef struct OS_Timer_s OS_Timer_t;
120
 
121
 
122
/*************** Globals ******************/
123
static OS_Heap_t *HeapArray[HEAP_COUNT];
124 189 rhoads
static OS_Thread_t *ThreadCurrent[OS_CPU_COUNT];
125
static OS_Thread_t *ThreadHead;   //Linked list of threads sorted by priority
126
static OS_Thread_t *TimeoutHead;  //Linked list of threads sorted by timeout
127 138 rhoads
static int ThreadSwapEnabled;
128
static int ThreadNeedReschedule;
129
static uint32 ThreadTime;
130
static void *NeedToFree;
131
static OS_Semaphore_t SemaphoreReserved[SEM_RESERVED_COUNT];
132 189 rhoads
static OS_Semaphore_t *SemaphoreSleep;
133
static OS_Semaphore_t *SemaphoreLock;
134
static OS_Semaphore_t *SemaphoreTimer;
135 138 rhoads
static OS_Timer_t *TimerHead;     //Linked list of timers sorted by timeout
136
static OS_FuncPtr_t Isr[32];
137
static int InterruptInside;
138 200 rhoads
int InitStack[128];               //Used by boot.asm
139 138 rhoads
 
140
 
141
/***************** Heap *******************/
142
/******************************************/
143 189 rhoads
OS_Heap_t *OS_HeapCreate(const char *name, void *memory, uint32 size)
144 138 rhoads
{
145
   OS_Heap_t *heap;
146
 
147 189 rhoads
   assert(((uint32)memory & 3) == 0);
148
   heap = (OS_Heap_t*)memory;
149 138 rhoads
   heap->magic = HEAP_MAGIC;
150 189 rhoads
   heap->name = name;
151
   heap->semaphore = OS_SemaphoreCreate(name, 1);
152 138 rhoads
   heap->available = (HeapNode_t*)(heap + 1);
153
   heap->available->next = &heap->base;
154 189 rhoads
   heap->available->size = (size - sizeof(OS_Heap_t)) / sizeof(HeapNode_t);
155 138 rhoads
   heap->base.next = heap->available;
156
   heap->base.size = 0;
157
   return heap;
158
}
159
 
160
 
161
/******************************************/
162 189 rhoads
void OS_HeapDestroy(OS_Heap_t *heap)
163 138 rhoads
{
164 189 rhoads
   OS_SemaphoreDelete(heap->semaphore);
165 138 rhoads
}
166
 
167
 
168
/******************************************/
169
//Modified from K&R
170 189 rhoads
void *OS_HeapMalloc(OS_Heap_t *heap, int bytes)
171 138 rhoads
{
172
   HeapNode_t *node, *prevp;
173
   int nunits;
174
 
175 189 rhoads
   if(heap == NULL && OS_ThreadSelf())
176
      heap = OS_ThreadSelf()->heap;
177
   if((uint32)heap < HEAP_COUNT)
178
      heap = HeapArray[(int)heap];
179
   nunits = (bytes + sizeof(HeapNode_t) - 1) / sizeof(HeapNode_t) + 1;
180
   OS_SemaphorePend(heap->semaphore, OS_WAIT_FOREVER);
181
   prevp = heap->available;
182 138 rhoads
   for(node = prevp->next; ; prevp = node, node = node->next)
183
   {
184
      if(node->size >= nunits)       //Big enough?
185
      {
186
         if(node->size == nunits)    //Exactly
187
            prevp->next = node->next;
188
         else
189
         {                           //Allocate tail end
190
            node->size -= nunits;
191
            node += node->size;
192
            node->size = nunits;
193
         }
194 189 rhoads
         heap->available = prevp;
195
         node->next = (HeapNode_t*)heap;
196
         OS_SemaphorePost(heap->semaphore);
197 138 rhoads
         return (void*)(node + 1);
198
      }
199 189 rhoads
      if(node == heap->available)   //Wrapped around free list
200 138 rhoads
      {
201 189 rhoads
         OS_SemaphorePost(heap->semaphore);
202
         if(heap->alternate)
203
            return OS_HeapMalloc(heap->alternate, bytes);
204 138 rhoads
         return NULL;
205
      }
206
   }
207
}
208
 
209
 
210
/******************************************/
211
//Modified from K&R
212 189 rhoads
void OS_HeapFree(void *block)
213 138 rhoads
{
214
   OS_Heap_t *heap;
215
   HeapNode_t *bp, *node;
216
 
217 189 rhoads
   assert(block);
218
   bp = (HeapNode_t*)block - 1;   //point to block header
219 138 rhoads
   heap = (OS_Heap_t*)bp->next;
220
   assert(heap->magic == HEAP_MAGIC);
221
   if(heap->magic != HEAP_MAGIC)
222
      return;
223
   OS_SemaphorePend(heap->semaphore, OS_WAIT_FOREVER);
224
   for(node = heap->available; !(node < bp && bp < node->next); node = node->next)
225
   {
226
      if(node >= node->next && (bp > node || bp < node->next))
227
         break;               //freed block at start or end of area
228
   }
229
 
230
   if(bp + bp->size == node->next)   //join to upper
231
   {
232
      bp->size += node->next->size;
233
      bp->next = node->next->next;
234
   }
235
   else
236
   {
237
      bp->next = node->next;
238
   }
239
 
240
   if(node + node->size == bp)       //join to lower
241
   {
242
      node->size += bp->size;
243
      node->next = bp->next;
244
   }
245
   else
246
      node->next = bp;
247
   heap->available = node;
248
   OS_SemaphorePost(heap->semaphore);
249
}
250
 
251
 
252
/******************************************/
253 189 rhoads
void OS_HeapAlternate(OS_Heap_t *heap, OS_Heap_t *alternate)
254 138 rhoads
{
255 189 rhoads
   heap->alternate = alternate;
256 138 rhoads
}
257
 
258
 
259
/******************************************/
260 189 rhoads
void OS_HeapRegister(void *index, OS_Heap_t *heap)
261 138 rhoads
{
262 189 rhoads
   if((uint32)index < HEAP_COUNT)
263
      HeapArray[(int)index] = heap;
264 138 rhoads
}
265
 
266
 
267
 
268
/***************** Thread *****************/
269
/******************************************/
270
//Linked list of threads sorted by priority
271
//Must be called with interrupts disabled
272
static void OS_ThreadPriorityInsert(OS_Thread_t **head, OS_Thread_t *thread)
273
{
274
   OS_Thread_t *node, *prev;
275
 
276
   prev = NULL;
277
   for(node = *head; node; node = node->next)
278
   {
279
      if(node->priority <= thread->priority)
280
         break;
281
      prev = node;
282
   }
283
 
284
   if(prev == NULL)
285
   {
286
      thread->next = *head;
287
      thread->prev = NULL;
288
      *head = thread;
289
   }
290
   else
291
   {
292
      if(prev->next)
293
         prev->next->prev = thread;
294
      thread->next = prev->next;
295
      thread->prev = prev;
296
      prev->next = thread;
297
   }
298
   assert(ThreadHead);
299 168 rhoads
   if(*head == ThreadHead)
300
      thread->state = THREAD_READY;
301 138 rhoads
}
302
 
303
 
304
/******************************************/
305
//Must be called with interrupts disabled
306
static void OS_ThreadPriorityRemove(OS_Thread_t **head, OS_Thread_t *thread)
307
{
308
   assert(thread->magic[0] == THREAD_MAGIC);  //check stack overflow
309
   if(thread->prev == NULL)
310
      *head = thread->next;
311
   else
312
      thread->prev->next = thread->next;
313
   if(thread->next)
314
      thread->next->prev = thread->prev;
315
   thread->next = NULL;
316
   thread->prev = NULL;
317 176 rhoads
   thread->state = THREAD_PEND;
318 138 rhoads
}
319
 
320
 
321
/******************************************/
322
//Linked list of threads sorted by timeout value
323
//Must be called with interrupts disabled
324
static void OS_ThreadTimeoutInsert(OS_Thread_t *thread)
325
{
326
   OS_Thread_t *node, *prev;
327
   int diff;
328
 
329
   prev = NULL;
330
   for(node = TimeoutHead; node; node = node->nextTimeout)
331
   {
332
      diff = thread->ticksTimeout - node->ticksTimeout;
333
      if(diff <= 0)
334
         break;
335
      prev = node;
336
   }
337
 
338
   if(prev == NULL)
339
   {
340
      thread->nextTimeout = TimeoutHead;
341
      thread->prevTimeout = NULL;
342 151 rhoads
      if(TimeoutHead)
343
         TimeoutHead->prevTimeout = thread;
344 138 rhoads
      TimeoutHead = thread;
345
   }
346
   else
347
   {
348
      if(prev->nextTimeout)
349
         prev->nextTimeout->prevTimeout = thread;
350
      thread->nextTimeout = prev->nextTimeout;
351
      thread->prevTimeout = prev;
352
      prev->nextTimeout = thread;
353
   }
354
}
355
 
356
 
357
/******************************************/
358
//Must be called with interrupts disabled
359
static void OS_ThreadTimeoutRemove(OS_Thread_t *thread)
360
{
361
   if(thread->prevTimeout == NULL && TimeoutHead != thread)
362
      return;         //not in list
363
   if(thread->prevTimeout == NULL)
364
      TimeoutHead = thread->nextTimeout;
365
   else
366
      thread->prevTimeout->nextTimeout = thread->nextTimeout;
367
   if(thread->nextTimeout)
368
      thread->nextTimeout->prevTimeout = thread->prevTimeout;
369
   thread->nextTimeout = NULL;
370
   thread->prevTimeout = NULL;
371
}
372
 
373
 
374 176 rhoads
#if OS_CPU_COUNT <= 1
375 138 rhoads
/******************************************/
376 151 rhoads
//Loads a new thread 
377 138 rhoads
//Must be called with interrupts disabled
378 189 rhoads
static void OS_ThreadReschedule(int roundRobin)
379 138 rhoads
{
380 176 rhoads
   OS_Thread_t *threadNext, *threadCurrent, *threadTry;
381 138 rhoads
   int rc;
382
 
383
   if(ThreadSwapEnabled == 0 || InterruptInside)
384
   {
385 189 rhoads
      ThreadNeedReschedule |= 2 + roundRobin;  //Reschedule later
386 138 rhoads
      return;
387
   }
388
 
389
   //Determine which thread should run
390 176 rhoads
   threadCurrent = ThreadCurrent[0];
391 167 rhoads
   threadNext = threadCurrent;
392 176 rhoads
   if(threadCurrent == NULL || threadCurrent->state == THREAD_PEND)
393
      threadNext = ThreadHead;
394
   else if(threadCurrent->priority < ThreadHead->priority)
395
      threadNext = ThreadHead;
396 189 rhoads
   else if(roundRobin)
397 176 rhoads
   {
398
      //Determine next ready thread with same priority
399
      threadTry = threadCurrent->next;
400
      if(threadTry && threadTry->priority == threadCurrent->priority)
401
         threadNext = threadTry;
402
      else
403
         threadNext = ThreadHead;
404
   }
405
 
406
   if(threadNext != threadCurrent)
407
   {
408
      //Swap threads
409
      ThreadCurrent[0] = threadNext;
410
      assert(threadNext);
411
      if(threadCurrent)
412
      {
413
         assert(threadCurrent->magic[0] == THREAD_MAGIC); //check stack overflow
414
         rc = setjmp(threadCurrent->env);  //ANSI C call to save registers
415
         if(rc)
416
         {
417
            //Returned from longjmp()
418
            return;
419
         }
420
      }
421
 
422
      threadNext = ThreadCurrent[0];       //removed warning
423
      longjmp(threadNext->env, 1);         //ANSI C call to restore registers
424
   }
425
}
426
 
427 189 rhoads
#else //OS_CPU_COUNT > 1
428 176 rhoads
 
429
/******************************************/
430 189 rhoads
//Check if a different CPU needs to swap threads
431
static void OS_ThreadRescheduleCheck(void)
432 176 rhoads
{
433 189 rhoads
   OS_Thread_t *threadBest;
434
   uint32 i, priorityLow, cpuIndex = OS_CpuIndex();
435
   int cpuLow;
436 176 rhoads
 
437 189 rhoads
   //Find the CPU running the lowest priority thread
438 176 rhoads
   cpuLow = 0;
439
   priorityLow = 0xffffffff;
440
   for(i = 0; i < OS_CPU_COUNT; ++i)
441
   {
442
      if(i != cpuIndex && (ThreadCurrent[i] == NULL ||
443
         ThreadCurrent[i]->priority < priorityLow))
444
      {
445
         cpuLow = i;
446
         if(ThreadCurrent[i])
447
            priorityLow = ThreadCurrent[i]->priority;
448
         else
449
            priorityLow = 0;
450
      }
451
   }
452
 
453 189 rhoads
   //Determine highest priority ready thread for other CPUs
454
   for(threadBest = ThreadHead; threadBest && threadBest->priority > priorityLow;
455
      threadBest = threadBest->next)
456
   {
457
      if(threadBest->state == THREAD_READY)
458
      {
459
         if(threadBest->cpuLock == -1)
460
         {
461
            OS_CpuInterrupt(cpuLow, 1);  //Reschedule on the other CPU
462
            break;
463
         }
464
         else if(threadBest->priority > ThreadCurrent[threadBest->cpuLock]->priority)
465
         {
466
            OS_CpuInterrupt(threadBest->cpuLock, 1);  //Reschedule on the other CPU
467
            break;
468
         }
469
      }
470
   }
471
}
472
 
473
 
474
/******************************************/
475
//Loads a new thread in a multiprocessor environment
476
//Must be called with interrupts disabled
477
static void OS_ThreadReschedule(int roundRobin)
478
{
479
   OS_Thread_t *threadNext, *threadCurrent, *threadBest, *threadAlt;
480
   uint32 cpuIndex = OS_CpuIndex();
481
   int rc;
482
 
483
   if(ThreadSwapEnabled == 0 || InterruptInside)
484
   {
485
      ThreadNeedReschedule |= 2 + roundRobin;  //Reschedule later
486
      return;
487
   }
488
 
489
   //Determine highest priority ready thread
490 168 rhoads
   for(threadBest = ThreadHead; threadBest; threadBest = threadBest->next)
491
   {
492 176 rhoads
      if(threadBest->state == THREAD_READY &&
493
         (threadBest->cpuLock == -1 || threadBest->cpuLock == (int)cpuIndex))
494 168 rhoads
         break;
495
   }
496 176 rhoads
 
497
   //Determine which thread should run
498
   threadCurrent = ThreadCurrent[cpuIndex];
499
   threadNext = threadCurrent;
500
   if(threadCurrent == NULL || threadCurrent->state == THREAD_PEND)
501
   {
502 168 rhoads
      threadNext = threadBest;
503 176 rhoads
   }
504
   else if(threadBest && threadCurrent->priority < threadBest->priority)
505
   {
506 168 rhoads
      threadNext = threadBest;
507 176 rhoads
   }
508 189 rhoads
   else if(roundRobin)
509 138 rhoads
   {
510 176 rhoads
      //Find the next ready thread
511
      for(threadAlt = threadCurrent->next; threadAlt; threadAlt = threadAlt->next)
512 168 rhoads
      {
513 176 rhoads
         if(threadAlt->state == THREAD_READY &&
514
            (threadAlt->cpuLock == -1 || threadAlt->cpuLock == (int)cpuIndex))
515 168 rhoads
            break;
516
      }
517 176 rhoads
      if(threadAlt && threadAlt->priority == threadCurrent->priority)
518
         threadNext = threadAlt;
519
      else if(threadBest && threadBest->priority >= threadCurrent->priority)
520 168 rhoads
         threadNext = threadBest;
521 138 rhoads
   }
522
 
523 167 rhoads
   if(threadNext != threadCurrent)
524 138 rhoads
   {
525
      //Swap threads
526 167 rhoads
      ThreadCurrent[cpuIndex] = threadNext;
527
      assert(threadNext);
528 168 rhoads
      if(threadCurrent)
529 138 rhoads
      {
530 168 rhoads
         assert(threadCurrent->magic[0] == THREAD_MAGIC); //check stack overflow
531
         threadCurrent->state = THREAD_READY;
532 176 rhoads
         threadCurrent->spinLocks = OS_SpinCountGet();
533 189 rhoads
         threadCurrent->cpuIndex = -1;
534 176 rhoads
         rc = setjmp(threadCurrent->env);   //ANSI C call to save registers
535 138 rhoads
         if(rc)
536
         {
537
            //Returned from longjmp()
538
            return;
539
         }
540
      }
541 168 rhoads
 
542 176 rhoads
      //Restore spin lock count
543 167 rhoads
      cpuIndex = OS_CpuIndex();             //removed warning
544
      threadNext = ThreadCurrent[cpuIndex]; //removed warning
545 176 rhoads
      threadNext->state = THREAD_RUNNING;
546
      OS_SpinCountSet(threadNext->spinLocks);
547 189 rhoads
      threadNext->cpuIndex = (int)cpuIndex;
548
      OS_ThreadRescheduleCheck();
549 176 rhoads
      longjmp(threadNext->env, 1);          //ANSI C call to restore registers
550 138 rhoads
   }
551 189 rhoads
   OS_ThreadRescheduleCheck();
552 138 rhoads
}
553
 
554
 
555 189 rhoads
/******************************************/
556 176 rhoads
void OS_ThreadCpuLock(OS_Thread_t *Thread, int CpuIndex)
557
{
558
   Thread->cpuLock = CpuIndex;
559
   if(Thread == OS_ThreadSelf() && CpuIndex != (int)OS_CpuIndex())
560
      OS_ThreadSleep(1);
561
}
562
 
563
#endif //#if OS_CPU_COUNT <= 1
564
 
565
 
566 138 rhoads
/******************************************/
567 189 rhoads
static void OS_ThreadInit(void *arg)
568 138 rhoads
{
569 167 rhoads
   uint32 cpuIndex = OS_CpuIndex();
570 189 rhoads
   (void)arg;
571 138 rhoads
 
572 168 rhoads
   OS_CriticalEnd(1);
573 167 rhoads
   ThreadCurrent[cpuIndex]->funcPtr(ThreadCurrent[cpuIndex]->arg);
574 138 rhoads
   OS_ThreadExit();
575
}
576
 
577
 
578
/******************************************/
579
//Stops warning "argument X might be clobbered by `longjmp'"
580
static void OS_ThreadRegsInit(jmp_buf env)
581
{
582
   setjmp(env); //ANSI C call to save registers
583
}
584
 
585
 
586
/******************************************/
587 189 rhoads
OS_Thread_t *OS_ThreadCreate(const char *name,
588
                             OS_FuncPtr_t funcPtr,
589
                             void *arg,
590
                             uint32 priority,
591
                             uint32 stackSize)
592 138 rhoads
{
593
   OS_Thread_t *thread;
594
   uint8 *stack;
595
   jmp_buf2 *env;
596
   uint32 state;
597
 
598
   OS_SemaphorePend(SemaphoreLock, OS_WAIT_FOREVER);
599
   if(NeedToFree)
600
      OS_HeapFree(NeedToFree);
601
   NeedToFree = NULL;
602
   OS_SemaphorePost(SemaphoreLock);
603
 
604 189 rhoads
   if(stackSize == 0)
605
      stackSize = STACK_SIZE_DEFAULT;
606
   if(stackSize < STACK_SIZE_MINIMUM)
607
      stackSize = STACK_SIZE_MINIMUM;
608
   thread = (OS_Thread_t*)OS_HeapMalloc(NULL, sizeof(OS_Thread_t) + stackSize);
609 138 rhoads
   assert(thread);
610
   if(thread == NULL)
611
      return NULL;
612 189 rhoads
   memset(thread, 0, sizeof(OS_Thread_t));
613 138 rhoads
   stack = (uint8*)(thread + 1);
614 189 rhoads
   memset(stack, 0xcd, stackSize);
615 138 rhoads
 
616 189 rhoads
   thread->name = name;
617 168 rhoads
   thread->state = THREAD_READY;
618 176 rhoads
   thread->cpuLock = -1;
619 189 rhoads
   thread->funcPtr = funcPtr;
620
   thread->arg = arg;
621
   thread->priority = priority;
622 138 rhoads
   thread->info = NULL;
623
   thread->semaphorePending = NULL;
624
   thread->returnCode = 0;
625 167 rhoads
   thread->spinLocks = 1;
626 189 rhoads
   if(OS_ThreadSelf())
627
   {
628
      thread->processId = OS_ThreadSelf()->processId;
629
      thread->heap = OS_ThreadSelf()->heap;
630
   }
631
   else
632
   {
633
      thread->processId = 0;
634
      thread->heap = NULL;
635
   }
636 138 rhoads
   thread->next = NULL;
637
   thread->prev = NULL;
638
   thread->nextTimeout = NULL;
639
   thread->prevTimeout = NULL;
640
   thread->magic[0] = THREAD_MAGIC;
641
 
642
   OS_ThreadRegsInit(thread->env);
643
   env = (jmp_buf2*)thread->env;
644 200 rhoads
   env->sp = (uint32)stack + stackSize - 24; //minimum stack frame size
645 138 rhoads
   env->pc = (uint32)OS_ThreadInit;
646
 
647
   state = OS_CriticalBegin();
648
   OS_ThreadPriorityInsert(&ThreadHead, thread);
649
   OS_ThreadReschedule(0);
650
   OS_CriticalEnd(state);
651
   return thread;
652
}
653
 
654
 
655
/******************************************/
656
void OS_ThreadExit(void)
657
{
658 167 rhoads
   uint32 state, cpuIndex = OS_CpuIndex();
659 138 rhoads
   OS_SemaphorePend(SemaphoreLock, OS_WAIT_FOREVER);
660
   if(NeedToFree)
661
      OS_HeapFree(NeedToFree);
662
   NeedToFree = NULL;
663
   OS_SemaphorePost(SemaphoreLock);
664
 
665
   state = OS_CriticalBegin();
666 167 rhoads
   OS_ThreadPriorityRemove(&ThreadHead, ThreadCurrent[cpuIndex]);
667
   NeedToFree = ThreadCurrent[cpuIndex];
668 138 rhoads
   OS_ThreadReschedule(0);
669
   OS_CriticalEnd(state);
670
}
671
 
672
 
673
/******************************************/
674
OS_Thread_t *OS_ThreadSelf(void)
675
{
676 167 rhoads
   return ThreadCurrent[OS_CpuIndex()];
677 138 rhoads
}
678
 
679
 
680
/******************************************/
681 189 rhoads
void OS_ThreadSleep(int ticks)
682 138 rhoads
{
683 189 rhoads
   OS_SemaphorePend(SemaphoreSleep, ticks);
684 138 rhoads
}
685
 
686
 
687
/******************************************/
688
uint32 OS_ThreadTime(void)
689
{
690
   return ThreadTime;
691
}
692
 
693
 
694
/******************************************/
695 189 rhoads
void OS_ThreadInfoSet(OS_Thread_t *thread, void *Info)
696 138 rhoads
{
697 189 rhoads
   thread->info = Info;
698 138 rhoads
}
699
 
700
 
701
/******************************************/
702 189 rhoads
void *OS_ThreadInfoGet(OS_Thread_t *thread)
703 138 rhoads
{
704 189 rhoads
   return thread->info;
705 138 rhoads
}
706
 
707
 
708
/******************************************/
709 189 rhoads
uint32 OS_ThreadPriorityGet(OS_Thread_t *thread)
710 138 rhoads
{
711 189 rhoads
   return thread->priority;
712 138 rhoads
}
713
 
714
 
715
/******************************************/
716 189 rhoads
void OS_ThreadPrioritySet(OS_Thread_t *thread, uint32 priority)
717 138 rhoads
{
718
   uint32 state;
719
   state = OS_CriticalBegin();
720 189 rhoads
   thread->priority = priority;
721
   if(thread->state != THREAD_PEND)
722 168 rhoads
   {
723 189 rhoads
      OS_ThreadPriorityRemove(&ThreadHead, thread);
724
      OS_ThreadPriorityInsert(&ThreadHead, thread);
725 168 rhoads
      OS_ThreadReschedule(0);
726
   }
727 138 rhoads
   OS_CriticalEnd(state);
728
}
729
 
730
 
731
/******************************************/
732 189 rhoads
void OS_ThreadProcessId(OS_Thread_t *thread, uint32 processId, OS_Heap_t *heap)
733
{
734
   thread->processId = processId;
735
   thread->heap = heap;
736
}
737
 
738
 
739
/******************************************/
740 138 rhoads
//Must be called with interrupts disabled
741
void OS_ThreadTick(void *Arg)
742
{
743
   OS_Thread_t *thread;
744
   OS_Semaphore_t *semaphore;
745
   int diff;
746
   (void)Arg;
747
 
748
   ++ThreadTime;
749
   while(TimeoutHead)
750
   {
751
      thread = TimeoutHead;
752
      diff = ThreadTime - thread->ticksTimeout;
753
      if(diff < 0)
754
         break;
755
      OS_ThreadTimeoutRemove(thread);
756
      semaphore = thread->semaphorePending;
757
      ++semaphore->count;
758
      thread->semaphorePending = NULL;
759
      thread->returnCode = -1;
760
      OS_ThreadPriorityRemove(&semaphore->threadHead, thread);
761
      OS_ThreadPriorityInsert(&ThreadHead, thread);
762
   }
763
   OS_ThreadReschedule(1);
764
}
765
 
766
 
767
 
768
/***************** Semaphore **************/
769
/******************************************/
770 189 rhoads
OS_Semaphore_t *OS_SemaphoreCreate(const char *name, uint32 count)
771 138 rhoads
{
772
   OS_Semaphore_t *semaphore;
773 189 rhoads
   static int semCount = 0;
774 138 rhoads
 
775
   if(semCount < SEM_RESERVED_COUNT)
776 189 rhoads
      semaphore = &SemaphoreReserved[semCount++];  //Heap not ready yet
777 138 rhoads
   else
778 189 rhoads
      semaphore = (OS_Semaphore_t*)OS_HeapMalloc(HEAP_SYSTEM, sizeof(OS_Semaphore_t));
779 138 rhoads
   assert(semaphore);
780
   if(semaphore == NULL)
781
      return NULL;
782
 
783 189 rhoads
   semaphore->name = name;
784 138 rhoads
   semaphore->threadHead = NULL;
785 189 rhoads
   semaphore->count = count;
786 138 rhoads
   return semaphore;
787
}
788
 
789
 
790
/******************************************/
791 189 rhoads
void OS_SemaphoreDelete(OS_Semaphore_t *semaphore)
792 138 rhoads
{
793 189 rhoads
   while(semaphore->threadHead)
794
      OS_SemaphorePost(semaphore);
795
   OS_HeapFree(semaphore);
796 138 rhoads
}
797
 
798
 
799
/******************************************/
800 189 rhoads
int OS_SemaphorePend(OS_Semaphore_t *semaphore, int ticks)
801 138 rhoads
{
802 167 rhoads
   uint32 state, cpuIndex;
803 138 rhoads
   OS_Thread_t *thread;
804
   int returnCode=0;
805
 
806 189 rhoads
   assert(semaphore);
807 151 rhoads
   assert(InterruptInside == 0);
808 138 rhoads
   state = OS_CriticalBegin();
809 189 rhoads
   if(--semaphore->count < 0)
810 138 rhoads
   {
811 189 rhoads
      if(ticks == 0)
812 138 rhoads
      {
813 189 rhoads
         ++semaphore->count;
814 138 rhoads
         OS_CriticalEnd(state);
815
         return -1;
816
      }
817 189 rhoads
      cpuIndex = OS_CpuIndex();
818 167 rhoads
      thread = ThreadCurrent[cpuIndex];
819 189 rhoads
      assert(thread);
820
      thread->semaphorePending = semaphore;
821
      thread->ticksTimeout = ticks + OS_ThreadTime();
822 138 rhoads
      OS_ThreadPriorityRemove(&ThreadHead, thread);
823 189 rhoads
      OS_ThreadPriorityInsert(&semaphore->threadHead, thread);
824
      if(ticks != OS_WAIT_FOREVER)
825 138 rhoads
         OS_ThreadTimeoutInsert(thread);
826
      assert(ThreadHead);
827
      OS_ThreadReschedule(0);
828
      returnCode = thread->returnCode;
829
   }
830
   OS_CriticalEnd(state);
831
   return returnCode;
832
}
833
 
834
 
835
/******************************************/
836 189 rhoads
void OS_SemaphorePost(OS_Semaphore_t *semaphore)
837 138 rhoads
{
838
   uint32 state;
839 146 rhoads
   OS_Thread_t *thread;
840 138 rhoads
 
841 189 rhoads
   assert(semaphore);
842 138 rhoads
   state = OS_CriticalBegin();
843 189 rhoads
   if(++semaphore->count <= 0)
844 138 rhoads
   {
845 189 rhoads
      thread = semaphore->threadHead;
846 138 rhoads
      OS_ThreadTimeoutRemove(thread);
847 189 rhoads
      OS_ThreadPriorityRemove(&semaphore->threadHead, thread);
848 138 rhoads
      OS_ThreadPriorityInsert(&ThreadHead, thread);
849
      thread->semaphorePending = NULL;
850
      thread->returnCode = 0;
851 146 rhoads
      OS_ThreadReschedule(0);
852 138 rhoads
   }
853
   OS_CriticalEnd(state);
854
}
855
 
856
 
857
 
858
/***************** Mutex ******************/
859
/******************************************/
860 189 rhoads
OS_Mutex_t *OS_MutexCreate(const char *name)
861 138 rhoads
{
862
   OS_Mutex_t *mutex;
863
 
864 189 rhoads
   mutex = (OS_Mutex_t*)OS_HeapMalloc(HEAP_SYSTEM, sizeof(OS_Mutex_t));
865 138 rhoads
   if(mutex == NULL)
866
      return NULL;
867 189 rhoads
   mutex->semaphore = OS_SemaphoreCreate(name, 1);
868 138 rhoads
   if(mutex->semaphore == NULL)
869
      return NULL;
870
   mutex->thread = NULL;
871
   mutex->count = 0;
872
   return mutex;
873
}
874
 
875
 
876
/******************************************/
877 189 rhoads
void OS_MutexDelete(OS_Mutex_t *mutex)
878 138 rhoads
{
879 189 rhoads
   OS_SemaphoreDelete(mutex->semaphore);
880
   OS_HeapFree(mutex);
881 138 rhoads
}
882
 
883
 
884
/******************************************/
885 189 rhoads
void OS_MutexPend(OS_Mutex_t *mutex)
886 138 rhoads
{
887
   OS_Thread_t *thread;
888
 
889 189 rhoads
   assert(mutex);
890 138 rhoads
   thread = OS_ThreadSelf();
891 189 rhoads
   if(thread == mutex->thread)
892 138 rhoads
   {
893 189 rhoads
      ++mutex->count;
894 138 rhoads
      return;
895
   }
896 189 rhoads
   OS_SemaphorePend(mutex->semaphore, OS_WAIT_FOREVER);
897
   mutex->thread = thread;
898
   mutex->count = 1;
899 138 rhoads
}
900
 
901
 
902
/******************************************/
903 189 rhoads
void OS_MutexPost(OS_Mutex_t *mutex)
904 138 rhoads
{
905 189 rhoads
   assert(mutex);
906
   assert(mutex->thread == OS_ThreadSelf());
907
   assert(mutex->count > 0);
908
   if(--mutex->count <= 0)
909 138 rhoads
   {
910 189 rhoads
      mutex->thread = NULL;
911
      OS_SemaphorePost(mutex->semaphore);
912 138 rhoads
   }
913
}
914
 
915
 
916
 
917
/***************** MQueue *****************/
918
/******************************************/
919 189 rhoads
OS_MQueue_t *OS_MQueueCreate(const char *name,
920
                             int messageCount,
921
                             int messageBytes)
922 138 rhoads
{
923
   OS_MQueue_t *queue;
924
   int size;
925
 
926 189 rhoads
   size = messageBytes / sizeof(uint32);
927
   queue = (OS_MQueue_t*)OS_HeapMalloc(HEAP_SYSTEM, sizeof(OS_MQueue_t) +
928
      messageCount * size * 4);
929 138 rhoads
   if(queue == NULL)
930
      return queue;
931 189 rhoads
   queue->name = name;
932
   queue->semaphore = OS_SemaphoreCreate(name, 0);
933 138 rhoads
   if(queue->semaphore == NULL)
934
      return NULL;
935 189 rhoads
   queue->count = messageCount;
936 138 rhoads
   queue->size = size;
937
   queue->used = 0;
938
   queue->read = 0;
939
   queue->write = 0;
940
   return queue;
941
}
942
 
943
 
944
/******************************************/
945 189 rhoads
void OS_MQueueDelete(OS_MQueue_t *mQueue)
946 138 rhoads
{
947 189 rhoads
   OS_SemaphoreDelete(mQueue->semaphore);
948
   OS_HeapFree(mQueue);
949 138 rhoads
}
950
 
951
 
952
/******************************************/
953 189 rhoads
int OS_MQueueSend(OS_MQueue_t *mQueue, void *message)
954 138 rhoads
{
955
   uint32 state, *dst, *src;
956
   int i;
957
 
958 189 rhoads
   assert(mQueue);
959
   src = (uint32*)message;
960 138 rhoads
   state = OS_CriticalBegin();
961 189 rhoads
   if(++mQueue->used > mQueue->count)
962 138 rhoads
   {
963 189 rhoads
      --mQueue->used;
964 138 rhoads
      OS_CriticalEnd(state);
965
      return -1;
966
   }
967 189 rhoads
   dst = (uint32*)(mQueue + 1) + mQueue->write * mQueue->size;
968
   for(i = 0; i < mQueue->size; ++i)
969 138 rhoads
      dst[i] = src[i];
970 189 rhoads
   if(++mQueue->write >= mQueue->count)
971
      mQueue->write = 0;
972 138 rhoads
   OS_CriticalEnd(state);
973 189 rhoads
   OS_SemaphorePost(mQueue->semaphore);
974 138 rhoads
   return 0;
975
}
976
 
977
 
978
/******************************************/
979 189 rhoads
int OS_MQueueGet(OS_MQueue_t *mQueue, void *message, int ticks)
980 138 rhoads
{
981
   uint32 state, *dst, *src;
982
   int i, rc;
983
 
984 189 rhoads
   assert(mQueue);
985
   dst = (uint32*)message;
986
   rc = OS_SemaphorePend(mQueue->semaphore, ticks);
987 138 rhoads
   if(rc)
988
      return rc;
989
   state = OS_CriticalBegin();
990 189 rhoads
   --mQueue->used;
991
   src = (uint32*)(mQueue + 1) + mQueue->read * mQueue->size;
992
   for(i = 0; i < mQueue->size; ++i)
993 138 rhoads
      dst[i] = src[i];
994 189 rhoads
   if(++mQueue->read >= mQueue->count)
995
      mQueue->read = 0;
996 138 rhoads
   OS_CriticalEnd(state);
997
   return 0;
998
}
999
 
1000
 
1001
 
1002
/***************** Timer ******************/
1003
/******************************************/
1004 189 rhoads
static void OS_TimerThread(void *arg)
1005 138 rhoads
{
1006
   uint32 timeNow;
1007
   int diff, ticks;
1008
   uint32 message[8];
1009
   OS_Timer_t *timer;
1010 189 rhoads
   (void)arg;
1011 138 rhoads
 
1012
   timeNow = OS_ThreadTime();
1013
   for(;;)
1014
   {
1015
      //Determine how long to sleep
1016
      OS_SemaphorePend(SemaphoreLock, OS_WAIT_FOREVER);
1017
      if(TimerHead)
1018
         ticks = TimerHead->ticksTimeout - timeNow;
1019
      else
1020
         ticks = OS_WAIT_FOREVER;
1021
      OS_SemaphorePost(SemaphoreLock);
1022
      OS_SemaphorePend(SemaphoreTimer, ticks);
1023
 
1024
      //Send messages for all timed out timers
1025
      timeNow = OS_ThreadTime();
1026
      for(;;)
1027
      {
1028 189 rhoads
         timer = NULL;
1029 138 rhoads
         OS_SemaphorePend(SemaphoreLock, OS_WAIT_FOREVER);
1030 189 rhoads
         if(TimerHead)
1031 138 rhoads
         {
1032 189 rhoads
            diff = timeNow - TimerHead->ticksTimeout;
1033
            if(diff >= 0)
1034
               timer = TimerHead;
1035 138 rhoads
         }
1036 189 rhoads
         OS_SemaphorePost(SemaphoreLock);
1037
         if(timer == NULL)
1038 138 rhoads
            break;
1039
         if(timer->ticksRestart)
1040
            OS_TimerStart(timer, timer->ticksRestart, timer->ticksRestart);
1041
         else
1042
            OS_TimerStop(timer);
1043
 
1044 218 rhoads
         if(timer->callback)
1045
            timer->callback(timer, timer->info);
1046
         else
1047
         {
1048
            //Send message
1049
            message[0] = MESSAGE_TYPE_TIMER;
1050
            message[1] = (uint32)timer;
1051
            message[2] = timer->info;
1052
            OS_MQueueSend(timer->mqueue, message);
1053
         }
1054 138 rhoads
      }
1055
   }
1056
}
1057
 
1058
 
1059
/******************************************/
1060 189 rhoads
OS_Timer_t *OS_TimerCreate(const char *name, OS_MQueue_t *mQueue, uint32 info)
1061 138 rhoads
{
1062
   OS_Timer_t *timer;
1063
   int startThread=0;
1064
 
1065
   OS_SemaphorePend(SemaphoreLock, OS_WAIT_FOREVER);
1066
   if(SemaphoreTimer == NULL)
1067
   {
1068
      SemaphoreTimer = OS_SemaphoreCreate("Timer", 0);
1069
      startThread = 1;
1070
   }
1071
   OS_SemaphorePost(SemaphoreLock);
1072
   if(startThread)
1073
      OS_ThreadCreate("Timer", OS_TimerThread, NULL, 250, 2000);
1074
 
1075 189 rhoads
   timer = (OS_Timer_t*)OS_HeapMalloc(HEAP_SYSTEM, sizeof(OS_Timer_t));
1076 138 rhoads
   if(timer == NULL)
1077
      return NULL;
1078 189 rhoads
   timer->name = name;
1079 218 rhoads
   timer->callback = NULL;
1080 189 rhoads
   timer->mqueue = mQueue;
1081 138 rhoads
   timer->next = NULL;
1082
   timer->prev = NULL;
1083 189 rhoads
   timer->info = info;
1084 138 rhoads
   timer->active = 0;
1085
   return timer;
1086
}
1087
 
1088
 
1089
/******************************************/
1090 189 rhoads
void OS_TimerDelete(OS_Timer_t *timer)
1091 138 rhoads
{
1092 189 rhoads
   OS_TimerStop(timer);
1093
   OS_HeapFree(timer);
1094 138 rhoads
}
1095
 
1096
 
1097
/******************************************/
1098 218 rhoads
void OS_TimerCallback(OS_Timer_t *timer, OS_TimerFuncPtr_t callback)
1099
{
1100
   timer->callback = callback;
1101
}
1102
 
1103
 
1104
/******************************************/
1105 138 rhoads
//Must not be called from an ISR
1106 189 rhoads
void OS_TimerStart(OS_Timer_t *timer, uint32 ticks, uint32 ticksRestart)
1107 138 rhoads
{
1108
   OS_Timer_t *node, *prev;
1109
   int diff, check=0;
1110
 
1111 189 rhoads
   assert(timer);
1112 167 rhoads
   assert(InterruptInside == 0);
1113 189 rhoads
   ticks += OS_ThreadTime();
1114
   if(timer->active)
1115
      OS_TimerStop(timer);
1116 138 rhoads
   OS_SemaphorePend(SemaphoreLock, OS_WAIT_FOREVER);
1117 189 rhoads
   if(timer->active)
1118
   {
1119
      //Prevent race condition
1120
      OS_SemaphorePost(SemaphoreLock);
1121
      return;
1122
   }
1123
   timer->ticksTimeout = ticks;
1124
   timer->ticksRestart = ticksRestart;
1125
   timer->active = 1;
1126 138 rhoads
   prev = NULL;
1127
   for(node = TimerHead; node; node = node->next)
1128
   {
1129 189 rhoads
      diff = ticks - node->ticksTimeout;
1130 138 rhoads
      if(diff <= 0)
1131
         break;
1132
      prev = node;
1133
   }
1134 189 rhoads
   timer->next = node;
1135
   timer->prev = prev;
1136 138 rhoads
   if(node)
1137 189 rhoads
      node->prev = timer;
1138 138 rhoads
   if(prev == NULL)
1139
   {
1140 189 rhoads
      TimerHead = timer;
1141 138 rhoads
      check = 1;
1142
   }
1143
   else
1144 189 rhoads
      prev->next = timer;
1145 138 rhoads
   OS_SemaphorePost(SemaphoreLock);
1146
   if(check)
1147
      OS_SemaphorePost(SemaphoreTimer);
1148
}
1149
 
1150
 
1151
/******************************************/
1152
//Must not be called from an ISR
1153 189 rhoads
void OS_TimerStop(OS_Timer_t *timer)
1154 138 rhoads
{
1155 189 rhoads
   assert(timer);
1156 167 rhoads
   assert(InterruptInside == 0);
1157 138 rhoads
   OS_SemaphorePend(SemaphoreLock, OS_WAIT_FOREVER);
1158 189 rhoads
   if(timer->active)
1159 138 rhoads
   {
1160 189 rhoads
      timer->active = 0;
1161
      if(timer->prev == NULL)
1162
         TimerHead = timer->next;
1163 138 rhoads
      else
1164 189 rhoads
         timer->prev->next = timer->next;
1165
      if(timer->next)
1166
         timer->next->prev = timer->prev;
1167 138 rhoads
   }
1168
   OS_SemaphorePost(SemaphoreLock);
1169
}
1170
 
1171
 
1172
/***************** ISR ********************/
1173
/******************************************/
1174 189 rhoads
void OS_InterruptServiceRoutine(uint32 status, uint32 *stack)
1175 138 rhoads
{
1176
   int i;
1177 167 rhoads
   uint32 state;
1178 138 rhoads
 
1179 189 rhoads
   if(status == 0 && Isr[31])
1180
      Isr[31](stack);                   //SYSCALL or BREAK
1181
 
1182 138 rhoads
   InterruptInside = 1;
1183
   i = 0;
1184
   do
1185
   {
1186 189 rhoads
      if(status & 1)
1187 138 rhoads
      {
1188
         if(Isr[i])
1189 189 rhoads
            Isr[i](stack);
1190 138 rhoads
         else
1191
            OS_InterruptMaskClear(1 << i);
1192
      }
1193 189 rhoads
      status >>= 1;
1194 138 rhoads
      ++i;
1195 189 rhoads
   } while(status);
1196 138 rhoads
   InterruptInside = 0;
1197
 
1198 167 rhoads
   state = OS_SpinLock();
1199 138 rhoads
   if(ThreadNeedReschedule)
1200
      OS_ThreadReschedule(ThreadNeedReschedule & 1);
1201 167 rhoads
   OS_SpinUnlock(state);
1202 138 rhoads
}
1203
 
1204
 
1205
/******************************************/
1206 189 rhoads
void OS_InterruptRegister(uint32 mask, OS_FuncPtr_t funcPtr)
1207 138 rhoads
{
1208
   int i;
1209
 
1210
   for(i = 0; i < 32; ++i)
1211
   {
1212 189 rhoads
      if(mask & (1 << i))
1213
         Isr[i] = funcPtr;
1214 138 rhoads
   }
1215
}
1216
 
1217
 
1218
/******************************************/
1219
//Plasma hardware dependent
1220
uint32 OS_InterruptStatus(void)
1221
{
1222
   return MemoryRead(IRQ_STATUS);
1223
}
1224
 
1225
 
1226
/******************************************/
1227
//Plasma hardware dependent
1228 189 rhoads
uint32 OS_InterruptMaskSet(uint32 mask)
1229 138 rhoads
{
1230 189 rhoads
   uint32 state;
1231 138 rhoads
   state = OS_CriticalBegin();
1232 189 rhoads
   mask |= MemoryRead(IRQ_MASK);
1233 138 rhoads
   MemoryWrite(IRQ_MASK, mask);
1234
   OS_CriticalEnd(state);
1235
   return mask;
1236
}
1237
 
1238
 
1239
/******************************************/
1240
//Plasma hardware dependent
1241 189 rhoads
uint32 OS_InterruptMaskClear(uint32 mask)
1242 138 rhoads
{
1243 189 rhoads
   uint32 state;
1244 138 rhoads
   state = OS_CriticalBegin();
1245 189 rhoads
   mask = MemoryRead(IRQ_MASK) & ~mask;
1246 138 rhoads
   MemoryWrite(IRQ_MASK, mask);
1247
   OS_CriticalEnd(state);
1248
   return mask;
1249
}
1250
 
1251
 
1252
/**************** Init ********************/
1253
/******************************************/
1254
static volatile uint32 IdleCount;
1255 189 rhoads
static void OS_IdleThread(void *arg)
1256 138 rhoads
{
1257 189 rhoads
   (void)arg;
1258 138 rhoads
 
1259 151 rhoads
   //Don't block in the idle thread!
1260 138 rhoads
   for(;;)
1261
   {
1262
      ++IdleCount;
1263
   }
1264
}
1265
 
1266
 
1267
/******************************************/
1268
#ifndef DISABLE_IRQ_SIM
1269 189 rhoads
static void OS_IdleSimulateIsr(void *arg)
1270 138 rhoads
{
1271
   uint32 count=0, value;
1272 189 rhoads
   (void)arg;
1273 138 rhoads
 
1274
   for(;;)
1275
   {
1276
      MemoryRead(IRQ_MASK + 4);       //calls Sleep(10)
1277
#if WIN32
1278
      while(OS_InterruptMaskSet(0) & IRQ_UART_WRITE_AVAILABLE)
1279 176 rhoads
         OS_InterruptServiceRoutine(IRQ_UART_WRITE_AVAILABLE, 0);
1280 138 rhoads
#endif
1281 189 rhoads
      value = OS_InterruptMaskSet(0) & 0xf;
1282
      if(value)
1283
         OS_InterruptServiceRoutine(value, 0);
1284 138 rhoads
      ++count;
1285
   }
1286
}
1287
#endif //DISABLE_IRQ_SIM
1288
 
1289
 
1290
/******************************************/
1291
//Plasma hardware dependent
1292 189 rhoads
static void OS_ThreadTickToggle(void *arg)
1293 138 rhoads
{
1294 167 rhoads
   uint32 status, mask, state;
1295 138 rhoads
 
1296 189 rhoads
   //Toggle looking for IRQ_COUNTER18 or IRQ_COUNTER18_NOT
1297 167 rhoads
   state = OS_SpinLock();
1298 138 rhoads
   status = MemoryRead(IRQ_STATUS) & (IRQ_COUNTER18 | IRQ_COUNTER18_NOT);
1299
   mask = MemoryRead(IRQ_MASK) | IRQ_COUNTER18 | IRQ_COUNTER18_NOT;
1300
   mask &= ~status;
1301
   MemoryWrite(IRQ_MASK, mask);
1302 189 rhoads
   OS_ThreadTick(arg);
1303 167 rhoads
   OS_SpinUnlock(state);
1304 138 rhoads
}
1305
 
1306
 
1307
/******************************************/
1308 189 rhoads
void OS_Init(uint32 *heapStorage, uint32 bytes)
1309 138 rhoads
{
1310 168 rhoads
   int i;
1311 138 rhoads
   OS_AsmInterruptInit();               //Patch interrupt vector
1312
   OS_InterruptMaskClear(0xffffffff);   //Disable interrupts
1313 189 rhoads
   HeapArray[0] = OS_HeapCreate("Default", heapStorage, bytes);
1314
   HeapArray[1] = HeapArray[0];
1315 138 rhoads
   SemaphoreSleep = OS_SemaphoreCreate("Sleep", 0);
1316
   SemaphoreLock = OS_SemaphoreCreate("Lock", 1);
1317 168 rhoads
   for(i = 0; i < OS_CPU_COUNT; ++i)
1318
      OS_ThreadCreate("Idle", OS_IdleThread, NULL, 0, 256);
1319 138 rhoads
#ifndef DISABLE_IRQ_SIM
1320
   if((OS_InterruptStatus() & (IRQ_COUNTER18 | IRQ_COUNTER18_NOT)) == 0)
1321
   {
1322
      //Detected that running in simulator so create SimIsr thread
1323
      UartPrintfCritical("SimIsr\n");
1324
      OS_ThreadCreate("SimIsr", OS_IdleSimulateIsr, NULL, 1, 0);
1325
   }
1326
#endif //DISABLE_IRQ_SIM
1327
 
1328
   //Plasma hardware dependent
1329 189 rhoads
   OS_InterruptRegister(IRQ_COUNTER18 | IRQ_COUNTER18_NOT, OS_ThreadTickToggle);
1330 138 rhoads
   OS_InterruptMaskSet(IRQ_COUNTER18 | IRQ_COUNTER18_NOT);
1331
}
1332
 
1333
 
1334
/******************************************/
1335
void OS_Start(void)
1336
{
1337
   ThreadSwapEnabled = 1;
1338 167 rhoads
   (void)OS_SpinLock();
1339 138 rhoads
   OS_ThreadReschedule(1);
1340
}
1341
 
1342
 
1343
/******************************************/
1344
//Place breakpoint here
1345
void OS_Assert(void)
1346
{
1347
}
1348
 
1349
 
1350 167 rhoads
#if OS_CPU_COUNT > 1
1351
static uint8 SpinLockArray[OS_CPU_COUNT];
1352
/******************************************/
1353
uint32 OS_CpuIndex(void)
1354
{
1355
   return 0; //0 to OS_CPU_COUNT-1
1356
}
1357
 
1358
 
1359
/******************************************/
1360
//Symmetric Multiprocessing Spin Lock Mutex
1361
uint32 OS_SpinLock(void)
1362
{
1363 168 rhoads
   uint32 state, cpuIndex, i, j, ok, delay;
1364 167 rhoads
 
1365 168 rhoads
   cpuIndex = OS_CpuIndex();
1366
   delay = cpuIndex + 8;
1367 167 rhoads
   state = OS_AsmInterruptEnable(0);
1368
   do
1369
   {
1370
      ok = 1;
1371 168 rhoads
      if(++SpinLockArray[cpuIndex] == 1)
1372 167 rhoads
      {
1373
         for(i = 0; i < OS_CPU_COUNT; ++i)
1374
         {
1375 168 rhoads
            if(i != cpuIndex && SpinLockArray[i])
1376 167 rhoads
               ok = 0;
1377
         }
1378
         if(ok == 0)
1379
         {
1380 168 rhoads
            SpinLockArray[cpuIndex] = 0;
1381
            for(j = 0; j < delay; ++j)  //wait a bit
1382 167 rhoads
               ++i;
1383 168 rhoads
            if(delay < 128)
1384
               delay <<= 1;
1385 167 rhoads
         }
1386
      }
1387
   } while(ok == 0);
1388
   return state;
1389
}
1390
 
1391
 
1392
/******************************************/
1393
void OS_SpinUnlock(uint32 state)
1394
{
1395 168 rhoads
   uint32 cpuIndex;
1396
   cpuIndex = OS_CpuIndex();
1397
   if(--SpinLockArray[cpuIndex] == 0)
1398 167 rhoads
      OS_AsmInterruptEnable(state);
1399 168 rhoads
 
1400
   assert(SpinLockArray[cpuIndex] < 10);
1401 167 rhoads
}
1402
 
1403
 
1404
/******************************************/
1405
//Must be called with interrupts disabled and spin locked
1406 176 rhoads
uint32 OS_SpinCountGet(void)
1407 167 rhoads
{
1408 168 rhoads
   uint32 cpuIndex, count;
1409
   cpuIndex = OS_CpuIndex();
1410
   count = SpinLockArray[cpuIndex];
1411 167 rhoads
   return count;
1412
}
1413
 
1414
 
1415
/******************************************/
1416
//Must be called with interrupts disabled and spin locked
1417 176 rhoads
void OS_SpinCountSet(uint32 count)
1418 167 rhoads
{
1419 168 rhoads
   uint32 cpuIndex;
1420
   cpuIndex = OS_CpuIndex();
1421
   SpinLockArray[cpuIndex] = (uint8)count;
1422
   assert(count);
1423 167 rhoads
}
1424 176 rhoads
 
1425
 
1426
/******************************************/
1427
void OS_CpuInterrupt(uint32 cpuIndex, uint32 bitfield)
1428
{
1429 189 rhoads
   //Request other CPU to reschedule threads
1430 176 rhoads
   (void)cpuIndex;
1431
   (void)bitfield;
1432
}
1433
 
1434
 
1435
/******************************************/
1436 189 rhoads
void OS_CpuInterruptServiceRoutine(void *arg)
1437 176 rhoads
{
1438
   uint32 state;
1439 189 rhoads
   (void)arg;
1440 176 rhoads
   state = OS_SpinLock();
1441
   OS_ThreadReschedule(0);
1442
   OS_SpinUnlock(state);
1443
}
1444 167 rhoads
#endif
1445
 
1446
 
1447 138 rhoads
/************** WIN32 Support *************/
1448
#ifdef WIN32
1449
//Support RTOS inside Windows
1450
extern int kbhit();
1451
extern int getch(void);
1452
extern int putch(int);
1453
extern void __stdcall Sleep(unsigned long value);
1454
 
1455
static uint32 Memory[8];
1456
 
1457 189 rhoads
uint32 MemoryRead(uint32 address)
1458 138 rhoads
{
1459 189 rhoads
   Memory[2] |= IRQ_UART_WRITE_AVAILABLE;    //IRQ_STATUS
1460
   switch(address)
1461 138 rhoads
   {
1462
   case UART_READ:
1463
      if(kbhit())
1464 189 rhoads
         Memory[0] = getch();                //UART_READ
1465 138 rhoads
      Memory[2] &= ~IRQ_UART_READ_AVAILABLE; //clear bit
1466
      return Memory[0];
1467
   case IRQ_MASK:
1468 189 rhoads
      return Memory[1];                      //IRQ_MASK
1469 138 rhoads
   case IRQ_MASK + 4:
1470
      Sleep(10);
1471
      return 0;
1472
   case IRQ_STATUS:
1473
      if(kbhit())
1474
         Memory[2] |= IRQ_UART_READ_AVAILABLE;
1475
      return Memory[2];
1476
   }
1477
   return 0;
1478
}
1479
 
1480 189 rhoads
void MemoryWrite(uint32 address, uint32 value)
1481 138 rhoads
{
1482 189 rhoads
   switch(address)
1483 138 rhoads
   {
1484
   case UART_WRITE:
1485 189 rhoads
      putch(value);
1486 138 rhoads
      break;
1487
   case IRQ_MASK:
1488 189 rhoads
      Memory[1] = value;
1489 138 rhoads
      break;
1490
   case IRQ_STATUS:
1491 189 rhoads
      Memory[2] = value;
1492 138 rhoads
      break;
1493
   }
1494
}
1495
 
1496 189 rhoads
uint32 OS_AsmInterruptEnable(uint32 enableInterrupt)
1497 138 rhoads
{
1498 189 rhoads
   return enableInterrupt;
1499 138 rhoads
}
1500
 
1501
void OS_AsmInterruptInit(void)
1502
{
1503
}
1504
#endif  //WIN32
1505
 
1506
 
1507
/**************** Example *****************/
1508
#ifndef NO_MAIN
1509 218 rhoads
#ifdef WIN32
1510 138 rhoads
static uint8 HeapSpace[1024*512];
1511 218 rhoads
#endif
1512 138 rhoads
 
1513 218 rhoads
int main(int programEnd, char *argv[])
1514 138 rhoads
{
1515 218 rhoads
   (void)programEnd;  //Pointer to end of used memory
1516
   (void)argv;
1517 138 rhoads
   UartPrintfCritical("Starting RTOS\n");
1518 218 rhoads
#ifdef WIN32
1519 138 rhoads
   OS_Init((uint32*)HeapSpace, sizeof(HeapSpace));
1520 218 rhoads
#else
1521
   //Remaining space after program in 1MB external RAM
1522
   OS_Init((uint32*)programEnd,
1523
           RAM_EXTERNAL_BASE + RAM_EXTERNAL_SIZE - programEnd);
1524
#endif
1525 138 rhoads
   UartInit();
1526 189 rhoads
   OS_ThreadCreate("Main", MainThread, NULL, 100, 64000);
1527 138 rhoads
   OS_Start();
1528
   return 0;
1529
}
1530
#endif
1531
 

powered by: WebSVN 2.1.0

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