Example application which combines the STMicroelectronics X-NUCLEO-CCA01M1 Sound Terminal Expansion Board and the X-NUCLEO-CCA02M1 MEMS Microphones Expansion Board to realize a simple stereo@32KHz Karaoke system. It is built against mbed OS.

Dependencies:   X_NUCLEO_CCA02M1 X_NUCLEO_CCA01M1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    main.cpp
00004  * @author  Davide Aliprandi, STMicroelectronics
00005  * @version V1.0.0
00006  * @date    November 25th, 2016
00007  * @brief   mbed test application which combines the STMicroelectronics
00008  *          X-NUCLEO-CCA01M1 and the X-NUCLEO-CCA02M1 Expansion Boards.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037  */
00038 
00039 
00040 /* Includes ------------------------------------------------------------------*/
00041 
00042 /* mbed specific header files. */
00043 #include "mbed.h"
00044 #include "rtos.h"
00045 
00046 
00047 #if DEVICE_I2S
00048 
00049 /* Helper header files. */
00050 #include "DevI2C.h"
00051 
00052 /* Component specific header files. */
00053 #include "STA350BW.h"
00054 
00055 /* Expansion Board specific header files. */
00056 #include "XNucleoCCA02M1.h"
00057 
00058 
00059 /* Variables -----------------------------------------------------------------*/
00060 
00061 /* Initialization parameters. */
00062 STA350BW_init_t sound_terminal_init =
00063 {
00064     32000,   /* Default Sampling Frequency [Hz]. */
00065     100      /* Default Volume. */
00066 };
00067 XNucleoCCA02M1_init_t microphones_init =
00068 {
00069     32000,   /* Default Sampling Frequency [Hz]. */
00070     2        /* Default number of channels. */
00071 };
00072 
00073 /* Sound Terminal Component. */
00074 STA350BW *sound_terminal;
00075 
00076 /* MEMS Microphones Expansion Board. */
00077 XNucleoCCA02M1 *microphones;
00078 
00079 /* Thread to manage I2S peripherals. */
00080 static rtos::Thread i2s_bh_daemon;
00081 
00082 
00083 /* Functions -----------------------------------------------------------------*/
00084 
00085 /**
00086  * @brief  Callback to start playing from the sound terminal the PCM buffer
00087  *         filled by the microphones.
00088  * @param  PCM_buffer       Buffer containing the PCM data to play.
00089  * @param  PCM_buffer_bytes Size of the buffer.
00090  * @retval None.
00091  */
00092 void StartPlaying(int16_t *PCM_buffer, uint16_t PCM_buffer_bytes)
00093 {
00094     static bool first_time = true;
00095     if (first_time)
00096     {
00097         /* Start playing from the sound terminal. */
00098         sound_terminal->play(PCM_buffer, PCM_buffer_bytes, true);
00099         first_time = false;
00100     }
00101 }
00102 
00103 /**
00104  * @brief  Entry point function of mbedOS.
00105  * @param  None
00106  * @retval None
00107  */
00108 int main(void)
00109 {
00110     /*----- Initialization. -----*/
00111 
00112     /* Initializing I2C bus. */
00113     DevI2C *dev_i2c = new DevI2C(PB_9, PB_8);
00114 
00115     /* Initializing Sound Terminal Component on the I2S2 interface. */
00116     sound_terminal = new STA350BW(PA_10, STA350BW_ADDRESS_2, *dev_i2c, PC_12, PC_10, PA_4, NC, PC_7);
00117     if (sound_terminal->init(&sound_terminal_init) != COMPONENT_OK)
00118     {
00119         error("Initialization of the Sound Terminal Expansion Board failed.\r\n");
00120         exit(EXIT_FAILURE);
00121     }
00122 
00123     /* Initializing MEMS Microphones Expansion Board on the I2S1 interface. */
00124     microphones = new XNucleoCCA02M1(PB_15, PB_13);
00125     if (microphones->init(&microphones_init) != COMPONENT_OK)
00126     {
00127         error("Initialization of the MEMS Microphones Expansion Board failed.\r\n");
00128         exit(EXIT_FAILURE);
00129     }
00130 
00131     /* Asking the I2S peripherals to work at frequencies one multiple of the
00132        other in order to avoid audio glitches that may be caused by a drift
00133        between the I2S clock signals.*/
00134     int ret = I2S::harmonize(sound_terminal->dev_i2s, microphones->dev_i2s);
00135     if (ret != 0)
00136         error("Unable to synchronize audio frequencies.\r\n");
00137 
00138     /* Starting a thread to manage I2S peripherals. */
00139     Callback<void()> i2s_bh_task(&I2S::i2s_bh_queue, &events::EventQueue::dispatch_forever);
00140     i2s_bh_daemon.start(i2s_bh_task);
00141 
00142     /* Printing to the console. */
00143     printf("Audio Karaoke Application Example\r\n\n");
00144 
00145 
00146     /*----- Recording and playing. -----*/
00147 
00148     /* Printing to the console. */
00149     printf("--> Sing a song and have fun!\r\n");
00150 
00151     /* Attaching a callback which will be called each time the microphones have
00152        recorded 1 ms of audio data, and start recording. */
00153     microphones->attach(&StartPlaying);
00154     microphones->record();
00155 }
00156 
00157 #else // DEVICE_I2S
00158 
00159 int main(void)
00160 {
00161     printf("The target does not support I2S API.\r\n");
00162 }
00163 
00164 #endif // DEVICE_I2S