ringbuffer.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 > rb->buffer_size)
  105. {
  106. ptr = &ptr[length - rb->buffer_size];
  107. length = rb->buffer_size;
  108. }
  109. if (rb->buffer_size - rb->write_index > length)
  110. {
  111. /* read_index - write_index = empty space */
  112. memcpy(&rb->buffer_ptr[rb->write_index], ptr, length);
  113. /* this should not cause overflow because there is enough space for
  114. * length of data in current mirror */
  115. rb->write_index += length;
  116. if (length > space_length)
  117. rb->read_index = rb->write_index;
  118. return length;
  119. }
  120. memcpy(&rb->buffer_ptr[rb->write_index],
  121. &ptr[0],
  122. rb->buffer_size - rb->write_index);
  123. memcpy(&rb->buffer_ptr[0],
  124. &ptr[rb->buffer_size - rb->write_index],
  125. length - (rb->buffer_size - rb->write_index));
  126. /* we are going into the other side of the mirror */
  127. rb->write_mirror = ~rb->write_mirror;
  128. rb->write_index = length - (rb->buffer_size - rb->write_index);
  129. if (length > space_length)
  130. {
  131. rb->read_mirror = ~rb->read_mirror;
  132. rb->read_index = rb->write_index;
  133. }
  134. return length;
  135. }
  136. RTM_EXPORT(rt_ringbuffer_put_force);
  137. /**
  138. * get data from ring buffer
  139. */
  140. rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb,
  141. rt_uint8_t *ptr,
  142. rt_uint16_t length)
  143. {
  144. rt_size_t size;
  145. RT_ASSERT(rb != RT_NULL);
  146. /* whether has enough data */
  147. size = rt_ringbuffer_data_len(rb);
  148. /* no data */
  149. if (size == 0)
  150. return 0;
  151. /* less data */
  152. if (size < length)
  153. length = size;
  154. if (rb->buffer_size - rb->read_index > length)
  155. {
  156. /* copy all of data */
  157. memcpy(ptr, &rb->buffer_ptr[rb->read_index], length);
  158. /* this should not cause overflow because there is enough space for
  159. * length of data in current mirror */
  160. rb->read_index += length;
  161. return length;
  162. }
  163. memcpy(&ptr[0],
  164. &rb->buffer_ptr[rb->read_index],
  165. rb->buffer_size - rb->read_index);
  166. memcpy(&ptr[rb->buffer_size - rb->read_index],
  167. &rb->buffer_ptr[0],
  168. length - (rb->buffer_size - rb->read_index));
  169. /* we are going into the other side of the mirror */
  170. rb->read_mirror = ~rb->read_mirror;
  171. rb->read_index = length - (rb->buffer_size - rb->read_index);
  172. return length;
  173. }
  174. RTM_EXPORT(rt_ringbuffer_get);
  175. /**
  176. * put a character into ring buffer
  177. */
  178. rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch)
  179. {
  180. RT_ASSERT(rb != RT_NULL);
  181. /* whether has enough space */
  182. if (!rt_ringbuffer_space_len(rb))
  183. return 0;
  184. rb->buffer_ptr[rb->write_index] = ch;
  185. /* flip mirror */
  186. if (rb->write_index == rb->buffer_size-1)
  187. {
  188. rb->write_mirror = ~rb->write_mirror;
  189. rb->write_index = 0;
  190. }
  191. else
  192. {
  193. rb->write_index++;
  194. }
  195. return 1;
  196. }
  197. RTM_EXPORT(rt_ringbuffer_putchar);
  198. /**
  199. * put a character into ring buffer
  200. *
  201. * When the buffer is full, it will discard one old data.
  202. */
  203. rt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb, const rt_uint8_t ch)
  204. {
  205. enum rt_ringbuffer_state old_state;
  206. RT_ASSERT(rb != RT_NULL);
  207. old_state = rt_ringbuffer_status(rb);
  208. rb->buffer_ptr[rb->write_index] = ch;
  209. /* flip mirror */
  210. if (rb->write_index == rb->buffer_size-1)
  211. {
  212. rb->write_mirror = ~rb->write_mirror;
  213. rb->write_index = 0;
  214. if (old_state == RT_RINGBUFFER_FULL)
  215. {
  216. rb->read_mirror = ~rb->read_mirror;
  217. rb->read_index = rb->write_index;
  218. }
  219. }
  220. else
  221. {
  222. rb->write_index++;
  223. if (old_state == RT_RINGBUFFER_FULL)
  224. rb->read_index = rb->write_index;
  225. }
  226. return 1;
  227. }
  228. RTM_EXPORT(rt_ringbuffer_putchar_force);
  229. /**
  230. * get a character from a ringbuffer
  231. */
  232. rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch)
  233. {
  234. RT_ASSERT(rb != RT_NULL);
  235. /* ringbuffer is empty */
  236. if (!rt_ringbuffer_data_len(rb))
  237. return 0;
  238. /* put character */
  239. *ch = rb->buffer_ptr[rb->read_index];
  240. if (rb->read_index == rb->buffer_size-1)
  241. {
  242. rb->read_mirror = ~rb->read_mirror;
  243. rb->read_index = 0;
  244. }
  245. else
  246. {
  247. rb->read_index++;
  248. }
  249. return 1;
  250. }
  251. RTM_EXPORT(rt_ringbuffer_getchar);
  252. /**
  253. * get the size of data in rb
  254. */
  255. rt_size_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb)
  256. {
  257. switch (rt_ringbuffer_status(rb))
  258. {
  259. case RT_RINGBUFFER_EMPTY:
  260. return 0;
  261. case RT_RINGBUFFER_FULL:
  262. return rb->buffer_size;
  263. case RT_RINGBUFFER_HALFFULL:
  264. default:
  265. if (rb->write_index > rb->read_index)
  266. return rb->write_index - rb->read_index;
  267. else
  268. return rb->buffer_size - (rb->read_index - rb->write_index);
  269. };
  270. }
  271. RTM_EXPORT(rt_ringbuffer_data_len);
  272. /**
  273. * empty the rb
  274. */
  275. void rt_ringbuffer_reset(struct rt_ringbuffer *rb)
  276. {
  277. RT_ASSERT(rb != RT_NULL);
  278. rb->read_mirror = 0;
  279. rb->read_index = 0;
  280. rb->write_mirror = 0;
  281. rb->write_index = 0;
  282. }
  283. RTM_EXPORT(rt_ringbuffer_reset);
  284. #ifdef RT_USING_HEAP
  285. struct rt_ringbuffer* rt_ringbuffer_create(rt_uint16_t size)
  286. {
  287. struct rt_ringbuffer *rb;
  288. rt_uint8_t *pool;
  289. RT_ASSERT(size > 0);
  290. size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
  291. rb = rt_malloc(sizeof(struct rt_ringbuffer));
  292. if (rb == RT_NULL)
  293. goto exit;
  294. pool = rt_malloc(size);
  295. if (pool == RT_NULL)
  296. {
  297. rt_free(rb);
  298. goto exit;
  299. }
  300. rt_ringbuffer_init(rb, pool, size);
  301. exit:
  302. return rb;
  303. }
  304. RTM_EXPORT(rt_ringbuffer_create);
  305. void rt_ringbuffer_destroy(struct rt_ringbuffer *rb)
  306. {
  307. RT_ASSERT(rb != RT_NULL);
  308. rt_free(rb->buffer_ptr);
  309. rt_free(rb);
  310. }
  311. RTM_EXPORT(rt_ringbuffer_destroy);
  312. #endif