blob: 07043a6c7021ad27860014fbd42d0df392eed0a9 [file] [log] [blame]
Harald Weltea40c8e52019-09-27 19:22:34 +02001/**
2 * \file
3 *
4 * \brief Ringbuffer declaration.
5 *
6 * Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.
7 *
8 * \asf_license_start
9 *
10 * \page License
11 *
12 * Subject to your compliance with these terms, you may use Microchip
13 * software and any derivatives exclusively with Microchip products.
14 * It is your responsibility to comply with third party license terms applicable
15 * to your use of third party software (including open source software) that
16 * may accompany Microchip software.
17 *
18 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
19 * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
20 * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
21 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
22 * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
23 * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
24 * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
25 * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
26 * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
27 * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
28 * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
29 *
30 * \asf_license_stop
31 *
32 */
33#ifndef _UTILS_RINGBUFFER_H_INCLUDED
34#define _UTILS_RINGBUFFER_H_INCLUDED
35
36#include <stdint.h>
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42/**
43 * \addtogroup doc_driver_hal_utils_ringbuffer
44 *
45 * @{
46 */
47
48/**
49 * \brief Ring buffer element type
50 */
51struct ringbuffer {
52 uint8_t *buf; /** Buffer base address */
53 uint32_t size; /** Buffer size */
54 uint32_t read_index; /** Buffer read index */
55 uint32_t write_index; /** Buffer write index */
56};
57
58/**
59 * \brief Ring buffer init
60 *
61 * \param[in] rb The pointer to a ring buffer structure instance
62 * \param[in] buf Space to store the data
63 * \param[in] size The buffer length, must be aligned with power of 2
64 *
65 * \return ERR_NONE on success, or an error code on failure.
66 */
67int32_t ringbuffer_init(struct ringbuffer *const rb, void *buf, uint32_t size);
68
69/**
70 * \brief Get one byte from ring buffer, the user needs to handle the concurrent
71 * access on buffer via put/get/flush
72 *
73 * \param[in] rb The pointer to a ring buffer structure instance
74 * \param[in] data One byte space to store the read data
75 *
76 * \return ERR_NONE on success, or an error code on failure.
77 */
78int32_t ringbuffer_get(struct ringbuffer *const rb, uint8_t *data);
79
80/**
81 * \brief Put one byte to ring buffer, the user needs to handle the concurrent access
82 * on buffer via put/get/flush
83 *
84 * \param[in] rb The pointer to a ring buffer structure instance
85 * \param[in] data One byte data to be put into ring buffer
86 *
87 * \return ERR_NONE on success, or an error code on failure.
88 */
89int32_t ringbuffer_put(struct ringbuffer *const rb, uint8_t data);
90
91/**
92 * \brief Return the element number of ring buffer
93 *
94 * \param[in] rb The pointer to a ring buffer structure instance
95 *
96 * \return The number of elements in ring buffer [0, rb->size]
97 */
98uint32_t ringbuffer_num(const struct ringbuffer *const rb);
99
100/**
101 * \brief Flush ring buffer, the user needs to handle the concurrent access on buffer
102 * via put/get/flush
103 *
104 * \param[in] rb The pointer to a ring buffer structure instance
105 *
106 * \return ERR_NONE on success, or an error code on failure.
107 */
108uint32_t ringbuffer_flush(struct ringbuffer *const rb);
109
110/**@}*/
111
112#ifdef __cplusplus
113}
114#endif
115#endif /* _UTILS_RINGBUFFER_H_INCLUDED */