blob: 45cac83fc6299f5c66159d11ae87f1214f21f3f0 [file] [log] [blame]
Kévin Redonccbed0b2019-01-24 18:30:26 +01001/**
2 * \file
3 *
4 * \brief Ringbuffer functionality implementation.
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#include "utils_ringbuffer.h"
34
35/**
36 * \brief Ringbuffer init
37 */
38int32_t ringbuffer_init(struct ringbuffer *const rb, void *buf, uint32_t size)
39{
40 ASSERT(rb && buf && size);
41
42 /*
43 * buf size must be aligned to power of 2
44 */
45 if ((size & (size - 1)) != 0) {
46 return ERR_INVALID_ARG;
47 }
48
49 /* size - 1 is faster in calculation */
50 rb->size = size - 1;
51 rb->read_index = 0;
52 rb->write_index = rb->read_index;
53 rb->buf = (uint8_t *)buf;
54
55 return ERR_NONE;
56}
57
58/**
59 * \brief Get one byte from ringbuffer
60 *
61 */
62int32_t ringbuffer_get(struct ringbuffer *const rb, uint8_t *data)
63{
64 ASSERT(rb && data);
65
66 if (rb->write_index != rb->read_index) {
67 *data = rb->buf[rb->read_index & rb->size];
68 rb->read_index++;
69 return ERR_NONE;
70 }
71
72 return ERR_NOT_FOUND;
73}
74
75/**
76 * \brief Put one byte to ringbuffer
77 *
78 */
79int32_t ringbuffer_put(struct ringbuffer *const rb, uint8_t data)
80{
81 ASSERT(rb);
82
83 rb->buf[rb->write_index & rb->size] = data;
84
85 /*
86 * buffer full strategy: new data will overwrite the oldest data in
87 * the buffer
88 */
89 if ((rb->write_index - rb->read_index) > rb->size) {
90 rb->read_index = rb->write_index - rb->size;
91 }
92
93 rb->write_index++;
94
95 return ERR_NONE;
96}
97
98/**
99 * \brief Return the element number of ringbuffer
100 */
101uint32_t ringbuffer_num(const struct ringbuffer *const rb)
102{
103 ASSERT(rb);
104
105 return rb->write_index - rb->read_index;
106}
107
108/**
109 * \brief Flush ringbuffer
110 */
111uint32_t ringbuffer_flush(struct ringbuffer *const rb)
112{
113 ASSERT(rb);
114
115 rb->read_index = rb->write_index;
116
117 return ERR_NONE;
118}