3c59875d34d7376c074c07cee4a929ce0a4c6487
[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 #ifndef SIMULATE_RX_ALPHA
261     IALG_Status *pStatus;
262     IHeap_Handle hHeap;
263     void *memSeg;
264 #else // SIMULATE_RX_ALPHA
265     Uint32 randVal;
266     Uint32 timeout;
267 #endif // SIMULATE_RX_ALPHA
268     
269 #ifndef SIMULATE_RX_ALPHA
270     Log_info0("TaskAfp: Enter");
272     if (handle == NULL)
273     {
274         handle = &AFP_OBJ;
275     }
277     /* Create an instance of ACP Algorithm */
278     handle->fxns->acpInit();
279     handle->acp = ACP_create(&ACP_MDS_IACP,handle->acpParams);
280     if (handle->acp == NULL)
281     {
282         //handle->fxns->log(handle,handle->logObj,
283         //    "AFP6: ACP instance creation failed");
284         Log_info0("TaskAfp: ACP instance creation failed");
285     }
286     else
287     {
288         //extern struct {
289         //   int size;
290         //} pafIdentification;
291         
292         // Get pointer to ACP Status structure
293         ((ALG_Handle)handle->acp)->fxns->algControl((IALG_Handle)(handle->acp),
294             ACP_GETSTATUSADDRESS1, 
295             (IALG_Status *)&pStatus);
296         // FL: (*** ?) Why STD BETA UART? Why not STD_BETA_ACP?
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_UART, 
301             pStatus);
303         // Attach ACP Status structure to Beta Unit Id==STD_BETA_UART
304         handle->acp->fxns->attach(handle->acp, 
305             ACP_SERIES_STD, 
306             STD_BETA_BETATABLE, 
307             (IALG_Status *)((ACP_MDS_Obj *)handle->acp)->config.betaTable[ACP_SERIES_STD]);
308         // Attach STD Beta Table to Beta Unit Id==STD_BETA_BETATABLE
309         handle->acp->fxns->attach(handle->acp, 
310             ACP_SERIES_STD, 
311             STD_BETA_PHITABLE, 
312             (IALG_Status *)((ACP_MDS_Obj *)handle->acp)->config.phiTable[ACP_SERIES_STD]);
313         // Attach STD Sigma Table to Beta Unit Id==STD_BETA_SIGMATABLE
314         handle->acp->fxns->attach(handle->acp, 
315             ACP_SERIES_STD, 
316             STD_BETA_SIGMATABLE, 
317             (IALG_Status *)((ACP_MDS_Obj *)handle->acp)->config.sigmaTable[ACP_SERIES_STD]);
318             
319         // FL: (*** ?) STD_BETA_IDENTITY already attached in main()->systemStreamMain(), so remove this
320         // Attach PAF Identification structure to Beta Unit Id==STD_BETA_IDENTITY
321         //handle->acp->fxns->attach(handle->acp, ACP_SERIES_STD, STD_BETA_IDENTITY, 
322         //    (IALG_Status *)&pafIdentification);
323         
324         //handle->fxns->log(handle,handle->logObj,"AFP6: ACP initialized");
325         Log_info0("TaskAfp: ACP initialized");
326         
327         // Initialize DCS
328         handle->dcs7 = handle->dcs7Fxns->open(handle->params->dcs7Params,
329             handle->dcs7Config, 
330             (DCS7_Fxns_Ptr)handle->dcs7Fxns,
331             handle->acp);
332         if (handle == NULL)
333         {
334             //handle->fxns->log(handle,handle->logObj,
335             //    "AFP6: DCS6 creation failed");
336             Log_info0("TaskAfp: DCS7 creation failed");
337         }
338         else
339         {
340             Log_info0("TaskAfp: DCS7 initialized");
341             
342             hHeap = pafHeapMgr_readHeapHandle(*handle->pBufHeapId);
343             memSeg = (void *)&hHeap;
344             handle->rxBuf = (Uint16 *)handle->dcs7Fxns->memAlloc(handle->dcs7,
345                 memSeg,
346                 handle->params->rxBufSize,
347                 4);
348             handle->txBuf = (Uint16 *)handle->dcs7Fxns->memAlloc(handle->dcs7,
349                 memSeg,
350                 handle->params->txBufSize,
351                 4);
352             if ((handle->rxBuf == NULL) || (handle->txBuf == NULL))
353             {
354                 //handle->fxns->log(handle,handle->logObj,
355                 //    "AFP6: Buffer allocation failed");
356                 Log_info0("TaskAfp: Buffer allocation failed");
357             }
358             else
359             {
360                 //handle->fxns->log(handle,handle->logObj,"AFP6: AFP6 initialized");
361                 Log_info0("TaskAfp: Initialized");
363 #ifdef MEMSTAT_REPORT
364                 // Output current heap memory usage
365                 PAF_ALG_memStatusPrint("AFP MEMSTAT REPORT", 
366                     PAF_HEAP_INT, PAF_HEAP_INT1, PAF_HEAP_EXT, 
367                         PAF_HEAP_INT1_SHM, PAF_HEAP_EXT_SHM, PAF_HEAP_EXT_NONCACHED_SHM);
368 #endif /* MEMSTAT_REPORT */
369                 //gRunIdleMemStatRpt=1; // FL: debug
370                
371                 // Process alpha commands
372                 handle->fxns->process(handle);
373             }
374         }
375     }
377     /* Terminate AFP */
378     handle->fxns->acpExit();
379     Log_info0("TaskAfp: Exit");
380     Task_setPri(TaskAfp, -1);
381     Task_yield();
382 #else // SIMULATE_RX_ALPHA
383     //System_printf("Enter taskAfpFxn()\n");
384     Log_info0("Enter taskAfpFxn()");
386     for (;;)
387     {
388         /* Compute next time out for one-shot timer */
389         randVal = (Uint32)rand();
390         timeout = (randVal - (randVal / RX_ALPHA_DELTA_TICKS)*RX_ALPHA_DELTA_TICKS) + RX_ALPHA_MIN_TICKS;
391         Clock_setTimeout(clockRxAlpha, timeout);
392         Clock_start(clockRxAlpha);
394         //System_printf("Start Rx alpha clock, timeout=%lu\n",(ULong)timeout);
395         Log_info1("Start Rx alpha clock, timeout=%u", timeout);
396         
397         /* Post semaphore indicating Rx alpha command */
398         Semaphore_pend(semaphoreRxAlpha, BIOS_WAIT_FOREVER); 
400         gTaskAfpCnt++;
401     }
402     
403     //System_printf("Exit taskAfpFxn()\n");
404     Log_info0("Exit taskAfpFxn()");
405 #endif // SIMULATE_RX_ALPHA
408 const AFP_Fxns AFP_DEFAULT_FXNS = {
409 //    AFP_log,
410     AFP_memAlloc,
411     AFP_process,
412     ACP_MDS_init,
413     ACP_MDS_exit
414 };
416 AFP_Obj AFP_OBJ = {
417     sizeof(AFP_Obj),
418     &AFP_DEFAULT_FXNS,
419     &AFP_PARAMS,
420     NULL,
421     &DCS7_FXNS,
422     NULL,
423     NULL,
424     (ACP_Params*)&ACP_PARAMS,
425     NULL,
426     NULL,
427     (Int *)&gPafHeapIdExt,
428     (Int *)&gPafHeapIdExt,
429 //    NULL,
430 };