deckle studioOpen in the editor

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.

NetPins
GNDC1 −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
V5C6 +C7 1C13 +C14 1L2 2R3 1U4 VINU5 5VINU5 5VIN
V12C4 +C46 1C52 1C60 1C61 1L1 2R1 1R21 2R31 1R32 1U11 V+U12 V+U13 VDD
V33C8 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_IRC90 1R40 2U15 VS
V33_OPTC15 1J3 VCCL3 2
V36C1 +C2 +C3 1C5 1C66 1C67 1J1 1PS1 +VU2 VINU3 VINU13 PVDD_ABU13 PVDD_CD
VB6C45 1R21 1R22 1R24 2R28 2U11 IN2+U12 IN2+
VBGC64 1U13 VBG
VCAP1C11 1U1 VCAP1
VCOMHC88 1DS1 VCOMH
VDRIVER16 2U9 VDRIVE
VDRV_BQ1 BR16 1
VMIDC22 1R7 1R8 1R9 2R10 2R11 2R12 2
VOUT0C47 1U9 VOUT0
VOUT1C53 1U9 VOUT1
VOUT2C80 1C82 1J12 14J13 3U9 VOUT2
VOUT3C81 1C83 1J12 16J13 1U9 VOUT3
VSS_HPC77 1U14 VSS
A31_RSTU1 PA8U5 CPU_RST
A31_RXU1 PA9U5 UART_RXD
A31_TXU1 PA10U5 UART_TXD
ADC0R13 2U9 ADC0
ADC1R14 2U9 ADC1
ADCRESR15 1U9 ADC_RES
AMP_FAULTU1 PB12U13 /FAULT
AMP_RSTU1 PB10U13 /RESET
B_L1C23 2R9 1U8 A0
B_L2C25 2R11 1U8 A1
B_R1C24 2R10 1U8 B0
B_R2C26 2R12 1U8 B1
BCKU7 2YU9 MP5/BCLK_IN
BCK_OU6 BCKOU7 2B
BCK_SU5 I2S_CLKU7 2A
BOOT0R5 1U1 BOOT0
BST_AC68 1U13 BST_A
BST_BC70 1U13 BST_B
BST_CC72 1U13 BST_C
BST_DC74 1U13 BST_D
C_STARTC65 1U13 C_START

94 more connections in the editor.

D3
LED

Parts

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

More examples: Wi-Fi and the Internet of Things