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

Subversion Repositories plasma

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

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 395 rhoads
   }
392
   ThreadNeedReschedule[cpuIndex] = 0;
393 138 rhoads
 
394
   //Determine which thread should run
395 256 rhoads
   threadNext = ThreadHead;
396
   while(threadNext && threadNext->cpuLock != -1 &&
397
         threadNext->cpuLock != cpuIndex)
398
      threadNext = threadNext->next;
399
   if(threadNext == NULL)
400 189 rhoads
      return;
401 176 rhoads
   threadCurrent = ThreadCurrent[cpuIndex];
402 138 rhoads
 
403 256 rhoads
   if(threadCurrent == NULL ||
404
      threadCurrent->state == THREAD_PEND ||
405
      threadCurrent->priority < threadNext->priority ||
406
      (roundRobin && threadCurrent->priority == threadNext->priority))
407 138 rhoads
   {
408
      //Swap threads
409 167 rhoads
      ThreadCurrent[cpuIndex] = threadNext;
410 168 rhoads
      if(threadCurrent)
411 138 rhoads
      {
412 168 rhoads
         assert(threadCurrent->magic[0] == THREAD_MAGIC); //check stack overflow
413 256 rhoads
         if(threadCurrent->state == THREAD_RUNNING)
414
            OS_ThreadPriorityInsert(&ThreadHead, threadCurrent);
415
         rc = setjmp(threadCurrent->env);  //ANSI C call to save registers
416 138 rhoads
         if(rc)
417 256 rhoads
            return;  //Returned from longjmp()
418 138 rhoads
      }
419 168 rhoads
 
420 256 rhoads
      //Remove the new running thread from the ThreadHead linked list
421
      threadNext = ThreadCurrent[OS_CpuIndex()]; //removed warning
422
      assert(threadNext->state == THREAD_READY);
423
      OS_ThreadPriorityRemove(&ThreadHead, threadNext);
424
      threadNext->state = THREAD_RUNNING;
425
      threadNext->cpuIndex = OS_CpuIndex();
426
      longjmp(threadNext->env, 1);         //ANSI C call to restore registers
427 138 rhoads
   }
428
}
429
 
430
 
431 189 rhoads
/******************************************/
432 256 rhoads
void OS_ThreadCpuLock(OS_Thread_t *thread, int cpuIndex)
433 176 rhoads
{
434 256 rhoads
   thread->cpuLock = cpuIndex;
435
   if(thread == OS_ThreadSelf() && cpuIndex != (int)OS_CpuIndex())
436 176 rhoads
      OS_ThreadSleep(1);
437
}
438
 
439
 
440 138 rhoads
/******************************************/
441 189 rhoads
static void OS_ThreadInit(void *arg)
442 138 rhoads
{
443 167 rhoads
   uint32 cpuIndex = OS_CpuIndex();
444 189 rhoads
   (void)arg;
445 138 rhoads
 
446 168 rhoads
   OS_CriticalEnd(1);
447 167 rhoads
   ThreadCurrent[cpuIndex]->funcPtr(ThreadCurrent[cpuIndex]->arg);
448 138 rhoads
   OS_ThreadExit();
449
}
450
 
451
 
452
/******************************************/
453
//Stops warning "argument X might be clobbered by `longjmp'"
454
static void OS_ThreadRegsInit(jmp_buf env)
455
{
456
   setjmp(env); //ANSI C call to save registers
457
}
458
 
459
 
460
/******************************************/
461 189 rhoads
OS_Thread_t *OS_ThreadCreate(const char *name,
462
                             OS_FuncPtr_t funcPtr,
463
                             void *arg,
464
                             uint32 priority,
465
                             uint32 stackSize)
466 138 rhoads
{
467
   OS_Thread_t *thread;
468
   uint8 *stack;
469
   jmp_buf2 *env;
470
   uint32 state;
471
 
472 256 rhoads
   OS_SemaphorePend(SemaphoreRelease, OS_WAIT_FOREVER);
473 138 rhoads
   if(NeedToFree)
474
      OS_HeapFree(NeedToFree);
475
   NeedToFree = NULL;
476 256 rhoads
   OS_SemaphorePost(SemaphoreRelease);
477 138 rhoads
 
478 189 rhoads
   if(stackSize == 0)
479
      stackSize = STACK_SIZE_DEFAULT;
480
   if(stackSize < STACK_SIZE_MINIMUM)
481
      stackSize = STACK_SIZE_MINIMUM;
482
   thread = (OS_Thread_t*)OS_HeapMalloc(NULL, sizeof(OS_Thread_t) + stackSize);
483 138 rhoads
   assert(thread);
484
   if(thread == NULL)
485
      return NULL;
486 189 rhoads
   memset(thread, 0, sizeof(OS_Thread_t));
487 138 rhoads
   stack = (uint8*)(thread + 1);
488 189 rhoads
   memset(stack, 0xcd, stackSize);
489 138 rhoads
 
490 189 rhoads
   thread->name = name;
491 168 rhoads
   thread->state = THREAD_READY;
492 176 rhoads
   thread->cpuLock = -1;
493 189 rhoads
   thread->funcPtr = funcPtr;
494
   thread->arg = arg;
495
   thread->priority = priority;
496 138 rhoads
   thread->semaphorePending = NULL;
497
   thread->returnCode = 0;
498 189 rhoads
   if(OS_ThreadSelf())
499
   {
500
      thread->processId = OS_ThreadSelf()->processId;
501
      thread->heap = OS_ThreadSelf()->heap;
502
   }
503
   else
504
   {
505
      thread->processId = 0;
506
      thread->heap = NULL;
507
   }
508 138 rhoads
   thread->next = NULL;
509
   thread->prev = NULL;
510
   thread->nextTimeout = NULL;
511
   thread->prevTimeout = NULL;
512
   thread->magic[0] = THREAD_MAGIC;
513
 
514
   OS_ThreadRegsInit(thread->env);
515
   env = (jmp_buf2*)thread->env;
516 200 rhoads
   env->sp = (uint32)stack + stackSize - 24; //minimum stack frame size
517 138 rhoads
   env->pc = (uint32)OS_ThreadInit;
518
 
519
   state = OS_CriticalBegin();
520
   OS_ThreadPriorityInsert(&ThreadHead, thread);
521
   OS_ThreadReschedule(0);
522
   OS_CriticalEnd(state);
523
   return thread;
524
}
525
 
