blob: 401d557246143f13f9c16d3591933cace652d2ed [file] [log] [blame]
Kévin Redonccbed0b2019-01-24 18:30:26 +01001/**
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#ifdef __cplusplus
37extern "C" {
38#endif
39
40/**
41 * \addtogroup doc_driver_hal_utils_ringbuffer
42 *
43 * @{
44 */
45
46#include "compiler.h"
47#include "utils_assert.h"
48
49/**
50 * \brief Ring buffer element type
51 */
52struct ringbuffer {
53 uint8_t *buf; /** Buffer base address */
54 uint32_t size; /** Buffer size */
55 uint32_t read_index; /** Buffer read index */
56 uint32_t write_index; /** Buffer write index */
57};
58
59/**
60 * \brief Ring buffer init
61 *
62 * \param[in] rb The pointer to a ring buffer structure instance
63 * \param[in] buf Space to store the data
64 * \param[in] size The buffer length, must be aligned with power of 2
65 *
66 * \return ERR_NONE on success, or an error code on failure.
67 */
68int32_t ringbuffer_init(struct ringbuffer *const rb, void *buf, uint32_t size);
69
70/**
71 * \brief Get one byte from ring buffer, the user needs to handle the concurrent
72 * access on buffer via put/get/flush
73 *
74 * \param[in] rb The pointer to a ring buffer structure instance
75 * \param[in] data One byte space to store the read data
76 *
77 * \return ERR_NONE on success, or an error code on failure.
78 */
79int32_t ringbuffer_get(struct ringbuffer *const rb, uint8_t *data);
80
81/**
82 * \brief Put one byte to ring buffer, the user needs to handle the concurrent access
83 * on buffer via put/get/flush
84 *
85 * \param[in] rb The pointer to a ring buffer structure instance
86 * \param[in] data One byte data to be put into ring buffer
87 *
88 * \return ERR_NONE on success, or an error code on failure.
89 */
90int32_t ringbuffer_put(struct ringbuffer *const rb, uint8_t data);
91
92/**
93 * \brief Return the element number of ring buffer
94 *
95 * \param[in] rb The pointer to a ring buffer structure instance
96 *
97 * \return The number of elements in ring buffer [0, rb->size]
98 */
99uint32_t ringbuffer_num(const struct ringbuffer *const rb);
100
101/**
102 * \brief Flush ring buffer, the user needs to handle the concurrent access on buffer
103 * via put/get/flush
104 *
105 * \param[in] rb The pointer to a ring buffer structure instance
106 *
107 * \return ERR_NONE on success, or an error code on failure.
108 */
109uint32_t ringbuffer_flush(struct ringbuffer *const rb);
110
111/**@}*/
112
113#ifdef __cplusplus
114}
115#endif
116#endif /* _UTILS_RINGBUFFER_H_INCLUDED */