]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/performance-audio-sr.git/blob - pasdk/test_dsp/framework/audioStreamInpProcNewIO.c
Fixed loss of SYNC problem and restart problem. ASIT FSM works fine with
[processor-sdk/performance-audio-sr.git] / pasdk / test_dsp / framework / audioStreamInpProcNewIO.c
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  *  ======== audioStreamInpProcNewIo.c ========
38  */
39 #include <xdc/runtime/Log.h>
40 #include <ti/sysbios/BIOS.h>
41 #include <xdc/runtime/Error.h>
42 #include <ti/sysbios/knl/Event.h>
44 #include "procsdk_audio_typ.h"
45 #include "audioStreamInpProc.h"
46 #include "audioStreamProc_common.h"
47 #include "aspMsg_common.h"
48 #include "aspMsg_master.h"
49 #include "asperr.h"
50 #include "common.h"
52 #include "audk2g.h"
53 #include "audk2g_audio.h"
54 #include "mcasp_cfg.h"
55 #include "ioConfig.h"    //TODO: remove this header
56 #include "ioBuff.h"
57 #include "ioPhy.h"
58 #include "ioData.h"
61 #define STRIDE_WORST_CASE 32  // 4-byte (32-bit) word, 2 slots, 4 serializers
63 //#define DEBUG_SKIP_DECODING
65 #define SYNC_PC_MASK         0x1F
66 #define SYNC_SUBTYPE_MASK    0x700
67 #define SYNC_SUBTYPE_SHIFT   8
68 #define SYNC_SUBTYPE_DTSHD   0x11
69 #define SYNC_DDP             0x15
70 #define SYNC_THD             0x16
72 #define IEC_HEADER_LENGTH    4
74 #define INPUT_SWITCH_HANGOVER 8
76 //table needed until PAF_SOURCE is reordered to match IEC numbering
77 const SmUns IECpafSource[23] =
78 {
79     PAF_SOURCE_UNKNOWN,  // 0: IEC NULL Type
80     PAF_SOURCE_AC3,      // 1: Comments on 1-15 match IEC 61937 part 2.
81     PAF_SOURCE_UNKNOWN,  // 2: IEC reserved
82     PAF_SOURCE_UNKNOWN,  // 3: IEC pause
83     PAF_SOURCE_UNKNOWN,  // 4: MPEG 1 layer 1
84     PAF_SOURCE_MP3,      // 5: MPEG layer 2 or 3
85     PAF_SOURCE_UNKNOWN,  // 6: MPEG 2 data with extension
86     PAF_SOURCE_AAC,      // 7: MPEG-2 AAC ADTS
87     PAF_SOURCE_UNKNOWN,  // 8: MPEG 2 layer 1 low sampling frequency
88     PAF_SOURCE_UNKNOWN,  // 9: MPEG 2 layer 2 or 3 low sampling frequency
89     PAF_SOURCE_UNKNOWN,  // 10: reserved
90     PAF_SOURCE_DTS,      // 11: DTS type 1 (11 bit: 512 sample repeat period)
91     PAF_SOURCE_DTS12,    // 12: DTS type 2 (12 bit: 1024 sample repeat period)
92     PAF_SOURCE_DTS13,    // 13: DTS type 3 (13 bit: 2048 sample repeat period)
93     PAF_SOURCE_DTS14,    // 14: ATRAC
94     PAF_SOURCE_UNKNOWN,  // 15: ATRAC 2/3
95     PAF_SOURCE_THD,      // 16
96     PAF_SOURCE_DTSHD,    // 17
97     PAF_SOURCE_WMA9PRO,  // 18
98     PAF_SOURCE_UNKNOWN,  // 19
99     PAF_SOURCE_UNKNOWN,  // 20
100     PAF_SOURCE_DDP,      // 21
101     PAF_SOURCE_THD,      // 22
102 };
104 /*
105  * Functions defined in other files and to be put into proper header files
106  */
107 extern Void taskAsipFxnInit(const PAF_ASIT_Params *pP,const PAF_ASIT_Patchs *pQ);
110 // avoid including sap_d10.h, which would cause symbol redefinition
111 // warning (MCASP_PFUNC_XXX)
112 extern XDAS_Int32 D10_init();
113 extern XDAS_Int32 D10_RxControl(const void *pD10RxParams,
114                                 XDAS_Int32 code, XDAS_Int32 arg);
116 /*
117  * Functions only used in this file
118  */
119 int asitPrepareProcessing();
120 int asitIoCompsInit(PAF_AST_InpBuf * pInpBuf, PAF_AST_IoInp * pInpIo);
121 void asitProcInit(PAF_AST_IoInp  *pInp, asipDecProc_t *pDec);
122 void asitIoPhyPrime(PAF_AST_IoInp *pInpIo);
123 void asitPhyTransferComplete(PAF_AST_IoInp * pInpIo);
124 void asitPhyTransferStart(PAF_AST_IoInp *pInpIo);
125 Int asitRecfgPhyXfer(PAF_AST_IoInp *pInp, size_t xferSize);
126 Int asitSelectDevices(const PAF_ASIT_Patchs *pQ, PAF_AST_IoInp *pInp);
127 Int asitUpdateInputStatus(const void *pRxParams, PAF_InpBufStatus *pStatus,
128                           PAF_InpBufConfig *pInpBuf);
130 Int asitSourceDetection(const PAF_ASIT_Params *pP,
131                         const PAF_ASIT_Patchs *pQ,
132                         PAF_ASIT_Config       *pAsitCfg);
134 Int asitDecideSource(PAF_AST_Config *pAstCfg, PAF_AST_IoInp  *pInp,
135                      ioDataAutoDetStat_t *autoDetStatus);
136 Int asitUpdateIoComps(const PAF_ASIT_Params *pP, PAF_AST_Config *pAstCfg,
137                       PAF_AST_IoInp  *pInp, ioDataAutoDetStat_t *autoDetStatus);
138 Int asitBypassIoData(PAF_AST_IoInp *pInp);
139 Int asitPcmTransition(PAF_ASIT_Config *pAsitCfg);
141 void asitUpdateInpBufConfig(PAF_AST_Config *pAstCfg, PAF_AST_IoInp  *pInp);
142 Int asitInputDataProcess(PAF_AST_IoInp *pInp, ioDataAutoDetStat_t *pAutoDetStats);
144 UInt asitEventsToDecMsg(UInt asitEvents);
145 void asitErrorHandling(PAF_ASIT_Config *pAsitCfg, Int asitErr);
146 Int asitEvtErrCheck(UInt actualEvents, UInt expectedEvents);
148 void asitPostInfoEvent();
149 void asitPostDecEvent();
151 /*
152  * variables/structures to be put into proper global structures
153  */
154 extern PAF_ASIT_Config gPAF_ASIT_config;
155 extern const MdUns iecFrameLength[23];
156 extern Ptr hMcaspRxChan;
158 enum {
159     ASIT_RESET,
160     ASIT_SOURCE_DETECTION,
161     ASIT_PCM_TRANSITION,
162     ASIT_DECODE_PROCESSING
163 };
165 #define ASIT_EVTMSK_NONE        0x0
166 #define ASIT_EVTMSK_INPDATA     0x1
167 #define ASIT_EVTMSK_INFOACK     0x2
168 #define ASIT_EVTMSK_DECACK      0x4
170 #define ASIT_ALL_EVENTS (  ASIT_EVTMSK_INPDATA \
171                          + ASIT_EVTMSK_INFOACK \
172                          + ASIT_EVTMSK_DECACK )
174 #define ASIT_DEC_EVENTS (  ASIT_EVTMSK_INPDATA \
175                          + ASIT_EVTMSK_INFOACK \
176                          + ASIT_EVTMSK_DECACK )
178 #define ASIP_DEBUG
180 #ifdef ASIP_DEBUG
181 /* define the following as global variables for easy debugging */
182 int asipLoopCount1, asipLoopCount2;
183 Int asipErrno;
184 Int inputReadyForProcessing;
185 #endif
187 Event_Handle asitEvent;
188 Int eventsOn;
190 /*
191  *  ======== taskAsipFxn ========
192  *  Audio Stream Input Processing task function
193  */
194 #ifndef PASDK_SIO_DEV
195 #ifndef IO_LOOPBACK_TEST
196 Void taskAsipFxn(                  // ASIP task function for new I/O
197 #else
198 Void taskAsipFxn_NewIO_Not_Used(   // not used for loopback test
199 #endif
200 #else
201 Void taskAsipFxn_NewIO_Not_USED(   // not used for SIO/DEV based I/O
202 #endif
203     const PAF_ASIT_Params *pP,
204         const PAF_ASIT_Patchs *pQ)
206     PAF_ASIT_Config *pAsitCfg;      /* ASIT configuration pointer */
207     PAF_AST_Config  *pAstCfg;       /* AST Common (shared) configuration pointer */
208     PAF_AST_IoInp *pInp;            /* Input I/O components */
209     Int as;                         /* Audio Stream Number (1, 2, etc.) */
210     Int z;                          /* input/encode/stream/decode/output counter */
211     Int zMI;
212 #ifndef ASIP_DEBUG
213 int asipLoopCount1, asipLoopCount2;
214 Int asipErrno;
215 Int inputReadyForProcessing;
216 #endif
217     Int asitErr;
218     UInt events;
219 //    Int eventsOn;
220     Error_Block  eb;
222     Log_info0("Enter taskAsipFxn()");
224     taskAsipFxnInit(pP, pQ);  // initialization of input task
225     
226     Error_init(&eb);          // initialize error block
228     asitEvent = Event_create(NULL, &eb);
229     if (asitEvent == NULL) {
230         System_abort("Event create failed");
231     }
232     //
233     // Audio Stream Input Task Configuration (*pAsitCfg):
234     //
235     pAsitCfg = &gPAF_ASIT_config;       // initialize pointer to task configuration
236     pAstCfg  = pAsitCfg->pAstCfg;       // pointer to AST common (shared) configuration
238     /* Set Audio Stream Number (1, 2, etc.) */
239     as = pAstCfg->as;
241     //
242     // Determine decoder and stream indices associated with the master input
243     //
244     zMI  = pP->zone.master;
245     pInp = &pAsitCfg->pIoInp[zMI];        // pointer to input I/O components
247     for (z=STREAM1; z < STREAMN; z++)
248     {
249         TRACE_VERBOSE1("TaskAsip: AS%d: running", as+z);
250     }
252     Log_info0("TaskAsip: Entering Main Loop.");
254     //
255     // Main processing loop
256     //
257     asipLoopCount1 = 0;
258     asipLoopCount2 = 0;
259     asitErr = ASIT_NO_ERR;
260     pInp->asipState = ASIT_RESET;
261     pInp->firstTimeInit = TRUE;
262     pInp->numAsitRestart = 0;
264     // The events_on flag will be removed if the RESTART state is changed to
265     // event-based scheduling instead of polling
266     eventsOn = FALSE;
268     for (;;)
269     {
270         asipLoopCount1++;
272         if(eventsOn) {
273             events = Event_pend(asitEvent, ASIT_EVTMSK_NONE, ASIT_ALL_EVENTS,
274                                 BIOS_WAIT_FOREVER);
275         }
276         switch (pInp->asipState)
277         {
278         case ASIT_RESET:    // No events pending in this state
279             // Indicate decoder no decoding yet
280             pP->fxns->sourceDecode(pP, pQ, pAsitCfg, PAF_SOURCE_NONE);
282             // 5 system tick, or 5 msec. Should remove this later when implementing
283             // event based scheduling.
284             Task_sleep(5);
286             inputReadyForProcessing = asitPrepareProcessing(pP, pQ, pAsitCfg, &asipErrno);
287             if (inputReadyForProcessing) {
288                 TRACE_VERBOSE0("TaskAsip: Input is ready. Initialize I/O components.");
289                 // Input is ready for processing, so we initialize the I/O components.
290                 // Note that the I/O components init. and I/O PHY prime are performed only
291                 // at the first time. This should be changed later - init. and prime
292                 // should be done whenever input interface has changed.
293                 asitErr = asitIoCompsInit(&pAstCfg->xInp[zMI], pInp);
295                 // Initialize ASIT processing
296                 asitProcInit(pInp, &pAsitCfg->inpDec);
297                 pInp->asipState = ASIT_SOURCE_DETECTION;
298                 eventsOn = TRUE;   // turn on events pending
299                 TRACE_VERBOSE0("TaskAsip: turn on events and go to source detection.");
300             }
301             break;
303         case ASIT_SOURCE_DETECTION:
304             // Source unknown - to detect & identify source
305             if(events == ASIT_EVTMSK_INPDATA) {  
306                 // Only this event is expected. If any other event, it's error.
307                 // Input data is ready - perform source detection.
308                 // New state will be decided inside the function
309                 asitErr = asitSourceDetection(pP, pQ, pAsitCfg);
310             }
311             else {
312                 //Error checking & debug
313                 asitErr = asitEvtErrCheck(events, ASIT_EVTMSK_INPDATA);
314             }
316             break;
318         case ASIT_PCM_TRANSITION:
319             // Source is PCM - transition to PCM decoding
320             if(events == ASIT_EVTMSK_INPDATA) {
321                 // Only this event is expected. If any other event, it's error.
322                 // Input data is ready - transition to PCM decoding
323                 // New state will be decided inside the function
324                 asitErr = asitPcmTransition(pAsitCfg);
325             }
326             else {
327                 //Error checking & debug
328                 asitErr = asitEvtErrCheck(events, ASIT_EVTMSK_INPDATA);
329             }
330             break;
332         case ASIT_DECODE_PROCESSING:
333             if(events & ASIT_DEC_EVENTS) {
334                 // Decode processing for either PCM or bitstream
335                 // New state will be decided inside the function
336                 asitErr = asitDecodeProcessing(pP, pQ, pAsitCfg, events);
337             }
338             else {
339                 //Error checking & debug
340                 asitErr = asitEvtErrCheck(events, ASIT_DEC_EVENTS);
341             }
342             break;
344         default:
345             break;
346         }
348         if(asitErr) {
349             asitErrorHandling(pAsitCfg, asitErr);
351             if(pInp->asipState == ASIT_RESET) {
352                 eventsOn = FALSE;
353             }
355             asitErr = ASIT_NO_ERR;
356         }
358     }  // for (;;)
360 }  /* taskAsipFxn */
363 /*===========================================================================
364  * ASIT Processing Preparation
365  * Output:
366  *        - return        TRUE (input is ready) or FALSE (input is not ready)
367  *        - *asipErrno    Error number
368 ============================================================================*/
369 Int asitPrepareProcessing(const PAF_ASIT_Params *pP,
370                           const PAF_ASIT_Patchs *pQ,
371                           PAF_ASIT_Config       *pC,
372                           Int                   *asipErrno)
374     Int as, zMS, zMI, zMD;
375     Int sourceConfig, mode;
376     PAF_AST_Config  *pAstCfg;
377     PAF_AST_IoInp *pInp;            /* I/O components for input */
379     pAstCfg  = pC->pAstCfg;         // pointer to AST common (shared) configuration
380     as  = pAstCfg->as;
381     zMI = pP->zone.master;
382     zMD = pAstCfg->masterDec;
383     zMS = pAstCfg->masterStr;
384     pInp = pC->pIoInp;              // pointer to input I/O components
386     *asipErrno = 0;
388     // Select source and initialize physical layer / HW interface
389     *asipErrno = asitSelectDevices(pQ, pInp);
390     if (*asipErrno) {
391         TRACE_TERSE2("asitSelectDevices returned asipErrno = 0x%04x at line %d. AS%d",
392                      *asipErrno, as+zMS);
393         return FALSE;    // Input is not ready for processing due to error
394     }
396     // If no master input selected then we don't know what may be at the input,
397     // so set to unknown and skip any remaining processing
398     if (!pInp[zMI].pRxParams) {
399         sharedMemWriteInt8(&(pAstCfg->xDec[zMD].decodeStatus.sourceProgram),
400                            (Int8)PAF_SOURCE_UNKNOWN, GATEMP_INDEX_DEC);
402         TRACE_VERBOSE1("TaskAsip: AS%d: No input selected...", as+zMS);
403         return FALSE;    // No error, but input is not ready for processing
404     }
406     // If here then we have a valid input so query its status
407     *asipErrno = asitUpdateInputStatus(pInp[zMI].pRxParams,
408                                    &pAstCfg->xInp[zMI].inpBufStatus,
409                                    &pAstCfg->xInp[zMI].inpBufConfig);
410     if(*asipErrno) {
411         TRACE_VERBOSE1("TaskAsip: updateInputStatus returns 0x%x", *asipErrno);
412         return FALSE;   // Input is not ready for processing due to error
413     }
415     // If master decoder is not enabled, or the input is unlocked, then do nothing
416     mode = (Int)sharedMemReadInt8(&(pAstCfg->xDec[zMD].decodeStatus.mode),
417                                   GATEMP_INDEX_DEC);
418     if (!mode || !pAstCfg->xInp[zMI].inpBufStatus.lock)
419     {
420         TRACE_VERBOSE0("TaskAsip: Not locked, continue");
421         return FALSE;  // No error, but input is not ready for processing
422     }
424     // Check selected source: sourceSelect is set by another task, AIP or AFP
425     sourceConfig = (Int)sharedMemReadInt8(&(pAstCfg->xDec[zMD].decodeStatus.sourceSelect),
426                                           GATEMP_INDEX_DEC);
427     // If no source selected then do nothing
428     if(sourceConfig == PAF_SOURCE_NONE) {
429         sharedMemWriteInt8(&(pAstCfg->xDec[zMD].decodeStatus.sourceProgram),
430                            (Int8)PAF_SOURCE_NONE, GATEMP_INDEX_DEC);
431         TRACE_VERBOSE1("TaskAsip: AS%d: no source selected, continue", as+zMS);
432         return FALSE;  // No error, but input is not ready for processing
433     }
435     // If we want pass processing then proceed directly
436     if (sourceConfig == PAF_SOURCE_PASS)
437     {
438         TRACE_VERBOSE1("TaskAsip: AS%d: Pass processing ...", as+zMS);
439         sharedMemWriteInt8(&(pAstCfg->xDec[zMD].decodeStatus.sourceProgram),
440                            (Int8)PAF_SOURCE_PASS, GATEMP_INDEX_DEC);
442         pP->fxns->sourceDecode(pP, pQ, pC, PAF_SOURCE_PASS);
443         if (pP->fxns->passProcessing) {
444             *asipErrno = pP->fxns->passProcessing(pP, pQ, pC, NULL);
445         }
446         else {
447             TRACE_TERSE2("TaskAsip: AS%d: Pass Processing not supported, asipErrno 0x%x",
448                          as+zMS, ASPERR_PASS);
449             *asipErrno = ASPERR_PASS;
450         }
452         TRACE_VERBOSE0("TaskAsip: continue");
453         return FALSE;  // Error or not, input is not ready for processing
454     }
456     // No error and input processing is ready
457     return TRUE;
458 } /* asitPrepareProcessing */
461 /*===========================================================================
462  * ASIT Source Detection:
463  *     ASIT processing for input source identification:
464  *     - mark I/O PHY transfer completion
465  *     - run auto-detection via I/O DATA in asitInputDataProcess
466  *     - mark I/O DATA read completion
467  *     - check auto-detection status and take corresponding actions if either
468  *       PCM or bitstream is identified:
469  *       - decide input source
470  *       - update I/O components
471  *       - reconfigure McASP LLD if it is PCM
472  *     - start next I/O PHY transfer
473 ============================================================================*/
474 Int asitSourceDetection(const PAF_ASIT_Params *pP,
475                         const PAF_ASIT_Patchs *pQ,
476                         PAF_ASIT_Config       *pAsitCfg)
478     PAF_AST_Config  *pAstCfg;
479     PAF_AST_IoInp *pInp;            /* I/O components for input */
480     Int zMD, mcaspErr, asitErr;
481     ioDataAutoDetStat_t autoDetStatus;
483     pAstCfg  = pAsitCfg->pAstCfg;         // pointer to AST common (shared) configuration
484     zMD = pAstCfg->masterDec;
485     pInp = &pAsitCfg->pIoInp[zMD];        // pointer to input I/O components
487     // Marks I/O PHY transfer and I/O BUFF write complete
488     asitPhyTransferComplete(pInp);
490     // Process input data - either searching SYNC for PCM or checking SYNC for bitstream
491     asitErr = asitInputDataProcess(pInp, &autoDetStatus);
492     if(asitErr != ASIT_NO_ERR) {
493         // Even though there is error, still need to start next transfer to
494         // maintain McASP transfer.
495         asitPhyTransferStart(pInp);
497         return asitErr;
498     }
500     // Mark input data read complete
501     ioDataReadComplete(pInp->hIoData);
503     // Check if bitstream or PCM is detected
504     if(   autoDetStatus.syncState == IODATA_SYNC_BITSTREAM
505        || autoDetStatus.syncState == IODATA_SYNC_PCM) {
506         // Decide input source and inform decoder
507         asitErr = asitDecideSource(pAstCfg, pInp, &autoDetStatus);
508         if(asitErr != ASIT_NO_ERR) {
509             return asitErr;
510         }
511         else {
512             // Update I/O components and input buffer config
513             asitUpdateIoComps(pP, pAstCfg, pInp, &autoDetStatus);
515             // set to unknown so that we can ensure, for IOS purposes, that
516             // sourceDecode = NONE iff we are in this top level state machine
517             // and specifically not in decodeProcessing
518 #ifndef DEBUG_SKIP_DECODING
519             pP->fxns->sourceDecode(pP, pQ, pAsitCfg, PAF_SOURCE_UNKNOWN);
520 #endif
522             if(autoDetStatus.syncState == IODATA_SYNC_BITSTREAM) {
523                 // Input is bit stream: go to decoding
524                 pInp->asipState = ASIT_DECODE_PROCESSING;
525             }
526             else  {
527                 // Input is PCM: stop swapping data
528                 pInp->swapData = FALSE;
530                 // Reconfigure McASP LLD to transfer 32-bit unpacked data
531                 mcaspErr = mcaspRecfgWordWidth(pInp->hMcaspChan, Mcasp_WordLength_32);
532                 if(mcaspErr != Audk2g_EOK) {
533                     return ASIT_ERR_MCASP_CFG;
534                 }
536                 // Adjust I/O BUFF delay and read pointer - to make sure read pointers
537                 // always point to PCM data from 1st I2S (out of 4 for HDMI 4xI2S)
538                 ioBuffAdjustDelay(pInp->hIoBuff, pInp->phyXferSize);
540                 // Go to transition state to switch to PCM
541                 pInp->asipState = ASIT_PCM_TRANSITION;
542             }
543         }
544     }
546     // Start next transfer
547     asitPhyTransferStart(pInp);
549     return (ASIT_NO_ERR);
550 }  /* asitSourceDetection */
553 /*===========================================================================
554     //
555     // which will cause all 0's in one McASP LLD transfer. This will
556     // be detected as loss of SYNC by auto detection. To prevent that,
557     // skip I/O DATA process for hangover period so that this all 0's
558     // frame will not be seen by auto-detection. Also, playing out PCM
559     // needs to be skipped as well, to prevent from playing out garbage
560     // (16-bit packed data).
561  *
562  * ASIT Transition to PCM decoding.
563  *     When PCM is detected, McASP LLD will be reconfigured to transmit 32-bit
564  *     words, which will modify the RFMT register. This will cause all 0's in
565  *     one McASP LLD transfer, which would be detected as loss of SYNC by auto
566  *     detection (performed by I/O DATA). To prevent that, skip I/O DATA process
567  *     for hangover period so that this all-0's frame will not be seen by the
568  *     auto-detection.
569  *
570  *     In addition, playing out PCM should be skipped as well to prevent from
571  *     playing out the 16-bit packed data that's still in the input buffer.
572  *
573  *     This function does the following:
574  *     - mark I/O PHY transfer completion
575  *     - bypass I/O DATA
576  *     - start next I/O PHY transfer
577 ============================================================================*/
578 Int asitPcmTransition(PAF_ASIT_Config *pAsitCfg)
580     Int asitErr;
581     PAF_AST_IoInp *pInp;                  // I/O components for input
582     Int zMD;
584     zMD = pAsitCfg->pAstCfg->masterDec;   // pointer to AST common (shared) configuration
585     pInp = &pAsitCfg->pIoInp[zMD];        // pointer to input I/O components
587     // Marks I/O PHY transfer and I/O BUFF write complete
588     asitPhyTransferComplete(pInp);
590     // Bypass I/O data processing due to McASP LLD work around
591     // (refer to comments inside the function)
592     asitErr = asitBypassIoData(pInp);
594     pInp->pcmSwitchHangOver--;
595     if(pInp->pcmSwitchHangOver == 0) {
596         pInp->asipState = ASIT_DECODE_PROCESSING;
597     }
598     else {
599         ; // stay in this state
600     }
602     // Start next transfer
603     asitPhyTransferStart(pInp);
605     return asitErr;
607 } /* asitPcmTransition */
609 /*============================================================================
610  * ASIT Bypass I/O DATA Processing
611  *     This function bypasses the I/O DATA processing. It maintains the read
612  *     operation of I/O BUFF by directly calling I/O BUFF APIs. When I/O DATA
613  *     is not bypassed, I/O BUFF read operation is invoked by I/O DATA.
614  *
615 ============================================================================*/
616 Int asitBypassIoData(PAF_AST_IoInp *pInp)
618     void *buff1, *buff2;
619     size_t size1, size2;
621     // Get read pointers (or sub-buffers) of the input buffer
622     if (ioBuffGetReadPtrs(pInp->hIoBuff, pInp->phyXferSize,
623                           &buff1, &size1, &buff2, &size2)
624         == IOBUFF_ERR_UNDERFLOW) {
625         pInp->numUnderflow += 1;
627         // Return since there is no enough data to process
628         return ASIT_ERR_INPBUF_UNDERFLOW;
629     }
631     ioBuffReadComplete(pInp->hIoBuff, buff1, size1);
633     if(buff2 != NULL) {
634         ioBuffReadComplete(pInp->hIoBuff, buff2, size2);
635     }
637     return ASIP_NO_ERR;
638 } /* asitBypassIoData */
641 /*============================================================================
642  * ASIT Decode Processing
643  *     This function performs the decode processing and does the following based
644  *     on the ASIT events:
645  *     - initialize the decode processing if it is the first time
646  *     - if there is ASIT INPUT DATA event (ASIT_EVTMSK_INPDATA):
647  *        - mark I/O PHY transfer completion
648  *        - run auto-detection via I/O DATA in asitInputDataProcess
649  *        - check auto-detection status and take corresponding actions if SYNC
650  *          is lost.
651  *        - start next I/O PHY transfer
652  *     - map ASIT events to decoding messages
653  *     - invoke decDecodeFsm() and pass the mapped decoding messages
654  *     - if there is ASIT DECODE ACK event (ASIT_EVTMSK_DECACK)
655  *        - mark I/O DATA read completion
656  *     - error handling
657 ============================================================================*/
658 Int asitDecodeProcessing(const PAF_ASIT_Params *pP,
659                          const PAF_ASIT_Patchs *pQ,
660                          PAF_ASIT_Config       *pAsitCfg,
661                          UInt asitEvents)
663     Int asitErr, decErr;
664     PAF_AST_IoInp *pInp;                  // I/O components for input
665     asipDecProc_t *pDec;
666     ioDataAutoDetStat_t autoDetStatus;
667     Int zMD;
668     UInt decMsg;
670     zMD = pAsitCfg->pAstCfg->masterDec;   // pointer to AST common (shared) configuration
671     pInp = &pAsitCfg->pIoInp[zMD];        // pointer to input I/O components
672     pDec = &pAsitCfg->inpDec;
674     // Initialization for decode processing when this function is called the first time
675 #ifndef DEBUG_SKIP_DECODING
676     if(!pDec->initDone) {
677         // Initialize decoder
678         decDecodeInit(pP, pAsitCfg, pInp->sourceSelect);
680         pDec->initDone = TRUE;
681     }
682 #endif
684     // Process input data if this is a data ready message
685     if(asitEvents & ASIT_EVTMSK_INPDATA) {
686         TRACE_TERSE0("asitDecodeProcessing: process input data.");
688         // Marks I/O PHY transfer and I/O BUFF write complete
689         asitPhyTransferComplete(pInp);
691         // Process input data - either search SYNC for PCM or check SYNC for bitstream
692         asitErr = asitInputDataProcess(pInp, &autoDetStatus);
694         if(asitErr == ASIT_NO_ERR) {
695             ioDataReadComplete(pInp->hIoData);
697             // Check if SYNC is maintained or lost (stream stops or format changes)
698             if(autoDetStatus.syncState == IODATA_SYNC_NONE) {
699                 // SYNC lost: change I/O PHY transfer size to default for auto-detection
700                 //asitErr = asitRecfgPhyXfer(pInp, INPUT_FRAME_SIZE_DEF);
701                 //if(asitErr != ASIT_NO_ERR) {
702                 //    return asitErr;
703                 //}
705                 // Inform decoder to complete the decoding of previous frame - is this good?
706                 pInp->sourceSelect = PAF_SOURCE_NONE;
707                 pInp->numFrameReceived = 0;    // for debugging
708                 TRACE_TERSE0("asitDecodeProcessing: SYNC lost.");
710 #ifdef DEBUG_SKIP_DECODING
711                 asitErr = ASIT_ERR_DECODE_QUIT;
712 #endif
713             }
714             else {
715                 pInp->numFrameReceived += 1;    // for debugging
717                 // Communicate input stream information to decoder through input
718                 // buffer configuration
719                 asitUpdateInpBufConfig(pAsitCfg->pAstCfg, pInp);
721                 // Start next transfer
722                 asitPhyTransferStart(pInp);
723             }
725             // Start next transfer
726             //asitPhyTransferStart(pInp);
728 #ifdef DEBUG_SKIP_DECODING
729             return asitErr;
730 #endif
731         }
732         else if(asitErr == ASIT_ERR_INPBUF_UNDERFLOW) {
733             TRACE_TERSE0("asitDecodeProcessing: Input buffer underflows.");
735             // When input buffer underflows, it is not an error but decoding needs
736             // to be skipped as there is not enough data in the buffer.
737             asitPhyTransferStart(pInp);
739             return asitErr;
740         }
741         else {
742             // Inform decoder to complete the decoding of previous frame - is this good?
743             pInp->sourceSelect = PAF_SOURCE_NONE;
744             pInp->numFrameReceived = 0;    // for debugging
745             TRACE_TERSE1("asitDecodeProcessing: asitInputDataProcess error: %d", asitErr);
747 #ifdef DEBUG_SKIP_DECODING
748             return ASIT_ERR_DECODE_QUIT;
749 #endif
750         }
751     } /* ASIT_EVTMSK_INPDATA */
752 #ifdef DEBUG_SKIP_DECODING
753     else {
754         TRACE_TERSE0("asitDecodeProcessing: events error.");
755         return ASIT_ERR_EVENTS;
756     }
757 #endif
759 #ifndef DEBUG_SKIP_DECODING
760     // Map ASIT events to decode messages
761     decMsg = asitEventsToDecMsg(asitEvents);
763     // Pass messages (corresponding to events) to decode FSM
764     decErr = decDecodeFsm(pP, pQ, pAsitCfg, pInp->sourceSelect, decMsg);
766     // Mark I/O DATA read complete if decoder indicates decoding is done.
767     if((asitEvents & ASIT_EVTMSK_DECACK)) {  // DECACK -> decoding done
768         //ioDataReadComplete(pInp->hIoData);
769     }
771     if(decErr != DEC_NO_ERR) {
772         TRACE_VERBOSE0("TaskAsip: send DEC_EXIT message to slave decoder.");
774         // Send dec exit message to slave decoder
775         if( AspMsgSend(ASP_SLAVE_DEC_EXIT, ASP_MASTER_DEC_EXIT_DONE, NULL, NULL)
776             != ASP_MSG_NO_ERR) {
777             TRACE_VERBOSE0("TaskAsip: error in sending DEC_EXIT message");
778             SW_BREAKPOINT;
779         }
781         return ASIT_ERR_DECODE_QUIT;  // This is not necessarily an error
782     }
783     else {
784         return ASIT_NO_ERR;
785     }
786 #endif
787 }  /* asitDecodeProcessing */
790 /*============================================================================
791  * ASIT Input Data Processing:
792  *    - invoke ioDataProcess() to inspect input data for
793  *       - initial auto-detection,  or
794  *       - background scanning for PCM data, or
795  *       - SYNC check for bitstream
796  *    - return auto-detection status (SYNC detected, SYNC loss, etc)
797 ============================================================================*/
798 Int asitInputDataProcess(PAF_AST_IoInp *pInp, ioDataAutoDetStat_t *pAutoDetStats)
800     Int ioDataErr, retVal;
801     ioDataCtl_t ioDataCtl;
803     // Perform auto-detection inside I/O DATA component
804     ioDataErr = ioDataProcess(pInp->hIoData);
806     if(ioDataErr == IODATA_NO_ERR) {
807         // Normal operation - check auto-detection status
808         ioDataCtl.code = IODATA_CTL_GET_AUTODET_STATUS;
809         ioDataControl(pInp->hIoData, &ioDataCtl);
811         *pAutoDetStats = ioDataCtl.param.autoDetStats;
813         retVal = ASIT_NO_ERR;
814     }
815     else if(ioDataErr == IODATA_ERR_IOBUF_UNDERFLOW) {
816         // Input buffer underflows - there is no enough data to process.
817         // This is not error and no action is needed.
818         pInp->numUnderflow += 1; // debug
820         retVal = ASIT_ERR_INPBUF_UNDERFLOW;
821     }
822     else {
823         // Something is wrong: print error log and return
824         //printf("IODATA processing error!\n");
825         retVal = ASIT_ERR_INPDATA_PROC;
826     }
828     return retVal;
829 } /* asitInputDataProcess */
831 /*============================================================================
832  * Mapping ASIT Events to Decoding Messages
833 ============================================================================*/
834 UInt asitEventsToDecMsg(UInt asitEvents)
836     UInt decMsg = 0;
838     if(asitEvents & ASIT_EVTMSK_INPDATA) {
839         decMsg |= DEC_MSGMSK_INPDATA;
840     }
842     if(asitEvents & ASIT_EVTMSK_INFOACK) {
843         decMsg |= DEC_MSGMSK_INFOACK;
844     }
846     if(asitEvents & ASIT_EVTMSK_DECACK) {
847         decMsg |= DEC_MSGMSK_DECACK;
848     }
850     return decMsg;
851 } /* asitEventsToDecMsg */
853 /*===========================================================================
854  * Initialize I/O components for input processing
855 ============================================================================*/
856 int asitIoCompsInit(PAF_AST_InpBuf * pInpBuf, PAF_AST_IoInp * pInpIo)
858     ioBuffParams_t ioBuffParams;
859     ioPhyParams_t  ioPhyParams;
860     ioDataParam_t  ioDataCfg;
861     ioPhyCtl_t     ioPhyCtl;
863     pInpIo->phyXferSize = INPUT_FRAME_SIZE_DEF;
865     if(pInpIo->firstTimeInit) {
866         TRACE_VERBOSE0("Initialize I/O BUFF and I/O PHY.");
867         ioBuffParams.base         = pInpBuf->inpBufConfig.base.pVoid;
868         ioBuffParams.size         = pInpBuf->inpBufConfig.allocation / STRIDE_WORST_CASE
869                                                                      * STRIDE_WORST_CASE;
870         ioBuffParams.sync         = IOBUFF_WRITE_SYNC;
871         ioBuffParams.nominalDelay = INPUT_FRAME_SIZE_DEF;
872         if(ioBuffInit(pInpIo->hIoBuff, &ioBuffParams) != IOBUFF_NOERR) {
873             return (ASIT_ERR_IOBUFF_INIT);   // to remove magic number
874         }
876         ioPhyParams.ioBuffHandle    = pInpIo->hIoBuff;
877         ioPhyParams.xferFrameSize   = pInpIo->phyXferSize;
878         ioPhyParams.mcaspChanHandle = pInpIo->hMcaspChan;
879         ioPhyParams.ioBuffOp        = IOPHY_IOBUFFOP_WRITE;
880         if(ioPhyInit(pInpIo->hIoPhy, &ioPhyParams) != IOPHY_NOERR) {
881             return (ASIT_ERR_IOPYH_INIT);   // to remove magic number
882         }
884         pInpIo->numPrimeXfers = NUM_PRIME_XFERS;
885     }
887     /* Reinitialize I/O DATA every time when ASIT restarts */
888     TRACE_VERBOSE0("Initialize I/O DATA.");
889     ioDataCfg.ioBuffHandle         = pInpIo->hIoBuff;
890     ioDataCfg.unknownSourceTimeOut = pInpBuf->inpBufConfig.pBufStatus->unknownTimeout;
891     ioDataCfg.frameLengthsIEC      = (uint_least16_t *)&iecFrameLength[0];
892     ioDataCfg.frameLengthPCM       = INPUT_FRAME_SIZE_PCM / WORD_SIZE_PCM;
893     ioDataCfg.frameLengthDef       = INPUT_FRAME_SIZE_DEF / WORD_SIZE_BITSTREAM;
894     ioDataCfg.ibMode               = pInpBuf->inpBufConfig.pBufStatus->mode;
895     ioDataCfg.zeroRunRestart       = pInpBuf->inpBufConfig.pBufStatus->zeroRunRestart;
896     ioDataCfg.zeroRunTrigger       = pInpBuf->inpBufConfig.pBufStatus->zeroRunTrigger;
898     if(ioDataInit(pInpIo->hIoData, &ioDataCfg) != IODATA_NO_ERR) {
899         return (ASIT_ERR_IODATA_INIT);   // to remove magic number
900     }
902     if(pInpIo->firstTimeInit) {
903         /* Initialize I/O BUFF and I/O PHY only when input interface changes. */
904         TRACE_VERBOSE0("Prime I/O PHY.");
906         // Start I/O physical layer by priming McASP LLD for input
907         asitIoPhyPrime(pInpIo);
909         pInpIo->firstTimeInit = FALSE;
910     }
911     else {
912         // Reconfigure I/O PHY transfer size
913         ioPhyCtl.code = IOPHY_CTL_FRAME_SIZE;
914         ioPhyCtl.params.xferFrameSize = pInpIo->phyXferSize;
915         ioPhyControl(pInpIo->hIoPhy, &ioPhyCtl);
917         // If previous stream before reset was PCM, reconfigure McASP LLD to receive 16-bit packed bits
918         if(!pInpIo->swapData) {
919             Int mcaspErr;
920             mcaspErr = mcaspRecfgWordWidth(pInpIo->hMcaspChan, Mcasp_WordLength_16);
921             if(mcaspErr != Audk2g_EOK) {
922                 return ASIT_ERR_MCASP_CFG;
923             }
925             // Start swapping data
926             pInpIo->swapData = TRUE;
927             TRACE_VERBOSE0("Reconfigure McASP word length and start swapping data.");
928         }
930         // Start PHY transfer
931         TRACE_VERBOSE0("Start I/O PHY transfer.");
932         asitPhyTransferStart(pInpIo);
933     }
935     return 0;
936 } /* asitIoCompsInit */
938 /*======================================================================================
939  *  This function initializes ASIT processing
940  *====================================================================================*/
941 void asitProcInit(PAF_AST_IoInp  *pInp, asipDecProc_t *pDec)
943     pInp->swapData = TRUE;
944     pInp->pcmSwitchHangOver = INPUT_SWITCH_HANGOVER;
945     pDec->initDone = FALSE;
946     pInp->numFrameReceived = 0;
949 /*======================================================================================
950  *  I/O physical layer prime operation required by McASP LLD
951  *====================================================================================*/
952 void asitIoPhyPrime(PAF_AST_IoInp *pInp)
954     Int32        count;
956     for(count = 0; count < pInp->numPrimeXfers; count++)
957     {
958         ioPhyXferSubmit(pInp->hIoPhy);
959 #ifdef ASIP_DEBUG
960         //pInp->numXferStart++;
961 #endif
962     }
963 } /* asitIoPhyPrime */
966 /*======================================================================================
967  *  This function marks the I/O PHY transfer as complete
968  *====================================================================================*/
969 void asitPhyTransferComplete(PAF_AST_IoInp * pInpIo)
971     // Mark underlining I/O BUFF write complete and swap data if needed
972     ioPhyXferComplete(pInpIo->hIoPhy, pInpIo->swapData);
973 } /* asitPhyTransferComplete */
975 Int asitRecfgPhyXfer(PAF_AST_IoInp *pInp, size_t xferSize)
977     ioPhyCtl_t ioPhyCtl;
978     Int mcaspErr;
980     ioPhyCtl.code = IOPHY_CTL_FRAME_SIZE;
981     ioPhyCtl.params.xferFrameSize = xferSize;
982     ioPhyControl(pInp->hIoPhy, &ioPhyCtl);
984     pInp->phyXferSize  = ioPhyCtl.params.xferFrameSize;
986     if(!pInp->swapData) {
987         // If it was PCM, reconfigure McASP LLD to receive 16-bit packed bits
988         mcaspErr = mcaspRecfgWordWidth(pInp->hMcaspChan, Mcasp_WordLength_16);
989         if(mcaspErr != Audk2g_EOK) {
990             return ASIT_ERR_MCASP_CFG;
991         }
993         // Start swapping data
994         pInp->swapData = TRUE;
995     }
997     return ASIT_NO_ERR;
999 } /* asitRecfgPhyXfer */
1001 /*======================================================================================
1002  *  McASP LLD call back function
1003  *====================================================================================*/
1004 void asipMcaspCallback(void* arg, MCASP_Packet *mcasp_packet)
1006     /* post semaphore */
1007     if(mcasp_packet->arg == IOPHY_XFER_FINAL) {
1008         //Semaphore_post(asipSemRx);
1009         Event_post(asitEvent, ASIT_EVTMSK_INPDATA);
1010     } else {
1011         ;    // intermediate packet due to buffer wrapping around
1012     }
1015 /*======================================================================================
1016  *  This function checks if McASP Rx for input overruns
1017  *====================================================================================*/
1018 int asipCheckMcaspRxOverrun(Ptr mcaspChanHandle)
1020     Mcasp_errCbStatus mcaspErrStat;
1022     mcaspControlChan(mcaspChanHandle, Mcasp_IOCTL_CHAN_QUERY_ERROR_STATS, &mcaspErrStat);
1024     return (mcaspErrStat.isRcvOvrRunOrTxUndRunErr);
1027 /*======================================================================================
1028  *  This function restarts McASP LLD channel for input
1029  *====================================================================================*/
1030 void asipMcaspRxRestart(void)
1032     mcaspRxReset();
1033     mcaspRxCreate();
1036 /*======================================================================================
1037  *  This function starts an I/O PHY transfer
1038  *====================================================================================*/
1039 void asitPhyTransferStart(PAF_AST_IoInp *pInpIo)
1041     Int ioPhyErr;
1043     if(asipCheckMcaspRxOverrun(pInpIo->hMcaspChan)) {
1044 #ifdef ASIP_DEBUG
1045         pInpIo->numInputOverrun++;
1046 #endif
1047         asipMcaspRxRestart();
1048     }
1049     else {
1050         ioPhyErr = ioPhyXferSubmit(pInpIo->hIoPhy);
1051         //if(ioPhyXferSubmit(pInpIo->hIoPhy)==IOPHY_ERR_BUFF_OVERFLOW) {
1052         if(ioPhyErr!=IOPHY_NOERR){
1053             printf("\n I/O PHY ioPhyXferSubmit fails with error %d!\n", ioPhyErr);
1054             // Input buffer overflows!
1055             //printf("\nInput buffer overflows!\n");
1056             exit(0);
1057         }
1058         else {
1059             // Input buffer operates normally
1060             ;
1061         }
1062 #ifdef ASIP_DEBUG
1063         //pInpIo->numXferStart++;
1064 #endif
1065     }
1068 Int d10Initialized = 0;
1069 //extern Audk2g_STATUS mcaspAudioConfig(void);
1070 extern void McaspDevice_init(void);
1072 /*======================================================================================
1073  *  This function initializes HW interface and selects the right device for input
1074  *====================================================================================*/
1075 Int asitSelectDevices(const PAF_ASIT_Patchs *pQ, PAF_AST_IoInp *pInp)
1077     Audk2g_STATUS status;
1079     //more configuration is needed to abstract out D10
1080     if(!d10Initialized) {
1081         /* Initialize McASP HW details */
1082         McaspDevice_init();
1084         D10_init();
1086 #ifdef INPUT_SPDIF
1087         // Input is DIR
1088         status = audk2g_AudioSelectClkSrc(AUDK2G_AUDIO_CLK_SRC_DIR);
1089 #else
1090         // Input is HDMI
1091         status = audk2g_AudioSelectClkSrc(AUDK2G_AUDIO_CLK_SRC_I2S);
1092 #endif
1093         if(status != Audk2g_EOK) {
1094             Log_info0("audk2g_AudioSelectClkSrc Failed!\n");
1095             return ASIT_ERR_D10_CFG;
1096         }
1097         audk2g_delay(50000); // Without delay between these 2 calls system aborts.
1099         /* Initialize McASP module */
1100         status = mcaspAudioConfig(); //defined in newio\fw\mcasp_cfg.c
1101         if(status != Audk2g_EOK) {
1102             Log_info0("McASP Configuration Failed!\n");
1103             return ASIT_ERR_MCASP_CFG;
1104         }
1106         pInp->hMcaspChan = hMcaspRxChan;
1107         d10Initialized = 1;
1108     }
1110     /////////////// TODO: HW interface selection and initialization //////////////
1111     ////// to add what PAF_ASIT_selectDevices() does /////////
1112 #ifdef IO_HW_INTERFACE
1113     pInp->pRxParams = pQ->devinp->x[IO_HW_INTERFACE];
1114 #else
1115     pInp->pRxParams = NULL;
1116 #endif
1118     return 0;
1119 }  /* asitSelectDevices */
1121 /*======================================================================================
1122  *  This function updates input status
1123  *====================================================================================*/
1124 Int asitUpdateInputStatus(const void *pRxParams, PAF_InpBufStatus *pStatus,
1125                           PAF_InpBufConfig *pInpBuf)
1127     Int asipErrno;
1129     PAF_SIO_InputStatus inputStatus;
1131     // initialize all values to unknown so that device specific
1132     //   driver layer need only fill in those entries that it is aware of.
1133     //   This allows extensibility of the structure without requiring users
1134     //   to re-code.
1135     inputStatus.lock = 0;
1136     inputStatus.sampleRateData = PAF_SAMPLERATE_UNKNOWN;
1137     inputStatus.sampleRateMeasured = PAF_SAMPLERATE_UNKNOWN;
1138     inputStatus.nonaudio = PAF_IEC_AUDIOMODE_UNKNOWN;
1139     inputStatus.emphasis = PAF_IEC_PREEMPHASIS_UNKNOWN;
1141     //more configuration is needed to abstract out D10
1142     asipErrno = D10_RxControl(pRxParams,
1143                           (Uns)PAF_SIO_CONTROL_GET_INPUT_STATUS,
1144                           (Arg) &inputStatus);
1145     if (asipErrno) {
1146         return asipErrno;
1147     }
1148     pStatus->sampleRateData = inputStatus.sampleRateData;
1149     pStatus->sampleRateMeasured = inputStatus.sampleRateMeasured;
1150     pStatus->nonaudio = inputStatus.nonaudio;
1151     pStatus->emphasisData = inputStatus.emphasis;
1153     // if MSB of override clear then use as reported lock
1154     // if = 0x80 then use default [0x81]
1155     // if = 0x81 then use measured (from device)
1156     // others not defined or implemented
1157     if ((pStatus->lockOverride & (XDAS_Int8)0x80) == 0)
1158         pStatus->lock = pStatus->lockOverride;
1159     else if (pStatus->lockOverride == (XDAS_Int8)0x80)
1160         pStatus->lock = inputStatus.lock;
1161     else if (pStatus->lockOverride == (XDAS_Int8)0x81)
1162         pStatus->lock = inputStatus.lock;
1164     // if MSB of override clear then use it as sample rate for system,
1165     // if = 0x80 then use default [0x82]
1166     // if = 0x81 then use data
1167     // if = 0x82 then use measured
1168     // others not defined or implemented
1169     if ((pStatus->sampleRateOverride & (XDAS_Int8)0x80) == 0)
1170         pStatus->sampleRateStatus = pStatus->sampleRateOverride;
1171     else if (pStatus->sampleRateOverride == (XDAS_Int8)0x80)
1172         pStatus->sampleRateStatus = pStatus->sampleRateMeasured;
1173     else if (pStatus->sampleRateOverride == (XDAS_Int8)0x81)
1174         pStatus->sampleRateStatus = pStatus->sampleRateData;
1175     else if (pStatus->sampleRateOverride == (XDAS_Int8)0x82)
1176         pStatus->sampleRateStatus = pStatus->sampleRateMeasured;
1178     // Update emphasis status:
1179     if ((pStatus->emphasisOverride & (XDAS_Int8)0x80) == 0) {
1180         if (pStatus->emphasisData == PAF_IEC_PREEMPHASIS_YES)
1181             pStatus->emphasisStatus = PAF_IEC_PREEMPHASIS_YES;
1182         else
1183             pStatus->emphasisStatus = PAF_IEC_PREEMPHASIS_NO;
1184     }
1185     else if (pStatus->emphasisOverride ==
1186              (XDAS_Int8 )(0x80+PAF_IEC_PREEMPHASIS_YES))
1187         pStatus->emphasisStatus = PAF_IEC_PREEMPHASIS_YES;
1188     else /* IBEmphasisOverrideNo or other */
1189         pStatus->emphasisStatus = PAF_IEC_PREEMPHASIS_NO;
1191     // Update precision control
1192     pInpBuf->precision = pStatus->precisionInput =
1193         pStatus->precisionOverride < 0
1194         ? pStatus->precisionDefault
1195         : pStatus->precisionOverride > 0
1196         ? pStatus->precisionOverride
1197         : pStatus->precisionDetect > 0
1198         ? pStatus->precisionDetect
1199         : pStatus->precisionDefault;
1201     return 0;
1205 /*==============================================================================
1206  * This function updates input buffer config based on frame information provided
1207  * by I/O DATA.
1208  ==============================================================================*/
1209 void asitUpdateInpBufConfig(PAF_AST_Config *pAstCfg, PAF_AST_IoInp  *pInp)
1211     PAF_InpBufConfig *pBufConfig;
1212     ioDataCtl_t ioDataCtl;
1214     /* Get information for reading input data */
1215     ioDataCtl.code = IODATA_CTL_GET_INPBUFFINFO;
1216     ioDataControl(pInp->hIoData, &ioDataCtl);
1218     if(ioDataCtl.param.dataReadInfo.frameSize != pInp->phyXferSize) {
1219         // Fatal error!
1220         TRACE_VERBOSE0("TaskAsip: error in updating I/O");
1221         SW_BREAKPOINT;
1222     }
1224     pBufConfig = &(pAstCfg->xInp[pAstCfg->masterDec].inpBufConfig);
1226     //JXTODO: do we need to gate here?
1227     //key = GateMP_enter(gateHandle);
1229     pBufConfig->base.pVoid   = ioDataCtl.param.dataReadInfo.buffBase;
1230     pBufConfig->sizeofBuffer = ioDataCtl.param.dataReadInfo.buffSize;
1231     pBufConfig->pntr.pSmInt  = ioDataCtl.param.dataReadInfo.startAddress;
1233     // Leave the gate
1234     //GateMP_leave(gateHandle, key);
1236     TRACE_TERSE2("Frame start address: 0x%x., preamble: 0x%x",
1237                  (UInt)ioDataCtl.param.dataReadInfo.startAddress,
1238                  *(UInt *)ioDataCtl.param.dataReadInfo.startAddress);
1241 /*==============================================================================
1242  * Decide source after SYNC is found, i.e. either bitstream preamble is detected
1243  * or it times out to PCM.
1244  ==============================================================================*/
1245 Int asitDecideSource(PAF_AST_Config *pAstCfg, PAF_AST_IoInp  *pInp,
1246                      ioDataAutoDetStat_t *autoDetStatus)
1248     Int sourceConfig, sourceSelect, sourceProgram;
1249     Int zMD;
1250     char asipMsgBuf[ASP_MSG_BUF_LEN];
1252     // Get the configured source
1253     zMD = pAstCfg->masterDec;
1254     sourceConfig = (Int)sharedMemReadInt8(&(pAstCfg->xDec[zMD].decodeStatus.sourceSelect),
1255                                           GATEMP_INDEX_DEC);
1257     if(autoDetStatus->syncState == IODATA_SYNC_PCM) {
1258         if (sourceConfig == PAF_SOURCE_DSD1 || sourceConfig == PAF_SOURCE_DSD2 ||
1259             sourceConfig == PAF_SOURCE_DSD3) {
1260             sourceProgram = sourceConfig;
1261         }
1262         else {
1263             sourceProgram = PAF_SOURCE_PCM;
1264         }
1265     }
1267     if(autoDetStatus->syncState == IODATA_SYNC_BITSTREAM) {
1268         uint_least16_t pc = autoDetStatus->bitStreamInfo & SYNC_PC_MASK; //0x001F
1269         sourceProgram = IECpafSource[pc];
1270     }
1272     // write the decided source program to memory
1273     sharedMemWriteInt8(&(pAstCfg->xDec[zMD].decodeStatus.sourceProgram), sourceProgram,
1274                        GATEMP_INDEX_DEC);
1276     // now that we have some input classification, and possibly an outstanding
1277     // input frame, we determine whether or not to call decodeProcessing and with
1278     // what decAlg.
1279     sourceSelect = PAF_SOURCE_NONE;
1281     switch (sourceConfig)
1282     {
1283         // If autodetecting, decoding everything, and input is something
1284         // (i.e. bitstream or PCM) then decode.
1285         case PAF_SOURCE_AUTO:
1286             if (sourceProgram >= PAF_SOURCE_PCM)  {
1287                 sourceSelect = sourceProgram; // use whatever from autodet
1288             }
1289             break;
1291         // If autodetecting, decoding only PCM, and input is PCM then decode.
1292         case PAF_SOURCE_PCMAUTO:
1293             if (sourceProgram == PAF_SOURCE_PCM) {
1294                 // only expect autodet to give PAF_SOURCE_PCM, otherwise set to NONE
1295                 sourceSelect = sourceProgram;
1296             }
1297             break;
1299         // If autodetecting, decoding only bitstreams, and input is a bitstream then decode.
1300         case PAF_SOURCE_BITSTREAM:
1301             if (sourceProgram >= PAF_SOURCE_AC3) {
1302                 sourceSelect = sourceProgram;
1303             }
1304             break;
1306         // If autodetecting, decoding only DTS, and input is DTS then decode.
1307         case PAF_SOURCE_DTSALL:
1308             switch (sourceProgram)
1309             {
1310                 case PAF_SOURCE_DTS11:
1311                 case PAF_SOURCE_DTS12:
1312                 case PAF_SOURCE_DTS13:
1313                 case PAF_SOURCE_DTS14:
1314                 case PAF_SOURCE_DTS16:
1315                 case PAF_SOURCE_DTSHD:
1316                     sourceSelect = sourceProgram;
1317                     break;
1318             }
1319             break;
1321         // All others, e.g., force modes, fall through to here.
1322         // If user made specific selection then program must match select.
1323         // (NB: this compare relies on ordering of PAF_SOURCE)
1324         default:
1325             sourceSelect = sourceConfig;
1326             if ((sourceSelect >= PAF_SOURCE_PCM) && (sourceSelect <= PAF_SOURCE_N)) {
1327                 if (sourceProgram != sourceSelect) {
1328                     sourceSelect = PAF_SOURCE_NONE;
1329                 }
1330             }
1331             break;
1332     }
1334     // if we didn't find any matches then skip
1335     if (sourceSelect == PAF_SOURCE_NONE) {
1336         TRACE_VERBOSE0("TaskAsip: no matching source type, continue");
1337         return ASIT_ERR_NO_MATCHING_SOURCE;
1338     }
1340 #ifndef DEBUG_SKIP_DECODING
1341     // send source select message to slave
1342     *(Int32 *)&asipMsgBuf[0] = sourceSelect;
1343     if(AspMsgSend(ASP_SLAVE_DEC_SOURCE_SELECT, ASP_MASTER_DEC_SOURCE_SELECT_DONE,
1344                   asipMsgBuf, NULL) != ASP_MSG_NO_ERR) {
1345         TRACE_VERBOSE0("TaskAsip: error in sending SOURCE_SELECT message");
1346         SW_BREAKPOINT;
1347     }
1348 #endif
1350     pInp->sourceSelect  = sourceSelect;
1351     pInp->sourceProgram = sourceProgram;
1353     return ASIT_NO_ERR;
1354 } /* asitDecideSource */
1356 /*==============================================================================
1357  * After SYNC is found, i.e. either bitstream preamble is detected or it times
1358  * out to PCM, update input buffer config and I/o components accordingly.
1359  ==============================================================================*/
1360 Int asitUpdateIoComps(const PAF_ASIT_Params *pP, PAF_AST_Config *pAstCfg,
1361                       PAF_AST_IoInp  *pInp, ioDataAutoDetStat_t *autoDetStatus)
1363     Int sourceConfig;
1364     Int zMD, deliverZeros;
1365     int ioFrameLength, decFrameLength;
1366     PAF_InpBufConfig *pBufConfig;
1367     ioPhyCtl_t  ioPhyCtl;
1368     ioDataCtl_t ioDataCtl;
1370     zMD = pAstCfg->masterDec;
1371     pBufConfig = &pAstCfg->xInp[zMD].inpBufConfig;
1373     // Compute decoder frame length based on source selection
1374     decFrameLength = getFrameLengthSourceSel(pP, pInp->sourceSelect);
1376     pAstCfg->xDec[zMD].decodeControl.frameLength  = decFrameLength;
1377     pAstCfg->xDec[zMD].decodeInStruct.sampleCount = decFrameLength;
1378     pAstCfg->xDec[zMD].decodeControl.sampleRate   = PAF_SAMPLERATE_UNKNOWN;
1380     // Decide frame length for I/O DATA and I/O PHY
1381     if(autoDetStatus->syncState == IODATA_SYNC_PCM) {
1382         // For PCM, I/O frame length is decode frame length multiplied by stride
1383         ioFrameLength = decFrameLength * INPUT_STRIDE;
1385         pBufConfig->sizeofElement = WORD_SIZE_PCM;
1386         pBufConfig->frameLength   = pBufConfig->lengthofData = ioFrameLength;
1388         // Configure I/O DATA PCM frame length
1389         ioDataCtl.code = IODATA_CTL_SET_PCM_FRAME_LENGTH;
1390         ioDataCtl.param.frameLengthPcm = ioFrameLength;
1391         ioDataControl(pInp->hIoData, &ioDataCtl);
1393         // Change I/O PHY transfer size to PCM frame size
1394         pInp->phyXferSize = ioFrameLength*(WORD_SIZE_PCM);
1396         // Adjust I/O BUFF delay and read pointer - to make sure read pointers always point to
1397         // PCM data from 1st I2S (out of 4 for HDMI 4xI2S)
1398         // Adjust delay and don't mark input buffer as read complete
1399         //ioBuffAdjustDelay(pInp->hIoBuff, pInp->phyXferSize);
1401         // Stop swapping data
1402         //pInp->swapData = FALSE;
1403     }
1404     else {
1405         // For bitstream, I/O frame length is the frame length of the bitstream
1406         uint_least16_t pc = autoDetStatus->bitStreamInfo & SYNC_PC_MASK; //0x001F
1407         ioFrameLength = iecFrameLength[pc];
1409 /*
1410         if(   (pc == 0x11) && (DTSHDSubType == 3)
1411            && (PAF_ASP_sampleRateHzTable[pBufConfig->pBufStatus->sampleRateStatus][PAF_SAMPLERATEHZ_STD] <=48000.0))
1412             pDevExt->sourceProgram = PAF_SOURCE_DXP;    // LBR is 23
1414         if (pc == 1)
1415             pDevExt->elementSize = 4288;
1416         else if (pc == 0x11) {
1417             pDevExt->frameLength = (pDevExt->pIECFrameLength[pc] << DTSHDSubType);
1418             pDevExt->lengthofData = pDevExt->frameLength;
1419         }
1420 */
1422         pBufConfig->sizeofElement = WORD_SIZE_BITSTREAM;
1423         pBufConfig->frameLength   = ioFrameLength;
1424         pBufConfig->lengthofData  = ioFrameLength - IEC_HEADER_LENGTH;
1426         // Change I/O PHY transfer size to bitstream frame size
1427         pInp->phyXferSize = ioFrameLength*WORD_SIZE_BITSTREAM;
1428     }
1430     pBufConfig->stride = INPUT_STRIDE;   // common for PCM and bitstream
1432     // Configure I/O PHY transfer size
1433     ioPhyCtl.code = IOPHY_CTL_FRAME_SIZE;
1434     ioPhyCtl.params.xferFrameSize = pInp->phyXferSize;
1435     ioPhyControl(pInp->hIoPhy, &ioPhyCtl);
1437     // Decide if zeros should be delivered based on the configured source
1438     sourceConfig = (Int)sharedMemReadInt8(&(pAstCfg->xDec[zMD].decodeStatus.sourceSelect),
1439                                           GATEMP_INDEX_DEC);
1441     if(autoDetStatus->syncState == IODATA_SYNC_PCM) {
1442         // Bitstream preamble is not found and it times out -> assume this is PCM
1443         deliverZeros = autoDetStatus->deliverZeros;
1444         if (sourceConfig == PAF_SOURCE_PCM || sourceConfig == PAF_SOURCE_DSD1 ||
1445             sourceConfig == PAF_SOURCE_DSD2 || sourceConfig == PAF_SOURCE_DSD3) {
1446             // set to one -- ensures that PCM decode calls made before data is
1447             // available will result in zero output.
1448             // (mostly needed for PA15 since, currently, all other frameworks
1449             // require a frame of data before the first decode call.
1450             deliverZeros = TRUE;  // override deliverZeros returned by ioDataControl
1451         }
1453         // update input buffer config structure
1454         pBufConfig->deliverZeros  = deliverZeros;
1455     }
1457     //JXTODO: decide what to do with hRxSio
1458     //temporary - does ARM use hRxSio or just check if it is not NULL?
1459     pAstCfg->xInp[zMD].hRxSio = pInp->hIoData;
1460     pAstCfg->xInp[zMD].pInpBuf = &(pAstCfg->xInp[zMD].inpBufConfig);
1462     return ASIT_NO_ERR;
1463 } /* asitUpdateIoComps */
1466 #ifndef IO_LOOPBACK_TEST
1467 #if OUTPUT_FRAME_LENGTH == INPUT_FRAME_LENGTH
1468 U8 pcmbuf[OUTPUT_FRAME_SIZE];
1469 #else
1470 #error Input frame length is not equal to output frame length!
1471 #endif
1473 Int rxDecodePcm(PAF_AST_IoInp  *pInp)
1475     ioDataCtl_t ioDataCtl;
1476     void *buffBase;
1477     void *dataStartAddress;
1478     size_t buffSize, frameSize, size1, size2;
1480     /* Get information for reading input data */
1481     ioDataCtl.code = IODATA_CTL_GET_INPBUFFINFO;
1482     ioDataControl(pInp->hIoData, &ioDataCtl);
1484     buffBase = ioDataCtl.param.dataReadInfo.buffBase;
1485     buffSize = ioDataCtl.param.dataReadInfo.buffSize;
1486     dataStartAddress = ioDataCtl.param.dataReadInfo.startAddress;
1487     frameSize        = ioDataCtl.param.dataReadInfo.frameSize;
1489     // Copy PCM data to output buffer
1490     if(((size_t)dataStartAddress+frameSize) <= ((size_t)buffBase+buffSize)) {
1491         // Input buffer doesn't wrap around
1492         Cache_inv(dataStartAddress, frameSize, Cache_Type_ALL, TRUE);
1493         memcpy((void *)&pcmbuf[0], dataStartAddress, frameSize);
1494     }
1495     else {
1496         // Input buffer wraps around
1497         size1 = (size_t)buffBase + buffSize - (size_t)dataStartAddress;
1498         size2 = frameSize - size1;
1499         Cache_inv(dataStartAddress, size1, Cache_Type_ALL, TRUE);
1500         memcpy((void *)&pcmbuf[0], dataStartAddress, size1);
1502         Cache_inv(buffBase, size2, Cache_Type_ALL, TRUE);
1503         memcpy((void *)&pcmbuf[size1], buffBase, size2);
1504     }
1507     return ASIT_NO_ERR;
1511 Int rxDecodePlayZero(PAF_AST_IoInp  *pInp)
1513     return ASIT_NO_ERR;
1515 #endif
1517 Int asitEvtErrCheck(UInt actualEvents, UInt expectedEvents)
1519     TRACE_VERBOSE2("ASIT events error: actual events are: %d, expected events are: %d.",
1520                    actualEvents, expectedEvents);
1522     return ASIT_ERR_EVENTS;
1525 void asitErrorHandling(PAF_ASIT_Config *pAsitCfg, Int asitErr)
1527     UInt events;
1529     if(asitErr == ASIT_ERR_INPBUF_UNDERFLOW) {
1530         TRACE_VERBOSE0("ASIT error handling: input buffer underflows. No actions needed.");
1531     }
1533     if(asitErr == ASIT_ERR_DECODE_QUIT) {
1534         TRACE_VERBOSE0("ASIT error handling: DECODE_QUIT - clear INPDATA event.");
1535 #if 0
1536         // Pend on INPTDATA event that should have been posted before decoding quits.
1537         events = Event_pend(asitEvent, ASIT_EVTMSK_NONE, ASIT_EVTMSK_INPDATA,
1538                             BIOS_WAIT_FOREVER);
1540         // Marks I/O PHY transfer and I/O BUFF write complete
1541         asitPhyTransferComplete(&pAsitCfg->pIoInp[0]);
1543         // Keep I/O BUFF read pointers going
1544         asitBypassIoData(&pAsitCfg->pIoInp[0]);
1545 #endif
1546         pAsitCfg->pIoInp[0].asipState = ASIT_RESET;
1547         pAsitCfg->pIoInp[0].numAsitRestart++;
1548         TRACE_VERBOSE0("ASIT error handling finished. Go to state ASIT_RESET.");
1549     }
1551     if(asitErr == ASIT_ERR_EVENTS) {
1552         pAsitCfg->pIoInp[0].asipState = ASIT_RESET;
1553         pAsitCfg->pIoInp[0].numAsitRestart++;
1554         TRACE_VERBOSE0("ASIT error handling: events error. Go to state ASIT_RESET.");
1555     }
1557     return;
1558 }  /* asitErrorHandling */
1561 //////////////////////////////////////////////////////////////////////////////
1562 void asitPostInfoEvent()
1564     Event_post(asitEvent, ASIT_EVTMSK_INFOACK);
1567 void asitPostDecEvent()
1569     Event_post(asitEvent, ASIT_EVTMSK_DECACK);
1571 //////////////////////////////////////////////////////////////////////////////
1573 /* Nothing past this line */