[{"data":1,"prerenderedAt":765},["ShallowReactive",2],{"example-page-hifi-amp":3},{"id":4,"info":5,"parts":6,"wired":80,"refs":81,"nets":82,"moreNets":758,"code":759},"hifi-amp","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.",[7,10,13,16,19,22,25,27,29,31,33,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78],[8,9],"capacitor",85,[11,12],"resistor",41,[14,15],"inductor",7,[17,18],"rca_jack",6,[20,21],"elko",5,[23,24],"diode",2,[26,24],"ffc_16",[28,24],"lm2596",[30,24],"opa1678",[32,24],"speaker",[34,35],"adau1701",1,[37,35],"ams1117_33",[39,35],"button",[41,35],"crystal",[43,35],"dir9001",[45,35],"eeprom_24lc256",[47,35],"encoder_ec11",[49,35],"hc4053",[51,35],"headphone_jack_635",[53,35],"led",[55,35],"linkplay_a31",[57,35],"pnp",[59,35],"psu_lrs150_36",[61,35],"sn74lvc157a",[63,35],"speaker_terminal",[65,35],"ssd1306_panel",[67,35],"stm32f411",[69,35],"terminal2",[71,35],"toslink_rx",[73,35],"tpa3251",[75,35],"tpa6139a2",[77,35],"tsop38238",[79,35],"wifi_router",false,{"C1":20,"C2":20,"C3":8,"C4":20,"C5":8,"C6":20,"C7":8,"C8":8,"C9":8,"C10":8,"C11":8,"C12":8,"C13":20,"C14":8,"C15":8,"C16":8,"C17":8,"C18":8,"C19":8,"C20":8,"C21":8,"C22":8,"C27":8,"C30":8,"C31":8,"C32":8,"C33":8,"C34":8,"C35":8,"C36":8,"C37":8,"C38":8,"C41":8,"C42":8,"C43":8,"C44":8,"C45":8,"C46":8,"C48":8,"C52":8,"C54":8,"C58":8,"C59":8,"C60":8,"C61":8,"C62":8,"C63":8,"C64":8,"C65":8,"C66":8,"C67":8,"C69":8,"C71":8,"C73":8,"C75":8,"C77":8,"C78":8,"C79":8,"C84":8,"C87":8,"C88":8,"C89":8,"C90":8,"D1":23,"D2":23,"D3":53,"DS1":65,"J1":69,"J3":71,"J4":17,"J5":17,"J6":17,"J7":17,"J9":51,"J10":17,"J11":17,"J12":26,"J13":26,"PS1":59,"R2":11,"R4":11,"R5":11,"R8":11,"R15":11,"R22":11,"R33":11,"R34":11,"R35":11,"R38":11,"SW1":47,"SW2":39,"U1":67,"U2":28,"U3":28,"U4":37,"U5":55,"U6":43,"U7":61,"U8":49,"U9":34,"U10":45,"U11":30,"U12":30,"U13":73,"U14":75,"U15":77,"Y1":41,"L2":14,"R3":11,"L1":14,"R1":11,"R21":11,"R31":11,"R32":11,"C39":8,"C40":8,"L3":14,"Q1":57,"R7":11,"R19":11,"R20":11,"R39":11,"R40":11,"R24":11,"R28":11,"R16":11,"R9":11,"R10":11,"R11":11,"R12":11,"C47":8,"C53":8,"C80":8,"C82":8,"C81":8,"C83":8,"R13":11,"R14":11,"C23":8,"C25":8,"C24":8,"C26":8,"C68":8,"C70":8,"C72":8,"C74":8},[83,380,398,421,496,503,509,527,540,545,550,555,561,567,581,587,593,605,615,620,627,634,641,647,653,659,666,673,680,687,695,703,710,717,724,729,735,741,747,753],{"name":84,"pins":85},"0",[86,89,91,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,196,198,200,202,204,206,208,210,212,214,217,219,222,225,227,229,231,234,236,238,240,243,245,247,249,251,253,255,257,258,261,263,265,267,269,271,273,275,277,279,281,284,286,288,290,292,294,296,297,299,301,302,303,304,307,309,311,313,315,317,319,321,323,325,327,329,331,333,334,335,337,339,341,342,344,345,347,349,350,352,353,356,358,360,362,365,367,370,372,373,375,376,378],[87,88],"C1","−",[90,88],"C2",[92,93],"C3","2",[95,88],"C4",[97,93],"C5",[99,88],"C6",[101,93],"C7",[103,93],"C8",[105,93],"C9",[107,93],"C10",[109,93],"C11",[111,93],"C12",[113,88],"C13",[115,93],"C14",[117,93],"C15",[119,93],"C16",[121,93],"C17",[123,93],"C18",[125,93],"C19",[127,93],"C20",[129,93],"C21",[131,93],"C22",[133,93],"C27",[135,93],"C30",[137,93],"C31",[139,93],"C32",[141,93],"C33",[143,93],"C34",[145,93],"C35",[147,93],"C36",[149,93],"C37",[151,93],"C38",[153,93],"C41",[155,93],"C42",[157,93],"C43",[159,93],"C44",[161,93],"C45",[163,93],"C46",[165,93],"C48",[167,93],"C52",[169,93],"C54",[171,93],"C58",[173,93],"C59",[175,93],"C60",[177,93],"C61",[179,93],"C62",[181,93],"C63",[183,93],"C64",[185,93],"C65",[187,93],"C66",[189,93],"C67",[191,93],"C69",[193,93],"C71",[195,93],"C73",[197,93],"C75",[199,93],"C77",[201,93],"C78",[203,93],"C79",[205,93],"C84",[207,93],"C87",[209,93],"C88",[211,93],"C89",[213,93],"C90",[215,216],"D1","A",[218,216],"D2",[220,221],"D3","K",[223,224],"DS1","GND",[223,226],"SA0 (D\u002FC#)",[228,93],"J1",[230,224],"J3",[232,233],"J4","SHELL",[235,233],"J5",[237,233],"J6",[239,233],"J7",[241,242],"J9","S (GND)",[244,233],"J10",[246,233],"J11",[248,93],"J12",[248,250],"5",[248,252],"15",[254,93],"J13",[254,256],"12",[254,252],[259,260],"PS1","−V",[262,93],"R2",[264,93],"R4",[266,93],"R5",[268,93],"R8",[270,93],"R15",[272,93],"R22",[274,93],"R33",[276,93],"R34",[278,93],"R35",[280,93],"R38",[282,283],"SW1","C",[282,285],"S2",[287,93],"SW2",[289,224],"U1",[291,224],"U2",[291,293],"ON\u002FOFF",[295,224],"U3",[295,293],[298,224],"U4",[300,224],"U5",[300,224],[300,224],[300,224],[305,306],"U6","DGND",[305,308],"PSCK1",[305,310],"RSV",[305,312],"AGND",[305,314],"CKSEL",[316,224],"U7",[316,318],"4B",[316,320],"4A",[316,322],"G̅",[324,87],"U8",[324,326],"COM C",[324,328],"C0",[324,330],"E̅",[324,332],"VEE",[324,224],[324,285],[336,312],"U9",[336,338],"ADDR0",[336,340],"MP0\u002FSDATA_IN1",[336,306],[336,343],"ADDR1\u002FWB",[336,306],[336,346],"RSVD",[336,348],"PGND",[336,312],[336,351],"PLL_MODE0",[336,312],[354,355],"U10","A0",[354,357],"A1",[354,359],"A2",[354,361],"VSS",[363,364],"U11","V−",[366,364],"U12",[368,369],"U13","M1",[368,371],"M2",[368,224],[374,224],"U14",[374,224],[377,224],"U15",[379,224],"Y1",{"name":381,"pins":382},"V5",[383,385,387,388,389,391,393,395,397],[99,384],"+",[101,386],"1",[113,384],[115,386],[390,93],"L2",[392,386],"R3",[298,394],"VIN",[300,396],"5VIN",[300,396],{"name":399,"pins":400},"V12",[401,402,403,404,405,406,408,410,412,414,416,418,419],[95,384],[163,386],[167,386],[175,386],[177,386],[407,93],"L1",[409,386],"R1",[411,93],"R21",[413,386],"R31",[415,386],"R32",[363,417],"V+",[366,417],[368,420],"VDD",{"name":422,"pins":423},"V33",[424,425,426,427,428,429,430,431,432,433,434,435,436,438,440,441,442,443,444,446,447,449,451,454,456,458,460,462,464,465,467,468,470,472,474,476,478,479,480,482,484,486,488,490,492,493,494,495],[103,386],[105,386],[107,386],[119,386],[121,386],[123,386],[129,386],[133,386],[135,386],[137,386],[139,386],[141,386],[437,93],"C39",[439,93],"C40",[159,386],[201,386],[203,386],[205,386],[223,445],"VDD\u002FVBAT",[248,386],[254,448],"16",[450,386],"L3",[452,453],"Q1","E",[455,93],"R7",[457,93],"R19",[459,93],"R20",[461,93],"R39",[463,386],"R40",[289,420],[298,466],"VOUT",[305,420],[305,469],"PSCK0",[305,471],"RST",[305,473],"VCC",[305,475],"FMT0",[305,477],"FMT1",[316,473],[324,473],[336,481],"SELFBOOT",[336,483],"IOVDD",[336,485],"WP",[336,487],"PVDD",[336,489],"AVDD",[336,491],"PLL_MODE1",[336,489],[354,485],[354,473],[374,420],{"name":497,"pins":498},"V33_IR",[499,500,501],[213,386],[463,93],[377,502],"VS",{"name":504,"pins":505},"V33_OPT",[506,507,508],[117,386],[230,473],[450,93],{"name":510,"pins":511},"V36",[512,513,514,515,516,517,518,519,521,522,523,525],[87,384],[90,384],[92,386],[97,386],[187,386],[189,386],[228,386],[259,520],"+V",[291,394],[295,394],[368,524],"PVDD_AB",[368,526],"PVDD_CD",{"name":528,"pins":529},"VB6",[530,531,532,533,535,537,539],[161,386],[411,386],[272,386],[534,93],"R24",[536,93],"R28",[363,538],"IN2+",[366,538],{"name":541,"pins":542},"VBG",[543,544],[183,386],[368,541],{"name":546,"pins":547},"VCAP1",[548,549],[109,386],[289,546],{"name":551,"pins":552},"VCOMH",[553,554],[209,386],[223,551],{"name":556,"pins":557},"VDRIVE",[558,560],[559,93],"R16",[336,556],{"name":562,"pins":563},"VDRV_B",[564,566],[452,565],"B",[559,386],{"name":568,"pins":569},"VMID",[570,571,572,573,575,577,579],[131,386],[455,386],[268,386],[574,93],"R9",[576,93],"R10",[578,93],"R11",[580,93],"R12",{"name":582,"pins":583},"VOUT0",[584,586],[585,386],"C47",[336,582],{"name":588,"pins":589},"VOUT1",[590,592],[591,386],"C53",[336,588],{"name":594,"pins":595},"VOUT2",[596,598,600,602,604],[597,386],"C80",[599,386],"C82",[248,601],"14",[254,603],"3",[336,594],{"name":606,"pins":607},"VOUT3",[608,610,612,613,614],[609,386],"C81",[611,386],"C83",[248,448],[254,386],[336,606],{"name":616,"pins":617},"VSS_HP",[618,619],[199,386],[374,361],{"name":621,"pins":622},"A31_RST",[623,625],[289,624],"PA8",[300,626],"CPU_RST",{"name":628,"pins":629},"A31_RX",[630,632],[289,631],"PA9",[300,633],"UART_RXD",{"name":635,"pins":636},"A31_TX",[637,639],[289,638],"PA10",[300,640],"UART_TXD",{"name":642,"pins":643},"ADC0",[644,646],[645,93],"R13",[336,642],{"name":648,"pins":649},"ADC1",[650,652],[651,93],"R14",[336,648],{"name":654,"pins":655},"ADCRES",[656,657],[270,386],[336,658],"ADC_RES",{"name":660,"pins":661},"AMP_FAULT",[662,664],[289,663],"PB12",[368,665],"\u002FFAULT",{"name":667,"pins":668},"AMP_RST",[669,671],[289,670],"PB10",[368,672],"\u002FRESET",{"name":674,"pins":675},"B_L1",[676,678,679],[677,93],"C23",[574,386],[324,355],{"name":681,"pins":682},"B_L2",[683,685,686],[684,93],"C25",[578,386],[324,357],{"name":688,"pins":689},"B_R1",[690,692,693],[691,93],"C24",[576,386],[324,694],"B0",{"name":696,"pins":697},"B_R2",[698,700,701],[699,93],"C26",[580,386],[324,702],"B1",{"name":704,"pins":705},"BCK",[706,708],[316,707],"2Y",[336,709],"MP5\u002FBCLK_IN",{"name":711,"pins":712},"BCK_O",[713,715],[305,714],"BCKO",[316,716],"2B",{"name":718,"pins":719},"BCK_S",[720,722],[300,721],"I2S_CLK",[316,723],"2A",{"name":725,"pins":726},"BOOT0",[727,728],[266,386],[289,725],{"name":730,"pins":731},"BST_A",[732,734],[733,386],"C68",[368,730],{"name":736,"pins":737},"BST_B",[738,740],[739,386],"C70",[368,736],{"name":742,"pins":743},"BST_C",[744,746],[745,386],"C72",[368,742],{"name":748,"pins":749},"BST_D",[750,752],[751,386],"C74",[368,748],{"name":754,"pins":755},"C_START",[756,757],[185,386],[368,754],94,{"file":760,"lang":761,"text":762,"lines":763,"more":764},"src\u002Fmain.c","c","\u002F*\n * HiFi amplifier 2 x 50 W - control firmware (STM32F411, Zephyr)\n *\n * The microcontroller runs the front panel and the housekeeping; the audio never passes through it:\n *   streaming module LinkPlay A31 (USART1, LinkPlay MCU protocol) and optical input (DIR9001)\n *   -> source switch 74LVC157 -> DSP ADAU1701 (I2C, volume \u002F source) -> class-D TPA3251 -> speakers\n *   line inputs 1 \u002F 2 -> analog switch 74HC4053 -> DSP analog inputs\n *   DSP outputs 2 \u002F 3 -> pre-out and headphone amplifier TPA6139A2\n *\n * Controls: rotary encoder = volume, its push = next source; standby button; IR remote (NEC).\n * Protection: the power amplifier starts muted (no thump), mutes when headphones are plugged in,\n * and is restarted after a fault.\n *\u002F\n#include \u003Czephyr\u002Fkernel.h>\n#include \u003Czephyr\u002Fdevice.h>\n#include \u003Czephyr\u002Fdrivers\u002Fgpio.h>\n#include \u003Czephyr\u002Fdrivers\u002Fi2c.h>\n#include \u003Czephyr\u002Fdrivers\u002Fuart.h>\n#include \u003Czephyr\u002Fsys\u002Fprintk.h>\n#include \u003Cstring.h>\n#include \u003Cstdio.h>\n#include \u003Cstdlib.h>\n\nextern const uint8_t font5x7[95][5];\n\n\u002F* ---- pins -------------------------------------------------------------------------------- *\u002F\n#define PIN_ENC_A      0   \u002F* PA0  encoder A (pull-up) *\u002F\n#define PIN_ENC_B      1   \u002F* PA1  encoder B (pull-up) *\u002F\n#define PIN_ENC_SW     4   \u002F* PA4  encoder push to GND *\u002F\n#define PIN_STANDBY    5   \u002F* PA5  standby button to GND *\u002F\n#define PIN_A31_RST    8   \u002F* PA8  streaming module reset, active low *\u002F\n#define PIN_IR         0   \u002F* PB0  IR receiver output (active low) *\u002F\n#define PIN_SRC_SEL    1   \u002F* PB1  74LVC157 A\u002FB: low = streaming, high = optical *\u002F\n#define PIN_LINE_SEL   2   \u002F* PB2  74HC4053 S0\u002FS1: low = line 1, high = line 2 *\u002F\n#define PIN_DSP_RST    4   \u002F* PB4  ADAU1701 RESET: rising edge = self-boot *\u002F\n#define PIN_AMP_RST    10  \u002F* PB10 TPA3251 \u002FRESET: low = speakers off *\u002F\n#define PIN_AMP_FAULT  12  \u002F* PB12 TPA3251 \u002FFAULT (open drain, pulled up) *\u002F\n#define PIN_HP_DET     13  \u002F* PB13 headphone jack switch: high = plug in *\u002F\n#define PIN_HP_MUTE    14  \u002F* PB14 TPA6139A2 MUTE: low = muted *\u002F\n#define PIN_OPT_ERR    15  \u002F* PB15 DIR9001 ERROR: low = locked to the optical input *\u002F\n#define PIN_LED_STBY   13  \u002F* PC13 standby LED (red) *\u002F\n\n#define DSP_ADDR   0x34\n#define OLED_ADDR  0x3C\n#define DSP_MASTER_VOL 0x0000   \u002F* parameters of the DSP program (5.23 fixed point) *\u002F\n#define DSP_LINE_VOL   0x0001\n#define DSP_SOURCE     0x0002   \u002F* 0 = I2S input, 1 = analog inputs *\u002F\n#define DSP_MUTE       0x0003\n\n\u002F* NEC remote: address 0x00, these commands *\u002F\n#define IR_STANDBY 0x45\n#define IR_MUTE    0x1C\n#define IR_VOLUP   0x18\n#define IR_VOLDN   0x52\n#define IR_SRCUP   0x5A\n#define IR_SRCDN   0x08\n#define IR_PLAY    0x40\n#define IR_NEXT    0x43\n#define IR_PREV    0x44\n\nstatic const struct device *gpioa = DEVICE_DT_GET(DT_NODELABEL(gpioa));\nstatic const struct device *gpiob = DEVICE_DT_GET(DT_NODELABEL(gpiob));\nstatic const struct device *gpioc = DEVICE_DT_GET(DT_NODELABEL(gpioc));\nstatic const struct device *i2c = DEVICE_DT_GET(DT_NODELABEL(i2c1));\nstatic const struct device *link = DEVICE_DT_GET(DT_NODELABEL(usart1));\n\n\u002F* ---- state ------------------------------------------------------------------------------- *\u002F\nenum source { SRC_STREAM, SRC_OPTICAL, SRC_LINE1, SRC_LINE2, SRC_COUNT };\nstatic const char *const SOURCE_NAME[SRC_COUNT] = { \"Streaming\", \"Optical\", \"Line 1\", \"Line 2\" };\n\nstatic enum source source = SRC_STREAM;\nstatic int volume = 30;            \u002F* 0..100 *\u002F\nstatic bool muted, standby, headphones, wifi, playing = true, optical_lock, amp_fault;\nstatic char title[22] = \"\", artist[22] = \"\";\nstatic bool redraw = true;\nstatic int64_t amp_enable_at;      \u002F* the power amplifier is released at this uptime (ms) *\u002F\n\n\u002F* ---- DSP (ADAU1701) ---------------------------------------------------------------------- *\u002F\n\u002F* a parameter write: 2-byte address, 4 bytes 5.23 fixed point *\u002F\nstatic void dsp_param(uint16_t addr, float value)\n{\n\tint32_t raw = (int32_t)(value * 8388608.0f);\n\tuint8_t b[6] = { addr >> 8, addr & 0xff, raw >> 24, raw >> 16, raw >> 8, raw };\n\ti2c_write(i2c, b, sizeof b, DSP_ADDR);\n}\n\n\u002F* volume 0..100 in 0.8 dB steps (-80 dB at 1, 0 dB at 100); 0 = silent *\u002F\nstatic float volume_gain(int v)\n{\n\tif (v \u003C= 0) return 0.0f;\n\tfloat db = -80.0f + 0.8f * v, g = 1.0f;\n\t\u002F* 10^(db\u002F20) without libm: repeated multiplication by 10^(-0.04) per 0.8 dB step below 0 dB *\u002F\n\tfor (float d = 0.0f; d > db + 0.01f; d -= 0.8f) g *= 0.912011f;\n\treturn g;\n}\n\nstatic void apply_volume(void)\n{\n\tfloat g = muted || standby ? 0.0f : volume_gain(volume);\n\tdsp_param(DSP_MASTER_VOL, g);\n\tdsp_param(DSP_LINE_VOL, g);\n}\n\n\u002F* ---- streaming module (LinkPlay MCU protocol) -------------------------------------------- *\u002F\nstatic char rx_line[200];\nstatic int rx_len;\nstatic char lines[4][200];\nstatic volatile int line_head, line_tail;\n\nstatic void uart_isr(const struct device *dev, void *user)\n{\n\tuint8_t c;\n\tARG_UNUSED(user);\n\twhile (uart_irq_update(dev) && uart_irq_rx_ready(dev)) {\n\t\tif (uart_fifo_read(dev, &c, 1) != 1) break;\n\t\tif (c == '\\n' || c == '\\r') {\n\t\t\tif (rx_len) {\n\t\t\t\trx_line[rx_len] = 0;\n\t\t\t\tmemcpy(lines[line_head], rx_line, rx_len + 1);\n\t\t\t\tline_head = (line_head + 1) % 4;",404,true,1796304409932]