Download How the Pros Develop Embedded Software
Transcript
* it to the end of the process chain, and sets
* inbuf to NIL.
*
* This routine is always called from the
* DMAIn() interrupt routine.
*/
void PutBuf(void)
{
s16 next,prev;
if(inBuf==NIL) {
HandleError(MISSING_INBUF);
} else if(firstProcess==NIL) {
firstProcess=inBuf;
} else {
prev=NIL;
next=firstProcess;
while(next!=NIL) {
prev=next;
next=buffers[prev].nextBuf;
}
buffers[prev].nextBuf=inBuf;
}
buffers[inBuf].nextBuf=NIL;
inBuf=NIL;
}
/*
* ReceiveBuf
*
*
If calcBuf is not NIL, calls the error handler.
*
If there is no buffer in the process list, returns
*
NULL to the caller.
*
If there is a buffer in the process list, removes
*
the first such buffer, places its index into calcBuf,
*
and returns a pointer to the buffer.
*
*
This routine is called in the background, and so must
*
disable interrupts while removing the buffer from the
*
process list, in case the DMAIn() interrupt should happen
*
while it is doing it.
*/
BUF *ReceiveBuf(void)
{
BUF *dabuf;
DisableInterrupts();
if(calcBuf!=NIL) {
HandleError(CALC_IN_PROGRESS); // don't receive until calc finished
dabuf=NULL;
// wait for calc position to be free
} else if(firstProcess==NIL) {
dabuf=NULL;
} else {
calcBuf=firstProcess;
dabuf=&buffers[calcBuf];
firstProcess=dabuf->nextBuf;
dabuf->nextBuf=NIL;
}
EnableInterrupts();
return dabuf;
}
/*
* OutputBuf
*
*
If calcBuf is NIL, this routine calls the
* error handler. Otherwise, it puts the buffer
* indicated by calcBuf onto the output buffer
* list, and sets calcBuf to NIL.
*
* This routine is called in the background, and so must
* disable interrupts while adding the buffer to the
* output list, in case the DMAOut() interrupt should happen
* while it is doing it.
*/
void OutputBuf(void)
{
s16 prev,next;
DisableInterrupts();
if(calcBuf==NIL) {
HandleError(NO_BUF_TO_OUTPUT);
} else if(firstOut==NIL) {
firstOut=calcBuf;
calcBuf=NIL;
buffers[firstOut].nextBuf=NIL;
} else {
prev=NIL;
next=firstOut;
while(next!=NIL) {
prev=next;
next=buffers[prev].nextBuf;