PASDK-376:Update build configurations
[processor-sdk/performance-audio-sr.git] / pasdk / test_dsp / framework / alphaFuncProc.c
2 /*
3 Copyright (c) 2017, 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>
49 #include "paf_heapMgr.h"
50 #include "alphaFuncProc.h"
52 //#define MEMSTAT_REPORT
53 #ifdef MEMSTAT_REPORT
54 #include "paf_alg_print.h"
55 #endif /* MEMSTAT_REPORT */
57 //#include "dbgIdle.h" // FL: debug -- initiate IDLE memstat
59 #define ENABLE_AE_TRACE
60 #ifdef ENABLE_AE_TRACE
61     //#define AE_TRACE0(a) LOG_printf a
62     #define AE_TRACE0(a)            Log_info0(a)
63     #define AE_TRACE1(a,b)          Log_info1(a,b)
64     #define AE_TRACE2(a,b,c)        Log_info2(a,b,c)
65     #define AE_TRACE3(a,b,c,d)      Log_info3(a,b,c,d)
66     #define AE_TRACE4(a,b,c,d,e)    Log_info4(a,b,c,d,e)    
67 #else
68     #define AE_TRACE0(a)
69     #define AE_TRACE1(a,b)
70     #define AE_TRACE2(a,b,c)
71     #define AE_TRACE3(a,b,c,d)
72     #define AE_TRACE4(a,b,c,d,e)
73 #endif
76 // Global debug counters */
77 Uint32 gTaskAfpCnt=0;
79 AFP_Obj AFP_OBJ;
81 Void *AFP_memAlloc(
82     AFP_Handle handle,
83     Int32 *pHeapId,
84     UInt32 size,
85     UInt32 align
86 )
87 {
88     IHeap_Handle hHeap;
89     
90     hHeap = pafHeapMgr_readHeapHandle(*pHeapId);
91     return (void *)Memory_alloc((IHeap_Handle)hHeap, size, align, NULL);
92 }
94 Void AFP_process(
95     AFP_Handle handle
96 )
97 {
98     Int rxIdx, txIdx;
99     Int encap, len, res_len, error, cnt, txStrip, i, rxOffset, txOffset;
101     /* Process Alpha commands */
102     for(;;)
103     {
104         /* Set read and write buffer indices */
105         rxIdx = 2;
106         txIdx = 3;
107         txOffset = 0;
108         encap = 0;
110         /* Read the payload */
111         if (handle->dcs7->fxns->read(handle->dcs7, 
112             handle->rxBuf+rxIdx,
113             handle->params->rxBufSize-(rxIdx<<1)))
114         {
115             //handle->fxns->log(handle,handle->logObj,
116             //    "AFP6: Error in reading message");
117             
118             /* Write the response */
119             handle->txBuf[txIdx++] = 0xdead;
120             AE_TRACE0("AFP_process:  0xDEAD: Error in reading alpha message.");
121             handle->txBuf[3-txOffset-1] = txIdx-3+txOffset;
122             handle->dcs7->fxns->write(handle->dcs7,
123                 &handle->txBuf[3-txOffset-1],
124                 handle->params->txBufSize-(3-txOffset-1<<1));
125                 
126             continue;
127         }
128         
129         /* Get CPM count */
130         cnt = handle->rxBuf[rxIdx++];
131         /* Process the C-record */
132         while (cnt)
133         {
134             /* Get the length of alpha code */
135             len = handle->acp->fxns->length(handle->acp, &handle->rxBuf[rxIdx]);
136             /* Get the response length */
137             res_len = handle->acp->fxns->htgnel(handle->acp, &handle->rxBuf[rxIdx]);
138             if (res_len > (handle->params->txBufSize-2>>1)-txIdx)
139             {
140                 handle->txBuf[txIdx++] = 0xdead;
141                 //handle->fxns->log(handle,handle->logObj,
142                 //    "AFP6: Response too big");
143                 AE_TRACE0("AFP_process:  0xDEAD: Response too big.");
144                 break;
145             }
147             /* Add encapsulation to alpha command -- ACP limitation */
148             if (((handle->rxBuf[rxIdx] & 0xff00) == 0xc900) || 
149                 (handle->rxBuf[rxIdx] == 0xcd01))
150             {
151                 Int lene, nele;
152                 Int lenp, nelp;
153                 Int k;
154                 if ((handle->rxBuf[rxIdx]&0xff00) == 0xc900)
155                 {
156                     k=1;
157                 }
158                 else
159                 {
160                     k=2;
161                 }
162                 lene = len-k;
163                 nele = 0;
164                 while (lene)
165                 {
166                     lenp = handle->acp->fxns->length(handle->acp, &handle->rxBuf[rxIdx+k]);
167                     nelp = handle->acp->fxns->htgnel(handle->acp, &handle->rxBuf[rxIdx+k]);
168                     k += lenp;
169                     lene -= lenp;
170                     nele += nelp;
171                 }
172                 if (nele < 256)
173                 {
174                     res_len = nele+1;
175                 }
176                 else
177                 {
178                     res_len = nele+2;
179                 }
180                 rxOffset = 0;
181                 if (res_len > (handle->params->txBufSize-4>>1)-txIdx)
182                 {
183                     handle->txBuf[txIdx++] = 0xc901;
184                     handle->txBuf[txIdx++] = 0xdead;
185                     AE_TRACE0("AFP_process:  0xDEAD: Response too big.");
186                     //handle->fxns->log(handle,handle->logObj,
187                     //    "AFP6: Response too big");
188                     break;
189                 }
190             }
191             else if (len < 256)
192             {
193                 handle->rxBuf[--rxIdx] = 0xc900+len;
194                 rxOffset = 1;
195             }
196             else
197             {
198                 handle->rxBuf[--rxIdx] = len;
199                 handle->rxBuf[--rxIdx] = 0xcd01;
200                 rxOffset = 2;
201             }
203             
204             /* Process alpha command */
205             error = handle->acp->fxns->sequence(handle->acp,
206                 &handle->rxBuf[rxIdx],
207                 &handle->txBuf[txIdx]);
209             
210             if (error)
211             {
212                 //handle->fxns->log(handle,handle->logObj,
213                 //    "AFP6: Sequence error");
214                 AE_TRACE0("AFP_process:  0xDEAD: Sequence error.");
215                 if (res_len || encap)
216                 {
217                     handle->txBuf[txIdx++] = 0xdead;
218                 }
219                 cnt -= len;
220                 rxIdx += len+rxOffset;
221                 continue;
222             }
223             else if (rxOffset)
224             {
225                 /* Delete encapsulation -- ACP limitation */
226                 if ((handle->txBuf[txIdx] & 0xff00) == 0xc900)
227                 {
228                     txStrip = 1;
229                 }
230                 else if (handle->txBuf[txIdx] == 0xcd01)
231                 {
232                     txStrip=2;
233                 }
234                 for (i=0; i<res_len; ++i)
235                 {
236                     handle->txBuf[txIdx+i] = handle->txBuf[txIdx+i+txStrip];
237                 }
238             }
239             
240             /* Update the count, rxIdx and txIdx */
241             cnt -= len;
242             rxIdx += len+rxOffset;
243             txIdx += res_len;
244         }
245         
246         /* Write the response */
247         handle->txBuf[3-txOffset-1] = txIdx-3+txOffset;
248         handle->dcs7->fxns->write(handle->dcs7,
249             &handle->txBuf[3-txOffset-1],
250             handle->params->txBufSize-(3-txOffset-1<<1));
251     }
254 /*
255  *  ======== taskAfpFxn ========
256  *  Audio Function Processing task function
257  */
258 Void taskAfpFxn(AFP_Handle handle)
260     IALG_Status *pStatus;
261     IHeap_Handle hHeap;
262     void *memSeg;
263     
264     Log_info0("TaskAfp: Enter");
266     if (handle == NULL)
267     {
268         handle = &AFP_OBJ;
269     }
271     /* Create an instance of ACP Algorithm */
272     handle->fxns->acpInit();
273     handle->acp = ACP_create(&ACP_MDS_IACP,handle->acpParams);
274     if (handle->acp == NULL)
275     {
276         //handle->fxns->log(handle,handle->logObj,
277         //    "AFP6: ACP instance creation failed");
278         Log_info0("TaskAfp: ACP instance creation failed");
279     }
280     else
281     {
282         //extern struct {
283         //   int size;
284         //} pafIdentification;
285         
286         // Get pointer to ACP Status structure
287         ((ALG_Handle)handle->acp)->fxns->algControl((IALG_Handle)(handle->acp),
288             ACP_GETSTATUSADDRESS1, 
289             (IALG_Status *)&pStatus);
290         // FL: (*** ?) Why STD BETA UART? Why not STD_BETA_ACP?
291         // Attach ACP Status structure to Beta Unit Id==STD_BETA_UART
292         handle->acp->fxns->attach(handle->acp, 
293             ACP_SERIES_STD, 
294             STD_BETA_UART, 
295             pStatus);
297         // Attach ACP Status structure to Beta Unit Id==STD_BETA_UART
298         handle->acp->fxns->attach(handle->acp, 
299             ACP_SERIES_STD, 
300             STD_BETA_BETATABLE, 
301             (IALG_Status *)((ACP_MDS_Obj *)handle->acp)->config.betaTable[ACP_SERIES_STD]);
302         // Attach STD Beta Table to Beta Unit Id==STD_BETA_BETATABLE
303         handle->acp->fxns->attach(handle->acp, 
304             ACP_SERIES_STD, 
305             STD_BETA_PHITABLE, 
306             (IALG_Status *)((ACP_MDS_Obj *)handle->acp)->config.phiTable[ACP_SERIES_STD]);
307         // Attach STD Sigma Table to Beta Unit Id==STD_BETA_SIGMATABLE
308         handle->acp->fxns->attach(handle->acp, 
309             ACP_SERIES_STD, 
310             STD_BETA_SIGMATABLE, 
311             (IALG_Status *)((ACP_MDS_Obj *)handle->acp)->config.sigmaTable[ACP_SERIES_STD]);
312             
313         // FL: (*** ?) STD_BETA_IDENTITY already attached in main()->systemStreamMain(), so remove this
314         // Attach PAF Identification structure to Beta Unit Id==STD_BETA_IDENTITY
315         //handle->acp->fxns->attach(handle->acp, ACP_SERIES_STD, STD_BETA_IDENTITY, 
316         //    (IALG_Status *)&pafIdentification);
317         
318         //handle->fxns->log(handle,handle->logObj,"AFP6: ACP initialized");
319         Log_info0("TaskAfp: ACP initialized");
320         
321         // Initialize DCS
322         handle->dcs7 = handle->dcs7Fxns->open(handle->params->dcs7Params,
323             handle->dcs7Config, 
324             (DCS7_Fxns_Ptr)handle->dcs7Fxns,
325             handle->acp);
326         if (handle == NULL)
327         {
328             //handle->fxns->log(handle,handle->logObj,
329             //    "AFP6: DCS6 creation failed");
330             Log_info0("TaskAfp: DCS7 creation failed");
331         }
332         else
333         {
334             Log_info0("TaskAfp: DCS7 initialized");
335             
336             hHeap = pafHeapMgr_readHeapHandle(*handle->pBufHeapId);
337             memSeg = (void *)&hHeap;
338             handle->rxBuf = (Uint16 *)handle->dcs7Fxns->memAlloc(handle->dcs7,
339                 memSeg,
340                 handle->params->rxBufSize,
341                 4);
342             handle->txBuf = (Uint16 *)handle->dcs7Fxns->memAlloc(handle->dcs7,
343                 memSeg,
344                 handle->params->txBufSize,
345                 4);
346             if ((handle->rxBuf == NULL) || (handle->txBuf == NULL))
347             {
348                 //handle->fxns->log(handle,handle->logObj,
349                 //    "AFP6: Buffer allocation failed");
350                 Log_info0("TaskAfp: Buffer allocation failed");
351             }
352             else
353             {
354                 //handle->fxns->log(handle,handle->logObj,"AFP6: AFP6 initialized");
355                 Log_info0("TaskAfp: Initialized");
357 #ifdef MEMSTAT_REPORT
358                 // Output current heap memory usage
359                 PAF_ALG_memStatusPrint("AFP MEMSTAT REPORT", 
360                     PAF_HEAP_INT, PAF_HEAP_INT1, PAF_HEAP_EXT, 
361                         PAF_HEAP_INT1_SHM, PAF_HEAP_EXT_SHM, PAF_HEAP_EXT_NONCACHED_SHM);
362 #endif /* MEMSTAT_REPORT */
363                 //gRunIdleMemStatRpt=1; // FL: debug
364                
365                 // Process alpha commands
366                 handle->fxns->process(handle);
367             }
368         }
369     }
371     /* Terminate AFP */
372     handle->fxns->acpExit();
373     Log_info0("TaskAfp: Exit");
374     Task_setPri(TaskAfp, -1);
375     Task_yield();
378 const AFP_Fxns AFP_DEFAULT_FXNS = {
379 //    AFP_log,
380     AFP_memAlloc,
381     AFP_process,
382     ACP_MDS_init,
383     ACP_MDS_exit
384 };
386 AFP_Obj AFP_OBJ = {
387     sizeof(AFP_Obj),
388     &AFP_DEFAULT_FXNS,
389     &AFP_PARAMS,
390     NULL,
391     &DCS7_FXNS,
392     NULL,
393     NULL,
394     (ACP_Params*)&ACP_PARAMS,
395     NULL,
396     NULL,
397     (Int *)&gPafHeapIdExt,
398     (Int *)&gPafHeapIdExt,
399 //    NULL,
400 };