]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/performance-audio-sr.git/blob - processor_audio_sdk_1_00_00_00/pasdk/test_dsp/sap/sap.c
Add Govind's changes for new HSR4/interconnect boards
[processor-sdk/performance-audio-sr.git] / processor_audio_sdk_1_00_00_00 / pasdk / test_dsp / sap / sap.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 //
38 // SIO driver implementation for audio I/O using McASP.
39 //
40 //
41 //
43 #ifndef SAP_CACHE_SUPPORT
44   // if you rebuild dap.c in your project without this defined, 
45   // the result is quite hard to find:  Occasional glitches in the sound.
46   #define SAP_CACHE_SUPPORT  // typically defined in the project
47 #endif
49 #ifdef SAP_CACHE_SUPPORT
50 #include <ti/sysbios/hal/Cache.h>
51 #endif
53 #include <stdlib.h>
54 #include <stdio.h>
55 #include <string.h> //memset
57 #include <xdc/std.h>
59 #include "sap.h"
61 #include <ti/sdo/edma3/drv/edma3_drv.h>
62 #include <ti/sdo/edma3/drv/src/edma3.h>
63 #include <ti/sdo/edma3/rm/edma3_rm.h>
64 #include <ti/sdo/edma3/drv/sample/bios6_edma3_drv_sample.h>
66 #include <ti/csl/cslr_device.h>
68 #define EDMA_HINV NULL
70 #ifdef SAP_PORT_MCASP
71 #include "sap_mcasp.h"
72 #else
73 #error "No port defined"
74 #endif
76 #include <pafsio.h>
78 // This works to set a breakpoint
79 #define SW_BREAKPOINT       asm( " SWBP 0" );
80 /* Software Breakpoint to Code Composer */
81 // SW_BREAKPOINT;
83 // global allocated in bios_edma3_drv_sample_init.c
84 extern EDMA3_DRV_Handle hEdma0;
85 extern EDMA3_DRV_Handle hEdma1;
87 int gStartError;
88 int gIsrCnt;
89 int gIsrElseCnt;
90 int gIsrErrCnt;
91 int gIsrRunCnt;
92 int gIsrNotRunCnt;
93 int gisrOutput;
95 typedef xdc_Short MdInt;
97 void swapHdmi(Ptr, int);
99 #define TEST_MULTICHANNEL
102 #ifdef TEST_MULTICHANNEL
103 #define SAP_UNDER_LEN 4
104 //#define SAP_UNDER_LEN 1024 // GJ: experiment
105 #else
106 #define SAP_UNDER_LEN 1024
107 #endif
108 int sap_UNDER[SAP_UNDER_LEN]; // used for underrun
109 int sap_OVER = 0;     // used for overrun
110 void DJDBG_SAP_EDMA_dumpParams(int tag_place)
112         unsigned int *ptrPARAM_BASE = (unsigned int *)0x02704000;
113         unsigned int *ptrPARAM0x18 = (unsigned int *)0x02704300; // ((*((EDMA3_CCRL_Regs *) 0x02700000)).PARAMENTRY)[24]
114         unsigned int *ptrPARAM0x19 = (unsigned int *)0x02704320; // ((*((EDMA3_CCRL_Regs *) 0x02700000)).PARAMENTRY)[24]
116         Log_info5("PARAM0x18a(%d): 0x%x 0x%x 0x%x 0x%x", tag_place, ptrPARAM0x18[0], ptrPARAM0x18[1], ptrPARAM0x18[2], ptrPARAM0x18[3]);
117         Log_info5("PARAM0x18b(%d): 0x%x 0x%x 0x%x 0x%x", tag_place, ptrPARAM0x18[4], ptrPARAM0x18[5], ptrPARAM0x18[6], ptrPARAM0x18[7]);
119         Log_info5("PARAM0x19a(%d): 0x%x 0x%x 0x%x 0x%x", tag_place, ptrPARAM0x19[0], ptrPARAM0x19[1], ptrPARAM0x19[2], ptrPARAM0x19[3]);
120         Log_info5("PARAM0x19b(%d): 0x%x 0x%x 0x%x 0x%x", tag_place, ptrPARAM0x19[4], ptrPARAM0x19[5], ptrPARAM0x19[6], ptrPARAM0x19[7]);
121     //Log_info1("TCC0: ERR reg %x", *((unsigned int *)0x02760120)); //DJDBG
123 // .............................................................................
124 // notes:
125 //  . add control function to PORT table
126 //  . how to handle DMA/PORT specifics in parameter entries
127 //      can assume numSers = numChans is general and can be applied by DMA
128 //      same for wordSize?
129 //  . why are two idle stages needed (seems like 1 is enough)?
131 // .............................................................................
132 // only one global variable, not static so that DMA and port functions
133 // can access. We cant just store the address in devExt since the ISR has
134 // no context.
136 SAP_DriverObject sapDrv;
138 // needed since SAP_watchDog is called before SAP_init
139 Int SAP_initialized = 0;
141 //Int  SAP_close(DEV2_Handle);
142 Int  SAP_ctrl(DEV2_Handle, Uns, Arg);
143 Int  SAP_idle(DEV2_Handle, Bool);
144 Int  SAP_issue(DEV2_Handle);
145 Int  SAP_open(DEV2_Handle, String);
146 void SAP_isrCallback (Uint32 tcc, EDMA3_RM_TccStatus status, Ptr context);
147 //Bool SAP_ready(DEV2_Handle, SEM_Handle);
148 Int  SAP_reclaim(DEV2_Handle);
149 Int  SAP_shutdown(DEV2_Handle);
150 Int  SAP_start(DEV2_Handle);
151 Int  SAP_config(DEV2_Handle device, const SAP_Params *pParams);
152 Int  SAP_EDMA_setupParam (DEV2_Handle device, XDAS_UInt32 targetEdma, XDAS_UInt32 childEdma, unsigned int addr, unsigned int size);
153 Int  SAP_EDMA_setupXfer (DEV2_Handle device, XDAS_UInt32 targetEdma, XDAS_UInt32 parentEdma, XDAS_UInt32 childEdma, DEV2_Frame *pFrame);
155 // .............................................................................
157 // .............................................................................
159 SAP_DMA_Fxns SAP_EDMA_FXNS =
161         SAP_EDMA_setupParam,
162         SAP_EDMA_setupXfer,
163 };
166 SAP_Fxns SAP_FXNS =
168     NULL, //SAP_close, -- remove for IROM since not using
169     SAP_ctrl,
170     SAP_idle,
171     SAP_issue,
172     SAP_open,
173     NULL, //SAP_ready, -- remove for IROM since not using
174     SAP_reclaim,
175     SAP_shutdown,
176     SAP_start,
177     SAP_config,
179 #ifdef SAP_PORT_MCASP
180     (SAP_PORT_Fxns *) &SAP_MCASP_FXNS,
181 #endif
182 #ifdef SAP_DMA_EDMA
183     (SAP_DMA_Fxns *) &SAP_EDMA_FXNS,
184 #endif
185 };
187 // -----------------------------------------------------------------------------
188 // This function is not in the driver function table.
189 // Must be pointed at in GUI config tool.
190 //
191 Void SAP_init (Void)
193     DEV2_Device  *entry;
194     SAP_Fxns    *pFxns;
196     //TRACE_GEN((&TR_MOD, "SAP_init.%d", __LINE__));
198     // find function table pointer (used by SAP_XX_FTABLE_init macros)
199     DEV2_match(SAP_NAME, &entry);
200     if (entry == NULL) {
201         Log_error1 ("SAP", SIO2_ENODEV);
202         return;
203     }
204     pFxns = (SAP_Fxns *) entry->fxns;
206     //SAP_DMA_FTABLE_init ();
207     SAP_PORT_FTABLE_init ();
209     sapDrv.numDevices = 0;
210     SAP_initialized = 1;
212     return;
213 } // SAP_init
215 // -----------------------------------------------------------------------------
217 Int SAP_ctrl (DEV2_Handle device, Uns code, Arg arg)
219     SAP_DeviceExtension *pDevExt = (SAP_DeviceExtension *)device->object;
220     const SAP_Params *pParams;
221     Int result = SIO2_OK;
222     EDMA3_DRV_Handle    hEdma;
223     //TRACE_GEN((&TR_MOD, "SAP_ctrl.%d (0x%x) code = 0x%x", __LINE__, device, code));
225     switch (code) {
227 /* .......................................................................... */
229         case PAF_SIO_CONTROL_MUTE:
230         case PAF_SIO_CONTROL_UNMUTE:
231             pParams = pDevExt->pParams;
232             if (pParams == NULL)
233                 return SIO2_OK;
235             if (pParams->sio.control != NULL)
236                 result = pParams->sio.control (device, (const PAF_SIO_Params *)pParams, code, arg);
237             break;
239 /* .......................................................................... */
241         case PAF_SIO_CONTROL_OPEN:
242             if (pDevExt->runState)
243                 return SIO2_EBUSY;
245             if (!( pParams = (const SAP_Params *) arg ))
246                 return SIO2_OK;
248             if (result = SAP_FTABLE_config (device, pParams))
249                 return result;
251             if (pParams->sio.control && (result = pParams->sio.control (device, (const PAF_SIO_Params *)pParams, code, arg)))
252                 return result;
254             break;
256 /* .......................................................................... */
258         case PAF_SIO_CONTROL_CLOSE:
259             if (pDevExt->runState)
260                 return SIO2_EBUSY;
262             if (pDevExt->pParams == NULL)
263                                 return SIO2_EINVAL;
265             pParams = pDevExt->pParams;
267                         if (pParams->sio.moduleNum == 0)
268                                 hEdma = hEdma0;
269                         else if (pParams->sio.moduleNum == 1 || pParams->sio.moduleNum == 2)
270                                 hEdma = hEdma1;
271             if (pDevExt->activeEdma != EDMA_HINV) {
272                                 EDMA3_DRV_freeChannel (hEdma, pDevExt->activeEdma);
273                             pDevExt->activeEdma = EDMA_HINV;
274                         }
276             if (!(pParams = pDevExt->pParams))
277                 return SIO2_OK;
279             if (pParams->sio.control && (result = pParams->sio.control (device, (const PAF_SIO_Params *)pParams, code, arg)))
280                 return result;
282             result = SAP_PORT_FTABLE_close (device);
283             if (result)
284                 return result;
286             pDevExt->pParams = NULL;
287             break;
289 /* .......................................................................... */
291         case PAF_SIO_CONTROL_GET_WORDSIZE:
292                 if (!arg)
293                                 return SIO2_EINVAL;
294                         *((int *) arg) = pDevExt->edmaWordSize;
295             break;
297         case PAF_SIO_CONTROL_SET_WORDSIZE:
298             // defer to DMA processing
299                 // currently only supported for input
300                         if (device->mode != DEV2_INPUT)
301                                 return SIO2_EINVAL;
303                         // can't be running
304                         if (pDevExt->runState)
305                                 return SIO2_EBUSY;
307                         // driver only supports 2 or 4 bytes
308                    if ((arg != 2) && (arg != 4))
309                                 return SIO2_EINVAL;
311                         // return success for unconfigured devices?
312                         if (!pDevExt->pParams)
313                                 return SIO2_OK;
315                         // ask platform if size is supported
316                         pParams = pDevExt->pParams;
317                         if (pDevExt->pParams->sio.control && (result = pDevExt->pParams->sio.control (device, (const PAF_SIO_Params *)pParams, code, arg)))
318                                 return result;
320                         pDevExt->edmaWordSize = arg;
321                         break;
323         case PAF_SIO_CONTROL_GET_PRECISION:
324             if (arg == 0)
325                 return SIO2_EINVAL;
327             pParams = pDevExt->pParams;
328             if (pParams == NULL)
329                 return( SIO2_EINVAL );
331             *((int *) arg) = pParams->sio.precision;
332             break;
334         case PAF_SIO_CONTROL_GET_NUMCHANNELS:
335             if (arg == 0)
336                 return SIO2_EINVAL;
338             *((int *) arg) = pDevExt->numSlots * pDevExt->numSers;
339             break;
341         case PAF_SIO_CONTROL_SET_RATEX:
342             pParams = pDevExt->pParams;
343             if (pParams == NULL)
344                 return SIO2_OK ;
346             if (pParams->sio.control == NULL)
347                 return SIO2_EINVAL;
349             result = pParams->sio.control( device, (const PAF_SIO_Params *)pParams, PAF_SIO_CONTROL_SET_RATEX, arg);
350             break;
352 /* .......................................................................... */
354         case PAF_SIO_CONTROL_IDLE:
355             pParams = pDevExt->pParams;
356             if (pParams == NULL)
357                 return SIO2_OK ;
359             if (pParams->sio.control == NULL)
360                 return SIO2_EINVAL;
362             result = pParams->sio.control( device, (const PAF_SIO_Params *)pParams, PAF_SIO_CONTROL_IDLE, arg);
363             break;
365         case PAF_SIO_CONTROL_IDLE_WITH_CLOCKS:
366             // 1. Here we are intentionally not using SIO_Idle() and
367             //    leaving the Tx clock running. We need this to avoid DAC noise,
368             //    as well as provide a DIT clock when using digital output.
369             if (device->mode != DEV2_OUTPUT || pDevExt->pParams == NULL)
370                 return SIO2_EINVAL;
372             pParams = pDevExt->pParams;
374             if (pParams->sio.moduleNum == 0)
375                 hEdma = hEdma0;
376             else if (pParams->sio.moduleNum == 1 || pParams->sio.moduleNum == 2)
377                                 hEdma = hEdma1;
379             result = SAP_FTABLE_shutdown (device);
380             if (result)
381                 return result;
383             Log_info0 ("SAP PAF_SIO_CONTROL_IDLE_WITH_CLOCKS; PAF_SIO_ERROR_IDLE_STAGE1");
384             pDevExt->errorState = PAF_SIO_ERROR_IDLE_STAGE1;
386 #if 1
387             //DJDBG, if below enableTransfer() is commented, input side continuous working.
388      if (pDevExt->activeEdma != EDMA_HINV) {
389                 //EDMA3_DRV_disableTransfer (hEdma0, pDevExt->activeEdma, EDMA3_DRV_TRIG_MODE_EVENT);
390             //if(*((unsigned int *)0x02701000) & 0x01000000) *((unsigned int *)0x02701008) = 0x01000000; //Clear pending even in bit 24! //DJDBG
391             EDMA3_DRV_enableTransfer (hEdma, pDevExt->activeEdma, EDMA3_DRV_TRIG_MODE_EVENT);
392      }
393 #endif
394             //TRACE_GEN((&TR_MOD, "SAP_ctrl.%d: (0x%x) errorState = PAF_SIO_ERROR_IDLE_STAGE1 0x%x.", __LINE__, device, PAF_SIO_ERROR_IDLE_STAGE1));
396             break;
398 /* .......................................................................... */
400         case PAF_SIO_CONTROL_GET_INPUT_STATUS:
401             // needs to be attached
402             pParams = pDevExt->pParams;
403             if (pParams == NULL)
404                 return SIO2_OK;
406             if (pParams->sio.control == NULL)
407                 return SIO2_EINVAL;
409             result = pParams->sio.control( device, (const PAF_SIO_Params *)pParams, code, arg );
410             break;
412         case PAF_SIO_CONTROL_WATCHDOG:
413             pParams = pDevExt->pParams;
414             if (pParams == NULL)
415                 return SIO2_OK;
416             if (pParams->sio.control && (result = pParams->sio.control (device, (const PAF_SIO_Params *)pParams, code, arg)))
417                 return result;
418             break;
419             
420 /* .......................................................................... */
422         // Timing stats specific to DMA engine
423         case PAF_SIO_CONTROL_ENABLE_STATS:
424         case PAF_SIO_CONTROL_DISABLE_STATS:
425         case PAF_SIO_CONTROL_GET_STATS:
426         case PAF_SIO_CONTROL_GET_NUM_EVENTS:
427         case PAF_SIO_CONTROL_GET_NUM_REMAINING:
428             //result = SAP_DMA_FTABLE_ctrl (device, code, arg);
429             // TRACE_VERBOSE((&TR_MOD, "SAP_ctrl: (0x%x) code 0x%x.  result 0x%x.", device, code, result));
430             break;
432 /* .......................................................................... */
434         case PAF_SIO_CONTROL_SET_DITSTATUS:
435             if(device->mode == DEV2_OUTPUT)
436             {
437                 const SAP_Params *pParams = pDevExt->pParams;
438                 MCASP_Handle hPort = sapMcaspDrv.hPort[pParams->sio.moduleNum];
439                 volatile Uint32 *base = (volatile Uint32 *)(hPort->baseAddr);
440                 MCASP_ConfigXmt *pTxConfig = (MCASP_ConfigXmt *)pParams->sio.pConfig;
441                 int encSelect = *((int *) arg);
443                 // HACK -- determine DIT need by FXWID
444                 if (((pTxConfig->afsxctl & _MCASP_AFSXCTL_FXWID_MASK)>> _MCASP_AFSXCTL_FXWID_SHIFT) == MCASP_AFSXCTL_FXWID_BIT)
445                 {
446                     if ( (encSelect == 0x13) ||
447                          (encSelect == 0xa)  || 
448                          (encSelect == 0x6)) // DTE, DDE, MPE 
449                     {
450                         base[_MCASP_DITCSRA0_OFFSET] |= 2;
451                         base[_MCASP_DITCSRB0_OFFSET] |= 2;
452                     }
453                     else
454                     {
455                         base[_MCASP_DITCSRA0_OFFSET] &= 0xfffffffd;
456                         base[_MCASP_DITCSRB0_OFFSET] &= 0xfffffffd;
457                     }
458                 }
460                 pParams = pDevExt->pParams;
461                 if (pParams == NULL)
462                     return SIO2_OK;
464                 if (pParams->sio.control != NULL)
465                     result = pParams->sio.control (device, (const PAF_SIO_Params *)pParams, code, arg);
466             }
467             break;
469 /* .......................................................................... */
471     }
473     return result;
474 } // SAP_ctrl
476 // -----------------------------------------------------------------------------
478 Int SAP_idle (DEV2_Handle device, Bool flush)
480     SAP_DeviceExtension *pDevExt = (SAP_DeviceExtension *)device->object;
481     Int result = SIO2_OK;
482     EDMA3_DRV_Handle hEdma;
484     // do nothing if already idled or unattached
485     if ((!pDevExt->runState) || (pDevExt->pParams == NULL))
486         return result;
488         if (pDevExt->pParams->sio.moduleNum == 0)
489                 hEdma = hEdma0;
490         else if (pDevExt->pParams->sio.moduleNum == 1 || pDevExt->pParams->sio.moduleNum == 2)
491                 hEdma = hEdma1;
493     // reset serial port -- stop generating sync events
494     result = SAP_PORT_FTABLE_reset (device);
495     if (result)
496     {
497         //TRACE_VERBOSE((&TR_MOD, "%s.%d: SAP_PORT_FTABLE_reset returned %d.\n", __FUNCTION__, __LINE__, result));
498         return result;
499     }
501     pDevExt->shutDown = 0; // force shutdown to run
502     result = SAP_FTABLE_shutdown (device);
503     if (result)
504     {
505         //TRACE_VERBOSE((&TR_MOD, "%s.%d: SAP_FTABLE_shutdown returned %d.\n", __FUNCTION__, __LINE__, result));
506         return result;
507     }
509     Log_info0("SAP_idle:Before EDMA3_DRV_disableTransfer");
511     // disable interrupts and EDMA servicing
512    if (pDevExt->activeEdma != EDMA_HINV)
513            EDMA3_DRV_disableTransfer (hEdma, pDevExt->activeEdma, EDMA3_DRV_TRIG_MODE_EVENT);
515     pDevExt->numQueued = 0;
517     // signal stopped
518     pDevExt->runState = 0;
520     // reset errorState
521     pDevExt->errorState = PAF_SIO_ERROR_NONE;
522     //TRACE_VERBOSE((&TR_MOD, "SAP_ctrl.%d: errorState = PAF_SIO_ERROR_NONE 0x%x.", __LINE__, PAF_SIO_ERROR_NONE));
524     // place call to physical device
525     if ((pDevExt->pParams != NULL) && (pDevExt->pParams->sio.control != NULL))
526         result = pDevExt->pParams->sio.control(device, (const PAF_SIO_Params *)pDevExt->pParams, PAF_SIO_CONTROL_IDLE, 0);
528     return result;
529 } // SAP_idle
531 // -----------------------------------------------------------------------------
533 Int SAP_start (DEV2_Handle device)
535     SAP_DeviceExtension *pDevExt = (SAP_DeviceExtension *)device->object;
536     DEV2_Frame *pFrame;
537     int result;
538     EDMA3_DRV_Handle hEdma;
540     //TRACE_GEN((&TR_MOD, "SAP_start.%d (0x%x)", __LINE__, device));
542     // signal we have started
543     //    we change the state here since we have already moved a frame from the 
544     //    todevice queue to the xferQue. If an error occurs during one of the 
545     //    following resets/enables then we need to have runState !=0 in order
546     //    for SAP_idle to properly cleanup. Moreover, the following resets/enables
547     //    do not (and are now required not to) depend on runState being 0.
548     //pDevExt->runState = 1;
549     // Assume todevice queue is not empty -- how else could we be here?
550     pFrame = (DEV2_Frame *) Queue_get (device->todevice);
552     // inidicate this xfer did not use param entry - just the active one
553     pFrame->misc = NULL;
554     if (pDevExt->pParams->sio.moduleNum == 0)
555         hEdma = hEdma0;
556     else if (pDevExt->pParams->sio.moduleNum == 1 || pDevExt->pParams->sio.moduleNum == 2)
557                 hEdma = hEdma1;
559     // non-atomic functions since not running yet.
560     Queue_enqueue (Queue_handle(&pDevExt->xferQue), (Queue_Elem *)pFrame);
562     // initialize count
563     pDevExt->numQueued = 1;
565     result = SAP_PORT_FTABLE_reset (device);
566     if (result)
567     {
568         //TRACE_VERBOSE((&TR_MOD, "%s.%d: SAP_PORT_FTABLE_reset returned %d.\n", __FUNCTION__, __LINE__, result));
569         return result;
570     }
572     // enable DMA processing
574     // config active xfer for this buffer
575     result = SAP_EDMA_setupXfer(device, pDevExt->activeEdma, EDMA_HINV, pDevExt->errorEdma, pFrame);
577     //Log_info3("SAP_start.%d, pDevExt->activeEdma 0x%x (pDevExt->errorEdma = 0x%x)",
578       //              __LINE__, pDevExt->activeEdma, pDevExt->errorEdma);
579         // signal we have started -- this must come before last enable to prevent a race
580         // condition where the initial EDMA transfer is very small (e.g. due to startClocks)
581         // and completes before any further instructions in this thread are executed.
582         // This comes before the EDMA enable since, if the # of samples is 1, then the EDMA
583         // will be serviced and generate an interrupt even before the McASP is enabled.
584         pDevExt->runState = 1;
585         pDevExt->shutDown = 0;
586         //Log_info0 ("SAP_start runState=1 & ENABLE TRANSFERS");
587         // enable interrupts and event servicing for this channel
588         EDMA3_DRV_enableTransfer (hEdma, pDevExt->activeEdma, EDMA3_DRV_TRIG_MODE_EVENT);
590     // enable peripheral
591     result = SAP_PORT_FTABLE_enable (device);
593     if (result)
594     {
595         //TRACE_VERBOSE((&TR_MOD, "%s.%d: SAP_PORT_FTABLE_enable returned %d.\n", __FUNCTION__, __LINE__, result));
596         return result;
597     }
599     return SIO2_OK;
600 } // SAP_start
602 int gDmaParamsarray[17][3];
603 int gDmaParamsidx=0;
604 // -----------------------------------------------------------------------------
606 Int SAP_issue (DEV2_Handle device)
608     SAP_DeviceExtension *pDevExt = (SAP_DeviceExtension *)device->object;
609     DEV2_Frame *pFrame;
610     Int result;
611     SAP_EDMA_Param *pParam;
612     XDAS_UInt32 parentEdma;
614     //TRACE_GEN((&TR_MOD, "SAP_issue.%d (0x%x)", __LINE__, device));
616     if ((device->mode == DEV2_OUTPUT) && (pDevExt->errorState >= PAF_SIO_ERROR_ERRBUF_XFER))
617     {
618         /*TRACE_TERSE((&TR_MOD, "SAP_issue.%d, errorState 0x%x (PAF_SIO_ERROR_ERRBUF_XFER = 0x%x)",
619             __LINE__, pDevExt->errorState, PAF_SIO_ERROR_ERRBUF_XFER));*/
620         Log_info3("SAP_issue.%d, PAF_SIO_ERROR_ERRBUF_XFER = 0x%x, mode = 0x%x)",
621                 __LINE__, PAF_SIO_ERROR_ERRBUF_XFER, device->mode );
622         return SIO2_EBADIO;
623     }
625     if ((device->mode == DEV2_INPUT) && pDevExt->errorState)
626         {
627                 Log_info1("SAP_issue: Input Error Trap, with errorState = 0x%x", pDevExt->errorState);
628             return SIO2_EBADIO;
629         }
631     // if not yet running then configure active xfer and start
632     if (pDevExt->runState == 0)
633         return (SAP_FTABLE_start(device));
635     // .........................................................................
636     // here if running
638     // disable device interrupts
639     // TODO: is there an API to just disable the IER bit for this tcc?
640     unsigned int key = Hwi_disable ();
641        /* determine parent EDMA
642           if no xfers in queue and we are running then must be in the
643           error state so link to active channel otherwise link to last
644           transfer queued.
645        */
647        /* here we assume after Tx SIO_idle or overrun, the user
648           will issue, at least, back-to-back issue requests so
649           there should be no problem here.
650        */
651        if ((pDevExt->numQueued <= 1) && (pDevExt->errorState != 2))
652            parentEdma = pDevExt->activeEdma;
653        else {
654            // if here then xferQue has more than one element so ok to use tail
655            // last scheduled transfer must be queue->prev
656            DEV2_Frame *tail = (DEV2_Frame *) Queue_prev ((Queue_Elem *)&pDevExt->xferQue);
657            parentEdma = ((SAP_EDMA_Param *) tail->misc)->hEdmaParam;
658        }
660     // get frame and parameter table to use; ints off => non-atomic OK
661     //     dont need to check for empty queues since were here then todevice
662     //     must have a frame placed there by the SIO_issue layer.
663     //     paramQue must be valid since it is accessed the same as todevice.
664     //     (indirectly -- isr places used items onto paramQue and fromdevice que
665     //      at the same time)
666     // set misc argument to pParam so get enqueue later
667     //pFrame = (DEV2_Frame *) Queue_dequeue (device->todevice);
668     pFrame = Queue_dequeue (device->todevice);
669     pParam = (SAP_EDMA_Param *) Queue_dequeue (Queue_handle(&pDevExt->paramQue));
671    /* if (pFrame->addr == NULL)
672                 SW_BREAKPOINT; */
673     if (pParam->hEdmaParam == NULL)
674         Log_info0("SAP_issue: hEdma value is NULL");
675     // set misc argument to pParam so get enqueue later
676     pFrame->misc = (Arg) pParam;
678     // place on holder queue, ints off => non-atomic OK
679     Queue_enqueue (Queue_handle(&pDevExt->xferQue), (Queue_Elem *) pFrame);
680     if (pFrame->addr) {
681                 if (device->mode == DEV2_INPUT)
682                         Cache_inv (pFrame->addr, pFrame->size, Cache_Type_ALL, TRUE);
683                 else
684                         Cache_wbInv (pFrame->addr, pFrame->size, Cache_Type_ALL, TRUE);
685         }
687     // increment count
688     pDevExt->numQueued += 1;
690     result = SAP_EDMA_setupXfer (device, pParam->hEdmaParam, parentEdma, pDevExt->errorEdma, pFrame);
691     //Log_info4("SAP_issue.%d, EDMA_setupXfer: Target EDMA: 0x%x, Parent Edma: 0x%x Error Edma: 0x%x",
692     //                __LINE__, pParam->hEdma, parentEdma, pDevExt->errorEdma);
694     /*if ((device->mode != DEV2_INPUT) && (gDmaParamsidx <=16))
695         {
696         gDmaParamsarray[gDmaParamsidx][0] = pParam->hEdma;
697                 gDmaParamsarray[gDmaParamsidx][1] = parentEdma;
698                 gDmaParamsarray[gDmaParamsidx++][2] = gisrOutput;
699         } */
701         if ((pDevExt->errorState == PAF_SIO_ERROR_IDLE_STAGE1) && (device->mode == DEV2_OUTPUT))
702                 pDevExt->errorState = PAF_SIO_ERROR_NONE;
704         pDevExt->shutDown = 0;
706     // special case enables when not yet started
707     if (pDevExt->runState == 0) {
708         result = SAP_FTABLE_start (device);
709         if (result) {
710             //SAP_DMA_FTABLE_unlock (device);
711                   Hwi_enable ();
712             return result;
713         }
714     }
715     Hwi_restore (key); //DJDBG
717     return result;
718 } // SAP_issue
720 // -----------------------------------------------------------------------------
722 void swapHdmi(Ptr Input, int size)
725         MdInt L0, L1, L2, L3, R0, R1, R2, R3 = 0;
726         MdInt *p1, *p2;
727         int i=0;
729         for (i=0; i< size; i+=16)
730         {
731                         p1 = (MdInt *)&Input[i];
732                         p2 = p1;
734                         L0 = *p1++;
735                         L1 = *p1++;
736                         L2 = *p1++;
737                         L3 = *p1++;
738                         R0 = *p1++;
739                         R1 = *p1++;
740                         R2 = *p1++;
741                         R3 = *p1++;
743                         *p2++ = L0;
744                         *p2++ = R0;
745                         *p2++ = L1;
746                         *p2++ = R1;
747                         *p2++ = L2;
748                         *p2++ = R2;
749                         *p2++ = L3;
750                         *p2++ = R3;
752         }
754         Log_info3("SAP: Exiting swapHdmi with Frame->Addr: 0x%x, p1->addr: 0x%x, p2->addr: 0x%x ", (MdInt *)Input, p1, p2);
756         /**p2++ = 0xF872;
757         *p2++ = 0x4E1F;
758         *p2++ = 0x0016;
759         *p2++ = 0xEFF0;
760         *p2++ = 0x079E;
761         *p2++ = 0x0003;
762         *p2++ = 0x8401;
763         *p2++ = 0x0101; */
765         return;
768 Int SAP_reclaim (DEV2_Handle device)
770     SAP_DeviceExtension *pDevExt = (SAP_DeviceExtension *)(device->object);
771     Int result, i, oldMask;
772 #ifdef SAP_CACHE_SUPPORT
773     DEV2_Frame *pFrame;
774 #endif
776     //TRACE_GEN((&TR_MOD, "SAP_reclaim.%d (0x%x)", __LINE__, device));
778     // must be running and  error free 
779     if ((!pDevExt->runState) || (pDevExt->errorState))
780     {
781         //TRACE_GEN((&TR_MOD, "SAP_reclaim.%d, not runState: 0x%x", __LINE__, pDevExt->errorState));
782         return SIO2_EBADIO;
783     }
785     // idle if necessary
786         if (pDevExt->errorState == PAF_SIO_ERROR_FATAL) {
787             DEV2_idle (device, 1);
788             return SIO2_EBADIO;
789         }
791        // Log_info0("SAP_reclaim: Before SEM Pend");
793     // wait for ISR to signal block completion
794     //TRACE_VERBOSE((&TR_MOD, "SAP_reclaim.%d wait for ISR to signal block completion", __LINE__));
795     if (!Semaphore_pend(pDevExt->sync, device->timeout))
796     {
797         Log_info0("SAP_reclaim, SYS_ETIMEOUT");
798         return SIO2_ETIMEOUT;
799     }
800     //Log_info1("SAP_reclaim: After SEM Pend for mode: 0x%x", device->mode);
802     // return error (owner must idle)
803     if (pDevExt->errorState == PAF_SIO_ERROR_FATAL)
804     {
805         DEV2_idle (device, 1);
806         //TRACE_TERSE((&TR_MOD, "SAP_reclaim.%d, PAF_SIO_ERROR_FATAL: 0x%x", __LINE__, pDevExt->errorState));
807         return PAF_SIO_ERROR_FATAL;
808     }
810 #ifdef SAP_CACHE_SUPPORT
811     // invalidate CACHE region if input -- use clean since
812     //    Dont clean if was for fill.
813     // since pend returned we know that head of fromdevice is valid
814     pFrame = Queue_head (device->fromdevice);
815     Log_info2("SAP: Inside SAP_Reclaim with From Device Frame->Addr: 0x%x and Frame->Size: %d", pFrame->addr, pFrame->size);
816     if ((device->mode == DEV2_INPUT) && (pFrame->addr != NULL))
817     {
818         Cache_inv (pFrame->addr, pFrame->size, Cache_Type_ALL, TRUE);
820         if(pDevExt->edmaWordSize == 2 && pDevExt->numSers == 4)
821         {
822                 //note: size here is in # of bytes, so incrementing by 4X32b words ( or 8X16b)
823                         //for(i=0; i<(pFrame->size)/(4*4); i+=16)
824                         //{
825                 oldMask = Hwi_disable ();
826                                 swapHdmi(pFrame->addr, pFrame->size);
827                                 Hwi_restore(oldMask);
828                         //}
829         }
831     }
833 #endif
834     /*if ((device->mode == DEV2_OUTPUT) && (pFrame->addr == NULL))
835         SW_BREAKPOINT; */
837     //TRACE_VERBOSE((&TR_MOD, "SAP_reclaim.%d, exit SIO2_OK", __LINE__));
838     return SIO2_OK;
839 } // SAP_reclaim
842 // -----------------------------------------------------------------------------
844 Int SAP_open (DEV2_Handle device, String name)
846     SAP_DeviceExtension   *pDevExt;
847     DEV2_Device            *entry;
848     Int                    oldMask, result;
849     Error_Block                         eb;
850     //TRACE_GEN((&TR_MOD, "SAP_open.%d (0x%x)", __LINE__, device));
852     // check SIO mode 
853     if ((device->mode != DEV2_INPUT) && (device->mode != DEV2_OUTPUT))
854         return SIO2_EMODE;
856     // allocate memory for device extension
857     device->object = NULL;
858     pDevExt = (SAP_DeviceExtension *) Memory_alloc (device->bufSeg, (sizeof(SAP_DeviceExtension)+3)/4*4, 4, &eb);
859     if (pDevExt == NULL)
860     {
861         printf("%s.%d:  MEM_alloc failed.\n", __FUNCTION__, __LINE__);
862         //TRACE_TERSE((&TR_MOD, "%s.%d:  MEM_alloc failed.\n", __FUNCTION__, __LINE__));
863         asm( " SWBP 0" );  // SW Breakpoint
864         return SIO2_EALLOC;
865     }
866     device->object = (Ptr)pDevExt;
868     // inits
869     pDevExt->device = device;
870     pDevExt->sync = NULL;
871     pDevExt->pParams = NULL;
872     pDevExt->runState = 0;  // not yet started
873     pDevExt->errorState = PAF_SIO_ERROR_NONE;
874     pDevExt->shutDown = 1;
875     pDevExt->numQueued = 0;
876     pDevExt->activeEdma = EDMA_HINV;
877     pDevExt->errorEdma = EDMA_HINV;
878     pDevExt->firstTCC = 0;
879     pDevExt->optLevel = 0;
880     pDevExt->numParamSetup = 0;
881     pDevExt->numEdmaParams = 4;
883     // use dev match to fetch function table pointer for SAP
884     DEV2_match(SAP_NAME, &entry);
885     if (entry == NULL) {
886         Log_error1("SAP", SIO2_ENODEV);
887         return SIO2_ENODEV;
888     }
889     pDevExt->pFxns = (SAP_Fxns *) entry->fxns;
891     // create semaphore for device
892     pDevExt->sync = Semaphore_create (0, NULL, NULL);
893     if (pDevExt->sync == NULL)
894     {
895         //TRACE_TERSE((&TR_MOD, "%s.%d: create semaphore for device failed.\n", __FUNCTION__, __LINE__));
896         return SIO2_EALLOC;
897     }
899     // queue inits
900     Queue_construct (&pDevExt->xferQue, NULL);
901     Queue_construct (&pDevExt->paramQue, NULL);
903     // update driver global (need to protect context)
904     if (sapDrv.numDevices >= MAX_SAP_DEVICES)
905     {
906         /*TRACE_TERSE((&TR_MOD, "%s.%d: add device failure: no. of devices = %d; need to increase MAX_SAP_DEVICES.\n",
907             __FUNCTION__, __LINE__, dapDrv.numDevices));*/
908         SW_BREAKPOINT;
909     }
910     oldMask = Hwi_disable ();
911     sapDrv.device[sapDrv.numDevices] = device;
912     pDevExt->deviceNum = sapDrv.numDevices++;
913     Hwi_restore (oldMask);
915     // PORT init
916     result = SAP_PORT_FTABLE_open (device);
917     if (result)
918     {
919         //TRACE_TERSE((&TR_MOD, "%s.%d: SAP_PORT_FTABLE_open returned %d.\n", __FUNCTION__, __LINE__, result));
920         return result;
921     }
923     return result;
924 } // SAP_open
926 // -----------------------------------------------------------------------------
928 Int SAP_config (DEV2_Handle device, const SAP_Params *pParams)
930     SAP_DeviceExtension *pDevExt = (SAP_DeviceExtension *)device->object;
931     Int                  result, Que_num, i;
932     EDMA3_DRV_Result     edmaResult;
933     Uint32                                      reqTcc;
934     EDMA3_DRV_Handle    hEdma;
935     Log_info2("SAP_config.%d (0x%x)", __LINE__, device);
937     // cannot configure if transfer started
938     if (pDevExt->runState == 1)
939         return SIO2_EBADIO;
941     // save pointer to config structure in device extension. here so that
942     //   forthcoming functions can use/modify config structure.
943     pDevExt->pParams = pParams;
944     pDevExt->edmaWordSize = pParams->sio.wordSize;
946     // allocate Port resources.
947     //    This must come before DMA configuration
948     result = SAP_PORT_FTABLE_alloc (device);
949     if (result)
950     {
951         Log_info3("%s.%d: SAP_PORT_FTABLE_alloc returned %d.\n", (xdc_IArg) __FUNCTION__, __LINE__, result);
952         return result;
953     }
955     // .............................................................................
956     // EDMA configuration
958     // DA10x McASP0 Specific
959     if (pParams->sio.moduleNum == 0)
960     {
961         hEdma = hEdma0;
962         if (device->mode == DEV2_INPUT)
963         {
964                 Que_num = 0;
965                 pDevExt->activeEdma = CSL_EDMACC_0_McASP_0_REVT;
966         }
967         else
968         {
969                 Que_num = 1;
970                 pDevExt->activeEdma = CSL_EDMACC_0_McASP_0_XEVT;
971         }
972     }
973     // DA10x McASP1 Specific
974     else if (pParams->sio.moduleNum == 1)
975     {
976         hEdma = hEdma1;
977         if (device->mode == DEV2_INPUT)
978                 {
979                 Que_num = 0;
980                 pDevExt->activeEdma = CSL_EDMACC_1_McASP_1_REVT;
981                 }
982                 else
983                 {
984                         Que_num = 1;
985                         pDevExt->activeEdma = CSL_EDMACC_1_McASP_1_XEVT;
986                 }
987     }
988     // DA10x McASP2 Specific
989     else if (pParams->sio.moduleNum == 2)
990     {
991         hEdma = hEdma1;
992         if (device->mode == DEV2_INPUT)
993                 {
994                         Que_num = 0;
995                         pDevExt->activeEdma = CSL_EDMACC_1_McASP_2_REVT;
996                 }
997                 else
998                 {
999                         Que_num = 1;
1000                         pDevExt->activeEdma = CSL_EDMACC_1_McASP_2_XEVT;
1001                 }
1002     }
1005     for (i=0; i < pDevExt->numEdmaParams; i++) {
1007         reqTcc = EDMA3_DRV_TCC_ANY;
1008         pDevExt->edmaParams[i].hEdmaParam = EDMA3_DRV_LINK_CHANNEL;
1009         edmaResult = EDMA3_DRV_requestChannel (
1010                 hEdma,
1011             &pDevExt->edmaParams[i].hEdmaParam,
1012             &reqTcc,
1013             (EDMA3_RM_EventQueue) Que_num,
1014             SAP_isrCallback,
1015             (void *) device);
1017         if (edmaResult != EDMA3_DRV_SOK)
1018             return SIO2_EALLOC;
1020         //not running => can use non-atomic functions
1021         Queue_enqueue (Queue_handle(&pDevExt->paramQue), (Queue_Elem *)&pDevExt->edmaParams[i]);
1023     }
1025     reqTcc = EDMA3_DRV_TCC_ANY;
1026         pDevExt->errorEdma = EDMA3_DRV_LINK_CHANNEL;
1027         edmaResult = EDMA3_DRV_requestChannel (
1028             hEdma,
1029             &pDevExt->errorEdma,
1030             &reqTcc,
1031             (EDMA3_RM_EventQueue)Que_num,
1032             SAP_isrCallback,
1033             (void *) device);
1034         if (edmaResult != EDMA3_DRV_SOK)
1035             return SIO2_EALLOC;
1037         // allocate edma channel -- also disable and clear the interrupt
1040         pDevExt->firstTCC = pDevExt->activeEdma ;
1041         edmaResult = EDMA3_DRV_requestChannel (
1042                 hEdma,
1043                 &pDevExt->activeEdma,
1044                 &pDevExt->firstTCC,
1045                 (EDMA3_RM_EventQueue) 0,
1046                 SAP_isrCallback,
1047                 (void *) device);
1048         if (edmaResult != EDMA3_DRV_SOK)
1049                     {
1050                         Log_info3("%s.%d: SAP_DMA_FTABLE_alloc returned %d.\n", (xdc_IArg)__FUNCTION__, __LINE__, edmaResult);
1051                         return SIO2_EALLOC;
1052                     }
1054         // Configure error transfer
1055         //   make cnt same as # of channels in order to maintain alignment
1056         //   and the error transfer small so that we never have to wait
1057         //   long for it to complete and trigger a linked transfer. This is
1058         //   important for establishing output timing when we are idling with
1059         //   clocks still running. Is fine for Rx as well.
1060         result = SAP_DMA_FTABLE_setupParam (device, pDevExt->errorEdma, pDevExt->errorEdma, NULL, pDevExt->edmaWordSize * pDevExt->numSers);
1062         Log_info3("%s.%d: Exiting SAP_alloc for %d.\n", (xdc_IArg)__FUNCTION__, __LINE__, pDevExt->activeEdma);
1064     return SIO2_OK;
1065 } // SAP_config
1067 // -----------------------------------------------------------------------------
1069 Int SAP_shutdown (DEV2_Handle device)
1071     SAP_DeviceExtension *pDevExt = (SAP_DeviceExtension *)(device->object);
1072     SIO2_Handle stream = (SIO2_Handle) device;
1073     DEV2_Frame *pFrame;
1074     Int result,i;
1075     EDMA3_DRV_Handle hEdma;
1076     //TRACE_GEN((&TR_MOD, "SAP_shutdown.%d (0x%x)", __LINE__, device));
1078     if (pDevExt->shutDown)
1079         return SIO2_EBADIO;
1081     if (pDevExt->pParams == NULL)
1082         return SIO2_EINVAL;
1084     if (pDevExt->pParams->sio.moduleNum == 0)
1085         hEdma = hEdma0;
1086     else if (pDevExt->pParams->sio.moduleNum == 1 || pDevExt->pParams->sio.moduleNum == 2)
1087         hEdma = hEdma1;
1088     if (pDevExt->activeEdma != EDMA_HINV)
1089         EDMA3_DRV_disableTransfer (hEdma, pDevExt->activeEdma, EDMA3_DRV_TRIG_MODE_EVENT);
1091     // reset queues
1092     while (!Queue_empty(device->todevice)) {
1093        // place oustanding requests onto holding queue
1094        pFrame = (DEV2_Frame *) Queue_dequeue (device->todevice);
1095        Queue_enqueue (Queue_handle(&pDevExt->xferQue), (Queue_Elem *) pFrame);
1096     }
1098     while (!Queue_empty(Queue_handle(&pDevExt->xferQue))) {
1099         // pull frame from holding queue and place on user queue
1100         pFrame = (DEV2_Frame *) Queue_dequeue (Queue_handle(&pDevExt->xferQue));
1101         Queue_enqueue (device->fromdevice, (Queue_Elem *) pFrame);
1102     }
1105     while (!Queue_empty(Queue_handle(&pDevExt->paramQue)))
1106         Queue_dequeue (Queue_handle(&pDevExt->paramQue));
1108     // not running => can use non-atomic functions
1109     for (i=0; i < pDevExt->numEdmaParams; i++)
1110         Queue_enqueue (Queue_handle(&pDevExt->paramQue), (Queue_Elem *) &pDevExt->edmaParams[i]);
1112     // reset counter
1113     pDevExt->numQueued = 0;
1115     // make sure active is linked to error
1116         EDMA3_DRV_linkChannel (hEdma, pDevExt->activeEdma, pDevExt->errorEdma);
1118     // think this is better (from SIO_idle for standard model )
1119     // refill frame list -- so user needn't call reclaim, which may cause Rx underrun.
1120     while (!Queue_empty(device->fromdevice)) {
1121         /* place oustanding requests onto holding queue */
1122         pFrame = (DEV2_Frame *) Queue_dequeue (device->fromdevice);
1123         Queue_enqueue (Queue_handle(&stream->framelist), (Queue_Elem *) pFrame);
1124     }
1125     Semaphore_reset (pDevExt->sync, 0);
1126     
1127     pDevExt->shutDown = 1;
1128     pDevExt->numParamSetup = 0;
1129     
1130     /*result = SAP_DMA_FTABLE_unlock (device);
1131     if (result)
1132     {
1133         //TRACE_TERSE((&TR_MOD, "%s.%d: SAP_DMA_FTABLE_unlock returned %d.\n", __FUNCTION__, __LINE__, result));
1134         return result;
1135     } */
1137     return SIO2_OK;
1138 } // SAP_shutdown
1140 // -----------------------------------------------------------------------------
1141 int gSapWatchDogThrottle = 0; //DJDBG
1142 Void SAP_watchDog (Void)
1144     DEV2_Handle device;
1145     SAP_DeviceExtension *pDevExt;
1146     int i, oldMask, result;
1148     //Log_info2("SAP_watchDog.%d (0x%x)", __LINE__, device);
1150     // do nothing if SAP_init not yet called
1151     if (!SAP_initialized)
1152     {
1153         Log_info2("%s.%d: SAP_init not yet called.\n", __FUNCTION__, __LINE__);
1154         return;
1155     }
1157     // protect context
1158     Task_disable (); // needed since we may call SEM_post
1159     oldMask = Hwi_disable ();
1161     //TRACE_VERBOSE((&TR_MOD, "%s.%d: devices loop, numDevices = %d", __FUNCTION__, __LINE__, dapDrv.numDevices));
1163     for (i=0; i < sapDrv.numDevices; i++) {
1164         device  = sapDrv.device[i];
1166         //TRACE_VERBOSE((&TR_MOD, "%s.%d, devices loop start, device = 0x%x", __FUNCTION__, __LINE__, device));
1168         pDevExt = (SAP_DeviceExtension *) device->object;
1170         // do nothing if not running
1171         if (!pDevExt->runState)
1172             continue;
1174         // call board specific watchdog
1175         // TODO: handle return value
1176         SIO2_ctrl (device, PAF_SIO_CONTROL_WATCHDOG, NULL);
1177         
1178         // if port layer returns error then must need to clean up
1179         result = SAP_PORT_FTABLE_watchDog (device);
1180         if (result) {
1181             // set errorState which will force owner thread
1182             //   to clean up via SIO_idle()
1183             pDevExt->errorState = PAF_SIO_ERROR_FATAL;
1184             //TRACE_TERSE((&TR_MOD, "SAP_watchDog.%d, PAF_SIO_ERROR_FATAL: 0x%x", __LINE__, pDevExt->errorState));
1185            /* if(gSapWatchDogThrottle == 0) //DJDBG
1186             {
1187                Log_info3("SAP_watchDog.%d (0x%x); THROTTLED result = 0x%x", __LINE__, device, result);
1188             }
1189             gSapWatchDogThrottle ++;
1190             if(gSapWatchDogThrottle > 10) gSapWatchDogThrottle = 0; */
1191             // if outstanding pend then post to free owner thead
1192             if (!Semaphore_pend(pDevExt->sync, 0))
1193                 Semaphore_post (pDevExt->sync);
1194         }
1195     }
1197     // renable interrupts and task manager.
1198     // If we posted to the semaphore then the TSK_enable call will lead to
1199     // an immediate task switch to the associated audio thread.
1200     Hwi_restore (oldMask);
1201     Task_enable ();
1203 } // SAP_watchDog
1205 // -----------------------------------------------------------------------------
1206 // Assumes that EDMA3 dispatcher handles TCC clearing.
1208 void SAP_isrCallback (Uint32 tcc, EDMA3_RM_TccStatus status, Ptr context)
1210     DEV2_Handle                 device;
1211     SAP_DeviceExtension       *pDevExt;
1212     DEV2_Frame                 *pFrame;
1213     unsigned int               opt;
1214     EDMA3_DRV_Handle                    hEdma;
1216     // could be here after Tx idle/overrun and this is the interrupt
1217     // for the last occuring error transfer so there is no transfer
1218     // to release, we just clear the int and exit.
1220     device = (DEV2_Handle) context;
1221     pDevExt = (SAP_DeviceExtension *)(device->object);
1222     if (pDevExt->pParams == NULL)
1223         return SIO2_EINVAL;
1225     if (pDevExt->pParams->sio.moduleNum == 0)
1226         hEdma = hEdma0;
1227     else if (pDevExt->pParams->sio.moduleNum == 1 || pDevExt->pParams->sio.moduleNum == 2)
1228         hEdma = hEdma1;
1230     if ((pDevExt->runState == 1) && !pDevExt->errorState) {
1231         // if here then an interrupt occured due to errorEdma or valid
1232         // transfer, we assume the xfer is long enough so it will not complete
1233         // before we are finished here.
1235         // if last transfer was valid then complete it
1236         if (!Queue_empty(Queue_handle(&pDevExt->xferQue))) {
1238             // pull frame from holding queue
1239             pFrame = (DEV2_Frame *) Queue_dequeue (Queue_handle(&pDevExt->xferQue));
1241             // if used param entry then return it to queue
1242             if (pFrame->misc != NULL)
1243             {
1244                 Queue_enqueue (Queue_handle(&pDevExt->paramQue), (Ptr) pFrame->misc);
1245                 if (device->mode == 1)
1246                         gisrOutput+=100;
1247             }
1249             // decrement count
1250             pDevExt->numQueued -= 1;
1251             gIsrCnt++;
1252             if (device->mode == 1)
1253                 gisrOutput++;
1255             // place frame onto user queue and signal user thread
1256             Queue_enqueue (device->fromdevice, (Ptr) pFrame);
1258             Log_info2("Before SEM_post for device: 0x%x gIsrOutput: %d", device->mode, gisrOutput);
1259             // signal user thread
1260             Semaphore_post (pDevExt->sync);
1261 #if 0
1262             if(gIsrCnt > 10) { //DJDBG
1263                Log_info1("SAP isrCallback enough interrupts! %d", gIsrCnt);
1265             }
1266 #endif
1267         }
1268         else
1269                 gIsrElseCnt++;
1271         // determine if currently transferring buffer is valid based on interrupt enable bit
1272         // only valid transfers will generate interrupts
1273         EDMA3_DRV_getPaRAMEntry (hEdma, pDevExt->activeEdma, EDMA3_DRV_PARAM_ENTRY_OPT, &opt);
1275         if (!(opt & EDMA3_DRV_OPT_TCINTEN_SET_MASK (1)))
1276         {
1277                 gIsrErrCnt++;
1278                 pDevExt->errorState = PAF_SIO_ERROR_ERRBUF_XFER;
1279         }
1281     } // runState
1282     else
1283     {
1284                 if (pDevExt->runState != 1)
1285                         gIsrRunCnt++;
1286                 else
1287                         gIsrNotRunCnt++;
1288     }
1290     return;
1291 } //SAP_isrCallback
1293 // -----------------------------------------------------------------------------
1295 Int SAP_EDMA_setupXfer (DEV2_Handle device, XDAS_UInt32 targetEdma, XDAS_UInt32 parentEdma, XDAS_UInt32 childEdma, DEV2_Frame *pFrame)
1297     EDMA3_DRV_Handle            hEdma;
1298     SAP_DeviceExtension *pDevExt = (SAP_DeviceExtension *)device->object;
1299 //     int mcbspNum = pDevExt->pParams->sio.moduleNum;
1301         if (pDevExt->pParams == NULL)
1302                 return SIO2_EINVAL;
1304             if (pDevExt->pParams->sio.moduleNum == 0)
1305                 hEdma = hEdma0;
1306             else if (pDevExt->pParams->sio.moduleNum == 1 || pDevExt->pParams->sio.moduleNum == 2)
1307                 hEdma = hEdma1;
1309     // TODO: shouldn't this just be tcc interrupt disable?
1310     // at least until linkage phase...
1311     unsigned int key = Hwi_disable (); //DJDBG
1313     if(targetEdma == NULL)
1314     {
1315         Log_info0("targetEdma is NULL");
1316     }
1317     // configure transfer
1318     if(pFrame->addr == NULL) //DJDBG
1319     {
1320         Log_info0("pFrame has NULL address?");
1321     }
1322     SAP_DMA_FTABLE_setupParam (device, targetEdma, childEdma, pFrame->addr, pFrame->size);
1324     if (parentEdma != EDMA_HINV)
1325         EDMA3_DRV_linkChannel (hEdma, parentEdma, targetEdma);
1328     Hwi_restore (key); //DJDBG
1330     return SIO2_OK;
1331 } // SAP_setupXfer
1333 // -----------------------------------------------------------------------------
1334 // Configure EDMA3 parameter entry
1336 Int SAP_EDMA_setupParam (DEV2_Handle device, XDAS_UInt32 targetEdma, XDAS_UInt32 childEdma, unsigned int addr, unsigned int size)
1338     SAP_DeviceExtension *pDevExt = (SAP_DeviceExtension *)device->object;
1339     EDMA3_DRV_Handle            hEdma;
1340     EDMA3_DRV_PaRAMRegs  edmaConfig;
1342     if (pDevExt->pParams == NULL)
1343                 return SIO2_EINVAL;
1345         if (pDevExt->pParams->sio.moduleNum == 0)
1346                 hEdma = hEdma0;
1347         else if (pDevExt->pParams->sio.moduleNum == 1 || pDevExt->pParams->sio.moduleNum == 2)
1348                 hEdma = hEdma1;
1349     MCASP_Handle hPort = sapMcaspDrv.hPort[pDevExt->pParams->sio.moduleNum];
1350     volatile Uint32 *base = (volatile Uint32 *)(hPort->baseAddr);
1352    //Log_info3("%s.%d: Entered SAP_EDMA_setupParam for Target: 0x%x.\n", (xdc_IArg)__FUNCTION__, __LINE__, targetEdma);
1354     // Init opt parameter to 0 which, without being overriden, configures as:
1355     //    A synchronized transfer (no FIFO mode on src or dst)
1356     //    no chaining or intermediate interrupts
1357     //    param is not static
1358     //    normal completion
1359     //    don't generate an interrupt (overriden below for regular xfers)
1360     edmaConfig.opt = 0;
1362     // not transferring blocks so c index is 0
1363     edmaConfig.destCIdx = 0;
1364     edmaConfig.srcCIdx = 0;
1366     edmaConfig.opt |= EDMA3_DRV_OPT_SYNCDIM_SET_MASK (EDMA3_DRV_SYNC_AB); //DJDBG!
1367     if (device->mode == DEV2_OUTPUT) {
1368       //edmaConfig.opt |= EDMA3_DRV_OPT_DAM_SET_MASK (EDMA3_DRV_ADDR_MODE_FIFO); //DJDBG!!!
1369       edmaConfig.opt |= 2;
1370     }
1371     else {
1372         //edmaConfig.opt |= EDMA3_DRV_OPT_SAM_SET_MASK (EDMA3_DRV_ADDR_MODE_FIFO); //DJDBG!!!
1373         //edmaConfig.opt |= 1;
1374     }
1376     // if regular transfer then enable interrupt with tcc code
1377     if (targetEdma != pDevExt->errorEdma) {
1378         edmaConfig.opt |= EDMA3_DRV_OPT_SYNCDIM_SET_MASK (EDMA3_DRV_SYNC_AB);
1379         edmaConfig.opt |= EDMA3_DRV_OPT_TCINTEN_SET_MASK (1);
1380         edmaConfig.opt |= EDMA3_DRV_OPT_TCC_SET_MASK (pDevExt->firstTCC);
1381     }
1384     edmaConfig.aCnt = 4;
1385         edmaConfig.bCnt = pDevExt->numSers;
1386         edmaConfig.cCnt = size/(edmaConfig.aCnt * edmaConfig.bCnt);
1387     edmaConfig.bCntReload = edmaConfig.bCnt;
1390     // handle direction specific requirements
1391     if (device->mode == DEV2_INPUT) {
1392         edmaConfig.srcBIdx  = 0;
1393                 edmaConfig.srcAddr  = (unsigned int) (hPort->rbufAddr);
1395         if (addr) {
1396             edmaConfig.destBIdx = pDevExt->edmaWordSize;
1397             edmaConfig.destAddr = addr;
1398             edmaConfig.destCIdx  = pDevExt->edmaWordSize * pDevExt->numSers ;
1399             if(pDevExt->edmaWordSize == 2)
1400                 edmaConfig.cCnt= (size)/((edmaConfig.aCnt * edmaConfig.bCnt)/2);        // GJ: Account for additional 2-bytes.
1401         }
1402         else {
1403                 //if(pDevExt->edmaWordSize == 2)
1404                 //edmaConfig.srcAddr= (unsigned int)edmaConfig.srcAddr+ 2;
1405             edmaConfig.destBIdx = 0;
1406             edmaConfig.destAddr = (unsigned int) &sap_OVER;
1407         }
1408     }
1409     else {
1410         edmaConfig.destBIdx = 0;
1411         edmaConfig.destAddr = (unsigned int) (hPort->xbufAddr);
1413         if (addr) {
1414             edmaConfig.srcBIdx  = pDevExt->edmaWordSize;
1415             edmaConfig.srcCIdx  = pDevExt->edmaWordSize * pDevExt->numSers ;
1416             edmaConfig.srcAddr  = addr;
1417             Edma3_CacheFlush ((unsigned int) addr, (size+3)/4);
1418         }
1419         else {
1420             edmaConfig.srcBIdx  = 0;
1421             edmaConfig.srcAddr  = (unsigned int) &sap_UNDER[0];
1422 #if 1
1423             //edmaConfig.cCnt = (SAP_UNDER_LEN * sizeof(int))/(edmaConfig.aCnt * edmaConfig.bCnt); //DJDBG
1424             edmaConfig.cCnt = 512; //DJDBG, if underrun have frame of silence
1425 #endif
1426         }
1427     }
1428     edmaConfig.srcAddr  = (unsigned int) getGlobalAddr(edmaConfig.srcAddr);
1429     edmaConfig.destAddr  = (unsigned int) getGlobalAddr(edmaConfig.destAddr);
1431     //Log_info3("SAP: Inside SAP_EDMA_setupParam with targetEdma = 0x%x linked to childEdma = 0x%x & dest-addr: 0x%x", targetEdma, childEdma, edmaConfig.destAddr);
1433     EDMA3_DRV_setPaRAM (hEdma, targetEdma, &edmaConfig);
1435     // link child xfer
1436     if (childEdma != EDMA_HINV)
1437         EDMA3_DRV_linkChannel (hEdma, targetEdma, childEdma);
1439     return SIO2_OK;
1440 } //SAP_setupParam