/* ****************SFIFO.C******** FIFO using semaphores This example accompanies the book "Embedded Microcomputer Systems: Real Time Interfacing", Brooks-Cole, copyright (c) 2000, Jonathan W. Valvano 7/6/99 Interface between the TExaS simulator running a MC68HC11 EVB with ICC11 freeware compiler TExaS Copyright 1999 by Jonathan W. Valvano for more information about ICC11 see http://www.imagecraft.com You may use this file without restrictions */ unsigned int *PutPt; /* Pointer of where to put next */ unsigned int *GetPt; /* Pointer of where to get next */ /* FIFO is empty when Available is less than or equal to zero */ /* FIFO is full when RoomLeft is less than or equal to zero */ int RoomLeft; /* Semaphore counting empty spaces */ int Available; /* Semaphore counting data in fifo */ unsigned int Fifo[FifoSize]; /* The statically allocated fifo data */ void InitFifo(void) { RoomLeft=FifoSize-1; /* Maximum storage capability */ Available=0; /* Initially empty */ PutPt=GetPt=&Fifo[0]; } void Put(unsigned int data) { Wait(&RoomLeft); asm(" sei"); /* read modify write nonreentrant code */ PORTC|=1; *(PutPt++)=data; /* Put data into fifo */ if (PutPt==&Fifo[FifoSize]) PutPt = &Fifo[0]; PORTC&=0xFE; asm(" cli"); Signal(&Available); } unsigned int Get(void) { unsigned int data; Wait(&Available); asm(" sei"); /* read modify write nonreentrant code */ PORTC|=2; data=*(GetPt++); if (GetPt==&Fifo[FifoSize]) GetPt = &Fifo[0]; PORTC&=0xFD; asm(" cli"); Signal(&RoomLeft); return(data);}