00001 00002 // This file is compiled with WinAVR version 4.1.2 00003 // 00004 // These routines mess with the stack 00005 // 00006 // you must use either of these optimizations: 00007 // -O1 00008 // -O3 00009 // -O2 00010 // 00011 // Whatever you do, do NOT use -O0....it will NOT work 00012 // 00013 // ///////////////////////////////////////////////////////////////////////////////////////// 00014 00015 #include "stdio.h" 00016 #include "stdlib.h" 00017 #include "string.h" 00018 #include "task.h" 00019 #include "pq.h" 00020 #include "HeapManager.h" 00021 00022 00023 00024 /*************************************************************************** 00025 ** PostSemaphore (Unlock) 00026 ** 00027 ** This function is used to incrment the value of a sempaphore 00028 ** 1. Check value of event flag 00029 ** 2. Increment event flag 00030 ** 3. If value is greater than or equal to 0, return 00031 ** 4. Else remove task in pending list and put it into the active task queue 00032 ** 5. return 00033 ** 00034 ** parameters: 00035 ** e.................pointer to event control block for semaphore 00036 ** Value.............Value to return to pending task (should be positive) 00037 ** 00038 ** return value: 0 = No Error, negative indicates error 00039 ***************************************************************************/ 00040 00041 int PostSemaphore(ECB *e, int Value) //signal semaphore availiable 00042 { 00043 TCB *t; 00044 char sr; 00045 00046 sr = Disable(); 00047 if(e->EventCount < 32766) //is it going to overflow?? 00048 { 00049 if(e->EventCount >= 0) //increment semaphore 00050 { 00051 ++e->EventCount; 00052 Enable(sr); 00053 } 00054 else 00055 { 00056 ++e->EventCount; 00057 if(e->task_h) //is there really a task waiting? 00058 { 00059 t = e->task_h; //get first task in 00060 e->task_h = t->next; //remove from list 00061 t->next = (TCB *)0; 00062 t->status = Value; //return this value 00063 Insert(&ActiveTasks,t); //put task back onto active list 00064 Enable(sr); 00065 Yeild(); 00066 } 00067 else 00068 { 00069 Enable(sr); 00070 } 00071 } 00072 return EVENT_NOERROR; 00073 } 00074 else 00075 { 00076 Enable(sr); 00077 return(EVENT_OVERFLOW); 00078 } 00079 } 00080 00081 int PostSemaphoreIrq(ECB *e, int Value) //signal semaphore availiable 00082 { 00083 TCB *t; 00084 00085 if(e->EventCount < 32766) //is it going to overflow?? 00086 { 00087 if(e->EventCount >= 0) //increment semaphore 00088 { 00089 ++e->EventCount; 00090 } 00091 else 00092 { 00093 ++e->EventCount; 00094 if(e->task_h) //is there really a task waiting? 00095 { 00096 t = e->task_h; //get first task in 00097 e->task_h = t->next; //remove from list 00098 t->next = (TCB *)0; 00099 t->status = Value; //return this value 00100 Insert(&ActiveTasks,t); //put task back onto active list 00101 } 00102 } 00103 return EVENT_NOERROR; 00104 } 00105 else 00106 { 00107 return(EVENT_OVERFLOW); 00108 } 00109 } 00110 00111 int PostSemaphoreWithData(ECB *e, int Value,unsigned data) //signal semaphore availiable 00112 { 00113 TCB *t; 00114 int sr; 00115 00116 sr = Disable(); 00117 if(e->EventCount < 32766) //is it going to overflow?? 00118 { 00119 if(e->EventCount++ >= 0) //increment semaphore 00120 { 00121 Enable(sr); 00122 } 00123 else 00124 { 00125 if(e->task_h) //is there really a task waiting? 00126 { 00127 t = e->task_h; //get first task in 00128 e->task_h = t->next; //remove from list 00129 t->next = (TCB *)0; 00130 t->status = Value; //return this value 00131 t->misc = data; //and add this value in as well 00132 Insert(&ActiveTasks,t); //put task back onto active list 00133 Enable(sr); 00134 Yeild(); 00135 } 00136 else 00137 { 00138 Enable(sr); 00139 } 00140 } 00141 return EVENT_NOERROR; 00142 } 00143 else 00144 { 00145 Enable(sr); 00146 return(EVENT_OVERFLOW); 00147 } 00148 }