1 /**
2 * \file
3 *
4 * \brief This file controls the software FIFO management.
5 *
6 * These functions manages FIFOs thanks to simple a API. The FIFO can
7 * be 100% full thanks to a double-index range implementation. For example,
8 * a FIFO of 4 elements can be implemented: the FIFO can really hold up to 4
9 * elements.
10 * This is particularly well suited for any kind of application needing a lot of
11 * small FIFO.
12 *
13 * Copyright (c) 2010-2015 Atmel Corporation. All rights reserved.
14 *
15 * \asf_license_start
16 *
17 * \page License
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions are met:
21 *
22 * 1. Redistributions of source code must retain the above copyright notice,
23 * this list of conditions and the following disclaimer.
24 *
25 * 2. Redistributions in binary form must reproduce the above copyright notice,
26 * this list of conditions and the following disclaimer in the documentation
27 * and/or other materials provided with the distribution.
28 *
29 * 3. The name of Atmel may not be used to endorse or promote products derived
30 * from this software without specific prior written permission.
31 *
32 * 4. This software may only be redistributed and used in connection with an
33 * Atmel microcontroller product.
34 *
35 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
38 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
39 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
44 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45 * POSSIBILITY OF SUCH DAMAGE.
46 *
47 * \asf_license_stop
48 *
49 */
50 /*
51 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
52 */
53
54 #include "fifo.h"
55
fifo_init(fifo_desc_t * fifo_desc,void * buffer,uint8_t size)56 int fifo_init(fifo_desc_t *fifo_desc, void *buffer, uint8_t size)
57 {
58 // Check the size parameter. It must be not null...
59 Assert (size);
60
61 // ... must be a 2-power ...
62 Assert (!(size & (size - 1)));
63
64 // ... and must fit in a uint8_t. Since the read and write indexes are using a
65 // double-index range implementation, the max FIFO size is thus 128 items.
66 Assert (size <= 128);
67
68 // Fifo starts empty.
69 fifo_desc->read_index = 0;
70 fifo_desc->write_index = 0;
71
72 // Save the size parameter.
73 fifo_desc->size = size;
74
75 // Create a mask to speed up the FIFO management (index swapping).
76 fifo_desc->mask = (2 * (uint16_t)size) - 1;
77
78 // Save the buffer pointer.
79 fifo_desc->buffer.u8ptr = buffer;
80
81 return FIFO_OK;
82 }
83