196692e3d014cf13a343740a51999e5ffbce49b9
1 /*\r
2 * Copyright (c) 2017, Texas Instruments Incorporated\r
3 * All rights reserved.\r
4 *\r
5 * Redistribution and use in source and binary forms, with or without\r
6 * modification, are permitted provided that the following conditions\r
7 * are met:\r
8 *\r
9 * * Redistributions of source code must retain the above copyright\r
10 * notice, this list of conditions and the following disclaimer.\r
11 *\r
12 * * Redistributions in binary form must reproduce the above copyright\r
13 * notice, this list of conditions and the following disclaimer in the\r
14 * documentation and/or other materials provided with the distribution.\r
15 *\r
16 * * Neither the name of Texas Instruments Incorporated nor the names of\r
17 * its contributors may be used to endorse or promote products derived\r
18 * from this software without specific prior written permission.\r
19 *\r
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,\r
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\r
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;\r
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\r
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR\r
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
31 *\r
32 */\r
33 \r
34 /**\r
35 * \file mcasp_config.c\r
36 *\r
37 * \brief Configures McASP module\r
38 *\r
39 */\r
40 \r
41 #include "fil.h" /* FILE I/O implementation */\r
42 #include "sys.h" /* System API and structures */\r
43 #include "sysbfflt.h" /* System support for BF filters */\r
44 \r
45 #include "mcasp_cfg.h"\r
46 #include "cmb.h"\r
47 \r
48 \r
49 /* The infamous xdc/std.h must come before any header file which uses XDC symbols */\r
50 #include <xdc/std.h> /* mandatory - have to include first, for BIOS types */\r
51 #include <ti/mas/types/types.h>\r
52 #include <ti/mas/aer/bf.h>\r
53 #include "../../../common/components/mss/mss.h" /* local version used */\r
54 #include <ti/mas/vpe/asnr.h>\r
55 #if (SYS_USE_DRC)\r
56 #include <ti/mas/vau/vau.h>\r
57 #include <ti/mas/aer/drc.h>\r
58 #endif\r
59 \r
60 /*----------------------------------------\r
61 * BIOS header files\r
62 *----------------------------------------*/\r
63 #include <ti/sysbios/BIOS.h> /* mandatory - if you call APIs like BIOS_start() */\r
64 #include <xdc/cfg/global.h> /* header file for statically defined objects/handles */\r
65 #include <xdc/runtime/System.h> /* for System_printf, and similar */\r
66 #include <xdc/runtime/Timestamp.h> /* for benchmarking/profiling */\r
67 \r
68 #include <xdc/runtime/Log.h> /* for tracing */\r
69 #include <xdc/runtime/Diags.h>\r
70 #include <ti/uia/events/UIAEvt.h> /* and more tracing */\r
71 \r
72 #include <ti/sysbios/knl/Semaphore.h> /* this looks obvious */\r
73 \r
74 #define Timestamp_get Timestamp_get32 /* use 32-bit time stamps */\r
75 \r
76 #define MIN(a,b) (((a)>(b))?(b):(a)) /* Min/Max macros */\r
77 #define MAX(a,b) (((a)<(b))?(b):(a))\r
78 #define loop while(1) /* endless loop for the task */\r
79 \r
80 \r
81 /* Frame index for Rx and Tx buffers */\r
82 uint8_t rxFrameIndex = 1;\r
83 uint8_t txFrameIndex = 1;\r
84 uint32_t gtxFrameIndexCount = 0;\r
85 uint32_t grxFrameIndexCount = 0;\r
86 \r
87 /* Flags for counting Rx and Tx interrupts */\r
88 volatile uint32_t rxFlag = 0;\r
89 volatile uint32_t txFlag = 0;\r
90 \r
91 /* Semaphore handle for Tx and Rx */\r
92 Semaphore_Handle semR;\r
93 Semaphore_Handle semT;\r
94 Semaphore_Params params;\r
95 \r
96 /* McASP device handles */\r
97 Ptr hMcaspDevTx;\r
98 Ptr hMcaspDevRx;\r
99 \r
100 /* McASP channel handles */\r
101 Ptr hMcaspTxChan;\r
102 Ptr hMcaspRxChan;\r
103 \r
104 /* McASP channel parameters */\r
105 Mcasp_Params mcaspTxParams;\r
106 Mcasp_Params mcaspRxParams;\r
107 \r
108 /* McASP Callback function argument */\r
109 uint32_t txChanMode;\r
110 uint32_t rxChanMode;\r
111 uint32_t mcaspRxChanArg = 1;\r
112 uint32_t mcaspTxChanArg = 2;\r
113 \r
114 /* McASP Tx and Rx frame buffers */\r
115 MCASP_Packet rxFrame[NUM_BUFS];\r
116 MCASP_Packet txFrame[NUM_BUFS];\r
117 \r
118 /* McASP Tx and Rx frame buffer pointers */\r
119 Ptr txBuf[NUM_BUFS];\r
120 Ptr rxBuf[NUM_BUFS];\r
121 Ptr wkBuf[NUM_BUFS];\r
122 Ptr outBuf[NUM_BUFS];\r
123 \r
124 /* channel 0 (serilizer 1 left) - mic1 */\r
125 /* channel 1 (serilizer 1 right) - mic2 */\r
126 /* channel 2 (serilizer 2 left) - mic5 */\r
127 /* channel 3 (serilizer 2 right) - mic6 */\r
128 /* channel 4 (serilizer 3 left) - mic3 */\r
129 /* channel 5 (serilizer 3 right) - mic4 */\r
130 /* channel 6 (serilizer 4 left) - mic8 */\r
131 /* channel 7 (serilizer 4 right) - mic7 */\r
132 int chanToMicMapping[8] = {0, 1, 4, 5, 3, 2, 7, 6};\r
133 \r
134 /* Error flag */\r
135 uint32_t gblErrFlag = 0;\r
136 Error_Block eb;\r
137 \r
138 /* External function declarations */\r
139 void GblErr(int arg);\r
140 signed char* getGlobalAddr(signed char* addr);\r
141 \r
142 /* McASP HW setup for receive */\r
143 Mcasp_HwSetupData mcaspRcvSetup = {\r
144 /* .rmask = */ 0xFFFFFFFF, /* 16 bits are to be used */\r
145 /* .rfmt = */ 0x000180F0, /*\r
146 * 1 bit delay from framesync\r
147 * MSB first\r
148 * No extra bit padding\r
149 * Padding bit (ignore)\r
150 * slot Size is 32\r
151 * Reads from DMA port\r
152 * NO rotation\r
153 */\r
154 /* .afsrctl = */ 0X00000111, /* I2S mode - 2 slot TDM\r
155 * Frame sync is one word\r
156 * Externally generated frame sync\r
157 * Falling edge is start of frame\r
158 */\r
159 /* .rtdm = */ 0x00000003, /* slot 1 and 2 are active (I2S) */\r
160 /* .rintctl = */ 0x00000000, /* sync error and overrun error */\r
161 /* .rstat = */ 0x000001FF, /* reset any existing status bits */\r
162 /* .revtctl = */ 0x00000000, /* DMA request is enabled */\r
163 {\r
164 /* .aclkrctl = */ 0x00000080, /* Bit CLK Pol: falling edge, ACLKR is external */\r
165 /* .ahclkrctl = */ 0x00000000, /* AHCLKR is external */\r
166 /* .rclkchk = */ 0x00000000\r
167 }\r
168 };\r
169 \r
170 /* McASP HW setup for transmit */\r
171 #if (CMB_AUDIO_DAC)\r
172 Mcasp_HwSetupData mcaspXmtSetup = {\r
173 /* .xmask = */ 0xFFFFFFFF, /* 16 bits are to be used */\r
174 /* .xfmt = */ 0x000180F0, /*\r
175 * 1 bit delay from framesync\r
176 * MSB first\r
177 * No extra bit padding\r
178 * Padding bit (ignore)\r
179 * slot Size is 32\r
180 * Reads from DMA port\r
181 * NO rotation\r
182 */\r
183 /* .afsxctl = */ 0x00000113, /* I2S mode - 2 slot TDM\r
184 * Frame sync is one word\r
185 * Falling edge is start of frame\r
186 * Externally generated frame sync\r
187 */\r
188 /* .xtdm = */ 0x00000003, /* slot 1 and 2 are active (I2S) */\r
189 /* .xintctl = */ 0x00000000, /* sync error,overrun error,clK error */\r
190 /* .xstat = */ 0x000001FF, /* reset any existing status bits */\r
191 /* .xevtctl = */ 0x00000000, /* DMA request is enabled or disabled */\r
192 {\r
193 #if (SYS_ADC_FS_HZ==16000)\r
194 /* .aclkxctl = */ 0X000000E3, /* Bit CLK Pol: falling edge, ASYNC is 1, ACLKX is internal, HF CLK to BCLK divider is 4 */\r
195 #else\r
196 /* .aclkxctl = */ 0X000000E7, /* Bit CLK Pol: falling edge, ASYNC is 1, ACLKX is internal, HF CLK to BCLK divider is 8 */\r
197 #endif\r
198 /* .ahclkxctl = */ 0x00000000, /* AHCLKX is external */\r
199 /* .xclkchk = */ 0x00000000\r
200 },\r
201 \r
202 };\r
203 #endif\r
204 \r
205 /* McAsp channel parameters for receive */\r
206 Mcasp_ChanParams mcaspRxChanParam =\r
207 {\r
208 0x0004, /* number of serializers */\r
209 {Mcasp_SerializerNum_0,\r
210 Mcasp_SerializerNum_1,\r
211 Mcasp_SerializerNum_2,\r
212 Mcasp_SerializerNum_3 }, /* serializer index */\r
213 &mcaspRcvSetup,\r
214 TRUE,\r
215 Mcasp_OpMode_TDM, /* Mode (TDM/DIT) */\r
216 Mcasp_WordLength_32,\r
217 NULL,\r
218 0,\r
219 NULL,\r
220 GblErr,\r
221 2, /* number of TDM channels */\r
222 ///Mcasp_BufferFormat_MULTISER_MULTISLOT_SEMI_INTERLEAVED_1,\r
223 Mcasp_BufferFormat_MULTISER_MULTISLOT_SEMI_INTERLEAVED_2,\r
224 TRUE,\r
225 TRUE\r
226 };\r
227 \r
228 #if (CMB_AUDIO_DAC)\r
229 /* McAsp channel parameters for transmit */\r
230 Mcasp_ChanParams mcaspTxChanParam =\r
231 {\r
232 0x0001, /* number of serializers */\r
233 {Mcasp_SerializerNum_2,}, /* serializer index for DAC0 */\r
234 &mcaspXmtSetup,\r
235 TRUE,\r
236 Mcasp_OpMode_TDM,\r
237 Mcasp_WordLength_32, /* word width */\r
238 NULL,\r
239 0,\r
240 NULL,\r
241 GblErr,\r
242 2, /* number of TDM channels */\r
243 Mcasp_BufferFormat_1SER_MULTISLOT_INTERLEAVED,\r
244 ///Mcasp_BufferFormat_1SER_MULTISLOT_NON_INTERLEAVED,\r
245 TRUE,\r
246 TRUE\r
247 };\r
248 #endif\r
249 \r
250 #if (SYS_USE_DRC)\r
251 /* Output frame for MSS, input for DRC */\r
252 #pragma DATA_ALIGN(txOutFrame1,8)\r
253 linSample txOutFrame1[SYS_FRAME_LENGTH];\r
254 \r
255 /* Output frame for DRC, input for VAU */\r
256 #pragma DATA_ALIGN(txOutFrame2,8)\r
257 linSample txOutFrame2[SYS_FRAME_LENGTH];\r
258 #endif\r
259 \r
260 typedef struct txBfDebug_stc {\r
261 tulong frmcnt; /* normal frames */\r
262 tulong silcnt; /* silence frames */\r
263 tuint invsrc; /* no mic active, invalid output */\r
264 tuint invopt; /* >1 mic active, invalid output */\r
265 } txBfDebug_t;\r
266 \r
267 typedef struct txTaskDebug_stc {\r
268 tuint overrun; /* counts how many times we ran out of MIPS */\r
269 txBfDebug_t bf[SYS_VMICS_MAX]; /* beamformer statistics */\r
270 } txTaskDebug_t;\r
271 \r
272 txTaskDebug_t txTaskDebug; /* Tx task debug stats */\r
273 \r
274 /* Profiling/benchmarking information for the Tx task */\r
275 typedef struct txTaskProfileData_stc {\r
276 tulong min; /* Minimum number of cycles */\r
277 tulong max; /* Maximum number of cycles */\r
278 tulong n; /* Number of measurements */\r
279 float total; /* Total number of cycles */\r
280 } txTaskProfileData_t;\r
281 \r
282 typedef struct txTaskProfile_stc {\r
283 txTaskProfileData_t bf; /* Beamformer profile */\r
284 txTaskProfileData_t asnr; /* ASNR profile */\r
285 txTaskProfileData_t mss; /* MSS profile */\r
286 txTaskProfileData_t drc; /* DRC profile */\r
287 txTaskProfileData_t vau; /* VAU profile */\r
288 } txTaskProfile_t;\r
289 volatile txTaskProfile_t txTaskProfile = {\r
290 {~(0uL), 0, 0, 0.0f},\r
291 {~(0uL), 0, 0, 0.0f},\r
292 {~(0uL), 0, 0, 0.0f},\r
293 {~(0uL), 0, 0, 0.0f},\r
294 {~(0uL), 0, 0, 0.0f}\r
295 };\r
296 \r
297 /* To be used for debug trace */\r
298 mssSrc_t mssDbgCurSrc = {\r
299 -1, -1 /* Current source group/index */\r
300 };\r
301 mssSrc_t mssDbgNewSrc = {\r
302 -1, -1 /* New source group/index */\r
303 };\r
304 \r
305 /* Handle to eDMA */\r
306 extern EDMA3_DRV_Handle hEdma1;\r
307 \r
308 /**\r
309 * \brief Function called by McASP driver in case of error\r
310 *\r
311 * \return None\r
312 */\r
313 void GblErr(int arg)\r
314 {\r
315 gblErrFlag = 1;\r
316 }\r
317 \r
318 /**\r
319 * \brief McASP callback function called up on the data transfer completion\r
320 *\r
321 * \param arg [IN] - Application specific callback argument\r
322 * \param ioBuf [IN] - McASP IO buffer\r
323 *\r
324 * \return None\r
325 */\r
326 void mcaspAppCallback(void *arg, MCASP_Packet *ioBuf)\r
327 {\r
328 /* Callback is triggered by Rx completion */\r
329 if(ioBuf->cmd == MCASP_READ)\r
330 {\r
331 rxFlag++;\r
332 \r
333 if(rxFrameIndex == 0)\r
334 {\r
335 rxFrameIndex = 1;\r
336 }\r
337 else\r
338 {\r
339 rxFrameIndex = 0;\r
340 }\r
341 \r
342 /* Post semaphore */\r
343 Semaphore_post(semR);\r
344 }\r
345 \r
346 /* Callback is triggered by Tx completion */\r
347 if(ioBuf->cmd == MCASP_WRITE)\r
348 {\r
349 if(txFrameIndex == 0)\r
350 {\r
351 txFrameIndex = 1;\r
352 }\r
353 else\r
354 {\r
355 txFrameIndex = 0;\r
356 }\r
357 \r
358 txFlag++;\r
359 \r
360 /* Post semaphore */\r
361 Semaphore_post(semT);\r
362 }\r
363 }\r
364 \r
365 /**\r
366 * \brief Initializes McASP data buffers and submits to McASP driver\r
367 *\r
368 * \return Cmb_EOK on Success or error code\r
369 */\r
370 Cmb_STATUS initBuffers(void)\r
371 {\r
372 Error_Block eb;\r
373 uint32_t count = 0;\r
374 IHeap_Handle iheap;\r
375 Int status;\r
376 \r
377 iheap = HeapMem_Handle_to_xdc_runtime_IHeap(heapHandle);\r
378 Error_init(&eb);\r
379 \r
380 /* Allocate buffers for the McASP data exchanges */\r
381 for(count = 0; count < NUM_BUFS; count++)\r
382 {\r
383 rxBuf[count] = Memory_calloc(iheap, (BUFSIZE * RX_NUM_SERIALIZER),\r
384 BUFALIGN, &eb);\r
385 if(NULL == rxBuf[count])\r
386 {\r
387 IFPRINT(cmb_write("\r\nMEM_calloc failed for Rx\n"));\r
388 }\r
389 }\r
390 \r
391 /* Allocate work buffers for signal processing */\r
392 for(count = 0; count < NUM_BUFS; count++)\r
393 {\r
394 wkBuf[count] = Memory_calloc(iheap, (BUFSIZE * RX_NUM_SERIALIZER/(SYS_FS_RATIO*2)),\r
395 BUFALIGN, &eb);\r
396 if(NULL == wkBuf[count])\r
397 {\r
398 IFPRINT(cmb_write("\r\nMEM_calloc failed for Wk\n"));\r
399 }\r
400 }\r
401 \r
402 #if (CMB_AUDIO_DAC)\r
403 /* Allocate buffers for the McASP data exchanges */\r
404 for(count = 0; count < NUM_BUFS; count++)\r
405 {\r
406 txBuf[count] = Memory_calloc(iheap, (BUFSIZE * TX_NUM_SERIALIZER),\r
407 BUFALIGN, &eb);\r
408 if(NULL == txBuf[count])\r
409 {\r
410 IFPRINT(cmb_write("\r\nMEM_calloc failed for Tx\n"));\r
411 }\r
412 }\r
413 \r
414 /* Allocate output buffers for the MSS */\r
415 for(count = 0; count < NUM_BUFS; count++)\r
416 {\r
417 outBuf[count] = Memory_calloc(iheap, (BUFSIZE * TX_NUM_SERIALIZER/(SYS_FS_RATIO*2*2)),\r
418 BUFALIGN, &eb);\r
419 if(NULL == outBuf[count])\r
420 {\r
421 IFPRINT(cmb_write("\r\nMEM_calloc failed for Out\n"));\r
422 }\r
423 }\r
424 #endif\r
425 for(count = 0; count < NUM_BUFS; count++)\r
426 {\r
427 /* Issue the first & second empty buffers to the input stream */\r
428 memset((uint8_t *)rxBuf[count], 0xAA, (BUFSIZE * RX_NUM_SERIALIZER));\r
429 memset((uint8_t *)wkBuf[count], 0xBB, (BUFSIZE * RX_NUM_SERIALIZER/(SYS_FS_RATIO*2)));\r
430 \r
431 /* RX frame processing */\r
432 rxFrame[count].cmd = MCASP_READ;\r
433 rxFrame[count].addr = (void*)(getGlobalAddr(rxBuf[count]));\r
434 rxFrame[count].size = BUFSIZE * RX_NUM_SERIALIZER;\r
435 rxFrame[count].arg = (uint32_t) mcaspRxChanArg;\r
436 rxFrame[count].status = 0;\r
437 rxFrame[count].misc = 1; /* reserved - used in callback to indicate asynch packet */\r
438 \r
439 /* Submit McASP packet for Rx */\r
440 status = mcaspSubmitChan(hMcaspRxChan, &rxFrame[count]);\r
441 if((status != MCASP_COMPLETED) && (status != MCASP_PENDING))\r
442 {\r
443 IFPRINT(cmb_write("mcaspSubmitChan for Rx Failed\n"));\r
444 return (Cmb_EFAIL);\r
445 }\r
446 }\r
447 \r
448 #if (CMB_AUDIO_DAC)\r
449 for(count = 0; count < NUM_BUFS; count++)\r
450 {\r
451 memset((uint8_t *)txBuf[count], 0xCC, (BUFSIZE * TX_NUM_SERIALIZER));\r
452 memset((uint8_t *)outBuf[count], 0xDD, (BUFSIZE * TX_NUM_SERIALIZER/(3*2*2)));\r
453 \r
454 /* TX frame processing */\r
455 txFrame[count].cmd = MCASP_WRITE;\r
456 txFrame[count].addr = (void*)(getGlobalAddr(txBuf[count]));\r
457 txFrame[count].size = BUFSIZE * TX_NUM_SERIALIZER;\r
458 txFrame[count].arg = (uint32_t) mcaspTxChanArg;\r
459 txFrame[count].status = 0;\r
460 txFrame[count].misc = 1; /* reserved - used in callback to indicate asynch packet */\r
461 \r
462 /* Submit McASP packet for Tx */\r
463 status = mcaspSubmitChan(hMcaspTxChan, &txFrame[count]);\r
464 if((status != MCASP_COMPLETED) && (status != MCASP_PENDING))\r
465 {\r
466 IFPRINT(cmb_write("mcaspSubmitChan for Tx Failed\n"));\r
467 return (Cmb_EFAIL);\r
468 }\r
469 }\r
470 #endif\r
471 return (Cmb_EOK);\r
472 }\r
473 \r
474 /**\r
475 * \brief Configures McASP module and creates the channel\r
476 * for audio Tx and Rx\r
477 *\r
478 * \return Cmb_EOK on Success or error code\r
479 */\r
480 Cmb_STATUS mcaspAudioConfig(void)\r
481 {\r
482 Int status;\r
483 \r
484 #if (CMB_AUDIO_DAC)\r
485 hMcaspDevTx = NULL;\r
486 hMcaspTxChan = NULL;\r
487 #endif\r
488 hMcaspDevRx = NULL;\r
489 hMcaspRxChan = NULL;\r
490 \r
491 /* Initialize McASP Tx and Rx parameters */\r
492 #if (CMB_AUDIO_DAC)\r
493 mcaspTxParams = Mcasp_PARAMS;\r
494 #endif\r
495 mcaspRxParams = Mcasp_PARAMS;\r
496 \r
497 #if (CMB_AUDIO_DAC)\r
498 mcaspTxParams.mcaspHwSetup.tx.clk.clkSetupClk = 0x63;\r
499 mcaspTxParams.mcaspHwSetup.rx.clk.clkSetupClk = 0x23;\r
500 #endif\r
501 mcaspRxParams.mcaspHwSetup.rx.clk.clkSetupClk = 0x23;\r
502 mcaspRxParams.mcaspHwSetup.tx.clk.clkSetupClk = 0x63;\r
503 \r
504 #if (CMB_AUDIO_DAC)\r
505 mcaspTxParams.mcaspHwSetup.glb.pdir |= 0x2000000; //Set Amute pin as output for Tx channel\r
506 #endif\r
507 \r
508 /* Set the HW interrupt number */\r
509 #if (CMB_AUDIO_DAC)\r
510 mcaspTxParams.hwiNumber = 8;\r
511 #endif\r
512 mcaspRxParams.hwiNumber = 8;\r
513 \r
514 /* Initialize eDMA handle */\r
515 mcaspRxChanParam.edmaHandle = hEdma1;\r
516 #if (CMB_AUDIO_DAC)\r
517 mcaspTxChanParam.edmaHandle = hEdma1;\r
518 \r
519 /* Bind McASP2 for Tx */\r
520 status = mcaspBindDev(&hMcaspDevTx, CSL_MCASP_2, &mcaspTxParams);\r
521 if((status != MCASP_COMPLETED) || (hMcaspDevTx == NULL))\r
522 {\r
523 IFPRINT(cmb_write("mcaspBindDev for Tx Failed\n"));\r
524 return (Cmb_EFAIL);\r
525 }\r
526 #endif\r
527 /* Bind McASP1 for Rx */\r
528 status = mcaspBindDev(&hMcaspDevRx, CSL_MCASP_1, &mcaspRxParams);\r
529 if((status != MCASP_COMPLETED) || (hMcaspDevRx == NULL))\r
530 {\r
531 IFPRINT(cmb_write("mcaspBindDev for Rx Failed\n"));\r
532 return (Cmb_EFAIL);\r
533 }\r
534 \r
535 #if (CMB_AUDIO_DAC)\r
536 /* Create McASP channel for Tx */\r
537 status = mcaspCreateChan(&hMcaspTxChan, hMcaspDevTx,\r
538 MCASP_OUTPUT,\r
539 &mcaspTxChanParam,\r
540 mcaspAppCallback, &txChanMode);\r
541 if((status != MCASP_COMPLETED) || (hMcaspTxChan == NULL))\r
542 {\r
543 IFPRINT(cmb_write("mcaspCreateChan for Tx Failed\n"));\r
544 return (Cmb_EFAIL);\r
545 }\r
546 \r
547 configAudioDAC();\r
548 #endif\r
549 \r
550 /* Create McASP channel for Rx */\r
551 status = mcaspCreateChan(&hMcaspRxChan, hMcaspDevRx,\r
552 MCASP_INPUT,\r
553 &mcaspRxChanParam,\r
554 mcaspAppCallback, &rxChanMode);\r
555 if((status != MCASP_COMPLETED) || (hMcaspRxChan == NULL))\r
556 {\r
557 IFPRINT(cmb_write("mcaspCreateChan for Rx Failed\n"));\r
558 return (Cmb_EFAIL);\r
559 }\r
560 \r
561 /* Initialize the buffers and submit for McASP Tx/Rx */\r
562 if(initBuffers() != Cmb_EOK)\r
563 {\r
564 IFPRINT(cmb_write("McASP Buffer Initialization Failed\n"));\r
565 return (Cmb_EFAIL);\r
566 }\r
567 \r
568 return (Cmb_EOK);\r
569 }\r
570 \r
571 /**\r
572 * \brief Function to exit the test\r
573 *\r
574 * \return None\r
575 */\r
576 void testRet(uint32_t status)\r
577 {\r
578 cmb_write("\n\nAudio DC Analog Interface Test Completed!\n");\r
579 \r
580 testExit(status);\r
581 }\r
582 \r
583 /**\r
584 * \brief Task to echo the input data to output\r
585 *\r
586 * Waits for the McASP data transfer completion and copies the\r
587 * Rx data to Tx buffers\r
588 *\r
589 * \return Cmb_EOK on Success or error code\r
590 */\r
591 #define DUMP_SEC 5\r
592 #define FRAME_PER_SEC 100\r
593 int gAudDumpBufIdx = 0;\r
594 unsigned char gAudDumpBuf[(BUFSIZE*RX_NUM_SERIALIZER)*FRAME_PER_SEC*DUMP_SEC];\r
595 unsigned char gAudOutDumpBuf[(BUFSIZE*TX_NUM_SERIALIZER)*FRAME_PER_SEC*DUMP_SEC];\r
596 Void Audio_echo_Task(void)\r
597 {\r
598 int32_t i, j, k;\r
599 unsigned char *tempTxPtr, *tempRxPtr, *tempWkPtr;\r
600 unsigned char *tempOutPtr, *tempMicPtr;\r
601 tint nmics, nvmics, err, angle;\r
602 volatile tulong t1, t2; /* for profiling */\r
603 tulong delta;\r
604 \r
605 void *inst_p;\r
606 linSample *in_r; /* pointer to current microphone input buffer */\r
607 linSample *frame_p; /* pointer to signal frame */\r
608 linSample *outframe_p; /* Output frame pointer for VAU */\r
609 linSample *mics_in[SYS_MICS_MAX+1]; /* pointers to microphone inputs */\r
610 \r
611 mssDebugStat_t mssDbg;\r
612 \r
613 Semaphore_Params_init(¶ms);\r
614 \r
615 /* Create semaphores to wait for buffer reclaiming */\r
616 semR = Semaphore_create(0, ¶ms, &eb);\r
617 semT = Semaphore_create(0, ¶ms, &eb);\r
618 \r
619 /* Forever loop to continuously receive and transmit audio data */\r
620 while (1)\r
621 {\r
622 if(gblErrFlag)\r
623 {\r
624 break;\r
625 }\r
626 \r
627 /* Reclaim full buffer from the input stream */\r
628 Semaphore_pend(semR, BIOS_WAIT_FOREVER);\r
629 #if (CMB_AUDIO_DAC)\r
630 Semaphore_pend(semT, BIOS_WAIT_FOREVER);\r
631 #endif\r
632 /* Reclaim full buffer from the input stream */\r
633 #if (CMB_AUDIO_DAC)\r
634 gtxFrameIndexCount = txFrameIndex;\r
635 #endif\r
636 grxFrameIndexCount = rxFrameIndex;\r
637 \r
638 #if (CMB_AUDIO_DAC)\r
639 \r
640 #if 0 // Mcasp_BufferFormat_MULTISER_MULTISLOT_SEMI_INTERLEAVED_1\r
641 // copy RX mic 1 to TX left channel and RX mic 5 to right channel\r
642 // set the RX pointer to mic 1\r
643 tempRxPtr = (uint32_t *)rxBuf[grxFrameIndexCount];\r
644 // set the TX pointer to left cahhnel\r
645 tempTxPtr = (uint32_t *)txBuf[gtxFrameIndexCount];\r
646 // copy RX mic 1 to TX left channel\r
647 for (i=0; i<BUFLEN/2; i++)\r
648 {\r
649 // copy the left channel of first serializer to the left channel of TX buffer\r
650 *tempTxPtr = *tempRxPtr;\r
651 tempTxPtr++;\r
652 // copy the left channel of swecond serializer to the right channel of TX buffer\r
653 *tempTxPtr = *(tempRxPtr+BUFLEN+2);\r
654 tempTxPtr++;\r
655 tempRxPtr += RX_NUM_SERIALIZER*2;\r
656 }\r
657 \r
658 #else // Mcasp_BufferFormat_MULTISER_MULTISLOT_SEMI_INTERLEAVED_2\r
659 #if 1 // Signal Processing Path\r
660 // 48khz to 16khz, 24bit to 16bit conversion\r
661 nmics = sysContext.nmics; /* fetch number of mics */\r
662 // for each channel, convert and copy the RX buffer to WK buffer\r
663 for (j=0; j<(nmics+1); j++)\r
664 {\r
665 // set the RX pointer\r
666 tempRxPtr = (unsigned char *)rxBuf[grxFrameIndexCount] + (j/2)*BUFSIZE + (j&0x00000001)*sizeof(Ptr) + sizeof(short);\r
667 // set the WK pointer\r
668 tempWkPtr = (unsigned char *)wkBuf[grxFrameIndexCount] + j*BUFSIZE/(SYS_FS_RATIO*2*2);\r
669 // convert and copy RX to WK every third sample\r
670 for (i=0; i<BUFLEN/2; i+=SYS_FS_RATIO)\r
671 {\r
672 // only copy the two most significant bytes (Q31 to Q15 conversion)\r
673 memcpy(tempWkPtr, tempRxPtr, sizeof(short));\r
674 tempWkPtr += sizeof(short);\r
675 tempRxPtr += sizeof(Ptr)*2*SYS_FS_RATIO;\r
676 }\r
677 }\r
678 \r
679 // set the sysContext.in_r\r
680 sysContext.in_r = wkBuf[grxFrameIndexCount];\r
681 in_r = (linSample *)sysContext.in_r;\r
682 for (k = 0; k < (nmics+1); k++) {\r
683 mics_in[chanToMicMapping[k]] = &in_r[k*SYS_FRAME_LENGTH]; /* find the frame start for each microphone */\r
684 }\r
685 /* consume samples pointed to by read pointer in_r as provided in misc_in[] */\r
686 \r
687 // BF+ASNR+MSS processing\r
688 /* Here comes a lot of work */\r
689 /* We start with beamformers */\r
690 \r
691 /* Start the beamformers */\r
692 // get the number of virtual mics\r
693 nvmics = sysContext.nvmics;\r
694 t1 = Timestamp_get();\r
695 for (k = 0; k < nvmics; k++) {\r
696 inst_p = sysContext.bfInst_p[k]; /* fetch the bf instance pointer */\r
697 frame_p = sysContext.vmicfrm[k]; /* point to the output frame buffer */\r
698 \r
699 err = bfProcess(inst_p, (void*)&mics_in[0], (void*)frame_p);\r
700 \r
701 if (err != bf_NOERR) {\r
702 SYS_CHECK_ERROR(SYS_ERR_BFERROR);\r
703 }\r
704 } /* for */\r
705 t2 = Timestamp_get();\r
706 delta = t2-t1;\r
707 txTaskProfile.bf.min = MIN(txTaskProfile.bf.min,delta);\r
708 txTaskProfile.bf.max = MAX(txTaskProfile.bf.max,delta);\r
709 txTaskProfile.bf.n++;\r
710 txTaskProfile.bf.total += (float)delta;\r
711 \r
712 /* At this point we have consumed all input samples. Currently we did not implement\r
713 * any protection to prevent the swiDataIn from stepping over while we were doing this.\r
714 * We could let this task to handle the read pointer and SWI to handle write pointer which\r
715 * could be used to detect if such overrun would happen. */\r
716 /* Done with the beamformers */\r
717 \r
718 /* Start ASNR's */\r
719 t1 = Timestamp_get();\r
720 for (k = 0; k < nvmics; k++) {\r
721 inst_p = sysContext.asnrInst_p[k]; /* fetch the bf instance pointer */\r
722 frame_p = sysContext.vmicfrm[k]; /* point to the output frame buffer */\r
723 \r
724 err = asnrProcess(inst_p, (void*)frame_p, (void*)frame_p);\r
725 \r
726 if (err != asnr_NOERR) {\r
727 SYS_CHECK_ERROR(SYS_ERR_ASNRERROR);\r
728 } /* if */\r
729 } /* for */\r
730 t2 = Timestamp_get();\r
731 delta = t2-t1;\r
732 txTaskProfile.asnr.min = MIN(txTaskProfile.asnr.min,delta);\r
733 txTaskProfile.asnr.max = MAX(txTaskProfile.asnr.max,delta);\r
734 txTaskProfile.asnr.n++;\r
735 txTaskProfile.asnr.total += (float)delta;\r
736 /* Done with the ASNR's */\r
737 \r
738 /* Run MSS */\r
739 t1 = Timestamp_get();\r
740 inst_p = sysContext.mssInst_p; /* fetch the MSS instance pointer */\r
741 #if (SYS_USE_DRC)\r
742 frame_p = txOutFrame1; /* point to the output frame buffer */\r
743 #else\r
744 frame_p = outBuf[gtxFrameIndexCount]; /* point to the output frame buffer */\r
745 #endif\r
746 \r
747 err = mssProcess(inst_p, /* instance */\r
748 (void*)frame_p, /* output frame pointers */\r
749 (void*)frame_p, /* WORKAROUND (not used, but no NULL) */\r
750 (void**)sysContext.vmicfrm, /* Virtual microphones (beams) */\r
751 NULL, /* No remote mics */\r
752 NULL, /* No clean mics */\r
753 (void**)mics_in, /* Raw microphone array inputs */\r
754 NULL); /* Beam not supported (see fixed inputs) */\r
755 \r
756 if (err != mss_NOERR) {\r
757 SYS_CHECK_ERROR(SYS_ERR_MSSERROR);\r
758 } /* if */\r
759 t2 = Timestamp_get();\r
760 delta = t2-t1;\r
761 txTaskProfile.mss.min = MIN(txTaskProfile.mss.min,delta);\r
762 txTaskProfile.mss.max = MAX(txTaskProfile.mss.max,delta);\r
763 txTaskProfile.mss.n++;\r
764 txTaskProfile.mss.total += (float)delta;\r
765 \r
766 /* Trace source selection */\r
767 /* Write Args:\r
768 * arg2: (value) Angle in degrees\r
769 * arg3: (aux1) 0 - current source, 1 - new source\r
770 * arg4: (aux2) source index\r
771 */\r
772 err = mssDebugStat(inst_p, &mssDbg);\r
773 if (err !=mss_NOERR) {\r
774 SYS_CHECK_ERROR(SYS_ERR_MSSDEBUG);\r
775 }\r
776 /* mssDbg.cur_src.group/.index has the current source */\r
777 /* mssDbg.new_src.group/.index has "proposed" source */\r
778 if (mssDbg.cur_src.group != mssDbgCurSrc.group ||\r
779 mssDbg.cur_src.index != mssDbgCurSrc.index)\r
780 {\r
781 mssDbgCurSrc = mssDbg.cur_src;\r
782 angle = sysBfFilterAngles[sysBfVMicAngles[mssDbgCurSrc.index]];\r
783 ///Log_write6(UIAEvt_intWithKey, angle, 0, mssDbgCurSrc.index, (IArg)"MSS-C: %d, G:%d", 0, mssDbgCurSrc.group);\r
784 }\r
785 if (mssDbg.new_src.group != mssDbgNewSrc.group ||\r
786 mssDbg.new_src.index != mssDbgNewSrc.index)\r
787 {\r
788 mssDbgNewSrc = mssDbg.new_src;\r
789 angle = sysBfFilterAngles[sysBfVMicAngles[mssDbgNewSrc.index]];\r
790 ///Log_write6(UIAEvt_intWithKey, angle, 1, mssDbgNewSrc.index, (IArg)"MSS-N: %d, G:%d", 1, mssDbgNewSrc.group);\r
791 }\r
792 /* Done with MSS */\r
793 \r
794 #if (SYS_USE_DRC)\r
795 /* Run DRC */\r
796 t1 = Timestamp_get();\r
797 inst_p = sysContext.drcInst_p; /* fetch the DRC instance pointer */\r
798 frame_p = txOutFrame1; /* point to the MSS output frame buffer and use it as input */\r
799 outframe_p = txOutFrame2; /* point to DRC output frame */\r
800 err = drcProcess(inst_p, frame_p, /* instance and input frame pointers */\r
801 outframe_p); /* pointer to output buffer pointer */\r
802 t2 = Timestamp_get();\r
803 delta = t2-t1;\r
804 txTaskProfile.drc.min = MIN(txTaskProfile.drc.min,delta);\r
805 txTaskProfile.drc.max = MAX(txTaskProfile.drc.max,delta);\r
806 txTaskProfile.drc.n++;\r
807 txTaskProfile.drc.total += (float)delta;\r
808 /* Done with DRC */\r
809 #endif\r
810 \r
811 /*---------------------------------*/\r
812 /* Save samples to the TX buffer */\r
813 /*---------------------------------*/\r
814 // copy MSS output to TX left channel and RX mic 5 to TX right channel\r
815 // set the tempOutPtr to MSS output\r
816 #if (SYS_USE_DRC)\r
817 tempOutPtr = txOutFrame2;\r
818 #else\r
819 tempOutPtr = outBuf[gtxFrameIndexCount];\r
820 #endif\r
821 // set the tempMicPtr to mic_in[7] (mic8)\r
822 tempMicPtr = (unsigned char *)mics_in[7];\r
823 // set the TX pointer to left cahhnel\r
824 tempTxPtr = txBuf[gtxFrameIndexCount];\r
825 // copy upsampled and Q15 to Q31 converted MSS output to TX left channel\r
826 // copy upsampled and Q15 to Q31 converted mics_in[4] to TX right channel\r
827 for (i=0; i<BUFLEN/(SYS_FS_RATIO*2); i++)\r
828 {\r
829 // up sampling by SYS_FS_RATIO (16khz to 48khz)\r
830 for (k=0; k<SYS_FS_RATIO; k++)\r
831 {\r
832 // MSS output\r
833 // Q15 to Q31\r
834 memset(tempTxPtr, 0, sizeof(short));\r
835 tempTxPtr += sizeof(short);\r
836 // copy the MSS output to the left channel of TX buffer\r
837 memcpy(tempTxPtr, tempOutPtr, sizeof(short));\r
838 tempTxPtr += sizeof(short);\r
839 \r
840 // mics_in[4]\r
841 // Q15 to Q31\r
842 memset(tempTxPtr, 0, sizeof(short));\r
843 tempTxPtr += sizeof(short);\r
844 // copy the mics_in[4] to the right channel of TX buffer\r
845 memcpy(tempTxPtr, tempMicPtr, sizeof(short));\r
846 tempTxPtr += sizeof(short);\r
847 }\r
848 // move to next sample\r
849 tempOutPtr += sizeof(short);\r
850 tempMicPtr += sizeof(short);\r
851 }\r
852 #else // 7 to 2 Loopback Path\r
853 // copy RX mic 1 to TX left channel and RX mic 5 to TX right channel\r
854 // set the RX pointer to mic 1\r
855 tempRxPtr = rxBuf[grxFrameIndexCount];\r
856 // set the TX pointer to left cahhnel\r
857 tempTxPtr = txBuf[gtxFrameIndexCount];\r
858 // copy RX mic 1 to TX left channel\r
859 for (i=0; i<BUFLEN/2; i++)\r
860 {\r
861 // copy the left channel of first serializer to the left channel of TX buffer\r
862 memcpy(tempTxPtr, tempRxPtr, 4);\r
863 tempTxPtr += 4;\r
864 // copy the left channel of second serializer to the right channel of TX buffer\r
865 memcpy(tempTxPtr, tempRxPtr+BUFSIZE, 4);\r
866 tempTxPtr += 4;\r
867 tempRxPtr += RX_NUM_SERIALIZER*2;\r
868 }\r
869 #endif // Signal Processing Path\r
870 #endif // Mcasp_BufferFormat\r
871 #endif // CMB_AUDIO_DAC\r
872 \r
873 // Audio debug dump\r
874 if (gAudDumpBufIdx<FRAME_PER_SEC*DUMP_SEC)\r
875 {\r
876 // copy the MSS output\r
877 ///memcpy((Ptr)(&gAudDumpBuf[gAudDumpBufIdx*BUFSIZE/(SYS_FS_RATIO*2*2)]), outBuf[gtxFrameIndexCount], (BUFSIZE/(SYS_FS_RATIO*2*2)));\r
878 \r
879 // copy first down-sampled and converted channel (L channel for first serializer)\r
880 ///memcpy((Ptr)(&gAudDumpBuf[gAudDumpBufIdx*BUFSIZE/(SYS_FS_RATIO*2*2)]), mics_in[0], (BUFSIZE/(SYS_FS_RATIO*2*2)));\r
881 \r
882 // copy RX all 8 channels (L/R channels for all 4 serializers)\r
883 ///memcpy((Ptr)(&gAudDumpBuf[gAudDumpBufIdx*BUFSIZE*RX_NUM_SERIALIZER]), rxBuf[grxFrameIndexCount], (BUFSIZE * RX_NUM_SERIALIZER));\r
884 \r
885 // copy RX first channel (L channel for the first serializer)\r
886 ///memcpy((Ptr)(&gAudDumpBuf[gAudDumpBufIdx*(BUFSIZE/2)]), rxBuf[grxFrameIndexCount], (BUFSIZE/2));\r
887 \r
888 // copy TX buffer\r
889 ///memcpy((Ptr)(&gAudDumpBuf[gAudDumpBufIdx*(BUFSIZE)]), txBuf[gtxFrameIndexCount], BUFSIZE);\r
890 \r
891 gAudDumpBufIdx++;\r
892 }\r
893 \r
894 #if (CMB_AUDIO_DAC)\r
895 /* Issue full buffer to the output stream */\r
896 /* TX frame processing */\r
897 txFrame[gtxFrameIndexCount].cmd = MCASP_WRITE;\r
898 txFrame[gtxFrameIndexCount].addr = (void*)(getGlobalAddr(txBuf[gtxFrameIndexCount]));\r
899 txFrame[gtxFrameIndexCount].size = (BUFSIZE * TX_NUM_SERIALIZER);\r
900 txFrame[gtxFrameIndexCount].arg = (uint32_t) mcaspTxChanArg;\r
901 txFrame[gtxFrameIndexCount].status = 0;\r
902 txFrame[gtxFrameIndexCount].misc = 1; /* reserved - used in callback to indicate asynch packet */\r
903 \r
904 mcaspSubmitChan(hMcaspTxChan, &txFrame[gtxFrameIndexCount]);\r
905 #endif\r
906 \r
907 /* Issue an empty buffer to the input stream */\r
908 rxFrame[grxFrameIndexCount].cmd = MCASP_READ;\r
909 rxFrame[grxFrameIndexCount].addr = (void*)(getGlobalAddr(rxBuf[grxFrameIndexCount]));\r
910 rxFrame[grxFrameIndexCount].size = (BUFSIZE * RX_NUM_SERIALIZER);\r
911 rxFrame[grxFrameIndexCount].arg = (uint32_t) mcaspRxChanArg;\r
912 rxFrame[grxFrameIndexCount].status = 0;\r
913 rxFrame[grxFrameIndexCount].misc = 1; /* reserved - used in callback to indicate asynch packet */\r
914 \r
915 mcaspSubmitChan(hMcaspRxChan, &rxFrame[grxFrameIndexCount]);\r
916 }\r
917 \r
918 testRet(0);\r
919 }\r
920 \r
921 /* Nothing past this point */\r