ECE/CS 5780/6780: Embedded System DesignScott R. LittleLecture 9: FIFOsScott R. Little (Lecture 9: FIFOs) ECE/CS 5780/6780 1 / 20AdministriviaCodeWarrior IDE compiler optimization configuration.No labs are scheduled next week. Please contact your TA if youwould like to meet them.Scott R. Little (Lecture 9: FIFOs) ECE/CS 5780/6780 2 / 20Introduction to FIFOsFIFO circular queue is useful for a buffered I/O interface.This order-preserving data structure temporarily saves datacreated by a producer before being processed by a consumer.Decouples the producer from the consumer.Use statically allocated global memory, so they can be shared bythreads, but must be accessed carefully.Scott R. Little (Lecture 9: FIFOs) ECE/CS 5780/6780 3 / 20FIFO with Infinite MemoryScott R. Little (Lecture 9: FIFOs) ECE/CS 5780/6780 4 / 20Basic Idea of a FIFOchar static volatile *PutPt; // put nextchar static volatile *GetPt; // get next// call by valueint Fifo_Put(char data){*PutPt = data; // PutPutPt++; // nextreturn(1); // true if success}// call by referenceint Fifo_Get(char *datapt){*datapt = *GetPt; // return by referenceGetPt++; // nextreturn(1); // true if success}Scott R. Little (Lecture 9: FIFOs) ECE/CS 5780/6780 5 / 20Two-Pointer FIFOScott R. Little (Lecture 9: FIFOs) ECE/CS 5780/6780 6 / 20Two-Pointer FIFOScott R. Little (Lecture 9: FIFOs) ECE/CS 5780/6780 7 / 20Initialization of a Two-Pointer FIFO#define FIFOSIZE 10 /* can hold 9 */char static volatile *PutPt; /* Pointer to put next */char static volatile *GetPt; /* Pointer to get next/* FIFO is empty if PutPt=GetPt *//* FIFO is full if PutPt+1=GetPt (with wrap) */char static Fifo[FIFOSIZE];void Fifo_Init(void) {unsigned char SaveSP;asm tpaasm staa SaveSPasm sei /* make atomic, entering critical */PutPt=GetPt=&Fifo[0]; /* Empty when PutPt=GetPt */asm ldaa SaveSPasm tap /* end critical section */}Scott R. Little (Lecture 9: FIFOs) ECE/CS 5780/6780 8 / 20Put for a Two-Pointer FIFOint Fifo_Put(char data) { char *Ppt; /* Temp put pointer */unsigned char SaveSP;asm tpaasm staa SaveSPasm sei /* make atomic, entering critical */Ppt=PutPt; /* Copy of put pointer */*(Ppt++)=data; /* Try to put data into fifo */if (Ppt == &Fifo[FIFOSIZE]) Ppt = &Fifo[0]; /* Wrap */if (Ppt == GetPt ) {asm ldaa SaveSPasm tap /* end critical section */return(0); /* Failed, fifo was full */} else {PutPt=Ppt;asm ldaa SaveSPasm tap /* end critical section */return(1); } /* Successful */}Scott R. Little (Lecture 9: FIFOs) ECE/CS 5780/6780 9 / 20Put for a Two-Pointer FIFO Exampledata = 0x040xXXGetPt → 0x010x020x03PutPt → 0xXX0xXXint Fifo Put(char data) {char *Ppt;unsigned char SaveSP;asm tpaasm staa SaveSPasm seiPpt=PutPt;*(Ppt++)=data;if (Ppt == &Fifo[FIFOSIZE])Ppt = &Fifo[0];if (Ppt == GetPt ) {asm ldaa SaveSPasm tapreturn(0);} else {PutPt=Ppt;asm ldaa SaveSPasm tapreturn(1);}}Scott R. Little (Lecture 9: FIFOs) ECE/CS 5780/6780 10 / 20Put for a Two-Pointer FIFO Exampledata = 0x040xXXGetPt → 0x010x020x03PutPt/Ppt → 0xXX0xXXint Fifo Put(char data) {char *Ppt;unsigned char SaveSP;asm tpaasm staa SaveSPasm seiPpt=PutPt;*(Ppt++)=data;if (Ppt == &Fifo[FIFOSIZE])Ppt = &Fifo[0];if (Ppt == GetPt ) {asm ldaa SaveSPasm tapreturn(0);} else {PutPt=Ppt;asm ldaa SaveSPasm tapreturn(1);}}Scott R. Little (Lecture 9: FIFOs) ECE/CS 5780/6780 11 / 20Put for a Two-Pointer FIFO Exampledata = 0x040xXXGetPt → 0x010x020x03PutPt → 0x04Ppt → 0xXXint Fifo Put(char data) {char *Ppt;unsigned char SaveSP;asm tpaasm staa SaveSPasm seiPpt=PutPt;*(Ppt++)=data;if (Ppt == &Fifo[FIFOSIZE])Ppt = &Fifo[0];if (Ppt == GetPt ) {asm ldaa SaveSPasm tapreturn(0);} else {PutPt=Ppt;asm ldaa SaveSPasm tapreturn(1);}}Scott R. Little (Lecture 9: FIFOs) ECE/CS 5780/6780 12 / 20Put for a Two-Pointer FIFO Exampledata = 0x04Ppt → 0xXXGetPt → 0x010x020x03PutPt → 0x040xXXint Fifo Put(char data) {char *Ppt;unsigned char SaveSP;asm tpaasm staa SaveSPasm seiPpt=PutPt;*(Ppt++)=data;if (Ppt == &Fifo[FIFOSIZE])Ppt = &Fifo[0];if (Ppt == GetPt ) {asm ldaa SaveSPasm tapreturn(0);} else {PutPt=Ppt;asm ldaa SaveSPasm tapreturn(1);}}Scott R. Little (Lecture 9: FIFOs) ECE/CS 5780/6780 13 / 20Put for a Two-Pointer FIFO Exampledata = 0x04PutPt → 0xXXGetPt → 0x010x020x030x040xXXint Fifo Put(char data) {char *Ppt;unsigned char SaveSP;asm tpaasm staa SaveSPasm seiPpt=PutPt;*(Ppt++)=data;if (Ppt == &Fifo[FIFOSIZE])Ppt = &Fifo[0];if (Ppt == GetPt ) {asm ldaa SaveSPasm tapreturn(0);} else {PutPt=Ppt;asm ldaa SaveSPasm tapreturn(1);}}Scott R. Little (Lecture 9: FIFOs) ECE/CS 5780/6780 14 / 20Get for a Two-Pointer FIFOint Fifo_Get(char *datapt) {unsigned char SaveSP;if (PutPt == GetPt ) {return(0); /* Empty if PutPt=GetPt */} else {asm tpaasm staa SaveSPasm sei /* make atomic, entering critical */*datapt=*(GetPt++);if (GetPt == &Fifo[FIFOSIZE]) GetPt = &Fifo[0]; /* Wrap */asm ldaa SaveSPasm tap /* end critical section */return(1);}}Scott R. Little (Lecture 9: FIFOs) ECE/CS 5780/6780 15 / 20Initialization of a Two-Pointer/Counter FIFO#define FIFOSIZE 10 /* can hold 10 */char static volatile *PutPt; /* Pointer to put next */char static volatile *GetPt; /* Pointer to get next */char Fifo[FIFOSIZE];unsigned char Size; /* Number of elements */void Fifo_Init(void) {unsigned char SaveSP;asm tpaasm staa SaveSPasm sei /* make atomic, entering critical */PutPt=GetPt=&Fifo[0]; /* Empty when Size==0 */Size=0;asm ldaa SaveSPasm tap /* end critical section */}Scott R. Little (Lecture 9: FIFOs) ECE/CS 5780/6780 16 / 20Put for a Two-Pointer/Counter FIFOint Fifo_Put(char data) {unsigned char SaveSP;if (Size == FIFOSIZE ) {return(0); /* Failed, fifo was full */} else {asm tpaasm staa SaveSPasm sei /* make atomic, entering critical */Size++;*(PutPt++)=data; /* put data into fifo */if (PutPt == &Fifo[FIFOSIZE]) {PutPt = &Fifo[0]; /* Wrap */}asm ldaa SaveSPasm tap /* end critical section */return(1); /* Successful */}}Scott R. Little (Lecture 9: FIFOs) ECE/CS 5780/6780 17 / 20Get for a Two-Pointer/Counter FIFOint Fifo_Get (char *datapt) {unsigned char SaveSP;if (Size == 0 ){return(0); /* Empty if Size=0 */} else {asm tpaasm staa SaveSPasm sei /* make atomic, entering critical */*datapt=*(GetPt++);Size--;if (GetPt == &Fifo[FIFOSIZE]) {GetPt = &Fifo[0]; /* Wrap */}asm ldaa SaveSPasm tap /* end critical section */return(1); }Scott R. Little (Lecture 9: FIFOs) ECE/CS 5780/6780 18 / 20FIFO DynamicsRates of production/consumption vary dynamically.tpis time between Put calls, rpis arrival rate (rp=1tp).tgis time between Get calls, rgis service rate
View Full Document