HDL-MIC · Documentation

Hardware

Components and wiring of the measurement unit: four INMP441 digital microphones and a development board with a Cyclone IV EP4CE6E22C8 FPGA and a CH340C USB-UART converter. The section gives the pinout, the structure of the FPGA design, its operation, the command set and the format of the transferred data.

Components

DesignatorComponentFunction
U5Altera (Intel) Cyclone IV E EP4CE6E22C8, EQFP-144 packageI2S clock generation, sample reception, buffering, UART transmission, command reception
U3EPCS4SI8N, 4 MbitConfiguration memory: FPGA configuration at power-up (Active Serial, MSEL = 010)
X250 MHz crystal oscillatorFPGA clock (pin 24, CLK2)
U21WCH CH340CUSB 2.0 Full Speed — UART converter; 3.3 V supply (VCC and V3 connected to the 3V3 net)
U22USB Type-C connectorConnection to the computer and board power (5 V)
U6AMS1117-3.3Supply of the FPGA I/O banks, CH340C and microphones (3.3 V)
—4 × INMP441 moduleOmnidirectional MEMS microphones with digital I2S output, 24 bits
—2 × 100 kΩ resistorPull-down of the SD0, SD1 lines to GND (external, at the FPGA inputs)
—Wire CH340C:14 → FPGA:33RTS# line for flow control (soldered additionally, see below)

The measurement unit is powered from the USB bus. The FPGA is an SRAM-based device: its configuration is lost at power-off and is loaded from U3 or via JTAG (.sof file).

Block diagram

4 × INMP441 I2S, 24 bits 50 MHz (X2) Reset button EPCS4 (U3) LED FPGA EP4CE6E22C8 (top.v) i2s_inmp441_rx SCK, WS, SD input 4 × 24 bits sample_packer word length, channel, shift SCFIFO 96 bits × 2048 M9K, show-ahead test generator TEST_MODE = 1 daq_fifo_ctrl header, bytes, ACK, overflow uart_tx_ctrl 8N1/8N2, RTS# pause uart_rx_ctrl 5-byte frame, BREAK reg_file registers 0x00…0x08 SCK, WS SD0, SD1 clk_50m rst_n configuration led_n configuration (dashed) CH340C (U21) 3 RXD 14 RTS# 2 TXD Computer fpgad, /dev/ttyUSB* USB 2.0 FS PIN_10 TX PIN_33 RTS# PIN_23 RX

Fig. 1. Block diagram. The data path (microphones → UART) is shown in blue, the command path and the RTS# signal in orange; dashed lines show the distribution of configuration register values to the modules.

The FPGA design is written in Verilog-2001 and consists of six modules instantiated in top.v:

ModuleFunction
i2s_inmp441_rxI2S master: generates SCK and WS, receives two SD lines (two microphones per line), outputs a frame of four 24-bit samples
sample_packerSaturating shift (SHIFT), channel selection (CH_SEL), word-length truncation (BIT_MODE), packing into a 96-bit word
daq_fifo_ctrlSCFIFO buffer 96 × 2048, frame (window) state machine, header generation, byte-wise word output, ACK/NAK insertion, overflow handling
uart_tx_ctrlUART transmitter 8N1/8N2 with a programmable divider; RTS# check before the start bit
uart_rx_ctrlUART receiver 8N1, parsing of the 5-byte command frame, XOR check, BREAK detector
reg_fileConfiguration registers with value clamping, ACK/NAK generation, deferred baud rate change

The test generator (in top.v) with TEST_MODE = 1 substitutes deterministic samples for the microphone signals: ch0 = n, ch1 = −n, ch2 = 0x123456, ch3 = {n[7:0], n[7:0], n[7:0]}, where n is the I2S frame number. It is used to verify the integrity of the data path.

Microphone connection

The four microphones form two I2S stereo pairs. The SCK and WS lines are common to all microphones; each pair uses a separate SD data line. The slot (left/right) of a microphone is set by the level on the L/R pin: GND — left channel (data while WS = 0), VDD — right (WS = 1). In the inactive slot the SD output of the microphone is in the high-impedance state, so the SD line is pulled down to GND by a 100 kΩ resistor; the FPGA internal weak pull-up on these pins is disabled.

FPGA U5 PIN_28 i2s_sck PIN_30 i2s_ws PIN_31 i2s_sd[0] PIN_32 i2s_sd[1] 3V3 GND SCKWS SD0SD1 3.3 VGND 100 kΩ Microphone 1 · ch0 SD0 · left SCK WS SD L/R VDD GND Microphone 2 · ch1 SD0 · right SCK WS SD L/R VDD GND Microphone 3 · ch2 SD1 · left SCK WS SD L/R VDD GND Microphone 4 · ch3 SD1 · right SCK WS SD L/R VDD GND

