fifo.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * fifo.c
  3. *
  4. * Created on: Nov 27, 2012
  5. * Author: hathach
  6. */
  7. /*
  8. * Software License Agreement (BSD License)
  9. * Copyright (c) 2013, hathach (tinyusb.org)
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. The name of the author may not be used to endorse or promote products
  21. * derived from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  26. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  28. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  31. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  32. * OF SUCH DAMAGE.
  33. *
  34. * This file is part of the tinyUSB stack.
  35. */
  36. #include "fifo.h"
  37. /**************************************************************************/
  38. /*!
  39. @brief Disables the IRQ specified in the FIFO's 'irq' field
  40. to prevent reads/write issues with interrupts
  41. @param[in] f
  42. Pointer to the FIFO that should be protected
  43. */
  44. /**************************************************************************/
  45. static inline void mutex_lock (fifo_t* f)
  46. {
  47. // if (f->irq > 0)
  48. // NVIC_DisableIRQ(f->irq);
  49. }
  50. /**************************************************************************/
  51. /*!
  52. @brief Re-enables the IRQ specified in the FIFO's 'irq' field
  53. @param[in] f
  54. Pointer to the FIFO that should be protected
  55. */
  56. /**************************************************************************/
  57. static inline void mutex_unlock (fifo_t* f)
  58. {
  59. // if (f->irq > 0)
  60. // NVIC_EnableIRQ(f->irq);
  61. }
  62. /**************************************************************************/
  63. /*!
  64. @brief Initialises the FIFO buffer
  65. @param[in] f
  66. Pointer to the fifo_t object to intiialize
  67. @param[in] buffer
  68. Pointer to the buffer's location in memory
  69. @param[in] size
  70. The buffer size in bytes
  71. @param[in] overwritable
  72. Set to TRUE is the FIFO is overwritable when the FIFO
  73. is full (the first element will be overwritten)
  74. @param[in] irq
  75. The IRQ number to disable for MUTEX protection.
  76. Set the -1 if not required.
  77. */
  78. /**************************************************************************/
  79. bool fifo_init(fifo_t* f, uint8_t* buffer, uint16_t size, bool overwritable) //, IRQn_Type irq)
  80. {
  81. ASSERT(size > 0, false);
  82. f->buf = buffer;
  83. f->size = size;
  84. f->rd_ptr = f->wr_ptr = f->len = 0;
  85. f->overwritable = overwritable;
  86. // f->irq = irq;
  87. return true;
  88. }
  89. /**************************************************************************/
  90. /*!
  91. @brief Read one byte out of the RX buffer.
  92. This function will return the byte located at the array index of the
  93. read pointer, and then increment the read pointer index. If the read
  94. pointer exceeds the maximum buffer size, it will roll over to zero.
  95. @param[in] f
  96. Pointer to the FIFO buffer to manipulate
  97. @param[in] data
  98. Pointer to the place holder for data read from the buffer
  99. @returns TRUE if the queue is not empty
  100. */
  101. /**************************************************************************/
  102. bool fifo_read(fifo_t* f, uint8_t *data)
  103. {
  104. if (fifo_is_empty(f))
  105. return false;
  106. mutex_lock(f);
  107. *data = f->buf[f->rd_ptr];
  108. f->rd_ptr = (f->rd_ptr + 1) % f->size;
  109. f->len--;
  110. mutex_unlock(f);
  111. return true;
  112. }
  113. /**************************************************************************/
  114. /*!
  115. @brief Read a byte array from FIFO
  116. @param[in] f
  117. Pointer to the FIFO buffer to manipulate
  118. @param[in] rx
  119. Pointer to the place holder for data read from the buffer
  120. @param[in] maxlen
  121. The maximum number of bytes to read from the FIFO
  122. @returns The actual number of bytes read from the FIFO
  123. */
  124. /**************************************************************************/
  125. uint16_t fifo_read_n(fifo_t* f, uint8_t* rx, uint16_t maxlen)
  126. {
  127. uint16_t len = 0;
  128. while ( len < maxlen && fifo_read(f, rx) )
  129. {
  130. len++;
  131. rx++;
  132. }
  133. return len;
  134. }
  135. /**************************************************************************/
  136. /*!
  137. @brief Write one byte into the RX buffer.
  138. This function will write one byte into the array index specified by
  139. the write pointer and increment the write index. If the write index
  140. exceeds the max buffer size, then it will roll over to zero.
  141. @param[in] f
  142. Pointer to the FIFO buffer to manipulate
  143. @param[in] data
  144. The byte to add to the FIFO
  145. @returns TRUE if the data was written to the FIFO (overwrittable
  146. FIFO will always return TRUE)
  147. */
  148. /**************************************************************************/
  149. bool fifo_write(fifo_t* f, uint8_t data)
  150. {
  151. if ( fifo_is_full(f) && f->overwritable == false)
  152. return false;
  153. mutex_lock(f);
  154. f->buf[f->wr_ptr] = data;
  155. f->wr_ptr = (f->wr_ptr + 1) % f->size;
  156. if (fifo_is_full(f))
  157. {
  158. f->rd_ptr = f->wr_ptr; // keep the full state (rd == wr && len = size)
  159. }else
  160. {
  161. f->len++;
  162. }
  163. mutex_unlock(f);
  164. return true;
  165. }
  166. /**************************************************************************/
  167. /*!
  168. @brief Clear the fifo read and write pointers and set length to zero
  169. @param[in] f
  170. Pointer to the FIFO buffer to manipulate
  171. */
  172. /**************************************************************************/
  173. void fifo_clear(fifo_t *f)
  174. {
  175. mutex_lock(f);
  176. f->rd_ptr = 0;
  177. f->wr_ptr = 0;
  178. f->len = 0;
  179. mutex_unlock(f);
  180. }