funzionicchia male thread

Dependencies:   Freetronics_16x2_LCD

Fork of HelloWorld_CCA01M1_mbedOS by vincenzo di capua

Committer:
Davidroid
Date:
Fri Apr 28 11:40:38 2017 +0000
Revision:
7:94e3191477c5
Parent:
5:ac729170df5f
Child:
12:d847a3c0cf76
Aligned to ARM mbed coding style.

Who changed what in which revision?

UserRevisionLine numberNew 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>&copy; 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. */
davide.aliprandi@st.com 5:ac729170df5f 53 #include "STA350BW.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. */
Davidroid 7:94e3191477c5 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 */
davide.aliprandi@st.com 5:ac729170df5f 105 void play_stop_handler(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)
davide.aliprandi@st.com 5:ac729170df5f 115 sound_terminal->stop();
nikapov 0:8d09369ca839 116 else
davide.aliprandi@st.com 5:ac729170df5f 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. */
Davidroid 4:6bb9e11d322b 134 DevI2C *dev_i2c = new DevI2C(PB_9, PB_8);
nikapov 0:8d09369ca839 135
nikapov 0:8d09369ca839 136 /* Initializing Sound Terminal Component. */
nikapov 0:8d09369ca839 137 #ifndef USE_I2S2
nikapov 0:8d09369ca839 138 sound_terminal = new STA350BW(PA_10, STA350BW_ADDRESS_1, *dev_i2c, PB_15, PB_13, PB_12, NC, PC_6);
nikapov 0:8d09369ca839 139 #else
nikapov 0:8d09369ca839 140 sound_terminal = new STA350BW(PA_10, STA350BW_ADDRESS_2, *dev_i2c, PC_12, PC_10, PA_4, NC, PC_7);
nikapov 0:8d09369ca839 141 #endif
davide.aliprandi@st.com 5:ac729170df5f 142 if (sound_terminal->init(&init) != COMPONENT_OK)
nikapov 0:8d09369ca839 143 {
nikapov 0:8d09369ca839 144 error("Initialization of the Sound Terminal Expansion Board failed.\r\n");
nikapov 0:8d09369ca839 145 exit(EXIT_FAILURE);
nikapov 0:8d09369ca839 146 }
nikapov 0:8d09369ca839 147
nikapov 0:8d09369ca839 148 /* Starting a thread to manage I2S peripherals. */
nikapov 0:8d09369ca839 149 Callback<void()> i2s_bh_task(&I2S::i2s_bh_queue, &events::EventQueue::dispatch_forever);
nikapov 0:8d09369ca839 150 i2s_bh_daemon.start(i2s_bh_task);
nikapov 0:8d09369ca839 151
nikapov 0:8d09369ca839 152 /* Scheduling the play/stop function. */
nikapov 0:8d09369ca839 153 play_stop_thread = new Thread();
davide.aliprandi@st.com 5:ac729170df5f 154 osStatus status = play_stop_thread->start(play_stop_handler);
nikapov 0:8d09369ca839 155 if (status != osOK)
nikapov 0:8d09369ca839 156 printf("Could not start the play/stop handler thread.\r\n");
nikapov 0:8d09369ca839 157 event.fall(&pressed);
nikapov 0:8d09369ca839 158
nikapov 0:8d09369ca839 159 /* Setting Sound Terminal Component's parameters. */
davide.aliprandi@st.com 5:ac729170df5f 160 sound_terminal->set_frequency(MY_SONG_AUDIO_FREQUENCY);
davide.aliprandi@st.com 5:ac729170df5f 161 sound_terminal->set_volume(STA350BW_CHANNEL_MASTER, 60);
nikapov 0:8d09369ca839 162
nikapov 0:8d09369ca839 163 /* Printing to the console. */
nikapov 0:8d09369ca839 164 printf("Sound Terminal Application Example\r\n\n");
nikapov 0:8d09369ca839 165
nikapov 0:8d09369ca839 166
nikapov 0:8d09369ca839 167 /*----- Playing. -----*/
nikapov 0:8d09369ca839 168
nikapov 0:8d09369ca839 169 /* Printing to the console. */
nikapov 0:8d09369ca839 170 printf("--> Playing...\r\n");
davide.aliprandi@st.com 5:ac729170df5f 171 sound_terminal->play((int16_t *) my_song, (uint16_t) sizeof(my_song), true);
nikapov 0:8d09369ca839 172 }
nikapov 0:8d09369ca839 173
nikapov 0:8d09369ca839 174 #else // DEVICE_I2S
nikapov 0:8d09369ca839 175
nikapov 0:8d09369ca839 176 int main(void)
nikapov 0:8d09369ca839 177 {
nikapov 0:8d09369ca839 178 printf("The target does not support I2S API.\r\n");
nikapov 0:8d09369ca839 179 }
nikapov 0:8d09369ca839 180
nikapov 0:8d09369ca839 181 #endif // DEVICE_I2S