Kinetis SDK v.1.2 Demo Applications User's Guide  Rev. 0
Freescale Semiconductor, Inc.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
SAI Demo

This demo application demonstrates how to use the SAI drivers.

Overview

The SAI Demo project is a digital audio demonstration program that uses the KSDK software. It performs audio playback from either a .wav file, stored in Flash, or from the line-in on a TWR-AUDIO-SGTL Tower System module using the KSDK I2S and I2C drivers. On the TWR-K22F120M, TWR-K24F120M, and the TWR-K64F120M Tower System modules, the project also uses the CMSIS-DSP library to perform a Fast Fourier Transform, and return the fundamental frequency of the line-in audio.

Supported Hardware

This demo supports the following Freescale Freedom development platforms and Tower System modules:

System Requirement

Hardware requirements

Toolchain requirements

Software requirements

Getting Started

GCC Compiler notes

When building the demo with GCC, ensure that the demo and platform library are built with this option:

<code>
    <br>CHOOSE_FLOAT=HARD_FP<br>
</code>

Otherwise, the project does not use the Kinetis device's hardware floating point when using the CMSIS-DSP library.

Hardware Settings

These Tower System modules are required to run the sai_demo:

Prepare the Demo

  1. Connect a USB cable between the PC host and the OpenSDA USB port on the board.
  2. Open a serial terminal with these settings:
    • 115200 baud rate
    • 8 data bits
    • No parity
    • One stop bit
    • No flow control
  3. Download the program to the target board.
  4. Either press the reset button on your board or launch the debugger in your IDE to begin running the demo.

For detailed instructions, see a Kinetis SDK User's Guide for your board.

Run the demo

To hear the audio playback, connect a set of headphones to the headphone output on the TWR-AUDIO-SGTL card. For input to the codec, connect an audio source to the Line-In on the TWR-AUDIO-SGTL.

When the demo starts, this message is displayed in the terminal output window:

Audio Demo!
Press spacebar to start demo.
Demo begin...

The user can either play back audio from the line-in source, or play a .wav file stored in the Flash.

The line-in option plays the audio gathered from the codec line-in for approximately 15 seconds.

Select player:
        1. Line-In Playback
        2. Wav File Playback
->1

If selecting playback from the line-in source, decide whether to perform an FFT analysis to find the fundamental frequency of the audio input. Finding the fundamental frequency is best suited for pure tones played into the line-in of the TWR-AUDIO-SGTL card.

Select filter:
        1. FFT - Find Fundamental Frequency
        2. None
->1

The user is prompted to select from a list of headphone output levels:

Choose headphone dB level:
        1. +3.0 dB
        2. 0.0 dB
        3. -3.0 dB
        4. -6.0 dB
        5. -12.0 dB
        6. -24.0 dB
        7. -48.0 dB
->5
Frequency is 93 Hz

The table shows the terminal display after playback has completed and the FFT option was selected.

These are the options for the .wav file option:

Select player:
        1. Line-In Playback
        2. Wav File Playback
->2
Select Wav file:
        1. Audio Demo
->1
Choose headphone dB level:
        1. +3.0 dB
        2. 0.0 dB
        3. -3.0 dB
        4. -6.0 dB
        5. -12.0 dB
        6. -24.0 dB
        7. -48.0 dB
->5

The quality of the .wav file PCM data depends on the demo system and the compiler.

This table shows the audio sample rate, channels and bit depth of the .wav file for the various platforms and compilers.

Hardware System Sample Rate (kHz) Bit Depth Channels
IAR ARM GNU-GCC KDS-GCC IAR ARM GNU-GCC KDS-GCC IAR ARM GNU-GCC KDS-GCC
TWR-K22F120M 44.1 44.1 11.025 11.025 16 16 16 16 2 2 2 2
TWR-K24F120M 44.1 44.1 44.1 44.1 32 32 32 32 2 2 2 2
TWR-K60F100M 44.1 44.1 44.1 44.1 32 32 32 32 2 2 2 2
TWR-K64F120M 44.1 44.1 44.1 44.1 32 32 32 32 2 2 2 2
TWR-K65F180M 44.1 44.1 44.1 44.1 32 32 32 32 2 2 2 2
TWR-K21F120M 44.1 44.1 11.025 11.025 16 16 16 16 2 2 2 2

Quality differences of the .wav playback depend on the size constraints of the target device, the Flash size, and the density of the code generated by the compiler.

Note that all supported platforms play audio from the line-in option with the same quality: 16-bit, 44.1 kHz, 2 channels.

Key Functions

void audio_stream_init(void)

Initializes the I2S, I2C, and TWR-AUDIO-SGTL Tower System module for streaming audio from Line-In.

void audio_wav_init(wave_file_t *newWav)

Initializes the I2S, I2C, and TWR-AUDIO-SGTL Tower System module for playing back WAV file in Flash.

Parameters
newWavPointer to wave file data structure.

uint32_t config_volume(sgtl_handler_t *handler, sgtl_module_t module, uint32_t volumeCtrl)

Sets volume from the user input.

Parameters
handlerpointer to codec handler structure.
modulename of module on codec to set the volume for.
volumeCtrluser input data from terminal menu.
Returns
status_t Return kStatus_Success if function completed successfully, return kStatusFail if function failed.

snd_status_t stream_audio(dsp_types_t dspType, uint8_t volumeCtrl)

Plays a stream of audio.

Parameters
dspTypeUsed to select one DSP function to perform on the data.
volumeCtrlValue used to set decibel level on codec.
Returns
Returns soundcard status

snd_status_t get_wav_data(wave_file_t *waveFile)

Collects data from WAV file header.

Parameters
waveFileData structure of pcm data array.
Returns
status_t Return kStatus_Success if function completed successfully, return kStatusFail if function failed.

snd_status_t play_wav(uint32_t *pcmBuffer, uint8_t volumeCtrl)

Plays the PCM audio data from the WAV format array.

Parameters
pcmBufferPointer to data array containing WAV formatted audio data.
volumeCtrlValue used to set decibel level on codec.
Returns
status_t Return kStatus_Success if function completed successfully, return kStatusFail if function failed.

void send_wav(uint8_t *dataBuffer, uint32_t length, sai_data_format_t *dataFormat)

Sends audio data to the sound card.

Parameters
pdataBufferPointer to data array containing WAV formatted audio data.
lengthlength of WAV file to send.
dataFormatPoint to audio_data_format_t for sound card.

float32_t do_fft(sai_data_format_t *dataFormat, uint8_t *buffer, float32_t *fftData, float32_t *fftResult)

Performs frequency analysis and finds fundamental frequency of the PCM data.

Parameters
dataFormatPointer to audio data format structure.
bufferPointer to data array to store modulated PCM data.
fftDataPointer to data array for storing Fast Fourier Transform data.
fftResultPoint to data array for storing real frequency bins from FFT.
Returns
float32_t Returns fundamental frequency in Hz.