ringbuffer.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. * 2016-08-18 heyuanjie add interface
  25. */
  26. #include <rtthread.h>
  27. #include <rtdevice.h>
  28. #include <string.h>
  29. rt_inline enum rt_ringbuffer_state rt_ringbuffer_status(struct rt_ringbuffer *rb)
  30. {
  31. if (rb->read_index == rb->write_index)
  32. {
  33. if (rb->read_mirror == rb->write_mirror)
  34. return RT_RINGBUFFER_EMPTY;
  35. else
  36. return RT_RINGBUFFER_FULL;
  37. }
  38. return RT_RINGBUFFER_HALFFULL;
  39. }
  40. void rt_ringbuffer_init(struct rt_ringbuffer *rb,
  41. rt_uint8_t *pool,
  42. rt_int16_t size)
  43. {
  44. RT_ASSERT(rb != RT_NULL);
  45. RT_ASSERT(size > 0);
  46. /* initialize read and write index */
  47. rb->read_mirror = rb->read_index = 0;
  48. rb->write_mirror = rb->write_index = 0;
  49. /* set buffer pool and size */
  50. rb->buffer_ptr = pool;
  51. rb->buffer_size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
  52. }
  53. RTM_EXPORT(rt_ringbuffer_init);
  54. /**
  55. * put a block of data into ring buffer
  56. */
  57. rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb,
  58. const rt_uint8_t *ptr,
  59. rt_uint16_t length)
  60. {
  61. rt_uint16_t size;
  62. RT_ASSERT(rb != RT_NULL);
  63. /* whether has enough space */
  64. size = rt_ringbuffer_space_len(rb);
  65. /* no space */
  66. if (size == 0)
  67. return 0;
  68. /* drop some data */
  69. if (size < length)
  70. length = size;
  71. if (rb->buffer_size - rb->write_index > length)
  72. {
  73. /* read_index - write_index = empty space */
  74. memcpy(&rb->buffer_ptr[rb->write_index], ptr, length);
  75. /* this should not cause overflow because there is enough space for
  76. * length of data in current mirror */
  77. rb->write_index += length;
  78. return length;
  79. }
  80. memcpy(&rb->buffer_ptr[rb->write_index],
  81. &ptr[0],
  82. rb->buffer_size - rb->write_index);
  83. memcpy(&rb->buffer_ptr[0],
  84. &ptr[rb->buffer_size - rb->write_index],
  85. length - (rb->buffer_size - rb->write_index));
  86. /* we are going into the other side of the mirror */
  87. rb->write_mirror = ~rb->write_mirror;
  88. rb->write_index = length - (rb->buffer_size - rb->write_index);
  89. return length;
  90. }
  91. RTM_EXPORT(rt_ringbuffer_put);
  92. /**
  93. * put a block of data into ring buffer
  94. *
  95. * When the buffer is full, it will discard the old data.
  96. */
  97. rt_size_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb,
  98. const rt_uint8_t *ptr,
  99. rt_uint16_t length)
  100. {
  101. rt_uint16_t space_length;
  102. RT_ASSERT(rb != RT_NULL);
  103. space_length = rt_ringbuffer_space_len(rb);
  104. if (length > space_length)
  105. length = rb->buffer_size;
  106. if (rb->buffer_size - rb->write_index > length)
  107. {
  108. /* read_index - write_index = empty space */
  109. memcpy(&rb->buffer_ptr[rb->write_index], ptr, length);
  110. /* this should not cause overflow because there is enough space for
  111. * length of data in current mirror */
  112. rb->write_index += length;
  113. if (length > space_length)
  114. rb->read_index = rb->write_index;
  115. return length;
  116. }
  117. memcpy(&rb->buffer_ptr[rb->write_index],
  118. &ptr[0],
  119. rb->buffer_size - rb->write_index);
  120. memcpy(&rb->buffer_ptr[0],
  121. &ptr[rb->buffer_size - rb->write_index],
  122. length - (rb->buffer_size - rb->write_index));
  123. /* we are going into the other side of the mirror */
  124. rb->write_mirror = ~rb->write_mirror;
  125. rb->write_index = length - (rb->buffer_size - rb->write_index);
  126. if (length > space_length)
  127. {
  128. rb->read_mirror = ~rb->read_mirror;
  129. rb->read_index = rb->write_index;
  130. }
  131. return length;
  132. }
  133. RTM_EXPORT(rt_ringbuffer_put_force);
  134. /**
  135. * get data from ring buffer
  136. */
  137. rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb,
  138. rt_uint8_t *ptr,
  139. rt_uint16_t length)
  140. {
  141. rt_size_t size;
  142. RT_ASSERT(rb != RT_NULL);
  143. /* whether has enough data */
  144. size = rt_ringbuffer_data_len(rb);
  145. /* no data */
  146. if (size == 0)
  147. return 0;
  148. /* less data */
  149. if (size < length)
  150. length = size;
  151. if (rb->buffer_size - rb->read_index > length)
  152. {
  153. /* copy all of data */
  154. memcpy(ptr, &rb->buffer_ptr[rb->read_index], length);
  155. /* this should not cause overflow because there is enough space for
  156. * length of data in current mirror */
  157. rb->read_index += length;
  158. return length;
  159. }
  160. memcpy(&ptr[0],
  161. &rb->buffer_ptr[rb->read_index],
  162. rb->buffer_size - rb->read_index);
  163. memcpy(&ptr[rb->buffer_size - rb->read_index],
  164. &rb->buffer_ptr[0],
  165. length - (rb->buffer_size - rb->read_index));
  166. /* we are going into the other side of the mirror */
  167. rb->read_mirror = ~rb->read_mirror;
  168. rb->read_index = length - (rb->buffer_size - rb->read_index);
  169. return length;
  170. }
  171. RTM_EXPORT(rt_ringbuffer_get);
  172. /**
  173. * put a character into ring buffer
  174. */
  175. rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch)
  176. {
  177. RT_ASSERT(rb != RT_NULL);
  178. /* whether has enough space */
  179. if (!rt_ringbuffer_space_len(rb))
  180. return 0;
  181. rb->buffer_ptr[rb->write_index] = ch;
  182. /* flip mirror */
  183. if (rb->write_index == rb->buffer_size-1)
  184. {
  185. rb->write_mirror = ~rb->write_mirror;
  186. rb->write_index = 0;
  187. }
  188. else
  189. {
  190. rb->write_index++;
  191. }
  192. return 1;
  193. }
  194. RTM_EXPORT(rt_ringbuffer_putchar);
  195. /**
  196. * put a character into ring buffer
  197. *
  198. * When the buffer is full, it will discard one old data.
  199. */
  200. rt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb, const rt_uint8_t ch)
  201. {
  202. enum rt_ringbuffer_state old_state;
  203. RT_ASSERT(rb != RT_NULL);
  204. old_state = rt_ringbuffer_status(rb);
  205. rb->buffer_ptr[rb->write_index] = ch;
  206. /* flip mirror */
  207. if (rb->write_index == rb->buffer_size-1)
  208. {
  209. rb->write_mirror = ~rb->write_mirror;
  210. rb->write_index = 0;
  211. if (old_state == RT_RINGBUFFER_FULL)
  212. {
  213. rb->read_mirror = ~rb->read_mirror;
  214. rb->read_index = rb->write_index;
  215. }
  216. }
  217. else
  218. {
  219. rb->write_index++;
  220. if (old_state == RT_RINGBUFFER_FULL)
  221. rb->read_index = rb->write_index;
  222. }
  223. return 1;
  224. }
  225. RTM_EXPORT(rt_ringbuffer_putchar_force);
  226. /**
  227. * get a character from a ringbuffer
  228. */
  229. rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch)
  230. {
  231. RT_ASSERT(rb != RT_NULL);
  232. /* ringbuffer is empty */
  233. if (!rt_ringbuffer_data_len(rb))
  234. return 0;
  235. /* put character */
  236. *ch = rb->buffer_ptr[rb->read_index];
  237. if (rb->read_index == rb->buffer_size-1)
  238. {
  239. rb->read_mirror = ~rb->read_mirror;
  240. rb->read_index = 0;
  241. }
  242. else
  243. {
  244. rb->read_index++;
  245. }
  246. return 1;
  247. }
  248. RTM_EXPORT(rt_ringbuffer_getchar);
  249. /**
  250. * get the size of data in rb
  251. */
  252. rt_size_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb)
  253. {
  254. switch (rt_ringbuffer_status(rb))
  255. {
  256. case RT_RINGBUFFER_EMPTY:
  257. return 0;
  258. case RT_RINGBUFFER_FULL:
  259. return rb->buffer_size;
  260. case RT_RINGBUFFER_HALFFULL:
  261. default:
  262. if (rb->write_index > rb->read_index)
  263. return rb->write_index - rb->read_index;
  264. else
  265. return rb->buffer_size - (rb->read_index - rb->write_index);
  266. };
  267. }
  268. RTM_EXPORT(rt_ringbuffer_data_len);
  269. /**
  270. * empty the rb
  271. */
  272. void rt_ringbuffer_reset(struct rt_ringbuffer *rb)
  273. {
  274. RT_ASSERT(rb != RT_NULL);
  275. rb->read_mirror = 0;
  276. rb->read_index = 0;
  277. rb->write_mirror = 0;
  278. rb->write_index = 0;
  279. }
  280. RTM_EXPORT(rt_ringbuffer_reset);
  281. #ifdef RT_USING_HEAP
  282. struct rt_ringbuffer* rt_ringbuffer_create(rt_uint16_t size)
  283. {
  284. struct rt_ringbuffer *rb;
  285. rt_uint8_t *pool;
  286. RT_ASSERT(size > 0);
  287. size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
  288. rb = rt_malloc(sizeof(struct rt_ringbuffer));
  289. if (rb == RT_NULL)
  290. goto exit;
  291. pool = rt_malloc(size);
  292. if (pool == RT_NULL)
  293. {
  294. rt_free(rb);
  295. goto exit;
  296. }
  297. rt_ringbuffer_init(rb, pool, size);
  298. exit:
  299. return rb;
  300. }
  301. RTM_EXPORT(rt_ringbuffer_create);
  302. void rt_ringbuffer_destroy(struct rt_ringbuffer *rb)
  303. {
  304. RT_ASSERT(rb != RT_NULL);
  305. rt_free(rb->buffer_ptr);
  306. rt_free(rb);
  307. }
  308. RTM_EXPORT(rt_ringbuffer_destroy);
  309. #endif