RTOS_MSPM0  1.1
ECE445M starter code
FIFO2.h
1 // FIFO.h
2 // Runs on any LM3Sxxx
3 // Provide functions that initialize a FIFO, put data in, get data out,
4 // and return the current size. The file includes a transmit FIFO
5 // using index implementation and a receive FIFO using pointer
6 // implementation. Other index or pointer implementation FIFOs can be
7 // created using the macros supplied at the end of the file.
8 // Daniel Valvano
9 // June 16, 2011
10 
11 /* This example accompanies the book
12  "Embedded Systems: Real Time Interfacing to Arm Cortex M Microcontrollers",
13  ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2015
14  Programs 3.7, 3.8., 3.9 and 3.10 in Section 3.7
15 
16  Copyright 2015 by Jonathan W. Valvano, valvano@mail.utexas.edu
17  You may use, edit, run or distribute this file
18  as long as the above copyright notice remains
19  THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
20  OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
21  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
22  VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
23  OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
24  For more information about my classes, my research, and my books, see
25  http://users.ece.utexas.edu/~valvano/
26  */
27 
28 #ifndef __FIFO_H__
29 #define __FIFO_H__
30 
31 
32 // macro to create an index FIFO
33 #define AddIndexFifo(NAME,SIZE,TYPE,SUCCESS,FAIL) \
34 uint32_t volatile NAME ## PutI; \
35 uint32_t volatile NAME ## GetI; \
36 TYPE static NAME ## Fifo [SIZE]; \
37 void NAME ## Fifo_Init(void){ \
38  NAME ## PutI = NAME ## GetI = 0; \
39 } \
40 int NAME ## Fifo_Put (TYPE data){ \
41  if(( NAME ## PutI - NAME ## GetI ) & ~(SIZE-1)){ \
42  return(FAIL); \
43  } \
44  NAME ## Fifo[ NAME ## PutI &(SIZE-1)] = data; \
45  NAME ## PutI ## ++; \
46  return(SUCCESS); \
47 } \
48 int NAME ## Fifo_Get (TYPE *datapt){ \
49  if( NAME ## PutI == NAME ## GetI ){ \
50  return(FAIL); \
51  } \
52  *datapt = NAME ## Fifo[ NAME ## GetI &(SIZE-1)]; \
53  NAME ## GetI ## ++; \
54  return(SUCCESS); \
55 } \
56 unsigned short NAME ## Fifo_Size (void){ \
57  return ((unsigned short)( NAME ## PutI - NAME ## GetI )); \
58 }
59 // e.g.,
60 // AddIndexFifo(Tx,32,unsigned char, 1,0)
61 // SIZE must be a power of two
62 // creates TxFifo_Init() TxFifo_Get() and TxFifo_Put()
63 
64 // macro to create a pointer FIFO
65 #define AddPointerFifo(NAME,SIZE,TYPE,SUCCESS,FAIL) \
66 TYPE volatile *NAME ## PutPt; \
67 TYPE volatile *NAME ## GetPt; \
68 TYPE static NAME ## Fifo [SIZE]; \
69 void NAME ## Fifo_Init(void){ long sr; \
70  sr = StartCritical(); \
71  NAME ## PutPt = NAME ## GetPt = &NAME ## Fifo[0]; \
72  EndCritical(sr); \
73 } \
74 int NAME ## Fifo_Put (TYPE data){ \
75  TYPE volatile *nextPutPt; \
76  nextPutPt = NAME ## PutPt + 1; \
77  if(nextPutPt == &NAME ## Fifo[SIZE]){ \
78  nextPutPt = &NAME ## Fifo[0]; \
79  } \
80  if(nextPutPt == NAME ## GetPt ){ \
81  return(FAIL); \
82  } \
83  else{ \
84  *( NAME ## PutPt ) = data; \
85  NAME ## PutPt = nextPutPt; \
86  return(SUCCESS); \
87  } \
88 } \
89 int NAME ## Fifo_Get (TYPE *datapt){ \
90  if( NAME ## PutPt == NAME ## GetPt ){ \
91  return(FAIL); \
92  } \
93  *datapt = *( NAME ## GetPt ## ++); \
94  if( NAME ## GetPt == &NAME ## Fifo[SIZE]){ \
95  NAME ## GetPt = &NAME ## Fifo[0]; \
96  } \
97  return(SUCCESS); \
98 } \
99 unsigned short NAME ## Fifo_Size (void){\
100  if( NAME ## PutPt < NAME ## GetPt ){ \
101  return ((unsigned short)( NAME ## PutPt - NAME ## GetPt + (SIZE*sizeof(TYPE)))/sizeof(TYPE)); \
102  } \
103  return ((unsigned short)( NAME ## PutPt - NAME ## GetPt )/sizeof(TYPE)); \
104 }
105 // e.g.,
106 // AddPointerFifo(Rx,32,unsigned char, 1,0)
107 // SIZE can be any size
108 // creates RxFifo_Init() RxFifo_Get() and RxFifo_Put()
109 
110 #endif // __FIFO_H__