SAI for Kinetis K Series MCUs: Serial Audio Interface and I2S Training

Revision 1.0

Introduction

Hello, and welcome to this presentation of the Serial Audio Interface (SAI) module for Kinetis K series MCUs. You may also have heard SAI referenced as Integrated Interchip Sound (I2S). These terms are all used interchangeably. In this session, you'll learn about the SAI, its main features, and the application benefits of leveraging this function.

Agenda

This session will cover:

1. Module Overview

First, let's start with an overview of the module.

SAI Features and Application Benefits

Features:

Application Benefits:

SAI Module Block Diagram

The SAI module has independent clocks for the transmitter and receiver, allowing both synchronous or asynchronous operation.

Diagram Description: The block diagram shows the SAI module connected to the MCG (Microcontroller Clock Generator) and DMA MUX (Direct Memory Access Multiplexer). The SAI module itself is divided into Transmitter (TX) and Receiver (RX) paths. Each path includes Control Registers, a FIFO buffer, Bit Clock Generation, and a Shift Register. The Transmitter receives data from the FIFO, synchronizes it with the Bit Clock and Frame Sync, and outputs it via SAI_TX_DATA. The Receiver takes incoming data (SAI_RX_DATA), synchronizes it, and stores it in the FIFO. The diagram also shows various interrupt signals originating from the SAI module, such as FIFO warnings, FIFO requests, word start signals, and sync/FIFO errors. These interrupts are routed to the NVIC (Nested Vectored Interrupt Controller) via the DMA MUX. The module operates using Bus Clock and Audio Clock inputs, and generates or receives MCLK, BCLK, and Frame Sync signals.

I2S is not the only audio protocol that can be used with the SAI; there are variants such as left justified, right justified, PCM, TDM, and others.

SAI Clocking

The master clock (MCLK) to the SAI can be internal or external.

For an external clock source, a master clock in (MCLK_IN) signal is used. In this case, a dedicated oscillator with a frequency multiple of the audio sampling frequency is expected.

Diagram Description: The clocking diagram illustrates clock generation and distribution for the I2S/SAI interface. On the left, the Clock Generation block takes inputs like MCGPLLCLK, OSCOERCLK, and SYSCLK, processes them through a Fractional Clock Divider, and outputs MCLK. MCLK_OUT can be used externally. On the right, the I2S/SAI block receives MCLK (or MCLK_IN) and BUSCLK. It generates BCLK_OUT and uses Frame Sync (FS). The diagram shows connections for TCR2/RCR2, MCR, and MDR registers, which control various clocking aspects like bit clock division, master/slave mode, and multiplexer selections.

I2S Requirements

In order to implement the I2S protocol, the following signals are required:

I2S Communication Model

The I2S communication protocol needs two devices: a master and a slave. The master device provides frame sync and serial clock to the slave.

Diagram Description: The diagram shows two interconnected blocks: "Kinetis MCU" and "Audio Codec". Arrows indicate the flow of signals. MCLK originates from the Kinetis MCU and goes to the Audio Codec. BCLK and FS also originate from the Kinetis MCU and go to the Audio Codec. TX data flows from the Kinetis MCU to the Audio Codec. The labels indicate that the Kinetis MCU is the Master (provides clocks) and the Audio Codec is the Slave (receives clocks).

In this example, the Kinetis MCU is the I2S master and the audio codec is the slave. Note that there is no MCLK going from the master to the slave. The reason is that the MCLK is used to generate the other clocks. This means the master is the only device with MCLK.

I2S Communication Model – Continued

There are some cases where the MCLK must be fed to the slave, but this depends on the overall system configuration.

Diagram Description: This diagram is identical to the previous one, illustrating the Kinetis MCU as Master and the Audio Codec as Slave, with MCLK, BCLK, FS, and TX signals. It serves to reinforce the master/slave concept.

In this situation (referring to a potential alternative configuration not explicitly diagrammed here but implied by the text), the Kinetis MCU would be the slave since it's receiving clocks from the audio codec.

2. On-chip Interconnections and Inter-module Dependencies

Now, let's talk about on-chip interconnections and inter-module dependencies.

