00001 //******************************************************** 00002 // 00003 // ByteQueue.c 00004 // 00005 //******************************************************** 00006 00007 #include "stdio.h" 00008 #include "task.h" 00009 #include "ByteQueue.h" 00010 00017 00038 void CreateBQ(BQ *pBQ,char *b,unsigned size) 00039 { 00040 pBQ->task_h = NULL; 00041 pBQ->pBfr = b; 00042 pBQ->Size = size; 00043 pBQ->Head = 0; 00044 pBQ->Tail = 0; 00045 pBQ->nChar = 0; 00046 } 00047 00060 int BQget(BQ *pBQ) 00061 { 00062 //****************************************************** 00063 // Get a character from the queue 00064 // 00065 // 00066 // parameters: 00067 // pBQ........pointer to the queue to get a char from 00068 // 00069 // return value: 00070 // returns a negative value on error 00071 // returns a positive value on success 00072 // suspends task if there are no characters to get 00073 //****************************************************** 00074 char sr; 00075 int retval; 00076 00077 sr = Disable(); 00078 if(pBQ->nChar) //are there any characters to get? 00079 { 00080 //------------------------------------- 00081 // Get Character from Buffer 00082 // Check Head pointer for Wrap 00083 // Increment number of chars in buffer 00084 //------------------------------------ 00085 retval = (int)((unsigned char)pBQ->pBfr[pBQ->Head++]); 00086 if(pBQ->Head == pBQ->Size) pBQ->Head = 0; 00087 pBQ->nChar++; 00088 Enable(sr); 00089 } 00090 else //no, suspend task 00091 { 00092 pBQ->task_h = CurrentTask; 00093 CurrentTask->status = EVENT_NOERROR; 00094 //--------------------------------- 00095 //swap out current task 00096 //--------------------------------- 00097 CurrentTask->TimeStamp = TStamp; 00098 if(Delete(&ActiveTasks,(void **)&NextTask)) //get highest priority task 00099 { 00100 NextTask->TcbSwaps++; 00101 DoSwap(); //do context swap 00102 } 00103 else 00104 { 00105 CurrentTask->status = EVENT_NOTASKS; 00106 } 00107 retval = CurrentTask->status; 00108 Enable(sr); 00109 } 00110 return retval; 00111 } 00112 00132 int BQPut(BQ *pBQ,int c) 00133 { 00134 //------------------------------------- 00135 // Put a character into the queue 00136 // 00137 // This function is intended to be used 00138 // inside of an interrupt routine to 00139 // signal a waiting task that data is 00140 // ready. It can also be used at the 00141 // task level, however if the buffer 00142 // overflows, we just do nothing (well 00143 // we return an error...but that is it 00144 // Interrupt routines cannot care if 00145 // the queue is full 00146 // 00147 // parameters: 00148 // pBQ.....pointer to queue to put data into 00149 // c.......character to add to queue 00150 // 00151 // return value: 00152 // 0 on success 00153 // negative value on fail 00154 //-------------------------------------- 00155 char sr; 00156 int retval = EVENT_NOERROR; 00157 00158 sr = Disable(); //just in case we are at task level 00159 if(pBQ->nChar < pBQ->Size) //any place for a character? 00160 { 00161 pBQ->pBfr[pBQ->Tail++] = (char)c; 00162 if(pBQ->Tail == pBQ->Size) pBQ->Tail = 0; 00163 pBQ->nChar++; 00164 } 00165 else 00166 { 00167 retval = EVENT_OVERFLOW; 00168 } 00169 if(pBQ->task_h) //is there a task pending? 00170 { 00171 pBQ->task_h->status = retval; //set its status to retval 00172 Insert(&ActiveTasks,pBQ->task_h); //put task back onto active list 00173 pBQ->task_h = NULL; 00174 Enable(sr); //enable interrupts 00175 Yeild(); //if we are at task level...we will yeild 00176 } 00177 else 00178 Enable(sr); 00179 return retval; 00180 } 00181 00182 int BQWrite(BQ *pBQ,char *b,int n) 00183 { 00184 return 0; 00185 } 00186