526
 
527
/******************************************/
528
void OS_ThreadExit(void)
529
{
530 167 rhoads
   uint32 state, cpuIndex = OS_CpuIndex();
531 138 rhoads
 
532 306 rhoads
   for(;;)
533
   {
534
      OS_SemaphorePend(SemaphoreRelease, OS_WAIT_FOREVER);
535
      if(NeedToFree)
536
         OS_HeapFree(NeedToFree);
537
      NeedToFree = NULL;
538
      OS_SemaphorePost(SemaphoreRelease);
539
 
540
      state = OS_CriticalBegin();
541
      if(NeedToFree)
542
      {
543
         OS_CriticalEnd(state);
544
         continue;
545
      }
546
      ThreadCurrent[cpuIndex]->state = THREAD_PEND;
547
      NeedToFree = ThreadCurrent[cpuIndex];
548
      OS_ThreadReschedule(0);
549
      OS_CriticalEnd(state);
550
   }
551 138 rhoads
}
552
 
553
 
554
/******************************************/
555
OS_Thread_t *OS_ThreadSelf(void)
556
{
557 167 rhoads
   return ThreadCurrent[OS_CpuIndex()];
558 138 rhoads
}
559
 
560
 
561
/******************************************/
562 189 rhoads
void OS_ThreadSleep(int ticks)
563 138 rhoads
{
564 189 rhoads
   OS_SemaphorePend(SemaphoreSleep, ticks);
565 138 rhoads
}
566
 
567
 
568
/******************************************/
569
uint32 OS_ThreadTime(void)
570
{
571
   return ThreadTime;
572
}
573
 
574
 
575
/******************************************/
576 256 rhoads
void OS_ThreadInfoSet(OS_Thread_t *thread, uint32 index, void *Info)
577 138 rhoads
{
578 256 rhoads
   if(index < INFO_COUNT)
579
      thread->info[index] = Info;
580 138 rhoads
}
581
 
582
 
583
/******************************************/
584 256 rhoads
void *OS_ThreadInfoGet(OS_Thread_t *thread, uint32 index)
585 138 rhoads
{
586 256 rhoads
   if(index < INFO_COUNT)
587
      return thread->info[index];
588
   return NULL;
589 138 rhoads
}
590
 
591
 
592
/******************************************/
593 189 rhoads
uint32 OS_ThreadPriorityGet(OS_Thread_t *thread)
594 138 rhoads
{
595 189 rhoads
   return thread->priority;
596 138 rhoads
}
597
 
598
 
599
/******************************************/
600 189 rhoads
void OS_ThreadPrioritySet(OS_Thread_t *thread, uint32 priority)
601 138 rhoads
{
602
   uint32 state;
603
   state = OS_CriticalBegin();
604 189 rhoads
   thread->priority = priority;
605 256 rhoads
   if(thread->state == THREAD_READY)
606 168 rhoads
   {
607 189 rhoads
      OS_ThreadPriorityRemove(&ThreadHead, thread);
608
      OS_ThreadPriorityInsert(&ThreadHead, thread);
609 168 rhoads
      OS_ThreadReschedule(0);
610
   }
611 138 rhoads
   OS_CriticalEnd(state);
612
}
613
 
614
 
615
/******************************************/
616 189 rhoads
void OS_ThreadProcessId(OS_Thread_t *thread, uint32 processId, OS_Heap_t *heap)
617
{
618
   thread->processId = processId;
619
   thread->heap = heap;
620
}
621
 
622
 
623
/******************************************/
624 138 rhoads
//Must be called with interrupts disabled
625
void OS_ThreadTick(void *Arg)
626
{
627
   OS_Thread_t *thread;
628
   OS_Semaphore_t *semaphore;
629
   int diff;
630
   (void)Arg;
631
 
632
   ++ThreadTime;
633
   while(TimeoutHead)
634
   {
635
      thread = TimeoutHead;
636
      diff = ThreadTime - thread->ticksTimeout;
637
      if(diff < 0)
638
         break;
639
      OS_ThreadTimeoutRemove(thread);
640
      semaphore = thread->semaphorePending;
641
      ++semaphore->count;
642
      thread->semaphorePending = NULL;
643
      thread->returnCode = -1;
644
      OS_ThreadPriorityRemove(&semaphore->threadHead, thread);
645
      OS_ThreadPriorityInsert(&ThreadHead, thread);
646
   }
647
   OS_ThreadReschedule(1);
648
}
649
 
650
 
651
 
652
/***************** Semaphore **************/
653
/******************************************/
654 189 rhoads
OS_Semaphore_t *OS_SemaphoreCreate(const char *name, uint32 count)
655 138 rhoads
{
656
   OS_Semaphore_t *semaphore;
657 189 rhoads
   static int semCount = 0;
658 138 rhoads
 
659
   if(semCount < SEM_RESERVED_COUNT)
660 189 rhoads
      semaphore = &SemaphoreReserved[semCount++];  //Heap not ready yet
661 138 rhoads
   else
662 189 rhoads
      semaphore = (OS_Semaphore_t*)OS_HeapMalloc(HEAP_SYSTEM, sizeof(OS_Semaphore_t));
663 138 rhoads
   assert(semaphore);
664
   if(semaphore == NULL)
665
      return NULL;
666
 
667 189 rhoads
   semaphore->name = name;
668 138 rhoads
   semaphore->threadHead = NULL;
669 189 rhoads
   semaphore->count = count;
670 138 rhoads
   return semaphore;
671
}
672
 
