fifo.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #include "fifo.h"
  54. int fifo_init(fifo_desc_t *fifo_desc, void *buffer, uint8_t size)
  55. {
  56. // Check the size parameter. It must be not null...
  57. Assert (size);
  58. // ... must be a 2-power ...
  59. Assert (!(size & (size - 1)));
  60. // ... and must fit in a uint8_t. Since the read and write indexes are using a
  61. // double-index range implementation, the max FIFO size is thus 128 items.
  62. Assert (size <= 128);
  63. // Fifo starts empty.
  64. fifo_desc->read_index = 0;
  65. fifo_desc->write_index = 0;
  66. // Save the size parameter.
  67. fifo_desc->size = size;
  68. // Create a mask to speed up the FIFO management (index swapping).
  69. fifo_desc->mask = (2 * (uint16_t)size) - 1;
  70. // Save the buffer pointer.
  71. fifo_desc->buffer.u8ptr = buffer;
  72. return FIFO_OK;
  73. }