Library to handle the X-NUCLEO-CCA02M1 MEMS Microphones Expansion Board.

Dependencies:   ST_I2S ST_FREQUENCY_DIVIDER USBDEVICE

Dependents:   HelloWorld_CCA02M1 HelloWorld_CCA02M1_mbedOS HelloWorld_CCA02M1 Karaoke_CCA01M1_CCA02M1_mbedOS

Fork of X_NUCLEO_CCA02M1 by ST Expansion SW Team

MEMS Microphones Library

Library to handle the X-NUCLEO-CCA02M1 MEMS Microphones Expansion Board. A single board allows to record a standard 2-channel stereo signal as an array of PCM samples (16 bit/sample); in principle, it could make use of six additional MEMS microphones to realize a 8-channel audio system.


Microphones configuration

Currently the configurations supported are the following:

  • Stereo@48KHz
  • Stereo@44.1KHz (CD audio quality)
  • Stereo@32KHz
  • Stereo@16KHz
  • Stereo@8KHz
  • Mono@48KHz
  • Mono@44.1KHz
  • Mono@32KHz
  • Mono@16KHz
  • Mono@8KHz

Mono configurations need a Jumper connecting PB_5 and PB_13 on the Morpho connector to properly work.


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 library is compatible both with mbed OS 5.x and mbed classic 2.x (to work with mbed classic, the main application has to import the "events" library, which is not included into the "mbed" library).


I2S Peripheral Usage

By default this board makes use of the I2S peripheral available on Nucleo boards.


Acquiring through the USB

In order to acquire the recorded PCM audio channel with an audio SW on a PC, please connect the expansion board to a USB port of the PC, and the Nucleo board to a USB power supply.

BSP/PDM2PCMAudio.cpp

Committer:
davide.aliprandi@st.com
Date:
2017-04-21
Revision:
7:9d6a4a53e640
Parent:
2:9f389fd8fb2e
Child:
11:b2f7f79026e4

File content as of revision 7:9d6a4a53e640:

/**
 ******************************************************************************
 * @file    PDM2PCMAudio_class.cpp
 * @author  AST / Software Platforms and Cloud
 * @version V1.0
 * @date    November 10th, 2016
 * @brief   Implementation file for the PDM2PCMAudio conversion library.
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2015 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 ------------------------------------------------------------------*/

#include "PDM2PCMAudio.h"


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

/* Demux filter. */
const uint8_t PDM2PCMAudio::_demux_filter[DEMUX_FILTER_SIZE] =
{
    0x00, 0x01, 0x00, 0x01, 0x02, 0x03, 0x02, 0x03,
    0x00, 0x01, 0x00, 0x01, 0x02, 0x03, 0x02, 0x03,
    0x04, 0x05, 0x04, 0x05, 0x06, 0x07, 0x06, 0x07,
    0x04, 0x05, 0x04, 0x05, 0x06, 0x07, 0x06, 0x07,
    0x00, 0x01, 0x00, 0x01, 0x02, 0x03, 0x02, 0x03,
    0x00, 0x01, 0x00, 0x01, 0x02, 0x03, 0x02, 0x03,
    0x04, 0x05, 0x04, 0x05, 0x06, 0x07, 0x06, 0x07,
    0x04, 0x05, 0x04, 0x05, 0x06, 0x07, 0x06, 0x07,
    0x08, 0x09, 0x08, 0x09, 0x0a, 0x0b, 0x0a, 0x0b,
    0x08, 0x09, 0x08, 0x09, 0x0a, 0x0b, 0x0a, 0x0b,
    0x0c, 0x0d, 0x0c, 0x0d, 0x0e, 0x0f, 0x0e, 0x0f,
    0x0c, 0x0d, 0x0c, 0x0d, 0x0e, 0x0f, 0x0e, 0x0f,
    0x08, 0x09, 0x08, 0x09, 0x0a, 0x0b, 0x0a, 0x0b,
    0x08, 0x09, 0x08, 0x09, 0x0a, 0x0b, 0x0a, 0x0b,
    0x0c, 0x0d, 0x0c, 0x0d, 0x0e, 0x0f, 0x0e, 0x0f,
    0x0c, 0x0d, 0x0c, 0x0d, 0x0e, 0x0f, 0x0e, 0x0f
};


