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

Subversion Repositories plasma

[/] [plasma/] [trunk/] [kernel/] [rtos.c] - Blame information for rev 276

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

powered by: WebSVN 2.1.0

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