ringbuffer.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #ifndef RINGBUFFER_H__
  2. #define RINGBUFFER_H__
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include <rtthread.h>
  7. /* ring buffer */
  8. struct rt_ringbuffer
  9. {
  10. rt_uint8_t *buffer_ptr;
  11. /* use the msb of the {read,write}_index as mirror bit. You can see this as
  12. * if the buffer adds a virtual mirror and the pointers point either to the
  13. * normal or to the mirrored buffer. If the write_index has the same value
  14. * with the read_index, but in a different mirror, the buffer is full.
  15. * While if the write_index and the read_index are the same and within the
  16. * same mirror, the buffer is empty. The ASCII art of the ringbuffer is:
  17. *
  18. * mirror = 0 mirror = 1
  19. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  20. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Full
  21. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  22. * read_idx-^ write_idx-^
  23. *
  24. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  25. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Empty
  26. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  27. * read_idx-^ ^-write_idx
  28. *
  29. * The tradeoff is we could only use 32KiB of buffer for 16 bit of index.
  30. * But it should be enough for most of the cases.
  31. *
  32. * Ref: http://en.wikipedia.org/wiki/Circular_buffer#Mirroring */
  33. rt_uint16_t read_mirror : 1;
  34. rt_uint16_t read_index : 15;
  35. rt_uint16_t write_mirror : 1;
  36. rt_uint16_t write_index : 15;
  37. /* as we use msb of index as mirror bit, the size should be signed and
  38. * could only be positive. */
  39. rt_int16_t buffer_size;
  40. };
  41. enum rt_ringbuffer_state
  42. {
  43. RT_RINGBUFFER_EMPTY,
  44. RT_RINGBUFFER_FULL,
  45. /* half full is neither full nor empty */
  46. RT_RINGBUFFER_HALFFULL,
  47. };
  48. /**
  49. * RingBuffer for DeviceDriver
  50. *
  51. * Please note that the ring buffer implementation of RT-Thread
  52. * has no thread wait or resume feature.
  53. */
  54. void rt_ringbuffer_init(struct rt_ringbuffer *rb, rt_uint8_t *pool, rt_int16_t size);
  55. void rt_ringbuffer_reset(struct rt_ringbuffer *rb);
  56. rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb, const rt_uint8_t *ptr, rt_uint16_t length);
  57. rt_size_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb, const rt_uint8_t *ptr, rt_uint16_t length);
  58. rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch);
  59. rt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb, const rt_uint8_t ch);
  60. rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb, rt_uint8_t *ptr, rt_uint16_t length);
  61. rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch);
  62. rt_size_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb);
  63. #ifdef RT_USING_HEAP
  64. struct rt_ringbuffer* rt_ringbuffer_create(rt_uint16_t length);
  65. void rt_ringbuffer_destroy(struct rt_ringbuffer *rb);
  66. #endif
  67. rt_inline rt_uint16_t rt_ringbuffer_get_size(struct rt_ringbuffer *rb)
  68. {
  69. RT_ASSERT(rb != RT_NULL);
  70. return rb->buffer_size;
  71. }
  72. /** return the size of empty space in rb */
  73. #define rt_ringbuffer_space_len(rb) ((rb)->buffer_size - rt_ringbuffer_data_len(rb))
  74. #ifdef __cplusplus
  75. }
  76. #endif
  77. #endif