ringbuffer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * RT-Thread RuiChing
  3. *
  4. * COPYRIGHT (C) 2024-2025 Shanghai Real-Thread Electronic Technology Co., Ltd.
  5. * All rights reserved.
  6. *
  7. * The license and distribution terms for this file may be
  8. * found in the file LICENSE in this distribution.
  9. */
  10. #ifndef __RINGBUFFER_H__
  11. #define __RINGBUFFER_H__
  12. #include <rtthread.h>
  13. #include <rtconfig.h>
  14. #include <rtdef.h>
  15. #include <kpi.h>
  16. /* ring buffer */
  17. struct rt_ringbuffer
  18. {
  19. rt_uint8_t *buffer_ptr;
  20. /* use the msb of the {read,write}_index as mirror bit. You can see this as
  21. * if the buffer adds a virtual mirror and the pointers point either to the
  22. * normal or to the mirrored buffer. If the write_index has the same value
  23. * with the read_index, but in a different mirror, the buffer is full.
  24. * While if the write_index and the read_index are the same and within the
  25. * same mirror, the buffer is empty. The ASCII art of the ringbuffer is:
  26. *
  27. * mirror = 0 mirror = 1
  28. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  29. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Full
  30. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  31. * read_idx-^ write_idx-^
  32. *
  33. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  34. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Empty
  35. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  36. * read_idx-^ ^-write_idx
  37. */
  38. rt_uint32_t read_mirror : 1;
  39. rt_uint32_t read_index : 31;
  40. rt_uint32_t write_mirror : 1;
  41. rt_uint32_t write_index : 31;
  42. /* as we use msb of index as mirror bit, the size should be signed and
  43. * could only be positive. */
  44. rt_int32_t buffer_size;
  45. };
  46. enum rt_ringbuffer_state
  47. {
  48. RT_RINGBUFFER_EMPTY,
  49. RT_RINGBUFFER_FULL,
  50. /* half full is neither full nor empty */
  51. RT_RINGBUFFER_HALFFULL,
  52. };
  53. /**
  54. * RingBuffer for DeviceDriver
  55. *
  56. * Please note that the ring buffer implementation of RT-Thread
  57. * has no thread wait or resume feature.
  58. */
  59. typedef void (*__kpi_rt_ringbuffer_init)(
  60. struct rt_ringbuffer *rb, rt_uint8_t *pool, rt_int32_t size);
  61. typedef void (*__kpi_rt_ringbuffer_reset)(struct rt_ringbuffer *rb);
  62. typedef rt_size_t (*__kpi_rt_ringbuffer_put)(
  63. struct rt_ringbuffer *rb, const rt_uint8_t *ptr, rt_uint32_t length);
  64. typedef rt_size_t (*__kpi_rt_ringbuffer_put_force)(
  65. struct rt_ringbuffer *rb, const rt_uint8_t *ptr, rt_uint32_t length);
  66. typedef rt_size_t (*__kpi_rt_ringbuffer_putchar)(
  67. struct rt_ringbuffer *rb, const rt_uint8_t ch);
  68. typedef rt_size_t (*__kpi_rt_ringbuffer_putchar_force)(
  69. struct rt_ringbuffer *rb, const rt_uint8_t ch);
  70. typedef rt_size_t (*__kpi_rt_ringbuffer_get)(
  71. struct rt_ringbuffer *rb, rt_uint8_t *ptr, rt_uint32_t length);
  72. typedef rt_size_t (*__kpi_rt_ringbuffer_peek)(
  73. struct rt_ringbuffer *rb, rt_uint8_t **ptr);
  74. typedef rt_size_t (*__kpi_rt_ringbuffer_getchar)(
  75. struct rt_ringbuffer *rb, rt_uint8_t *ch);
  76. typedef rt_size_t (*__kpi_rt_ringbuffer_data_len)(struct rt_ringbuffer *rb);
  77. #ifdef RT_USING_HEAP
  78. typedef struct rt_ringbuffer *(*__kpi_rt_ringbuffer_create)(rt_uint32_t length);
  79. typedef void (*__kpi_rt_ringbuffer_destroy)(struct rt_ringbuffer *rb);
  80. #endif
  81. /**
  82. * @brief Get the buffer size of the ring buffer object.
  83. *
  84. * @param rb A pointer to the ring buffer object.
  85. *
  86. * @return Buffer size.
  87. */
  88. rt_inline rt_uint32_t rt_ringbuffer_get_size(struct rt_ringbuffer *rb)
  89. {
  90. RT_ASSERT(rb != RT_NULL);
  91. return rb->buffer_size;
  92. }
  93. /** return the size of empty space in rb */
  94. #define rt_ringbuffer_space_len(rb) \
  95. ((rb)->buffer_size - rt_ringbuffer_data_len(rb))
  96. KPI_EXTERN(rt_ringbuffer_init);
  97. KPI_EXTERN(rt_ringbuffer_reset);
  98. KPI_EXTERN(rt_ringbuffer_put);
  99. KPI_EXTERN(rt_ringbuffer_put_force);
  100. KPI_EXTERN(rt_ringbuffer_putchar);
  101. KPI_EXTERN(rt_ringbuffer_putchar_force);
  102. KPI_EXTERN(rt_ringbuffer_get);
  103. KPI_EXTERN(rt_ringbuffer_peek);
  104. KPI_EXTERN(rt_ringbuffer_getchar);
  105. KPI_EXTERN(rt_ringbuffer_data_len);
  106. KPI_EXTERN(rt_ringbuffer_create);
  107. KPI_EXTERN(rt_ringbuffer_destroy);
  108. #endif /* __RINGBUFFER_H__ */