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