b7257d1be38a1f7a7a22a5a26875cfc3c7b5193c
2 /*
3 Copyright (c) 2016, Texas Instruments Incorporated - http://www.ti.com/
4 All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the
16 * distribution.
17 *
18 * Neither the name of Texas Instruments Incorporated nor the names of
19 * its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
36 /*
37 * ======== AudioFuncProc.c ========
38 */
40 #include <xdc/std.h>
41 #include <xdc/cfg/global.h>
42 #include <xdc/runtime/Log.h>
43 #include <xdc/runtime/System.h>
44 #include <ti/sysbios/BIOS.h>
45 #include <ti/sysbios/knl/Clock.h>
46 #include <ti/sysbios/knl/Semaphore.h>
47 #include <stdlib.h>
48 #include "fwkSim.h"
50 #include "paf_heapMgr.h"
51 #include "alphaFuncProc.h"
53 #define ENABLE_AE_TRACE
54 #ifdef ENABLE_AE_TRACE
55 //#define AE_TRACE0(a) LOG_printf a
56 #define AE_TRACE0(a) Log_info0(a)
57 #define AE_TRACE1(a,b) Log_info1(a,b)
58 #define AE_TRACE2(a,b,c) Log_info2(a,b,c)
59 #define AE_TRACE3(a,b,c,d) Log_info3(a,b,c,d)
60 #define AE_TRACE4(a,b,c,d,e) Log_info4(a,b,c,d,e)
61 #else
62 #define AE_TRACE0(a)
63 #define AE_TRACE1(a,b)
64 #define AE_TRACE2(a,b,c)
65 #define AE_TRACE3(a,b,c,d)
66 #define AE_TRACE4(a,b,c,d,e)
67 #endif
70 // Global debug counters */
71 Uint32 gTaskAfpCnt=0;
73 AFP_Obj AFP_OBJ;
75 Void *AFP_memAlloc(
76 AFP_Handle handle,
77 Int32 *pHeapId,
78 UInt32 size,
79 UInt32 align
80 )
81 {
82 IHeap_Handle hHeap;
84 hHeap = pafHeapMgr_readHeapHandle(*pHeapId);
85 return (void *)Memory_alloc((IHeap_Handle)hHeap, size, align, NULL);
86 }
89 Void AFP_process(
90 AFP_Handle handle
91 )
92 {
93 Int rxIdx, txIdx;
94 Int encap, len, res_len, error, cnt, txStrip, i, rxOffset, txOffset;
96 /* Process Alpha commands */
97 for(;;)
98 {
99 /* Set read and write buffer indices */
100 rxIdx = 2;
101 txIdx = 3;
102 txOffset = 0;
103 encap = 0;
106 /* Read the payload */
107 if (handle->dcs7->fxns->read(handle->dcs7,
108 handle->rxBuf+rxIdx,
109 handle->params->rxBufSize-(rxIdx<<1)))
110 {
111 //handle->fxns->log(handle,handle->logObj,
112 // "AFP6: Error in reading message");
114 /* Write the response */
115 handle->txBuf[txIdx++] = 0xdead;
116 AE_TRACE0("AFP_process: 0xDEAD: Error in reading alpha message.");
117 handle->txBuf[3-txOffset-1] = txIdx-3+txOffset;
118 handle->dcs7->fxns->write(handle->dcs7,
119 &handle->txBuf[3-txOffset-1],
120 handle->params->txBufSize-(3-txOffset-1<<1));
122 continue;
123 }
125 /* Get CPM count */
126 cnt = handle->rxBuf[rxIdx++];
127 /* Process the C-record */
128 while (cnt)
129 {
130 /* Get the length of alpha code */
131 len = handle->acp->fxns->length(handle->acp, &handle->rxBuf[rxIdx]);
132 /* Get the response length */
133 res_len = handle->acp->fxns->htgnel(handle->acp, &handle->rxBuf[rxIdx]);
134 if (res_len > (handle->params->txBufSize-2>>1)-txIdx)
135 {
136 handle->txBuf[txIdx++] = 0xdead;
137 //handle->fxns->log(handle,handle->logObj,
138 // "AFP6: Response too big");
139 AE_TRACE0("AFP_process: 0xDEAD: Response too big.");
140 break;
141 }
143 /* Add encapsulation to alpha command -- ACP limitation */
144 if (((handle->rxBuf[rxIdx] & 0xff00) == 0xc900) ||
145 (handle->rxBuf[rxIdx] == 0xcd01))
146 {
147 Int lene, nele;
148 Int lenp, nelp;
149 Int k;
150 if ((handle->rxBuf[rxIdx]&0xff00) == 0xc900)
151 {
152 k=1;
153 }
154 else
155 {
156 k=2;
157 }
158 lene = len-k;
159 nele = 0;
160 while (lene)
161 {
162 lenp = handle->acp->fxns->length(handle->acp, &handle->rxBuf[rxIdx+k]);
163 nelp = handle->acp->fxns->htgnel(handle->acp, &handle->rxBuf[rxIdx+k]);
164 k += lenp;
165 lene -= lenp;
166 nele += nelp;
167 }
168 if (nele < 256)
169 {
170 res_len = nele+1;
171 }
172 else
173 {
174 res_len = nele+2;
175 }
176 rxOffset = 0;
177 if (res_len > (handle->params->txBufSize-4>>1)-txIdx)
178 {
179 handle->txBuf[txIdx++] = 0xc901;
180 handle->txBuf[txIdx++] = 0xdead;
181 AE_TRACE0("AFP_process: 0xDEAD: Response too big.");
182 //handle->fxns->log(handle,handle->logObj,
183 // "AFP6: Response too big");
184 break;
185 }
186 }
187 else if (len < 256)
188 {
189 handle->rxBuf[--rxIdx] = 0xc900+len;
190 rxOffset = 1;
191 }
192 else
193 {
194 handle->rxBuf[--rxIdx] = len;
195 handle->rxBuf[--rxIdx] = 0xcd01;
196 rxOffset = 2;
197 }
200 /* Process alpha command */
201 error = handle->acp->fxns->sequence(handle->acp,
202 &handle->rxBuf[rxIdx],
203 &handle->txBuf[txIdx]);
206 if (error)
207 {
208 //handle->fxns->log(handle,handle->logObj,
209 // "AFP6: Sequence error");
210 AE_TRACE0("AFP_process: 0xDEAD: Sequence error.");
211 if (res_len || encap)
212 {
213 handle->txBuf[txIdx++] = 0xdead;
214 }
215 cnt -= len;
216 rxIdx += len+rxOffset;
217 continue;
218 }
219 else if (rxOffset)
220 {
221 /* Delete encapsulation -- ACP limitation */
222 if ((handle->txBuf[txIdx] & 0xff00) == 0xc900)
223 {
224 txStrip = 1;
225 }
226 else if (handle->txBuf[txIdx] == 0xcd01)
227 {
228 txStrip=2;
229 }
230 for (i=0; i<res_len; ++i)
231 {
232 handle->txBuf[txIdx+i] = handle->txBuf[txIdx+i+txStrip];
233 }
234 }
236 /* Update the count, rxIdx and txIdx */
237 cnt -= len;
238 rxIdx += len+rxOffset;
239 txIdx += res_len;
240 }
242 /* Write the response */
243 handle->txBuf[3-txOffset-1] = txIdx-3+txOffset;
244 handle->dcs7->fxns->write(handle->dcs7,
245 &handle->txBuf[3-txOffset-1],
246 handle->params->txBufSize-(3-txOffset-1<<1));
247 }
248 }
250 /*
251 * ======== taskAfpFxn ========
252 * Audio Function Processing task function
253 */
254 Void taskAfpFxn(AFP_Handle handle)
255 {
256 #ifndef SIMULATE_RX_ALPHA
257 IALG_Status *pStatus;
258 IHeap_Handle hHeap;
259 void *memSeg;
260 #else // SIMULATE_RX_ALPHA
261 Uint32 randVal;
262 Uint32 timeout;
263 #endif // SIMULATE_RX_ALPHA
265 #ifndef SIMULATE_RX_ALPHA
266 Log_info0("TaskAfp: Enter");
268 if (handle == NULL)
269 {
270 handle = &AFP_OBJ;
271 }
273 /* Create an instance of ACP Algorithm */
274 handle->fxns->acpInit();
275 handle->acp = ACP_create(&ACP_MDS_IACP,handle->acpParams);
276 if (handle->acp == NULL)
277 {
278 //handle->fxns->log(handle,handle->logObj,
279 // "AFP6: ACP instance creation failed");
280 Log_info0("TaskAfp: ACP instance creation failed");
281 }
282 else
283 {
284 //extern struct {
285 // int size;
286 //} pafIdentification;
288 // Get pointer to ACP Status structure
289 ((ALG_Handle)handle->acp)->fxns->algControl((IALG_Handle)(handle->acp),
290 ACP_GETSTATUSADDRESS1,
291 (IALG_Status *)&pStatus);
292 // FL: (*** ?) Why STD BETA UART? Why not STD_BETA_ACP?
293 // Attach ACP Status structure to Beta Unit Id==STD_BETA_UART
294 handle->acp->fxns->attach(handle->acp,
295 ACP_SERIES_STD,
296 STD_BETA_UART,
297 pStatus);
299 // Attach ACP Status structure to Beta Unit Id==STD_BETA_UART
300 handle->acp->fxns->attach(handle->acp,
301 ACP_SERIES_STD,
302 STD_BETA_BETATABLE,
303 (IALG_Status *)((ACP_MDS_Obj *)handle->acp)->config.betaTable[ACP_SERIES_STD]);
304 // Attach STD Beta Table to Beta Unit Id==STD_BETA_BETATABLE
305 handle->acp->fxns->attach(handle->acp,
306 ACP_SERIES_STD,
307 STD_BETA_PHITABLE,
308 (IALG_Status *)((ACP_MDS_Obj *)handle->acp)->config.phiTable[ACP_SERIES_STD]);
309 // Attach STD Sigma Table to Beta Unit Id==STD_BETA_SIGMATABLE
310 handle->acp->fxns->attach(handle->acp,
311 ACP_SERIES_STD,
312 STD_BETA_SIGMATABLE,
313 (IALG_Status *)((ACP_MDS_Obj *)handle->acp)->config.sigmaTable[ACP_SERIES_STD]);
315 // FL: (*** ?) STD_BETA_IDENTITY already attached in main()->systemStreamMain(), so remove this
316 // Attach PAF Identification structure to Beta Unit Id==STD_BETA_IDENTITY
317 //handle->acp->fxns->attach(handle->acp, ACP_SERIES_STD, STD_BETA_IDENTITY,
318 // (IALG_Status *)&pafIdentification);
320 //handle->fxns->log(handle,handle->logObj,"AFP6: ACP initialized");
321 Log_info0("TaskAfp: ACP initialized");
323 // Initialize DCS
324 handle->dcs7 = handle->dcs7Fxns->open(handle->params->dcs7Params,
325 handle->dcs7Config,
326 (DCS7_Fxns_Ptr)handle->dcs7Fxns,
327 handle->acp);
328 if (handle == NULL)
329 {
330 //handle->fxns->log(handle,handle->logObj,
331 // "AFP6: DCS6 creation failed");
332 Log_info0("TaskAfp: DCS7 creation failed");
333 }
334 else
335 {
336 Log_info0("TaskAfp: DCS7 initialized");
338 hHeap = pafHeapMgr_readHeapHandle(*handle->pBufHeapId);
339 memSeg = (void *)&hHeap;
340 handle->rxBuf = (Uint16 *)handle->dcs7Fxns->memAlloc(handle->dcs7,
341 memSeg,
342 handle->params->rxBufSize,
343 4);
344 handle->txBuf = (Uint16 *)handle->dcs7Fxns->memAlloc(handle->dcs7,
345 memSeg,
346 handle->params->txBufSize,
347 4);
348 if ((handle->rxBuf == NULL) || (handle->txBuf == NULL))
349 {
350 //handle->fxns->log(handle,handle->logObj,
351 // "AFP6: Buffer allocation failed");
352 Log_info0("TaskAfp: Buffer allocation failed");
353 }
354 else
355 {
356 //handle->fxns->log(handle,handle->logObj,"AFP6: AFP6 initialized");
357 Log_info0("TaskAfp: Initialized");
359 // Process alpha commands
360 handle->fxns->process(handle);
361 }
362 }
363 }
365 /* Terminate AFP */
366 handle->fxns->acpExit();
367 Log_info0("TaskAfp: Exit");
368 Task_setPri(TaskAfp, -1);
369 Task_yield();
370 #else // SIMULATE_RX_ALPHA
371 //System_printf("Enter taskAfpFxn()\n");
372 Log_info0("Enter taskAfpFxn()");
374 for (;;)
375 {
376 /* Compute next time out for one-shot timer */
377 randVal = (Uint32)rand();
378 timeout = (randVal - (randVal / RX_ALPHA_DELTA_TICKS)*RX_ALPHA_DELTA_TICKS) + RX_ALPHA_MIN_TICKS;
379 Clock_setTimeout(clockRxAlpha, timeout);
380 Clock_start(clockRxAlpha);
382 //System_printf("Start Rx alpha clock, timeout=%lu\n",(ULong)timeout);
383 Log_info1("Start Rx alpha clock, timeout=%u", timeout);
385 /* Post semaphore indicating Rx alpha command */
386 Semaphore_pend(semaphoreRxAlpha, BIOS_WAIT_FOREVER);
388 gTaskAfpCnt++;
389 }
391 //System_printf("Exit taskAfpFxn()\n");
392 Log_info0("Exit taskAfpFxn()");
393 #endif // SIMULATE_RX_ALPHA
394 }
396 const AFP_Fxns AFP_DEFAULT_FXNS = {
397 // AFP_log,
398 AFP_memAlloc,
399 AFP_process,
400 ACP_MDS_init,
401 ACP_MDS_exit
402 };
404 AFP_Obj AFP_OBJ = {
405 sizeof(AFP_Obj),
406 &AFP_DEFAULT_FXNS,
407 &AFP_PARAMS,
408 NULL,
409 &DCS7_FXNS,
410 NULL,
411 NULL,
412 (ACP_Params*)&ACP_PARAMS,
413 NULL,
414 NULL,
415 (Int *)&gPafHeapIdExt,
416 (Int *)&gPafHeapIdExt,
417 // NULL,
418 };