-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfifo.c
214 lines (170 loc) · 4.43 KB
/
fifo.c
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*!
\@file Fifo_c.case
\@brief pure C implementation of fifo
\@author Jason Berger
\@date 05/03/2017
*/
#include "fifo.h"
#include <stdlib.h>
#include <string.h>
#define FIFO_LOCK //pFifo->mLock =1//while(pFifo->lock){delay_ms(1);} pFifo->lock = 1
#define FIFO_UNLOCK //pFifo->mLock = 0
#define fifo_min(X,Y) (((X) < (Y)) ? (X) : (Y))
#define fifo_max(X,Y) (((X) > (Y)) ? (X) : (Y))
void fifo_init(fifo_t* pFifo, int depth, int width)
{
pFifo->mBuffer = (uint8_t*) malloc(depth * width);
pFifo->mHead = 0;
pFifo->mTail = 0;
pFifo->mMaxLen = depth;
pFifo->mCount = 0;
pFifo->mObjSize = width;
pFifo->mLock = 0;
}
void fifo_deinit(fifo_t* pFifo)
{
free(pFifo->mBuffer);
}
int fifo_push( fifo_t* pFifo, void* data)
{
FIFO_LOCK;
if (pFifo->mCount >= pFifo->mMaxLen) /* check if fifo is full */
{
FIFO_UNLOCK;
return FIFO_OVERFLOW;
}
int next = pFifo->mHead + 1; /*next is where head will point to after this write*/
if (next >= pFifo->mMaxLen) /* wrap head at end of buffer*/
{
next = 0;
}
pFifo->mCount++; /* increment count*/
int addr = pFifo->mHead * pFifo->mObjSize; /* get address of current object*/
memcpy(&pFifo->mBuffer[addr], data, pFifo->mObjSize); /* Copy object into next */
pFifo->mHead = next; /* set head to next */
FIFO_UNLOCK;
return FIFO_OK; // return success to indicate successful push.
}
int fifo_pop( fifo_t* pFifo, void* data)
{
FIFO_LOCK;
// if the head isn't ahead of the tail, we don't have any characters
if (pFifo->mCount <= 0) // check if circular buffer is empty
{
FIFO_UNLOCK;
return FIFO_UNDERFLOW; // and return with an error
}
// next is where tail will point to after this read.
int next = pFifo->mTail + 1;
if (next >= pFifo->mMaxLen)
{
next = 0;
}
int addr = pFifo->mTail * pFifo->mObjSize;
memcpy(data,&pFifo->mBuffer[addr],pFifo->mObjSize);
pFifo->mTail = next; // tail to next data offset.
if(pFifo->mCount > 0)
{
pFifo->mCount--;
}
FIFO_UNLOCK;
return FIFO_OK; // return success to indicate successful push.
}
int fifo_push_buf( fifo_t* pFifo, void* data, int len)
{
int result = 0;
uint8_t* cast = (uint8_t*) data;
for(int i=0; i < len; i++)
{
result |= fifo_push(pFifo,&cast[i * pFifo->mObjSize]);
}
return result;
}
int fifo_pop_buf( fifo_t* pFifo, void* data, int len)
{
int result = 0;
uint8_t* cast = (uint8_t*) data;
for(int i=0; i < len; i++)
{
result |= fifo_pop(pFifo, &cast[i * pFifo->mObjSize]);
}
return result;
}
int fifo_clear( fifo_t* pFifo, int len)
{
//create trash bin based on objsize
uint8_t* trash = (uint8_t*)malloc(pFifo->mObjSize);
if(len > pFifo->mCount)
len = pFifo->mCount;
//there are more effecient ways to do this
//but it should be a rare case and this utilizes locks in place
while(len > 0)
{
fifo_pop(pFifo,trash);
len--;
}
//free up trash memory
free(trash);
return len;
}
int fifo_peek( fifo_t* pFifo, void* data, int idx)
{
FIFO_LOCK;
if(pFifo->mCount <= idx)
{
FIFO_UNLOCK;
return -1;
}
int addr = pFifo->mTail + idx; //
//handle buffer wrapping
if(addr > pFifo->mMaxLen)
{
addr -= pFifo->mMaxLen;
}
memcpy(data,&pFifo->mBuffer[addr* pFifo->mObjSize],pFifo->mObjSize);
FIFO_UNLOCK;
return 0; // return success to indicate successful push.
}
int fifo_peek_buf(fifo_t* pFifo, void* data, int len)
{
FIFO_LOCK;
int addr = pFifo->mTail;
uint8_t* cast = (uint8_t*) data;
len = fifo_min(pFifo->mCount, len);
len*=pFifo->mObjSize;
for(int i=0; i < len; i++)
{
cast[i] = pFifo->mBuffer[addr++];
//wrap;
if(addr == pFifo->mMaxLen)
{
addr =0;
}
}
//we return as an int so we can send -1 to indicate there isnt enough data
FIFO_UNLOCK;
return len;
}
int fifo_checksum(fifo_t* pFifo, int offset, int len)
{
FIFO_LOCK;
uint16_t sum = 0;
int addr = pFifo->mTail + offset;
if(pFifo->mCount < (len + offset))
{
FIFO_UNLOCK;
return -1;
}
for(int i=0; i < len; i++)
{
sum+= pFifo->mBuffer[addr++];
//wrap
if(addr == pFifo->mMaxLen)
{
addr =0;
}
}
//we return as an int so we can send -1 to indicate there isnt enough data
FIFO_UNLOCK;
return (int) sum;
}