673
 
674
/******************************************/
675 189 rhoads
void OS_SemaphoreDelete(OS_Semaphore_t *semaphore)
676 138 rhoads
{
677 189 rhoads
   while(semaphore->threadHead)
678
      OS_SemaphorePost(semaphore);
679
   OS_HeapFree(semaphore);
680 138 rhoads
}
681
 
682
 
683
/******************************************/
684 189 rhoads
int OS_SemaphorePend(OS_Semaphore_t *semaphore, int ticks)
685 138 rhoads
{
686 167 rhoads
   uint32 state, cpuIndex;
687 138 rhoads
   OS_Thread_t *thread;
688
   int returnCode=0;
689
 
690 189 rhoads
   assert(semaphore);
691 256 rhoads
   assert(InterruptInside[OS_CpuIndex()] == 0);
692 138 rhoads
   state = OS_CriticalBegin();
693 189 rhoads
   if(--semaphore->count < 0)
694 138 rhoads
   {
695 189 rhoads
      if(ticks == 0)
696 138 rhoads
      {
697 189 rhoads
         ++semaphore->count;
698 138 rhoads
         OS_CriticalEnd(state);
699
         return -1;
700
      }
701 189 rhoads
      cpuIndex = OS_CpuIndex();
702 167 rhoads
      thread = ThreadCurrent[cpuIndex];
703 189 rhoads
      assert(thread);
704
      thread->semaphorePending = semaphore;
705
      thread->ticksTimeout = ticks + OS_ThreadTime();
706 256 rhoads
      //FYI: The current thread isn't in the ThreadHead linked list
707 189 rhoads
      OS_ThreadPriorityInsert(&semaphore->threadHead, thread);
708 256 rhoads
      thread->state = THREAD_PEND;
709 189 rhoads
      if(ticks != OS_WAIT_FOREVER)
710 138 rhoads
         OS_ThreadTimeoutInsert(thread);
711
      assert(ThreadHead);
712
      OS_ThreadReschedule(0);
713
      returnCode = thread->returnCode;
714
   }
715
   OS_CriticalEnd(state);
716
   return returnCode;
717
}
718
 
719
 
720
/******************************************/
721 189 rhoads
void OS_SemaphorePost(OS_Semaphore_t *semaphore)
722 138 rhoads
{
723
   uint32 state;
724 146 rhoads
   OS_Thread_t *thread;
725 138 rhoads
 
726 189 rhoads
   assert(semaphore);
727 138 rhoads
   state = OS_CriticalBegin();
728 189 rhoads
   if(++semaphore->count <= 0)
729 138 rhoads
   {
730 189 rhoads
      thread = semaphore->threadHead;
731 138 rhoads
      OS_ThreadTimeoutRemove(thread);
732 189 rhoads
      OS_ThreadPriorityRemove(&semaphore->threadHead, thread);
733 138 rhoads
      OS_ThreadPriorityInsert(&ThreadHead, thread);
734
      thread->semaphorePending = NULL;
735
      thread->returnCode = 0;
736 146 rhoads
      OS_ThreadReschedule(0);
737 138 rhoads
   }
738
   OS_CriticalEnd(state);
739
}
740
 
741
 
742
 
743
/***************** Mutex ******************/
744
/******************************************/
745 189 rhoads
OS_Mutex_t *OS_MutexCreate(const char *name)
746 138 rhoads
{
747
   OS_Mutex_t *mutex;
748
 
749 189 rhoads
   mutex = (OS_Mutex_t*)OS_HeapMalloc(HEAP_SYSTEM, sizeof(OS_Mutex_t));
750 138 rhoads
   if(mutex == NULL)
751
      return NULL;
752 189 rhoads
   mutex->semaphore = OS_SemaphoreCreate(name, 1);
753 138 rhoads
   if(mutex->semaphore == NULL)
754
      return NULL;
755
   mutex->thread = NULL;
756
   mutex->count = 0;
757
   return mutex;
758
}
759
 
760
 
761
/******************************************/
762 189 rhoads
void OS_MutexDelete(OS_Mutex_t *mutex)
763 138 rhoads
{
764 189 rhoads
   OS_SemaphoreDelete(mutex->semaphore);
765
   OS_HeapFree(mutex);
766 138 rhoads
}
767
 
768
 
769
/******************************************/
770 189 rhoads
void OS_MutexPend(OS_Mutex_t *mutex)
771 138 rhoads
{
772
   OS_Thread_t *thread;
773
 
774 189 rhoads
   assert(mutex);
775 138 rhoads
   thread = OS_ThreadSelf();
776 189 rhoads
   if(thread == mutex->thread)
777 138 rhoads
   {
778 189 rhoads
      ++mutex->count;
779 138 rhoads
      return;
780
   }
781 189 rhoads
   OS_SemaphorePend(mutex->semaphore, OS_WAIT_FOREVER);
782
   mutex->thread = thread;
783
   mutex->count = 1;
784 138 rhoads
}
785
 
786
 
787
/******************************************/
788 189 rhoads
void OS_MutexPost(OS_Mutex_t *mutex)
789 138 rhoads
{
790 189 rhoads
   assert(mutex);
791
   assert(mutex->thread == OS_ThreadSelf());
792
   assert(mutex->count > 0);
793
   if(--mutex->count <= 0)
794 138 rhoads
   {
795 189 rhoads
      mutex->thread = NULL;
796
      OS_SemaphorePost(mutex->semaphore);
797 138 rhoads
   }
798
}
799
 
