c646e3462b71bce9ed596d2e24982ec4859b2d16
[processor-sdk/performance-audio-sr.git] / processor_audio_sdk_1_00_00_00 / pasdk / test_dsp / framework / alphaFuncProc.c
1 /*
2 * ======== AudioFuncProc.c ========
3 */
5 #include <xdc/std.h>
6 #include <xdc/cfg/global.h>
7 #include <xdc/runtime/Log.h>
8 #include <xdc/runtime/System.h>
9 #include <ti/sysbios/BIOS.h>
10 #include <ti/sysbios/knl/Clock.h>
11 #include <ti/sysbios/knl/Semaphore.h>
12 #include <stdlib.h>
13 #include "fwkSim.h"
15 #include "paf_heapMgr.h"
16 #include "alphaFuncProc.h"
18 #define ENABLE_AE_TRACE
19 #ifdef ENABLE_AE_TRACE
20 //#define AE_TRACE0(a) LOG_printf a
21 #define AE_TRACE0(a) Log_info0(a)
22 #define AE_TRACE1(a,b) Log_info1(a,b)
23 #define AE_TRACE2(a,b,c) Log_info2(a,b,c)
24 #define AE_TRACE3(a,b,c,d) Log_info3(a,b,c,d)
25 #define AE_TRACE4(a,b,c,d,e) Log_info4(a,b,c,d,e)
26 #else
27 #define AE_TRACE0(a)
28 #define AE_TRACE1(a,b)
29 #define AE_TRACE2(a,b,c)
30 #define AE_TRACE3(a,b,c,d)
31 #define AE_TRACE4(a,b,c,d,e)
32 #endif
34 // Global debug counters */
35 Uint32 gTaskAfpCnt=0;
37 AFP_Obj AFP_OBJ;
39 Void *AFP_memAlloc(
40 AFP_Handle handle,
41 Int32 *pHeapId,
42 UInt32 size,
43 UInt32 align
44 )
45 {
46 IHeap_Handle hHeap;
48 hHeap = pafHeapMgr_readHeapHandle(*pHeapId);
49 return (void *)Memory_alloc((IHeap_Handle)hHeap, size, align, NULL);
50 }
52 Void AFP_process(
53 AFP_Handle handle
54 )
55 {
56 Int rxIdx, txIdx;
57 Int encap, len, res_len, error, cnt, txStrip, i, rxOffset, txOffset;
59 /* Process Alpha commands */
60 for(;;)
61 {
62 /* Set read and write buffer indices */
63 rxIdx = 2;
64 txIdx = 3;
65 txOffset = 0;
66 encap = 0;
68 /* Read the payload */
69 if (handle->dcs7->fxns->read(handle->dcs7,
70 handle->rxBuf+rxIdx,
71 handle->params->rxBufSize-(rxIdx<<1)))
72 {
73 //handle->fxns->log(handle,handle->logObj,
74 // "AFP6: Error in reading message");
76 /* Write the response */
77 handle->txBuf[txIdx++] = 0xdead;
78 AE_TRACE0("AFP_process: 0xDEAD: Error in reading alpha message.");
79 handle->txBuf[3-txOffset-1] = txIdx-3+txOffset;
80 handle->dcs7->fxns->write(handle->dcs7,
81 &handle->txBuf[3-txOffset-1],
82 handle->params->txBufSize-(3-txOffset-1<<1));
84 continue;
85 }
87 /* Get CPM count */
88 cnt = handle->rxBuf[rxIdx++];
89 /* Process the C-record */
90 while (cnt)
91 {
92 /* Get the length of alpha code */
93 len = handle->acp->fxns->length(handle->acp, &handle->rxBuf[rxIdx]);
94 /* Get the response length */
95 res_len = handle->acp->fxns->htgnel(handle->acp, &handle->rxBuf[rxIdx]);
96 if (res_len > (handle->params->txBufSize-2>>1)-txIdx)
97 {
98 handle->txBuf[txIdx++] = 0xdead;
99 //handle->fxns->log(handle,handle->logObj,
100 // "AFP6: Response too big");
101 AE_TRACE0("AFP_process: 0xDEAD: Response too big.");
102 break;
103 }
105 /* Add encapsulation to alpha command -- ACP limitation */
106 if (((handle->rxBuf[rxIdx] & 0xff00) == 0xc900) ||
107 (handle->rxBuf[rxIdx] == 0xcd01))
108 {
109 Int lene, nele;
110 Int lenp, nelp;
111 Int k;
112 if ((handle->rxBuf[rxIdx]&0xff00) == 0xc900)
113 {
114 k=1;
115 }
116 else
117 {
118 k=2;
119 }
120 lene = len-k;
121 nele = 0;
122 while (lene)
123 {
124 lenp = handle->acp->fxns->length(handle->acp, &handle->rxBuf[rxIdx+k]);
125 nelp = handle->acp->fxns->htgnel(handle->acp, &handle->rxBuf[rxIdx+k]);
126 k += lenp;
127 lene -= lenp;
128 nele += nelp;
129 }
130 if (nele < 256)
131 {
132 res_len = nele+1;
133 }
134 else
135 {
136 res_len = nele+2;
137 }
138 rxOffset = 0;
139 if (res_len > (handle->params->txBufSize-4>>1)-txIdx)
140 {
141 handle->txBuf[txIdx++] = 0xc901;
142 handle->txBuf[txIdx++] = 0xdead;
143 AE_TRACE0("AFP_process: 0xDEAD: Response too big.");
144 //handle->fxns->log(handle,handle->logObj,
145 // "AFP6: Response too big");
146 break;
147 }
148 }
149 else if (len < 256)
150 {
151 handle->rxBuf[--rxIdx] = 0xc900+len;
152 rxOffset = 1;
153 }
154 else
155 {
156 handle->rxBuf[--rxIdx] = len;
157 handle->rxBuf[--rxIdx] = 0xcd01;
158 rxOffset = 2;
159 }
161 /* Process alpha command */
162 error = handle->acp->fxns->sequence(handle->acp,
163 &handle->rxBuf[rxIdx],
164 &handle->txBuf[txIdx]);
165 if (error)
166 {
167 //handle->fxns->log(handle,handle->logObj,
168 // "AFP6: Sequence error");
169 AE_TRACE0("AFP_process: 0xDEAD: Sequence error.");
170 if (res_len || encap)
171 {
172 handle->txBuf[txIdx++] = 0xdead;
173 }
174 cnt -= len;
175 rxIdx += len+rxOffset;
176 continue;
177 }
178 else if (rxOffset)
179 {
180 /* Delete encapsulation -- ACP limitation */
181 if ((handle->txBuf[txIdx] & 0xff00) == 0xc900)
182 {
183 txStrip = 1;
184 }
185 else if (handle->txBuf[txIdx] == 0xcd01)
186 {
187 txStrip=2;
188 }
189 for (i=0; i<res_len; ++i)
190 {
191 handle->txBuf[txIdx+i] = handle->txBuf[txIdx+i+txStrip];
192 }
193 }
195 /* Update the count, rxIdx and txIdx */
196 cnt -= len;
197 rxIdx += len+rxOffset;
198 txIdx += res_len;
199 }
201 /* Write the response */
202 handle->txBuf[3-txOffset-1] = txIdx-3+txOffset;
203 handle->dcs7->fxns->write(handle->dcs7,
204 &handle->txBuf[3-txOffset-1],
205 handle->params->txBufSize-(3-txOffset-1<<1));
206 }
207 }
209 /*
210 * ======== taskAfpFxn ========
211 * Audio Function Processing task function
212 */
213 Void taskAfpFxn(AFP_Handle handle)
214 {
215 #ifndef SIMULATE_RX_ALPHA
216 IALG_Status *pStatus;
217 IHeap_Handle hHeap;
218 void *memSeg;
219 #else // SIMULATE_RX_ALPHA
220 Uint32 randVal;
221 Uint32 timeout;
222 #endif // SIMULATE_RX_ALPHA
224 #ifndef SIMULATE_RX_ALPHA
225 Log_info0("TaskAfp: Enter");
227 if (handle == NULL)
228 {
229 handle = &AFP_OBJ;
230 }
232 /* Create an instance of ACP Algorithm */
233 handle->fxns->acpInit();
234 handle->acp = ACP_create(&ACP_MDS_IACP,handle->acpParams);
235 if (handle->acp == NULL)
236 {
237 //handle->fxns->log(handle,handle->logObj,
238 // "AFP6: ACP instance creation failed");
239 Log_info0("TaskAfp: ACP instance creation failed");
240 }
241 else
242 {
243 //extern struct {
244 // int size;
245 //} pafIdentification;
247 // Get pointer to ACP Status structure
248 ((ALG_Handle)handle->acp)->fxns->algControl((IALG_Handle)(handle->acp),
249 ACP_GETSTATUSADDRESS1,
250 (IALG_Status *)&pStatus);
251 // FL: (*** ?) Why STD BETA UART? Why not STD_BETA_ACP?
252 // Attach ACP Status structure to Beta Unit Id==STD_BETA_UART
253 handle->acp->fxns->attach(handle->acp,
254 ACP_SERIES_STD,
255 STD_BETA_UART,
256 pStatus);
258 // Attach ACP Status structure to Beta Unit Id==STD_BETA_UART
259 handle->acp->fxns->attach(handle->acp,
260 ACP_SERIES_STD,
261 STD_BETA_BETATABLE,
262 (IALG_Status *)((ACP_MDS_Obj *)handle->acp)->config.betaTable[ACP_SERIES_STD]);
263 // Attach STD Beta Table to Beta Unit Id==STD_BETA_BETATABLE
264 handle->acp->fxns->attach(handle->acp,
265 ACP_SERIES_STD,
266 STD_BETA_PHITABLE,
267 (IALG_Status *)((ACP_MDS_Obj *)handle->acp)->config.phiTable[ACP_SERIES_STD]);
268 // Attach STD Sigma Table to Beta Unit Id==STD_BETA_SIGMATABLE
269 handle->acp->fxns->attach(handle->acp,
270 ACP_SERIES_STD,
271 STD_BETA_SIGMATABLE,
272 (IALG_Status *)((ACP_MDS_Obj *)handle->acp)->config.sigmaTable[ACP_SERIES_STD]);
274 // FL: (*** ?) STD_BETA_IDENTITY already attached in main()->systemStreamMain(), so remove this
275 // Attach PAF Identification structure to Beta Unit Id==STD_BETA_IDENTITY
276 //handle->acp->fxns->attach(handle->acp, ACP_SERIES_STD, STD_BETA_IDENTITY,
277 // (IALG_Status *)&pafIdentification);
279 //handle->fxns->log(handle,handle->logObj,"AFP6: ACP initialized");
280 Log_info0("TaskAfp: ACP initialized");
282 // Initialize DCS
283 handle->dcs7 = handle->dcs7Fxns->open(handle->params->dcs7Params,
284 handle->dcs7Config,
285 (DCS7_Fxns_Ptr)handle->dcs7Fxns,
286 handle->acp);
287 if (handle == NULL)
288 {
289 //handle->fxns->log(handle,handle->logObj,
290 // "AFP6: DCS6 creation failed");
291 Log_info0("TaskAfp: DCS7 creation failed");
292 }
293 else
294 {
295 Log_info0("TaskAfp: DCS7 initialized");
297 hHeap = pafHeapMgr_readHeapHandle(*handle->pBufHeapId);
298 memSeg = (void *)&hHeap;
299 handle->rxBuf = (Uint16 *)handle->dcs7Fxns->memAlloc(handle->dcs7,
300 memSeg,
301 handle->params->rxBufSize,
302 4);
303 handle->txBuf = (Uint16 *)handle->dcs7Fxns->memAlloc(handle->dcs7,
304 memSeg,
305 handle->params->txBufSize,
306 4);
307 if ((handle->rxBuf == NULL) || (handle->txBuf == NULL))
308 {
309 //handle->fxns->log(handle,handle->logObj,
310 // "AFP6: Buffer allocation failed");
311 Log_info0("TaskAfp: Buffer allocation failed");
312 }
313 else
314 {
315 //handle->fxns->log(handle,handle->logObj,"AFP6: AFP6 initialized");
316 Log_info0("TaskAfp: Initialized");
318 // Process alpha commands
319 handle->fxns->process(handle);
320 }
321 }
322 }
324 /* Terminate AFP */
325 handle->fxns->acpExit();
326 Log_info0("TaskAfp: Exit");
327 Task_setPri(TaskAfp, -1);
328 Task_yield();
329 #else // SIMULATE_RX_ALPHA
330 //System_printf("Enter taskAfpFxn()\n");
331 Log_info0("Enter taskAfpFxn()");
333 for (;;)
334 {
335 /* Compute next time out for one-shot timer */
336 randVal = (Uint32)rand();
337 timeout = (randVal - (randVal / RX_ALPHA_DELTA_TICKS)*RX_ALPHA_DELTA_TICKS) + RX_ALPHA_MIN_TICKS;
338 Clock_setTimeout(clockRxAlpha, timeout);
339 Clock_start(clockRxAlpha);
341 //System_printf("Start Rx alpha clock, timeout=%lu\n",(ULong)timeout);
342 Log_info1("Start Rx alpha clock, timeout=%u", timeout);
344 /* Post semaphore indicating Rx alpha command */
345 Semaphore_pend(semaphoreRxAlpha, BIOS_WAIT_FOREVER);
347 gTaskAfpCnt++;
348 }
350 //System_printf("Exit taskAfpFxn()\n");
351 Log_info0("Exit taskAfpFxn()");
352 #endif // SIMULATE_RX_ALPHA
353 }
355 const AFP_Fxns AFP_DEFAULT_FXNS = {
356 // AFP_log,
357 AFP_memAlloc,
358 AFP_process,
359 ACP_MDS_init,
360 ACP_MDS_exit
361 };
363 AFP_Obj AFP_OBJ = {
364 sizeof(AFP_Obj),
365 &AFP_DEFAULT_FXNS,
366 &AFP_PARAMS,
367 NULL,
368 &DCS7_FXNS,
369 NULL,
370 NULL,
371 (ACP_Params*)&ACP_PARAMS,
372 NULL,
373 NULL,
374 (Int *)&gPafHeapIdExt,
375 (Int *)&gPafHeapIdExt,
376 // NULL,
377 };