ringbuffer.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. /**
  43. * put a block of data into ring buffer
  44. */
  45. rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb,
  46. const rt_uint8_t *ptr,
  47. rt_uint16_t length)
  48. {
  49. rt_uint16_t size;
  50. RT_ASSERT(rb != RT_NULL);
  51. /* whether has enough space */
  52. size = rt_ringbuffer_space_len(rb);
  53. /* no space */
  54. if (size == 0)
  55. return 0;
  56. /* drop some data */
  57. if (size < length)
  58. length = size;
  59. if (rb->buffer_size - rb->write_index > length)
  60. {
  61. /* read_index - write_index = empty space */
  62. memcpy(&rb->buffer_ptr[rb->write_index], ptr, length);
  63. /* this should not cause overflow because there is enough space for
  64. * length of data in current mirror */
  65. rb->write_index += length;
  66. return length;
  67. }
  68. memcpy(&rb->buffer_ptr[rb->write_index],
  69. &ptr[0],
  70. rb->buffer_size - rb->write_index);
  71. memcpy(&rb->buffer_ptr[0],
  72. &ptr[rb->buffer_size - rb->write_index],
  73. length - (rb->buffer_size - rb->write_index));
  74. /* we are going into the other side of the mirror */
  75. rb->write_mirror = ~rb->write_mirror;
  76. rb->write_index = length - (rb->buffer_size - rb->write_index);
  77. return length;
  78. }
  79. RTM_EXPORT(rt_ringbuffer_put);
  80. /**
  81. * put a block of data into ring buffer
  82. *
  83. * When the buffer is full, it will discard the old data.
  84. */
  85. rt_size_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb,
  86. const rt_uint8_t *ptr,
  87. rt_uint16_t length)
  88. {
  89. enum rt_ringbuffer_state old_state;
  90. RT_ASSERT(rb != RT_NULL);
  91. old_state = rt_ringbuffer_status(rb);
  92. if (length > rb->buffer_size)
  93. length = rb->buffer_size;
  94. if (rb->buffer_size - rb->write_index > length)
  95. {
  96. /* read_index - write_index = empty space */
  97. memcpy(&rb->buffer_ptr[rb->write_index], ptr, length);
  98. /* this should not cause overflow because there is enough space for
  99. * length of data in current mirror */
  100. rb->write_index += length;
  101. if (old_state == RT_RINGBUFFER_FULL)
  102. rb->read_index = rb->write_index;
  103. return length;
  104. }
  105. memcpy(&rb->buffer_ptr[rb->write_index],
  106. &ptr[0],
  107. rb->buffer_size - rb->write_index);
  108. memcpy(&rb->buffer_ptr[0],
  109. &ptr[rb->buffer_size - rb->write_index],
  110. length - (rb->buffer_size - rb->write_index));
  111. /* we are going into the other side of the mirror */
  112. rb->write_mirror = ~rb->write_mirror;
  113. rb->write_index = length - (rb->buffer_size - rb->write_index);
  114. if (old_state == RT_RINGBUFFER_FULL)
  115. {
  116. rb->read_mirror = ~rb->read_mirror;
  117. rb->read_index = rb->write_index;
  118. }
  119. return length;
  120. }
  121. RTM_EXPORT(rt_ringbuffer_put_force);
  122. /**
  123. * get data from ring buffer
  124. */
  125. rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb,
  126. rt_uint8_t *ptr,
  127. rt_uint16_t length)
  128. {
  129. rt_size_t size;
  130. RT_ASSERT(rb != RT_NULL);
  131. /* whether has enough data */
  132. size = rt_ringbuffer_data_len(rb);
  133. /* no data */
  134. if (size == 0)
  135. return 0;
  136. /* less data */
  137. if (size < length)
  138. length = size;
  139. if (rb->buffer_size - rb->read_index > length)
  140. {
  141. /* copy all of data */
  142. memcpy(ptr, &rb->buffer_ptr[rb->read_index], length);
  143. /* this should not cause overflow because there is enough space for
  144. * length of data in current mirror */
  145. rb->read_index += length;
  146. return length;
  147. }
  148. memcpy(&ptr[0],
  149. &rb->buffer_ptr[rb->read_index],
  150. rb->buffer_size - rb->read_index);
  151. memcpy(&ptr[rb->buffer_size - rb->read_index],
  152. &rb->buffer_ptr[0],
  153. length - (rb->buffer_size - rb->read_index));
  154. /* we are going into the other side of the mirror */
  155. rb->read_mirror = ~rb->read_mirror;
  156. rb->read_index = length - (rb->buffer_size - rb->read_index);
  157. return length;
  158. }
  159. RTM_EXPORT(rt_ringbuffer_get);
  160. /**
  161. * put a character into ring buffer
  162. */
  163. rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch)
  164. {
  165. RT_ASSERT(rb != RT_NULL);
  166. /* whether has enough space */
  167. if (!rt_ringbuffer_space_len(rb))
  168. return 0;
  169. rb->buffer_ptr[rb->write_index] = ch;
  170. /* flip mirror */
  171. if (rb->write_index == rb->buffer_size-1)
  172. {
  173. rb->write_mirror = ~rb->write_mirror;
  174. rb->write_index = 0;
  175. }
  176. else
  177. {
  178. rb->write_index++;
  179. }
  180. return 1;
  181. }
  182. RTM_EXPORT(rt_ringbuffer_putchar);
  183. /**
  184. * put a character into ring buffer
  185. *
  186. * When the buffer is full, it will discard one old data.
  187. */
  188. rt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb, const rt_uint8_t ch)
  189. {
  190. enum rt_ringbuffer_state old_state;
  191. RT_ASSERT(rb != RT_NULL);
  192. old_state = rt_ringbuffer_status(rb);
  193. rb->buffer_ptr[rb->write_index] = ch;
  194. /* flip mirror */
  195. if (rb->write_index == rb->buffer_size-1)
  196. {
  197. rb->write_mirror = ~rb->write_mirror;
  198. rb->write_index = 0;
  199. if (old_state == RT_RINGBUFFER_FULL)
  200. {
  201. rb->read_mirror = ~rb->read_mirror;
  202. rb->read_index = rb->write_index;
  203. }
  204. }
  205. else
  206. {
  207. rb->write_index++;
  208. if (old_state == RT_RINGBUFFER_FULL)
  209. rb->read_index = rb->write_index;
  210. }
  211. return 1;
  212. }
  213. RTM_EXPORT(rt_ringbuffer_putchar_force);
  214. /**
  215. * get a character from a ringbuffer
  216. */
  217. rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch)
  218. {
  219. RT_ASSERT(rb != RT_NULL);
  220. /* ringbuffer is empty */
  221. if (!rt_ringbuffer_data_len(rb))
  222. return 0;
  223. /* put character */
  224. *ch = rb->buffer_ptr[rb->read_index];
  225. if (rb->read_index == rb->buffer_size-1)
  226. {
  227. rb->read_mirror = ~rb->read_mirror;
  228. rb->read_index = 0;
  229. }
  230. else
  231. {
  232. rb->read_index++;
  233. }
  234. return 1;
  235. }
  236. RTM_EXPORT(rt_ringbuffer_getchar);