suono
Dependencies: X_NUCLEO_CCA01M1
Fork of HelloWorld_CCA01M1_mbedOS by
main.cpp@0:8d09369ca839, 2017-02-22 (annotated)
- Committer:
- nikapov
- Date:
- Wed Feb 22 17:43:34 2017 +0100
- Revision:
- 0:8d09369ca839
- Child:
- 4:6bb9e11d322b
First release.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nikapov | 0:8d09369ca839 | 1 | /** |
nikapov | 0:8d09369ca839 | 2 | ****************************************************************************** |
nikapov | 0:8d09369ca839 | 3 | * @file main.cpp |
nikapov | 0:8d09369ca839 | 4 | * @author Davide Aliprandi, STMicroelectronics |
nikapov | 0:8d09369ca839 | 5 | * @version V1.0.0 |
nikapov | 0:8d09369ca839 | 6 | * @date March 25th, 2016 |
nikapov | 0:8d09369ca839 | 7 | * @brief mbed test application for the STMicroelectronics X-NUCLEO-CCA01M1 |
nikapov | 0:8d09369ca839 | 8 | * Sound Terminal Expansion Board. |
nikapov | 0:8d09369ca839 | 9 | ****************************************************************************** |
nikapov | 0:8d09369ca839 | 10 | * @attention |
nikapov | 0:8d09369ca839 | 11 | * |
nikapov | 0:8d09369ca839 | 12 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
nikapov | 0:8d09369ca839 | 13 | * |
nikapov | 0:8d09369ca839 | 14 | * Redistribution and use in source and binary forms, with or without modification, |
nikapov | 0:8d09369ca839 | 15 | * are permitted provided that the following conditions are met: |
nikapov | 0:8d09369ca839 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
nikapov | 0:8d09369ca839 | 17 | * this list of conditions and the following disclaimer. |
nikapov | 0:8d09369ca839 | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
nikapov | 0:8d09369ca839 | 19 | * this list of conditions and the following disclaimer in the documentation |
nikapov | 0:8d09369ca839 | 20 | * and/or other materials provided with the distribution. |
nikapov | 0:8d09369ca839 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
nikapov | 0:8d09369ca839 | 22 | * may be used to endorse or promote products derived from this software |
nikapov | 0:8d09369ca839 | 23 | * without specific prior written permission. |
nikapov | 0:8d09369ca839 | 24 | * |
nikapov | 0:8d09369ca839 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
nikapov | 0:8d09369ca839 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
nikapov | 0:8d09369ca839 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
nikapov | 0:8d09369ca839 | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
nikapov | 0:8d09369ca839 | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
nikapov | 0:8d09369ca839 | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
nikapov | 0:8d09369ca839 | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
nikapov | 0:8d09369ca839 | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
nikapov | 0:8d09369ca839 | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
nikapov | 0:8d09369ca839 | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
nikapov | 0:8d09369ca839 | 35 | * |
nikapov | 0:8d09369ca839 | 36 | ****************************************************************************** |
nikapov | 0:8d09369ca839 | 37 | */ |
nikapov | 0:8d09369ca839 | 38 | |
nikapov | 0:8d09369ca839 | 39 | |
nikapov | 0:8d09369ca839 | 40 | /* Includes ------------------------------------------------------------------*/ |
nikapov | 0:8d09369ca839 | 41 | |
nikapov | 0:8d09369ca839 | 42 | /* mbed specific header files. */ |
nikapov | 0:8d09369ca839 | 43 | #include "mbed.h" |
nikapov | 0:8d09369ca839 | 44 | #include "rtos.h" |
nikapov | 0:8d09369ca839 | 45 | |
nikapov | 0:8d09369ca839 | 46 | |
nikapov | 0:8d09369ca839 | 47 | #if DEVICE_I2S |
nikapov | 0:8d09369ca839 | 48 | |
nikapov | 0:8d09369ca839 | 49 | /* Helper header files. */ |
nikapov | 0:8d09369ca839 | 50 | #include "DevI2C.h" |
nikapov | 0:8d09369ca839 | 51 | |
nikapov | 0:8d09369ca839 | 52 | /* Component specific header files. */ |
nikapov | 0:8d09369ca839 | 53 | #include "sta350bw_class.h" |
nikapov | 0:8d09369ca839 | 54 | |
nikapov | 0:8d09369ca839 | 55 | /* My song header file. */ |
nikapov | 0:8d09369ca839 | 56 | #include "my_song.h" |
nikapov | 0:8d09369ca839 | 57 | |
nikapov | 0:8d09369ca839 | 58 | |
nikapov | 0:8d09369ca839 | 59 | /* Definitions ---------------------------------------------------------------*/ |
nikapov | 0:8d09369ca839 | 60 | |
nikapov | 0:8d09369ca839 | 61 | /* Events. */ |
nikapov | 0:8d09369ca839 | 62 | #define PLAY_STOP_EVENT (0x1) |
nikapov | 0:8d09369ca839 | 63 | |
nikapov | 0:8d09369ca839 | 64 | |
nikapov | 0:8d09369ca839 | 65 | /* Variables -----------------------------------------------------------------*/ |
nikapov | 0:8d09369ca839 | 66 | |
nikapov | 0:8d09369ca839 | 67 | /* Initialization parameters. */ |
nikapov | 0:8d09369ca839 | 68 | STA350BW_Init_t init = |
nikapov | 0:8d09369ca839 | 69 | { |
nikapov | 0:8d09369ca839 | 70 | 32000, /* Default Sampling Frequency [Hz]. */ |
nikapov | 0:8d09369ca839 | 71 | 100 /* Default Volume. */ |
nikapov | 0:8d09369ca839 | 72 | }; |
nikapov | 0:8d09369ca839 | 73 | |
nikapov | 0:8d09369ca839 | 74 | /* Sound Terminal Component. */ |
nikapov | 0:8d09369ca839 | 75 | STA350BW *sound_terminal; |
nikapov | 0:8d09369ca839 | 76 | |
nikapov | 0:8d09369ca839 | 77 | /* Thread to manage I2S peripherals. */ |
nikapov | 0:8d09369ca839 | 78 | static rtos::Thread i2s_bh_daemon; |
nikapov | 0:8d09369ca839 | 79 | |
nikapov | 0:8d09369ca839 | 80 | /* Threads. */ |
nikapov | 0:8d09369ca839 | 81 | Thread *play_stop_thread; |
nikapov | 0:8d09369ca839 | 82 | |
nikapov | 0:8d09369ca839 | 83 | /* User button handling. */ |
nikapov | 0:8d09369ca839 | 84 | InterruptIn event(USER_BUTTON); |
nikapov | 0:8d09369ca839 | 85 | |
nikapov | 0:8d09369ca839 | 86 | |
nikapov | 0:8d09369ca839 | 87 | /* Functions -----------------------------------------------------------------*/ |
nikapov | 0:8d09369ca839 | 88 | |
nikapov | 0:8d09369ca839 | 89 | /** |
nikapov | 0:8d09369ca839 | 90 | * @brief User button handler function. |
nikapov | 0:8d09369ca839 | 91 | * @param None. |
nikapov | 0:8d09369ca839 | 92 | * @retval None. |
nikapov | 0:8d09369ca839 | 93 | */ |
nikapov | 0:8d09369ca839 | 94 | void pressed(void) |
nikapov | 0:8d09369ca839 | 95 | { |
nikapov | 0:8d09369ca839 | 96 | /* Signal to play/stop handler thread. */ |
nikapov | 0:8d09369ca839 | 97 | play_stop_thread->signal_set(PLAY_STOP_EVENT); |
nikapov | 0:8d09369ca839 | 98 | } |
nikapov | 0:8d09369ca839 | 99 | |
nikapov | 0:8d09369ca839 | 100 | /** |
nikapov | 0:8d09369ca839 | 101 | * @brief Play/stop handler function. |
nikapov | 0:8d09369ca839 | 102 | * @param None. |
nikapov | 0:8d09369ca839 | 103 | * @retval None. |
nikapov | 0:8d09369ca839 | 104 | */ |
nikapov | 0:8d09369ca839 | 105 | void PlayStopHandler(void) |
nikapov | 0:8d09369ca839 | 106 | { |
nikapov | 0:8d09369ca839 | 107 | while (true) |
nikapov | 0:8d09369ca839 | 108 | { |
nikapov | 0:8d09369ca839 | 109 | static bool stop = true; |
nikapov | 0:8d09369ca839 | 110 | |
nikapov | 0:8d09369ca839 | 111 | /* Waiting for play/stop events. */ |
nikapov | 0:8d09369ca839 | 112 | play_stop_thread->signal_wait(PLAY_STOP_EVENT); |
nikapov | 0:8d09369ca839 | 113 | |
nikapov | 0:8d09369ca839 | 114 | if (stop) |
nikapov | 0:8d09369ca839 | 115 | sound_terminal->Stop(); |
nikapov | 0:8d09369ca839 | 116 | else |
nikapov | 0:8d09369ca839 | 117 | sound_terminal->Play((int16_t *) my_song, (uint16_t) sizeof(my_song), true); |
nikapov | 0:8d09369ca839 | 118 | printf("--> %s\r\n", stop ? "Stop." : "Playing..."); |
nikapov | 0:8d09369ca839 | 119 | |
nikapov | 0:8d09369ca839 | 120 | stop = !stop; |
nikapov | 0:8d09369ca839 | 121 | } |
nikapov | 0:8d09369ca839 | 122 | } |
nikapov | 0:8d09369ca839 | 123 | |
nikapov | 0:8d09369ca839 | 124 | /** |
nikapov | 0:8d09369ca839 | 125 | * @brief Entry point function of mbedOS. |
nikapov | 0:8d09369ca839 | 126 | * @param None |
nikapov | 0:8d09369ca839 | 127 | * @retval None |
nikapov | 0:8d09369ca839 | 128 | */ |
nikapov | 0:8d09369ca839 | 129 | int main(void) |
nikapov | 0:8d09369ca839 | 130 | { |
nikapov | 0:8d09369ca839 | 131 | /*----- Initialization. -----*/ |
nikapov | 0:8d09369ca839 | 132 | |
nikapov | 0:8d09369ca839 | 133 | /* Initializing I2C bus. */ |
nikapov | 0:8d09369ca839 | 134 | DevI2C *dev_i2c = new DevI2C( |
nikapov | 0:8d09369ca839 | 135 | PB_9, PullUp, |
nikapov | 0:8d09369ca839 | 136 | PB_8, PullUp |
nikapov | 0:8d09369ca839 | 137 | ); |
nikapov | 0:8d09369ca839 | 138 | |
nikapov | 0:8d09369ca839 | 139 | /* Initializing Sound Terminal Component. */ |
nikapov | 0:8d09369ca839 | 140 | #ifndef USE_I2S2 |
nikapov | 0:8d09369ca839 | 141 | sound_terminal = new STA350BW(PA_10, STA350BW_ADDRESS_1, *dev_i2c, PB_15, PB_13, PB_12, NC, PC_6); |
nikapov | 0:8d09369ca839 | 142 | #else |
nikapov | 0:8d09369ca839 | 143 | sound_terminal = new STA350BW(PA_10, STA350BW_ADDRESS_2, *dev_i2c, PC_12, PC_10, PA_4, NC, PC_7); |
nikapov | 0:8d09369ca839 | 144 | #endif |
nikapov | 0:8d09369ca839 | 145 | if (sound_terminal->Init(&init) != COMPONENT_OK) |
nikapov | 0:8d09369ca839 | 146 | { |
nikapov | 0:8d09369ca839 | 147 | error("Initialization of the Sound Terminal Expansion Board failed.\r\n"); |
nikapov | 0:8d09369ca839 | 148 | exit(EXIT_FAILURE); |
nikapov | 0:8d09369ca839 | 149 | } |
nikapov | 0:8d09369ca839 | 150 | |
nikapov | 0:8d09369ca839 | 151 | /* Starting a thread to manage I2S peripherals. */ |
nikapov | 0:8d09369ca839 | 152 | Callback<void()> i2s_bh_task(&I2S::i2s_bh_queue, &events::EventQueue::dispatch_forever); |
nikapov | 0:8d09369ca839 | 153 | i2s_bh_daemon.start(i2s_bh_task); |
nikapov | 0:8d09369ca839 | 154 | |
nikapov | 0:8d09369ca839 | 155 | /* Scheduling the play/stop function. */ |
nikapov | 0:8d09369ca839 | 156 | play_stop_thread = new Thread(); |
nikapov | 0:8d09369ca839 | 157 | osStatus status = play_stop_thread->start(PlayStopHandler); |
nikapov | 0:8d09369ca839 | 158 | if (status != osOK) |
nikapov | 0:8d09369ca839 | 159 | printf("Could not start the play/stop handler thread.\r\n"); |
nikapov | 0:8d09369ca839 | 160 | event.fall(&pressed); |
nikapov | 0:8d09369ca839 | 161 | |
nikapov | 0:8d09369ca839 | 162 | /* Setting Sound Terminal Component's parameters. */ |
nikapov | 0:8d09369ca839 | 163 | sound_terminal->SetFrequency(MY_SONG_AUDIO_FREQUENCY); |
nikapov | 0:8d09369ca839 | 164 | sound_terminal->SetVolume(STA350BW_CHANNEL_MASTER, 60); |
nikapov | 0:8d09369ca839 | 165 | |
nikapov | 0:8d09369ca839 | 166 | /* Printing to the console. */ |
nikapov | 0:8d09369ca839 | 167 | printf("Sound Terminal Application Example\r\n\n"); |
nikapov | 0:8d09369ca839 | 168 | |
nikapov | 0:8d09369ca839 | 169 | |
nikapov | 0:8d09369ca839 | 170 | /*----- Playing. -----*/ |
nikapov | 0:8d09369ca839 | 171 | |
nikapov | 0:8d09369ca839 | 172 | /* Printing to the console. */ |
nikapov | 0:8d09369ca839 | 173 | printf("--> Playing...\r\n"); |
nikapov | 0:8d09369ca839 | 174 | sound_terminal->Play((int16_t *) my_song, (uint16_t) sizeof(my_song), true); |
nikapov | 0:8d09369ca839 | 175 | } |
nikapov | 0:8d09369ca839 | 176 | |
nikapov | 0:8d09369ca839 | 177 | #else // DEVICE_I2S |
nikapov | 0:8d09369ca839 | 178 | |
nikapov | 0:8d09369ca839 | 179 | int main(void) |
nikapov | 0:8d09369ca839 | 180 | { |
nikapov | 0:8d09369ca839 | 181 | printf("The target does not support I2S API.\r\n"); |
nikapov | 0:8d09369ca839 | 182 | } |
nikapov | 0:8d09369ca839 | 183 | |
nikapov | 0:8d09369ca839 | 184 | #endif // DEVICE_I2S |