800
 
801
 
802
/***************** MQueue *****************/
803
/******************************************/
804 189 rhoads
OS_MQueue_t *OS_MQueueCreate(const char *name,
805
                             int messageCount,
806
                             int messageBytes)
807 138 rhoads
{
808
   OS_MQueue_t *queue;
809
   int size;
810
 
811 189 rhoads
   size = messageBytes / sizeof(uint32);
812
   queue = (OS_MQueue_t*)OS_HeapMalloc(HEAP_SYSTEM, sizeof(OS_MQueue_t) +
813
      messageCount * size * 4);
814 138 rhoads
   if(queue == NULL)
815
      return queue;
816 189 rhoads
   queue->name = name;
817
   queue->semaphore = OS_SemaphoreCreate(name, 0);
818 138 rhoads
   if(queue->semaphore == NULL)
819
      return NULL;
820 189 rhoads
   queue->count = messageCount;
821 138 rhoads
   queue->size = size;
822
   queue->used = 0;
823
   queue->read = 0;
824
   queue->write = 0;
825
   return queue;
826
}
827
 
828
 
829
/******************************************/
830 189 rhoads
void OS_MQueueDelete(OS_MQueue_t *mQueue)
831 138 rhoads
{
832 189 rhoads
   OS_SemaphoreDelete(mQueue->semaphore);
833
   OS_HeapFree(mQueue);
834 138 rhoads
}
835
 
836
 
837
/******************************************/
838 189 rhoads
int OS_MQueueSend(OS_MQueue_t *mQueue, void *message)
839 138 rhoads
{
840
   uint32 state, *dst, *src;
841
   int i;
842
 
843 189 rhoads
   assert(mQueue);
844
   src = (uint32*)message;
845 138 rhoads
   state = OS_CriticalBegin();
846 189 rhoads
   if(++mQueue->used > mQueue->count)
847 138 rhoads
   {
848 189 rhoads
      --mQueue->used;
849 138 rhoads
      OS_CriticalEnd(state);
850
      return -1;
851
   }
852 189 rhoads
   dst = (uint32*)(mQueue + 1) + mQueue->write * mQueue->size;
853
   for(i = 0; i < mQueue->size; ++i)
854 138 rhoads
      dst[i] = src[i];
855 189 rhoads
   if(++mQueue->write >= mQueue->count)
856
      mQueue->write = 0;
857 138 rhoads
   OS_CriticalEnd(state);
858 189 rhoads
   OS_SemaphorePost(mQueue->semaphore);
859 138 rhoads
   return 0;
860
}
861
 
862
 
863
/******************************************/
864 189 rhoads
int OS_MQueueGet(OS_MQueue_t *mQueue, void *message, int ticks)
865 138 rhoads
{
866
   uint32 state, *dst, *src;
867
   int i, rc;
868
 
869 189 rhoads
   assert(mQueue);
870
   dst = (uint32*)message;
871
   rc = OS_SemaphorePend(mQueue->semaphore, ticks);
872 138 rhoads
   if(rc)
873
      return rc;
874
   state = OS_CriticalBegin();
875 189 rhoads
   --mQueue->used;
876
   src = (uint32*)(mQueue + 1) + mQueue->read * mQueue->size;
877
   for(i = 0; i < mQueue->size; ++i)
878 138 rhoads
      dst[i] = src[i];
879 189 rhoads
   if(++mQueue->read >= mQueue->count)
880
      mQueue->read = 0;
881 138 rhoads
   OS_CriticalEnd(state);
882
   return 0;
883
}
884
 
885
 
886
 
887 223 rhoads
/***************** Jobs *******************/
888
/******************************************/
889
typedef void (*JobFunc_t)();
890
static OS_MQueue_t *jobQueue;
891
static OS_Thread_t *jobThread;
892
 
893
static void JobThread(void *arg)
894
{
895
   uint32 message[4];
896
   JobFunc_t funcPtr;
897
   (void)arg;
898
   for(;;)
899
   {
900
      OS_MQueueGet(jobQueue, message, OS_WAIT_FOREVER);
901
      funcPtr = (JobFunc_t)message[0];
902
      funcPtr(message[1], message[2], message[3]);
903
   }
904
}
905
 
906
 
907
/******************************************/
908
void OS_Job(void (*funcPtr)(), void *arg0, void *arg1, void *arg2)
909
{
910
   uint32 message[4];
911
   int rc;
912
 
913 256 rhoads
   OS_SemaphorePend(SemaphoreLock, OS_WAIT_FOREVER);
914 223 rhoads
   if(jobThread == NULL)
915
   {
916
      jobQueue = OS_MQueueCreate("job", 100, 16);
917
      jobThread = OS_ThreadCreate("job", JobThread, NULL, 150, 4000);
918
   }
919 256 rhoads
   OS_SemaphorePost(SemaphoreLock);
920
 
921 223 rhoads
   message[0] = (uint32)funcPtr;
922
   message[1] = (uint32)arg0;
923
   message[2] = (uint32)arg1;
924
   message[3] = (uint32)arg2;
925 304 rhoads
   rc = OS_MQueueSend(jobQueue, message);
926 223 rhoads
}
927
 
928
 