/* Methods -------------------------------------------------------------------*/

/**
* @brief  Converting audio data from PDM to PCM.
* @param  output_buffer     Pointer to output PCM buffer data.
* @param  input_buffer      Pointer to input PDM buffer data.
* @param  volume            Volume level (it must be in the range [0..64]).
* @param  decimation_factor Decimation factor (it must be either 64 or 128).
* @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise.
*/
status_t PDM2PCMAudio::convert(int16_t *output_buffer, uint16_t *input_buffer, uint32_t volume, uint32_t decimation_factor)
{
    if (!(volume <= PDM2PCM_MAX_VOLUME))
        error("Volume level not supported: it must be in the range [0..64].\r\n");

    switch (decimation_factor)
    {
        case 64:
            for (uint32_t index = 0; index < _channels; index++) {
#ifdef PDM2PCM_AUDIO_DEBUG
                _pdm2pcm_audio_signal = 1;
#endif
#ifdef USE_OPEN_PDM2PCM_LIBRARY
                Open_PDM_Filter(&((uint8_t *) input_buffer)[index], (uint16_t *) &(output_buffer[index]), volume, (TPDMFilter_InitStruct *) &_PDM2PCM_filter[index]);
                index++;
#else                
                PDM_Filter_64_LSB(&((uint8_t *) input_buffer)[index], (uint16_t *) &(output_buffer[index]), volume, (PDMFilter_InitStruct *) &_PDM2PCM_filter[index]);
#endif
#ifdef PDM2PCM_AUDIO_DEBUG
                _pdm2pcm_audio_signal = 0;
#endif
            }
            break;

        case 128:
            for (uint32_t index = 0; index < _channels; index++) {
#ifdef PDM2PCM_AUDIO_DEBUG
                _pdm2pcm_audio_signal = 1;
#endif
#ifdef USE_OPEN_PDM2PCM_LIBRARY
                Open_PDM_Filter(&((uint8_t *) input_buffer)[index], (uint16_t *) &(output_buffer[index]), volume, (TPDMFilter_InitStruct *) &_PDM2PCM_filter[index]);
                index++;
#else                
                PDM_Filter_128_LSB(&((uint8_t *) input_buffer)[index], (uint16_t *) &(output_buffer[index]), volume, (PDMFilter_InitStruct *) &_PDM2PCM_filter[index]);
#endif
#ifdef PDM2PCM_AUDIO_DEBUG
                _pdm2pcm_audio_signal = 0;
#endif
            }
            break;

        default:
            error("Decimation factor not supported: it must be either 64 or 128.\r\n");
    }

    return COMPONENT_OK;
}

/**
 * @brief  Scrambling audio data.
 * @param  output_buffer Pointer to output PDM buffer data.
 * @param  input_buffer  Pointer to input PDM buffer data.
 * @param  size          Size of the buffers (thay has to be equally sized).
 * @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise.
 */
status_t PDM2PCMAudio::scramble(uint16_t *output_buffer, uint16_t *input_buffer, uint32_t size)
{
    for (uint32_t index = 0; index < size; index++) {
        output_buffer[index] = HTONS(input_buffer[index]);
    }

    return COMPONENT_OK;
}

/**s
 * @brief  Demuxing audio data.
 * @param  output_buffer Pointer to output PDM buffer data.
 * @param  input_buffer  Pointer to input PDM buffer data.
 * @param  size          Size of the buffers (thay has to be equally sized).
 * @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise.
 */
status_t PDM2PCMAudio::demux(uint16_t *output_buffer, uint16_t *input_buffer, uint32_t size)
{
    for (uint32_t index = 0; index < size; index++) {
        uint8_t a = ((uint8_t *) input_buffer)[index * 2];
        uint8_t b = ((uint8_t *) input_buffer)[index * 2 + 1];
        ((uint8_t *) (output_buffer))[index * 2] = _demux_filter[a & DEMUX_FILTER_MASK] | _demux_filter[b & DEMUX_FILTER_MASK] << 4;
        ((uint8_t *) (output_buffer))[index * 2 + 1] = _demux_filter[(a >> 1) & DEMUX_FILTER_MASK] | _demux_filter[(b >> 1) & DEMUX_FILTER_MASK] << 4;
    }

    return COMPONENT_OK;
}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/