2 * Copyright (c) 2012, Alexander I. Mykyta
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 /*==================================================================================================
29 * Alex M. 04/07/2011 born
31 *=================================================================================================*/
34 * \addtogroup MOD_FIFO
40 * \brief Code for \ref MOD_FIFO "FIFO Datapipe"
41 * \author Alex Mykyta (amykyta3@gmail.com)
52 //--------------------------------------------------------------------------------------------------
53 ///\addtogroup FIFO_FUNCTIONS
57 * \brief Initializes a new FIFO Datapipe
58 * \param [in] bufptr Pointer to the buffer to be used as storage for the FIFO
59 * \param [in] bufsize Size of the buffer in bytes
60 * \param [in] fifo Pointer to an empty #FIFO_t object
61 * \param [out] fifo Initialized FIFO object
64 void fifo_init(FIFO_t
*fifo
, register void *bufptr
, register size_t bufsize
){
65 fifo
->bufptr
= bufptr
;
66 fifo
->bufsize
= bufsize
;
69 #if(FIFO_LOG_MAX_USAGE == 1)
74 //--------------------------------------------------------------------------------------------------
76 * \brief Write data into the FIFO buffer
77 * \param [in] fifo Pointer to the #FIFO_t object
78 * \param [in] src Pointer to the data to be stored
79 * \param [in] size Number of bytes to be written to the FIFO
81 * \retval RES_FULL Not enough space in FIFO for requested write operation
83 RES_t
fifo_write(FIFO_t
*fifo
, register void *src
, register size_t size
){
84 register size_t wrcount
;
86 if(size
> fifo_wrcount(fifo
)){
92 if((wrcount
= fifo
->bufsize
- fifo
->wridx
) <= size
){
93 // write operation will wrap around in fifo
94 // write first half of fifo
95 memcpy(fifo
->bufptr
+fifo
->wridx
,src
,wrcount
);
97 //wrap around and continue
100 src
= (uint8_t*)src
+ wrcount
;
104 memcpy(fifo
->bufptr
+fifo
->wridx
,src
,size
);
108 #if(FIFO_LOG_MAX_USAGE == 1)
109 wrcount
= fifo_rdcount(fifo
);
110 if(wrcount
> fifo
->max
){
119 //--------------------------------------------------------------------------------------------------
121 * \brief Read data from the FIFO buffer
122 * \param [in] fifo Pointer to the #FIFO_t object
123 * \param [out] dst Destination of the data to be read. A \c NULL pointer discards the data.
124 * \param [in] size Number of bytes to be read from the FIFO
126 * \retval RES_PARAMERR Not enough bytes written in FIFO for requested read operation
128 RES_t
fifo_read(FIFO_t
*fifo
, register void *dst
, register size_t size
){
129 register size_t rdcount
;
131 if(size
> fifo_rdcount(fifo
)){
132 return(RES_PARAMERR
);
135 if((rdcount
= fifo
->bufsize
- fifo
->rdidx
) <= size
){
136 // read operation will wrap around in fifo
137 // read first half of fifo
139 memcpy(dst
,fifo
->bufptr
+fifo
->rdidx
,rdcount
);
141 //wrap around and continue
144 dst
= (uint8_t*)dst
+ rdcount
;
149 memcpy(dst
,fifo
->bufptr
+fifo
->rdidx
,size
);
156 //--------------------------------------------------------------------------------------------------
158 * \brief Read data from the FIFO buffer without advancing the read pointer
159 * \param [in] fifo Pointer to the #FIFO_t object
160 * \param [out] dst Destination of the data to be read.
161 * \param [in] size Number of bytes to be read from the FIFO
163 * \retval RES_PARAMERR Not enough bytes written in FIFO for requested read operation
165 RES_t
fifo_peek(FIFO_t
*fifo
, register void *dst
, register size_t size
){
166 register size_t rdcount
;
167 register size_t rdidx
;
169 if(size
> fifo_rdcount(fifo
)){
170 return(RES_PARAMERR
);
175 if((rdcount
= fifo
->bufsize
- rdidx
) <= size
){
176 // read operation will wrap around in fifo
177 // read first half of fifo
178 memcpy(dst
,fifo
->bufptr
+rdidx
,rdcount
);
179 //wrap around and continue
182 dst
= (uint8_t*)dst
+ rdcount
;
186 memcpy(dst
,fifo
->bufptr
+rdidx
,size
);
193 //--------------------------------------------------------------------------------------------------
195 * \brief Empties the FIFO
196 * \param [in] fifo Pointer to the #FIFO_t object
199 void fifo_clear(FIFO_t
*fifo
){
204 //--------------------------------------------------------------------------------------------------
206 * \brief Get the number of bytes available for read in the FIFO
207 * \param [in] fifo Pointer to the #FIFO_t object
208 * \return Number of bytes
210 size_t fifo_rdcount(FIFO_t
*fifo
){
211 register size_t wridx
,rdidx
;
218 return((fifo
->bufsize
-rdidx
)+wridx
);
222 //--------------------------------------------------------------------------------------------------
224 * \brief Get the number of bytes that can be written to the FIFO
225 * \param [in] fifo Pointer to the #FIFO_t object
226 * \return Number of bytes
228 size_t fifo_wrcount(FIFO_t
*fifo
){
229 register size_t wridx
,rdidx
;
233 if(fifo
->rdidx
>= fifo
->wridx
+1){
234 return(rdidx
-wridx
-1);
236 return((fifo
->bufsize
-wridx
)+rdidx
-1);