929 138 rhoads
/***************** Timer ******************/
930
/******************************************/
931 189 rhoads
static void OS_TimerThread(void *arg)
932 138 rhoads
{
933
   uint32 timeNow;
934
   int diff, ticks;
935
   uint32 message[8];
936
   OS_Timer_t *timer;
937 189 rhoads
   (void)arg;
938 138 rhoads
 
939
   timeNow = OS_ThreadTime();
940
   for(;;)
941
   {
942
      //Determine how long to sleep
943
      OS_SemaphorePend(SemaphoreLock, OS_WAIT_FOREVER);
944
      if(TimerHead)
945
         ticks = TimerHead->ticksTimeout - timeNow;
946
      else
947
         ticks = OS_WAIT_FOREVER;
948
      OS_SemaphorePost(SemaphoreLock);
949
      OS_SemaphorePend(SemaphoreTimer, ticks);
950
 
951
      //Send messages for all timed out timers
952
      timeNow = OS_ThreadTime();
953
      for(;;)
954
      {
955 189 rhoads
         timer = NULL;
956 138 rhoads
         OS_SemaphorePend(SemaphoreLock, OS_WAIT_FOREVER);
957 189 rhoads
         if(TimerHead)
958 138 rhoads
         {
959 189 rhoads
            diff = timeNow - TimerHead->ticksTimeout;
960
            if(diff >= 0)
961
               timer = TimerHead;
962 138 rhoads
         }
963 189 rhoads
         OS_SemaphorePost(SemaphoreLock);
964
         if(timer == NULL)
965 138 rhoads
            break;
966
         if(timer->ticksRestart)
967
            OS_TimerStart(timer, timer->ticksRestart, timer->ticksRestart);
968
         else
969
            OS_TimerStop(timer);
970
 
971 218 rhoads
         if(timer->callback)
972
            timer->callback(timer, timer->info);
973
         else
974
         {
975
            //Send message
976
            message[0] = MESSAGE_TYPE_TIMER;
977
            message[1] = (uint32)timer;
978
            message[2] = timer->info;
979
            OS_MQueueSend(timer->mqueue, message);
980
         }
981 138 rhoads
      }
982
   }
983
}
984
 
985
 
986
/******************************************/
987 189 rhoads
OS_Timer_t *OS_TimerCreate(const char *name, OS_MQueue_t *mQueue, uint32 info)
988 138 rhoads
{
989
   OS_Timer_t *timer;
990
 
991
   OS_SemaphorePend(SemaphoreLock, OS_WAIT_FOREVER);
992
   if(SemaphoreTimer == NULL)
993
   {
994
      SemaphoreTimer = OS_SemaphoreCreate("Timer", 0);
995 256 rhoads
      OS_ThreadCreate("Timer", OS_TimerThread, NULL, 250, 2000);
996 138 rhoads
   }
997
   OS_SemaphorePost(SemaphoreLock);
998
 
999 189 rhoads
   timer = (OS_Timer_t*)OS_HeapMalloc(HEAP_SYSTEM, sizeof(OS_Timer_t));
1000 138 rhoads
   if(timer == NULL)
1001
      return NULL;
1002 189 rhoads
   timer->name = name;
1003 218 rhoads
   timer->callback = NULL;
1004 189 rhoads
   timer->mqueue = mQueue;
1005 138 rhoads
   timer->next = NULL;
1006
   timer->prev = NULL;
1007 189 rhoads
   timer->info = info;
1008 138 rhoads
   timer->active = 0;
1009
   return timer;
1010
}
1011
 
1012
 
1013
/******************************************/
1014 189 rhoads
void OS_TimerDelete(OS_Timer_t *timer)
1015 138 rhoads
{
1016 189 rhoads
   OS_TimerStop(timer);
1017
   OS_HeapFree(timer);
1018 138 rhoads
}
1019
 
1020
 
1021
/******************************************/
1022 218 rhoads
void OS_TimerCallback(OS_Timer_t *timer, OS_TimerFuncPtr_t callback)
1023
{
1024
   timer->callback = callback;
1025
}
1026
 
1027
 
1028
/******************************************/
1029 138 rhoads
//Must not be called from an ISR
1030 189 rhoads
void OS_TimerStart(OS_Timer_t *timer, uint32 ticks, uint32 ticksRestart)
1031 138 rhoads
{
1032
   OS_Timer_t *node, *prev;
1033
   int diff, check=0;
1034
 
1035 189 rhoads
   assert(timer);
1036 256 rhoads
   assert(InterruptInside[OS_CpuIndex()] == 0);
1037 189 rhoads
   ticks += OS_ThreadTime();
1038
   if(timer->active)
1039
      OS_TimerStop(timer);
1040 138 rhoads
   OS_SemaphorePend(SemaphoreLock, OS_WAIT_FOREVER);
1041 189 rhoads
   if(timer->active)
1042
   {
1043
      //Prevent race condition
1044
      OS_SemaphorePost(SemaphoreLock);
1045
      return;
1046
   }
1047
   timer->ticksTimeout = ticks;
1048
   timer->ticksRestart = ticksRestart;
1049
   timer->active = 1;
1050 138 rhoads
   prev = NULL;
1051
   for(node = TimerHead; node; node = node->next)
1052
   {
1053 189 rhoads
      diff = ticks - node->ticksTimeout;
1054 138 rhoads
      if(diff <= 0)
1055
         break;
1056
      prev = node;
1057
   }
1058 189 rhoads
   timer->next = node;
1059
   timer->prev = prev;
1060 138 rhoads
   if(node)
1061 189 rhoads
      node->prev = timer;
1062 138 rhoads
   if(prev == NULL)
1063
   {
1064 189 rhoads
      TimerHead = timer;
1065 138 rhoads
      check = 1;
1066
   }
1067
   else
1068 189 rhoads
      prev->next = timer;
1069 138 rhoads
   OS_SemaphorePost(SemaphoreLock);
1070
   if(check)
1071
      OS_SemaphorePost(SemaphoreTimer);
1072
}
1073
 
