ringbuffer.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. * peak data from ring buffer
  163. */
  164. rt_size_t rt_ringbuffer_peak(struct rt_ringbuffer *rb, rt_uint8_t **ptr)
  165. {
  166. RT_ASSERT(rb != RT_NULL);
  167. *ptr = RT_NULL;
  168. /* whether has enough data */
  169. rt_size_t size = rt_ringbuffer_data_len(rb);
  170. /* no data */
  171. if (size == 0)
  172. return 0;
  173. *ptr = &rb->buffer_ptr[rb->read_index];
  174. if(rb->buffer_size - rb->read_index > size)
  175. {
  176. rb->read_index += size;
  177. return size;
  178. }
  179. size = rb->buffer_size - rb->read_index;
  180. /* we are going into the other side of the mirror */
  181. rb->read_mirror = ~rb->read_mirror;
  182. rb->read_index = 0;
  183. return size;
  184. }
  185. RTM_EXPORT(rt_ringbuffer_peak);
  186. /**
  187. * put a character into ring buffer
  188. */
  189. rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch)
  190. {
  191. RT_ASSERT(rb != RT_NULL);
  192. /* whether has enough space */
  193. if (!rt_ringbuffer_space_len(rb))
  194. return 0;
  195. rb->buffer_ptr[rb->write_index] = ch;
  196. /* flip mirror */
  197. if (rb->write_index == rb->buffer_size-1)
  198. {
  199. rb->write_mirror = ~rb->write_mirror;
  200. rb->write_index = 0;
  201. }
  202. else
  203. {
  204. rb->write_index++;
  205. }
  206. return 1;
  207. }
  208. RTM_EXPORT(rt_ringbuffer_putchar);
  209. /**
  210. * put a character into ring buffer
  211. *
  212. * When the buffer is full, it will discard one old data.
  213. */
  214. rt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb, const rt_uint8_t ch)
  215. {
  216. enum rt_ringbuffer_state old_state;
  217. RT_ASSERT(rb != RT_NULL);
  218. old_state = rt_ringbuffer_status(rb);
  219. rb->buffer_ptr[rb->write_index] = ch;
  220. /* flip mirror */
  221. if (rb->write_index == rb->buffer_size-1)
  222. {
  223. rb->write_mirror = ~rb->write_mirror;
  224. rb->write_index = 0;
  225. if (old_state == RT_RINGBUFFER_FULL)
  226. {
  227. rb->read_mirror = ~rb->read_mirror;
  228. rb->read_index = rb->write_index;
  229. }
  230. }
  231. else
  232. {
  233. rb->write_index++;
  234. if (old_state == RT_RINGBUFFER_FULL)
  235. rb->read_index = rb->write_index;
  236. }
  237. return 1;
  238. }
  239. RTM_EXPORT(rt_ringbuffer_putchar_force);
  240. /**
  241. * get a character from a ringbuffer
  242. */
  243. rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch)
  244. {
  245. RT_ASSERT(rb != RT_NULL);
  246. /* ringbuffer is empty */
  247. if (!rt_ringbuffer_data_len(rb))
  248. return 0;
  249. /* put character */
  250. *ch = rb->buffer_ptr[rb->read_index];
  251. if (rb->read_index == rb->buffer_size-1)
  252. {
  253. rb->read_mirror = ~rb->read_mirror;
  254. rb->read_index = 0;
  255. }
  256. else
  257. {
  258. rb->read_index++;
  259. }
  260. return 1;
  261. }
  262. RTM_EXPORT(rt_ringbuffer_getchar);
  263. /**
  264. * get the size of data in rb
  265. */
  266. rt_size_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb)
  267. {
  268. switch (rt_ringbuffer_status(rb))
  269. {
  270. case RT_RINGBUFFER_EMPTY:
  271. return 0;
  272. case RT_RINGBUFFER_FULL:
  273. return rb->buffer_size;
  274. case RT_RINGBUFFER_HALFFULL:
  275. default:
  276. if (rb->write_index > rb->read_index)
  277. return rb->write_index - rb->read_index;
  278. else
  279. return rb->buffer_size - (rb->read_index - rb->write_index);
  280. };
  281. }
  282. RTM_EXPORT(rt_ringbuffer_data_len);
  283. /**
  284. * empty the rb
  285. */
  286. void rt_ringbuffer_reset(struct rt_ringbuffer *rb)
  287. {
  288. RT_ASSERT(rb != RT_NULL);
  289. rb->read_mirror = 0;
  290. rb->read_index = 0;
  291. rb->write_mirror = 0;
  292. rb->write_index = 0;
  293. }
  294. RTM_EXPORT(rt_ringbuffer_reset);
  295. #ifdef RT_USING_HEAP
  296. struct rt_ringbuffer* rt_ringbuffer_create(rt_uint16_t size)
  297. {
  298. struct rt_ringbuffer *rb;
  299. rt_uint8_t *pool;
  300. RT_ASSERT(size > 0);
  301. size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
  302. rb = (struct rt_ringbuffer *)rt_malloc(sizeof(struct rt_ringbuffer));
  303. if (rb == RT_NULL)
  304. goto exit;
  305. pool = (rt_uint8_t *)rt_malloc(size);
  306. if (pool == RT_NULL)
  307. {
  308. rt_free(rb);
  309. rb = RT_NULL;
  310. goto exit;
  311. }
  312. rt_ringbuffer_init(rb, pool, size);
  313. exit:
  314. return rb;
  315. }
  316. RTM_EXPORT(rt_ringbuffer_create);
  317. void rt_ringbuffer_destroy(struct rt_ringbuffer *rb)
  318. {
  319. RT_ASSERT(rb != RT_NULL);
  320. rt_free(rb->buffer_ptr);
  321. rt_free(rb);
  322. }
  323. RTM_EXPORT(rt_ringbuffer_destroy);
  324. #endif