Try Embedded

I2S audio and Ethernet

Streaming audio between chips, and wired Ethernet from MAC to PHY.

Two streaming interfaces

This lesson covers two interfaces that move continuous data rather than occasional commands: I2S for digital audio between chips, and Ethernet for wired networking.

I2S: digital audio

I2S (Inter-IC Sound) connects microcontrollers to audio codecs, DACs, ADCs, digital microphones and amplifiers. Despite the name it has nothing to do with I2C.

SignalAlso calledPurpose
SCKBCLK, bit clockOne pulse per data bit
WSLRCLK, frame syncSelects the left (low) or right (high) channel; its frequency is the sample rate
SDSDATA, DIN/DOUTSerial data, MSB first
MCLKMaster clockOften required by codecs: typically 256 × the sample rate
bit clock = sample rate × bits per channel × channels
48 kHz × 32 bits × 2 channels = 3.072 MHz

In standard (Philips) I2S, the data's MSB starts one bit clock after WS changes. Variants shift that: left-justified (MSB aligned with the WS edge), right-justified, and TDM (many channels in one frame, for multi-microphone arrays and multi-channel codecs). Both ends must agree on format, word length and which side generates the clocks.

Getting the clocks right

The sample rate must be exact or audio drifts and clicks. Common rates (44.1 kHz family and 48 kHz family) need specific master clocks (11.2896 MHz, 12.288 MHz); generating them from a 16 MHz crystal requires an audio PLL with fractional dividers, and most MCU I2S peripherals have one.

#include <stdio.h>

int main(void) {
  int rates[] = { 8000, 16000, 44100, 48000 };
  for (int i = 0; i < 4; i++) {
    double bclk = rates[i] * 32.0 * 2;           /* 32-bit slots, stereo */
    double mclk = rates[i] * 256.0;
    printf("%5d Hz: bit clock %.4f MHz, MCLK %.4f MHz\n", rates[i], bclk / 1e6, mclk / 1e6);
  }
  /* Buffer timing: how often must the DMA interrupt be serviced? */
  int frames = 256;
  printf("a %d-frame half buffer at 48 kHz lasts %.2f ms\n", frames, frames * 1000.0 / 48000);
  return 0;
}

Streaming without glitches

Audio is a hard real-time stream: a late buffer is an audible click. The standard design is the circular DMA with half and full transfer interrupts from the DMA lesson: DMA plays one half of the buffer while the CPU fills the other. Larger buffers tolerate more scheduling jitter but add latency. Digital MEMS microphones often use PDM (a 1-bit high-rate stream) instead of I2S; many MCUs have a PDM-to-PCM filter peripheral.

Ethernet

Wired Ethernet gives devices fast, reliable, low-latency networking: industrial controllers, gateways, cameras, audio-over-IP, test equipment.

The pieces on the board

MCU [ MAC + DMA ] --RMII/MII--> [ PHY ] --> magnetics --> RJ45
                 --MDIO/MDC---> (PHY configuration and status)
PartJob
MAC (inside the MCU)Frames: addresses, CRC, DMA to and from RAM, filtering
PHY (external chip, e.g. LAN8742, DP83848)Turns bits into the line signalling of 10BASE-T / 100BASE-TX, auto-negotiation, link detection
MII / RMIIMAC-to-PHY data interface. RMII uses fewer pins with a 50 MHz reference clock
MDIO / MDCTwo-wire management bus to read PHY registers (link up, speed, duplex)
MagneticsIsolation transformers, often built into the RJ45 jack

Single Pair Ethernet (10BASE-T1L for up to 1 km in industry, 100BASE-T1 and 1000BASE-T1 in cars) brings Ethernet to places that used fieldbuses and CAN.

Frames

| preamble (7) | SFD (1) | destination MAC (6) | source MAC (6) | EtherType (2) | payload (46-1500) | FCS CRC-32 (4) |

EtherType says what the payload is: 0x0800 IPv4, 0x86DD IPv6, 0x0806 ARP, 0x8100 a VLAN tag. The MAC usually computes and checks the CRC in hardware. Every device needs a unique MAC address: buy a block (an OUI from the IEEE) or use a chip with one programmed (for example an EEPROM with an EUI-48 inside).

The driver

