2018-10-15 00:50:04 +08:00
|
|
|
/*
|
2023-04-18 10:26:23 +08:00
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
2018-10-15 00:50:04 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
2021-08-17 22:44:29 +08:00
|
|
|
* 2021-08-14 Jackistang add comments for function interface.
|
2018-10-15 00:50:04 +08:00
|
|
|
*/
|
2017-10-15 22:56:46 +08:00
|
|
|
#ifndef RINGBUFFER_H__
|
|
|
|
#define RINGBUFFER_H__
|
|
|
|
|
2023-07-29 06:26:51 +08:00
|
|
|
#include <rtdef.h>
|
|
|
|
#include <rtconfig.h>
|
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2018-10-14 19:37:18 +08:00
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
/* ring buffer */
|
|
|
|
struct rt_ringbuffer
|
2018-10-14 19:37:18 +08:00
|
|
|
{
|
2017-10-15 22:56:46 +08:00
|
|
|
rt_uint8_t *buffer_ptr;
|
|
|
|
/* use the msb of the {read,write}_index as mirror bit. You can see this as
|
|
|
|
* if the buffer adds a virtual mirror and the pointers point either to the
|
|
|
|
* normal or to the mirrored buffer. If the write_index has the same value
|
2018-10-14 19:37:18 +08:00
|
|
|
* with the read_index, but in a different mirror, the buffer is full.
|
|
|
|
* While if the write_index and the read_index are the same and within the
|
2017-10-15 22:56:46 +08:00
|
|
|
* same mirror, the buffer is empty. The ASCII art of the ringbuffer is:
|
|
|
|
*
|
|
|
|
* mirror = 0 mirror = 1
|
2018-10-14 19:37:18 +08:00
|
|
|
* +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
|
|
|
|
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Full
|
2017-10-15 22:56:46 +08:00
|
|
|
* +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
|
|
|
|
* read_idx-^ write_idx-^
|
|
|
|
*
|
2018-10-14 19:37:18 +08:00
|
|
|
* +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
|
|
|
|
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Empty
|
2017-10-15 22:56:46 +08:00
|
|
|
* +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
|
|
|
|
* read_idx-^ ^-write_idx
|
2023-02-09 11:55:52 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
rt_uint32_t read_mirror : 1;
|
|
|
|
rt_uint32_t read_index : 31;
|
|
|
|
rt_uint32_t write_mirror : 1;
|
|
|
|
rt_uint32_t write_index : 31;
|
2017-10-15 22:56:46 +08:00
|
|
|
/* as we use msb of index as mirror bit, the size should be signed and
|
|
|
|
* could only be positive. */
|
2023-02-09 11:55:52 +08:00
|
|
|
rt_int32_t buffer_size;
|
2018-10-14 19:37:18 +08:00
|
|
|
};
|
2017-10-15 22:56:46 +08:00
|
|
|
|
2018-10-14 19:37:18 +08:00
|
|
|
enum rt_ringbuffer_state
|
|
|
|
{
|
2017-10-15 22:56:46 +08:00
|
|
|
RT_RINGBUFFER_EMPTY,
|
|
|
|
RT_RINGBUFFER_FULL,
|
2018-10-14 19:37:18 +08:00
|
|
|
/* half full is neither full nor empty */
|
|
|
|
RT_RINGBUFFER_HALFFULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RingBuffer for DeviceDriver
|
|
|
|
*
|
|
|
|
* Please note that the ring buffer implementation of RT-Thread
|
|
|
|
* has no thread wait or resume feature.
|
2017-10-15 22:56:46 +08:00
|
|
|
*/
|
2023-02-09 11:55:52 +08:00
|
|
|
void rt_ringbuffer_init(struct rt_ringbuffer *rb, rt_uint8_t *pool, rt_int32_t size);
|
2018-10-14 19:37:18 +08:00
|
|
|
void rt_ringbuffer_reset(struct rt_ringbuffer *rb);
|
2023-02-09 11:55:52 +08:00
|
|
|
rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb, const rt_uint8_t *ptr, rt_uint32_t length);
|
|
|
|
rt_size_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb, const rt_uint8_t *ptr, rt_uint32_t length);
|
2017-10-15 22:56:46 +08:00
|
|
|
rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch);
|
2018-10-14 19:37:18 +08:00
|
|
|
rt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb, const rt_uint8_t ch);
|
2023-02-09 11:55:52 +08:00
|
|
|
rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb, rt_uint8_t *ptr, rt_uint32_t length);
|
2022-06-17 18:46:46 +08:00
|
|
|
rt_size_t rt_ringbuffer_peek(struct rt_ringbuffer *rb, rt_uint8_t **ptr);
|
2018-10-14 19:37:18 +08:00
|
|
|
rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch);
|
|
|
|
rt_size_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb);
|
2017-10-15 22:56:46 +08:00
|
|
|
|
2018-10-14 19:37:18 +08:00
|
|
|
#ifdef RT_USING_HEAP
|
2023-02-09 11:55:52 +08:00
|
|
|
struct rt_ringbuffer* rt_ringbuffer_create(rt_uint32_t length);
|
2018-10-14 19:37:18 +08:00
|
|
|
void rt_ringbuffer_destroy(struct rt_ringbuffer *rb);
|
|
|
|
#endif
|
2017-10-15 22:56:46 +08:00
|
|
|
|
2021-08-14 13:47:05 +08:00
|
|
|
/**
|
2021-08-18 09:44:52 +08:00
|
|
|
* @brief Get the buffer size of the ring buffer object.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-17 22:44:29 +08:00
|
|
|
* @param rb A pointer to the ring buffer object.
|
2021-08-14 14:32:58 +08:00
|
|
|
*
|
2021-08-14 13:47:05 +08:00
|
|
|
* @return Buffer size.
|
|
|
|
*/
|
2023-02-09 11:55:52 +08:00
|
|
|
rt_inline rt_uint32_t rt_ringbuffer_get_size(struct rt_ringbuffer *rb)
|
2018-10-14 19:37:18 +08:00
|
|
|
{
|
|
|
|
RT_ASSERT(rb != RT_NULL);
|
|
|
|
return rb->buffer_size;
|
2017-10-15 22:56:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** return the size of empty space in rb */
|
2023-02-09 11:55:52 +08:00
|
|
|
#define rt_ringbuffer_space_len(rb) ((rb)->buffer_size - rt_ringbuffer_data_len(rb))
|
2017-10-15 22:56:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|