Fig. 2. Microphone wiring diagram. A dot marks a connection to a bus. On the left microphones (1, 3) the L/R pin is connected to GND, on the right ones (2, 4) to 3.3 V.

Channel numbering: ch0 = SD0, left slot; ch1 = SD0, right; ch2 = SD1, left; ch3 = SD1, right. Microphone numbers 1…4 in the interface correspond to channels ch0…ch3. Changing the wiring of the SD lines (swapping the PIN_31 and PIN_32 wires) changes the correspondence between channels and microphones.

Pinout

INMP441 module

Module pinFunctionMicrophone 1Microphone 2Microphone 3Microphone 4
VDDSupply 1.8…3.3 V3.3 V of the FPGA board (3V3 net)
GNDGroundGND
SCKBit clock, inputPIN_28
WSWord select (frame), inputPIN_30
SDSerial data, tri-state outputPIN_31PIN_31PIN_32PIN_32
L/RSlot select: GND — left, VDD — rightGNDVDDGNDVDD

FPGA EP4CE6E22C8

Pin numbers refer to the EQFP-144 package; the I/O standard of all pins is 3.3-V LVCMOS. Unused pins are configured as inputs with internal pull-up.

PinBankSignal (top.v)DirectionConnectionNote
242clk_50minputX2, 50 MHzClock input CLK2
885rst_ninputbuttonActive low; asynchronous assertion, synchronous release of reset
11led_noutputLEDActive low
282i2s_sckoutputSCK of all microphones0.5…3.125 MHz
302i2s_wsoutputWS of all microphonesfs = SCK/64
312i2s_sd[0]inputSD of microphones 1, 2External 100 kΩ resistor to GND
322i2s_sd[1]inputSD of microphones 3, 4External 100 kΩ resistor to GND
101uart_txoutputCH340C pin 3 (RXD)Board net UART_TX
231uart_rxinputCH340C pin 2 (TXD)Board net UART_RX; clock input CLK1, internal pull-up not supported
332uart_rts_ninputCH340C pin 14 (RTS#)Soldered wire; internal pull-up to 3.3 V

USB-UART connection

FPGA U5 PIN_10 uart_tx PIN_23 uart_rx PIN_33 uart_rts_n CH340C (U21) 3 RXD 2 TXD 14 RTS# 9 CTS# 5 D+ 6 D− VCC, V3: 3.3 V data → PC commands ← PC soldered wire (pin 14 unconnected on the board) not connected (CH340C input) USB Type-C PC

Fig. 3. Connection of the FPGA to the CH340C converter. The TX/RX nets are routed on the board; the RTS# line (highlighted) was added with a wire.

On the computer side the CH340C is detected as the serial port /dev/ttyUSB* (VID:PID 1a86:7523, kernel driver ch341). The CH340C logic levels are 3.3 V (the V3 and VCC pins are connected to the 3V3 net), which is compatible with the 3.3-V LVCMOS standard of the FPGA pins without level shifting.

RTS# line (CH340C pin 14)

The specification requires hardware flow control: the FPGA transmitter pauses byte output when the computer is not ready to receive data (signal uart_cts_n, active low). On the development board only the TXD and RXD lines are routed to the FPGA; the CH340C modem pins (9–15) are not connected.

CH340C pin 9 (CTS#) is an input of the chip: through it an external device reports its readiness to the computer, so it cannot provide a signal to the FPGA. The computer readiness signal is driven by the output RTS# (Request To Send, pin 14), whose level is set by software on the computer through the port modem control lines (ioctl TIOCMSET; when a port is opened in Linux, RTS# is set to the active level 0). For this reason a wire is soldered to CH340C pin 14 and connected to FPGA pin 33 (bank 2, input uart_rts_n, internal pull-up to 3.3 V).

RTS# levelComputer stateFPGA transmitter action (with CTS_EN = 1)
0ready to receivetransmission enabled
1not ready (or the wire is absent — pull-up)transmission pauses before the next start bit; a byte already started is transmitted completely

The signal passes through a two-flip-flop synchronizer and is checked only before the start bit, so the pause does not break the byte structure. During the pause samples keep entering the FIFO; when it becomes full, the overflow handling procedure is executed (see FPGA operation).

Purpose of the line in the system:

Limitation. The ch341 driver of Linux kernel 6.8 does not implement automatic hardware flow control (RTS/CTS): the RTS# level does not change when the computer receive buffer fills and is set only by software. In normal operation fpgad writes UART_CFG = 0x0001 (8N2, CTS_EN = 0), the RTS# signal is ignored, and data integrity is ensured by checking the window length on the receiving side.

I2S interface

The FPGA is the bus master: it generates SCK and WS from the 50 MHz clock by integer division. One WS frame contains 64 SCK periods (32 per slot). The INMP441 outputs a 24-bit two's complement sample MSB first; the MSB follows one SCK period after the WS transition (I2S Philips format), and after 24 bits the SD output goes to the high-impedance state.

SCK = 50 MHz / (2H),   fs = SCK / 64 = 50 MHz / (128·H),   H = round(FS_DIV / 128) ∈ [8; 50]
H = 8 → fs = 48,828 Hz (SCK 3.125 MHz);   H = 24 → 16,276 Hz;   H = 50 → 7,813 Hz (SCK 0.5 MHz)
SCKWSSD………………WS = 0: left slotWS = 1: right slotb23b22b21 … b16b1 b0Hi-Z (7 SCK)b23b22 …

Fig. 4. I2S timing diagram (fragments of a frame). WS and SD change on the falling edge of SCK; the MSB (b23) is output one SCK period after the WS transition; after b0 the SD line is in the high-impedance state until the end of the slot.

Receiver implementation details:

FPGA operation

IDLEwaiting HEADER8-byte header ACTIVEcapture and transmit WAIT_USBRTS# = 0 ≥ 100 µs RUN orSINGLE_SHOT headersent FIFO full window sent (RUN, CONT_MODE = 0) aclr FIFO, drop_cnt + 1 → new header SINGLE_SHOT window sent; RUN = 0

Fig. 5. State machine of the daq_fifo_ctrl module. From any state the transition to IDLE occurs when RUN = 0 (outside SINGLE_SHOT mode) and on SOFT_RST.

  1. IDLE. The module waits for a start command (RUN = 1 or a SINGLE_SHOT pulse). Start occurs after the ACK has been transmitted, so the ACK always precedes the data. At start BIT_MODE, CH_SEL, SHIFT, WIN_SIZE are latched; changes of these registers during transmission take effect from the next window.
  2. HEADER. 8 header bytes are transmitted; after the last byte frame_id is incremented by 1 and the word counters are cleared.
  3. ACTIVE. Each I2S frame (samples of four channels) is packed into a 96-bit word and written into the SCFIFO. In windowed mode exactly WIN_SIZE words are written, in continuous mode (CONT_MODE = 1) writing is not limited. Transmission starts as soon as the first word appears in the FIFO, in parallel with writing the following ones. The end of a window is determined by the number of words read from the FIFO for transmission (rd_cnt = WIN_SIZE).
  4. End of window. SINGLE_SHOT — return to IDLE. RUN with CONT_MODE = 0 — transition to HEADER and capture of the next window.
  5. Overflow. An attempt to write into a full FIFO means that part of the window samples is lost. An asynchronous FIFO clear (aclr) is performed, drop_cnt is incremented by 1, and the module goes to WAIT_USB. Transmission resumes, from a new header, once the transmitter is idle and the RTS# line has continuously permitted transmission for at least 100 µs (with CTS_EN = 0 — after 100 µs). The receiving side detects the overflow from the change of drop_cnt.

The FIFO capacity of 2048 words limits the window size at which overflow is excluded regardless of the UART rate: WIN_SIZE ≤ 2048. The register accepts values up to 4096; windows larger than 2048 are transferred without loss only if the UART rate exceeds the incoming data rate.

Command control

Commands are sent by the computer over the FPGA RX line (UART 8N1, baud rate according to the current BAUD_DIV value) in fixed-length 5-byte frames:

Byte01234
Contents0x5AADDRDATA[7:0]DATA[15:8]XOR of bytes 0…3
Example: write FS_DIV = 3072 (0x0C00) → 5A 01 00 0C 57    (0x5A ⊕ 0x01 ⊕ 0x00 ⊕ 0x0C = 0x57)

The receiver synchronizes on the byte 0x5A; a gap of more than 10 ms between frame bytes resets parsing. The FPGA responds to each received command with one byte: ACK = 0x06 — command executed; NAK = 0x15 — invalid checksum or unknown address. The response is inserted into the output stream only at a word boundary (never inside a header or a sample). Writing registers during continuous transmission breaks the alignment of the received data, so fpgad changes the configuration only while transmission is stopped.

AddressRegisterValuesDefaultDescription
0x00CTRLbits 0…30bit0 RUN — continuous window capture; bit1 CONT_MODE — continuous stream without division into windows; bit2 SINGLE_SHOT — one window (pulse); bit3 SOFT_RST — reset of the FIFO and counters (pulse)
0x01FS_DIV1024…64003072Sampling rate divider: fs = 50 MHz / (128·round(FS_DIV/128)), 7.81…48.83 kHz
0x02BAUD_DIV25…5208434UART baud rate divider: rate = 50 MHz / BAUD_DIV (434 → 115,200 baud, 50 → 1 Mbaud)
0x03BIT_MODE0…40Word length: 24/20/16/12/8 bits (12/10/8/6/4 bytes per four-channel frame)
0x04WIN_SIZE64…40961024Number of samples in a window
0x05UART_CFGbits 0…11bit0 STOP_BITS: 1 — 8N2, 0 — 8N1 (transmitter only); bit1 CTS_EN — honour RTS#
0x06TEST_MODE0, 101 — test generator instead of the microphones
0x07CH_SEL0…440…3 — transmit one channel (24 bits → 3 bytes, 16 → 2, 8 → 1); 4 — all channels
0x08SHIFT0…80Saturating left shift of samples before word-length truncation (6 dB gain per bit)

Out-of-range values are limited in hardware (clamping). Registers 0x06…0x08 are extensions with respect to the specification.

Baud rate change. After BAUD_DIV is written, the ACK is transmitted at the old rate; the new value is applied to the receiver and transmitter after the stop bits of the ACK have been sent.

Link recovery. Holding the RX line at 0 for more than 20 ms (BREAK condition) clears RUN and sets the rate to 115,200 baud regardless of the current one. This allows a link to be established with the FPGA in an unknown state (for example, after the program on the computer is restarted).

Link establishment sequence (fpgad):

  1. port at 115,200 baud; BREAK 100 ms; CTRL = 0 — check for an ACK response;
  2. BAUD_DIV = 50 — ACK at 115,200 baud; switch of the computer port to 1 Mbaud;
  3. UART_CFG = 0x0001 (8N2, CTS_EN = 0) — link check at the new rate.

Capture of one window: FS_DIV, BIT_MODE = 0, WIN_SIZE, CH_SEL = 4, SHIFT = 0 (written when changed), then CTRL = 0x0004 (SINGLE_SHOT); the response is an ACK, a header and 12·WIN_SIZE data bytes. Continuous stream: CTRL = 0x0003 (RUN | CONT_MODE), stop — CTRL = 0.

For debugging without the server, mic_recorder/pc/daq_debug.py (Python, pyserial) is used: commands set, run, stop, shot, baud, dump, break, rts, level. The port is opened exclusively: it cannot be used simultaneously with fpgad.

Data transfer

Data are transmitted over the FPGA TX line as a byte stream in UART 8N2 format (normal operation). Each window (in continuous mode — the stream after start or after an overflow) begins with an 8-byte header:

Bytes0…34…56…78…
ContentsAA 55 AA 55frame_id, LEdrop_cnt, LEdata words

frame_id is the sequence number of the window (from 0 after SOFT_RST); drop_cnt is the number of FIFO overflows. A data word contains the channel samples in the order ch0, ch1, ch2, ch3, each least significant byte first (little-endian); in the 20- and 12-bit modes the samples are packed without byte alignment. With BIT_MODE > 0 the least significant bits are discarded.

24 bits, 4 channels (12 bytes per frame):
ch0[7:0] ch0[15:8] ch0[23:16] ch1[7:0] ch1[15:8] ch1[23:16] ch2[7:0] … ch3[23:16]

Start of the response to SINGLE_SHOT:
06 | AA 55 AA 55 | 05 00 | 00 00 | 84 F3 FF E0 FB FF …

A sample is a 24-bit two's complement number; full scale (0 dBFS) corresponds to 223. With 8N2 the transfer rate is baud/11 bytes/s: 90,909 bytes/s at 1 Mbaud. A stream of four 24-bit channels requires 12·fs bytes/s (from 93,750 bytes/s at 7.81 kHz), which exceeds the channel capacity, so measurements are performed in windowed mode: a window is acquired without gaps, and the signal is not acquired between windows (for details see “Capture: sampling and windows”). Continuous mode is used for one channel (CH_SEL) or for four 16-bit channels at fs ≤ 10.8 kHz.

Window integrity is checked on the receiving side by the header signature, the window number and the length: an incompletely received window is discarded. Losses of individual bytes were observed only on the USB link between the CH340C and the computer (about one window in 2200 at 1.5 Mbaud; no losses recorded at 1 Mbaud), so the operating rate is 1 Mbaud. At 2 Mbaud the losses are systematic.

Indication

LED (PIN_1)State
blinks at ≈ 12 Hzmicrophone start-up (218 SCK periods)
blinks with a period of ≈ 1.3 swaiting for a command
ontransmission of a header or data
off for ≈ 0.34 sFIFO overflow