ringbuffer.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #ifndef RINGBUFFER_H__
  2. #ifndef RINGBUFFER_H__
  3. #define RINGBUFFER_H__
  4. #define RINGBUFFER_H__
  5. #ifdef __cplusplus
  6. #ifdef __cplusplus
  7. extern "C" {
  8. extern "C" {
  9. #endif
  10. #endif
  11. #include <rtthread.h>
  12. #include <rtthread.h>
  13. /* ring buffer */
  14. /* ring buffer */
  15. struct rt_ringbuffer
  16. struct rt_ringbuffer
  17. {
  18. {
  19. rt_uint8_t *buffer_ptr;
  20. rt_uint8_t *buffer_ptr;
  21. /* use the msb of the {read,write}_index as mirror bit. You can see this as
  22. /* use the msb of the {read,write}_index as mirror bit. You can see this as
  23. * if the buffer adds a virtual mirror and the pointers point either to the
  24. * if the buffer adds a virtual mirror and the pointers point either to the
  25. * normal or to the mirrored buffer. If the write_index has the same value
  26. * normal or to the mirrored buffer. If the write_index has the same value
  27. * with the read_index, but in a different mirror, the buffer is full.
  28. * with the read_index, but in a different mirror, the buffer is full.
  29. * While if the write_index and the read_index are the same and within the
  30. * While if the write_index and the read_index are the same and within the
  31. * same mirror, the buffer is empty. The ASCII art of the ringbuffer is:
  32. * same mirror, the buffer is empty. The ASCII art of the ringbuffer is:
  33. *
  34. *
  35. * mirror = 0 mirror = 1
  36. * mirror = 0 mirror = 1
  37. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  38. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  39. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Full
  40. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Full
  41. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  42. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  43. * read_idx-^ write_idx-^
  44. * read_idx-^ write_idx-^
  45. *
  46. *
  47. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  48. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  49. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Empty
  50. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Empty
  51. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  52. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  53. * read_idx-^ ^-write_idx
  54. * read_idx-^ ^-write_idx
  55. *
  56. *
  57. * The tradeoff is we could only use 32KiB of buffer for 16 bit of index.
  58. * The tradeoff is we could only use 32KiB of buffer for 16 bit of index.
  59. * But it should be enough for most of the cases.
  60. * But it should be enough for most of the cases.
  61. *
  62. *
  63. * Ref: http://en.wikipedia.org/wiki/Circular_buffer#Mirroring */
  64. * Ref: http://en.wikipedia.org/wiki/Circular_buffer#Mirroring */
  65. rt_uint16_t read_mirror : 1;
  66. rt_uint16_t read_mirror : 1;
  67. rt_uint16_t read_index : 15;
  68. rt_uint16_t read_index : 15;
  69. rt_uint16_t write_mirror : 1;
  70. rt_uint16_t write_mirror : 1;
  71. rt_uint16_t write_index : 15;
  72. rt_uint16_t write_index : 15;
  73. /* as we use msb of index as mirror bit, the size should be signed and
  74. /* as we use msb of index as mirror bit, the size should be signed and
  75. * could only be positive. */
  76. * could only be positive. */
  77. rt_int16_t buffer_size;
  78. rt_int16_t buffer_size;
  79. };
  80. };
  81. enum rt_ringbuffer_state
  82. enum rt_ringbuffer_state
  83. {
  84. {
  85. RT_RINGBUFFER_EMPTY,
  86. RT_RINGBUFFER_EMPTY,
  87. RT_RINGBUFFER_FULL,
  88. RT_RINGBUFFER_FULL,
  89. /* half full is neither full nor empty */
  90. /* half full is neither full nor empty */
  91. RT_RINGBUFFER_HALFFULL,
  92. RT_RINGBUFFER_HALFFULL,
  93. };
  94. };
  95. /**
  96. /**
  97. * RingBuffer for DeviceDriver
  98. * RingBuffer for DeviceDriver
  99. *
  100. *
  101. * Please note that the ring buffer implementation of RT-Thread
  102. * Please note that the ring buffer implementation of RT-Thread
  103. * has no thread wait or resume feature.
  104. * has no thread wait or resume feature.
  105. */
  106. */
  107. void rt_ringbuffer_init(struct rt_ringbuffer *rb, rt_uint8_t *pool, rt_int16_t size);
  108. void rt_ringbuffer_init(struct rt_ringbuffer *rb, rt_uint8_t *pool, rt_int16_t size);
  109. void rt_ringbuffer_reset(struct rt_ringbuffer *rb);
  110. void rt_ringbuffer_reset(struct rt_ringbuffer *rb);
  111. rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb, const rt_uint8_t *ptr, rt_uint16_t length);
  112. rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb, const rt_uint8_t *ptr, rt_uint16_t length);
  113. rt_size_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb, const rt_uint8_t *ptr, rt_uint16_t length);
  114. rt_size_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb, const rt_uint8_t *ptr, rt_uint16_t length);
  115. rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch);
  116. rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch);
  117. rt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb, const rt_uint8_t ch);
  118. rt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb, const rt_uint8_t ch);
  119. rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb, rt_uint8_t *ptr, rt_uint16_t length);
  120. rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb, rt_uint8_t *ptr, rt_uint16_t length);
  121. rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch);
  122. rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch);
  123. rt_size_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb);
  124. rt_size_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb);
  125. #ifdef RT_USING_HEAP
  126. #ifdef RT_USING_HEAP
  127. struct rt_ringbuffer* rt_ringbuffer_create(rt_uint16_t length);
  128. struct rt_ringbuffer* rt_ringbuffer_create(rt_uint16_t length);
  129. void rt_ringbuffer_destroy(struct rt_ringbuffer *rb);
  130. void rt_ringbuffer_destroy(struct rt_ringbuffer *rb);
  131. #endif
  132. #endif
  133. rt_inline rt_uint16_t rt_ringbuffer_get_size(struct rt_ringbuffer *rb)
  134. rt_inline rt_uint16_t rt_ringbuffer_get_size(struct rt_ringbuffer *rb)
  135. {
  136. {
  137. RT_ASSERT(rb != RT_NULL);
  138. RT_ASSERT(rb != RT_NULL);
  139. return rb->buffer_size;
  140. return rb->buffer_size;
  141. }
  142. }
  143. /** return the size of empty space in rb */
  144. #define rt_ringbuffer_space_len(rb) ((rb)->buffer_size - rt_ringbuffer_data_len(rb))
  145. #ifdef __cplusplus
  146. }
  147. #endif
  148. #endif