ringbuffer.c 8.6 KB

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