HiFi amplifier 2 × 50 W with streaming
Wi-Fi streaming amplifier with DSP, class-D and a front panel.
- STM32F411
- Wi-Fi router
Open in the simulatorNo account needed: press Simulate and it runs.
How it works
A stereo integrated amplifier like one from the shop: LinkPlay A31 Wi-Fi streaming (AirPlay, Spotify Connect), optical and two RCA inputs, ADAU1701 DSP, TPA3251 class-D (2 × 50 W into 8 Ω) on 36 V, headphone amplifier. An STM32F411 with Zephyr firmware runs it: encoder for volume and source, standby button, IR remote, OLED. The controls sit on a front-panel board joined to the main board by a 16-way FFC. Choose your own music file in the inspector of U5 and hear it through the whole circuit.
Wiring
Each line is one connection (net) and the pins it joins.
| Net | Pins |
|---|---|
| GND | C1 −C2 −C3 2C4 −C5 2C6 −C7 2C8 2C9 2C10 2C11 2C12 2C13 −C14 2C15 2C16 2C17 2C18 2C19 2C20 2C21 2C22 2C27 2C30 2C31 2C32 2C33 2C34 2C35 2C36 2C37 2C38 2C41 2C42 2C43 2C44 2C45 2C46 2C48 2C52 2C54 2C58 2C59 2C60 2C61 2C62 2C63 2C64 2C65 2C66 2C67 2C69 2C71 2C73 2C75 2C77 2C78 2C79 2C84 2C87 2C88 2C89 2C90 2D1 AD2 AD3 KDS1 GNDDS1 SA0 (D/C#)J1 2J3 GNDJ4 SHELLJ5 SHELLJ6 SHELLJ7 SHELLJ9 S (GND)J10 SHELLJ11 SHELLJ12 2J12 5J12 15J13 2J13 12J13 15PS1 −VR2 2R4 2R5 2R8 2R15 2R22 2R33 2R34 2R35 2R38 2SW1 CSW1 S2SW2 2U1 GNDU2 GNDU2 ON/OFFU3 GNDU3 ON/OFFU4 GNDU5 GNDU5 GNDU5 GNDU5 GNDU6 DGNDU6 PSCK1U6 RSVU6 AGNDU6 CKSELU7 GNDU7 4BU7 4AU7 G̅U8 C1U8 COM CU8 C0U8 E̅U8 VEEU8 GNDU8 S2U9 AGNDU9 ADDR0U9 MP0/SDATA_IN1U9 DGNDU9 ADDR1/WBU9 DGNDU9 RSVDU9 PGNDU9 AGNDU9 PLL_MODE0U9 AGNDU10 A0U10 A1U10 A2U10 VSSU11 V−U12 V−U13 M1U13 M2U13 GNDU14 GNDU14 GNDU15 GNDY1 GND |
| V5 | C6 +C7 1C13 +C14 1L2 2R3 1U4 VINU5 5VINU5 5VIN |
| V12 | C4 +C46 1C52 1C60 1C61 1L1 2R1 1R21 2R31 1R32 1U11 V+U12 V+U13 VDD |
| V33 | C8 1C9 1C10 1C16 1C17 1C18 1C21 1C27 1C30 1C31 1C32 1C33 1C39 2C40 2C44 1C78 1C79 1C84 1DS1 VDD/VBATJ12 1J13 16L3 1Q1 ER7 2R19 2R20 2R39 2R40 1U1 VDDU4 VOUTU6 VDDU6 PSCK0U6 RSTU6 VCCU6 FMT0U6 FMT1U7 VCCU8 VCCU9 SELFBOOTU9 IOVDDU9 WPU9 PVDDU9 AVDDU9 PLL_MODE1U9 AVDDU10 WPU10 VCCU14 VDD |
| V33_IR | C90 1R40 2U15 VS |
| V33_OPT | C15 1J3 VCCL3 2 |
| V36 | C1 +C2 +C3 1C5 1C66 1C67 1J1 1PS1 +VU2 VINU3 VINU13 PVDD_ABU13 PVDD_CD |
| VB6 | C45 1R21 1R22 1R24 2R28 2U11 IN2+U12 IN2+ |
| VBG | C64 1U13 VBG |
| VCAP1 | C11 1U1 VCAP1 |
| VCOMH | C88 1DS1 VCOMH |
| VDRIVE | R16 2U9 VDRIVE |
| VDRV_B | Q1 BR16 1 |
| VMID | C22 1R7 1R8 1R9 2R10 2R11 2R12 2 |
| VOUT0 | C47 1U9 VOUT0 |
| VOUT1 | C53 1U9 VOUT1 |
| VOUT2 | C80 1C82 1J12 14J13 3U9 VOUT2 |
| VOUT3 | C81 1C83 1J12 16J13 1U9 VOUT3 |
| VSS_HP | C77 1U14 VSS |
| A31_RST | U1 PA8U5 CPU_RST |
| A31_RX | U1 PA9U5 UART_RXD |
| A31_TX | U1 PA10U5 UART_TXD |
| ADC0 | R13 2U9 ADC0 |
| ADC1 | R14 2U9 ADC1 |
| ADCRES | R15 1U9 ADC_RES |
| AMP_FAULT | U1 PB12U13 /FAULT |
| AMP_RST | U1 PB10U13 /RESET |
| B_L1 | C23 2R9 1U8 A0 |
| B_L2 | C25 2R11 1U8 A1 |
| B_R1 | C24 2R10 1U8 B0 |
| B_R2 | C26 2R12 1U8 B1 |
| BCK | U7 2YU9 MP5/BCLK_IN |
| BCK_O | U6 BCKOU7 2B |
| BCK_S | U5 I2S_CLKU7 2A |
| BOOT0 | R5 1U1 BOOT0 |
| BST_A | C68 1U13 BST_A |
| BST_B | C70 1U13 BST_B |
| BST_C | C72 1U13 BST_C |
| BST_D | C74 1U13 BST_D |
| C_START | C65 1U13 C_START |
94 more connections in the editor.
- C10
- Capacitor
- C11
- Capacitor
- C12
- Capacitor
- C14
- Capacitor
- C15
- Capacitor
- C16
- Capacitor
- C17
- Capacitor
- C18
- Capacitor
- C19
- Capacitor
- C20
- Capacitor
- C21
- Capacitor
- C22
- Capacitor
- C23
- Capacitor
- C24
- Capacitor
- C25
- Capacitor
- C26
- Capacitor
- C27
- Capacitor
- C30
- Capacitor
- C31
- Capacitor
- C32
- Capacitor
- C33
- Capacitor
- C34
- Capacitor
- C35
- Capacitor
- C36
- Capacitor
- C37
- Capacitor
- C38
- Capacitor
- C39
- Capacitor
- C40
- Capacitor
- C41
- Capacitor
- C42
- Capacitor
- C43
- Capacitor
- C44
- Capacitor
- C45
- Capacitor
- C46
- Capacitor
- C47
- Capacitor
- C48
- Capacitor
- C52
- Capacitor
- C53
- Capacitor
- C54
- Capacitor
- C58
- Capacitor
- C59
- Capacitor
- C60
- Capacitor
- C61
- Capacitor
- C62
- Capacitor
- C63
- Capacitor
- C64
- Capacitor
- C65
- Capacitor
- C66
- Capacitor
- C67
- Capacitor
- C68
- Capacitor
- C69
- Capacitor
- C70
- Capacitor
- C71
- Capacitor
- C72
- Capacitor
- C73
- Capacitor
- C74
- Capacitor
- C75
- Capacitor
- C77
- Capacitor
- C78
- Capacitor
- C79
- Capacitor
- C80
- Capacitor
- C81
- Capacitor
- C82
- Capacitor
- C83
- Capacitor
- C84
- Capacitor
- C87
- Capacitor
- C88
- Capacitor
- C89
- Capacitor
- C90
- Capacitor
- D1
- Diode
- D2
- Diode
- D3
- LED
- L1
- Inductor
- L2
- Inductor
- L3
- Inductor
- R1
- Resistor
- R2
- Resistor
- R3
- Resistor
- R4
- Resistor
- R5
- Resistor
- R7
- Resistor
- R8
- Resistor
- R9
- Resistor
- R10
- Resistor
- R11
- Resistor
- R12
- Resistor
- R13
- Resistor
- R14
- Resistor
- R15
- Resistor
- R16
- Resistor
- R19
- Resistor
- R20
- Resistor
- R21
- Resistor
- R22
- Resistor
- R24
- Resistor
- R28
- Resistor
- R31
- Resistor
- R32
- Resistor
- R33
- Resistor
- R34
- Resistor
- R35
- Resistor
- R38
- Resistor
- R39
- Resistor
- R40
- Resistor
- SW2
- Push button
- Y1
- Crystal
Parts
- Capacitor× 85
- Resistor× 41
- Inductor× 7
- RCA (cinch) jack, right angle× 6
- Electrolytic capacitor (polarized)× 5
- Diode× 2
- FFC connector 16-way 0.5 mm, ZIF (Hirose FH12)× 2
- LM2596S-ADJ (step-down regulator 3 A, 150 kHz)× 2
- OPA1678 (dual low-distortion audio op-amp)× 2
- Speaker× 2
- ADAU1701 (SigmaDSP audio processor, 2 ADC / 4 DAC)
- AMS1117-3.3 (1 A LDO regulator)
- Push button
- Crystal
- DIR9001 (S/PDIF receiver to I2S)
- 24LC256 (32 KB I2C EEPROM)
- Rotary encoder Alps EC11E (with push switch, on the board)
- 74HC4053 (triple 2:1 analog switch)
- Headphone jack 6.35 mm, switched (Neutrik NMJ6HCD2)
- LED
- Wi-Fi streaming module LinkPlay A31 (AirPlay, DLNA, Spotify Connect)
- PNP transistor
- Power supply module Mean Well LRS-150-36 (36 V 4.3 A, mains)
- 74LVC157 (quad 2:1 multiplexer, I2S source switch)
- Speaker terminal 4-pole (L+ L− R+ R−)
- OLED panel 0.96" 128×64 (SSD1306, FPC, I²C)
- STM32F411RE (Cortex-M4F, Firmware)
- Screw terminal 2-pin (5.08 mm)
- Optical audio input (TOSLINK receiver)
- TPA3251 (class-D stereo power amplifier, 2 × 78 W into 8 Ω)
- TPA6139A2 (headphone amplifier, ground-referenced)
- IR receiver 38 kHz (TSOP38238, remote controls)
- Wi-Fi router (virtual network)
Firmware
src/main.c · 404 lines · the first 120 here, all of it in the editor
/*
* HiFi amplifier 2 x 50 W - control firmware (STM32F411, Zephyr)
*
* The microcontroller runs the front panel and the housekeeping; the audio never passes through it:
* streaming module LinkPlay A31 (USART1, LinkPlay MCU protocol) and optical input (DIR9001)
* -> source switch 74LVC157 -> DSP ADAU1701 (I2C, volume / source) -> class-D TPA3251 -> speakers
* line inputs 1 / 2 -> analog switch 74HC4053 -> DSP analog inputs
* DSP outputs 2 / 3 -> pre-out and headphone amplifier TPA6139A2
*
* Controls: rotary encoder = volume, its push = next source; standby button; IR remote (NEC).
* Protection: the power amplifier starts muted (no thump), mutes when headphones are plugged in,
* and is restarted after a fault.
*/
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/sys/printk.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
extern const uint8_t font5x7[95][5];
/* ---- pins -------------------------------------------------------------------------------- */
#define PIN_ENC_A 0 /* PA0 encoder A (pull-up) */
#define PIN_ENC_B 1 /* PA1 encoder B (pull-up) */
#define PIN_ENC_SW 4 /* PA4 encoder push to GND */
#define PIN_STANDBY 5 /* PA5 standby button to GND */
#define PIN_A31_RST 8 /* PA8 streaming module reset, active low */
#define PIN_IR 0 /* PB0 IR receiver output (active low) */
#define PIN_SRC_SEL 1 /* PB1 74LVC157 A/B: low = streaming, high = optical */
#define PIN_LINE_SEL 2 /* PB2 74HC4053 S0/S1: low = line 1, high = line 2 */
#define PIN_DSP_RST 4 /* PB4 ADAU1701 RESET: rising edge = self-boot */
#define PIN_AMP_RST 10 /* PB10 TPA3251 /RESET: low = speakers off */
#define PIN_AMP_FAULT 12 /* PB12 TPA3251 /FAULT (open drain, pulled up) */
#define PIN_HP_DET 13 /* PB13 headphone jack switch: high = plug in */
#define PIN_HP_MUTE 14 /* PB14 TPA6139A2 MUTE: low = muted */
#define PIN_OPT_ERR 15 /* PB15 DIR9001 ERROR: low = locked to the optical input */
#define PIN_LED_STBY 13 /* PC13 standby LED (red) */
#define DSP_ADDR 0x34
#define OLED_ADDR 0x3C
#define DSP_MASTER_VOL 0x0000 /* parameters of the DSP program (5.23 fixed point) */
#define DSP_LINE_VOL 0x0001
#define DSP_SOURCE 0x0002 /* 0 = I2S input, 1 = analog inputs */
#define DSP_MUTE 0x0003
/* NEC remote: address 0x00, these commands */
#define IR_STANDBY 0x45
#define IR_MUTE 0x1C
#define IR_VOLUP 0x18
#define IR_VOLDN 0x52
#define IR_SRCUP 0x5A
#define IR_SRCDN 0x08
#define IR_PLAY 0x40
#define IR_NEXT 0x43
#define IR_PREV 0x44
static const struct device *gpioa = DEVICE_DT_GET(DT_NODELABEL(gpioa));
static const struct device *gpiob = DEVICE_DT_GET(DT_NODELABEL(gpiob));
static const struct device *gpioc = DEVICE_DT_GET(DT_NODELABEL(gpioc));
static const struct device *i2c = DEVICE_DT_GET(DT_NODELABEL(i2c1));
static const struct device *link = DEVICE_DT_GET(DT_NODELABEL(usart1));
/* ---- state ------------------------------------------------------------------------------- */
enum source { SRC_STREAM, SRC_OPTICAL, SRC_LINE1, SRC_LINE2, SRC_COUNT };
static const char *const SOURCE_NAME[SRC_COUNT] = { "Streaming", "Optical", "Line 1", "Line 2" };
static enum source source = SRC_STREAM;
static int volume = 30; /* 0..100 */
static bool muted, standby, headphones, wifi, playing = true, optical_lock, amp_fault;
static char title[22] = "", artist[22] = "";
static bool redraw = true;
static int64_t amp_enable_at; /* the power amplifier is released at this uptime (ms) */
/* ---- DSP (ADAU1701) ---------------------------------------------------------------------- */
/* a parameter write: 2-byte address, 4 bytes 5.23 fixed point */
static void dsp_param(uint16_t addr, float value)
{
int32_t raw = (int32_t)(value * 8388608.0f);
uint8_t b[6] = { addr >> 8, addr & 0xff, raw >> 24, raw >> 16, raw >> 8, raw };
i2c_write(i2c, b, sizeof b, DSP_ADDR);
}
/* volume 0..100 in 0.8 dB steps (-80 dB at 1, 0 dB at 100); 0 = silent */
static float volume_gain(int v)
{
if (v <= 0) return 0.0f;
float db = -80.0f + 0.8f * v, g = 1.0f;
/* 10^(db/20) without libm: repeated multiplication by 10^(-0.04) per 0.8 dB step below 0 dB */
for (float d = 0.0f; d > db + 0.01f; d -= 0.8f) g *= 0.912011f;
return g;
}
static void apply_volume(void)
{
float g = muted || standby ? 0.0f : volume_gain(volume);
dsp_param(DSP_MASTER_VOL, g);
dsp_param(DSP_LINE_VOL, g);
}
/* ---- streaming module (LinkPlay MCU protocol) -------------------------------------------- */
static char rx_line[200];
static int rx_len;
static char lines[4][200];
static volatile int line_head, line_tail;
static void uart_isr(const struct device *dev, void *user)
{
uint8_t c;
ARG_UNUSED(user);
while (uart_irq_update(dev) && uart_irq_rx_ready(dev)) {
if (uart_fifo_read(dev, &c, 1) != 1) break;
if (c == '\n' || c == '\r') {
if (rx_len) {
rx_line[rx_len] = 0;
memcpy(lines[line_head], rx_line, rx_len + 1);
line_head = (line_head + 1) % 4;Open in the simulator