1074
 
1075
/******************************************/
1076
//Must not be called from an ISR
1077 189 rhoads
void OS_TimerStop(OS_Timer_t *timer)
1078 138 rhoads
{
1079 189 rhoads
   assert(timer);
1080 256 rhoads
   assert(InterruptInside[OS_CpuIndex()] == 0);
1081 138 rhoads
   OS_SemaphorePend(SemaphoreLock, OS_WAIT_FOREVER);
1082 189 rhoads
   if(timer->active)
1083 138 rhoads
   {
1084 189 rhoads
      timer->active = 0;
1085
      if(timer->prev == NULL)
1086
         TimerHead = timer->next;
1087 138 rhoads
      else
1088 189 rhoads
         timer->prev->next = timer->next;
1089
      if(timer->next)
1090
         timer->next->prev = timer->prev;
1091 138 rhoads
   }
1092
   OS_SemaphorePost(SemaphoreLock);
1093
}
1094
 
1095
 
1096
/***************** ISR ********************/
1097
/******************************************/
1098 189 rhoads
void OS_InterruptServiceRoutine(uint32 status, uint32 *stack)
1099 138 rhoads
{
1100
   int i;
1101 256 rhoads
   uint32 state, cpuIndex = OS_CpuIndex();
1102 138 rhoads
 
1103 189 rhoads
   if(status == 0 && Isr[31])
1104
      Isr[31](stack);                   //SYSCALL or BREAK
1105
 
1106 256 rhoads
   InterruptInside[cpuIndex] = 1;
1107 138 rhoads
   i = 0;
1108
   do
1109
   {
1110 189 rhoads
      if(status & 1)
1111 138 rhoads
      {
1112
         if(Isr[i])
1113 189 rhoads
            Isr[i](stack);
1114 138 rhoads
         else
1115
            OS_InterruptMaskClear(1 << i);
1116
      }
1117 189 rhoads
      status >>= 1;
1118 138 rhoads
      ++i;
1119 189 rhoads
   } while(status);
1120 256 rhoads
   InterruptInside[cpuIndex] = 0;
1121 138 rhoads
 
1122 167 rhoads
   state = OS_SpinLock();
1123 256 rhoads
   if(ThreadNeedReschedule[cpuIndex])
1124
      OS_ThreadReschedule(ThreadNeedReschedule[cpuIndex] & 1);
1125 167 rhoads
   OS_SpinUnlock(state);
1126 138 rhoads
}
1127
 
1128
 
1129
/******************************************/
1130 189 rhoads
void OS_InterruptRegister(uint32 mask, OS_FuncPtr_t funcPtr)
1131 138 rhoads
{
1132
   int i;
1133
 
1134
   for(i = 0; i < 32; ++i)
1135
   {
1136 189 rhoads
      if(mask & (1 << i))
1137
         Isr[i] = funcPtr;
1138 138 rhoads
   }
1139
}
1140
 
1141
 
1142
/******************************************/
1143
//Plasma hardware dependent
1144
uint32 OS_InterruptStatus(void)
1145
{
1146
   return MemoryRead(IRQ_STATUS);
1147
}
1148
 
1149
 
1150
/******************************************/
1151
//Plasma hardware dependent
1152 189 rhoads
uint32 OS_InterruptMaskSet(uint32 mask)
1153 138 rhoads
{
1154 189 rhoads
   uint32 state;
1155 138 rhoads
   state = OS_CriticalBegin();
1156 189 rhoads
   mask |= MemoryRead(IRQ_MASK);
1157 138 rhoads
   MemoryWrite(IRQ_MASK, mask);
1158
   OS_CriticalEnd(state);
1159
   return mask;
1160
}
1161
 
1162
 
1163
/******************************************/
1164
//Plasma hardware dependent
1165 189 rhoads
uint32 OS_InterruptMaskClear(uint32 mask)
1166 138 rhoads
{
1167 189 rhoads
   uint32 state;
1168 138 rhoads
   state = OS_CriticalBegin();
1169 189 rhoads
   mask = MemoryRead(IRQ_MASK) & ~mask;
1170 138 rhoads
   MemoryWrite(IRQ_MASK, mask);
1171
   OS_CriticalEnd(state);
1172
   return mask;
1173
}
1174
 
1175
 
1176
/**************** Init ********************/
1177
/******************************************/
1178
static volatile uint32 IdleCount;
1179 189 rhoads
static void OS_IdleThread(void *arg)
1180 138 rhoads
{
1181 189 rhoads
   (void)arg;
1182 138 rhoads
 
1183 151 rhoads
   //Don't block in the idle thread!
1184 138 rhoads
   for(;;)
1185
   {
1186
      ++IdleCount;
1187
   }
1188
}
1189
 
1190
 
1191
/******************************************/
1192
#ifndef DISABLE_IRQ_SIM
1193 189 rhoads
static void OS_IdleSimulateIsr(void *arg)
1194 138 rhoads
{
1195
   uint32 count=0, value;
1196 189 rhoads
   (void)arg;
1197 138 rhoads
 
1198
   for(;;)
1199
   {
1200
      MemoryRead(IRQ_MASK + 4);       //calls Sleep(10)
1201
#if WIN32
1202
      while(OS_InterruptMaskSet(0) & IRQ_UART_WRITE_AVAILABLE)
1203 176 rhoads
         OS_InterruptServiceRoutine(IRQ_UART_WRITE_AVAILABLE, 0);
1204 138 rhoads
#endif
1205 189 rhoads
      value = OS_InterruptMaskSet(0) & 0xf;
1206
      if(value)
1207
         OS_InterruptServiceRoutine(value, 0);
1208 138 rhoads
      ++count;
1209
   }
1210
}
1211
#endif //DISABLE_IRQ_SIM
1212
 
