-
Notifications
You must be signed in to change notification settings - Fork 0
/
FIFO.h
116 lines (107 loc) · 4.46 KB
/
FIFO.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// 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 Arm Cortex M Microcontrollers",
ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2015
Programs 3.7, 3.8., 3.9 and 3.10 in Section 3.7
Copyright 2015 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
// 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 ((unsigned short)( 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 ((unsigned short)( NAME ## PutPt - NAME ## GetPt + (SIZE*sizeof(TYPE)))/sizeof(TYPE)); \
} \
return ((unsigned short)( 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__