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
| Designator | Component | Function |
|---|---|---|
| U5 | Altera (Intel) Cyclone IV E EP4CE6E22C8, EQFP-144 package | I2S clock generation, sample reception, buffering, UART transmission, command reception |
| U3 | EPCS4SI8N, 4 Mbit | Configuration memory: FPGA configuration at power-up (Active Serial, MSEL = 010) |
| X2 | 50 MHz crystal oscillator | FPGA clock (pin 24, CLK2) |
| U21 | WCH CH340C | USB 2.0 Full Speed — UART converter; 3.3 V supply (VCC and V3 connected to the 3V3 net) |
| U22 | USB Type-C connector | Connection to the computer and board power (5 V) |
| U6 | AMS1117-3.3 | Supply of the FPGA I/O banks, CH340C and microphones (3.3 V) |
| — | 4 × INMP441 module | Omnidirectional MEMS microphones with digital I2S output, 24 bits |
| — | 2 × 100 kΩ resistor | Pull-down of the SD0, SD1 lines to GND (external, at the FPGA inputs) |
| — | Wire CH340C:14 → FPGA:33 | RTS# 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
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:
| Module | Function |
|---|---|
i2s_inmp441_rx | I2S master: generates SCK and WS, receives two SD lines (two microphones per line), outputs a frame of four 24-bit samples |
sample_packer | Saturating shift (SHIFT), channel selection (CH_SEL), word-length truncation (BIT_MODE), packing into a 96-bit word |
daq_fifo_ctrl | SCFIFO buffer 96 × 2048, frame (window) state machine, header generation, byte-wise word output, ACK/NAK insertion, overflow handling |
uart_tx_ctrl | UART transmitter 8N1/8N2 with a programmable divider; RTS# check before the start bit |
uart_rx_ctrl | UART receiver 8N1, parsing of the 5-byte command frame, XOR check, BREAK detector |
reg_file | Configuration 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.
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 pin | Function | Microphone 1 | Microphone 2 | Microphone 3 | Microphone 4 |
|---|---|---|---|---|---|
| VDD | Supply 1.8…3.3 V | 3.3 V of the FPGA board (3V3 net) | |||
| GND | Ground | GND | |||
| SCK | Bit clock, input | PIN_28 | |||
| WS | Word select (frame), input | PIN_30 | |||
| SD | Serial data, tri-state output | PIN_31 | PIN_31 | PIN_32 | PIN_32 |
| L/R | Slot select: GND — left, VDD — right | GND | VDD | GND | VDD |
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.
| Pin | Bank | Signal (top.v) | Direction | Connection | Note |
|---|---|---|---|---|---|
| 24 | 2 | clk_50m | input | X2, 50 MHz | Clock input CLK2 |
| 88 | 5 | rst_n | input | button | Active low; asynchronous assertion, synchronous release of reset |
| 1 | 1 | led_n | output | LED | Active low |
| 28 | 2 | i2s_sck | output | SCK of all microphones | 0.5…3.125 MHz |
| 30 | 2 | i2s_ws | output | WS of all microphones | fs = SCK/64 |
| 31 | 2 | i2s_sd[0] | input | SD of microphones 1, 2 | External 100 kΩ resistor to GND |
| 32 | 2 | i2s_sd[1] | input | SD of microphones 3, 4 | External 100 kΩ resistor to GND |
| 10 | 1 | uart_tx | output | CH340C pin 3 (RXD) | Board net UART_TX |
| 23 | 1 | uart_rx | input | CH340C pin 2 (TXD) | Board net UART_RX; clock input CLK1, internal pull-up not supported |
| 33 | 2 | uart_rts_n | input | CH340C pin 14 (RTS#) | Soldered wire; internal pull-up to 3.3 V |
USB-UART connection
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# level | Computer state | FPGA transmitter action (with CTS_EN = 1) |
|---|---|---|
| 0 | ready to receive | transmission enabled |
| 1 | not 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:
- implementation of the specification requirement for hardware pausing of the transmitter (backpressure);
- verification of the overflow handling mechanism on real hardware: setting RTS# = 1 for 0.5 s with
daq_debug.py rts 1causes FIFO overflow, a buffer reset, an increment of the loss counter drop_cnt and resumption of transmission from a new header (verified).
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.
H = 8 → fs = 48,828 Hz (SCK 3.125 MHz); H = 24 → 16,276 Hz; H = 50 → 7,813 Hz (SCK 0.5 MHz)
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:
- The SD inputs pass through a two-flip-flop synchronizer; the value is captured at the end of the SCK high phase, immediately before the falling edge. With capture on the SCK rising edge, receive errors were observed at SCK ≥ 1 MHz due to the long rise time on the SD line.
- A new value of the divider H is applied only at a WS frame boundary, without shortened SCK pulses.
- After SCK starts, the microphone requires 218 SCK periods (from 84 ms at 3.125 MHz to 0.52 s at 0.5 MHz) to enter
normal operation; until this interval expires, no samples are output (signal
ready). - The samples of all four channels of one WS frame are output simultaneously (the
validflag at the end of the frame), so the channels are sampled synchronously.
FPGA operation
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.
- 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.
- HEADER. 8 header bytes are transmitted; after the last byte frame_id is incremented by 1 and the word counters are cleared.
- 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).
- End of window. SINGLE_SHOT — return to IDLE. RUN with CONT_MODE = 0 — transition to HEADER and capture of the next window.
- 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:
| Byte | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Contents | 0x5A | ADDR | DATA[7:0] | DATA[15:8] | XOR of bytes 0…3 |
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.
| Address | Register | Values | Default | Description |
|---|---|---|---|---|
| 0x00 | CTRL | bits 0…3 | 0 | bit0 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) |
| 0x01 | FS_DIV | 1024…6400 | 3072 | Sampling rate divider: fs = 50 MHz / (128·round(FS_DIV/128)), 7.81…48.83 kHz |
| 0x02 | BAUD_DIV | 25…5208 | 434 | UART baud rate divider: rate = 50 MHz / BAUD_DIV (434 → 115,200 baud, 50 → 1 Mbaud) |
| 0x03 | BIT_MODE | 0…4 | 0 | Word length: 24/20/16/12/8 bits (12/10/8/6/4 bytes per four-channel frame) |
| 0x04 | WIN_SIZE | 64…4096 | 1024 | Number of samples in a window |
| 0x05 | UART_CFG | bits 0…1 | 1 | bit0 STOP_BITS: 1 — 8N2, 0 — 8N1 (transmitter only); bit1 CTS_EN — honour RTS# |
| 0x06 | TEST_MODE | 0, 1 | 0 | 1 — test generator instead of the microphones |
| 0x07 | CH_SEL | 0…4 | 4 | 0…3 — transmit one channel (24 bits → 3 bytes, 16 → 2, 8 → 1); 4 — all channels |
| 0x08 | SHIFT | 0…8 | 0 | Saturating 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):
- port at 115,200 baud; BREAK 100 ms; CTRL = 0 — check for an ACK response;
- BAUD_DIV = 50 — ACK at 115,200 baud; switch of the computer port to 1 Mbaud;
- 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:
| Bytes | 0…3 | 4…5 | 6…7 | 8… |
|---|---|---|---|---|
| Contents | AA 55 AA 55 | frame_id, LE | drop_cnt, LE | data 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.
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 Hz | microphone start-up (218 SCK periods) |
| blinks with a period of ≈ 1.3 s | waiting for a command |
| on | transmission of a header or data |
| off for ≈ 0.34 s | FIFO overflow |