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