mirror of
https://github.com/D-Modem/hardware.git
synced 2026-11-28 17:48:05 -08:00
firmware: the wideband stick (PS_BOARD=wb), TLV320AIC3204 over USB
pico-serial WB (SPEC-WB.md): the Line stick's USB device in front of a TLV320AIC3204 codec at 96 kHz (48 and 192 kHz for tests), USB 1209:0003. - Clocks (SPEC-WB 4): MCLK is the 12 MHz crystal on GPIO21 (GPOUT0); the codec's PLL P=1 R=1 J.D=8.1920 makes 98.304 MHz; NDAC=NADC= MDAC=MADC=4; 96 kHz: DOSR=AOSR=64, PRB_P7/PRB_R7, BCLK N=4 (6.144 MHz, 64 a frame); 192 kHz: 32, PRB_P17/PRB_R13, N=2; 48 kHz: 128, PRB_P1/PRB_R1, N=8. The codec is the I2S master, 32-bit words. - Bring-up (src/core/wb_codec.c) in SLAA557's order: pin and software reset, PLL, dividers, I2S, INT1 on MFP5, weak AVDD off then the A-LDO at 1.77 V, CM 0.9 V, the left DAC's + and - to HPL and HPR (one DAC, a differential drive), drivers up and their gain flags waited for, then the DAC and both ADCs, their flags waited for. - The hybrid in the left MicPGA: + IN1L, IN1R; - IN2R, IN3R; 10 k each (P1 R52=0x41, R54=0x14): line minus replica. The right MicPGA takes the line alone (IN2R - IN1L, 20 k); the replica alone can't be routed differentially. - I2S slave in PIO (src/wb/i2s.pio), full duplex, DMA ping-pong blocks of 32 frames into src/core/wb_audio.c; out of step, it stops and is restarted (counted). - Requests (src/core/wb_proto.h): info, status (peaks, codec flags), rate, carrier LED, TX gain (DAC volume), RX gain (MicPGA), input channel, firmware and codec loopbacks, codec register read/write by page, reset, UF2. Mono S16 packets up to 240 samples (480 bytes). - test_wb.c: the clocks re-derived from the registers and checked against SLAA557's limits for every rate, the MicPGA bits against the netlist, the bring-up order and timeouts, packet sizing, the samples path, the requests. Each of 15 deliberate breaks is caught. - CI builds PS_BOARD=wb and runs test_wb. All five boards build (SDK 2.3.1, TinyUSB 0.21.0); host tests and check_docs pass. Nothing has run on a WB board yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013PRsUCpxazyipPn5UGGkF8
This commit is contained in:
23 files changed
+2126
-13
No files matched your search
@@ -56,6 +56,13 @@ jobs:
|
||||
-DPICO_TINYUSB_PATH=$PWD/firmware/build-hood/_deps/tinyusb-src
|
||||
ninja -C firmware/build-line
|
||||
|
||||
- name: Build (wideband stick)
|
||||
run: |
|
||||
cmake -S firmware -B firmware/build-wb -G Ninja -DPS_BOARD=wb \
|
||||
-DPICO_SDK_PATH=$PWD/firmware/build-hood/_deps/pico_sdk-src \
|
||||
-DPICO_TINYUSB_PATH=$PWD/firmware/build-hood/_deps/tinyusb-src
|
||||
ninja -C firmware/build-wb
|
||||
|
||||
- uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 22
|
||||
@@ -71,3 +78,4 @@ jobs:
|
||||
firmware/build-w/pico-serial-w.uf2
|
||||
firmware/build-pico/pico-serial-pico.uf2
|
||||
firmware/build-line/pico-serial-line.uf2
|
||||
firmware/build-wb/pico-serial-wb.uf2
|
||||
+25
-4
@@ -4,9 +4,10 @@ cmake_minimum_required(VERSION 3.13)
|
||||
|
||||
# Which board: "hood" (the DB25 hood board), "w" (pico-serial W, with the
|
||||
# RM2 Bluetooth module, SPEC-W.md), "line" (pico-serial Line, the phone-line
|
||||
# stick, SPEC-L.md: other firmware, src/line) or "pico" (bench rig: a
|
||||
# stick, SPEC-L.md: other firmware, src/line), "wb" (pico-serial WB, the
|
||||
# wideband bare-pair stick, SPEC-WB.md: src/wb) or "pico" (bench rig: a
|
||||
# Raspberry Pi Pico wired to the transceivers as SPEC.md 4.5).
|
||||
set(PS_BOARD hood CACHE STRING "pico-serial board: hood | w | line | pico")
|
||||
set(PS_BOARD hood CACHE STRING "pico-serial board: hood | w | line | wb | pico")
|
||||
if (PS_BOARD STREQUAL "hood")
|
||||
set(PICO_BOARD pico_serial_hood)
|
||||
set(PICO_BOARD_HEADER_DIRS ${CMAKE_CURRENT_LIST_DIR}/boards)
|
||||
@@ -16,10 +17,13 @@ elseif (PS_BOARD STREQUAL "w")
|
||||
elseif (PS_BOARD STREQUAL "line")
|
||||
set(PICO_BOARD pico_serial_line)
|
||||
set(PICO_BOARD_HEADER_DIRS ${CMAKE_CURRENT_LIST_DIR}/boards)
|
||||
elseif (PS_BOARD STREQUAL "wb")
|
||||
set(PICO_BOARD pico_serial_wb)
|
||||
set(PICO_BOARD_HEADER_DIRS ${CMAKE_CURRENT_LIST_DIR}/boards)
|
||||
elseif (PS_BOARD STREQUAL "pico")
|
||||
set(PICO_BOARD pico)
|
||||
else ()
|
||||
message(FATAL_ERROR "PS_BOARD must be hood, w, line or pico")
|
||||
message(FATAL_ERROR "PS_BOARD must be hood, w, line, wb or pico")
|
||||
endif ()
|
||||
|
||||
# Pico SDK: PICO_SDK_PATH, or fetched at this tag.
|
||||
@@ -62,6 +66,22 @@ if (PS_BOARD STREQUAL "line")
|
||||
src/core/line_dev.c
|
||||
)
|
||||
pico_generate_pio_header(pico_serial ${CMAKE_CURRENT_LIST_DIR}/src/line/si3056.pio)
|
||||
elseif (PS_BOARD STREQUAL "wb")
|
||||
# the wideband stick: a USB front end for the TLV320AIC3204 codec, no RS-232 core
|
||||
add_executable(pico_serial
|
||||
src/wb/main.c
|
||||
src/wb/codec.c
|
||||
src/wb/wb_stick.c
|
||||
src/stick/usb_stick.c
|
||||
src/core/sstream.c
|
||||
src/core/wb_audio.c
|
||||
src/core/wb_codec.c
|
||||
src/core/wb_dev.c
|
||||
)
|
||||
pico_generate_pio_header(pico_serial ${CMAKE_CURRENT_LIST_DIR}/src/wb/i2s.pio)
|
||||
# the samples' FIFOs: ~20 ms at 192 kHz (sfifo.h)
|
||||
target_compile_definitions(pico_serial PRIVATE SFIFO_SIZE=4096u)
|
||||
target_link_libraries(pico_serial PRIVATE hardware_i2c hardware_dma)
|
||||
else ()
|
||||
add_executable(pico_serial
|
||||
src/rp2040/main.c
|
||||
@@ -73,13 +93,14 @@ else ()
|
||||
)
|
||||
pico_generate_pio_header(pico_serial ${CMAKE_CURRENT_LIST_DIR}/src/rp2040/autobaud.pio)
|
||||
endif ()
|
||||
target_include_directories(pico_serial PRIVATE src src/core src/rp2040 src/stick src/line)
|
||||
target_include_directories(pico_serial PRIVATE src src/core src/rp2040 src/stick src/line src/wb)
|
||||
target_compile_options(pico_serial PRIVATE -Wall -Wextra)
|
||||
target_compile_definitions(pico_serial PRIVATE
|
||||
PS_FW_MAJOR=${PS_FW_MAJOR} PS_FW_MINOR=${PS_FW_MINOR} PS_FW_PATCH=${PS_FW_PATCH}
|
||||
$<$<STREQUAL:${PS_BOARD},pico>:PS_BOARD_PICO>
|
||||
$<$<STREQUAL:${PS_BOARD},w>:PS_BOARD_W>
|
||||
$<$<STREQUAL:${PS_BOARD},line>:PS_BOARD_LINE>
|
||||
$<$<STREQUAL:${PS_BOARD},wb>:PS_BOARD_WB>
|
||||
# picotool can reboot it (into BOOTSEL or the app) through the reset interface;
|
||||
# no "1200 baud means reboot": 1200 baud is a real modem speed
|
||||
PICO_ENABLE_USB_RESET_VIA_VENDOR_INTERFACE=1
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 Neo-Desktop
|
||||
*/
|
||||
// pico-serial WB, the wideband bare-pair stick (SPEC-WB.md): bare RP2040,
|
||||
// W25Q128JV, 12 MHz ABM8-272-T3 (also the codec's MCLK), TLV320AIC3204 codec.
|
||||
#ifndef _BOARDS_PICO_SERIAL_WB_H
|
||||
#define _BOARDS_PICO_SERIAL_WB_H
|
||||
|
||||
pico_board_cmake_set(PICO_PLATFORM, rp2040)
|
||||
|
||||
#define PICO_SERIAL_WB
|
||||
|
||||
// No UART/I2C/SPI defaults (I2C1 is the codec's: src/wb/codec.c); LEDs are
|
||||
// the firmware's (LINK, DATA).
|
||||
|
||||
// W25Q128JV works with the generic W25Q080 boot stage 2
|
||||
#define PICO_BOOT_STAGE2_CHOOSE_W25Q080 1
|
||||
#ifndef PICO_FLASH_SPI_CLKDIV
|
||||
#define PICO_FLASH_SPI_CLKDIV 2
|
||||
#endif
|
||||
pico_board_cmake_set_default(PICO_FLASH_SIZE_BYTES, (16 * 1024 * 1024))
|
||||
#ifndef PICO_FLASH_SIZE_BYTES
|
||||
#define PICO_FLASH_SIZE_BYTES (16 * 1024 * 1024)
|
||||
#endif
|
||||
|
||||
// SPEC.md 4.2: margin for the crystal to start
|
||||
#ifndef PICO_XOSC_STARTUP_DELAY_MULTIPLIER
|
||||
#define PICO_XOSC_STARTUP_DELAY_MULTIPLIER 64
|
||||
#endif
|
||||
|
||||
#ifndef PICO_RP2040_B0_SUPPORTED
|
||||
#define PICO_RP2040_B0_SUPPORTED 0
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 Neo-Desktop
|
||||
*/
|
||||
// TI TLV320AIC3204: the registers and bits the firmware uses. TI SLAA557
|
||||
// (Application Reference Guide, November 2012), chapter 5 "Register Map";
|
||||
// AIC0_* are on page 0, AIC1_* on page 1. Register 0 of every page selects
|
||||
// the page.
|
||||
#pragma once
|
||||
|
||||
#define AIC_I2C_ADDR 0x18 // 7-bit (SLAA557 2.8.1)
|
||||
#define AIC_PAGE 0 // page select, on every page
|
||||
|
||||
// --- page 0 --------------------------------------------------------------
|
||||
#define AIC0_RESET 1 // D0: software reset (self clearing)
|
||||
#define AIC0_CLK_MUX 4 // D6 PLL range, D3:2 PLL_CLKIN, D1:0 CODEC_CLKIN
|
||||
#define AIC0_CLK_MUX_PLL_MCLK 0x00 // PLL_CLKIN = MCLK
|
||||
#define AIC0_CLK_MUX_CODEC_PLL 0x03 // CODEC_CLKIN = PLL_CLK
|
||||
#define AIC0_PLL_PR 5 // D7 power, D6:4 P (0 means 8), D3:0 R
|
||||
#define AIC0_PLL_ON 0x80
|
||||
#define AIC0_PLL_J 6 // D5:0 J, 4..63
|
||||
#define AIC0_PLL_D_MSB 7 // D5:0 D[13:8]: write 7, then 8 at once
|
||||
#define AIC0_PLL_D_LSB 8 // D7:0 D[7:0]
|
||||
#define AIC0_NDAC 11 // D7 power, D6:0 the divider (0 means 128)
|
||||
#define AIC0_MDAC 12
|
||||
#define AIC0_DOSR_MSB 13 // D1:0 DOSR[9:8]: write 13, then 14
|
||||
#define AIC0_DOSR_LSB 14
|
||||
#define AIC0_NADC 18
|
||||
#define AIC0_MADC 19
|
||||
#define AIC0_AOSR 20 // 32, 64 or 128 (0 means 256)
|
||||
#define AIC0_DIV_ON 0x80 // NDAC, MDAC, NADC, MADC, BCLK N: the divider powered up
|
||||
#define AIC0_IFACE1 27 // D7:6 format, D5:4 word length, D3 BCLK out, D2 WCLK out
|
||||
#define AIC0_IFACE1_I2S 0x00
|
||||
#define AIC0_IFACE1_32BIT 0x30
|
||||
#define AIC0_IFACE1_BCLK_OUT 0x08
|
||||
#define AIC0_IFACE1_WCLK_OUT 0x04
|
||||
#define AIC0_IFACE3 29 // D5, D4 loopbacks, D3 BCLK inverted, D2 clocks kept, D1:0 BDIV_CLKIN
|
||||
#define AIC0_IFACE3_ADC_TO_DAC 0x10 // the stereo ADC's output is the DAC's input
|
||||
#define AIC0_IFACE3_CLKS_ON 0x04 // BCLK and WCLK run even with the codec powered down
|
||||
#define AIC0_IFACE3_BDIV_DAC_CLK 0x00 // BCLK = DAC_CLK / N
|
||||
#define AIC0_BCLK_N 30 // D7 power, D6:0 N (0 means 128)
|
||||
#define AIC0_ADC_FLAGS 36 // D7 left PGA at gain, D6 left ADC up, D3 right PGA, D2 right ADC up
|
||||
#define AIC0_ADC_FLAGS_UP 0x44
|
||||
#define AIC0_DAC_FLAGS 37 // D7 left DAC up, D5 HPL up, D3 right DAC up, D1 HPR up
|
||||
#define AIC0_DAC_FLAGS_UP 0xA2 // left DAC, HPL, HPR (the right DAC stays down)
|
||||
#define AIC0_STICKY1 42 // overflows since the last read: D7/D6 left/right DAC, D3/D2 left/right ADC
|
||||
#define AIC0_STICKY1_MASK 0xCC
|
||||
#define AIC0_STICKY2 44 // D7/D6 HPL/HPR over current (and others) since the last read
|
||||
#define AIC0_STICKY2_OC 0xC0
|
||||
#define AIC0_INT1 48 // D3 over current, D2 overflow, D0 pulses until the flags are read
|
||||
#define AIC0_INT1_OC 0x08
|
||||
#define AIC0_INT1_OVF 0x04
|
||||
#define AIC0_INT1_PULSES 0x01
|
||||
#define AIC0_MFP5 52 // GPIO/MFP5: D5:2 function
|
||||
#define AIC0_MFP5_INT1 0x14 // 0101: the pin is INT1
|
||||
#define AIC0_DAC_PRB 60 // D4:0 PRB_Px
|
||||
#define AIC0_ADC_PRB 61 // D4:0 PRB_Rx
|
||||
#define AIC0_DAC_SETUP1 63 // D7 left DAC up, D6 right DAC up, D5:4 left data, D3:2 right data
|
||||
#define AIC0_DAC_LEFT_ON 0x80
|
||||
#define AIC0_DAC_LEFT_DATA 0x10 // left DAC = left channel of the audio interface
|
||||
#define AIC0_DAC_SETUP2 64 // D3 left mute, D2 right mute
|
||||
#define AIC0_DAC_MUTE_R 0x04
|
||||
#define AIC0_DAC_MUTE_L 0x08
|
||||
#define AIC0_DAC_VOL_L 65 // signed, 0.5 dB steps: -127 (-63.5 dB) .. 48 (+24 dB)
|
||||
#define AIC0_ADC_SETUP 81 // D7 left ADC up, D6 right ADC up
|
||||
#define AIC0_ADC_ON 0xC0
|
||||
#define AIC0_ADC_FINE 82 // D7 left mute, D3 right mute (both muted at reset)
|
||||
#define AIC0_ADC_VOL_L 83 // signed, 0.5 dB steps: -24 .. 40
|
||||
#define AIC0_ADC_VOL_R 84
|
||||
|
||||
// --- page 1 --------------------------------------------------------------
|
||||
#define AIC1_POWER 1 // D3: AVDD's weak connection to DVDD off
|
||||
#define AIC1_POWER_NO_WEAK_AVDD 0x08
|
||||
#define AIC1_LDO 2 // D7:6 DVDD LDO, D5:4 AVDD LDO, D3 analog off, D0 AVDD LDO on
|
||||
#define AIC1_LDO_AVDD_1V77 0x20 // 10: 1.77 V (datasheet 7.9, LDOMode 2)
|
||||
#define AIC1_LDO_ANALOG_OFF 0x08
|
||||
#define AIC1_LDO_AVDD_ON 0x01
|
||||
#define AIC1_PLAY1 3 // D4:2 left DAC PowerTune: 000 PTM_P3/P4
|
||||
#define AIC1_PLAY2 4 // the right DAC's
|
||||
#define AIC1_OUT_POWER 9 // D5 HPL, D4 HPR, D3 LOL, D2 LOR, D1 MAL, D0 MAR
|
||||
#define AIC1_OUT_HPL 0x20
|
||||
#define AIC1_OUT_HPR 0x10
|
||||
#define AIC1_CM 10 // D6 chip CM (0: 0.9 V), D5:4 HP CM (00: = chip CM), D1 HP from LDOIN
|
||||
#define AIC1_HPL_ROUTE 12 // D3 left DAC +, D2 IN1L, D1 MAL, D0 MAR
|
||||
#define AIC1_HPL_LDAC 0x08
|
||||
#define AIC1_HPR_ROUTE 13 // D4 left DAC -, D3 right DAC +, D2 IN1R, D1 MAR, D0 HPL
|
||||
#define AIC1_HPR_LDAC_M 0x10
|
||||
#define AIC1_HPL_GAIN 16 // D6 mute, D5:0 gain in dB, 6-bit two's complement: -6 .. 29
|
||||
#define AIC1_HPR_GAIN 17
|
||||
#define AIC1_HP_START 20 // D7:6 soft routing, D5:2 slow charge, D1:0 its resistor
|
||||
#define AIC1_LPGA_P 52 // left MicPGA +: D7:6 IN1L, D5:4 IN2L, D3:2 IN3L, D1:0 IN1R
|
||||
#define AIC1_LPGA_M 54 // left MicPGA -: D7:6 CM1L, D5:4 IN2R, D3:2 IN3R, D1:0 CM2L
|
||||
#define AIC1_RPGA_P 55 // right MicPGA +: D7:6 IN1R, D5:4 IN2R, D3:2 IN3R, D1:0 IN2L
|
||||
#define AIC1_RPGA_M 57 // right MicPGA -: D7:6 CM1R, D5:4 IN1L, D3:2 IN3L, D1:0 CM2R
|
||||
#define AIC_R10K 1u // each routing field: 00 off, 01 10 kOhm, 10 20 kOhm, 11 40 kOhm
|
||||
#define AIC_R20K 2u
|
||||
#define AIC_ROUTE(r, shift) ((r) << (shift))
|
||||
#define AIC1_FLOAT 58 // inputs weakly at CM: D7 IN1L, D6 IN1R, D5 IN2L, D4 IN2R, D3 IN3L, D2 IN3R
|
||||
#define AIC1_FLOAT_IN2L 0x20
|
||||
#define AIC1_FLOAT_IN3L 0x08
|
||||
#define AIC1_LPGA_VOL 59 // D7: 1 = fixed 0 dB; D6:0 gain in 0.5 dB steps, 0..95
|
||||
#define AIC1_RPGA_VOL 60
|
||||
#define AIC1_PGA_MAX 95
|
||||
#define AIC1_PTM_R 61 // 0x00: PTM_R4
|
||||
#define AIC1_DAC_GAIN_FLAGS 63 // D7 HPL at its gain, D6 HPR at its gain
|
||||
#define AIC1_DAC_GAIN_FLAGS_HP 0xC0
|
||||
#define AIC1_IN_CHARGE 71 // analog inputs' power-up time: 0x32 = 3.1 ms
|
||||
#define AIC1_REF 123 // D2 reference always on, D1:0 fast charge: 01 = 40 ms
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 Neo-Desktop
|
||||
*/
|
||||
// The WB stick's samples between the I2S frames and the streams. See wb_audio.h.
|
||||
#include "wb_audio.h"
|
||||
#include "wb_proto.h"
|
||||
|
||||
void wb_audio_init(wb_audio_t *a, uint32_t prefill) {
|
||||
sstream_init(&a->s, prefill);
|
||||
a->input = WP_INPUT_HYBRID;
|
||||
a->loopback = false;
|
||||
for (int i = 0; i < WB_PEAKS; i++) a->peak[i] = 0;
|
||||
a->frames = 0;
|
||||
a->tx_active = 0;
|
||||
}
|
||||
|
||||
// |s| (32768 for -32768).
|
||||
static uint16_t mag(int16_t s) { return (uint16_t)(s < 0 ? -(int32_t)s : s); }
|
||||
|
||||
// peak = max(peak, |s|).
|
||||
static void track(volatile uint16_t *peak, int16_t s) {
|
||||
uint16_t m = mag(s);
|
||||
if (m > *peak) *peak = m;
|
||||
}
|
||||
|
||||
void wb_audio_rx(wb_audio_t *a, const uint32_t *words, uint32_t frames) {
|
||||
uint8_t input = a->input;
|
||||
bool loop = a->loopback;
|
||||
for (uint32_t i = 0; i < frames; i++) {
|
||||
int16_t l = wb_word_sample(words[2 * i]), r = wb_word_sample(words[2 * i + 1]);
|
||||
track(&a->peak[WB_PEAK_HYBRID], l);
|
||||
track(&a->peak[WB_PEAK_LINE], r);
|
||||
if (!loop) sstream_in(&a->s, input == WP_INPUT_LINE ? r : l);
|
||||
}
|
||||
a->frames += frames;
|
||||
}
|
||||
|
||||
void wb_audio_tx(wb_audio_t *a, uint32_t *words, uint32_t frames) {
|
||||
bool loop = a->loopback;
|
||||
for (uint32_t i = 0; i < frames; i++) {
|
||||
int16_t s = sstream_out(&a->s);
|
||||
if (s) a->tx_active++;
|
||||
track(&a->peak[WB_PEAK_TX], s);
|
||||
words[2 * i] = words[2 * i + 1] = wb_sample_word(s);
|
||||
if (loop) sstream_in(&a->s, s);
|
||||
}
|
||||
}
|
||||
|
||||
void wb_audio_peaks(wb_audio_t *a, uint16_t peak[WB_PEAKS]) {
|
||||
for (int i = 0; i < WB_PEAKS; i++) {
|
||||
peak[i] = a->peak[i];
|
||||
a->peak[i] = 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 Neo-Desktop
|
||||
*/
|
||||
// The WB stick's samples between the codec's I2S frames and the sample
|
||||
// streams (portable: the DMA interrupt calls it with each block of frames;
|
||||
// host tests do too).
|
||||
//
|
||||
// Each I2S frame is two 32-bit words, left then right, MSB first: the
|
||||
// codec's two ADCs in (left: the hybrid, right: the line), the DACs out.
|
||||
// One ADC channel goes to the host as S16 (the top 16 bits); the host's
|
||||
// S16 sample goes out in both slots (the left DAC plays it, the right one
|
||||
// is powered down). The firmware loopback sends the host's samples back to
|
||||
// it in place of the line's. The largest |sample| each way is kept for the
|
||||
// host's level readout.
|
||||
#pragma once
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "sstream.h"
|
||||
|
||||
enum { WB_PEAK_HYBRID, WB_PEAK_LINE, WB_PEAK_TX, WB_PEAKS };
|
||||
|
||||
typedef struct {
|
||||
sstream_t s; // the samples (sstream.h)
|
||||
volatile uint8_t input; // WP_INPUT_*: the ADC channel the host gets
|
||||
volatile bool loopback; // the host's samples back to it (WP_LOOP_FIRMWARE)
|
||||
volatile uint16_t peak[WB_PEAKS];
|
||||
volatile uint32_t frames; // frames received (wraps)
|
||||
volatile uint32_t tx_active; // non-silent samples of the host's played (wraps)
|
||||
} wb_audio_t;
|
||||
|
||||
// Fresh streams (prefill samples of head start for the host's), the left
|
||||
// ADC for the host, no loopback, the peaks cleared.
|
||||
void wb_audio_init(wb_audio_t *a, uint32_t prefill);
|
||||
// A block of received frames (2 words each): the chosen channel to the host
|
||||
// (unless looping back), the peaks, the frame count.
|
||||
void wb_audio_rx(wb_audio_t *a, const uint32_t *words, uint32_t frames);
|
||||
// A block of frames to send (2 words each), from the host's samples (silence
|
||||
// while there are none); in loopback each also goes back to the host.
|
||||
void wb_audio_tx(wb_audio_t *a, uint32_t *words, uint32_t frames);
|
||||
// The peaks since the last call, then cleared. Call with the interrupt off.
|
||||
void wb_audio_peaks(wb_audio_t *a, uint16_t peak[WB_PEAKS]);
|
||||
// An I2S word's sample: its top 16 bits.
|
||||
static inline int16_t wb_word_sample(uint32_t w) { return (int16_t)(uint16_t)(w >> 16); }
|
||||
// A sample as an I2S word: MSB-aligned, the low bits zero.
|
||||
static inline uint32_t wb_sample_word(int16_t s) { return (uint32_t)(uint16_t)s << 16; }
|
||||
@@ -0,0 +1,179 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 Neo-Desktop
|
||||
*/
|
||||
// The WB stick's codec: clocks, register access by page, bring-up. See
|
||||
// wb_codec.h; the registers are in aic3204.h.
|
||||
#include <stddef.h>
|
||||
#include "aic3204.h"
|
||||
#include "wb_codec.h"
|
||||
#include "wb_hal.h"
|
||||
|
||||
// The rates (SPEC-WB.md §4). Each keeps DOSR x fs = 6.144 MHz (SLAA557
|
||||
// 2.4.6: between 2.8 and 6.2 MHz) and MDAC x DOSR / 32 at least the
|
||||
// processing block's resource class (Tables 2-7 and 2-16):
|
||||
// 48 kHz: OSR 128, filter A: PRB_P1 (RC 8), PRB_R1 (RC 6); 4 x 128 / 32 = 16
|
||||
// 96 kHz: OSR 64, filter B: PRB_P7 (RC 6), PRB_R7 (RC 3); 4 x 64 / 32 = 8
|
||||
// 192 kHz: OSR 32, filter C: PRB_P17 (RC 3), PRB_R13 (RC 3); 4 x 32 / 32 = 4
|
||||
// BCLK = 24.576 MHz / N = 64 fs: N = 8, 4, 2.
|
||||
static const wb_rate_t rates[] = {
|
||||
{48000, 128, 1, 1, 8},
|
||||
{96000, 64, 7, 7, 4},
|
||||
{192000, 32, 17, 13, 2},
|
||||
};
|
||||
|
||||
#define PAGE_UNKNOWN 0xFF
|
||||
static uint8_t s_page = PAGE_UNKNOWN;
|
||||
|
||||
#define HP_TIMEOUT_MS 500 // the drivers' soft-stepping up to their gain
|
||||
#define POWER_TIMEOUT_MS 200 // the converters' power-up flags
|
||||
|
||||
const wb_rate_t *wb_rate(uint32_t hz) {
|
||||
for (size_t i = 0; i < sizeof rates / sizeof rates[0]; i++)
|
||||
if (rates[i].hz == hz) return &rates[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Select a page, unless it already is (an access that failed forgets it).
|
||||
static bool page(uint8_t p) {
|
||||
if (p == s_page) return true;
|
||||
if (!whal_codec_write(AIC_PAGE, p)) {
|
||||
s_page = PAGE_UNKNOWN;
|
||||
return false;
|
||||
}
|
||||
s_page = p;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool wb_codec_write(uint8_t p, uint8_t reg, uint8_t v) {
|
||||
if (!page(p)) return false;
|
||||
if (whal_codec_write(reg, v)) return true;
|
||||
s_page = PAGE_UNKNOWN;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool wb_codec_read(uint8_t p, uint8_t reg, uint8_t *v) {
|
||||
if (!page(p)) return false;
|
||||
if (whal_codec_read(reg, v)) return true;
|
||||
s_page = PAGE_UNKNOWN;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Wait until (register & mask) == mask, reading every millisecond; false
|
||||
// after timeout_ms (or if the codec stops answering).
|
||||
static bool wait_flags(uint8_t p, uint8_t reg, uint8_t mask, uint32_t timeout_ms) {
|
||||
for (uint32_t t = 0;; t++) {
|
||||
uint8_t v = 0;
|
||||
if (!wb_codec_read(p, reg, &v)) return false;
|
||||
if ((v & mask) == mask) return true;
|
||||
if (t >= timeout_ms) return false;
|
||||
whal_sleep_ms(1);
|
||||
}
|
||||
}
|
||||
|
||||
#define W(p, r, v) do { if (!wb_codec_write((p), (r), (uint8_t)(v))) return WB_CODEC_NO_ANSWER; } while (0)
|
||||
|
||||
wb_codec_result_t wb_codec_start(const wb_codec_cfg_t *cfg) {
|
||||
const wb_rate_t *rt = cfg->rate;
|
||||
|
||||
// a known start: hardware reset, then the software reset on page 0
|
||||
whal_codec_reset();
|
||||
whal_sleep_ms(1); // SLAA557 3.2: 1 ms before any access
|
||||
s_page = PAGE_UNKNOWN;
|
||||
W(0, AIC0_RESET, 0x01);
|
||||
s_page = 0; // a reset leaves page 0 selected
|
||||
whal_sleep_ms(1);
|
||||
|
||||
// clocks: PLL from MCLK; P, R, J, D first, then the PLL powered up
|
||||
// (10 ms to lock, SLAA557 3.4); CODEC_CLKIN = PLL_CLK
|
||||
W(0, AIC0_CLK_MUX, AIC0_CLK_MUX_PLL_MCLK | AIC0_CLK_MUX_CODEC_PLL);
|
||||
W(0, AIC0_PLL_PR, (WB_PLL_P & 7) << 4 | WB_PLL_R);
|
||||
W(0, AIC0_PLL_J, WB_PLL_J);
|
||||
W(0, AIC0_PLL_D_MSB, WB_PLL_D >> 8); // D: 7 then 8, back to back
|
||||
W(0, AIC0_PLL_D_LSB, WB_PLL_D & 0xFF);
|
||||
W(0, AIC0_PLL_PR, AIC0_PLL_ON | (WB_PLL_P & 7) << 4 | WB_PLL_R);
|
||||
whal_sleep_ms(10);
|
||||
// dividers, oversampling (DOSR: 13 then 14), processing blocks
|
||||
W(0, AIC0_NDAC, AIC0_DIV_ON | WB_NDAC);
|
||||
W(0, AIC0_MDAC, AIC0_DIV_ON | WB_MDAC);
|
||||
W(0, AIC0_DOSR_MSB, rt->osr >> 8);
|
||||
W(0, AIC0_DOSR_LSB, rt->osr & 0xFF);
|
||||
W(0, AIC0_NADC, AIC0_DIV_ON | WB_NADC);
|
||||
W(0, AIC0_MADC, AIC0_DIV_ON | WB_MADC);
|
||||
W(0, AIC0_AOSR, rt->osr & 0xFF);
|
||||
// the audio interface: I2S, 32-bit words (PTM_P4 wants 20 or more), the
|
||||
// codec the master: BCLK = DAC_CLK / N, WCLK = DAC_FS
|
||||
W(0, AIC0_IFACE1, AIC0_IFACE1_I2S | AIC0_IFACE1_32BIT | AIC0_IFACE1_BCLK_OUT | AIC0_IFACE1_WCLK_OUT);
|
||||
W(0, AIC0_IFACE3, AIC0_IFACE3_CLKS_ON | AIC0_IFACE3_BDIV_DAC_CLK |
|
||||
(cfg->loop_codec ? AIC0_IFACE3_ADC_TO_DAC : 0));
|
||||
W(0, AIC0_BCLK_N, AIC0_DIV_ON | rt->bclk_n);
|
||||
W(0, AIC0_DAC_PRB, rt->prb_p);
|
||||
W(0, AIC0_ADC_PRB, rt->prb_r);
|
||||
// INT1 on MFP5: overflows and over current, pulsing until the flags are read
|
||||
W(0, AIC0_INT1, AIC0_INT1_OC | AIC0_INT1_OVF | AIC0_INT1_PULSES);
|
||||
W(0, AIC0_MFP5, AIC0_MFP5_INT1);
|
||||
|
||||
// analog: AVDD from the A-LDO (1.77 V, its highest) with the weak
|
||||
// AVDD-DVDD link off first; the analog blocks on
|
||||
W(1, AIC1_POWER, AIC1_POWER_NO_WEAK_AVDD);
|
||||
W(1, AIC1_LDO, AIC1_LDO_AVDD_1V77 | AIC1_LDO_AVDD_ON);
|
||||
// common mode 0.9 V, the headphone drivers' the same and powered from
|
||||
// AVDD (a differential load needs that: SLAA557 2.2.3.4)
|
||||
W(1, AIC1_CM, 0x00);
|
||||
W(1, AIC1_PLAY1, 0x00); // PTM_P4 (with 32-bit words)
|
||||
W(1, AIC1_PLAY2, 0x00);
|
||||
W(1, AIC1_PTM_R, 0x00); // PTM_R4
|
||||
W(1, AIC1_IN_CHARGE, 0x32); // the inputs' power-up time: 3.1 ms
|
||||
W(1, AIC1_REF, 0x01); // the reference's fast charge: 40 ms
|
||||
// no slow charge: the drivers' load is differential with no coupling
|
||||
// capacitor (SLAA557 2.2.3: "unnecessary")
|
||||
W(1, AIC1_HP_START, 0x00);
|
||||
// the line drive: the left DAC's two outputs, HPL = +, HPR = -
|
||||
// (SLAA557 2.2.3.2, "Mono Differential DAC to Mono Differential Headphone")
|
||||
W(1, AIC1_HPL_ROUTE, AIC1_HPL_LDAC);
|
||||
W(1, AIC1_HPR_ROUTE, AIC1_HPR_LDAC_M);
|
||||
// the hybrid, in the left MicPGA: + = IN1L (line A) + IN1R (replica B),
|
||||
// - = IN2R (line B) + IN3R (replica A), 10 kOhm each: line - replica
|
||||
W(1, AIC1_LPGA_P, AIC_ROUTE(AIC_R10K, 6) | AIC_ROUTE(AIC_R10K, 0));
|
||||
W(1, AIC1_LPGA_M, AIC_ROUTE(AIC_R10K, 4) | AIC_ROUTE(AIC_R10K, 2));
|
||||
// the right MicPGA: the line alone, + = IN2R (line B), - = IN1L (line A),
|
||||
// 20 kOhm (6 dB less gain: our own full-scale transmit fits)
|
||||
W(1, AIC1_RPGA_P, AIC_ROUTE(AIC_R20K, 4));
|
||||
W(1, AIC1_RPGA_M, AIC_ROUTE(AIC_R20K, 4));
|
||||
W(1, AIC1_FLOAT, AIC1_FLOAT_IN2L | AIC1_FLOAT_IN3L); // the unused inputs held at CM
|
||||
W(1, AIC1_LPGA_VOL, cfg->rx_gain); // D7 clear: the gain applies
|
||||
W(1, AIC1_RPGA_VOL, 0x00);
|
||||
// the drivers at 0 dB, unmuted, then powered up; wait for their soft-stepping
|
||||
W(1, AIC1_HPL_GAIN, 0x00);
|
||||
W(1, AIC1_HPR_GAIN, 0x00);
|
||||
W(1, AIC1_OUT_POWER, AIC1_OUT_HPL | AIC1_OUT_HPR);
|
||||
if (!wait_flags(1, AIC1_DAC_GAIN_FLAGS, AIC1_DAC_GAIN_FLAGS_HP, HP_TIMEOUT_MS)) return WB_CODEC_HP_TIMEOUT;
|
||||
|
||||
// the left DAC up (the left channel's data), unmuted at its volume; the
|
||||
// right DAC stays down and muted
|
||||
W(0, AIC0_DAC_SETUP1, AIC0_DAC_LEFT_ON | AIC0_DAC_LEFT_DATA);
|
||||
W(0, AIC0_DAC_VOL_L, cfg->tx_gain);
|
||||
W(0, AIC0_DAC_SETUP2, AIC0_DAC_MUTE_R);
|
||||
// both ADCs up, unmuted, 0 dB digital
|
||||
W(0, AIC0_ADC_SETUP, AIC0_ADC_ON);
|
||||
W(0, AIC0_ADC_FINE, 0x00);
|
||||
if (!wait_flags(0, AIC0_DAC_FLAGS, AIC0_DAC_FLAGS_UP, POWER_TIMEOUT_MS) ||
|
||||
!wait_flags(0, AIC0_ADC_FLAGS, AIC0_ADC_FLAGS_UP, POWER_TIMEOUT_MS))
|
||||
return WB_CODEC_POWER_TIMEOUT;
|
||||
return WB_CODEC_OK;
|
||||
}
|
||||
|
||||
bool wb_codec_tx_gain(int16_t half_db) {
|
||||
if (half_db < -127 || half_db > 48) return false;
|
||||
return wb_codec_write(0, AIC0_DAC_VOL_L, (uint8_t)(int8_t)half_db);
|
||||
}
|
||||
|
||||
bool wb_codec_rx_gain(int16_t half_db) {
|
||||
if (half_db < 0 || half_db > AIC1_PGA_MAX) return false;
|
||||
return wb_codec_write(1, AIC1_LPGA_VOL, (uint8_t)half_db);
|
||||
}
|
||||
|
||||
bool wb_codec_loop(bool on) {
|
||||
return wb_codec_write(0, AIC0_IFACE3, AIC0_IFACE3_CLKS_ON | AIC0_IFACE3_BDIV_DAC_CLK |
|
||||
(on ? AIC0_IFACE3_ADC_TO_DAC : 0));
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 Neo-Desktop
|
||||
*/
|
||||
// The WB stick's codec (TLV320AIC3204, SPEC-WB.md §3-4), portable: its
|
||||
// clocks for each sample rate, its registers by page, and its bring-up
|
||||
// (TI SLAA557 2.3.3.3 "ADC Setup", 2.4.6 "DAC Setup"). The hardware is
|
||||
// behind wb_hal.h.
|
||||
//
|
||||
// Clocks: MCLK is the RP2040's 12 MHz crystal (GPOUT0, undivided). The
|
||||
// codec's PLL makes PLL_CLK = 12 MHz x R x J.D / P = 12 x 8.192 = 98.304 MHz,
|
||||
// its CODEC_CLKIN. Then DAC_CLK = ADC_CLK = 98.304 / 4 = 24.576 MHz,
|
||||
// DAC_MOD_CLK = ADC_MOD_CLK = 24.576 / 4 = 6.144 MHz, fs = 6.144 MHz / OSR,
|
||||
// and BCLK = DAC_CLK / N = 64 fs: 32-bit slots, I2S, the codec the master.
|
||||
#pragma once
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define WB_MCLK_HZ 12000000u
|
||||
#define WB_PLL_P 1
|
||||
#define WB_PLL_R 1
|
||||
#define WB_PLL_J 8
|
||||
#define WB_PLL_D 1920 // J.D = 8.1920
|
||||
#define WB_NDAC 4
|
||||
#define WB_MDAC 4
|
||||
#define WB_NADC 4
|
||||
#define WB_MADC 4
|
||||
#define WB_BCLK_PER_FRAME 64 // two 32-bit slots
|
||||
|
||||
// One sample rate's settings: the oversampling ratio (DOSR = AOSR), the
|
||||
// processing blocks (PRB_Px, PRB_Rx: their decimation and interpolation
|
||||
// filters suit the OSR) and the BCLK divider.
|
||||
typedef struct {
|
||||
uint32_t hz;
|
||||
uint16_t osr;
|
||||
uint8_t prb_p, prb_r;
|
||||
uint8_t bclk_n;
|
||||
} wb_rate_t;
|
||||
|
||||
// What the bring-up sets besides the rate: the host's settings.
|
||||
typedef struct {
|
||||
const wb_rate_t *rate;
|
||||
int8_t tx_gain; // the left DAC's digital volume, 0.5 dB steps (-127..48)
|
||||
uint8_t rx_gain; // the left (hybrid) MicPGA's gain, 0.5 dB steps (0..95)
|
||||
bool loop_codec; // the ADC's output to the DAC (WP_LOOP_CODEC)
|
||||
} wb_codec_cfg_t;
|
||||
|
||||
// How the bring-up went.
|
||||
typedef enum {
|
||||
WB_CODEC_OK,
|
||||
WB_CODEC_NO_ANSWER, // no I2C answer
|
||||
WB_CODEC_HP_TIMEOUT, // the headphone drivers never reached their gain
|
||||
WB_CODEC_POWER_TIMEOUT, // the DAC, the drivers or the ADCs never said they were up
|
||||
} wb_codec_result_t;
|
||||
|
||||
// The settings for a rate (48000, 96000 or 192000 Hz), NULL for another.
|
||||
const wb_rate_t *wb_rate(uint32_t hz);
|
||||
// A register on a page (the page selected first if it isn't already).
|
||||
bool wb_codec_write(uint8_t page, uint8_t reg, uint8_t v);
|
||||
// Read a register on a page.
|
||||
bool wb_codec_read(uint8_t page, uint8_t reg, uint8_t *v);
|
||||
// The codec reset (hardware and software) and brought up as cfg says: PLL
|
||||
// and dividers, I2S master, LDO and common mode, the hybrid's mixer, the
|
||||
// differential line drive; the drivers and converters powered up in
|
||||
// SLAA557's order, waiting on their flags. Takes about 60 ms.
|
||||
wb_codec_result_t wb_codec_start(const wb_codec_cfg_t *cfg);
|
||||
// The left DAC's volume (0.5 dB steps, -127..48); false if out of range.
|
||||
bool wb_codec_tx_gain(int16_t half_db);
|
||||
// The left MicPGA's gain (0.5 dB steps, 0..95); false if out of range.
|
||||
bool wb_codec_rx_gain(int16_t half_db);
|
||||
// The ADC-to-DAC loopback on or off.
|
||||
bool wb_codec_loop(bool on);
|
||||
@@ -0,0 +1,229 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 Neo-Desktop
|
||||
*/
|
||||
// The WB stick's device logic. See wb_dev.h and wb_proto.h.
|
||||
#include <string.h>
|
||||
#include "aic3204.h"
|
||||
#include "wb_dev.h"
|
||||
#include "wb_hal.h"
|
||||
|
||||
#ifndef PS_FW_MAJOR
|
||||
#define PS_FW_MAJOR 0
|
||||
#define PS_FW_MINOR 0
|
||||
#define PS_FW_PATCH 0
|
||||
#endif
|
||||
|
||||
#define CLOCK_CHECK_MS 100 // the frames counted this often
|
||||
#define CLOCK_START_MS 20 // the first frames after a start, at most this late
|
||||
#define DATA_MS 50 // the DATA LED stays lit this long after a non-silent sample
|
||||
|
||||
// Whether the codec is up and clocking.
|
||||
static bool codec_up(const wb_dev_t *d) { return d->result == WB_CODEC_OK && d->clock; }
|
||||
|
||||
// Whether the LINK LED is lit: as the host set it, or (AUTO) while streaming.
|
||||
static bool carrier(const wb_dev_t *d) {
|
||||
return d->carrier == WP_CARRIER_ON || (d->carrier == WP_CARRIER_AUTO && whal_streaming());
|
||||
}
|
||||
|
||||
bool wb_dev_init(wb_dev_t *d, uint32_t rate) {
|
||||
memset(d, 0, sizeof *d);
|
||||
d->rate = wb_rate(rate);
|
||||
if (!d->rate) d->rate = wb_rate(96000);
|
||||
d->carrier = WP_CARRIER_AUTO;
|
||||
d->input = WP_INPUT_HYBRID;
|
||||
d->loopback = WP_LOOP_OFF;
|
||||
return wb_dev_start(d);
|
||||
}
|
||||
|
||||
bool wb_dev_start(wb_dev_t *d) {
|
||||
whal_i2s_stop();
|
||||
wb_codec_cfg_t cfg = {d->rate, d->tx_gain, d->rx_gain, d->loopback == WP_LOOP_CODEC};
|
||||
d->result = wb_codec_start(&cfg);
|
||||
whal_i2s_start(d->rate->hz);
|
||||
whal_set_input(d->input);
|
||||
whal_set_loopback(d->loopback == WP_LOOP_FIRMWARE);
|
||||
// the codec's clocks: frames arriving within a few ms
|
||||
uint32_t f0 = whal_frames();
|
||||
d->clock = false;
|
||||
for (uint32_t t = 0; t < CLOCK_START_MS && !d->clock; t++) {
|
||||
whal_sleep_ms(1);
|
||||
d->clock = whal_frames() != f0;
|
||||
}
|
||||
d->t_clock = whal_millis();
|
||||
d->frames_seen = whal_frames();
|
||||
return codec_up(d);
|
||||
}
|
||||
|
||||
uint8_t wb_dev_state(const wb_dev_t *d) {
|
||||
uint8_t s = 0;
|
||||
if (codec_up(d)) s |= WP_ST_CODEC;
|
||||
if (carrier(d)) s |= WP_ST_CARRIER;
|
||||
if (d->loopback != WP_LOOP_OFF) s |= WP_ST_LOOPBACK;
|
||||
if (whal_streaming()) s |= WP_ST_STREAMING;
|
||||
return s;
|
||||
}
|
||||
|
||||
// The codec's sticky flags read (which clears them and its INT1), and kept
|
||||
// for the next event and the next status.
|
||||
static void read_sticky(wb_dev_t *d) {
|
||||
uint8_t v;
|
||||
if (d->result != WB_CODEC_OK) return;
|
||||
if (wb_codec_read(0, AIC0_STICKY1, &v)) {
|
||||
v &= AIC0_STICKY1_MASK;
|
||||
d->ovf |= v;
|
||||
d->ovf_status |= v;
|
||||
}
|
||||
if (wb_codec_read(0, AIC0_STICKY2, &v)) {
|
||||
v &= AIC0_STICKY2_OC;
|
||||
d->oc |= v;
|
||||
d->oc_status |= v;
|
||||
}
|
||||
}
|
||||
|
||||
// A page 0 register for a status reply: its value, 0 if it couldn't be read.
|
||||
static uint8_t reg0_or_0(const wb_dev_t *d, uint8_t reg) {
|
||||
uint8_t v = 0;
|
||||
if (d->result == WB_CODEC_NO_ANSWER || !wb_codec_read(0, reg, &v)) v = 0;
|
||||
return v;
|
||||
}
|
||||
|
||||
// Copy a reply into buf, cut to max bytes; its length.
|
||||
static int reply(uint8_t *buf, uint16_t max, const void *src, uint16_t n) {
|
||||
if (n > max) n = max;
|
||||
memcpy(buf, src, n);
|
||||
return n;
|
||||
}
|
||||
|
||||
int wb_dev_vendor_in(wb_dev_t *d, uint8_t req, uint16_t value, uint16_t index, uint8_t *buf, uint16_t max) {
|
||||
switch (req) {
|
||||
case WP_REQ_GET_INFO: {
|
||||
wp_info_t i = {WP_VERSION, PS_FW_MAJOR, PS_FW_MINOR, PS_FW_PATCH, d->rate->hz, WP_MAX_SAMPLES,
|
||||
wb_dev_state(d), WP_BOARD, d->input, d->loopback, d->tx_gain, d->rx_gain};
|
||||
return reply(buf, max, &i, sizeof i);
|
||||
}
|
||||
case WP_REQ_GET_STATUS: {
|
||||
wp_status_t s;
|
||||
memset(&s, 0, sizeof s);
|
||||
uint16_t peak[3];
|
||||
whal_peaks(peak);
|
||||
s.peak_hybrid = peak[0];
|
||||
s.peak_line = peak[1];
|
||||
s.peak_tx = peak[2];
|
||||
read_sticky(d);
|
||||
s.ovf = d->ovf_status;
|
||||
s.oc = d->oc_status;
|
||||
d->ovf_status = d->oc_status = 0;
|
||||
s.adc_flags = reg0_or_0(d, AIC0_ADC_FLAGS);
|
||||
s.dac_flags = reg0_or_0(d, AIC0_DAC_FLAGS);
|
||||
s.state = wb_dev_state(d);
|
||||
uint16_t under, over;
|
||||
uint32_t in, out;
|
||||
whal_counters(&under, &over, &in, &out);
|
||||
s.underruns = under;
|
||||
s.overruns = over;
|
||||
s.samples_in = in;
|
||||
s.samples_out = out;
|
||||
s.resyncs = d->resyncs;
|
||||
return reply(buf, max, &s, sizeof s);
|
||||
}
|
||||
case WP_REQ_CODEC_READ: {
|
||||
uint8_t v;
|
||||
if (value > 127 || max < 1 || !wb_codec_read((uint8_t)(index >> 8), (uint8_t)value, &v)) return -1;
|
||||
buf[0] = v;
|
||||
return 1;
|
||||
}
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
bool wb_dev_vendor_out(wb_dev_t *d, uint8_t req, uint16_t value, uint16_t index) {
|
||||
switch (req) {
|
||||
case WP_REQ_SET_RATE: {
|
||||
const wb_rate_t *r = wb_rate(1000u * value);
|
||||
if (!r) return false;
|
||||
d->rate = r;
|
||||
wb_dev_start(d);
|
||||
return true;
|
||||
}
|
||||
case WP_REQ_SET_CARRIER:
|
||||
if (value > WP_CARRIER_AUTO) return false;
|
||||
d->carrier = (uint8_t)value;
|
||||
return true;
|
||||
case WP_REQ_SET_TX_GAIN: {
|
||||
int16_t g = (int16_t)value;
|
||||
if (g < -127 || g > 48) return false;
|
||||
d->tx_gain = (int8_t)g;
|
||||
return d->result == WB_CODEC_NO_ANSWER || wb_codec_tx_gain(g);
|
||||
}
|
||||
case WP_REQ_SET_RX_GAIN:
|
||||
if (value > AIC1_PGA_MAX) return false;
|
||||
d->rx_gain = (uint8_t)value;
|
||||
return d->result == WB_CODEC_NO_ANSWER || wb_codec_rx_gain((int16_t)value);
|
||||
case WP_REQ_SET_INPUT:
|
||||
if (value > WP_INPUT_LINE) return false;
|
||||
d->input = (uint8_t)value;
|
||||
whal_set_input(d->input);
|
||||
return true;
|
||||
case WP_REQ_SET_LOOPBACK:
|
||||
if (value > WP_LOOP_CODEC) return false;
|
||||
d->loopback = (uint8_t)value;
|
||||
whal_set_loopback(value == WP_LOOP_FIRMWARE);
|
||||
return d->result == WB_CODEC_NO_ANSWER || wb_codec_loop(value == WP_LOOP_CODEC);
|
||||
case WP_REQ_CODEC_WRITE: {
|
||||
uint8_t reg = (uint8_t)(value >> 8);
|
||||
if (reg == AIC_PAGE || reg > 127) return false; // the page is the firmware's to select
|
||||
return wb_codec_write((uint8_t)(index >> 8), reg, (uint8_t)value);
|
||||
}
|
||||
case WP_REQ_RESET:
|
||||
wb_dev_start(d);
|
||||
return true;
|
||||
case WP_REQ_REBOOT_UF2:
|
||||
whal_reboot_uf2();
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void wb_dev_poll(wb_dev_t *d) {
|
||||
uint32_t now = whal_millis();
|
||||
if (whal_i2s_lost()) d->resyncs++;
|
||||
if ((uint32_t)(now - d->t_clock) >= CLOCK_CHECK_MS) {
|
||||
uint32_t f = whal_frames();
|
||||
d->clock = f != d->frames_seen;
|
||||
d->frames_seen = f;
|
||||
d->t_clock = now;
|
||||
}
|
||||
if (d->result == WB_CODEC_OK && whal_codec_int()) read_sticky(d);
|
||||
uint32_t tx = whal_tx_active();
|
||||
if (tx != d->tx_seen) {
|
||||
d->tx_seen = tx;
|
||||
d->t_data = now;
|
||||
d->data = true;
|
||||
} else if (d->data && (uint32_t)(now - d->t_data) >= DATA_MS) {
|
||||
d->data = false;
|
||||
}
|
||||
whal_leds(carrier(d), d->data);
|
||||
}
|
||||
|
||||
bool wb_dev_event(wb_dev_t *d, wp_event_t *e) {
|
||||
uint16_t under, over;
|
||||
uint32_t in, out;
|
||||
whal_counters(&under, &over, &in, &out);
|
||||
memset(e, 0, sizeof *e);
|
||||
e->state = wb_dev_state(d);
|
||||
e->ovf = d->ovf;
|
||||
e->oc = d->oc;
|
||||
e->underruns = under;
|
||||
e->overruns = over;
|
||||
if (d->sent_valid && e->state == d->sent.state && e->ovf == 0 && e->oc == 0 &&
|
||||
e->underruns == d->sent.underruns && e->overruns == d->sent.overruns)
|
||||
return false;
|
||||
e->seq = (uint8_t)(d->sent.seq + 1);
|
||||
d->sent = *e;
|
||||
d->sent_valid = true;
|
||||
d->ovf = d->oc = 0;
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 Neo-Desktop
|
||||
*/
|
||||
// The WB stick's device logic (portable): the vendor requests of
|
||||
// wb_proto.h, the codec's bring-up at the host's rate and settings, its
|
||||
// flags and clocks watched, the LEDs, and the events for the host. The
|
||||
// hardware is behind wb_hal.h.
|
||||
#pragma once
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "wb_codec.h"
|
||||
#include "wb_proto.h"
|
||||
|
||||
typedef struct {
|
||||
const wb_rate_t *rate;
|
||||
wb_codec_result_t result; // the last bring-up's
|
||||
bool clock; // the codec's frames arrive
|
||||
uint32_t t_clock; // when they were last checked (ms)
|
||||
uint32_t frames_seen; // ... and the count then
|
||||
uint8_t carrier; // WP_CARRIER_*
|
||||
uint8_t input; // WP_INPUT_*
|
||||
uint8_t loopback; // WP_LOOP_*
|
||||
int8_t tx_gain; // 0.5 dB steps
|
||||
uint8_t rx_gain; // 0.5 dB steps
|
||||
uint8_t ovf, oc; // the codec's sticky flags not yet reported in an event
|
||||
uint8_t ovf_status, oc_status; // ... nor in a status
|
||||
uint16_t resyncs; // the I2S slave lost its place
|
||||
uint32_t tx_seen; // whal_tx_active() at the last poll
|
||||
uint32_t t_data; // when the host's samples last weren't silent (ms)
|
||||
bool data; // ... within DATA_MS
|
||||
wp_event_t sent; // the last event sent
|
||||
bool sent_valid;
|
||||
} wb_dev_t;
|
||||
|
||||
// Defaults (the hybrid to the host, no loopback, 0 dB both ways, LINK while
|
||||
// streaming), then wb_dev_start() at rate (Hz; 96000 if it isn't one).
|
||||
bool wb_dev_init(wb_dev_t *d, uint32_t rate);
|
||||
// The codec brought up again at d's rate and settings, and the I2S slave
|
||||
// restarted on its clocks. False if the codec didn't come up or its clocks
|
||||
// don't run; the requests work either way.
|
||||
bool wb_dev_start(wb_dev_t *d);
|
||||
// Vendor IN request: the reply in buf (at most max bytes); its length, or -1 to stall.
|
||||
int wb_dev_vendor_in(wb_dev_t *d, uint8_t req, uint16_t value, uint16_t index, uint8_t *buf, uint16_t max);
|
||||
// Vendor OUT request (no data stage); false to stall.
|
||||
bool wb_dev_vendor_out(wb_dev_t *d, uint8_t req, uint16_t value, uint16_t index);
|
||||
// From the main loop: the codec's interrupt (its sticky flags), the I2S
|
||||
// slave's place and the clocks, the LEDs.
|
||||
void wb_dev_poll(wb_dev_t *d);
|
||||
// The state now as an event; true if it differs from the last one sent
|
||||
// (then it counts as sent: its seq is the next one).
|
||||
bool wb_dev_event(wb_dev_t *d, wp_event_t *e);
|
||||
// The WP_ST_* bits now.
|
||||
uint8_t wb_dev_state(const wb_dev_t *d);
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 Neo-Desktop
|
||||
*/
|
||||
// What wb_codec.c and wb_dev.c need from the WB stick's hardware.
|
||||
// Implemented for the RP2040 in src/wb/codec.c and faked for the host tests
|
||||
// in test/test_wb.c.
|
||||
#pragma once
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// Milliseconds since boot (wraps; callers compare differences).
|
||||
uint32_t whal_millis(void);
|
||||
// Wait ms milliseconds (the watchdog kept fed).
|
||||
void whal_sleep_ms(uint32_t ms);
|
||||
// The codec's RESET pin low for a while, then high: a hardware reset with
|
||||
// MCLK (12 MHz) running. Its registers can be written 1 ms later.
|
||||
void whal_codec_reset(void);
|
||||
// A register of the codec's, on the page selected last (I2C); false if the
|
||||
// codec didn't answer.
|
||||
bool whal_codec_write(uint8_t reg, uint8_t v);
|
||||
// Read a register on the page selected last; false if the codec didn't answer.
|
||||
bool whal_codec_read(uint8_t reg, uint8_t *v);
|
||||
// The codec's INT1 (on MFP5) is asserted: a sticky flag wants reading.
|
||||
bool whal_codec_int(void);
|
||||
// Start the I2S slave (PIO and DMA) on the codec's clocks, the streams'
|
||||
// head start set for the rate; the streams stay open if they were.
|
||||
void whal_i2s_start(uint32_t rate);
|
||||
// Stop the I2S slave (the codec is about to be reset or reclocked).
|
||||
void whal_i2s_stop(void);
|
||||
// The I2S slave lost its place in the frames since the last call (it has
|
||||
// been started again).
|
||||
bool whal_i2s_lost(void);
|
||||
// Frames received since boot (wraps): the codec's clocks run while it rises.
|
||||
uint32_t whal_frames(void);
|
||||
// Whether the host has the sample stream open (USB alternate setting 1).
|
||||
bool whal_streaming(void);
|
||||
// The sample streams' counters (sstream_t).
|
||||
void whal_counters(uint16_t *underruns, uint16_t *overruns, uint32_t *in, uint32_t *out);
|
||||
// The largest |sample| of the left ADC, the right ADC and the host's samples
|
||||
// since the last call (wb_audio_t.peak); then cleared.
|
||||
void whal_peaks(uint16_t peak[3]);
|
||||
// Non-silent samples of the host's played so far (wraps).
|
||||
uint32_t whal_tx_active(void);
|
||||
// Which ADC channel goes to the host (WP_INPUT_*).
|
||||
void whal_set_input(uint8_t input);
|
||||
// Firmware loopback: the host's samples come back to it instead of the line's.
|
||||
void whal_set_loopback(bool on);
|
||||
// The status LEDs.
|
||||
void whal_leds(bool link, bool data);
|
||||
// Into the UF2 loader (doesn't return on the board).
|
||||
void whal_reboot_uf2(void);
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 Neo-Desktop
|
||||
*/
|
||||
// The wideband stick's USB protocol (SPEC-WB.md §5, firmware/PROTOCOL.md
|
||||
// "WB stick"): what the host's driver (D-Modem's dmodemd) and the firmware
|
||||
// share. Shaped as the Line stick's (line_proto.h): the same interface
|
||||
// layout and endpoints, other requests. Little-endian, packed.
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
#define WP_VERSION 1
|
||||
#define WP_BOARD 4 // wp_info_t.board: the WB stick (board_config.h PS_BOARD_ID)
|
||||
|
||||
// Interface 0: vendor class, this subclass and protocol. Alternate setting
|
||||
// 0 has no endpoints (no bandwidth); alternate setting 1 has the three below,
|
||||
// and selecting it starts the samples both ways.
|
||||
#define WP_ITF_SUBCLASS 0x57 // 'W'
|
||||
#define WP_ITF_PROTOCOL 0x01
|
||||
#define WP_EP_OUT 0x01 // isochronous, 1 ms: the modem's samples to the line
|
||||
#define WP_EP_IN 0x82 // isochronous, 1 ms: the line's samples
|
||||
#define WP_EP_EVENT 0x83 // interrupt, 4 ms: wp_event_t when something changes
|
||||
#define WP_MAX_SAMPLES 240 // per packet: 192 kHz (192 a ms) with room to catch up
|
||||
#define WP_PACKET_BYTES (2 * WP_MAX_SAMPLES)
|
||||
|
||||
// Vendor requests (recipient: the interface or the device; wIndex's low byte
|
||||
// the interface, 0)
|
||||
enum {
|
||||
WP_REQ_GET_INFO = 0x01, // IN: wp_info_t
|
||||
WP_REQ_GET_STATUS = 0x02, // IN: wp_status_t (the peaks and the codec's flags since the last one)
|
||||
WP_REQ_SET_RATE = 0x10, // OUT: wValue = the sample rate in kHz: 48, 96 or 192
|
||||
WP_REQ_SET_CARRIER = 0x11, // OUT: wValue = WP_CARRIER_* (the LINK LED)
|
||||
WP_REQ_SET_TX_GAIN = 0x12, // OUT: wValue = int16, 0.5 dB steps, -127..48: the DAC's volume
|
||||
WP_REQ_SET_RX_GAIN = 0x13, // OUT: wValue = 0..95, 0.5 dB steps: the hybrid's MicPGA
|
||||
WP_REQ_SET_INPUT = 0x14, // OUT: wValue = WP_INPUT_*: which ADC channel the host gets
|
||||
WP_REQ_SET_LOOPBACK = 0x15, // OUT: wValue = WP_LOOP_*
|
||||
WP_REQ_CODEC_READ = 0x20, // IN: wValue = register (1..127), wIndex = page << 8; 1 byte
|
||||
WP_REQ_CODEC_WRITE = 0x21, // OUT: wValue = register << 8 | value, wIndex = page << 8
|
||||
WP_REQ_RESET = 0x30, // OUT: the codec reset and brought up again (rate and settings kept)
|
||||
WP_REQ_REBOOT_UF2 = 0x31, // OUT: into the RP2040's UF2 loader
|
||||
WP_REQ_MS_OS_20 = 0x4D, // IN, wIndex 7: the Microsoft OS 2.0 descriptor set (Windows)
|
||||
};
|
||||
|
||||
enum { WP_CARRIER_OFF, WP_CARRIER_ON, WP_CARRIER_AUTO }; // AUTO (the default): lit while streaming
|
||||
enum { WP_INPUT_HYBRID, WP_INPUT_LINE }; // left ADC (default), right ADC
|
||||
enum { WP_LOOP_OFF, WP_LOOP_FIRMWARE, WP_LOOP_CODEC };
|
||||
|
||||
// State bits (wp_info_t.state, wp_status_t.state, wp_event_t.state)
|
||||
#define WP_ST_CODEC 0x01 // the codec came up and its clocks run
|
||||
#define WP_ST_CARRIER 0x02 // the LINK LED is lit
|
||||
#define WP_ST_LOOPBACK 0x04 // a loopback is on
|
||||
#define WP_ST_STREAMING 0x08 // alternate setting 1
|
||||
|
||||
typedef struct __attribute__((packed)) {
|
||||
uint8_t version; // WP_VERSION
|
||||
uint8_t fw_major, fw_minor, fw_patch;
|
||||
uint32_t rate; // samples a second
|
||||
uint16_t max_samples; // WP_MAX_SAMPLES
|
||||
uint8_t state; // WP_ST_*
|
||||
uint8_t board; // WP_BOARD
|
||||
uint8_t input; // WP_INPUT_*
|
||||
uint8_t loopback; // WP_LOOP_*
|
||||
int8_t tx_gain; // the DAC's volume, 0.5 dB steps
|
||||
uint8_t rx_gain; // the hybrid MicPGA's gain, 0.5 dB steps
|
||||
} wp_info_t;
|
||||
|
||||
typedef struct __attribute__((packed)) {
|
||||
uint16_t peak_hybrid; // the largest |sample| since the last status: left ADC (32768 = full scale)
|
||||
uint16_t peak_line; // ... right ADC
|
||||
uint16_t peak_tx; // ... the host's samples played
|
||||
uint8_t ovf; // codec page 0 register 42 bits since the last status: DAC/ADC overflows
|
||||
uint8_t oc; // register 44's over-current bits (HPL 0x80, HPR 0x40) since the last status
|
||||
uint8_t adc_flags; // register 36 now: PGAs at gain, ADCs powered
|
||||
uint8_t dac_flags; // register 37 now: DACs and drivers powered
|
||||
uint8_t state; // WP_ST_*
|
||||
uint8_t reserved;
|
||||
uint16_t underruns; // OUT samples the line wanted and the host hadn't sent
|
||||
uint16_t overruns; // samples dropped: OUT too far ahead, or IN not taken
|
||||
uint32_t samples_in; // the line's samples handed to the host
|
||||
uint32_t samples_out; // the host's samples played
|
||||
uint16_t resyncs; // the I2S slave lost its place and started again
|
||||
uint16_t reserved2;
|
||||
} wp_status_t;
|
||||
|
||||
typedef struct __attribute__((packed)) {
|
||||
uint8_t state; // WP_ST_*
|
||||
uint8_t seq; // +1 each event
|
||||
uint8_t ovf; // register 42 bits since the last event
|
||||
uint8_t oc; // register 44's over-current bits since the last event
|
||||
uint16_t underruns, overruns;
|
||||
} wp_event_t;
|
||||
|
||||
_Static_assert(sizeof(wp_info_t) == 16, "wp_info_t");
|
||||
_Static_assert(sizeof(wp_status_t) == 28, "wp_status_t");
|
||||
_Static_assert(sizeof(wp_event_t) == 8, "wp_event_t");
|
||||
_Static_assert(WP_PACKET_BYTES <= 1023, "a full-speed isochronous packet");
|
||||
|
||||
// Samples a millisecond at a rate (Hz): the IN packets' usual size.
|
||||
static inline unsigned wp_samples_per_ms(unsigned hz) { return hz / 1000u; }
|
||||
@@ -8,19 +8,24 @@
|
||||
#include "generated/board_pins_w.h"
|
||||
#elif defined(PS_BOARD_LINE)
|
||||
#include "generated/board_pins_line.h"
|
||||
#elif defined(PS_BOARD_WB)
|
||||
#include "generated/board_pins_wb.h"
|
||||
#else
|
||||
#include "generated/board_pins.h"
|
||||
#endif
|
||||
|
||||
// USB IDs. 1209:0001 and 1209:0002 are pid.codes' shared *test* PIDs: fine
|
||||
// on the bench, not for boards given to other people. Apply for a PID
|
||||
// (Raspberry Pi's program under VID 2E8A, or pid.codes) and set it here
|
||||
// before release. The line stick has its own, being another kind of device
|
||||
// (no CDC port; its driver finds it by VID:PID, as dmodemd does the SmartUSB56).
|
||||
// USB IDs. 1209:0001, 1209:0002 and 1209:0003 are pid.codes' shared *test*
|
||||
// PIDs: fine on the bench, not for boards given to other people. Apply for a
|
||||
// PID (Raspberry Pi's program under VID 2E8A, or pid.codes) and set it here
|
||||
// before release. The line stick and the wideband stick each have their own,
|
||||
// being other kinds of device (no CDC port; their driver finds them by
|
||||
// VID:PID, as dmodemd does the SmartUSB56).
|
||||
#ifndef PS_USB_VID
|
||||
#define PS_USB_VID 0x1209
|
||||
#if defined(PS_BOARD_LINE)
|
||||
#define PS_USB_PID 0x0002
|
||||
#elif defined(PS_BOARD_WB)
|
||||
#define PS_USB_PID 0x0003
|
||||
#else
|
||||
#define PS_USB_PID 0x0001
|
||||
#endif
|
||||
@@ -44,6 +49,11 @@
|
||||
// (src/line/daa.c drives them); no RS-232.
|
||||
#define PS_BOARD_ID 3
|
||||
#define PS_PRODUCT "pico-serial Line"
|
||||
#elif defined(PS_BOARD_WB)
|
||||
// pico-serial WB, the wideband bare-pair stick (SPEC-WB.md): two LEDs, LINK
|
||||
// and DATA (src/wb/codec.c drives them); no RS-232.
|
||||
#define PS_BOARD_ID 4
|
||||
#define PS_PRODUCT "pico-serial WB"
|
||||
#elif defined(PS_BOARD_W)
|
||||
// pico-serial W (SPEC-W.md). LEDs fitted. PWR/BT shows the Bluetooth state
|
||||
// (bt.c); RD and SD are on the RM2's own GPIOs (WLGPIO_LED_*), driven
|
||||
|
||||
@@ -11,8 +11,8 @@
|
||||
#define CFG_TUSB_OS OPT_OS_PICO
|
||||
#define CFG_TUD_ENDPOINT0_SIZE 64
|
||||
|
||||
#if defined(PS_BOARD_LINE)
|
||||
#define CFG_TUD_CDC 0 // the line stick: one vendor interface (src/stick/usb_stick.c)
|
||||
#if defined(PS_BOARD_LINE) || defined(PS_BOARD_WB)
|
||||
#define CFG_TUD_CDC 0 // the sticks: one vendor interface (src/stick/usb_stick.c)
|
||||
#else
|
||||
#define CFG_TUD_CDC 1
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 Neo-Desktop
|
||||
*/
|
||||
// The WB stick's codec on the RP2040 (SPEC-WB.md §3-4): the TLV320AIC3204
|
||||
// as the I2S master on the RP2040's 12 MHz crystal clock, the RP2040 its
|
||||
// slave in PIO, the frames moved by DMA in blocks and handed to wb_audio.c
|
||||
// from the DMA's interrupt; its registers over I2C1. See codec.h and wb_hal.h.
|
||||
#include <string.h>
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/clocks.h"
|
||||
#include "hardware/dma.h"
|
||||
#include "hardware/i2c.h"
|
||||
#include "hardware/irq.h"
|
||||
#include "hardware/pio.h"
|
||||
#include "hardware/sync.h"
|
||||
#include "hardware/watchdog.h"
|
||||
#include "board_config.h"
|
||||
#include "aic3204.h"
|
||||
#include "codec.h"
|
||||
#include "wb_hal.h"
|
||||
#include "i2s.pio.h"
|
||||
|
||||
static_assert(PIN_CODEC_BCLK == 2 && PIN_CODEC_WCLK == 3, "i2s.pio waits on GPIO 2 (BCLK) and 3 (WCLK)");
|
||||
static_assert(PIN_CODEC_MCLK == 21, "MCLK is clk_gpout0, on GPIO21");
|
||||
static_assert(PIN_CODEC_SDA == 6 && PIN_CODEC_SCL == 7, "I2C1 on GPIO 6 (SDA) and 7 (SCL)");
|
||||
|
||||
#define I2S_PIO pio0
|
||||
#define I2S_SM 0
|
||||
#define I2C_PORT i2c1
|
||||
#define I2C_HZ 400000 // fast mode (datasheet 7.17)
|
||||
#define I2C_TIMEOUT_US 2000
|
||||
#define BLOCK 32 // frames a DMA block: 1/3 ms at 96 kHz
|
||||
#define PREFILL_MS 3 // the modem's samples ahead before playing
|
||||
|
||||
wb_audio_t g_audio;
|
||||
static uint s_offset;
|
||||
static int s_rx[2], s_tx[2]; // DMA channels: two each way, each chained to the other
|
||||
static uint32_t s_rx_buf[2][2 * BLOCK], s_tx_buf[2][2 * BLOCK];
|
||||
static bool s_running;
|
||||
|
||||
// The DMA's interrupt: a received block to wb_audio_rx() and its channel
|
||||
// pointed at its buffer again; a sent block's buffer refilled from
|
||||
// wb_audio_tx(). Each channel has the other's whole block to wait for this.
|
||||
static void __not_in_flash_func(dma_irq)(void) {
|
||||
uint32_t pending = dma_hw->ints0;
|
||||
for (int k = 0; k < 2; k++) {
|
||||
if (pending & (1u << s_rx[k])) {
|
||||
dma_hw->ints0 = 1u << s_rx[k];
|
||||
wb_audio_rx(&g_audio, s_rx_buf[k], BLOCK);
|
||||
dma_channel_set_write_addr((uint)s_rx[k], s_rx_buf[k], false);
|
||||
}
|
||||
if (pending & (1u << s_tx[k])) {
|
||||
dma_hw->ints0 = 1u << s_tx[k];
|
||||
wb_audio_tx(&g_audio, s_tx_buf[k], BLOCK);
|
||||
dma_channel_set_read_addr((uint)s_tx[k], s_tx_buf[k], false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// One output pin at a level.
|
||||
static void out_pin(uint pin, bool level) {
|
||||
gpio_init(pin);
|
||||
gpio_put(pin, level);
|
||||
gpio_set_dir(pin, GPIO_OUT);
|
||||
}
|
||||
|
||||
void codec_hw_init(void) {
|
||||
out_pin(PIN_CODEC_RESET_N, 0); // in reset until whal_codec_reset (pulled down too)
|
||||
out_pin(PIN_LED_LINK, 0);
|
||||
out_pin(PIN_LED_DATA, 0);
|
||||
gpio_init(PIN_CODEC_MFP5); // INT1 once the codec is set up; until then nothing drives it
|
||||
gpio_set_dir(PIN_CODEC_MFP5, GPIO_IN);
|
||||
gpio_pull_down(PIN_CODEC_MFP5);
|
||||
gpio_init(PIN_CODEC_BCLK);
|
||||
gpio_set_dir(PIN_CODEC_BCLK, GPIO_IN);
|
||||
gpio_init(PIN_CODEC_WCLK);
|
||||
gpio_set_dir(PIN_CODEC_WCLK, GPIO_IN);
|
||||
gpio_init(PIN_CODEC_DOUT);
|
||||
gpio_set_dir(PIN_CODEC_DOUT, GPIO_IN);
|
||||
// MCLK: the crystal's 12 MHz, undivided (no fractional divider: a clean clock)
|
||||
clock_gpio_init_int_frac8(PIN_CODEC_MCLK, CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_XOSC_CLKSRC, 1, 0);
|
||||
// I2C1 (4.7 kOhm pull-ups on the board)
|
||||
i2c_init(I2C_PORT, I2C_HZ);
|
||||
gpio_set_function(PIN_CODEC_SDA, GPIO_FUNC_I2C);
|
||||
gpio_set_function(PIN_CODEC_SCL, GPIO_FUNC_I2C);
|
||||
// the I2S slave and its DMA
|
||||
s_offset = pio_add_program(I2S_PIO, &i2s_slave_program);
|
||||
pio_sm_claim(I2S_PIO, I2S_SM);
|
||||
for (int k = 0; k < 2; k++) {
|
||||
s_rx[k] = dma_claim_unused_channel(true);
|
||||
s_tx[k] = dma_claim_unused_channel(true);
|
||||
}
|
||||
irq_set_exclusive_handler(DMA_IRQ_0, dma_irq);
|
||||
irq_set_priority(DMA_IRQ_0, 0); // highest: a block must be back before its turn comes again
|
||||
wb_audio_init(&g_audio, 0);
|
||||
}
|
||||
|
||||
// One DMA channel of a pair: words between the PIO's FIFO and buf, a block
|
||||
// at a time, chained to its partner, an interrupt at each block's end.
|
||||
static void dma_setup(int ch, int partner, bool rx, uint32_t *buf, bool start) {
|
||||
dma_channel_config c = dma_channel_get_default_config((uint)ch);
|
||||
channel_config_set_transfer_data_size(&c, DMA_SIZE_32);
|
||||
channel_config_set_read_increment(&c, !rx);
|
||||
channel_config_set_write_increment(&c, rx);
|
||||
channel_config_set_dreq(&c, pio_get_dreq(I2S_PIO, I2S_SM, !rx));
|
||||
channel_config_set_chain_to(&c, (uint)partner);
|
||||
if (rx)
|
||||
dma_channel_configure((uint)ch, &c, buf, &I2S_PIO->rxf[I2S_SM], 2 * BLOCK, start);
|
||||
else
|
||||
dma_channel_configure((uint)ch, &c, &I2S_PIO->txf[I2S_SM], buf, 2 * BLOCK, start);
|
||||
dma_channel_set_irq0_enabled((uint)ch, true);
|
||||
}
|
||||
|
||||
// The PIO program from its entry (it finds a frame's start) and the four
|
||||
// DMA channels, the samples' state as it is. Silence until wb_audio_tx's
|
||||
// first block.
|
||||
static void i2s_go(void) {
|
||||
memset(s_tx_buf, 0, sizeof s_tx_buf);
|
||||
i2s_slave_program_init(I2S_PIO, I2S_SM, s_offset, PIN_CODEC_DOUT, PIN_CODEC_DIN, PIN_CODEC_WCLK);
|
||||
pio_sm_clear_fifos(I2S_PIO, I2S_SM);
|
||||
pio_interrupt_clear(I2S_PIO, 0);
|
||||
for (int k = 0; k < 2; k++) {
|
||||
dma_setup(s_rx[k], s_rx[k ^ 1], true, s_rx_buf[k], k == 0);
|
||||
dma_setup(s_tx[k], s_tx[k ^ 1], false, s_tx_buf[k], k == 0);
|
||||
}
|
||||
irq_set_enabled(DMA_IRQ_0, true);
|
||||
pio_sm_set_enabled(I2S_PIO, I2S_SM, true);
|
||||
s_running = true;
|
||||
}
|
||||
|
||||
// The PIO stopped and the DMA channels aborted. Each channel is disabled
|
||||
// before any is aborted, so none can trigger its partner (RP2040-E13).
|
||||
static void i2s_halt(void) {
|
||||
if (!s_running) return;
|
||||
pio_sm_set_enabled(I2S_PIO, I2S_SM, false);
|
||||
irq_set_enabled(DMA_IRQ_0, false);
|
||||
uint32_t mask = 0;
|
||||
for (int k = 0; k < 2; k++) {
|
||||
int ch[2] = {s_rx[k], s_tx[k]};
|
||||
for (int i = 0; i < 2; i++) {
|
||||
dma_channel_set_irq0_enabled((uint)ch[i], false);
|
||||
hw_clear_bits(&dma_hw->ch[ch[i]].al1_ctrl, DMA_CH0_CTRL_TRIG_EN_BITS);
|
||||
mask |= 1u << ch[i];
|
||||
}
|
||||
}
|
||||
for (int k = 0; k < 2; k++) {
|
||||
dma_channel_abort((uint)s_rx[k]);
|
||||
dma_channel_abort((uint)s_tx[k]);
|
||||
}
|
||||
dma_hw->ints0 = mask;
|
||||
s_running = false;
|
||||
}
|
||||
|
||||
void whal_i2s_start(uint32_t rate) {
|
||||
i2s_halt();
|
||||
bool streaming = g_audio.s.streaming;
|
||||
wb_audio_init(&g_audio, PREFILL_MS * rate / 1000);
|
||||
g_audio.s.streaming = streaming;
|
||||
i2s_go();
|
||||
}
|
||||
|
||||
void whal_i2s_stop(void) { i2s_halt(); }
|
||||
|
||||
bool whal_i2s_lost(void) {
|
||||
if (!s_running || !pio_interrupt_get(I2S_PIO, 0)) return false;
|
||||
i2s_halt();
|
||||
i2s_go(); // the streams carry on; only the frames start over
|
||||
return true;
|
||||
}
|
||||
|
||||
void codec_stream(bool on) {
|
||||
uint32_t irq = save_and_disable_interrupts();
|
||||
sstream_set(&g_audio.s, on);
|
||||
restore_interrupts(irq);
|
||||
}
|
||||
|
||||
void codec_from_host(const int16_t *s, uint32_t n) {
|
||||
// the overrun count is shared with the interrupt's: count with it held off
|
||||
uint32_t irq = save_and_disable_interrupts();
|
||||
sstream_from_host(&g_audio.s, s, n);
|
||||
restore_interrupts(irq);
|
||||
}
|
||||
|
||||
uint32_t whal_millis(void) { return to_ms_since_boot(get_absolute_time()); }
|
||||
|
||||
void whal_sleep_ms(uint32_t ms) {
|
||||
while (ms--) {
|
||||
sleep_ms(1);
|
||||
watchdog_update();
|
||||
}
|
||||
}
|
||||
|
||||
void whal_codec_reset(void) {
|
||||
gpio_put(PIN_CODEC_RESET_N, 0);
|
||||
sleep_ms(1); // at least 10 ns (SLAA557 3.1)
|
||||
gpio_put(PIN_CODEC_RESET_N, 1);
|
||||
}
|
||||
|
||||
bool whal_codec_write(uint8_t reg, uint8_t v) {
|
||||
uint8_t b[2] = {reg, v};
|
||||
return i2c_write_timeout_us(I2C_PORT, AIC_I2C_ADDR, b, 2, false, I2C_TIMEOUT_US) == 2;
|
||||
}
|
||||
|
||||
bool whal_codec_read(uint8_t reg, uint8_t *v) {
|
||||
if (i2c_write_timeout_us(I2C_PORT, AIC_I2C_ADDR, ®, 1, true, I2C_TIMEOUT_US) != 1) return false;
|
||||
return i2c_read_timeout_us(I2C_PORT, AIC_I2C_ADDR, v, 1, false, I2C_TIMEOUT_US) == 1;
|
||||
}
|
||||
|
||||
bool whal_codec_int(void) { return gpio_get(PIN_CODEC_MFP5); }
|
||||
uint32_t whal_frames(void) { return g_audio.frames; }
|
||||
bool whal_streaming(void) { return g_audio.s.streaming; }
|
||||
uint32_t whal_tx_active(void) { return g_audio.tx_active; }
|
||||
void whal_set_input(uint8_t input) { g_audio.input = input; }
|
||||
void whal_set_loopback(bool on) { g_audio.loopback = on; }
|
||||
|
||||
void whal_counters(uint16_t *underruns, uint16_t *overruns, uint32_t *in, uint32_t *out) {
|
||||
*underruns = g_audio.s.underruns;
|
||||
*overruns = g_audio.s.overruns;
|
||||
*in = g_audio.s.samples_in;
|
||||
*out = g_audio.s.samples_out;
|
||||
}
|
||||
|
||||
void whal_peaks(uint16_t peak[3]) {
|
||||
uint32_t irq = save_and_disable_interrupts();
|
||||
wb_audio_peaks(&g_audio, peak);
|
||||
restore_interrupts(irq);
|
||||
}
|
||||
|
||||
void whal_leds(bool link, bool data) {
|
||||
gpio_put(PIN_LED_LINK, link);
|
||||
gpio_put(PIN_LED_DATA, data);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 Neo-Desktop
|
||||
*/
|
||||
// The WB stick's codec on the RP2040: its pins, MCLK, I2C, and its I2S
|
||||
// frames (PIO and DMA) with the samples through wb_audio.c. Implements
|
||||
// wb_hal.h.
|
||||
#pragma once
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "wb_audio.h"
|
||||
|
||||
// The samples (USB reads and writes their streams; the DMA interrupt runs them).
|
||||
extern wb_audio_t g_audio;
|
||||
|
||||
// Pins, LEDs, MCLK (the 12 MHz crystal on GPIO21), I2C1, the PIO program
|
||||
// and the DMA channels. Leaves the codec in reset. Call once at startup,
|
||||
// before wb_dev_init().
|
||||
void codec_hw_init(void);
|
||||
// The host opened (alternate setting 1) or closed the stream: the FIFOs emptied.
|
||||
void codec_stream(bool on);
|
||||
// The modem's samples from USB OUT, into the stream (overruns counted).
|
||||
void codec_from_host(const int16_t *s, uint32_t n);
|
||||
@@ -0,0 +1,69 @@
|
||||
; SPDX-License-Identifier: AGPL-3.0-only
|
||||
; Copyright (C) 2026 Neo-Desktop
|
||||
;
|
||||
; The RP2040 as the TLV320AIC3204's I2S slave, full duplex (SLAA557 2.6.3,
|
||||
; "I2S Mode"; datasheet 7.13, I2S timing in master mode). The codec makes
|
||||
; BCLK (64 a frame) and WCLK; WCLK is low for the left slot and changes one
|
||||
; BCLK before each slot's MSB. The codec changes DOUT and WCLK as BCLK
|
||||
; falls (at most 22 ns after) and samples DIN as BCLK rises (8 ns setup and
|
||||
; hold). So here: the codec's bit in as BCLK rises, ours out just after
|
||||
; (a whole BCLK of setup before the codec samples it), MSB first, 32 bits a
|
||||
; slot, left then right.
|
||||
;
|
||||
; BCLK and WCLK are GPIO 2 and 3 (wait gpio: absolute numbers; codec.c checks
|
||||
; them against the netlist's), and WCLK is the JMP pin too. IN base: DOUT;
|
||||
; OUT base: DIN. Autopush and autopull at 32 bits: the RX FIFO gets left,
|
||||
; right, left... and the TX FIFO is read the same way (DMA on both).
|
||||
;
|
||||
; In step, WCLK is high after each left slot's 32 bits and low after each
|
||||
; right slot's. If it isn't, the program raises IRQ 0 and stops; the
|
||||
; firmware starts it (and the DMA) again from the top.
|
||||
|
||||
.program i2s_slave
|
||||
lost:
|
||||
irq nowait 0 ; out of step: tell the firmware
|
||||
stop:
|
||||
jmp stop ; ... and wait for it to start over
|
||||
public entry:
|
||||
wait 1 gpio 3 ; WCLK high: a right slot
|
||||
wait 0 gpio 3 ; WCLK falls with BCLK: a frame starts
|
||||
wait 1 gpio 2 ; BCLK rises: I2S's one-bit delay (the right word's LSB)
|
||||
.wrap_target
|
||||
set y, 31
|
||||
lbit:
|
||||
out pins, 1 ; our bit, for the codec to take on the next rise
|
||||
wait 0 gpio 2
|
||||
wait 1 gpio 2 ; BCLK rises: the codec's bit is valid
|
||||
in pins, 1
|
||||
jmp y-- lbit
|
||||
jmp pin rslot ; WCLK high: the right slot, in step
|
||||
jmp lost
|
||||
rslot:
|
||||
set y, 31
|
||||
rbit:
|
||||
out pins, 1
|
||||
wait 0 gpio 2
|
||||
wait 1 gpio 2
|
||||
in pins, 1
|
||||
jmp y-- rbit
|
||||
jmp pin lost ; WCLK must be low again: the next frame's left slot
|
||||
.wrap
|
||||
|
||||
% c-sdk {
|
||||
// The program on sm, from its entry: DOUT in, DIN out, WCLK the JMP pin,
|
||||
// shifting MSB first with autopush and autopull at 32 bits, at the system
|
||||
// clock (the PIO follows BCLK; it only needs to be fast enough to see it).
|
||||
static inline void i2s_slave_program_init(PIO pio, uint sm, uint offset, uint pin_dout, uint pin_din,
|
||||
uint pin_wclk) {
|
||||
pio_sm_config c = i2s_slave_program_get_default_config(offset);
|
||||
sm_config_set_in_pins(&c, pin_dout);
|
||||
sm_config_set_out_pins(&c, pin_din, 1);
|
||||
sm_config_set_jmp_pin(&c, pin_wclk);
|
||||
sm_config_set_in_shift(&c, false, true, 32); // left: MSB first, autopush
|
||||
sm_config_set_out_shift(&c, false, true, 32); // left: MSB first, autopull
|
||||
pio_gpio_init(pio, pin_din);
|
||||
pio_sm_set_consecutive_pindirs(pio, sm, pin_din, 1, true);
|
||||
pio_sm_set_consecutive_pindirs(pio, sm, pin_dout, 1, false);
|
||||
pio_sm_init(pio, sm, offset + i2s_slave_offset_entry, &c);
|
||||
}
|
||||
%}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 Neo-Desktop
|
||||
*/
|
||||
// pico-serial WB firmware: the wideband bare-pair stick's USB front end for
|
||||
// the TLV320AIC3204 codec (SPEC-WB.md). The modem itself is on the host
|
||||
// (D-Modem).
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/watchdog.h"
|
||||
#include "codec.h"
|
||||
#include "usb_stick.h"
|
||||
#include "wb_stick.h"
|
||||
|
||||
// Brings up the codec at 96 kHz, then USB, then runs both from one loop.
|
||||
// The watchdog resets the stick if the loop stalls for a second (the codec's
|
||||
// RESET is pulled down: it goes quiet).
|
||||
int main(void) {
|
||||
codec_hw_init();
|
||||
wb_dev_init(&g_wb, 96000);
|
||||
usb_stick_init();
|
||||
watchdog_enable(1000, true);
|
||||
for (;;) {
|
||||
usb_stick_service();
|
||||
watchdog_update();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 Neo-Desktop
|
||||
*/
|
||||
// The wideband stick behind the sticks' USB device (stick.h): its device
|
||||
// logic (wb_dev.c) and the codec's sample streams (codec.c).
|
||||
#include "board_config.h"
|
||||
#include "codec.h"
|
||||
#include "stick.h"
|
||||
#include "usb_stick.h"
|
||||
#include "wb_hal.h"
|
||||
#include "wb_stick.h"
|
||||
|
||||
_Static_assert(PS_BOARD_ID == WP_BOARD, "wp_info_t.board is the board's ID");
|
||||
|
||||
wb_dev_t g_wb;
|
||||
|
||||
int stick_vendor_in(uint8_t req, uint16_t value, uint16_t index, uint8_t *buf, uint16_t max) {
|
||||
return wb_dev_vendor_in(&g_wb, req, value, index, buf, max);
|
||||
}
|
||||
|
||||
bool stick_vendor_out(uint8_t req, uint16_t value, uint16_t index) {
|
||||
return wb_dev_vendor_out(&g_wb, req, value, index);
|
||||
}
|
||||
|
||||
void stick_poll(void) { wb_dev_poll(&g_wb); }
|
||||
bool stick_event(stick_event_t *e) { return wb_dev_event(&g_wb, e); }
|
||||
void stick_event_again(void) { g_wb.sent_valid = false; }
|
||||
void stick_stream(bool on) { codec_stream(on); }
|
||||
void stick_from_host(const int16_t *s, uint32_t n) { codec_from_host(s, n); }
|
||||
uint32_t stick_to_host(int16_t *s, uint32_t max) { return sstream_to_host(&g_audio.s, s, max); }
|
||||
|
||||
// Nothing to let go of (no hook): the stream has stopped with the bus; the
|
||||
// LINK LED follows the host's next say, AUTO until then.
|
||||
void stick_unmounted(void) { g_wb.carrier = WP_CARRIER_AUTO; }
|
||||
|
||||
// wb_hal.h: into the UF2 loader, once this request's status stage has gone.
|
||||
void whal_reboot_uf2(void) { usb_stick_reboot_uf2(); }
|
||||
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 Neo-Desktop
|
||||
*/
|
||||
// The wideband stick's device state, behind the sticks' USB device
|
||||
// (stick.h, wb_stick.c).
|
||||
#pragma once
|
||||
#include "wb_dev.h"
|
||||
|
||||
// The device state the requests act on (main.c brings it up).
|
||||
extern wb_dev_t g_wb;
|
||||
@@ -0,0 +1 @@
|
||||
test_wb
|
||||
@@ -5,7 +5,7 @@ CFLAGS ?= -std=c11 -O1 -g -Wall -Wextra -Werror -Wconversion -Wno-sign-conversio
|
||||
CORE = ../src/core/core.c ../src/core/autobaud.c
|
||||
DEPS = $(CORE) $(wildcard ../src/core/*.h) fake_hal.h wave.h wave.c
|
||||
|
||||
all: test_core test_autobaud test_link test_line
|
||||
all: test_core test_autobaud test_link test_line test_wb
|
||||
|
||||
test_core: test_core.c fake_hal.c $(DEPS)
|
||||
$(CC) $(CFLAGS) -o $@ test_core.c fake_hal.c wave.c $(CORE) -lm
|
||||
@@ -20,12 +20,17 @@ LINE = ../src/core/daa_frame.c ../src/core/sstream.c ../src/core/line_dev.c
|
||||
test_line: test_line.c $(LINE) $(wildcard ../src/core/*.h)
|
||||
$(CC) $(CFLAGS) -o $@ test_line.c $(LINE)
|
||||
|
||||
WB = ../src/core/sstream.c ../src/core/wb_audio.c ../src/core/wb_codec.c ../src/core/wb_dev.c
|
||||
test_wb: test_wb.c $(WB) $(wildcard ../src/core/*.h)
|
||||
$(CC) $(CFLAGS) -DSFIFO_SIZE=4096u -o $@ test_wb.c $(WB)
|
||||
|
||||
.PHONY: all run clean
|
||||
run: all
|
||||
./test_core
|
||||
./test_autobaud
|
||||
./test_link
|
||||
./test_line
|
||||
./test_wb
|
||||
|
||||
clean:
|
||||
rm -f test_core test_autobaud test_link test_line
|
||||
rm -f test_core test_autobaud test_link test_line test_wb
|
||||
@@ -0,0 +1,738 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 Neo-Desktop
|
||||
*/
|
||||
// Host tests for the wideband stick's portable firmware: the codec's
|
||||
// bring-up (wb_codec.c) against a model of the TLV320AIC3204's registers,
|
||||
// its clocks re-derived from the registers and checked against TI SLAA557's
|
||||
// limits, the hybrid's routing against the board's netlist, the samples
|
||||
// between the I2S frames and the streams (wb_audio.c), and the device logic
|
||||
// and requests (wb_dev.c) on a fake board.
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "../src/core/aic3204.h"
|
||||
#include "../src/core/wb_audio.h"
|
||||
#include "../src/core/wb_codec.h"
|
||||
#include "../src/core/wb_dev.h"
|
||||
#include "../src/core/wb_hal.h"
|
||||
#include "../src/core/wb_proto.h"
|
||||
|
||||
static int failures, checks;
|
||||
#define CHECK(cond) do { checks++; if (!(cond)) { failures++; \
|
||||
printf(" FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); } } while (0)
|
||||
#define TEST(name) static void name(void)
|
||||
#define RUN(name) do { printf("%s\n", #name); name(); } while (0)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// A model of the codec's registers (pages 0 and 1, SLAA557 chapter 5): page
|
||||
// select in register 0, the software reset, the reset values the bring-up
|
||||
// must change, read-to-clear sticky flags, and the power-up flags following
|
||||
// what was powered (the headphone drivers' after hp_delay reads).
|
||||
// ---------------------------------------------------------------------------
|
||||
#define LOG_MAX 512
|
||||
typedef struct { uint8_t page, reg, v; uint32_t t; } wr_t;
|
||||
|
||||
static struct {
|
||||
uint8_t reg[2][128];
|
||||
uint8_t page;
|
||||
bool answers; // I2C: the codec acks
|
||||
bool hp_never; // the drivers never reach their gain
|
||||
int hp_delay, hp_reads; // reads of P1 R63 before its flags show
|
||||
wr_t log[LOG_MAX];
|
||||
int nlog;
|
||||
int bad_page_writes; // writes to a page other than 0 and 1
|
||||
uint32_t t_reset; // when RESET last rose (ms)
|
||||
int early; // accesses within 1 ms of a reset
|
||||
} aic;
|
||||
|
||||
// Reset values (SLAA557 chapter 5) of the registers the tests look at.
|
||||
static void aic_defaults(void) {
|
||||
memset(aic.reg, 0, sizeof aic.reg);
|
||||
aic.reg[0][AIC0_PLL_PR] = 0x11;
|
||||
aic.reg[0][AIC0_PLL_J] = 0x04;
|
||||
aic.reg[0][AIC0_NDAC] = aic.reg[0][AIC0_MDAC] = aic.reg[0][AIC0_NADC] = aic.reg[0][AIC0_MADC] = 0x01;
|
||||
aic.reg[0][AIC0_DOSR_LSB] = 0x80;
|
||||
aic.reg[0][AIC0_AOSR] = 0x80;
|
||||
aic.reg[0][AIC0_BCLK_N] = 0x01;
|
||||
aic.reg[0][AIC0_DAC_PRB] = aic.reg[0][AIC0_ADC_PRB] = 0x01;
|
||||
aic.reg[0][AIC0_DAC_SETUP1] = 0x14;
|
||||
aic.reg[0][AIC0_DAC_SETUP2] = 0x0C;
|
||||
aic.reg[0][AIC0_ADC_FINE] = 0x88;
|
||||
aic.reg[1][AIC1_LDO] = AIC1_LDO_ANALOG_OFF;
|
||||
aic.reg[1][AIC1_HPL_GAIN] = aic.reg[1][AIC1_HPR_GAIN] = 0x40;
|
||||
aic.reg[1][AIC1_LPGA_VOL] = aic.reg[1][AIC1_RPGA_VOL] = 0x80;
|
||||
aic.reg[1][11] = 0x10;
|
||||
aic.page = 0;
|
||||
aic.hp_reads = 0;
|
||||
}
|
||||
|
||||
// What the flags registers say now, from what is powered.
|
||||
static void aic_flags(void) {
|
||||
uint8_t out = aic.reg[1][AIC1_OUT_POWER], dac = aic.reg[0][AIC0_DAC_SETUP1], adc = aic.reg[0][AIC0_ADC_SETUP];
|
||||
aic.reg[0][AIC0_DAC_FLAGS] = (uint8_t)((dac & 0x80 ? 0x80 : 0) | (dac & 0x40 ? 0x08 : 0) |
|
||||
(out & AIC1_OUT_HPL ? 0x20 : 0) | (out & AIC1_OUT_HPR ? 0x02 : 0));
|
||||
aic.reg[0][AIC0_ADC_FLAGS] = (uint8_t)((adc & 0x80 ? 0xC0 : 0) | (adc & 0x40 ? 0x0C : 0));
|
||||
}
|
||||
|
||||
static struct {
|
||||
uint32_t now;
|
||||
bool i2s_on, clock_on; // the I2S slave running; the codec's clocks running
|
||||
uint32_t i2s_rate, frames, frame_frac;
|
||||
int starts, stops, resets, reboots;
|
||||
bool lost, streaming, intr;
|
||||
uint8_t input;
|
||||
bool loopback;
|
||||
uint16_t peaks[3];
|
||||
uint32_t tx_active;
|
||||
bool led_link, led_data;
|
||||
} fb;
|
||||
|
||||
uint32_t whal_millis(void) { return fb.now; }
|
||||
|
||||
// Time passes: the codec's frames arrive while its clocks and the slave run.
|
||||
void whal_sleep_ms(uint32_t ms) {
|
||||
fb.now += ms;
|
||||
if (fb.i2s_on && fb.clock_on) fb.frames += ms * fb.i2s_rate / 1000;
|
||||
}
|
||||
|
||||
void whal_codec_reset(void) {
|
||||
fb.resets++;
|
||||
aic_defaults();
|
||||
aic.t_reset = fb.now;
|
||||
}
|
||||
|
||||
// An access too soon after reset (SLAA557 3.2) is counted.
|
||||
static void aic_access(void) {
|
||||
if (fb.resets && fb.now - aic.t_reset < 1) aic.early++;
|
||||
}
|
||||
|
||||
bool whal_codec_write(uint8_t reg, uint8_t v) {
|
||||
if (!aic.answers) return false;
|
||||
aic_access();
|
||||
if (reg == AIC_PAGE) {
|
||||
aic.page = v;
|
||||
} else if (aic.page > 1) {
|
||||
aic.bad_page_writes++;
|
||||
} else if (aic.page == 0 && reg == AIC0_RESET && (v & 1)) {
|
||||
aic_defaults();
|
||||
} else {
|
||||
aic.reg[aic.page][reg & 0x7F] = v;
|
||||
}
|
||||
if (aic.nlog < LOG_MAX) aic.log[aic.nlog++] = (wr_t){aic.page, reg, v, fb.now};
|
||||
if (aic.page == 1 && reg == AIC1_OUT_POWER) aic.hp_reads = 0;
|
||||
aic_flags();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool whal_codec_read(uint8_t reg, uint8_t *v) {
|
||||
if (!aic.answers) return false;
|
||||
aic_access();
|
||||
if (reg == AIC_PAGE) {
|
||||
*v = aic.page;
|
||||
return true;
|
||||
}
|
||||
uint8_t p = aic.page > 1 ? 1 : aic.page;
|
||||
if (p == 1 && reg == AIC1_DAC_GAIN_FLAGS) {
|
||||
uint8_t hp = aic.reg[1][AIC1_OUT_POWER] & (AIC1_OUT_HPL | AIC1_OUT_HPR);
|
||||
bool ready = hp && !aic.hp_never && aic.hp_reads++ >= aic.hp_delay;
|
||||
aic.reg[1][reg] = ready ? (uint8_t)((hp & AIC1_OUT_HPL ? 0x80 : 0) | (hp & AIC1_OUT_HPR ? 0x40 : 0)) : 0;
|
||||
}
|
||||
*v = aic.reg[p][reg & 0x7F];
|
||||
if (p == 0 && (reg == AIC0_STICKY1 || reg == AIC0_STICKY2)) aic.reg[0][reg] = 0; // read to clear
|
||||
return true;
|
||||
}
|
||||
|
||||
bool whal_codec_int(void) { return fb.intr; }
|
||||
void whal_i2s_start(uint32_t rate) { fb.i2s_on = true; fb.i2s_rate = rate; fb.starts++; fb.frames = 0; }
|
||||
void whal_i2s_stop(void) { fb.i2s_on = false; fb.stops++; }
|
||||
|
||||
bool whal_i2s_lost(void) {
|
||||
bool l = fb.lost;
|
||||
fb.lost = false;
|
||||
return l;
|
||||
}
|
||||
|
||||
uint32_t whal_frames(void) { return fb.frames; }
|
||||
bool whal_streaming(void) { return fb.streaming; }
|
||||
// The fake streams' counters: fixed values, to see them carried through.
|
||||
void whal_counters(uint16_t *u, uint16_t *o, uint32_t *in, uint32_t *out) { *u = 2; *o = 3; *in = 4000; *out = 3000; }
|
||||
|
||||
void whal_peaks(uint16_t peak[3]) {
|
||||
memcpy(peak, fb.peaks, sizeof fb.peaks);
|
||||
memset(fb.peaks, 0, sizeof fb.peaks);
|
||||
}
|
||||
|
||||
uint32_t whal_tx_active(void) { return fb.tx_active; }
|
||||
void whal_set_input(uint8_t input) { fb.input = input; }
|
||||
void whal_set_loopback(bool on) { fb.loopback = on; }
|
||||
void whal_leds(bool link, bool data) { fb.led_link = link; fb.led_data = data; }
|
||||
void whal_reboot_uf2(void) { fb.reboots++; }
|
||||
|
||||
// A fresh fake board with a codec that answers and runs its clocks.
|
||||
static void board(void) {
|
||||
memset(&fb, 0, sizeof fb);
|
||||
memset(&aic, 0, sizeof aic);
|
||||
aic_defaults();
|
||||
aic.answers = true;
|
||||
aic.hp_delay = 5;
|
||||
fb.now = 1000;
|
||||
fb.clock_on = true;
|
||||
}
|
||||
|
||||
// The index in the write log of the first write to (page, reg) with
|
||||
// (v & mask) == want, from index `from`; -1 if none.
|
||||
static int find(int from, uint8_t page, uint8_t reg, uint8_t mask, uint8_t want) {
|
||||
for (int i = from; i < aic.nlog; i++)
|
||||
if (aic.log[i].page == page && aic.log[i].reg == reg && (aic.log[i].v & mask) == want) return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// The index of the last write to (page, reg); -1 if none.
|
||||
static int last(uint8_t page, uint8_t reg) {
|
||||
for (int i = aic.nlog - 1; i >= 0; i--)
|
||||
if (aic.log[i].page == page && aic.log[i].reg == reg) return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// A divider's value from its register (D6:0, 0 means 128).
|
||||
static unsigned div7(uint8_t v) { return (v & 0x7F) ? (v & 0x7Fu) : 128u; }
|
||||
|
||||
// SLAA557 Table 2-16 and Table 2-7: the processing blocks' resource class
|
||||
// and interpolation/decimation filter (A, B, C).
|
||||
static const unsigned prb_p_rc[26] = {0, 8, 12, 10, 4, 6, 6, 6, 8, 8, 10, 8, 3, 4, 4, 6, 4, 3, 6, 4, 2, 3, 2, 8, 12, 12};
|
||||
static const char prb_p_filter[26] = "-AAAAAABBBBBBBBBBCCCCCCAAA";
|
||||
static const unsigned prb_r_rc[19] = {0, 6, 8, 8, 3, 4, 4, 3, 4, 4, 2, 2, 2, 3, 4, 4, 2, 2, 2};
|
||||
static const char prb_r_filter[19] = "-AAAAAABBBBBBCCCCCC";
|
||||
|
||||
// The clocks the registers give, for one rate, against SLAA557's limits
|
||||
// (sections 2.3.3.3, 2.4.6, 2.7, Tables 2-25 and 2-26).
|
||||
static void check_clocks(uint32_t hz) {
|
||||
const uint8_t *p0 = aic.reg[0];
|
||||
// the PLL: from MCLK, the codec's clock; P (0 means 8), R, J.D
|
||||
CHECK((p0[AIC0_CLK_MUX] & 0x0C) == 0x00); // PLL_CLKIN = MCLK
|
||||
CHECK((p0[AIC0_CLK_MUX] & 0x03) == 0x03); // CODEC_CLKIN = PLL_CLK
|
||||
CHECK((p0[AIC0_CLK_MUX] & 0x40) == 0); // the low PLL range
|
||||
CHECK(p0[AIC0_PLL_PR] & AIC0_PLL_ON);
|
||||
unsigned P = (p0[AIC0_PLL_PR] >> 4) & 7, R = p0[AIC0_PLL_PR] & 0x0F, J = p0[AIC0_PLL_J] & 0x3F;
|
||||
unsigned D = (unsigned)(p0[AIC0_PLL_D_MSB] & 0x3F) << 8 | p0[AIC0_PLL_D_LSB];
|
||||
if (P == 0) P = 8;
|
||||
CHECK(P == 1 && R == 1 && J == 8 && D == 1920);
|
||||
CHECK(R >= 1 && R <= 4 && J >= 4 && J <= 63 && D <= 9999);
|
||||
uint64_t pll_clkin = WB_MCLK_HZ;
|
||||
CHECK(pll_clkin / P >= 10000000 && pll_clkin / P <= 20000000); // D != 0: 10..20 MHz (eq. 20)
|
||||
uint64_t pll = pll_clkin * R * (J * 10000u + D) / (P * 10000u);
|
||||
CHECK(pll * P * 10000u == pll_clkin * R * (J * 10000u + D)); // exact
|
||||
CHECK(pll == 98304000u);
|
||||
CHECK(pll >= 80000000u && pll <= 118000000u); // Table 2-26: AVDD >= 1.65 V, low range
|
||||
// the dividers, powered, and the rate they give
|
||||
for (int r = AIC0_NDAC; r <= AIC0_MDAC; r++) CHECK(p0[r] & AIC0_DIV_ON);
|
||||
CHECK((p0[AIC0_NADC] & AIC0_DIV_ON) && (p0[AIC0_MADC] & AIC0_DIV_ON) && (p0[AIC0_BCLK_N] & AIC0_DIV_ON));
|
||||
unsigned ndac = div7(p0[AIC0_NDAC]), mdac = div7(p0[AIC0_MDAC]), nadc = div7(p0[AIC0_NADC]),
|
||||
madc = div7(p0[AIC0_MADC]), bn = div7(p0[AIC0_BCLK_N]);
|
||||
unsigned dosr = (unsigned)(p0[AIC0_DOSR_MSB] & 3) << 8 | p0[AIC0_DOSR_LSB], aosr = p0[AIC0_AOSR];
|
||||
if (dosr == 0) dosr = 1024;
|
||||
if (aosr == 0) aosr = 256;
|
||||
CHECK(pll % ((uint64_t)ndac * mdac * dosr) == 0 && pll / ((uint64_t)ndac * mdac * dosr) == hz); // DAC_FS
|
||||
CHECK(pll % ((uint64_t)nadc * madc * aosr) == 0 && pll / ((uint64_t)nadc * madc * aosr) == hz); // ADC_FS
|
||||
CHECK(pll / ndac / bn == (uint64_t)WB_BCLK_PER_FRAME * hz); // BCLK
|
||||
// Table 2-25 (DVDD >= 1.65 V)
|
||||
CHECK(pll <= (ndac % 2 == 0 && nadc % 2 == 0 ? 137000000u : 110000000u)); // CODEC_CLKIN
|
||||
CHECK(pll / ndac <= 55296000u && pll / nadc <= 55296000u); // DAC_CLK, ADC_CLK, BDIV_CLKIN
|
||||
CHECK(pll / ndac / mdac <= 6758000u && pll / nadc / madc <= 6758000u); // the modulators' clocks
|
||||
CHECK(hz <= 192000u);
|
||||
// DOSR x fs between 2.8 and 6.2 MHz (2.4.6), the blocks' resources met
|
||||
CHECK((uint64_t)dosr * hz > 2800000u && (uint64_t)dosr * hz < 6200000u);
|
||||
unsigned pp = p0[AIC0_DAC_PRB] & 0x1F, pr = p0[AIC0_ADC_PRB] & 0x1F;
|
||||
CHECK(pp >= 1 && pp <= 25 && pr >= 1 && pr <= 18);
|
||||
if (pp < 1 || pp > 25 || pr < 1 || pr > 18) return;
|
||||
CHECK(mdac * dosr / 32 >= prb_p_rc[pp]);
|
||||
CHECK(madc * aosr / 32 >= prb_r_rc[pr]);
|
||||
// each block's filter suits its OSR (2.4.6: A multiple of 8, B of 4, C of 2;
|
||||
// register 20: A 128 or 64, B 64, C 32)
|
||||
char fp = prb_p_filter[pp], fr = prb_r_filter[pr];
|
||||
CHECK(dosr % (fp == 'A' ? 8u : fp == 'B' ? 4u : 2u) == 0);
|
||||
CHECK(fr == 'A' ? (aosr == 128 || aosr == 64) : fr == 'B' ? aosr == 64 : aosr == 32);
|
||||
// SLAA557 2.3.3.3 and 2.4.6: filter A for 48 kHz, B for 96, C for 192
|
||||
char want = hz <= 48000 ? 'A' : hz <= 96000 ? 'B' : 'C';
|
||||
CHECK(fp == want && fr == want);
|
||||
// both stereo blocks (the ADC's two channels are both used)
|
||||
CHECK(pr == 1 || pr == 2 || pr == 3 || pr == 7 || pr == 8 || pr == 9 || pr == 13 || pr == 14 || pr == 15);
|
||||
// I2S, 32-bit words, the codec the master; BCLK from DAC_CLK
|
||||
CHECK(p0[AIC0_IFACE1] == (AIC0_IFACE1_I2S | AIC0_IFACE1_32BIT | AIC0_IFACE1_BCLK_OUT | AIC0_IFACE1_WCLK_OUT));
|
||||
CHECK((p0[AIC0_IFACE3] & 0x03) == AIC0_IFACE3_BDIV_DAC_CLK);
|
||||
}
|
||||
|
||||
// Each rate's settings give exactly that rate and a 64-bit frame, within
|
||||
// every clock limit SLAA557 sets; the spec's 96 kHz numbers (SPEC-WB §4).
|
||||
TEST(codec_clocks) {
|
||||
static const uint32_t hz[] = {48000, 96000, 192000};
|
||||
for (int i = 0; i < 3; i++) {
|
||||
board();
|
||||
const wb_rate_t *r = wb_rate(hz[i]);
|
||||
CHECK(r != NULL);
|
||||
if (!r) continue;
|
||||
wb_codec_cfg_t cfg = {r, 0, 0, false};
|
||||
CHECK(wb_codec_start(&cfg) == WB_CODEC_OK);
|
||||
check_clocks(hz[i]);
|
||||
}
|
||||
const wb_rate_t *r = wb_rate(96000);
|
||||
CHECK(r->osr == 64 && r->prb_p == 7 && r->prb_r == 7 && r->bclk_n == 4);
|
||||
r = wb_rate(192000);
|
||||
CHECK(r->osr == 32 && r->prb_p == 17 && r->prb_r == 13 && r->bclk_n == 2);
|
||||
CHECK(wb_rate(44100) == NULL && wb_rate(8000) == NULL && wb_rate(0) == NULL);
|
||||
}
|
||||
|
||||
// The board's netlist (hardware/netlist/build_netlist.py, the wb variant):
|
||||
// each codec input's node, as +1/-1 of the line's voltage (XF_A - XF_B) and
|
||||
// the replica's (REP_A - REP_B), each node half of its pair's voltage.
|
||||
typedef struct { const char *pin; int line, replica; } node_t;
|
||||
static const node_t inputs[] = {
|
||||
{"IN1L", +1, 0}, // XF_A: line +
|
||||
{"IN1R", 0, -1}, // REP_B: replica -
|
||||
{"IN2R", -1, 0}, // XF_B: line -
|
||||
{"IN3R", 0, +1}, // REP_A: replica +
|
||||
};
|
||||
|
||||
// The input routed into a MicPGA field: its node's line and replica parts
|
||||
// added to *line and *rep with the terminal's sign, and its resistance.
|
||||
static void route(uint8_t reg, int shift, int input, int sign, int *line, int *rep, unsigned *r) {
|
||||
unsigned f = (reg >> shift) & 3u;
|
||||
if (!f) return;
|
||||
*line += sign * inputs[input].line;
|
||||
*rep += sign * inputs[input].replica;
|
||||
*r = f;
|
||||
}
|
||||
|
||||
// The MicPGAs' routing bits (SLAA557 5.3.26-5.3.31) make the left ADC
|
||||
// the hybrid, line - replica, through 10 kOhm on every input, and the right
|
||||
// ADC the line alone (inverted) through 20 kOhm; nothing else is routed and
|
||||
// the unused inputs sit at the common mode.
|
||||
TEST(codec_hybrid_routing) {
|
||||
board();
|
||||
wb_codec_cfg_t cfg = {wb_rate(96000), 0, 24, false};
|
||||
CHECK(wb_codec_start(&cfg) == WB_CODEC_OK);
|
||||
const uint8_t *p1 = aic.reg[1];
|
||||
CHECK(p1[AIC1_LPGA_P] == 0x41); // IN1L 10k (D7:6 = 01), IN1R 10k (D1:0 = 01)
|
||||
CHECK(p1[AIC1_LPGA_M] == 0x14); // IN2R 10k (D5:4 = 01), IN3R 10k (D3:2 = 01), no CM
|
||||
CHECK(p1[AIC1_RPGA_P] == 0x20); // IN2R 20k (D5:4 = 10)
|
||||
CHECK(p1[AIC1_RPGA_M] == 0x20); // IN1L 20k (D5:4 = 10)
|
||||
int line = 0, rep = 0, ln = 0, rp = 0;
|
||||
unsigned r = 0, rl[4] = {0, 0, 0, 0};
|
||||
// left +: IN1L D7:6, IN1R D1:0 (IN2L D5:4 and IN3L D3:2 must be off)
|
||||
route(p1[AIC1_LPGA_P], 6, 0, +1, &line, &rep, &rl[0]);
|
||||
route(p1[AIC1_LPGA_P], 0, 1, +1, &line, &rep, &rl[1]);
|
||||
CHECK(((p1[AIC1_LPGA_P] >> 2) & 0x0F) == 0);
|
||||
// left -: IN2R D5:4, IN3R D3:2 (CM1L D7:6, CM2L D1:0 off)
|
||||
route(p1[AIC1_LPGA_M], 4, 2, -1, &line, &rep, &rl[2]);
|
||||
route(p1[AIC1_LPGA_M], 2, 3, -1, &line, &rep, &rl[3]);
|
||||
CHECK((p1[AIC1_LPGA_M] & 0xC3) == 0);
|
||||
CHECK(line == 2 && rep == -2); // (A - B) of the line minus (A - B) of the replica
|
||||
for (int i = 0; i < 4; i++) CHECK(rl[i] == AIC_R10K);
|
||||
// right +: IN2R D5:4 (IN1R, IN3R, IN2L off); right -: IN1L D5:4 (CMs, IN3L off)
|
||||
route(p1[AIC1_RPGA_P], 4, 2, +1, &ln, &rp, &r);
|
||||
CHECK(r == AIC_R20K && (p1[AIC1_RPGA_P] & 0xCF) == 0);
|
||||
route(p1[AIC1_RPGA_M], 4, 0, -1, &ln, &rp, &r);
|
||||
CHECK(r == AIC_R20K && (p1[AIC1_RPGA_M] & 0xCF) == 0);
|
||||
CHECK(ln == -2 && rp == 0);
|
||||
CHECK(p1[AIC1_FLOAT] == (AIC1_FLOAT_IN2L | AIC1_FLOAT_IN3L));
|
||||
CHECK(p1[AIC1_LPGA_VOL] == 24 && p1[AIC1_RPGA_VOL] == 0); // gains on (D7 clear)
|
||||
// the line drive: HPL = left DAC +, HPR = left DAC -, nothing else; CM 0.9 V,
|
||||
// the drivers from AVDD at 0 dB, unmuted and on; the A-LDO at 1.77 V
|
||||
CHECK(p1[AIC1_HPL_ROUTE] == AIC1_HPL_LDAC && p1[AIC1_HPR_ROUTE] == AIC1_HPR_LDAC_M);
|
||||
CHECK(p1[AIC1_CM] == 0x00);
|
||||
CHECK(p1[AIC1_HPL_GAIN] == 0x00 && p1[AIC1_HPR_GAIN] == 0x00);
|
||||
CHECK(p1[AIC1_OUT_POWER] == (AIC1_OUT_HPL | AIC1_OUT_HPR));
|
||||
CHECK(p1[AIC1_POWER] == AIC1_POWER_NO_WEAK_AVDD);
|
||||
CHECK(p1[AIC1_LDO] == (AIC1_LDO_AVDD_1V77 | AIC1_LDO_AVDD_ON));
|
||||
// the left DAC on and unmuted, the right one off and muted; both ADCs on and unmuted
|
||||
CHECK(aic.reg[0][AIC0_DAC_SETUP1] == 0x90 && aic.reg[0][AIC0_DAC_SETUP2] == AIC0_DAC_MUTE_R);
|
||||
CHECK(aic.reg[0][AIC0_ADC_SETUP] == AIC0_ADC_ON && aic.reg[0][AIC0_ADC_FINE] == 0x00);
|
||||
CHECK(aic.reg[0][AIC0_MFP5] == AIC0_MFP5_INT1);
|
||||
CHECK(aic.bad_page_writes == 0);
|
||||
}
|
||||
|
||||
// The bring-up keeps SLAA557's order: reset first and nothing for 1 ms
|
||||
// after it; P, R, J, D before the PLL is powered, D's two registers back to
|
||||
// back (and DOSR's); the dividers once the PLL runs; the weak AVDD link off
|
||||
// before the LDO; the drivers set up before they are powered; the DAC only
|
||||
// once the drivers' flags say they're up; the ADCs after their routing.
|
||||
TEST(codec_sequence) {
|
||||
board();
|
||||
wb_codec_cfg_t cfg = {wb_rate(96000), -12, 0, false};
|
||||
uint32_t t0 = fb.now;
|
||||
CHECK(wb_codec_start(&cfg) == WB_CODEC_OK);
|
||||
CHECK(fb.resets == 1 && aic.early == 0);
|
||||
CHECK(fb.now - t0 >= 12 && fb.now - t0 < 100); // 1 + 1 + 10 ms and the flags
|
||||
int rst = find(0, 0, AIC0_RESET, 0x01, 0x01);
|
||||
CHECK(rst == 1 && aic.log[0].reg == AIC_PAGE && aic.log[0].v == 0); // page 0, then the reset
|
||||
int j = find(0, 0, AIC0_PLL_J, 0, 0), dm = find(0, 0, AIC0_PLL_D_MSB, 0, 0), dl = find(0, 0, AIC0_PLL_D_LSB, 0, 0);
|
||||
int pll_on = find(0, 0, AIC0_PLL_PR, AIC0_PLL_ON, AIC0_PLL_ON), pr = find(0, 0, AIC0_PLL_PR, AIC0_PLL_ON, 0);
|
||||
CHECK(pr > rst && j > rst && dl == dm + 1 && pll_on > dl && pll_on > j && pll_on > pr);
|
||||
CHECK(aic.log[find(0, 0, AIC0_NDAC, 0, 0)].t >= aic.log[pll_on].t + 10); // the PLL's 10 ms
|
||||
CHECK(find(0, 0, AIC0_DOSR_LSB, 0, 0) == find(0, 0, AIC0_DOSR_MSB, 0, 0) + 1);
|
||||
int weak = find(0, 1, AIC1_POWER, 0, 0), ldo = find(0, 1, AIC1_LDO, AIC1_LDO_AVDD_ON, AIC1_LDO_AVDD_ON);
|
||||
CHECK(weak > 0 && ldo > weak);
|
||||
int hp_on = find(0, 1, AIC1_OUT_POWER, 0x30, 0x30);
|
||||
CHECK(hp_on > 0);
|
||||
static const uint8_t before_hp[] = {AIC1_CM, AIC1_HP_START, AIC1_HPL_ROUTE, AIC1_HPR_ROUTE, AIC1_HPL_GAIN,
|
||||
AIC1_HPR_GAIN, AIC1_REF, AIC1_PLAY1};
|
||||
for (unsigned i = 0; i < sizeof before_hp; i++) {
|
||||
int w = find(0, 1, before_hp[i], 0, 0);
|
||||
CHECK(w > ldo && w < hp_on);
|
||||
}
|
||||
int dac_on = find(0, 0, AIC0_DAC_SETUP1, 0x80, 0x80), adc_on = find(0, 0, AIC0_ADC_SETUP, 0xC0, 0xC0);
|
||||
CHECK(dac_on > hp_on && aic.hp_reads > aic.hp_delay); // after the drivers' flags were seen
|
||||
CHECK(adc_on > last(1, AIC1_LPGA_M) && adc_on > last(1, AIC1_RPGA_M) && adc_on > last(1, AIC1_LPGA_VOL));
|
||||
CHECK(dac_on > find(0, 0, AIC0_DAC_PRB, 0, 0) && adc_on > find(0, 0, AIC0_ADC_PRB, 0, 0));
|
||||
CHECK(find(0, 0, AIC0_DAC_VOL_L, 0, 0) > 0 && aic.reg[0][AIC0_DAC_VOL_L] == 0xF4); // -12 x 0.5 dB
|
||||
CHECK(aic.reg[0][AIC0_IFACE3] == AIC0_IFACE3_CLKS_ON);
|
||||
// the codec loopback is register 29's ADC-to-DAC bit
|
||||
board();
|
||||
cfg.loop_codec = true;
|
||||
CHECK(wb_codec_start(&cfg) == WB_CODEC_OK);
|
||||
CHECK(aic.reg[0][AIC0_IFACE3] == (AIC0_IFACE3_CLKS_ON | AIC0_IFACE3_ADC_TO_DAC));
|
||||
CHECK(wb_codec_loop(false) && aic.reg[0][AIC0_IFACE3] == AIC0_IFACE3_CLKS_ON);
|
||||
}
|
||||
|
||||
// A codec that doesn't answer, or whose drivers never come up: the bring-up
|
||||
// says so (within its timeouts), and the device's requests still work.
|
||||
TEST(codec_failures) {
|
||||
board();
|
||||
aic.answers = false;
|
||||
wb_codec_cfg_t cfg = {wb_rate(96000), 0, 0, false};
|
||||
CHECK(wb_codec_start(&cfg) == WB_CODEC_NO_ANSWER);
|
||||
board();
|
||||
aic.hp_never = true;
|
||||
uint32_t t0 = fb.now;
|
||||
CHECK(wb_codec_start(&cfg) == WB_CODEC_HP_TIMEOUT);
|
||||
CHECK(fb.now - t0 >= 500 && fb.now - t0 < 600);
|
||||
CHECK((aic.reg[0][AIC0_DAC_SETUP1] & 0x80) == 0); // no DAC without its drivers
|
||||
// the device: no codec, no CODEC state; GET_INFO still answers
|
||||
board();
|
||||
aic.answers = false;
|
||||
fb.clock_on = false;
|
||||
wb_dev_t d;
|
||||
CHECK(!wb_dev_init(&d, 96000));
|
||||
CHECK(!(wb_dev_state(&d) & WP_ST_CODEC));
|
||||
uint8_t buf[32];
|
||||
CHECK(wb_dev_vendor_in(&d, WP_REQ_GET_INFO, 0, 0, buf, sizeof buf) == (int)sizeof(wp_info_t));
|
||||
CHECK(wb_dev_vendor_out(&d, WP_REQ_SET_TX_GAIN, 4, 0)); // kept for when it answers
|
||||
CHECK(d.tx_gain == 4);
|
||||
CHECK(wb_dev_vendor_in(&d, WP_REQ_CODEC_READ, 5, 0, buf, sizeof buf) == -1);
|
||||
// answering but no clocks: up, but not CODEC
|
||||
board();
|
||||
fb.clock_on = false;
|
||||
CHECK(!wb_dev_init(&d, 96000));
|
||||
CHECK(d.result == WB_CODEC_OK && !d.clock && !(wb_dev_state(&d) & WP_ST_CODEC));
|
||||
}
|
||||
|
||||
// Packets are sized by the codec's clock: a millisecond's samples, mono S16,
|
||||
// with room to catch up, within a full-speed isochronous packet.
|
||||
TEST(packet_sizing) {
|
||||
CHECK(wp_samples_per_ms(96000) * 2 == 192); // SPEC-WB §5
|
||||
CHECK(wp_samples_per_ms(192000) * 2 == 384);
|
||||
CHECK(wp_samples_per_ms(48000) * 2 == 96);
|
||||
CHECK(WP_MAX_SAMPLES * 4 >= wp_samples_per_ms(192000) * 5); // 25% to catch up at the top rate
|
||||
CHECK(WP_PACKET_BYTES == 2 * WP_MAX_SAMPLES && WP_PACKET_BYTES <= 1023);
|
||||
CHECK(sizeof(wp_info_t) == 16 && sizeof(wp_status_t) == 28 && sizeof(wp_event_t) == 8);
|
||||
CHECK(SFIFO_SIZE >= 3u * wp_samples_per_ms(192000) + 2u * WP_MAX_SAMPLES); // head start and two packets
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// The samples between the frames and the streams
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// A frame's words: left, right samples MSB-aligned, junk in the low bits.
|
||||
static void frame(uint32_t *w, int16_t l, int16_t r) {
|
||||
w[0] = (uint32_t)(uint16_t)l << 16 | 0x1234u;
|
||||
w[1] = (uint32_t)(uint16_t)r << 16 | 0xABCDu;
|
||||
}
|
||||
|
||||
// Received frames: the chosen channel's top 16 bits reach the host in order,
|
||||
// only while streaming; the peaks follow both channels.
|
||||
TEST(audio_rx) {
|
||||
static wb_audio_t a;
|
||||
wb_audio_init(&a, 0);
|
||||
uint32_t w[2 * 8];
|
||||
for (int i = 0; i < 8; i++) frame(&w[2 * i], (int16_t)(100 * i), (int16_t)(-1000 - i));
|
||||
wb_audio_rx(&a, w, 8);
|
||||
CHECK(sfifo_count(&a.s.to_host) == 0 && a.frames == 8); // not streaming
|
||||
sstream_set(&a.s, true);
|
||||
wb_audio_rx(&a, w, 8);
|
||||
int16_t got[16];
|
||||
CHECK(sstream_to_host(&a.s, got, 16) == 8);
|
||||
bool ok = true;
|
||||
for (int i = 0; i < 8; i++) ok &= got[i] == (int16_t)(100 * i);
|
||||
CHECK(ok);
|
||||
a.input = WP_INPUT_LINE;
|
||||
wb_audio_rx(&a, w, 8);
|
||||
CHECK(sstream_to_host(&a.s, got, 16) == 8 && got[0] == -1000 && got[7] == -1007);
|
||||
uint16_t pk[WB_PEAKS];
|
||||
wb_audio_peaks(&a, pk);
|
||||
CHECK(pk[WB_PEAK_HYBRID] == 700 && pk[WB_PEAK_LINE] == 1007 && pk[WB_PEAK_TX] == 0);
|
||||
wb_audio_peaks(&a, pk);
|
||||
CHECK(pk[WB_PEAK_HYBRID] == 0 && pk[WB_PEAK_LINE] == 0);
|
||||
frame(w, -32768, 32767);
|
||||
wb_audio_rx(&a, w, 1);
|
||||
wb_audio_peaks(&a, pk);
|
||||
CHECK(pk[WB_PEAK_HYBRID] == 32768 && pk[WB_PEAK_LINE] == 32767);
|
||||
CHECK(wb_word_sample(0x80000000u) == -32768 && wb_sample_word(-1) == 0xFFFF0000u);
|
||||
}
|
||||
|
||||
// Frames to send: the host's samples after their head start, in both slots
|
||||
// with the low bits clear; silence before and after; the DATA count.
|
||||
TEST(audio_tx) {
|
||||
static wb_audio_t a;
|
||||
wb_audio_init(&a, 4);
|
||||
uint32_t w[2 * 8];
|
||||
wb_audio_tx(&a, w, 8);
|
||||
bool silent = true;
|
||||
for (int i = 0; i < 16; i++) silent &= w[i] == 0;
|
||||
CHECK(silent && a.s.underruns == 0);
|
||||
sstream_set(&a.s, true);
|
||||
int16_t s[6] = {1, -2, 3, -4, 5, -32768};
|
||||
sstream_from_host(&a.s, s, 3);
|
||||
wb_audio_tx(&a, w, 2);
|
||||
CHECK(w[0] == 0 && a.s.samples_out == 0); // 3 < 4: still filling
|
||||
sstream_from_host(&a.s, s + 3, 3);
|
||||
wb_audio_tx(&a, w, 8);
|
||||
CHECK(w[0] == 0x00010000u && w[1] == 0x00010000u && w[2] == 0xFFFE0000u && w[3] == 0xFFFE0000u);
|
||||
CHECK(w[10] == 0x80000000u && w[12] == 0);
|
||||
CHECK(a.s.samples_out == 6 && a.tx_active == 6 && a.s.underruns == 1);
|
||||
uint16_t pk[WB_PEAKS];
|
||||
wb_audio_peaks(&a, pk);
|
||||
CHECK(pk[WB_PEAK_TX] == 32768);
|
||||
}
|
||||
|
||||
// Firmware loopback: the host's samples come back to it, one per frame, and
|
||||
// the line's don't; off again, the line's do.
|
||||
TEST(audio_loopback) {
|
||||
static wb_audio_t a;
|
||||
wb_audio_init(&a, 0);
|
||||
sstream_set(&a.s, true);
|
||||
a.loopback = true;
|
||||
int16_t s[5] = {11, 22, 33, 44, 55};
|
||||
sstream_from_host(&a.s, s, 5);
|
||||
uint32_t w[2 * 5];
|
||||
for (int i = 0; i < 5; i++) frame(&w[2 * i], 999, 999);
|
||||
wb_audio_tx(&a, w, 5);
|
||||
wb_audio_rx(&a, w, 5);
|
||||
int16_t got[10];
|
||||
CHECK(sstream_to_host(&a.s, got, 10) == 5);
|
||||
CHECK(got[0] == 11 && got[4] == 55);
|
||||
a.loopback = false;
|
||||
for (int i = 0; i < 5; i++) frame(&w[2 * i], 999, 999);
|
||||
wb_audio_rx(&a, w, 5);
|
||||
CHECK(sstream_to_host(&a.s, got, 10) == 5 && got[0] == 999);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// The device logic and requests
|
||||
// ---------------------------------------------------------------------------
|
||||
static wb_dev_t d;
|
||||
|
||||
// A fresh board and device, up at 96 kHz.
|
||||
static void dev_setup(void) {
|
||||
board();
|
||||
CHECK(wb_dev_init(&d, 96000));
|
||||
}
|
||||
|
||||
// Init: the codec up at 96 kHz, the I2S slave started at that rate, the
|
||||
// defaults, and all of it in GET_INFO.
|
||||
TEST(dev_init_and_info) {
|
||||
dev_setup();
|
||||
CHECK(fb.i2s_on && fb.i2s_rate == 96000 && fb.starts == 1);
|
||||
CHECK(d.clock && d.result == WB_CODEC_OK);
|
||||
CHECK(fb.input == WP_INPUT_HYBRID && !fb.loopback);
|
||||
uint8_t buf[64];
|
||||
CHECK(wb_dev_vendor_in(&d, WP_REQ_GET_INFO, 0, 0, buf, sizeof buf) == (int)sizeof(wp_info_t));
|
||||
wp_info_t i;
|
||||
memcpy(&i, buf, sizeof i);
|
||||
CHECK(i.version == WP_VERSION && i.rate == 96000 && i.max_samples == WP_MAX_SAMPLES && i.board == WP_BOARD);
|
||||
CHECK(i.state == WP_ST_CODEC && i.input == WP_INPUT_HYBRID && i.loopback == WP_LOOP_OFF);
|
||||
CHECK(i.tx_gain == 0 && i.rx_gain == 0);
|
||||
CHECK(wb_dev_vendor_in(&d, WP_REQ_GET_INFO, 0, 0, buf, 4) == 4); // short wLength: cut
|
||||
board();
|
||||
CHECK(wb_dev_init(&d, 12345)); // not a rate it has: 96 kHz
|
||||
CHECK(d.rate->hz == 96000);
|
||||
}
|
||||
|
||||
// SET_RATE takes 48, 96 and 192 (kHz): the codec brought up again at that
|
||||
// rate (its registers say so) and the slave restarted; others stall.
|
||||
TEST(dev_rate) {
|
||||
dev_setup();
|
||||
CHECK(wb_dev_vendor_out(&d, WP_REQ_SET_RATE, 192, 0));
|
||||
CHECK(d.rate->hz == 192000 && fb.i2s_rate == 192000 && fb.starts == 2 && fb.resets == 2);
|
||||
check_clocks(192000);
|
||||
CHECK(wb_dev_vendor_out(&d, WP_REQ_SET_RATE, 48, 0));
|
||||
check_clocks(48000);
|
||||
CHECK(!wb_dev_vendor_out(&d, WP_REQ_SET_RATE, 44, 0));
|
||||
CHECK(!wb_dev_vendor_out(&d, WP_REQ_SET_RATE, 96000 & 0xFFFF, 0));
|
||||
CHECK(d.rate->hz == 48000);
|
||||
uint8_t buf[16];
|
||||
wp_info_t i;
|
||||
CHECK(wb_dev_vendor_in(&d, WP_REQ_GET_INFO, 0, 0, buf, sizeof buf) == 16);
|
||||
memcpy(&i, buf, sizeof i);
|
||||
CHECK(i.rate == 48000);
|
||||
}
|
||||
|
||||
// The gains, the input and the loopbacks: in range they reach the codec
|
||||
// (or the samples' path) and survive a reset; out of range they stall.
|
||||
TEST(dev_settings) {
|
||||
dev_setup();
|
||||
CHECK(wb_dev_vendor_out(&d, WP_REQ_SET_TX_GAIN, (uint16_t)-20, 0));
|
||||
CHECK(aic.reg[0][AIC0_DAC_VOL_L] == (uint8_t)-20 && d.tx_gain == -20);
|
||||
CHECK(!wb_dev_vendor_out(&d, WP_REQ_SET_TX_GAIN, (uint16_t)-128, 0));
|
||||
CHECK(!wb_dev_vendor_out(&d, WP_REQ_SET_TX_GAIN, 49, 0));
|
||||
CHECK(wb_dev_vendor_out(&d, WP_REQ_SET_TX_GAIN, 48, 0) && aic.reg[0][AIC0_DAC_VOL_L] == 48);
|
||||
CHECK(wb_dev_vendor_out(&d, WP_REQ_SET_RX_GAIN, 40, 0) && aic.reg[1][AIC1_LPGA_VOL] == 40);
|
||||
CHECK(!wb_dev_vendor_out(&d, WP_REQ_SET_RX_GAIN, 96, 0));
|
||||
CHECK(wb_dev_vendor_out(&d, WP_REQ_SET_INPUT, WP_INPUT_LINE, 0) && fb.input == WP_INPUT_LINE);
|
||||
CHECK(!wb_dev_vendor_out(&d, WP_REQ_SET_INPUT, 2, 0));
|
||||
CHECK(wb_dev_vendor_out(&d, WP_REQ_SET_LOOPBACK, WP_LOOP_FIRMWARE, 0) && fb.loopback);
|
||||
CHECK(aic.reg[0][AIC0_IFACE3] == AIC0_IFACE3_CLKS_ON);
|
||||
CHECK(wb_dev_state(&d) & WP_ST_LOOPBACK);
|
||||
CHECK(wb_dev_vendor_out(&d, WP_REQ_SET_LOOPBACK, WP_LOOP_CODEC, 0) && !fb.loopback);
|
||||
CHECK(aic.reg[0][AIC0_IFACE3] & AIC0_IFACE3_ADC_TO_DAC);
|
||||
CHECK(!wb_dev_vendor_out(&d, WP_REQ_SET_LOOPBACK, 3, 0));
|
||||
// a reset keeps them all
|
||||
CHECK(wb_dev_vendor_out(&d, WP_REQ_RESET, 0, 0) && fb.resets == 2);
|
||||
CHECK(aic.reg[0][AIC0_DAC_VOL_L] == 48 && aic.reg[1][AIC1_LPGA_VOL] == 40);
|
||||
CHECK((aic.reg[0][AIC0_IFACE3] & AIC0_IFACE3_ADC_TO_DAC) && fb.input == WP_INPUT_LINE);
|
||||
CHECK(wb_dev_vendor_out(&d, WP_REQ_SET_LOOPBACK, WP_LOOP_OFF, 0));
|
||||
CHECK(!(aic.reg[0][AIC0_IFACE3] & AIC0_IFACE3_ADC_TO_DAC) && !(wb_dev_state(&d) & WP_ST_LOOPBACK));
|
||||
}
|
||||
|
||||
// Raw register access with the page in wIndex's high byte; the page select
|
||||
// itself is the firmware's; unknown requests stall.
|
||||
TEST(dev_register_access) {
|
||||
dev_setup();
|
||||
CHECK(wb_dev_vendor_out(&d, WP_REQ_CODEC_WRITE, AIC1_HPL_GAIN << 8 | 0x05, 1 << 8));
|
||||
CHECK(aic.reg[1][AIC1_HPL_GAIN] == 0x05);
|
||||
CHECK(wb_dev_vendor_out(&d, WP_REQ_CODEC_WRITE, 100 << 8 | 0x42, 0));
|
||||
CHECK(aic.reg[0][100] == 0x42);
|
||||
uint8_t b[4];
|
||||
CHECK(wb_dev_vendor_in(&d, WP_REQ_CODEC_READ, AIC1_HPL_GAIN, 1 << 8, b, sizeof b) == 1 && b[0] == 0x05);
|
||||
CHECK(wb_dev_vendor_in(&d, WP_REQ_CODEC_READ, 100, 0, b, sizeof b) == 1 && b[0] == 0x42);
|
||||
CHECK(wb_dev_vendor_in(&d, WP_REQ_CODEC_READ, 128, 0, b, sizeof b) == -1);
|
||||
CHECK(!wb_dev_vendor_out(&d, WP_REQ_CODEC_WRITE, AIC_PAGE << 8 | 1, 0));
|
||||
CHECK(!wb_dev_vendor_out(&d, WP_REQ_CODEC_WRITE, 0x80 << 8, 0));
|
||||
CHECK(wb_dev_vendor_in(&d, 0x7F, 0, 0, b, sizeof b) == -1);
|
||||
CHECK(!wb_dev_vendor_out(&d, 0x7F, 0, 0));
|
||||
CHECK(wb_dev_vendor_in(&d, WP_REQ_SET_RATE, 96, 0, b, sizeof b) == -1); // an OUT request asked IN
|
||||
CHECK(wb_dev_vendor_out(&d, WP_REQ_REBOOT_UF2, 0, 0) && fb.reboots == 1);
|
||||
}
|
||||
|
||||
// GET_STATUS: the peaks and the codec's sticky flags since the last one,
|
||||
// its power flags, the streams' counters.
|
||||
TEST(dev_status) {
|
||||
dev_setup();
|
||||
fb.peaks[0] = 1000;
|
||||
fb.peaks[1] = 20000;
|
||||
fb.peaks[2] = 30000;
|
||||
aic.reg[0][AIC0_STICKY1] = 0x08; // left ADC overflow
|
||||
aic.reg[0][AIC0_STICKY2] = 0x80; // HPL over current
|
||||
uint8_t buf[64];
|
||||
CHECK(wb_dev_vendor_in(&d, WP_REQ_GET_STATUS, 0, 0, buf, sizeof buf) == (int)sizeof(wp_status_t));
|
||||
wp_status_t s;
|
||||
memcpy(&s, buf, sizeof s);
|
||||
CHECK(s.peak_hybrid == 1000 && s.peak_line == 20000 && s.peak_tx == 30000);
|
||||
CHECK(s.ovf == 0x08 && s.oc == 0x80);
|
||||
CHECK((s.adc_flags & AIC0_ADC_FLAGS_UP) == AIC0_ADC_FLAGS_UP && (s.dac_flags & AIC0_DAC_FLAGS_UP) == AIC0_DAC_FLAGS_UP);
|
||||
CHECK(s.underruns == 2 && s.overruns == 3 && s.samples_in == 4000 && s.samples_out == 3000);
|
||||
CHECK(s.state == WP_ST_CODEC);
|
||||
CHECK(wb_dev_vendor_in(&d, WP_REQ_GET_STATUS, 0, 0, buf, sizeof buf) > 0);
|
||||
memcpy(&s, buf, sizeof s);
|
||||
CHECK(s.peak_hybrid == 0 && s.ovf == 0 && s.oc == 0);
|
||||
}
|
||||
|
||||
// The LEDs: LINK as the host sets it (AUTO: while streaming); DATA while
|
||||
// the host's samples aren't silent, and 50 ms after.
|
||||
TEST(dev_leds) {
|
||||
dev_setup();
|
||||
wb_dev_poll(&d);
|
||||
CHECK(!fb.led_link && !fb.led_data);
|
||||
fb.streaming = true;
|
||||
wb_dev_poll(&d);
|
||||
CHECK(fb.led_link && (wb_dev_state(&d) & WP_ST_CARRIER));
|
||||
CHECK(wb_dev_vendor_out(&d, WP_REQ_SET_CARRIER, WP_CARRIER_OFF, 0));
|
||||
wb_dev_poll(&d);
|
||||
CHECK(!fb.led_link);
|
||||
fb.streaming = false;
|
||||
CHECK(wb_dev_vendor_out(&d, WP_REQ_SET_CARRIER, WP_CARRIER_ON, 0));
|
||||
wb_dev_poll(&d);
|
||||
CHECK(fb.led_link);
|
||||
CHECK(!wb_dev_vendor_out(&d, WP_REQ_SET_CARRIER, 3, 0));
|
||||
fb.tx_active = 10;
|
||||
wb_dev_poll(&d);
|
||||
CHECK(fb.led_data);
|
||||
fb.now += 49;
|
||||
wb_dev_poll(&d);
|
||||
CHECK(fb.led_data);
|
||||
fb.now += 1;
|
||||
wb_dev_poll(&d);
|
||||
CHECK(!fb.led_data);
|
||||
}
|
||||
|
||||
// Events: when the state changes, the codec's INT1 brings its sticky flags
|
||||
// (read and cleared), the clocks stop or the slave loses its place (counted).
|
||||
TEST(dev_events) {
|
||||
dev_setup();
|
||||
wp_event_t e;
|
||||
CHECK(wb_dev_event(&d, &e));
|
||||
uint8_t seq = e.seq;
|
||||
CHECK(e.state == WP_ST_CODEC && e.underruns == 2 && e.overruns == 3);
|
||||
CHECK(!wb_dev_event(&d, &e));
|
||||
fb.streaming = true;
|
||||
CHECK(wb_dev_event(&d, &e) && e.seq == (uint8_t)(seq + 1));
|
||||
CHECK(e.state == (WP_ST_CODEC | WP_ST_STREAMING | WP_ST_CARRIER));
|
||||
// INT1: the overflow read out of register 42 and cleared, once in an event
|
||||
aic.reg[0][AIC0_STICKY1] = 0x04;
|
||||
fb.intr = true;
|
||||
wb_dev_poll(&d);
|
||||
fb.intr = false;
|
||||
CHECK(aic.reg[0][AIC0_STICKY1] == 0);
|
||||
CHECK(wb_dev_event(&d, &e) && e.ovf == 0x04);
|
||||
CHECK(!wb_dev_event(&d, &e));
|
||||
// the codec's clocks stop: within 100 ms, no CODEC
|
||||
fb.clock_on = false;
|
||||
fb.now += 100;
|
||||
wb_dev_poll(&d);
|
||||
fb.now += 100;
|
||||
wb_dev_poll(&d);
|
||||
CHECK(!d.clock && wb_dev_event(&d, &e) && !(e.state & WP_ST_CODEC));
|
||||
fb.clock_on = true;
|
||||
whal_sleep_ms(100);
|
||||
wb_dev_poll(&d);
|
||||
CHECK(d.clock && wb_dev_event(&d, &e) && (e.state & WP_ST_CODEC));
|
||||
// the slave lost its place: counted for the status
|
||||
fb.lost = true;
|
||||
wb_dev_poll(&d);
|
||||
CHECK(d.resyncs == 1);
|
||||
uint8_t buf[32];
|
||||
wp_status_t s;
|
||||
CHECK(wb_dev_vendor_in(&d, WP_REQ_GET_STATUS, 0, 0, buf, sizeof buf) > 0);
|
||||
memcpy(&s, buf, sizeof s);
|
||||
CHECK(s.resyncs == 1);
|
||||
}
|
||||
|
||||
// Runs every test; exit status 1 if any check failed.
|
||||
int main(void) {
|
||||
RUN(codec_clocks);
|
||||
RUN(codec_hybrid_routing);
|
||||
RUN(codec_sequence);
|
||||
RUN(codec_failures);
|
||||
RUN(packet_sizing);
|
||||
RUN(audio_rx);
|
||||
RUN(audio_tx);
|
||||
RUN(audio_loopback);
|
||||
RUN(dev_init_and_info);
|
||||
RUN(dev_rate);
|
||||
RUN(dev_settings);
|
||||
RUN(dev_register_access);
|
||||
RUN(dev_status);
|
||||
RUN(dev_leds);
|
||||
RUN(dev_events);
|
||||
printf("%d checks, %d failed\n", checks, failures);
|
||||
return failures ? 1 : 0;
|
||||
}
|
||||
Reference in new issue
Block a user