/************************************* Fifo queue implemented with pointers source - CD in valvano's book (F:\embed\chap7\chap7.htm) **************************************/ #define FifoSize 10 // Number of 8 bit data in the Fifo #define START_CRITICAL() asm(" tpa\n staa %SaveSP\n sei") #define END_CRITICAL() asm( ldaa %SaveSP\n tap") char *PUTPT; // Pointer of where to put next char *GETPT; // Pointer of where to get next // FIFO is empty if PUTPT=GETPT // FIFO is full if PUTPT+1=GETPT char Fifo[FifoSize]; // The statically allocated fifo data void InitFifo(void) { unsigned char SaveSP; START_CRITICAL(); // make atomic, entering critical section PUTPT=GETPT=&Fifo[0]; // Empty when PUTPT=GETPT END_CRITICAL(); // end critical section } int PutFifo (char data) { char *Ppt; // Temporary put pointer unsigned char SaveSP; START_CRITICAL(); // make atomic, entering critical section Ppt=PUTPT; // Copy of put pointer *(Ppt++)=data; // Try to put data into fifo [MIDE comments - isn this kinda stupid??? // cuz it writes data to a mem location that might have something valuable] if (Ppt == &Fifo[FifoSize]) Ppt = &Fifo[0]; // Wrap if (Ppt == GETPT ) { END_CRITICAL(); // end critical section return(0); // Failed, fifo was full [MIDE comments -this will like happen if u keep // putting stuff in and not getting it out. PUTPT will loop round to meet GETPT] } else { PUTPT=Ppt; END_CRITICAL(); // end critical section return(-1); // Successful } } int GetFifo (char *datapt) { unsigned char SaveSP; if (PUTPT == GETPT) return(0); // Empty if PUTPT=GETPT else { START_CRITICAL(); // make atomic, entering critical section *datapt = *(GETPT++); if (GETPT == &Fifo[FifoSize]) GETPT = &Fifo[0]; END_CRITICAL(); // end critical section return(-1); } } /********************************************************************** A FIFO Queue Example using indices source - Listing 8-7: FIFO implemented with two indices and a counter The START_CRITICAL and END_CRITICAL macros are specific to ICC11/ICC12, otherwise this example will operate using Hiware. ***********************************************************************/ //Another method to implement a statically allocated first-in-first-out FIFO is to use indices instead of pointers. //This method is necessary for compilers that do not support pointers. The purpose of this example is to illustrate //the use of arrays and indices. Just like the previous FIFO, this is used for order-preserving temporary storage. //The function PutFifo will enter one 8-bit byte into the queue, and GetFifo will remove one byte. If you call PutFifo //while the FIFO is full (Size is equal to FifoSize), the routine will return a zero. Otherwise, PutFifo will save the //data in the queue and return a one. The index PutI specifies where to put the next 8-bit data. The routine GetFifo //actually returns two parameters. The queue status is the regular function return parameter, while the data removed //from the queue is return by reference. I.e., the calling routine passes in a pointer, and GetFifo stores the removed //data at that address. If you call GetFifo while the FIFO is empty (Size is equal to zero), the routine will return a //zero. Otherwise, GetFifo will return the oldest data from the queue and return a one. The index GetI specifies where //to get the next 8-bit data. The following FIFO implementation uses two indices and a counter. /* Index,counter implementation of the FIFO */ #define FifoSize 10 /* Number of 8 bit data in the Fifo */ #define START_CRITICAL() asm(" tpa\n staa %SaveSP\n sei") #define END_CRITICAL() asm( ldaa %SaveSP\n tap") unsigned char PutI; /* Index of where to put next */ unsigned char GetI; /* Index of where to get next */ unsigned char Size; /* Number currently in the FIFO */ /* FIFO is empty if Size=0 */ /* FIFO is full if Size=FifoSize */ char Fifo[FifoSize]; /* The statically allocated data */ void InitFifo(void) { unsigned char SaveSP; START_CRITICAL(); /* make atomic, entering critical section */ PutI=GetI=Size=0; /* Empty when Size==0 */ asm(" cli"); /* end critical section */ } int PutFifo (char data) { unsigned char SaveSP; if (Size == FifoSize) return(0); /* Failed, fifo was full */ else { START_CRITICAL(); /* make atomic, entering critical section */ Size++; Fifo[PutI++]=data; /* put data into fifo */ if (PutI == FifoSize) PutI = 0; /* Wrap */ END_CRITICAL(); /* end critical section */ return(-1); /* Successful */ } } int GetFifo (char *datapt) { unsigned char SaveSP; if (Size == 0 ) return(0); /* Empty if Size=0 */ else { START_CRITICAL(); /* make atomic, entering critical section */ *datapt=Fifo[GetI++]; Size--; if (GetI == FifoSize) GetI = 0; END_CRITICAL(); /* end critical section */ return(-1); } }