SAI Interconnect Diagram

The diagram displays the SAI module and its relation to other peripherals. As previously discussed, the SAI can be connected to different clock options, from internal and external clocks.

Diagram Description: This diagram shows the SAI module as a central component connected to other system blocks. The MCG (Microcontroller Clock Generator) provides clock sources (MCGPLLCLK, SYSCLK, OSCOERCLK) to the SAI CLOCK block, which generates MCLK. MCLK is then supplied to the SAI module. The SAI module has Transmitter (TX) and Receiver (RX) interfaces. The TX interface connects to a DMA MUX (Direct Memory Access Multiplexer) and outputs data. The RX interface receives data and connects to the DMA MUX. The diagram lists various interrupt sources from the SAI module, such as Tx/Rx FIFO Warnings, Tx/Rx FIFO Requests, Tx/Rx Word Start, Tx/Rx Sync Errors, and Tx/Rx FIFO Errors. These interrupts are routed through the DMA MUX to the NVIC (Nested Vectored Interrupt Controller). The diagram also shows the external clock input (MCLK In) and output (MCLK Out) for the SAI CLOCK block.

The module has several interrupt sources for:

Transmitter and receiver have independent interrupt vectors.

FIFO watermark and FIFO full or empty can generate a DMA request to offload the CPU and fill or empty the FIFO in the background.

3. Hardware Configuration

Next, let's go over the hardware configurations of the SAI module.

Master Clock Hardware Configuration | Scenario One

The master clock is used to generate the different clocks for I2S communication. The source and values of this clock vary based on the use case.

Let's assume we have a system that plays back sound, which represents the example shown here. A Kinetis MCU and an audio codec are connected through the I2S bus, and there's an 8MHz crystal connected to the Kinetis MCU.

We'll review four different clock connection scenarios.

Diagram Description: This diagram shows a Kinetis MCU connected to an Audio Codec. An 8MHz clock source is connected to the Kinetis MCU. The Kinetis MCU acts as the I2S master, outputting FS (Frame Sync), BCLK (Bit Clock), and TX (Transmit Data) signals to the Audio Codec. There is no explicit MCLK line shown going to the codec in this scenario.

In the first scenario, the Kinetis MCU is the I2S master and generates the master clock internally. In this use case, just the bit clock and frame sync are connected to the audio codec. The advantage for this scenario is that there's no dedicated crystal, which reduces the bill of materials (BOM) cost. The disadvantage is that the jitter generated by the internal fractional dividers might not be suitable for applications where high audio quality is required.

Master Clock Hardware Configuration | Scenario Two

Diagram Description: This diagram shows a Kinetis MCU connected to an Audio Codec. An external clock source of 12.288MHz is connected to the Kinetis MCU. The Kinetis MCU acts as the I2S master, outputting MCLK, FS (Frame Sync), BCLK (Bit Clock), and TX (Transmit Data) signals to the Audio Codec.

In the second scenario displayed here, the Kinetis MCU is the I2S master, and the master clock is sourced from an external oscillator with a specific frequency, say, 12.288MHz. For this scenario, the advantage is that the jitter noise is reduced, which is suitable for applications where audio quality is key. The disadvantage is a more expensive BOM due to the addition of an external crystal.

Master Clock Hardware Configuration | Scenario Three

Diagram Description: This diagram shows a Kinetis MCU connected to an Audio Codec. An 8MHz clock source is connected to the Kinetis MCU. The Audio Codec acts as the I2S master, outputting MCLK to the Kinetis MCU. The Kinetis MCU outputs FS (Frame Sync), BCLK (Bit Clock), and TX (Transmit Data) signals to the Audio Codec.

Scenario three is similar to scenario two, except the Kinetis MCU is the I2S slave and the audio codec is the I2S master.

Master Clock Hardware Configuration | Scenario Four

Diagram Description: This diagram shows a Kinetis MCU connected to an Audio Codec. An 8MHz clock source is connected to the Kinetis MCU. The Kinetis MCU outputs an 8MHz_MCLK signal to the Audio Codec. The Kinetis MCU also outputs FS (Frame Sync), BCLK (Bit Clock), and TX (Transmit Data) signals to the Audio Codec. The Audio Codec is the I2S master.