1213
 
1214
/******************************************/
1215
//Plasma hardware dependent
1216 189 rhoads
static void OS_ThreadTickToggle(void *arg)
1217 138 rhoads
{
1218 167 rhoads
   uint32 status, mask, state;
1219 138 rhoads
 
1220 189 rhoads
   //Toggle looking for IRQ_COUNTER18 or IRQ_COUNTER18_NOT
1221 167 rhoads
   state = OS_SpinLock();
1222 138 rhoads
   status = MemoryRead(IRQ_STATUS) & (IRQ_COUNTER18 | IRQ_COUNTER18_NOT);
1223
   mask = MemoryRead(IRQ_MASK) | IRQ_COUNTER18 | IRQ_COUNTER18_NOT;
1224
   mask &= ~status;
1225
   MemoryWrite(IRQ_MASK, mask);
1226 189 rhoads
   OS_ThreadTick(arg);
1227 167 rhoads
   OS_SpinUnlock(state);
1228 138 rhoads
}
1229
 
1230
 
1231
/******************************************/
1232 189 rhoads
void OS_Init(uint32 *heapStorage, uint32 bytes)
1233 138 rhoads
{
1234 393 rhoads
   int i;
1235
   if((int)OS_Init > 0x10000000)        //Running from DDR?
1236
      OS_AsmInterruptInit();            //Patch interrupt vector
1237 138 rhoads
   OS_InterruptMaskClear(0xffffffff);   //Disable interrupts
1238 189 rhoads
   HeapArray[0] = OS_HeapCreate("Default", heapStorage, bytes);
1239
   HeapArray[1] = HeapArray[0];
1240 138 rhoads
   SemaphoreSleep = OS_SemaphoreCreate("Sleep", 0);
1241 256 rhoads
   SemaphoreRelease = OS_SemaphoreCreate("Release", 1);
1242 138 rhoads
   SemaphoreLock = OS_SemaphoreCreate("Lock", 1);
1243 168 rhoads
   for(i = 0; i < OS_CPU_COUNT; ++i)
1244
      OS_ThreadCreate("Idle", OS_IdleThread, NULL, 0, 256);
1245 138 rhoads
#ifndef DISABLE_IRQ_SIM
1246
   if((OS_InterruptStatus() & (IRQ_COUNTER18 | IRQ_COUNTER18_NOT)) == 0)
1247
   {
1248
      //Detected that running in simulator so create SimIsr thread
1249
      UartPrintfCritical("SimIsr\n");
1250
      OS_ThreadCreate("SimIsr", OS_IdleSimulateIsr, NULL, 1, 0);
1251
   }
1252
#endif //DISABLE_IRQ_SIM
1253
   //Plasma hardware dependent
1254 189 rhoads
   OS_InterruptRegister(IRQ_COUNTER18 | IRQ_COUNTER18_NOT, OS_ThreadTickToggle);
1255 138 rhoads
   OS_InterruptMaskSet(IRQ_COUNTER18 | IRQ_COUNTER18_NOT);
1256
}
1257
 
1258
 
1259
/******************************************/
1260
void OS_Start(void)
1261
{
1262
   ThreadSwapEnabled = 1;
1263 167 rhoads
   (void)OS_SpinLock();
1264 138 rhoads
   OS_ThreadReschedule(1);
1265
}
1266
 
1267
 
1268
/******************************************/
1269
//Place breakpoint here
1270
void OS_Assert(void)
1271
{
1272
}
1273
 
1274
 
1275 167 rhoads
#if OS_CPU_COUNT > 1
1276
static uint8 SpinLockArray[OS_CPU_COUNT];
1277
/******************************************/
1278
uint32 OS_CpuIndex(void)
1279
{
1280
   return 0; //0 to OS_CPU_COUNT-1
1281
}
1282
 
1283
 
1284
/******************************************/
1285
//Symmetric Multiprocessing Spin Lock Mutex
1286
uint32 OS_SpinLock(void)
1287
{
1288 168 rhoads
   uint32 state, cpuIndex, i, j, ok, delay;
1289 167 rhoads
 
1290 168 rhoads
   cpuIndex = OS_CpuIndex();
1291
   delay = cpuIndex + 8;
1292 167 rhoads
   state = OS_AsmInterruptEnable(0);
1293
   do
1294
   {
1295
      ok = 1;
1296 168 rhoads
      if(++SpinLockArray[cpuIndex] == 1)
1297 167 rhoads
      {
1298
         for(i = 0; i < OS_CPU_COUNT; ++i)
1299
         {
1300 168 rhoads
            if(i != cpuIndex && SpinLockArray[i])
1301 167 rhoads
               ok = 0;
1302
         }
1303
         if(ok == 0)
1304
         {
1305 168 rhoads
            SpinLockArray[cpuIndex] = 0;
1306
            for(j = 0; j < delay; ++j)  //wait a bit
1307 167 rhoads
               ++i;
1308 168 rhoads
            if(delay < 128)
1309
               delay <<= 1;
1310 167 rhoads
         }
1311
      }
1312
   } while(ok == 0);
1313
   return state;
1314
}
1315
 
1316
 
1317
/******************************************/
1318
void OS_SpinUnlock(uint32 state)
1319
{
1320 168 rhoads
   uint32 cpuIndex;
1321
   cpuIndex = OS_CpuIndex();
1322
   if(--SpinLockArray[cpuIndex] == 0)
1323 167 rhoads
      OS_AsmInterruptEnable(state);
1324 168 rhoads
 
1325
   assert(SpinLockArray[cpuIndex] < 10);
1326 167 rhoads
}
1327 256 rhoads
#endif  //OS_CPU_COUNT > 1
1328 167 rhoads
 
