WXK
2024-12-16 78e84fcf264afd731cd66c807d9fcb690fe12126
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/**
 *******************************************************************************
 * @FileName  : fifo.h
 * @Author    : GaoQiu
 * @CreateDate: 2020-02-18
 * @Copyright : Copyright(C) GaoQiu
 *              All Rights Reserved.
 *******************************************************************************
 *
 * The information contained herein is confidential and proprietary property of
 * GaoQiu and is available under the terms of Commercial License Agreement
 * between GaoQiu and the licensee in separate contract or the terms described
 * here-in.
 *
 * This heading MUST NOT be removed from this file.
 *
 * Licensees are granted free, non-transferable use of the information in this
 * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
 *
 *******************************************************************************
 */
 
#ifndef FIFO_H_
#define FIFO_H_
 
#include "defs_types.h"
 
#define FIFO_NEW_EN               0
 
/*! FIFO error code */
#define FIFO_SUCCESS              0x00
#define FIFO_ERR_INVALID_PARAM    0x01
#define FIFO_ERR_INVALID_LENGTH   0x02
#define FIFO_ERR_INVALID_NUM      0x03
 
typedef uint8_t buf_num_t;
 
/*! FIFO type */
typedef struct{
    uint8_t       *pBuf;
    uint32_t       size;
    buf_num_t      r;
    buf_num_t      w;
 
#if FIFO_NEW_EN
    buf_num_t      num;
#else
    buf_num_t      numMask;
#endif
}fifo_t;
 
/**
 * !!!Note: num must is 2 power.
 */
#if FIFO_NEW_EN
    #define FIFO_DEFINE(fifoName, size, num) \
    static uint8_t (fifoName##Buf)[(size) * (num)];\
    fifo_t fifoName = { \
       (fifoName##Buf),\
       (size),\
       0,\
       0,\
       (num),\
    }
#else
    #define FIFO_DEFINE(fifoName, size, num) \
    static uint8_t (fifoName##Buf)[(size) * (num)];\
    fifo_t fifoName = { \
       (fifoName##Buf),\
       (size),\
       0,\
       0,\
       (num)-1,\
    }
#endif
 
/**
 * @brief: FIFO initialization.
 * @param: pFifo     pointer to FIFO
 * @param: pBuf      pointer to memory
 * @param: byteSize  the size of memory
 * @param: num       number of buffer;
 * @return:
 */
uint8_t FIFO_Init(fifo_t *pFifo, uint8_t *pBuf, uint32_t byteSize, uint32_t num);
 
/**
 * @brief:  Get the number of used buffer
 * @param:  pFifo     pointer point to FIFO
 * @return:
 */
static inline buf_num_t FIFO_Length(fifo_t *pFifo)
{
#if FIFO_NEW_EN
    return (buf_num_t)(pFifo->w - pFifo->r);
#else
    return (buf_num_t)(pFifo->w - pFifo->r);
#endif
}
 
/**
 * @brief:  Get FIFO total number
 * @param:  pFifo     pointer point to FIFO
 * @return:
 */
static inline buf_num_t FIFO_GetBufTotalNum(fifo_t *pFifo)
{
#if FIFO_NEW_EN
    return pFifo->num;
#else
    return pFifo->numMask + 1;
#endif
}
 
/**
 * @brief:
 * @param:
 * @return:
 */
static inline bool_t FIFO_IsFull(fifo_t *pFifo)
{
#if FIFO_NEW_EN
    return FIFO_Length(pFifo) >= pFifo->num ? true : false;
#else
    return FIFO_Length(pFifo) >= (pFifo->numMask + 1);
#endif
}
 
/**
 * @brief:
 * @param:
 * @return:
 */
static inline uint8_t *FIFO_GetWriteBuf(fifo_t *pFifo)
{
    if(pFifo == NULL){
        return NULL;
    }
#if FIFO_NEW_EN
    return pFifo->pBuf + pFifo->size * (pFifo->w % pFifo->num);
#else
    return pFifo->pBuf + pFifo->size * (pFifo->w & pFifo->numMask);
#endif
}
 
/**
 * @brief:
 * @param:
 * @return:
 */
static inline uint8_t *FIFO_GetNextWriteBuf(fifo_t *pFifo)
{
    if(pFifo == NULL){
        return NULL;
    }
 
    pFifo->w++;
 
#if FIFO_NEW_EN
    return pFifo->pBuf + pFifo->size * (pFifo->w % pFifo->num);
#else
    return pFifo->pBuf + pFifo->size * (pFifo->w & pFifo->numMask);
#endif
}
 
/**
 * @brief:
 * @param:
 * @return:
 */
static inline uint8_t *FIFO_GetPreWriteBuf(fifo_t *pFifo)
{
    if(pFifo == NULL){
        return NULL;
    }
 
    pFifo->w--;
 
#if FIFO_NEW_EN
    return pFifo->pBuf + pFifo->size * (pFifo->w % pFifo->num);
#else
    return pFifo->pBuf + pFifo->size * (pFifo->w & pFifo->numMask);
#endif
}
 
/**
 * @brief:
 * @param:
 * @return:
 */
static inline void FIFO_MoveToNextWriteBuf(fifo_t *pFifo)
{
    if(pFifo == NULL)
        return;
 
    pFifo->w++;
}
 
/**
 * @brief:
 * @param:
 * @return:
 */
static inline uint8_t *FIFO_GetReadBuf(fifo_t *pFifo)
{
    if(pFifo == NULL){
        return NULL;
    }
 
#if FIFO_NEW_EN
    return pFifo->pBuf + pFifo->size * (pFifo->r % pFifo->num);
#else
    return pFifo->pBuf + pFifo->size * (pFifo->r & pFifo->numMask);
#endif
}
 
/**
 * @brief:
 * @param:
 * @return:
 */
static inline void FIFO_MoveToNextReadBuf(fifo_t *pFifo)
{
    pFifo->r++;
}
 
/**
 * @brief:
 * @param:
 * @return:
 */
static inline uint32_t FIFO_Flush(fifo_t *pFifo)
{
    if(pFifo == NULL){
        return FIFO_ERR_INVALID_PARAM;
    }
 
    pFifo->r = pFifo->w;//=0
    return FIFO_SUCCESS;
}
 
 
#endif /* FIFO_H_ */