1 /*
2 * Copyright (c) 2018, 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
33 #include <ti/drivers/utils/StructRingBuf.h>
34
35 #include <ti/drivers/dpl/HwiP.h>
36
37 /*
38 * ======== StructRingBuf_construct ========
39 */
StructRingBuf_construct(StructRingBuf_Handle object,void * bufPtr,size_t bufSize,size_t structSize)40 void StructRingBuf_construct(StructRingBuf_Handle object, void *bufPtr,
41 size_t bufSize, size_t structSize)
42 {
43 object->buffer = bufPtr;
44 object->length = bufSize;
45 object->count = 0;
46 object->head = bufSize - 1;
47 object->tail = 0;
48 object->maxCount = 0;
49 object->structSize = structSize;
50 }
51
52 /*
53 * ======== StructRingBuf_get ========
54 */
StructRingBuf_get(StructRingBuf_Handle object,void * data)55 int StructRingBuf_get(StructRingBuf_Handle object, void *data)
56 {
57 unsigned int key;
58
59 key = HwiP_disable();
60
61 if (!object->count) {
62 HwiP_restore(key);
63 return -1;
64 }
65
66 memcpy(data, &object->buffer[object->structSize * object->tail], object->structSize);
67 object->tail = (object->tail + 1) % object->length;
68 object->count--;
69
70 HwiP_restore(key);
71
72 return (object->count);
73 }
74
75 /*
76 * ======== StructRingBuf_getCount ========
77 */
StructRingBuf_getCount(StructRingBuf_Handle object)78 int StructRingBuf_getCount(StructRingBuf_Handle object)
79 {
80 return (object->count);
81 }
82
83 /*
84 * ======== StructRingBuf_isFull ========
85 */
StructRingBuf_isFull(StructRingBuf_Handle object)86 bool StructRingBuf_isFull(StructRingBuf_Handle object)
87 {
88 return (object->count == object->length);
89 }
90
91 /*
92 * ======== StructRingBuf_getMaxCount ========
93 */
StructRingBuf_getMaxCount(StructRingBuf_Handle object)94 int StructRingBuf_getMaxCount(StructRingBuf_Handle object)
95 {
96 return (object->maxCount);
97 }
98
99 /*
100 * ======== StructRingBuf_peek ========
101 */
StructRingBuf_peek(StructRingBuf_Handle object,void ** data)102 int StructRingBuf_peek(StructRingBuf_Handle object, void **data)
103 {
104 *data = &object->buffer[object->structSize * object->tail];
105
106 return (object->count);
107 }
108
109 /*
110 * ======== StructRingBuf_put ========
111 */
StructRingBuf_put(StructRingBuf_Handle object,const void * data)112 int StructRingBuf_put(StructRingBuf_Handle object, const void *data)
113 {
114 unsigned int key;
115 unsigned int next;
116
117 key = HwiP_disable();
118
119 if (object->count != object->length) {
120 next = (object->head + 1) % object->length;
121 memcpy(&object->buffer[object->structSize * next], data, object->structSize);
122 object->head = next;
123 object->count++;
124 object->maxCount = (object->count > object->maxCount) ?
125 object->count :
126 object->maxCount;
127 }
128 else {
129
130 HwiP_restore(key);
131 return (-1);
132 }
133
134 HwiP_restore(key);
135
136 return (object->count);
137 }
138