aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios-contrib/os/various/tribuf.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios-contrib/os/various/tribuf.c')
-rw-r--r--lib/chibios-contrib/os/various/tribuf.c214
1 files changed, 214 insertions, 0 deletions
diff --git a/lib/chibios-contrib/os/various/tribuf.c b/lib/chibios-contrib/os/various/tribuf.c
new file mode 100644
index 000000000..6ba78d312
--- /dev/null
+++ b/lib/chibios-contrib/os/various/tribuf.c
@@ -0,0 +1,214 @@
1/*
2 Copyright (C) 2014..2015 Andrea Zoppi
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#include "osal.h"
18#include "tribuf.h"
19
20/**
21 * @file tribuf.c
22 * @brief Triple buffer handler source.
23 *
24 * @addtogroup TriBuf
25 * @{
26 */
27
28/*===========================================================================*/
29/* Driver local definitions. */
30/*===========================================================================*/
31
32/*===========================================================================*/
33/* Driver exported variables. */
34/*===========================================================================*/
35
36/*===========================================================================*/
37/* Driver local variables and types. */
38/*===========================================================================*/
39
40/*===========================================================================*/
41/* Driver local functions. */
42/*===========================================================================*/
43
44/*===========================================================================*/
45/* Driver interrupt handlers. */
46/*===========================================================================*/
47
48/*===========================================================================*/
49/* Driver exported functions. */
50/*===========================================================================*/
51
52/**
53 * @brief Initializes the tribuf handler object.
54 *
55 * @param[in] handler Pointer to the tribuf handler object.
56 * @param[in] front Pointer to the initial front buffer.
57 * @param[in] back Pointer to the initial back buffer.
58 * @param[in] orphan Pointer to the initial orphan buffer.
59 *
60 * @init
61 */
62void tribufObjectInit(tribuf_t *handler, void *front, void *back, void *orphan) {
63
64 handler->front = front;
65 handler->back = back;
66 handler->orphan = orphan;
67#if (TRIBUF_USE_WAIT == TRUE)
68 chSemObjectInit(&handler->ready, (cnt_t)0);
69#else
70 handler->ready = false;
71#endif
72}
73
74/**
75 * @brief Gets the current front buffer.
76 *
77 * @param[in] handler Pointer to the tribuf handler object.
78 * @return Pointer to the current front buffer.
79 *
80 * @api
81 */
82void *tribufGetFront(tribuf_t *handler) {
83
84 void *front;
85
86 osalSysLock();
87 front = tribufGetFrontI(handler);
88 osalSysUnlock();
89 return front;
90}
91
92/**
93 * @brief Swaps the current front buffer.
94 *
95 * @details Exchanges the pointer of the current front buffer, which will be
96 * dismissed, with the pointer of the current orphan buffer, which
97 * holds the content of the new front buffer.
98 *
99 * @pre The orphan buffer holds new data, swapped by the back buffer.
100 * @pre The fron buffer is ready for swap.
101 * @post The orphan buffer can be used as new back buffer in the future.
102 *
103 * @param[in] handler Pointer to the tribuf handler object.
104 *
105 * @iclass
106 */
107void tribufSwapFrontI(tribuf_t *handler) {
108
109 void *front;
110
111 osalDbgCheckClassI();
112
113 front = handler->orphan;
114 handler->orphan = handler->front;
115 handler->front = front;
116}
117
118/**
119 * @brief Swaps the current front buffer.
120 *
121 * @details Exchanges the pointer of the current front buffer, which will be
122 * dismissed, with the pointer of the current orphan buffer, which
123 * holds the content of the new front buffer.
124 *
125 * @pre The orphan buffer holds new data, swapped by the back buffer.
126 * @pre The fron buffer is ready for swap.
127 * @post The orphan buffer can be used as new back buffer in the future.
128 *
129 * @param[in] handler Pointer to the tribuf handler object.
130 *
131 * @api
132 */
133void tribufSwapFront(tribuf_t *handler) {
134
135 osalSysLock();
136 tribufSwapFrontI(handler);
137 osalSysUnlock();
138}
139
140/**
141 * @brief Gets the current back buffer.
142 *
143 * @param[in] handler Pointer to the tribuf handler object.
144 * @return Pointer to the current back buffer.
145 *
146 * @api
147 */
148void *tribufGetBack(tribuf_t *handler) {
149
150 void *back;
151
152 osalSysLock();
153 back = tribufGetBackI(handler);
154 osalSysUnlock();
155 return back;
156}
157
158/**
159 * @brief Swaps the current back buffer.
160 *
161 * @details Exchanges the pointer of the current back buffer, which holds new
162 * useful data, with the pointer of the current orphan buffer.
163 *
164 * @pre The orphan buffer holds no meaningful data.
165 * @post The orphan buffer is candidate for new front buffer.
166 * @post A new front buffer is ready and signaled.
167 *
168 * @param[in] handler Pointer to the tribuf handler object.
169 *
170 * @iclass
171 */
172void tribufSwapBackI(tribuf_t *handler) {
173
174 void *back;
175
176 osalDbgCheckClassI();
177
178 back = handler->orphan;
179 handler->orphan = handler->back;
180 handler->back = back;
181
182#if (TRIBUF_USE_WAIT == TRUE)
183 if (chSemGetCounterI(&handler->ready) < (cnt_t)1)
184 chSemSignalI(&handler->ready);
185#else
186 handler->ready = true;
187#endif
188}
189
190/**
191 * @brief Swaps the current back buffer.
192 *
193 * @details Exchanges the pointer of the current back buffer, which holds new
194 * useful data, with the pointer of the current orphan buffer.
195 *
196 * @pre The orphan buffer holds no meaningful data.
197 * @post The orphan buffer is candidate for new front buffer.
198 * @post A new front buffer is ready and signaled.
199 *
200 * @param[in] handler Pointer to the tribuf handler object.
201 *
202 * @api
203 */
204void tribufSwapBack(tribuf_t *handler) {
205
206 osalSysLock();
207 tribufSwapBackI(handler);
208#if (TRIBUF_USE_WAIT == TRUE)
209 osalOsRescheduleS();
210#endif
211 osalSysUnlock();
212}
213
214/** @} */