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
#include <assert.h>
#include "app_fifo.h"
#include "defs_types.h"
#include "PanSeries.h"
 
static __INLINE uint32_t fifo_length(app_fifo_t * p_fifo)
{
    uint32_t tmp = p_fifo->read_pos;
    return p_fifo->write_pos - tmp;
}
 
 
#define FIFO_LENGTH() fifo_length(p_fifo)  /**< Macro for calculating the FIFO length. */
 
 
/**@brief Put one byte to the FIFO. */
static __INLINE void fifo_put(app_fifo_t * p_fifo, uint8_t byte)
{
    p_fifo->p_buf[p_fifo->write_pos & p_fifo->buf_size_mask] = byte;
    p_fifo->write_pos++;
}
 
 
/**@brief Look at one byte in the FIFO. */
static __INLINE void fifo_peek(app_fifo_t * p_fifo, uint16_t index, uint8_t * p_byte)
{
    *p_byte = p_fifo->p_buf[(p_fifo->read_pos + index) & p_fifo->buf_size_mask];
}
 
 
/**@brief Get one byte from the FIFO. */
static __INLINE void fifo_get(app_fifo_t * p_fifo, uint8_t * p_byte)
{
    fifo_peek(p_fifo, 0, p_byte);
    p_fifo->read_pos++;
}
 
 
uint32_t app_fifo_init(app_fifo_t * p_fifo, uint8_t * p_buf, uint16_t buf_size)
{
    // Check buffer for null pointer.
    if (p_buf == NULL)
    {
        return APP_FIFO_NULL;
    }
 
    // Check that the buffer size is a power of two.
    if (!IS_POWER_OF_TWO(buf_size))
    {
        return APP_FIFO_INVALID_LEN;
    }
 
    p_fifo->p_buf         = p_buf;
    p_fifo->buf_size_mask = buf_size - 1;
    p_fifo->read_pos      = 0;
    p_fifo->write_pos     = 0;
 
    return APP_FIFO_OK;
}
 
uint32_t app_fifo_put(app_fifo_t * p_fifo, uint8_t byte)
{
    if (FIFO_LENGTH() <= p_fifo->buf_size_mask)
    {
        fifo_put(p_fifo, byte);
        return APP_FIFO_OK;
    }
 
    return APP_FIFO_NO_MEM;
}
 
uint32_t app_fifo_get(app_fifo_t * p_fifo, uint8_t * p_byte)
{
    if (FIFO_LENGTH() != 0)
    {
        fifo_get(p_fifo, p_byte);
        return APP_FIFO_OK;
    }
 
    return APP_FIFO_NOT_FOUND;
}
 
uint32_t app_fifo_peek(app_fifo_t * p_fifo, uint16_t index, uint8_t * p_byte)
{
    if (FIFO_LENGTH() > index)
    {
        fifo_peek(p_fifo, index, p_byte);
        return APP_FIFO_OK;
    }
 
    return APP_FIFO_NOT_FOUND;
}
 
uint32_t app_fifo_flush(app_fifo_t * p_fifo)
{
    p_fifo->read_pos = p_fifo->write_pos;
    return APP_FIFO_OK;
}
 
uint32_t app_fifo_read(app_fifo_t * p_fifo, uint8_t * p_byte_array, uint32_t * p_size)
{
    assert(p_fifo != NULL);
    assert(p_size != NULL);
 
    if(p_fifo == NULL || p_size == NULL){
        return APP_FIFO_INVALID_PARAM;
    }
 
    const uint32_t byte_count    = fifo_length(p_fifo);
    const uint32_t requested_len = (*p_size);
    uint32_t       index         = 0;
    uint32_t       read_size     = MIN(requested_len, byte_count);
 
    (*p_size) = byte_count;
 
    // Check if the FIFO is empty.
    if (byte_count == 0)
    {
        return APP_FIFO_NOT_FOUND;
    }
 
    // Check if application has requested only the size.
    if (p_byte_array == NULL)
    {
        return APP_FIFO_OK;
    }
 
    // Fetch bytes from the FIFO.
    while (index < read_size)
    {
        fifo_get(p_fifo, &p_byte_array[index++]);
    }
 
    (*p_size) = read_size;
 
    return APP_FIFO_OK;
}
 
uint32_t app_fifo_write(app_fifo_t * p_fifo, uint8_t const * p_byte_array, uint32_t * p_size)
{
    assert(p_fifo != NULL);
    assert(p_size != NULL);
 
    if(p_fifo == NULL || p_size == NULL){
        return APP_FIFO_INVALID_PARAM;
    }
 
    const uint32_t available_count = p_fifo->buf_size_mask - fifo_length(p_fifo) + 1;
    const uint32_t requested_len   = (*p_size);
    uint32_t       index           = 0;
    uint32_t       write_size      = MIN(requested_len, available_count);
 
    (*p_size) = available_count;
 
    // Check if the FIFO is FULL.
    if (available_count == 0)
    {
        return APP_FIFO_NO_MEM;
    }
 
    // Check if application has requested only the size.
    if (p_byte_array == NULL)
    {
        return APP_FIFO_OK;
    }
 
    //Fetch bytes from the FIFO.
    while (index < write_size)
    {
        fifo_put(p_fifo, p_byte_array[index++]);
    }
 
    (*p_size) = write_size;
 
    return APP_FIFO_OK;
}