# I2S Transceiver (VHDL)

**URL:** <https://forum.digikey.com/t/i2s-transceiver-vhdl/12845>\
**Category:** Logic Design\
**Tags:** tech-tips\
**Created:** [March 19, 2021, 12:38am UTC](https://forum.digikey.com/t/i2s-transceiver-vhdl/12845 "2021-03-19T00:38:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scott\_1767](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@Scott\_1767](https://forum.digikey.com/u/Scott_1767)\
**Post date:** [March 19, 2021, 12:38am UTC](https://forum.digikey.com/t/i2s-transceiver-vhdl/12845/1 "2021-03-19T00:38:42Z")

</div>

[Logic Home](https://forum.digikey.com/t/logic-home-table-of-contents/12507)

 

# Code Download

Version 1.0: [i2s\_transceiver.vhd](https://forum.digikey.com/uploads/short-url/cBLXjfBwdIZWMahNHgLxr4Erndo.vhd) (7.5 KB)  
Initial Public Release

# Features

- VHDL source code of an Inter-IC Sound (I2S) transceiver component
- Meets the I2S Bus Specification
- Configurable sample rate
- Configurable serial bit clock rate
- Configurable data width
- Master only
- Complete example design available (see Additional Information section)

# Introduction

This details a master I2S transceiver component for use in FPGAs, written in VHDL. The component receives audio data over an I2S interface and presents this received data to user logic on a parallel interface at the sample rate. It also inputs parallel data from user logic at the sample rate, and transmits this data out over an I2S interface. Figure 1 illustrates a typical example of the I2S transceiver integrated into a system.

 ![block_diagram](https://global.discourse-cdn.com/digikey/original/2X/6/630b70728ff732b2367f4a727842a7507a2ab85c.jpeg)

**Figure 1.** Example Implementation

# Background

I2S (also I2S or IIS) is a standardized serial communication bus for digital audio. The bus consists of 3 lines: serial clock (SCK), word select (WS), and serial data (SD). Figure 2 shows the data framing for an I2S communication link.

 ![data_framing](https://global.discourse-cdn.com/digikey/original/2X/8/8a55324a3ced176d8d1caa32548179566a55c2b2.jpeg)

**Figure 2.** I2S Data Framing

The serial data line transmits two time-multiplexed data channels - the left and the right channel. Data is transmitted most significant bit (MSB) first in a 2’s compliment format. The word select line indicates which channel is presently being transmitted (low = left channel, high = right channel). That channel data word’s MSB gets transmitted one clock cycle after the word select line toggles, followed by the rest of the data word. Data bits are transmitted on falling clock edges and read on rising clock edges.

Either the I2S transmitter or the I2S receiver can act as the master. The master provides the serial clock and word select signals.

Audio codecs also typically require a “master clock” (MCLK) to run their internal circuitry. The MCLK frequency is commonly a multiple of the sample rate, such as 256\*Fs (where Fs is the sample rate). Requirements depend on the specific audio codec.

# Theory of Operation

This I2S Transceiver inputs a master clock and derives the serial clock and word select signals from it using counters. The serial clock frequency is an integer multiple of the master clock, and the word select frequency (i.e. the sample rate) is an integer multiple of the serial clock.

The receive data is clocked into internal receive shift registers from the _sd\_rx_ port on serial clock rising edges. Shifting is enabled for the appropriate channel one serial clock cycle after the word select toggle and continues for the width of the data word, before being again disabled. The transceiver presents receive data for both right and left channels on the _r\_data\_rx_ and _l\_data\_rx_ ports, respectively, on each word select toggle.

Conversely, the right and left data presented on the _r\_data\_tx_ and _l\_data\_tx_ input ports is parallel loaded into internal transmit shift registers on each word select toggle. This data is clocked out the _sd\_tx_ port on serial clock falling edges. Shifting is enabled for the appropriate channel one serial clock cycle after the word select toggle and continues for one serial clock beyond the width of the data word, before being again disabled. The extra transmission bit sets the _sd\_tx_ data line to ‘0’ in case there is a data width mismatch with the receiving I2S device, as per the I2S Bus Specification.

# Configuring the I2S Transceiver

The I2S Transceiver is configured by setting the generic parameters in the ENTITY.

## _sclk\_ws\_ratio_

The word select (_ws_) signal’s frequency is the sample rate. The _sclk\_ws\_ratio_ parameter defines the number of serial clock (_sclk_) cycles per word select period.

![equation1](https://global.discourse-cdn.com/digikey/original/2X/a/a5f7f087fec2f77d07e35cda69094c0b4491f892.jpeg)

## _mclk\_sclk\_ratio_

The _mclk\_sclk\_ratio_ parameter defines the number of master clock (_mclk_) cycles per serial clock (_sclk_) cycle.

![equation2](https://global.discourse-cdn.com/digikey/original/2X/1/1114efd799467c82aee6d83d4457be029e13b13a.jpeg)

## _d\_width_

The parameter _d\_width_ defines the size of each data word in bits.

This does not need to equal the number of serial clock cycles during a word select half period (i.e. the number of serial clocks during a channel data transmission). If there are excess clock cycles during the word select half period, then any data on the serial data receive port (_sd\_rx_) during the extra serial clock cycles is ignored, and the serial data transmit port (_sd\_tx_) outputs ‘0’ for these extra bits.

# Providing the Master Clock

The I2S Transceiver requires a master clock _mclk_ input to operate. This clock is intended to be the same master clock signal sent to the I2S slave device that the I2S Transceiver is communicating with.

Typically, the desired sample rate (Fs) is known, and the I2S device requires a master clock of some multiple of this sample rate. For instance, suppose the desired sample rate is the commonly used 44.1 kHz, and the I2S device accepts a master clock of 256\*Fs. This means the user needs to provide an 11.29 MHz _mclk_ to both the I2S device and the I2S Transceiver.

The _mclk_ signal can be derived inside the FPGA using a PLL.

# Port Descriptions

Table 1 describes the I2S Transceiver’s ports.

**Table 1.** Port Descriptions

 ![port_descriptions](https://global.discourse-cdn.com/digikey/original/2X/0/0395e11169b6308dc0d39055be1d9b4622e7ba05.jpeg)

# Streaming Audio Data Transactions

The I2S Transceiver makes received data available to the user logic on the _l\_data\_rx_ and _r\_data\_rx_ ports. Both of these receive data ports update on each _ws_ transition. Since new left channel data is only received while _ws_ = ‘0’, the _l\_data\_rx_ port gets new values concurrent with the rising edge of _ws_, i.e. when the left channel data word finishes. Likewise, new right channel data is only received while _ws_ = ‘1’, so the _r\_data\_rx_ port gets new values concurrent with the falling edge of _ws_, i.e. when the right channel data word finishes. The user logic can use the _ws_ signal to indicate that new data is available and thereby retrieve this data at the sample rate. Figure 3 illustrates this behavior.

Similarly, the I2S Transceiver latches in new transmit data on the _l\_data\_tx_ and _r\_data\_tx_ ports on each _ws_ transition. The user logic can use the _ws_ signal to indicate when its outgoing data is latched in, and it is therefore free to present the next set of transmit data on these ports. The _l\_data\_tx_ values latched in on the falling edge of _ws_ are transmitted on _sd\_tx_ during that left channel data word transmission. Likewise, the _r\_data\_tx_ values latched in on the rising edge of _ws_ are transmitted on _sd\_tx_ during that right channel data word transmission. Figure 3 shows this behavior.

 ![timing_diagram](https://global.discourse-cdn.com/digikey/original/2X/f/f7894b31b87a39ea8e2591572b408d163a83f7df.jpeg)

**Figure 3.** Timing Diagram

# Reset

The _reset\_n_ input port must have a logic high for the I2S Transceiver to operate. A low signal on this port asynchronously resets the component. During reset, the component holds the _sclk_, _ws_, and _sd\_tx_ ports low. Any transmit currently underway is discontinued. Any receive currently in progress is abandoned, and the _l\_data\_rx_ and _r\_data\_rx_ output ports clear. All internal data buffers are cleared. Once released from reset, the I2S Transceiver resumes operation.

# Conclusion

This I2S Transceiver is a configurable programmable logic component that transmits and receives audio data over a standard I2S bus. It operates as a master, providing the serial clock and word select signals for the interface. It allows the user to configure it for a particular sampling frequency, data width, master clock, and number of serial clock cycles per word.

# Additional Information

[I2S Bus Specification, Philips Semiconductors](https://forum.digikey.com/uploads/short-url/tqfx2h2zxJzDX4Mz46aUvtaoe12.pdf) (59.8 KB)

[I2S Pmod Quick Start (VHDL)](https://www.digikey.com/eewiki/pages/viewpage.action?pageId=85295576) - This design uses the I2S Transceiver component described on this page to interface an FPGA to a Cirrus Logic CS5343 Multi-Bit Audio ADC and also a Cirrus Logic CS4344 Stereo DAC.
