]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/audio-preprocessing.git/blob - realtime_demo_bios/k2g/src/main.c
audio-preprocessing-fw: change real time demo to use the cmbk2g library
[processor-sdk/audio-preprocessing.git] / realtime_demo_bios / k2g / src / main.c
1 /*\r
2  * Copyright (c) 2015, 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      main.c\r
36  *\r
37  * \brief     Audio DC analog test main file\r
38  *\r
39  *  Audio analog test verifies the functionality of ADC and DAC available on\r
40  *  audio daughter card. During this test, data received from ADC will be\r
41  *  sent to DAC. There are two instances of DAC and ADC available on\r
42  *  audio daughter card. One ADC supports 4 analog channels and one DAC\r
43  *  supports 8 analog channels. Data received from one DAC shall be sent\r
44  *  to both the ADCs making 8 channel Rx/Tx. Each DAC should be validated\r
45  *  separately.\r
46  *\r
47  */\r
48 \r
49 #include "analog_test.h"\r
50 #include "board.h"\r
51 \r
52 /* System header files */\r
53 #include <xdc/std.h>\r
54 \r
55 #include <xdc/runtime/Error.h>\r
56 #include <xdc/runtime/System.h>\r
57 \r
58 /*----------------------------------------\r
59  * BIOS header files\r
60  *----------------------------------------*/\r
61 \r
62 #include <ti/sysbios/BIOS.h>            /* mandatory - if you call APIs like BIOS_start() */\r
63 \r
64 /* Portable data types */\r
65 #include <ti/mas/types/types.h>\r
66 #include <ti/mas/util/ecomem.h>\r
67 \r
68 #include "../../../common/components/fil.h"\r
69 #include "../../../common/components/sys.h"\r
70 \r
71 /* Global System Configuration Structure */\r
72 sysConfig_t sysConfig = {\r
73   SYS_MICS_MAX,   /* #microphones */\r
74   SYS_VMICS_MAX,  /* #virtual microphones */\r
75   FALSE,          /* Do not use file I/O to load files (must use GEL to load into memory) */\r
76   TRUE            /* Use default angles for microphone configurations */\r
77 };\r
78 \r
79 /* Global FILE I/O Configuration Structure (must be configured even when file I/O not used */\r
80 filConfig_t filConfig = {\r
81   FIL_LENGTH_MAX,   /* number of samples to load from a file */\r
82   SYS_MICS_MAX,     /* number of files to use */\r
83   TRUE,             /* big endian */\r
84   FALSE,            /* do not wrap around */\r
85   40*SYS_FS_HZ      /* Process 40s of the signal (current mic input file length */\r
86 };\r
87 \r
88 /* Acoustic environment */\r
89 /* Room Size (X,Y,Z) in meters */\r
90 /*    5.2000    4.3000    2.7000  */\r
91 \r
92 /* Table size (X,Y,Z) in meters */\r
93 /*    3.0000    2.4000    0.7600  */\r
94 \r
95 /* "Echo" generating speaker position (X,Y,Z) in meters */\r
96 /*    0         2.1500    1.2172     180  -90   VM-9 */\r
97 \r
98 /* People positions (X,Y,Z) in meters and angles relative to the table and array  */\r
99 /*    1.8200    0.6452    1.2172  position#1  (person#1)   -123.6056 -146.3944 VM-7\r
100       2.8200    0.6452    1.2172  -not used-                -90.0000  180.0000 VM-6\r
101       3.8200    0.6452    1.2172              (person#2)    -56.3944  146.3944 VM-5\r
102       1.8200    3.6548    1.2172              (person#3)    123.6056  -33.6056 VM-11\r
103       2.8200    3.6548    1.2172  -not used-                 90.0000    0.0000 VM-0\r
104       3.8200    3.6548    1.2172  position#6  (person#4)     56.3944   33.6056 VM-1\r
105 */\r
106 \r
107 /* Microphone array mic positions (X,Y,Z) in meters */\r
108 /*    2.7922    2.1500    0.7600  mic#1 ("left" most)    180\r
109       2.8004    2.1304    0.7600                        -135\r
110       2.8200    2.1222    0.7600        ("lower" most)  -90\r
111       2.8396    2.1304    0.7600                        -45\r
112       2.8478    2.1500    0.7600        ("right" most)   0\r
113       2.8396    2.1696    0.7600                         45\r
114       2.8200    2.1778    0.7600        ("upper" most)   90\r
115       2.8004    2.1696    0.7600  mic#8                  135\r
116 */\r
117 /* Array Center = (2.82, 2.15, 0.76) (same as table center) */\r
118 \r
119 /* Noise source position (X,Y,Z) in meters */\r
120 /*    2.6000    2.1500    2.3000  */\r
121 \r
122 /* Initialize FILE I/O, system context */\r
123 static void system_init(void)\r
124 {\r
125   int err;\r
126 \r
127   err = sysCreate(&sysConfig);        /* Create system context */\r
128   SYS_CHECK_ERROR(err);\r
129 \r
130   err = sysPrintConfig(SYSM_SCOPE_STATIC|SYSM_SCOPE_DYNAMIC);\r
131   SYS_CHECK_ERROR(err);\r
132 \r
133 } /* system_init */\r
134 \r
135 /* ADC default configuration parameters */\r
136 Cmbk2gAdcConfig adcCfg =\r
137 {\r
138         80,                  /* ADC gain */\r
139         CMBK2G_ADC_INL_SE_VINL1,    /* Left input mux for ADC1L */\r
140         CMBK2G_ADC_INL_SE_VINL2,    /* Left input mux for ADC2L */\r
141         CMBK2G_ADC_INR_SE_VINR1,    /* Right input mux for ADC1R */\r
142         CMBK2G_ADC_INR_SE_VINR2,    /* Right input mux for ADC2R */\r
143         CMBK2G_ADC_RX_WLEN_24BIT,   /* ADC word length */\r
144         CMBK2G_ADC_DATA_FORMAT_I2S, /* ADC data format */\r
145         0\r
146 };\r
147 \r
148 /* DAC default configuration parameters */\r
149 Cmbk2gDacConfig  dacCfg =\r
150 {\r
151         CMBK2G_DAC_AMUTE_CTRL_SCKI_LOST,   /* Amute event */\r
152         0,                          /* Amute control */\r
153         CMBK2G_DAC_SAMPLING_MODE_AUTO,     /* Sampling mode */\r
154         CMBK2G_DAC_DATA_FORMAT_I2S,        /* Data format */\r
155         0,                          /* Soft mute control */\r
156         CMBK2G_DAC_ATTENUATION_WIDE_RANGE, /* Attenuation mode */\r
157         CMBK2G_DAC_DEEMP_44KHZ,            /* De-emph control */\r
158         80                          /* Volume */\r
159 };\r
160 \r
161 typedef enum {\r
162     PADCONFIG_MUX_MODE_PRIMARY = 0,  /**< Primary pin mux mode    */\r
163     PADCONFIG_MUX_MODE_SECONDARY,    /**< Secondary pin mux mode  */\r
164     PADCONFIG_MUX_MODE_TERTIARY,     /**< Tertiary pin mux mode   */\r
165     PADCONFIG_MUX_MODE_QUATERNARY,   /**< Quaternary pin mux mode */\r
166     PADCONFIG_MUX_MODE_QUINARY,      /**< Quinary pin mux mode    */\r
167     PADCONFIG_MUX_MODE_SENARY        /**< Senary pin mux mode     */\r
168 \r
169 } PadCfgMuxMode;\r
170 \r
171 /*\r
172  * Function to enable the pinmux for the mcasp and i2c devices in the soc.\r
173  *\r
174  */\r
175 void pinMuxSetMode_local(uint32_t padCfgId, PadCfgMuxMode muxMode)\r
176 {\r
177         volatile uint32_t *padCfgBase;\r
178 \r
179     padCfgBase = &hBootCfg->PADCONFIG0;\r
180     CSL_FINS(*(padCfgBase + padCfgId), BOOTCFG_PADCONFIG0_MUXMODE, muxMode);\r
181 }\r
182 \r
183 char write_buffer[MAX_WRITE_LEN];\r
184 void cmbk2g_write(const char *fmt, ... )\r
185 {\r
186         va_list         arg_ptr;\r
187         uint32_t        length;\r
188 \r
189         /* Initial platform_write to temporary buffer.. at least try some sort of sanity check so we don't write all over\r
190          * memory if the print is too large.\r
191          */\r
192         if (strlen(fmt) > MAX_WRITE_LEN) {return;}\r
193 \r
194         va_start( arg_ptr, fmt );\r
195         length = vsprintf( (char *)write_buffer, fmt, arg_ptr );\r
196         va_end( arg_ptr );\r
197 \r
198         printf( "%s", write_buffer );\r
199         //fflush(stdout);\r
200 \r
201         return;\r
202 }\r
203 \r
204 void McaspDevice_init(void);\r
205 \r
206 /**\r
207  *  \brief    Initializes platform specific modules\r
208  *\r
209  *  This function initializes the modules like PLL, DDR, I2C etc\r
210  *  which are required for audio processing. Need to call this function\r
211  *  before doing any HW related operations.\r
212  *\r
213  *  \return    Cmbk2g_EOK on Success or error code\r
214  */\r
215 Cmbk2g_STATUS initPlatform(void)\r
216 {\r
217         Board_STATUS status = BOARD_SOK;\r
218         Board_initCfg arg = BOARD_INIT_PINMUX_CONFIG | BOARD_INIT_MODULE_CLOCK;\r
219         status = Board_init(arg);\r
220 \r
221 #if 1\r
222         /* Configure McASP AUXCLK source as AUDIO_OSCCLK  */\r
223         hBootCfg->SERIALPORT_CLKCTL = 0;\r
224         /* Enable audio oscillator  */\r
225         hBootCfg->OSC_CTL = 0x200;      //SW2:SW1 = 01 (15 - 30 MHz)\r
226 \r
227         {\r
228                 int padCfg;\r
229                 for (padCfg = 139; padCfg <= 191; padCfg++)\r
230                 {\r
231                         pinMuxSetMode_local(padCfg, PADCONFIG_MUX_MODE_QUINARY);\r
232                 }\r
233         }\r
234 #endif\r
235 \r
236          // I2C init\r
237     /* Initialize all the I2C ports - Needed for different I2C devices on the board */\r
238         evmI2CInit(I2C_PORT_0);\r
239         evmI2CInit(I2C_PORT_1);\r
240         evmI2CInit(I2C_PORT_2);\r
241 \r
242         ///pinMuxSetMode(125, PADCONFIG_MUX_MODE_QUATERNARY);\r
243         ///gpioSetDirection(GPIO_PORT_1, 54, GPIO_OUT);\r
244         ///gpioSetOutput(GPIO_PORT_1, 54);\r
245 \r
246         /* Configure platform log messages to standard printf */\r
247     ///Cmbk2g_write_configure(Cmbk2g_WRITE_UART);\r
248 \r
249     /* Initialize UART */\r
250     ///Cmbk2g_uart_init();\r
251     ///Cmbk2g_uart_set_baudrate(115200);\r
252 \r
253     return(status);\r
254 }\r
255 \r
256 /**\r
257  *  \brief    Audio analog test main function\r
258  *\r
259  *  \return    none\r
260  */\r
261 void main (void)\r
262 {\r
263         Cmbk2g_STATUS status;\r
264 \r
265         status = initPlatform();\r
266         if(status != Cmbk2g_EOK)\r
267         {\r
268                 cmbk2g_write("Platform Init Failed!\n");\r
269                 testRet(1);\r
270         }\r
271 \r
272         cmbk2g_delay(10000);\r
273 \r
274 \r
275         system_init();      /* initialize system context, etc. */\r
276 \r
277         sysBfCreate();      /* Create beamformers */\r
278         sysAsnrCreate();    /* Create ASNR's */\r
279         sysMssCreate();     /* Create MSS */\r
280         ///sysVauCreate();     /* Create VAU */\r
281 \r
282 \r
283         cmbk2g_write("\n******************************************\n");\r
284         cmbk2g_write(  "        CMB Analog Loopback Test          \n");\r
285     cmbk2g_write(  "******************************************\n");\r
286 \r
287     cmbk2g_write("\nTest Verifies ADC and DAC Channels\n");\r
288         cmbk2g_write("Test Runs in Audio Loopback Mode\n");\r
289         cmbk2g_write("Confirm that CMB Mic 1 and Mic 8 Input are Played at On-Board Line-Out Left and Right Channels\n");\r
290 \r
291         /* Initialize McASP HW details */\r
292         McaspDevice_init();\r
293 \r
294         /* Configure eDMA module */\r
295         status = eDmaConfig();\r
296         if(status != Cmbk2g_EOK)\r
297         {\r
298                 cmbk2g_write("eDMA Configuration Failed!\n");\r
299                 testRet(1);\r
300         }\r
301 \r
302         /* Initialize common audio configurations */\r
303         status = (Cmbk2g_STATUS)cmbk2g_AudioInit();\r
304         if(status != Cmbk2g_EOK)\r
305         {\r
306                 cmbk2g_write("Audio Init Failed!\n");\r
307                 testRet(1);\r
308         }\r
309 \r
310         /* Initialize Audio ADC module */\r
311         status = audioAdcConfig(CMBK2G_ADC_DEVICE_0, &adcCfg);\r
312         if(status != Cmbk2g_EOK)\r
313         {\r
314                 cmbk2g_write("Audio ADC0 Configuration Failed!\n");\r
315                 testRet(1);\r
316         }\r
317 \r
318         status = audioAdcConfig(CMBK2G_ADC_DEVICE_1, &adcCfg);\r
319         if(status != Cmbk2g_EOK)\r
320         {\r
321                 cmbk2g_write("Audio ADC1 Configuration Failed!\n");\r
322                 testRet(1);\r
323         }\r
324 \r
325         /* Initialize McASP module */\r
326         status = mcaspAudioConfig();\r
327         if(status != Cmbk2g_EOK)\r
328         {\r
329                 cmbk2g_write("McASP Configuration Failed!\n");\r
330                 testRet(1);\r
331         }\r
332 \r
333         /* Start BIOS execution */\r
334         BIOS_start();\r
335 }\r
336 \r
337 /**\r
338  *  \brief    Configures audio DAC module\r
339  *\r
340  *  \return    none\r
341  */\r
342 #if CMBK2G_AUDIO_DAC\r
343 void configAudioDAC(void)\r
344 {\r
345         Cmbk2g_STATUS status;\r
346 \r
347         /* Initialize Audio DAC0 module */\r
348         status = audioDacConfig(CMBK2G_DAC_DEVICE_0, &dacCfg);\r
349         if(status != Cmbk2g_EOK)\r
350         {\r
351                 cmbk2g_write("Audio DAC0 Configuration Failed!\n");\r
352                 testRet(1);\r
353         }\r
354 }\r
355 #endif\r
356 \r
357 /* Nothing past this point */\r