Lastly, in scenario four displayed here, some audio codecs implement a phase-locked loop (PLL), tuned for audio applications and capable of generating more accurate I2S clock frequencies. If this feature is available on the audio codec, then the 8MHz clock from the Kinetis MCU can be used to provide a master clock to the audio codec. In this scenario, the Kinetis MCU remains as the I2S slave, and the audio codec is the master. The advantages for this scenario are a lower BOM cost and reduced jitter because the audio codec PLL is created for this type of use case.

4. Software Configuration

In this next section, we'll discuss software configuration.

SAI Kinetis SDK Initialization Example

The Kinetis SDK integrates an SAI peripheral driver that can be used to output I2S data.

First, a configuration structure must be filled with:

After that, an audio format structure is filled with the actual audio requirements. In the example displayed here, we use 16-bits per sample, with a 44.1 KHz sampling rate. The master clock generated by SAI will be 256 times the sampling frequency, and we want a stereo output.

Once the structures are filled, call the API to initialize the module, register a callback for receiving notifications, and finally, send data as required.

Once the application is done sending data, the SAI transmitter can be stopped, and if required, the peripheral driver can be de-initialized.

Example code snippet (conceptual):


// Example configuration structure
sai_user_config_t tx_config;
tx_config.protocol = kSaiBusI2SType;
tx_config.channel = 0;
tx_config.slave_master = kSaiMaster;
tx_config.sync_mode = kSaiModeAsync;
// ... other configuration parameters

// Example data format structure
sai_data_format_t format;
format.bits = 16;
format.sample_rate = 44100;
format.mclk = 256 * format.sample_rate;
format.mono_stereo = kSaiStereo;
// ... other format parameters

// Initialize SAI Tx
SAI_DRV_TxInit(SAI_INSTANCE, &tx_config, &tx_state);

// Configure data format
SAI_DRV_TxConfigDataFormat(SAI_INSTANCE, &format);

// Register callback (optional)
SAI_DRV_TxRegisterCallback(SAI_INSTANCE, callback, callback_param);

// Start sending data
SAI_DRV_SendDataInt(SAI_INSTANCE, addr, len);

// Stop Tx
SAI_DRV_TxStopModule(SAI_INSTANCE);

// De-initialize Tx
SAI_DRV_TxDeinit(SAI_INSTANCE);
        

SAI Operation In Low-Power Modes

For battery-powered applications, the SAI can be used in different low-power modes in slave or master mode using an external source for the master clock. For low leakage stop modes, the SAI module is static.

Low-Power Mode SAI Operation Comments
STOP Fully functional with external clock Use an externally generated bit clock or an externally generated audio master clock (including EXTAL).
VLPR Fully functional with external clock Frequency is limited in VLPR mode.
VLPW Fully functional with external clock Frequency is limited in VLPW mode.
VLPS Fully functional with external clock Use an externally generated bit clock or an externally generated audio master clock (including EXTAL).
LLS Static Registers will retain state.
VLLSx Off I2S state is not retained.

5. Example Use Case

Now, let's review an example use case.

USB Audio Streaming Example

Here, we have a USB audio streaming example, and let's say we want to develop a USB speaker using Kinetis MCUs.

Diagram Description: The diagram shows a laptop/PC connected via USB to a Kinetis MCU. The Kinetis MCU is connected to an Audio Codec via I2C and I2S interfaces. The Audio Codec is connected to headphones.

USB Audio Streaming Implementation

Diagram Description: This diagram illustrates the data flow for USB audio streaming. Audio data comes from USB into a USB Buffer. A DMA Channel 1 (Ch1) with a Software (SW) trigger moves this data to an Audio Buffer. From the Audio Buffer, a DMA Channel 0 (Ch0) with a TX FIFO Watermark trigger moves the data to the SAI TX FIFO. Finally, the SAI TX interface transmits the audio data.

In this example, the audio data is received from USB into a buffer. We can use a DMA channel to take this data and move it to a buffer dedicated for the audio samples.