The MAC's DMA works with descriptor rings: circular lists of descriptors in RAM, each pointing to a buffer and owned either by the DMA or by software (an OWN bit). For reception, the driver gives the DMA a ring of empty buffers; the DMA fills them and flips ownership; the driver hands filled buffers to the TCP/IP stack (lwIP, Zephyr, FreeRTOS+TCP) and gives fresh ones back. On cores with a data cache, descriptors and buffers need cache maintenance or non-cacheable memory.

Real-time Ethernet

Standard Ethernet has no timing guarantees under load. Industrial and automotive variants add them: PROFINET, EtherCAT (frames processed on the fly by each node), EtherNet/IP, and IEEE TSN (Time-Sensitive Networking) with PTP (IEEE 1588) clock synchronisation to sub-microsecond accuracy; many MCU MACs timestamp PTP packets in hardware.

Pitfall: "Link up but no traffic" is very often the RMII reference clock: either neither the MCU nor the PHY supplies the 50 MHz clock, or both do, or the PHY's strap pins select a different mode or MDIO address than the driver assumes. Read the PHY's ID registers over MDIO first to prove communication.

Interview question: How would you play continuous audio from an MCU through an I2S DAC without glitches?

Answer: Configure the I2S peripheral and its audio PLL for the exact sample rate and the format the DAC expects, with the DAC's MCLK if required. Use circular DMA from a buffer split into two halves; in the half-transfer and transfer-complete interrupts, signal a high-priority task (or do it directly if cheap) to decode or generate the next block into the half just played. Size the buffer for the worst-case delay before the producer runs, give the audio path a high priority, and watch for underrun flags so a late buffer is detected rather than heard.

Interview question: Describe how an Ethernet MAC driver receives frames.

Answer: The driver allocates a ring of receive descriptors, each pointing to a buffer, and marks them owned by the DMA. When a frame arrives the MAC's DMA writes it into the next buffer, sets the length and status in the descriptor, clears the ownership bit and raises an interrupt. The driver (in a task, signalled by the interrupt) walks the ring from where it stopped, passes each completed buffer to the TCP/IP stack, attaches a new empty buffer, and returns the descriptor to the DMA. Cache maintenance or non-cacheable memory is needed on cores with data caches, and errors such as CRC failures or ring overruns are counted.

Going deeper

Interview question: How would you design a conference speakerphone that streams microphone audio over Ethernet with no gaps?

Answer: Capture from the microphones over I2S by DMA into a double buffer, process in fixed-size blocks (echo cancellation, beamforming), and send RTP packets at a steady rate. On the receive side, a jitter buffer absorbs network delay variation, and the audio clock is synchronised to the network (PTP) or adjusted against buffer fill so neither end drifts.

Interview question: What is the difference between I2S and PDM microphones?

Answer: An I2S microphone contains its own decimation filter and outputs PCM samples (for example 24-bit at 48 kHz) on the I2S frame format. A PDM microphone outputs a 1-bit pulse-density stream at a few MHz, cheaper and simpler, which the receiver must low-pass filter and decimate into PCM, either in a hardware PDM interface or in software. PDM pairs easily (two microphones share a clock and data line, sampled on opposite edges).

Interview question: Why does Ethernet need precision time protocols for industrial control?

Answer: Ordinary Ethernet and switches introduce variable queuing delays, so a controller cannot know exactly when a packet was sent or received, and devices' clocks drift apart. PTP (IEEE 1588) synchronises clocks across the network to sub-microsecond accuracy using hardware timestamps on sync messages and measured path delays, and TSN adds scheduled, reserved traffic. Together they let distributed devices sample inputs and drive outputs at precisely coordinated times, as motion control requires.

Explain it: In your own words, explain the roles of the MAC, the PHY and the TCP/IP stack in an Ethernet-connected microcontroller.

Answer: The TCP/IP stack is software that turns application data into IP packets and handles addressing, reliability and protocols such as TCP, DHCP and ARP. The MAC, a peripheral inside the microcontroller, takes complete Ethernet frames from memory using DMA, adds or checks the frame CRC and addresses, and passes frames to and from the PHY over a digital interface such as RMII. The PHY, a separate chip, converts that digital stream into the analog signalling on the cable, negotiates speed and duplex with the other end, and reports whether the link is up, which the driver reads over the MDIO management bus.