1 /*
2  * Copyright (c) 2015-2016, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 #include <ti/drivers/dpl/HwiP.h>
33 #include <ti/drivers/utils/RingBuf.h>
34 
35 /*
36  *  ======== RingBuf_construct ========
37  */
RingBuf_construct(RingBuf_Handle object,unsigned char * bufPtr,size_t bufSize)38 void RingBuf_construct(RingBuf_Handle object, unsigned char *bufPtr,
39     size_t bufSize)
40 {
41     object->buffer = bufPtr;
42     object->length = bufSize;
43     object->count = 0;
44     object->head = bufSize - 1;
45     object->tail = 0;
46     object->maxCount = 0;
47 }
48 
49 /*
50  *  ======== RingBuf_get ========
51  */
RingBuf_get(RingBuf_Handle object,unsigned char * data)52 int RingBuf_get(RingBuf_Handle object, unsigned char *data)
53 {
54     unsigned int key;
55 
56     key = HwiP_disable();
57 
58     if (!object->count) {
59         HwiP_restore(key);
60         return -1;
61     }
62 
63     *data = object->buffer[object->tail];
64     object->tail = (object->tail + 1) % object->length;
65     object->count--;
66 
67     HwiP_restore(key);
68 
69     return (object->count);
70 }
71 
72 /*
73  *  ======== RingBuf_getCount ========
74  */
RingBuf_getCount(RingBuf_Handle object)75 int RingBuf_getCount(RingBuf_Handle object)
76 {
77     return (object->count);
78 }
79 
80 /*
81  *  ======== RingBuf_isFull ========
82  */
RingBuf_isFull(RingBuf_Handle object)83 bool RingBuf_isFull(RingBuf_Handle object)
84 {
85     return (object->count == object->length);
86 }
87 
88 /*
89  *  ======== RingBuf_getMaxCount ========
90  */
RingBuf_getMaxCount(RingBuf_Handle object)91 int RingBuf_getMaxCount(RingBuf_Handle object)
92 {
93     return (object->maxCount);
94 }
95 
96 /*
97  *  ======== RingBuf_peek ========
98  */
RingBuf_peek(RingBuf_Handle object,unsigned char * data)99 int RingBuf_peek(RingBuf_Handle object, unsigned char *data)
100 {
101     unsigned int key;
102     int          retCount;
103 
104     key = HwiP_disable();
105 
106     *data = object->buffer[object->tail];
107     retCount = object->count;
108 
109     HwiP_restore(key);
110 
111     return (retCount);
112 }
113 
114 /*
115  *  ======== RingBuf_put ========
116  */
RingBuf_put(RingBuf_Handle object,unsigned char data)117 int RingBuf_put(RingBuf_Handle object, unsigned char data)
118 {
119     unsigned int key;
120     unsigned int next;
121 
122     key = HwiP_disable();
123 
124     if (object->count != object->length) {
125         next = (object->head + 1) % object->length;
126         object->buffer[next] = data;
127         object->head = next;
128         object->count++;
129         object->maxCount = (object->count > object->maxCount) ?
130                             object->count :
131                             object->maxCount;
132     }
133     else {
134 
135         HwiP_restore(key);
136         return (-1);
137     }
138 
139     HwiP_restore(key);
140 
141     return (object->count);
142 }
143