ringbuffer.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * File : ringbuffer.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2012-09-30 Bernard first version.
  23. * 2013-05-08 Grissiom reimplement
  24. */
  25. #include <rtthread.h>
  26. #include <rtdevice.h>
  27. #include <string.h>
  28. void rt_ringbuffer_init(struct rt_ringbuffer *rb,
  29. rt_uint8_t *pool,
  30. rt_int16_t size)
  31. {
  32. RT_ASSERT(rb != RT_NULL);
  33. RT_ASSERT(size > 0)
  34. /* initialize read and write index */
  35. rb->read_mirror = rb->read_index = 0;
  36. rb->write_mirror = rb->write_index = 0;
  37. /* set buffer pool and size */
  38. rb->buffer_ptr = pool;
  39. rb->buffer_size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
  40. }
  41. RTM_EXPORT(rt_ringbuffer_init);
  42. rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb,
  43. const rt_uint8_t *ptr,
  44. rt_uint16_t length)
  45. {
  46. rt_uint16_t size;
  47. RT_ASSERT(rb != RT_NULL);
  48. /* whether has enough space */
  49. size = rt_ringbuffer_space_len(rb);
  50. /* no space */
  51. if (size == 0)
  52. return 0;
  53. /* drop some data */
  54. if (size < length)
  55. length = size;
  56. if (rb->buffer_size - rb->write_index > length)
  57. {
  58. /* read_index - write_index = empty space */
  59. memcpy(&rb->buffer_ptr[rb->write_index], ptr, length);
  60. /* this should not cause overflow because there is enough space for
  61. * length of data in current mirror */
  62. rb->write_index += length;
  63. return length;
  64. }
  65. memcpy(&rb->buffer_ptr[rb->write_index],
  66. &ptr[0],
  67. rb->buffer_size - rb->write_index);
  68. memcpy(&rb->buffer_ptr[0],
  69. &ptr[rb->buffer_size - rb->write_index],
  70. length - (rb->buffer_size - rb->write_index));
  71. /* we are going into the other side of the mirror */
  72. rb->write_mirror = ~rb->write_mirror;
  73. rb->write_index = length - (rb->buffer_size - rb->write_index);
  74. return length;
  75. }
  76. RTM_EXPORT(rt_ringbuffer_put);
  77. /**
  78. * get data from ring buffer
  79. */
  80. rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb,
  81. rt_uint8_t *ptr,
  82. rt_uint16_t length)
  83. {
  84. rt_size_t size;
  85. RT_ASSERT(rb != RT_NULL);
  86. /* whether has enough data */
  87. size = rt_ringbuffer_data_len(rb);
  88. /* no data */
  89. if (size == 0)
  90. return 0;
  91. /* less data */
  92. if (size < length)
  93. length = size;
  94. if (rb->buffer_size - rb->read_index > length)
  95. {
  96. /* copy all of data */
  97. memcpy(ptr, &rb->buffer_ptr[rb->read_index], length);
  98. /* this should not cause overflow because there is enough space for
  99. * length of data in current mirror */
  100. rb->read_index += length;
  101. return length;
  102. }
  103. memcpy(&ptr[0],
  104. &rb->buffer_ptr[rb->read_index],
  105. rb->buffer_size - rb->read_index);
  106. memcpy(&ptr[rb->buffer_size - rb->read_index],
  107. &rb->buffer_ptr[0],
  108. length - (rb->buffer_size - rb->read_index));
  109. /* we are going into the other side of the mirror */
  110. rb->read_mirror = ~rb->read_mirror;
  111. rb->read_index = length - (rb->buffer_size - rb->read_index);
  112. return length;
  113. }
  114. RTM_EXPORT(rt_ringbuffer_get);
  115. /**
  116. * put a character into ring buffer
  117. */
  118. rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch)
  119. {
  120. RT_ASSERT(rb != RT_NULL);
  121. /* whether has enough space */
  122. if (!rt_ringbuffer_space_len(rb))
  123. return 0;
  124. rb->buffer_ptr[rb->write_index] = ch;
  125. /* flip mirror */
  126. if (rb->write_index == rb->buffer_size-1)
  127. {
  128. rb->write_mirror = ~rb->write_mirror;
  129. rb->write_index = 0;
  130. }
  131. else
  132. {
  133. rb->write_index++;
  134. }
  135. return 1;
  136. }
  137. RTM_EXPORT(rt_ringbuffer_putchar);
  138. /**
  139. * get a character from a ringbuffer
  140. */
  141. rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch)
  142. {
  143. RT_ASSERT(rb != RT_NULL);
  144. /* ringbuffer is empty */
  145. if (!rt_ringbuffer_data_len(rb))
  146. return 0;
  147. /* put character */
  148. *ch = rb->buffer_ptr[rb->read_index];
  149. if (rb->read_index == rb->buffer_size-1)
  150. {
  151. rb->read_mirror = ~rb->read_mirror;
  152. rb->read_index = 0;
  153. }
  154. else
  155. {
  156. rb->read_index++;
  157. }
  158. return 1;
  159. }
  160. RTM_EXPORT(rt_ringbuffer_getchar);