A second DMA channel can be used to move the data from the audio buffer to the SAI TX FIFO. This DMA will be triggered by the SAI TX FIFO watermark signal.

With this use case, the CPU can handle other tasks such as audio synchronization. There is no need to send audio data to the audio codec, since everything is done in the background.

6. SAI Frequently Asked Questions (FAQs)

Finally, some frequently asked questions about the SAI module:

FAQ: My audio codec has WS, SCK and TX data; how can I connect it to a Kinetis device?

There are different names for the I2S lines, which might be confusing when trying to interface an audio codec with a Kinetis device. The table below shows a few examples of the different line names and how they relate to Kinetis devices.

Device\Line Name Master clock Bit Clock Frame sync Data transmission Data receive
Kinetis MCU MCLK TX_BCLK / RX_BCLK TX_FS / RX_FS TXDX RXDX
SGTL5000 MCLK SCLK LRCLK DOUT DIN
DA7321 MCLK BCLK WCLK DI DO
AK4954A MCKI BICK LRCK SDTO SDTI

FAQ: Is the SAI capable of multichannel audio?

Yes, the SAI module is capable of multichannel communication either by using time division multiplexing, which is a method that sends all channel data on the same data line, or by assigning different transmit or receive lines to different channels. Time division multiplexing may also be referred to as "network mode."

References

This concludes our presentation on the SAI Module for Kinetis K series MCUs.

More information about the SAI module can be found in the application notes listed here:

We also invite you to visit us on the web at Freescale.com/Kinetis and check out our Kinetis community page.

PDF preview unavailable. Download the PDF instead.

Serial-Audio-Interface-Training Acrobat Distiller 10.1.9 (Windows)

Related Documents

Preview MCUXpresso IDE User Guide - NXP Semiconductors
A comprehensive user guide for the MCUXpresso Integrated Development Environment (IDE) from NXP Semiconductors. This guide covers features, setup, debugging, and project creation for NXP's ARM-based microcontrollers.
Preview NXP GUI Guider User Guide: Design Embedded GUIs
Comprehensive user guide for NXP's GUI Guider, an IDE for designing embedded graphical user interfaces with drag-and-drop functionality. Learn about installation, usage, features, and supported hardware for creating visually rich applications on NXP MCU devices.
Preview FLAC Porting and Codec Performance Evaluation on NXP i.MX RT685
This application note details the porting of the FLAC codec library to NXP's i.MX RT685 platform. It compares the performance of the Cortex-M33 and HiFi4 DSP cores, utilizes GNU profiler for analysis, and provides insights into optimization strategies for embedded audio applications.
Preview MCF547x Reference Manual - NXP Semiconductors
Detailed reference manual for the NXP MCF547x microcontroller family, covering architecture, features, peripherals, and programming. Essential for embedded system design.
Preview NXP FRDM-MCXA156 Development Board: Quick Start Guide for MCUXpresso
Get started quickly with the NXP FRDM-MCXA156 development board. This guide covers setup, software, expansion boards, and support for the MCUXpresso Developer Experience.
Preview AN14184: Using SmartDMA for Keyscan on MCX N Series MCU
This application note explains how to use SmartDMA for keyscan on MCX N Series MCUs, detailing the solution, its features, API examples, and a demonstration. It covers hardware connections, software implementation, and setup for the FRDM-MCXN947 and FRDM-MCXN236 development boards.
Preview AN14179: Migration Guide from MCXNx4x to MCXN23x Microcontrollers
This application note from NXP Semiconductors provides a comprehensive guide for migrating applications from the MCXNx4x microcontroller platform to the MCXN23x platform. It details differences in system resources, peripherals, clock systems, pinouts, and software considerations to facilitate a smooth transition.
Preview NXP AN14155: Using i.MX RT1180 EtherCAT with BECKOFF TwinCAT3 and SSC Tool
Application note detailing the integration and usage of the NXP i.MX RT1180 EtherCAT peripheral with BECKOFF TwinCAT3 software and the Slave Stack Code (SSC) tool, using the MIMXRT1180-EVK board and SDK.