]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/performance-audio-sr.git/blob - pasdk/test_dsp/application/itopo/evmk2g/mcasp_cfg.c
Started refactoring input task. New code works up to auto-detection which detects
[processor-sdk/performance-audio-sr.git] / pasdk / test_dsp / application / itopo / evmk2g / mcasp_cfg.c
1 /*
2  * Copyright (c) 2015, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
34 /**
35  * \file      mcasp_config.c
36  *
37  * \brief     Configures McASP module
38  *
39  */
41 #include "mcasp_cfg.h"
42 #include "ioConfig.h"
44 #define AUDIO_DAC0_TEST
46 /* McASP device handles */
47 Ptr hMcaspDevTx;
48 Ptr hMcaspDevRx;
50 /* McASP channel handles */
51 Ptr hMcaspTxChan;
52 Ptr hMcaspRxChan;
53 Ptr hMcaspRxChan2;
55 /* McASP channel parameters */
56 Mcasp_Params mcaspTxParams;
57 Mcasp_Params mcaspRxParams;
59 /* McASP Callback function argument */
60 //uint32_t txChanMode;
61 //uint32_t rxChanMode;
63 /* Error flag */
64 uint32_t gblErrFlag = 0;
65 Error_Block eb;
67 void GblErr(Mcasp_errCbStatus arg);
69 /* External function declarations */
70 extern void McaspDevice_init(void);
71 extern signed char*  getGlobalAddr(signed char* addr);
73 #ifdef IO_LOOPBACK_TEST
74 extern void mcaspAppCallbackRx(void* arg, MCASP_Packet *mcasp_packet);
75 extern void mcaspAppCallbackTx(void* arg, MCASP_Packet *mcasp_packet);
76 #else
77 extern void asipMcaspCallback(void* arg, MCASP_Packet *mcasp_packet);
78 extern void asopMcaspCallback(void* arg, MCASP_Packet *mcasp_packet);
80 #endif
82 /* McASP HW setup for receive */
83 Mcasp_HwSetupData mcaspRcvSetup = {
84         MCASP_DIR_RMASK,   /* .rmask:   0xFFFFFFFF    */
85         MCASP_DIR_RFMT,    /* .rfmt:    0x000180F0    */
86         MCASP_DIR_AFSRCTL, /* .afsrctl: 0x00000111    */
87         MCASP_DIR_RTDM,    /* .rtdm:    0x00000003    */
88         MCASP_DIR_RINTCTL, /* .rintctl: 0x00000000    */
89         MCASP_DIR_RSTAT,   /* .rstat:   0x000001FF    */
90         MCASP_DIR_REVTCTL, /* .revtctl  */
91         {
92                         MCASP_DIR_ACLKRCTL,  /* .aclkrctl:  0x00000080  */
93                         MCASP_DIR_AHCLKRCTL, /* .ahclkrctl: 0x00000000  */
94                         MCASP_DIR_RCLKCHK    /* .rclkchk:   0x00000000  */
95         }
96 };
98 /* McASP HW setup for transmit */
99 Mcasp_HwSetupData mcaspXmtSetup = {
100         /* .xmask    = */ 0xFFFFFFFF, /* 16 bits are to be used     */
101         /* .xfmt     = */ 0x000180F0, /*
102                                        * 0 bit delay from framesync
103                                        * MSB first
104                                        * No extra bit padding
105                                        * Padding bit (ignore)
106                                        * slot Size is 32
107                                        * Reads from DMA port
108                                        * NO rotation
109                                        */
110         /* .afsxctl  = */ 0x00000112, /* I2S mode - 2 slot TDM
111                                        * Frame sync is one word
112                                        * Rising edge is start of frame
113                                        * Internally generated frame sync
114                                        */
115         /* .xtdm     = */ 0x00000003, /* slot 1 and 2 are active (I2S)               */
116         /* .xintctl  = */ 0x00000000, /* sync error,overrun error,clK error   */
117         /* .xstat    = */ 0x000001FF, /* reset any existing status bits       */
118         /* .xevtctl  = */ 0x00000000, /* DMA request is enabled or disabled   */
119         {
120              /* .aclkxctl  = */ 0X000000E1,
121              /* .ahclkxctl = */ 0x00004000      ,
122              /* .xclkchk   = */ 0x00000000
123         },
125 };
127 #ifdef INPUT_SPDIF
128 /* McAsp channel parameters for receive - DIR                */
129 Mcasp_ChanParams  mcaspRxChanParam =
131         0x0001,                    /* Number of serializers      */
132         {Mcasp_SerializerNum_5},   /* Serializer index           */
133         &mcaspRcvSetup,
134         TRUE,
135         Mcasp_OpMode_TDM,          /* Mode (TDM/DIT)             */
136         Mcasp_WordLength_32,
137         NULL,
138         0,
139         NULL,
140         GblErr,
141         2,                        /* number of TDM channels      */
142         Mcasp_BufferFormat_1SER_MULTISLOT_INTERLEAVED,
143         TRUE,
144         1,                        /* hwFifoEventDMARatio */
145         TRUE,                     /* isDataPacked */
146         Mcasp_WordBitsSelect_MSB  /* wordBitsSelect */
147 };
148 #endif
150 /* McASP LLD channel parameters for HDMI input:
151  *     When slot size of McASP is configured to 32-bit, HDMI data always come to 16 MSBs of the slot
152  *     and the 16 LSBs are filled with 0's. This is the nature of HDMI and I2S.
153  *     For PCM data, we want all 32 bits in the slot to be transferred to the input buffer:
154  *        - wordWidth = Mcasp_WordLength_32
155  *        - isDataPacked = 1,
156  *        - wordBitsSelect having no effect since wordWidth = slot size
157  *     For bit stream, we want only 16 MSBs in the slot to be transferred to the input buffer:
158  *        - wordWidth = Mcasp_WordLength_16
159  *        - isDataPacked = 1,
160  *        - wordBitsSelect = Mcasp_WordBitsSelect_MSB
161  */
162 #ifdef INPUT_HDMI_4xI2S
163 /* McAsp channel parameters for receive - HDMI 4XI2S layout  */
164 Mcasp_ChanParams  mcaspRxChanParam =
166         0x0004,                    /* number of serializers      */
167         {Mcasp_SerializerNum_12,
168          Mcasp_SerializerNum_13,
169          Mcasp_SerializerNum_14,
170          Mcasp_SerializerNum_15 }, /* serializer index           */
171         &mcaspRcvSetup,
172         TRUE,
173         Mcasp_OpMode_TDM,          /* Mode (TDM/DIT)             */
174 #ifdef INPUT_PCM_ONLY
175         Mcasp_WordLength_32,       /* 32-bit word length, whole slot (size=32) to be transfered */
176 #else
177     Mcasp_WordLength_16,       /* 16-bit word length, MSB or LSB of slot to be transfered, depending on wordBitsSelect */
178 #endif
179         NULL,
180         0,
181         NULL,
182         GblErr,
183         2,                        /* number of TDM channels      */
184         Mcasp_BufferFormat_MULTISER_MULTISLOT_SEMI_INTERLEAVED_1,
185         TRUE,                     /* enableHwFifo */
186         1,                        /* hwFifoEventDMARatio */
187         TRUE,                     /* isDataPacked, only transfer the selected bits of slot, based on wordWidth and wordBitsSelect */
188         Mcasp_WordBitsSelect_MSB  /* wordBitsSelect, only matters if wordWidth < slot size */
189 };
190 #endif
192 #ifdef INPUT_HDMI_STEREO
193 /* McAsp channel parameters for receive - HDMI   1XI2S layout             */
194 Mcasp_ChanParams  mcaspRxChanParam =
196         0x0001,                    /* Number of serializers      */
197         {Mcasp_SerializerNum_12},   /* Serializer index           */
198         &mcaspRcvSetup,
199         TRUE,
200         Mcasp_OpMode_TDM,          /* Mode (TDM/DIT)             */
201         Mcasp_WordLength_32,
202         NULL,
203         0,
204         NULL,
205         GblErr,
206         2,                        /* number of TDM channels      */
207         Mcasp_BufferFormat_1SER_MULTISLOT_INTERLEAVED,
208         TRUE,                     /* enableHwFifo */
209         1,                        /* hwFifoEventDMARatio */
210         TRUE,                     /* isDataPacked */
211         Mcasp_WordBitsSelect_MSB  /* wordBitsSelect */
212 };
213 #endif
215 /* McAsp channel parameters for transmit - DAC0              */
216 #ifndef TX_MCASP_USE_MULT_SER
217 Mcasp_ChanParams  mcaspTx0ChanParam =
219         0x0001,                   /* number of serializers       */
220         {Mcasp_SerializerNum_0}, /* serializer index for DAC0    */
221         &mcaspXmtSetup,
222         TRUE,
223         Mcasp_OpMode_TDM,
224         Mcasp_WordLength_32,      /* word width                  */
225         NULL,
226         0,
227         NULL,
228         GblErr,
229         2,                        /* number of TDM channels      */
230         Mcasp_BufferFormat_1SER_MULTISLOT_INTERLEAVED,
231         TRUE,
232         1,                        /* hwFifoEventDMARatio */
233         TRUE,                     /* isDataPacked */
234         Mcasp_WordBitsSelect_LSB  /* wordBitsSelect */
235 };
236 #else
237 Mcasp_ChanParams  mcaspTx0ChanParam =
239         0x0004,                   /* number of serializers       */
240         {Mcasp_SerializerNum_0,
241          Mcasp_SerializerNum_1,
242          Mcasp_SerializerNum_2,
243          Mcasp_SerializerNum_3 }, /* serializer index for DAC0    */
244         &mcaspXmtSetup,
245         TRUE,
246         Mcasp_OpMode_TDM,
247         Mcasp_WordLength_32,      /* word width                  */
248         NULL,
249         0,
250         NULL,
251         GblErr,
252         2,                        /* number of TDM channels      */
253         Mcasp_BufferFormat_MULTISER_MULTISLOT_SEMI_INTERLEAVED_1,
254         TRUE,
255         1,                        /* hwFifoEventDMARatio */
256         TRUE,                     /* isDataPacked */
257         Mcasp_WordBitsSelect_LSB  /* wordBitsSelect */
258 };
259 #endif
261 /* McAsp channel parameters for transmit - DAC1              */
262 Mcasp_ChanParams  mcaspTx1ChanParam =
264         0x0001,                   /* number of serializers       */
265         {Mcasp_SerializerNum_4},  /* serializer index for DAC0   */
266         &mcaspXmtSetup,
267         TRUE,
268         Mcasp_OpMode_TDM,
269         Mcasp_WordLength_32,      /* word width                  */
270         NULL,
271         0,
272         NULL,
273         GblErr,
274         2,                        /* number of TDM channels      */
275         Mcasp_BufferFormat_1SER_MULTISLOT_INTERLEAVED,
276         TRUE,
277         1,                        /* hwFifoEventDMARatio */
278         TRUE,                     /* isDataPacked */
279         Mcasp_WordBitsSelect_LSB  /* wordBitsSelect */
280 };
282 /* Handle to eDMA */
283 extern EDMA3_DRV_Handle hEdma0;
284 extern EDMA3_DRV_Handle hEdma1;
286 /**
287  *  \brief    Function called by McASP driver in case of error
288  *
289  *  \return    None
290  */
291 void GblErr(Mcasp_errCbStatus arg)
293         gblErrFlag = 1;
297 /* DAC default configuration parameters */
298 DacConfig  DAC_Cfg =
300         AUDK2G_DAC_AMUTE_CTRL_DAC_DISABLE_CMD,   /* Amute event */
301         0,                          /* Amute control */
302         AUDK2G_DAC_SAMPLING_MODE_AUTO,     /* Sampling mode */
303         AUDK2G_DAC_DATA_FORMAT_I2S,        /* Data format */
304         0,                          /* Soft mute control */
305         AUDK2G_DAC_ATTENUATION_WIDE_RANGE, /* Attenuation mode */
306         AUDK2G_DAC_DEEMP_DISABLE,            /* De-emph control */
307         100                          /* Volume */
308 };
310 /**
311  *  \brief    Configures audio DAC module
312  *
313  *  \return    none
314  */
315 void configAudioDAC(void)
317         Audk2g_STATUS status;
319         audk2g_delay(10000);
321         /* Initialize Audio DAC module */
322         status = audioDacConfig(AUDK2G_DAC_DEVICE_ALL, &DAC_Cfg);
323         if(status != Audk2g_EOK)
324         {
325                 //platform_write("Audio DAC Configuration Failed!\n");
326                 //testRet(1);
327         }
330 /**
331  *  \brief   Configures McASP module and creates the channel
332  *           for audio Tx and Rx
333  *
334  *  \return    Audk2g_EOK on Success or error code
335  */
336 Audk2g_STATUS mcaspAudioConfig(void)
338     int32_t status;
340         hMcaspDevTx  = NULL;
341         hMcaspDevRx  = NULL;
342         hMcaspTxChan = NULL;
343         hMcaspRxChan = NULL;
345         /* Initialize McASP Tx and Rx parameters */
346         mcaspTxParams = Mcasp_PARAMS;
347         mcaspRxParams = Mcasp_PARAMS;
349         mcaspTxParams.mcaspHwSetup.tx.clk.clkSetupClk = 0x23;  // not used
350         mcaspTxParams.mcaspHwSetup.rx.clk.clkSetupClk = 0x23;  // not used
351         mcaspRxParams.mcaspHwSetup.rx.clk.clkSetupClk = 0x23;
352         mcaspRxParams.mcaspHwSetup.tx.clk.clkSetupClk = 0x63; // Asynchronous. Separate clock and frame sync used by transmit and receive sections.
354 #ifndef INPUT_SPDIF
355         mcaspRxParams.mcaspHwSetup.glb.pdir |= 0x2000000;   //Special case, since for HDMI input - mcasp0 is both Rx & Tx
356         mcaspRxParams.mcaspHwSetup.glb.amute  = 0x2;            // this to ensure one doesn't overwrite the other (rx/tx)
357         mcaspTxParams.mcaspHwSetup.glb.pdir |= 0x2000000;   //Set Amute pin as output for Tx channel
358         mcaspTxParams.mcaspHwSetup.glb.amute  = 0x2;
359 #else
360         mcaspTxParams.mcaspHwSetup.glb.pdir |= 0x2000000;   //Set Amute pin as output for Tx channel
361         mcaspTxParams.mcaspHwSetup.glb.amute  = 0x2;
362 #endif
364         /* Set the HW interrupt number */
365         //mcaspTxParams.hwiNumber = 8;
366         //mcaspRxParams.hwiNumber = 8;
368         /* Initialize eDMA handle */
369 #ifdef INPUT_SPDIF
370         mcaspRxChanParam.edmaHandle  = hEdma1;
371 #else
372         mcaspRxChanParam.edmaHandle  = hEdma0;
373 #endif
375         mcaspTx0ChanParam.edmaHandle = hEdma0;
376         mcaspTx1ChanParam.edmaHandle = hEdma0;
377     
378 #ifdef INPUT_SPDIF
379         /* Bind McASP0 for Tx */
380         status = mcaspBindDev(&hMcaspDevTx, CSL_MCASP_0, &mcaspTxParams);
381         if((status != MCASP_COMPLETED) || (hMcaspDevTx == NULL))
382         {
383                 //IFPRINT(platform_write("mcaspBindDev for Tx Failed\n"));
384                 return (Audk2g_EFAIL);
385         }
387         /* Bind McASP2 for Rx */
388         status = mcaspBindDev(&hMcaspDevRx, CSL_MCASP_2, &mcaspRxParams);
389         if((status != MCASP_COMPLETED) || (hMcaspDevRx == NULL))
390         {
391                 //IFPRINT(platform_write("mcaspBindDev for Rx Failed\n"));
392                 return (Audk2g_EFAIL);
393         }
395 #else /* HDMI or HDMI_STEREO */
396         /* Bind McASP0 for Rx and Tx */
397         status = mcaspBindDev(&hMcaspDevRx, CSL_MCASP_0, &mcaspRxParams);
398         if((status != MCASP_COMPLETED) || (hMcaspDevRx == NULL))
399         {
400                 //IFPRINT(platform_write("mcaspBindDev for Rx Failed\n"));
401                 return (Audk2g_EFAIL);
402         }
404         hMcaspDevTx = hMcaspDevRx;
405 #endif
407         /* Create McASP channel for Tx */
408         status = mcaspCreateChan(&hMcaspTxChan, hMcaspDevTx,
409                                  MCASP_OUTPUT,
410 #ifdef AUDIO_DAC0_TEST
411                                  &mcaspTx0ChanParam,
412 #else
413                                  &mcaspTx1ChanParam,
414 #endif
415 #ifdef IO_LOOPBACK_TEST
416                                  mcaspAppCallbackTx, NULL);
417 #else
418                                  asopMcaspCallback, NULL);
419 #endif
421         if((status != MCASP_COMPLETED) || (hMcaspTxChan == NULL))
422         {
423                 //IFPRINT(platform_write("mcaspCreateChan for Tx Failed\n"));
424                 return (Audk2g_EFAIL);
425         }
427         /* Create McASP channel for Rx */
428         status = mcaspCreateChan(&hMcaspRxChan, hMcaspDevRx,
429                                  MCASP_INPUT,
430                                  &mcaspRxChanParam,
431 #ifdef IO_LOOPBACK_TEST
432                                  mcaspAppCallbackRx, NULL);
433 #else
434                                                          asipMcaspCallback, NULL);
435 #endif
437         if((status != MCASP_COMPLETED) || (hMcaspRxChan == NULL))
438         {
439                 //IFPRINT(platform_write("mcaspCreateChan for Rx Failed\n"));
440                 return (Audk2g_EFAIL);
441         }
443         return (Audk2g_EOK);
444 } /* mcaspAudioConfig */
446 Audk2g_STATUS mcaspRxDeInit(void)
448         mcaspDeleteChan(hMcaspRxChan);
449         hMcaspRxChan = NULL;
451         mcaspUnBindDev(hMcaspDevRx);
452         hMcaspDevRx = NULL;
454     return (Audk2g_EOK);
457 Audk2g_STATUS mcaspRxReset(void)
459     if(hMcaspRxChan != NULL) {
460                 mcaspDeleteChan(hMcaspRxChan);
461                 hMcaspRxChan = NULL;
462     }
464         return (Audk2g_EOK);
467 Audk2g_STATUS mcaspRxCreate(void)
469     int32_t status;
471         /* Create McASP channel for Rx */
472         status = mcaspCreateChan(&hMcaspRxChan, hMcaspDevRx,
473                                  MCASP_INPUT,
474                                  &mcaspRxChanParam,
475 #ifdef IO_LOOPBACK_TEST
476                                  mcaspAppCallbackRx, NULL);
477 #else
478                                                          asipMcaspCallback, NULL);
479 #endif
481         if((status != MCASP_COMPLETED) || (hMcaspRxChan == NULL))
482         {
483                 //IFPRINT(platform_write("mcaspCreateChan for Rx Failed\n"));
484                 return (Audk2g_EFAIL);
485         }
487         return (Audk2g_EOK);
490 Audk2g_STATUS mcaspTxReset(void)
492     if(hMcaspTxChan != NULL) {
493                 mcaspDeleteChan(hMcaspTxChan);
494                 hMcaspTxChan = NULL;
495     }
497         return (Audk2g_EOK);
500 Audk2g_STATUS mcaspTxCreate(void)
502     int32_t status;
504         /* Create McASP channel for Tx */
505         status = mcaspCreateChan(&hMcaspTxChan, hMcaspDevTx,
506                                  MCASP_OUTPUT,
507                                  &mcaspTx0ChanParam,
508 #ifdef IO_LOOPBACK_TEST
509                                  mcaspAppCallbackTx, NULL);
510 #else
511                                  asopMcaspCallback, NULL);
512 #endif
513         if((status != MCASP_COMPLETED) || (hMcaspTxChan == NULL))
514         {
515                 //IFPRINT(platform_write("mcaspCreateChan for Tx Failed\n"));
516                 return (Audk2g_EFAIL);
517         }
519         return (Audk2g_EOK);
523 Audk2g_STATUS mcaspRecfgWordWidth(Ptr hMcaspChan, uint16_t wordWidth)
525     Mcasp_ChanParams chanParams;
526     int32_t status;
528     chanParams.wordWidth = wordWidth;  //to do: change mcaspControlChan to have wordWidth as the parameter instead of chanParams!!
530     status = mcaspControlChan(hMcaspChan, Mcasp_IOCTL_CHAN_PARAMS_WORD_WIDTH, &chanParams);
532     if((status != MCASP_COMPLETED)) {
533         return (Audk2g_EFAIL);
534     }
535     else {
536         return (Audk2g_EOK);
537     }
538 } /* mcaspRecfgWordWidth */
540 /* Nothing past this point */