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
69 // Global debug counters */
70 Uint32 gTaskAfpCnt=0;
72 AFP_Obj AFP_OBJ;
74 Void *AFP_memAlloc(
75 AFP_Handle handle,
76 Int32 *pHeapId,
77 UInt32 size,
78 UInt32 align
79 )
80 {
81 IHeap_Handle hHeap;
83 hHeap = pafHeapMgr_readHeapHandle(*pHeapId);
84 return (void *)Memory_alloc((IHeap_Handle)hHeap, size, align, NULL);
85 }
87 Void AFP_process(
88 AFP_Handle handle
89 )
90 {
91 Int rxIdx, txIdx;
92 Int encap, len, res_len, error, cnt, txStrip, i, rxOffset, txOffset;
94 /* Process Alpha commands */
95 for(;;)
96 {
97 /* Set read and write buffer indices */
98 rxIdx = 2;
99 txIdx = 3;
100 txOffset = 0;
101 encap = 0;
103 /* Read the payload */
104 if (handle->dcs7->fxns->read(handle->dcs7,
105 handle->rxBuf+rxIdx,
106 handle->params->rxBufSize-(rxIdx<<1)))
107 {
108 //handle->fxns->log(handle,handle->logObj,
109 // "AFP6: Error in reading message");
111 /* Write the response */
112 handle->txBuf[txIdx++] = 0xdead;
113 AE_TRACE0("AFP_process: 0xDEAD: Error in reading alpha message.");
114 handle->txBuf[3-txOffset-1] = txIdx-3+txOffset;
115 handle->dcs7->fxns->write(handle->dcs7,
116 &handle->txBuf[3-txOffset-1],
117 handle->params->txBufSize-(3-txOffset-1<<1));
119 continue;
120 }
122 /* Get CPM count */
123 cnt = handle->rxBuf[rxIdx++];
124 /* Process the C-record */
125 while (cnt)
126 {
127 /* Get the length of alpha code */
128 len = handle->acp->fxns->length(handle->acp, &handle->rxBuf[rxIdx]);
129 /* Get the response length */
130 res_len = handle->acp->fxns->htgnel(handle->acp, &handle->rxBuf[rxIdx]);
131 if (res_len > (handle->params->txBufSize-2>>1)-txIdx)
132 {
133 handle->txBuf[txIdx++] = 0xdead;
134 //handle->fxns->log(handle,handle->logObj,
135 // "AFP6: Response too big");
136 AE_TRACE0("AFP_process: 0xDEAD: Response too big.");
137 break;
138 }
140 /* Add encapsulation to alpha command -- ACP limitation */
141 if (((handle->rxBuf[rxIdx] & 0xff00) == 0xc900) ||
142 (handle->rxBuf[rxIdx] == 0xcd01))
143 {
144 Int lene, nele;
145 Int lenp, nelp;
146 Int k;
147 if ((handle->rxBuf[rxIdx]&0xff00) == 0xc900)
148 {
149 k=1;
150 }
151 else
152 {
153 k=2;
154 }
155 lene = len-k;
156 nele = 0;
157 while (lene)
158 {
159 lenp = handle->acp->fxns->length(handle->acp, &handle->rxBuf[rxIdx+k]);
160 nelp = handle->acp->fxns->htgnel(handle->acp, &handle->rxBuf[rxIdx+k]);
161 k += lenp;
162 lene -= lenp;
163 nele += nelp;
164 }
165 if (nele < 256)
166 {
167 res_len = nele+1;
168 }
169 else
170 {
171 res_len = nele+2;
172 }
173 rxOffset = 0;
174 if (res_len > (handle->params->txBufSize-4>>1)-txIdx)
175 {
176 handle->txBuf[txIdx++] = 0xc901;
177 handle->txBuf[txIdx++] = 0xdead;
178 AE_TRACE0("AFP_process: 0xDEAD: Response too big.");
179 //handle->fxns->log(handle,handle->logObj,
180 // "AFP6: Response too big");
181 break;
182 }
183 }
184 else if (len < 256)
185 {
186 handle->rxBuf[--rxIdx] = 0xc900+len;
187 rxOffset = 1;
188 }
189 else
190 {
191 handle->rxBuf[--rxIdx] = len;
192 handle->rxBuf[--rxIdx] = 0xcd01;
193 rxOffset = 2;
194 }
196 /* Process alpha command */
197 error = handle->acp->fxns->sequence(handle->acp,
198 &handle->rxBuf[rxIdx],
199 &handle->txBuf[txIdx]);
200 if (error)
201 {
202 //handle->fxns->log(handle,handle->logObj,
203 // "AFP6: Sequence error");
204 AE_TRACE0("AFP_process: 0xDEAD: Sequence error.");
205 if (res_len || encap)
206 {
207 handle->txBuf[txIdx++] = 0xdead;
208 }
209 cnt -= len;
210 rxIdx += len+rxOffset;
211 continue;
212 }
213 else if (rxOffset)
214 {
215 /* Delete encapsulation -- ACP limitation */
216 if ((handle->txBuf[txIdx] & 0xff00) == 0xc900)
217 {
218 txStrip = 1;
219 }
220 else if (handle->txBuf[txIdx] == 0xcd01)
221 {
222 txStrip=2;
223 }
224 for (i=0; i<res_len; ++i)
225 {
226 handle->txBuf[txIdx+i] = handle->txBuf[txIdx+i+txStrip];
227 }
228 }
230 /* Update the count, rxIdx and txIdx */
231 cnt -= len;
232 rxIdx += len+rxOffset;
233 txIdx += res_len;
234 }
236 /* Write the response */
237 handle->txBuf[3-txOffset-1] = txIdx-3+txOffset;
238 handle->dcs7->fxns->write(handle->dcs7,
239 &handle->txBuf[3-txOffset-1],
240 handle->params->txBufSize-(3-txOffset-1<<1));
241 }
242 }
244 /*
245 * ======== taskAfpFxn ========
246 * Audio Function Processing task function
247 */
248 Void taskAfpFxn(AFP_Handle handle)
249 {
250 #ifndef SIMULATE_RX_ALPHA
251 IALG_Status *pStatus;
252 IHeap_Handle hHeap;
253 void *memSeg;
254 #else // SIMULATE_RX_ALPHA
255 Uint32 randVal;
256 Uint32 timeout;
257 #endif // SIMULATE_RX_ALPHA
259 #ifndef SIMULATE_RX_ALPHA
260 Log_info0("TaskAfp: Enter");
262 if (handle == NULL)
263 {
264 handle = &AFP_OBJ;
265 }
267 /* Create an instance of ACP Algorithm */
268 handle->fxns->acpInit();
269 handle->acp = ACP_create(&ACP_MDS_IACP,handle->acpParams);
270 if (handle->acp == NULL)
271 {
272 //handle->fxns->log(handle,handle->logObj,
273 // "AFP6: ACP instance creation failed");
274 Log_info0("TaskAfp: ACP instance creation failed");
275 }
276 else
277 {
278 //extern struct {
279 // int size;
280 //} pafIdentification;
282 // Get pointer to ACP Status structure
283 ((ALG_Handle)handle->acp)->fxns->algControl((IALG_Handle)(handle->acp),
284 ACP_GETSTATUSADDRESS1,
285 (IALG_Status *)&pStatus);
286 // FL: (*** ?) Why STD BETA UART? Why not STD_BETA_ACP?
287 // Attach ACP Status structure to Beta Unit Id==STD_BETA_UART
288 handle->acp->fxns->attach(handle->acp,
289 ACP_SERIES_STD,
290 STD_BETA_UART,
291 pStatus);
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_BETATABLE,
297 (IALG_Status *)((ACP_MDS_Obj *)handle->acp)->config.betaTable[ACP_SERIES_STD]);
298 // Attach STD Beta Table to Beta Unit Id==STD_BETA_BETATABLE
299 handle->acp->fxns->attach(handle->acp,
300 ACP_SERIES_STD,
301 STD_BETA_PHITABLE,
302 (IALG_Status *)((ACP_MDS_Obj *)handle->acp)->config.phiTable[ACP_SERIES_STD]);
303 // Attach STD Sigma Table to Beta Unit Id==STD_BETA_SIGMATABLE
304 handle->acp->fxns->attach(handle->acp,
305 ACP_SERIES_STD,
306 STD_BETA_SIGMATABLE,
307 (IALG_Status *)((ACP_MDS_Obj *)handle->acp)->config.sigmaTable[ACP_SERIES_STD]);
309 // FL: (*** ?) STD_BETA_IDENTITY already attached in main()->systemStreamMain(), so remove this
310 // Attach PAF Identification structure to Beta Unit Id==STD_BETA_IDENTITY
311 //handle->acp->fxns->attach(handle->acp, ACP_SERIES_STD, STD_BETA_IDENTITY,
312 // (IALG_Status *)&pafIdentification);
314 //handle->fxns->log(handle,handle->logObj,"AFP6: ACP initialized");
315 Log_info0("TaskAfp: ACP initialized");
317 // Initialize DCS
318 handle->dcs7 = handle->dcs7Fxns->open(handle->params->dcs7Params,
319 handle->dcs7Config,
320 (DCS7_Fxns_Ptr)handle->dcs7Fxns,
321 handle->acp);
322 if (handle == NULL)
323 {
324 //handle->fxns->log(handle,handle->logObj,
325 // "AFP6: DCS6 creation failed");
326 Log_info0("TaskAfp: DCS7 creation failed");
327 }
328 else
329 {
330 Log_info0("TaskAfp: DCS7 initialized");
332 hHeap = pafHeapMgr_readHeapHandle(*handle->pBufHeapId);
333 memSeg = (void *)&hHeap;
334 handle->rxBuf = (Uint16 *)handle->dcs7Fxns->memAlloc(handle->dcs7,
335 memSeg,
336 handle->params->rxBufSize,
337 4);
338 handle->txBuf = (Uint16 *)handle->dcs7Fxns->memAlloc(handle->dcs7,
339 memSeg,
340 handle->params->txBufSize,
341 4);
342 if ((handle->rxBuf == NULL) || (handle->txBuf == NULL))
343 {
344 //handle->fxns->log(handle,handle->logObj,
345 // "AFP6: Buffer allocation failed");
346 Log_info0("TaskAfp: Buffer allocation failed");
347 }
348 else
349 {
350 //handle->fxns->log(handle,handle->logObj,"AFP6: AFP6 initialized");
351 Log_info0("TaskAfp: Initialized");
353 // Process alpha commands
354 handle->fxns->process(handle);
355 }
356 }
357 }
359 /* Terminate AFP */
360 handle->fxns->acpExit();
361 Log_info0("TaskAfp: Exit");
362 Task_setPri(TaskAfp, -1);
363 Task_yield();
364 #else // SIMULATE_RX_ALPHA
365 //System_printf("Enter taskAfpFxn()\n");
366 Log_info0("Enter taskAfpFxn()");
368 for (;;)
369 {
370 /* Compute next time out for one-shot timer */
371 randVal = (Uint32)rand();
372 timeout = (randVal - (randVal / RX_ALPHA_DELTA_TICKS)*RX_ALPHA_DELTA_TICKS) + RX_ALPHA_MIN_TICKS;
373 Clock_setTimeout(clockRxAlpha, timeout);
374 Clock_start(clockRxAlpha);
376 //System_printf("Start Rx alpha clock, timeout=%lu\n",(ULong)timeout);
377 Log_info1("Start Rx alpha clock, timeout=%u", timeout);
379 /* Post semaphore indicating Rx alpha command */
380 Semaphore_pend(semaphoreRxAlpha, BIOS_WAIT_FOREVER);
382 gTaskAfpCnt++;
383 }
385 //System_printf("Exit taskAfpFxn()\n");
386 Log_info0("Exit taskAfpFxn()");
387 #endif // SIMULATE_RX_ALPHA
388 }
390 const AFP_Fxns AFP_DEFAULT_FXNS = {
391 // AFP_log,
392 AFP_memAlloc,
393 AFP_process,
394 ACP_MDS_init,
395 ACP_MDS_exit
396 };
398 AFP_Obj AFP_OBJ = {
399 sizeof(AFP_Obj),
400 &AFP_DEFAULT_FXNS,
401 &AFP_PARAMS,
402 NULL,
403 &DCS7_FXNS,
404 NULL,
405 NULL,
406 (ACP_Params*)&ACP_PARAMS,
407 NULL,
408 NULL,
409 (Int *)&gPafHeapIdExt,
410 (Int *)&gPafHeapIdExt,
411 // NULL,
412 };