1329
 
1330 336 rhoads
/************** WIN32/Linux Support *************/
1331 138 rhoads
#ifdef WIN32
1332 336 rhoads
#ifdef LINUX
1333
#define putch putchar
1334
#undef _LIBC
1335
#undef kbhit
1336
#undef getch
1337
#define UartPrintf UartPrintf2
1338
#define UartScanf UartScanf2
1339
#include <stdio.h>
1340
#include <stdlib.h>
1341
#include <termios.h>
1342
#include <unistd.h>
1343
void Sleep(unsigned int value)
1344
{
1345
   usleep(value * 1000);
1346
}
1347
 
1348
int kbhit(void)
1349
{
1350
   struct termios oldt, newt;
1351
   struct timeval tv;
1352
   fd_set read_fd;
1353
 
1354
   tcgetattr(STDIN_FILENO, &oldt);
1355
   newt = oldt;
1356
   newt.c_lflag &= ~(ICANON | ECHO);
1357
   tcsetattr(STDIN_FILENO, TCSANOW, &newt);
1358
   tv.tv_sec=0;
1359
   tv.tv_usec=0;
1360
   FD_ZERO(&read_fd);
1361
   FD_SET(0,&read_fd);
1362
   if(select(1, &read_fd, NULL, NULL, &tv) == -1)
1363
      return 0;
1364
   if(FD_ISSET(0,&read_fd))
1365
      return 1;
1366
   return 0;
1367
}
1368
 
1369
int getch(void)
1370
{
1371
   struct termios oldt, newt;
1372
   int ch;
1373
 
1374
   tcgetattr(STDIN_FILENO, &oldt);
1375
   newt = oldt;
1376
   newt.c_lflag &= ~(ICANON | ECHO);
1377
   tcsetattr(STDIN_FILENO, TCSANOW, &newt);
1378
   ch = getchar();
1379
   return ch;
1380
}
1381
#else
1382 138 rhoads
//Support RTOS inside Windows
1383 233 rhoads
#undef kbhit
1384
#undef getch
1385
#undef putch
1386 138 rhoads
extern int kbhit();
1387
extern int getch(void);
1388
extern int putch(int);
1389
extern void __stdcall Sleep(unsigned long value);
1390 336 rhoads
#endif
1391 138 rhoads
 
1392
static uint32 Memory[8];
1393
 
1394 189 rhoads
uint32 MemoryRead(uint32 address)
1395 138 rhoads
{
1396 189 rhoads
   Memory[2] |= IRQ_UART_WRITE_AVAILABLE;    //IRQ_STATUS
1397
   switch(address)
1398 138 rhoads
   {
1399
   case UART_READ:
1400
      if(kbhit())
1401 189 rhoads
         Memory[0] = getch();                //UART_READ
1402 138 rhoads
      Memory[2] &= ~IRQ_UART_READ_AVAILABLE; //clear bit
1403
      return Memory[0];
1404
   case IRQ_MASK:
1405 189 rhoads
      return Memory[1];                      //IRQ_MASK
1406 138 rhoads
   case IRQ_MASK + 4:
1407
      Sleep(10);
1408
      return 0;
1409
   case IRQ_STATUS:
1410
      if(kbhit())
1411
         Memory[2] |= IRQ_UART_READ_AVAILABLE;
1412
      return Memory[2];
1413
   }
1414
   return 0;
1415
}
1416
 
1417 189 rhoads
void MemoryWrite(uint32 address, uint32 value)
1418 138 rhoads
{
1419 189 rhoads
   switch(address)
1420 138 rhoads
   {
1421
   case UART_WRITE:
1422 189 rhoads
      putch(value);
1423 138 rhoads
      break;
1424
   case IRQ_MASK:
1425 189 rhoads
      Memory[1] = value;
1426 138 rhoads
      break;
1427
   case IRQ_STATUS:
1428 189 rhoads
      Memory[2] = value;
1429 138 rhoads
      break;
1430
   }
1431
}
1432
 
1433 189 rhoads
uint32 OS_AsmInterruptEnable(uint32 enableInterrupt)
1434 138 rhoads
{
1435 189 rhoads
   return enableInterrupt;
1436 138 rhoads
}
1437
 
1438
void OS_AsmInterruptInit(void)
1439
{
1440
}
1441
#endif  //WIN32
1442
 
1443
 
1444
/**************** Example *****************/
1445
#ifndef NO_MAIN
1446 218 rhoads
#ifdef WIN32
1447 138 rhoads
static uint8 HeapSpace[1024*512];
1448 218 rhoads
#endif
1449 138 rhoads
 
1450 218 rhoads
int main(int programEnd, char *argv[])
1451 138 rhoads
{
1452 218 rhoads
   (void)programEnd;  //Pointer to end of used memory
1453
   (void)argv;
1454 256 rhoads
 
1455 138 rhoads
   UartPrintfCritical("Starting RTOS\n");
1456 218 rhoads
#ifdef WIN32
1457 138 rhoads
   OS_Init((uint32*)HeapSpace, sizeof(HeapSpace));
1458 218 rhoads
#else
1459
   //Remaining space after program in 1MB external RAM
1460
   OS_Init((uint32*)programEnd,
1461
           RAM_EXTERNAL_BASE + RAM_EXTERNAL_SIZE - programEnd);
1462
#endif
1463 138 rhoads
   UartInit();
1464 223 rhoads
   OS_ThreadCreate("Main", MainThread, NULL, 100, 4000);
1465 138 rhoads
   OS_Start();
1466
   return 0;
1467
}
1468 256 rhoads
#endif  //NO_MAIN
1469 138 rhoads
 

powered by: WebSVN 2.1.0

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