comparison g23m/condat/com/src/comlib/cl_ribu.c @ 0:509db1a7b7b8

initial import: leo2moko-r1
author Space Falcon <falcon@ivan.Harhan.ORG>
date Mon, 01 Jun 2015 03:24:05 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:509db1a7b7b8
1 /*
2 +-----------------------------------------------------------------------------
3 | Project : COMLIB
4 | Modul : cl_ribu.c
5 +-----------------------------------------------------------------------------
6 | Copyright 2002 Texas Instruments Berlin, AG
7 | All rights reserved.
8 |
9 | This file is confidential and a trade secret of Texas
10 | Instruments Berlin, AG
11 | The receipt of or possession of this file does not convey
12 | any rights to reproduce or disclose its contents or to
13 | manufacture, use, or sell anything it may describe, in
14 | whole, or in part, without the specific written consent of
15 | Texas Instruments Berlin, AG.
16 +-----------------------------------------------------------------------------
17 | Purpose : Definitions of common library functions: ring buffer
18 +-----------------------------------------------------------------------------
19 */
20 /*
21 * Version 1.0
22 */
23
24 /**********************************************************************************/
25
26 /*
27 NOTE:
28 */
29
30 /**********************************************************************************/
31
32 #ifndef CL_RIBU_C
33 #define CL_RIBU_C
34
35 #include <string.h>
36
37 #include "typedefs.h"
38 #include "vsi.h"
39 #include "cl_ribu.h"
40
41 #undef ENA_ASSERT
42
43 /*==== FUNCTIONS ==================================================*/
44
45 GLOBAL void cl_ribu_init(T_RIBU *ribu, const U8 depth)
46 {
47 TRACE_FUNCTION("cl_ribu_init()");
48
49 #ifdef ENA_ASSERT
50 assert(ribu NEQ NULL);
51 #else
52 if (ribu EQ NULL)
53 {
54 TRACE_ERROR("ribu EQ NULL");
55 return;
56 }
57 #endif
58
59 ribu->ri = 0;
60 ribu->wi = 0;
61 ribu->depth = depth;
62 ribu->filled = 0;
63 }
64
65 GLOBAL U8 cl_ribu_read_index(T_RIBU *ribu)
66 {
67 U8 ri;
68
69 TRACE_FUNCTION("cl_ribu_read_index()");
70
71 #ifdef ENA_ASSERT
72 assert(ribu NEQ NULL);
73 #else
74 if (ribu EQ NULL)
75 {
76 TRACE_ERROR("ribu EQ NULL");
77 return 0; //255;
78 }
79 #endif
80
81 ri = ribu->ri;
82 ribu->ri++;
83 if (ribu->ri EQ ribu->depth)
84 {
85 ribu->ri = 0;
86 }
87 ribu->filled--;
88 return ri;
89 }
90
91 GLOBAL U8 cl_ribu_write_index(T_RIBU *ribu)
92 {
93 U8 wi;
94
95 TRACE_FUNCTION("cl_ribu_write_index()");
96
97 #ifdef ENA_ASSERT
98 assert(ribu NEQ NULL);
99 #else
100 if (ribu EQ NULL)
101 {
102 TRACE_ERROR("ribu EQ NULL");
103 return 0; //255;
104 }
105 #endif
106
107 wi = ribu->wi;
108 ribu->wi++;
109 if (ribu->wi EQ ribu->depth)
110 {
111 ribu->wi = 0;
112 }
113
114 #ifdef ENA_ASSERT
115 assert(ribu->ri NEQ ribu->wi);
116 #else
117 if (ribu->ri EQ ribu->wi)
118 {
119 TRACE_ERROR("cl_ribu_write_index(): buffer full!");
120 return 0; //255;
121 }
122 #endif
123
124 ribu->filled++;
125 return wi;
126 }
127
128 GLOBAL void cl_ribu_create(T_RIBU_FD **ribu, const U8 buflen, const U8 depth)
129 {
130 int i;
131
132 TRACE_FUNCTION("cl_ribu_create()");
133
134 if (*ribu NEQ NULL)
135 {
136 TRACE_EVENT("cl_ribu_create(): *ribu already created ?");
137 }
138
139 MALLOC(*ribu, sizeof(T_RIBU_FD));
140
141 cl_ribu_init(&(*ribu)->idx, depth);
142
143 MALLOC((*ribu)->pFDv, depth * sizeof(T_FD*));
144
145 for (i = 0; i < depth; i++)
146 {
147 T_FD **pFD = &(*ribu)->pFDv[i];
148 MALLOC(*pFD, sizeof(T_FD));
149 MALLOC((*pFD)->buf, buflen * sizeof(U8));
150 }
151 }
152
153 GLOBAL void cl_ribu_release(T_RIBU_FD **ribu)
154 {
155 int i;
156
157 TRACE_FUNCTION("cl_ribu_release()");
158
159 if (*ribu EQ NULL)
160 {
161 TRACE_EVENT("cl_ribu_release(): *ribu EQ NULL!");
162 return;
163 }
164
165 for (i = 0; i < (*ribu)->idx.depth; i++)
166 {
167 T_FD *pFD = (*ribu)->pFDv[i];
168 MFREE(pFD->buf);
169 MFREE(pFD);
170 }
171
172 MFREE((*ribu)->pFDv);
173 MFREE(*ribu);
174 *ribu = NULL;
175 }
176
177 GLOBAL BOOL cl_ribu_data_avail(const T_RIBU_FD *ribu)
178 {
179 TRACE_FUNCTION("cl_ribu_data_avail()");
180
181 #ifdef ENA_ASSERT
182 assert(ribu NEQ NULL);
183 #else
184 if (ribu EQ NULL)
185 {
186 TRACE_ERROR("ribu EQ NULL");
187 return 0; //255;
188 }
189 #endif
190
191 return ribu->idx.ri NEQ ribu->idx.wi;
192 }
193
194 GLOBAL T_FD *cl_ribu_get_new_frame_desc(T_RIBU_FD *ribu)
195 {
196 U8 wi;
197 T_FD *pFDc;
198
199 TRACE_FUNCTION("cl_ribu_get_new_frame_desc()");
200
201 #ifdef ENA_ASSERT
202 assert(ribu NEQ NULL);
203 #else
204 if (ribu EQ NULL)
205 {
206 TRACE_ERROR("ribu EQ NULL");
207 return NULL;
208 }
209 #endif
210
211 wi = cl_ribu_write_index(&ribu->idx);
212 if (wi >= ribu->idx.depth)
213 {
214 TRACE_EVENT_P1("invalid write index: %d", (int)wi);
215 return NULL;
216 }
217 pFDc = ribu->pFDv[wi];
218
219 return pFDc;
220 }
221
222 GLOBAL void cl_ribu_put(const T_FD fd, T_RIBU_FD *ribu)
223 {
224 T_FD *pFDc = cl_ribu_get_new_frame_desc(ribu);
225
226 TRACE_FUNCTION("cl_ribu_put()");
227
228 if (pFDc EQ NULL)
229 {
230 TRACE_ERROR("cl_ribu_put(): no write buffer!");
231 return;
232 }
233
234 (*pFDc).type = fd.type;
235 (*pFDc).status = fd.status;
236 (*pFDc).len = fd.len;
237 memcpy((*pFDc).buf, fd.buf, fd.len);
238 }
239
240 GLOBAL T_FD *cl_ribu_get(T_RIBU_FD *ribu)
241 {
242 int ri;
243 T_FD *pFDc;
244
245 TRACE_FUNCTION("cl_ribu_get()");
246
247 #ifdef ENA_ASSERT
248 assert(ribu NEQ NULL);
249 #else
250 if (ribu EQ NULL)
251 {
252 TRACE_ERROR("ribu EQ NULL");
253 return NULL;
254 }
255 #endif
256
257 ri = (int)cl_ribu_read_index(&ribu->idx);
258 pFDc = ribu->pFDv[ri];
259
260 return pFDc;
261 }
262
263 GLOBAL void cl_set_frame_desc(T_FRAME_DESC *frame_desc, U8 *A0, U16 L0, U8 *A1, U16 L1)
264 {
265 assert(frame_desc NEQ NULL);
266
267 frame_desc->Adr[0] = A0;
268 frame_desc->Len[0] = L0;
269 frame_desc->Adr[1] = A1;
270 frame_desc->Len[1] = L1;
271 }
272
273 GLOBAL void cl_set_frame_desc_0(T_FRAME_DESC *frame_desc, U8 *A0, U16 L0)
274 {
275 assert(frame_desc NEQ NULL);
276
277 frame_desc->Adr[0] = A0;
278 frame_desc->Len[0] = L0;
279 frame_desc->Adr[1] = NULL;
280 frame_desc->Len[1] = 0;
281 }
282
283 #endif /* CL_RIBU_C */