This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

can anyone help me to understand the application of the header file.

I am reading the FiFO code for the text book of Valvano, this code is used for implemented the  FIFO, and there is a description of the code

"First in first out queue, pointer method and index method Provide functions that initialize a FIFO, put data in, get data out, and return the current size. The file includes an index and a pointer implementation and macros to create more FIFOs. Periodic interrupts are used to verify the FIFO has no critical sections."

 can anyone help me with the code? what all these ## means?

// FIFO.h

// Runs on any LM3Sxxx

// Provide functions that initialize a FIFO, put data in, get data out,

// and return the current size.  The file includes a transmit FIFO

// using index implementation and a receive FIFO using pointer

// implementation.  Other index or pointer implementation FIFOs can be

// created using the macros supplied at the end of the file.

// Daniel Valvano

// June 16, 2011



/* This example accompanies the book

   "Embedded Systems: Real Time Interfacing to the Arm Cortex M3",

   ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2011

      Programs 3.7, 3.8., 3.9 and 3.10 in Section 3.7



 Copyright 2011 by Jonathan W. Valvano, valvano@mail.utexas.edu

    You may use, edit, run or distribute this file

    as long as the above copyright notice remains

 THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED

 OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF

 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.

 VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,

 OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.

 For more information about my classes, my research, and my books, see

 http://users.ece.utexas.edu/~valvano/

 */



#ifndef __FIFO_H__

#define __FIFO_H__



long StartCritical (void);    // previous I bit, disable interrupts

void EndCritical(long sr);    // restore I bit to previous value



// Two-index implementation of the transmit FIFO

// can hold 0 to TXFIFOSIZE elements

#define TXFIFOSIZE 16 // must be a power of 2

#define TXFIFOSUCCESS 1

#define TXFIFOFAIL    0

typedef char txDataType;



// initialize index FIFO

void TxFifo_Init(void);

// add element to end of index FIFO

// return TXFIFOSUCCESS if successful

int TxFifo_Put(txDataType data);

// remove element from front of index FIFO

// return TXFIFOSUCCESS if successful

int TxFifo_Get(txDataType *datapt);

// number of elements in index FIFO

// 0 to TXFIFOSIZE-1

uint32_t TxFifo_Size(void);



// Two-pointer implementation of the receive FIFO

// can hold 0 to RXFIFOSIZE-1 elements

#define RXFIFOSIZE 16 // can be any size

#define RXFIFOSUCCESS 1

#define RXFIFOFAIL    0



typedef char rxDataType;



// initialize pointer FIFO

void RxFifo_Init(void);

// add element to end of pointer FIFO

// return RXFIFOSUCCESS if successful

int RxFifo_Put(rxDataType data);

// remove element from front of pointer FIFO

// return RXFIFOSUCCESS if successful

int RxFifo_Get(rxDataType *datapt);

// number of elements in pointer FIFO

// 0 to RXFIFOSIZE-1

uint32_t RxFifo_Size(void);



// macro to create an index FIFO

#define AddIndexFifo(NAME,SIZE,TYPE,SUCCESS,FAIL) \

uint32_t volatile NAME ## PutI;    \

uint32_t volatile NAME ## GetI;    \

TYPE static NAME ## Fifo [SIZE];        \

void NAME ## Fifo_Init(void){ long sr;  \

  sr = StartCritical();                 \

  NAME ## PutI = NAME ## GetI = 0;      \

  EndCritical(sr);                      \

}                                       \

int NAME ## Fifo_Put (TYPE data){       \

  if(( NAME ## PutI - NAME ## GetI ) & ~(SIZE-1)){  \

    return(FAIL);      \

  }                    \

  NAME ## Fifo[ NAME ## PutI &(SIZE-1)] = data; \

  NAME ## PutI ## ++;  \

  return(SUCCESS);     \

}                      \

int NAME ## Fifo_Get (TYPE *datapt){  \

  if( NAME ## PutI == NAME ## GetI ){ \

    return(FAIL);      \

  }                    \

  *datapt = NAME ## Fifo[ NAME ## GetI &(SIZE-1)];  \

  NAME ## GetI ## ++;  \

  return(SUCCESS);     \

}                      \

unsigned short NAME ## Fifo_Size (void){  \

 return ((uint32_t)( NAME ## PutI - NAME ## GetI ));  \

}

// e.g.,

// AddIndexFifo(Tx,32,unsigned char, 1,0)

// SIZE must be a power of two

// creates TxFifo_Init() TxFifo_Get() and TxFifo_Put()



// macro to create a pointer FIFO

#define AddPointerFifo(NAME,SIZE,TYPE,SUCCESS,FAIL) \

TYPE volatile *NAME ## PutPt;    \

TYPE volatile *NAME ## GetPt;    \

TYPE static NAME ## Fifo [SIZE];        \

void NAME ## Fifo_Init(void){ long sr;  \

  sr = StartCritical();                 \

  NAME ## PutPt = NAME ## GetPt = &NAME ## Fifo[0]; \

  EndCritical(sr);                      \

}                                       \

int NAME ## Fifo_Put (TYPE data){       \

  TYPE volatile *nextPutPt;             \

  nextPutPt = NAME ## PutPt + 1;        \

  if(nextPutPt == &NAME ## Fifo[SIZE]){ \

    nextPutPt = &NAME ## Fifo[0];       \

  }                                     \

  if(nextPutPt == NAME ## GetPt ){      \

    return(FAIL);                       \

  }                                     \

  else{                                 \

    *( NAME ## PutPt ) = data;          \

    NAME ## PutPt = nextPutPt;          \

    return(SUCCESS);                    \

  }                                     \

}                                       \

int NAME ## Fifo_Get (TYPE *datapt){    \

  if( NAME ## PutPt == NAME ## GetPt ){ \

    return(FAIL);                       \

  }                                     \

  *datapt = *( NAME ## GetPt ## ++);    \

  if( NAME ## GetPt == &NAME ## Fifo[SIZE]){ \

    NAME ## GetPt = &NAME ## Fifo[0];   \

  }                                     \

  return(SUCCESS);                      \

}                                       \

unsigned short NAME ## Fifo_Size (void){\

  if( NAME ## PutPt < NAME ## GetPt ){  \

    return ((uint32_t)( NAME ## PutPt - NAME ## GetPt + (SIZE*sizeof(TYPE)))/sizeof(TYPE)); \

  }                                     \

  return ((uint32_t)( NAME ## PutPt - NAME ## GetPt )/sizeof(TYPE)); \

}

// e.g.,

// AddPointerFifo(Rx,32,unsigned char, 1,0)

// SIZE can be any size

// creates RxFifo_Init() RxFifo_Get() and RxFifo_Put()



#endif //  __FIFO_H__