Simple mbedOS test application for the STMicroelectronics X-NUCLEO-CCA01M1 Sound Terminal Expansion Board, built against mbed OS.

Dependencies:   X_NUCLEO_CCA01M1

Fork of HelloWorld_CCA01M1 by Nicola Capovilla

Playing audio with the X-NUCLEO-CCA01M1 Expansion Board

This application provides a simple example of usage of the X-NUCLEO-CCA01M1 Sound Terminal Expansion Board, built against mbed OS.

It shows how to play a 2-channel stereo signal stored in an array of PCM samples directly on the speakers connected to the expansion board.

It also allows to stop/play the audio by pressing the user button on the Nucleo board.


Platform compatibility

  • This board can be currently used with the Nucleo F4 Family only, please see the ST_I2S library compatibility for further information.
  • The application built against mbed classic 2.x can be found here.

main.cpp

Committer:
Davidroid
Date:
2017-07-12
Revision:
11:088e0fd41b40
Parent:
7:94e3191477c5

File content as of revision 11:088e0fd41b40:

/**
 ******************************************************************************
 * @file    main.cpp
 * @author  Davide Aliprandi, STMicroelectronics
 * @version V1.0.0
 * @date    March 25th, 2016
 * @brief   mbed test application for the STMicroelectronics X-NUCLEO-CCA01M1
 *          Sound Terminal Expansion Board.
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *   1. Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the documentation
 *      and/or other materials provided with the distribution.
 *   3. Neither the name of STMicroelectronics nor the names of its contributors
 *      may be used to endorse or promote products derived from this software
 *      without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************
 */


/* Includes ------------------------------------------------------------------*/

/* mbed specific header files. */
#include "mbed.h"
#include "rtos.h"


#if DEVICE_I2S

/* Helper header files. */
#include "DevI2C.h"

/* Component specific header files. */
#include "STA350BW.h"

/* My song header file. */
#include "my_song.h"


/* Definitions ---------------------------------------------------------------*/

/* Events. */
#define PLAY_STOP_EVENT      (0x1)


/* Variables -----------------------------------------------------------------*/

/* Initialization parameters. */
STA350BW_init_t init =
{
    32000,   /* Default Sampling Frequency [Hz]. */
    100      /* Default Volume. */
};

/* Sound Terminal Component. */
STA350BW *sound_terminal;

/* Thread to manage I2S peripherals. */
static rtos::Thread i2s_bh_daemon;

/* Threads. */
Thread *play_stop_thread;

/* User button handling. */
InterruptIn event(USER_BUTTON);


/* Functions -----------------------------------------------------------------*/

/**
 * @brief  User button handler function.
 * @param  None.
 * @retval None.
 */
void pressed(void)
{
    /* Signal to play/stop handler thread. */
    play_stop_thread->signal_set(PLAY_STOP_EVENT);
}

/**
 * @brief  Play/stop handler function.
 * @param  None.
 * @retval None.
 */
void play_stop_handler(void)
{
    while (true)
    {
        static bool stop = true;

        /* Waiting for play/stop events. */
        play_stop_thread->signal_wait(PLAY_STOP_EVENT);

        if (stop)
            sound_terminal->stop();
        else
            sound_terminal->play((int16_t *) my_song, (uint16_t) sizeof(my_song), true);
        printf("--> %s\r\n", stop ? "Stop." : "Playing...");

        stop = !stop;
    }
}

/**
 * @brief  Entry point function of mbedOS.
 * @param  None
 * @retval None
 */
int main(void)
{
    /*----- Initialization. -----*/

    /* Initializing I2C bus. */
    DevI2C *dev_i2c = new DevI2C(PB_9, PB_8);

    /* Initializing Sound Terminal Component. */
#ifndef USE_I2S2
    sound_terminal = new STA350BW(PA_10, STA350BW_ADDRESS_1, *dev_i2c, PB_15, PB_13, PB_12, NC, PC_6);
#else
    sound_terminal = new STA350BW(PA_10, STA350BW_ADDRESS_2, *dev_i2c, PC_12, PC_10, PA_4, NC, PC_7);
#endif
    if (sound_terminal->init(&init) != COMPONENT_OK)
    {
        error("Initialization of the Sound Terminal Expansion Board failed.\r\n");
        exit(EXIT_FAILURE);
    }

    /* Starting a thread to manage I2S peripherals. */
    Callback<void()> i2s_bh_task(&I2S::i2s_bh_queue, &events::EventQueue::dispatch_forever);
    i2s_bh_daemon.start(i2s_bh_task);

    /* Scheduling the play/stop function. */
    play_stop_thread = new Thread();
    osStatus status = play_stop_thread->start(play_stop_handler);
    if (status != osOK)
        printf("Could not start the play/stop handler thread.\r\n");
    event.fall(&pressed);

    /* Setting Sound Terminal Component's parameters. */
    sound_terminal->set_frequency(MY_SONG_AUDIO_FREQUENCY);
    sound_terminal->set_volume(STA350BW_CHANNEL_MASTER, 60);

    /* Printing to the console. */
    printf("Sound Terminal Application Example\r\n\n");


    /*----- Playing. -----*/

    /* Printing to the console. */
    printf("--> Playing...\r\n");
    sound_terminal->play((int16_t *) my_song, (uint16_t) sizeof(my_song), true);
}

#else // DEVICE_I2S

int main(void)
{
    printf("The target does not support I2S API.\r\n");
}

#endif // DEVICE_I2S