Compare commits
9
Commits
31edf91933
...
v0.3.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fabacf0a04 | ||
|
|
e255636f3a | ||
|
|
bb7f701f20 | ||
|
|
245c45c682 | ||
|
|
93a20cf8e1
|
||
|
|
9d66ee6ab8
|
||
|
|
ef39602b63
|
||
|
|
34df570dda
|
||
|
|
bdd946ad11
|
No files matched your search
@@ -0,0 +1,155 @@
|
|||||||
|
name: Build and Release Firmware
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- "v*"
|
||||||
|
|
||||||
|
env:
|
||||||
|
TARGET: thumbv8m.main-none-eabihf
|
||||||
|
# The catthehacker image installs rust here and only symlinks ~/.rustup and ~/.cargo to it.
|
||||||
|
# actions/cache archives the symlink, not its contents, so point at the real paths.
|
||||||
|
RUSTUP_HOME: /usr/share/rust/.rustup
|
||||||
|
CARGO_HOME: /usr/share/rust/.cargo
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
firmware:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: catthehacker/ubuntu:rust-latest
|
||||||
|
strategy:
|
||||||
|
fail-fast: true
|
||||||
|
matrix:
|
||||||
|
variant: [cs4398, ak4490, evk]
|
||||||
|
env:
|
||||||
|
FEATURES: ${{ matrix.variant }},hid
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Cache rustup
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: ${{ env.RUSTUP_HOME }}
|
||||||
|
key: rustup-firmware-${{ env.TARGET }}-${{ runner.os }}
|
||||||
|
|
||||||
|
- name: Install Rust Toolchain
|
||||||
|
run: |
|
||||||
|
rustup target install ${{ env.TARGET }}
|
||||||
|
rustup component add llvm-tools
|
||||||
|
|
||||||
|
- name: Cache cargo
|
||||||
|
uses: Swatinem/rust-cache@v2
|
||||||
|
with:
|
||||||
|
shared-key: firmware-${{ matrix.variant }}
|
||||||
|
|
||||||
|
- name: Install cargo-binutils
|
||||||
|
run: cargo binstall -y --locked cargo-binutils
|
||||||
|
|
||||||
|
- name: Build firmware (${{ matrix.variant }})
|
||||||
|
working-directory: firmware
|
||||||
|
run: cargo build --release --target ${{ env.TARGET }} --no-default-features --features "${{ env.FEATURES }}"
|
||||||
|
|
||||||
|
- name: Create hex file
|
||||||
|
working-directory: firmware
|
||||||
|
run: cargo objcopy --release --target ${{ env.TARGET }} --no-default-features --features "${{ env.FEATURES }}" -- -O ihex ../target/${{ env.TARGET }}/release/guac-${{ matrix.variant }}.hex
|
||||||
|
|
||||||
|
- name: Package
|
||||||
|
run: |
|
||||||
|
out=target/${{ env.TARGET }}/release
|
||||||
|
cp "$out/guac" "$out/guac-${{ matrix.variant }}.elf"
|
||||||
|
mkdir -p dist
|
||||||
|
zip -j "dist/guac-firmware-${{ matrix.variant }}.zip" "$out/guac-${{ matrix.variant }}.elf" "$out/guac-${{ matrix.variant }}.hex"
|
||||||
|
|
||||||
|
- name: Upload artifact
|
||||||
|
uses: actions/upload-artifact@v3
|
||||||
|
with:
|
||||||
|
name: firmware-${{ matrix.variant }}
|
||||||
|
path: dist/*.zip
|
||||||
|
|
||||||
|
cli:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: catthehacker/ubuntu:rust-latest
|
||||||
|
strategy:
|
||||||
|
fail-fast: true
|
||||||
|
matrix:
|
||||||
|
target:
|
||||||
|
- aarch64-unknown-linux-musl
|
||||||
|
- x86_64-pc-windows-gnu
|
||||||
|
- x86_64-unknown-linux-musl
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Cache rustup
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: ${{ env.RUSTUP_HOME }}
|
||||||
|
key: rustup-cli-${{ matrix.target }}-${{ runner.os }}
|
||||||
|
|
||||||
|
- name: Install Rust Toolchain
|
||||||
|
run: rustup target install ${{ matrix.target }}
|
||||||
|
|
||||||
|
- name: Cache cargo
|
||||||
|
uses: Swatinem/rust-cache@v2
|
||||||
|
with:
|
||||||
|
shared-key: cli-${{ matrix.target }}
|
||||||
|
|
||||||
|
- name: Cache zigbuild install
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: /opt/zigvenv
|
||||||
|
key: zigbuild-venv-${{ runner.os }}
|
||||||
|
|
||||||
|
# compiled libc/compiler-rt are per target, so these stay keyed on it
|
||||||
|
- name: Cache zig build artifacts
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
~/.cache/zig
|
||||||
|
~/.cache/cargo-zigbuild
|
||||||
|
key: zigbuild-cache-${{ matrix.target }}-${{ runner.os }}
|
||||||
|
|
||||||
|
- name: Install cargo-zigbuild Toolchain
|
||||||
|
run: |
|
||||||
|
# venv keeps cargo-zigbuild and ziglang in one relocatable-by-path directory we can cache
|
||||||
|
[ -d /opt/zigvenv ] || python3 -m venv /opt/zigvenv
|
||||||
|
/opt/zigvenv/bin/pip install cargo-zigbuild
|
||||||
|
echo /opt/zigvenv/bin >> "$GITHUB_PATH"
|
||||||
|
|
||||||
|
- name: Build cli (${{ matrix.target }})
|
||||||
|
working-directory: cli
|
||||||
|
run: cargo zigbuild --release --target ${{ matrix.target }}
|
||||||
|
|
||||||
|
- name: Package
|
||||||
|
run: |
|
||||||
|
bin=cli
|
||||||
|
[[ "${{ matrix.target }}" == *windows* ]] && bin=cli.exe
|
||||||
|
mkdir -p dist
|
||||||
|
zip -j "dist/guac-cli-${{ matrix.target }}.zip" "target/${{ matrix.target }}/release/$bin"
|
||||||
|
shell: bash
|
||||||
|
|
||||||
|
- name: Upload artifact
|
||||||
|
uses: actions/upload-artifact@v3
|
||||||
|
with:
|
||||||
|
name: cli-${{ matrix.target }}
|
||||||
|
path: dist/*.zip
|
||||||
|
|
||||||
|
release:
|
||||||
|
needs: [firmware, cli]
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: catthehacker/ubuntu:act-latest
|
||||||
|
steps:
|
||||||
|
- name: Download artifacts
|
||||||
|
uses: actions/download-artifact@v3
|
||||||
|
with:
|
||||||
|
path: dist
|
||||||
|
|
||||||
|
- name: Create Gitea Release
|
||||||
|
uses: akkuman/gitea-release-action@v1
|
||||||
|
with:
|
||||||
|
files: dist/**/*.zip
|
||||||
|
draft: false
|
||||||
|
prerelease: false
|
||||||
Generated
+52
-39
@@ -173,6 +173,17 @@ version = "0.14.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2d7e60934ceec538daadb9d8432424ed043a904d8e0243f3c6446bce549a46ac"
|
checksum = "2d7e60934ceec538daadb9d8432424ed043a904d8e0243f3c6446bce549a46ac"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bitfield-struct"
|
||||||
|
version = "0.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3ca6739863c590881f038d033a146c51ddae239186a4327014839fd864f44ed5"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "1.3.2"
|
version = "1.3.2"
|
||||||
@@ -217,9 +228,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bytemuck"
|
name = "bytemuck"
|
||||||
version = "1.25.0"
|
version = "1.25.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c8efb64bd706a16a1bdde310ae86b351e4d21550d98d056f22f8a7f7a2183fec"
|
checksum = "95832e849adfb21180ccb6826a99da14e5d266ae5c2e668e1602cf234f153797"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bytemuck_derive",
|
"bytemuck_derive",
|
||||||
]
|
]
|
||||||
@@ -325,7 +336,7 @@ checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cli"
|
name = "cli"
|
||||||
version = "0.1.0"
|
version = "0.3.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-hid",
|
"async-hid",
|
||||||
"clap",
|
"clap",
|
||||||
@@ -515,14 +526,14 @@ version = "0.3.100"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f0963443817029b2024136fc4dd07a5107eb8f977eaf18fcd1fdeb11306b64ad"
|
checksum = "f0963443817029b2024136fc4dd07a5107eb8f977eaf18fcd1fdeb11306b64ad"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"defmt 1.1.0",
|
"defmt 1.1.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "defmt"
|
name = "defmt"
|
||||||
version = "1.1.0"
|
version = "1.1.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a6e524506490a1953d237cb87b1cfc1e46f88c18f10a22dfe0f507dc6bfc7f7f"
|
checksum = "e2953bfe4f93bbd20cc71198842756f77d161884c99ebbabc41d80231ded88d1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 1.3.2",
|
"bitflags 1.3.2",
|
||||||
"defmt-macros",
|
"defmt-macros",
|
||||||
@@ -530,12 +541,11 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "defmt-macros"
|
name = "defmt-macros"
|
||||||
version = "1.1.0"
|
version = "1.1.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f0a27770e9c8f719a79d8b638281f4d828f77d8fd61e0bd94451b9b85e576a0b"
|
checksum = "bad9c72e7ca2137e0dc3813245a0d282fd6daad32fd800af018306a9169b5fe8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"defmt-parser",
|
"defmt-parser",
|
||||||
"proc-macro-error2",
|
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn",
|
||||||
@@ -557,7 +567,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "c0f73a4a4a91609e977ae3b7bd831ffa292edfd42ad140a3244a61d805b0e05e"
|
checksum = "c0f73a4a4a91609e977ae3b7bd831ffa292edfd42ad140a3244a61d805b0e05e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"critical-section",
|
"critical-section",
|
||||||
"defmt 1.1.0",
|
"defmt 1.1.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -757,15 +767,16 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "guac"
|
name = "guac"
|
||||||
version = "0.1.0"
|
version = "0.3.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"atomic",
|
"atomic",
|
||||||
"bbqueue",
|
"bbqueue",
|
||||||
"bytemuck",
|
"bitfield-struct",
|
||||||
"cortex-m",
|
"cortex-m",
|
||||||
"cortex-m-rt",
|
"cortex-m-rt",
|
||||||
"defmt 1.1.0",
|
"defmt 1.1.1",
|
||||||
"defmt-rtt",
|
"defmt-rtt",
|
||||||
|
"deku",
|
||||||
"embedded-hal",
|
"embedded-hal",
|
||||||
"embedded-io",
|
"embedded-io",
|
||||||
"log-to-defmt",
|
"log-to-defmt",
|
||||||
@@ -1178,6 +1189,27 @@ dependencies = [
|
|||||||
"autocfg",
|
"autocfg",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num_enum"
|
||||||
|
version = "0.7.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5d0bca838442ec211fa11de3a8b0e0e8f3a4522575b5c4c06ed722e005036f26"
|
||||||
|
dependencies = [
|
||||||
|
"num_enum_derive",
|
||||||
|
"rustversion",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num_enum_derive"
|
||||||
|
version = "0.7.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "680998035259dcfcafe653688bf2aa6d3e2dc05e98be6ab46afb089dc84f1df8"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "objc2"
|
name = "objc2"
|
||||||
version = "0.6.4"
|
version = "0.6.4"
|
||||||
@@ -1245,7 +1277,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "fd402d00b0fb94c5aee000029204a46884b1262e0c443f166d86d2c0747e1a1a"
|
checksum = "fd402d00b0fb94c5aee000029204a46884b1262e0c443f166d86d2c0747e1a1a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cortex-m",
|
"cortex-m",
|
||||||
"defmt 1.1.0",
|
"defmt 1.1.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1338,28 +1370,6 @@ dependencies = [
|
|||||||
"toml_edit",
|
"toml_edit",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "proc-macro-error-attr2"
|
|
||||||
version = "2.0.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "96de42df36bb9bba5542fe9f1a054b8cc87e172759a1868aa05c1f3acc89dfc5"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "proc-macro-error2"
|
|
||||||
version = "2.0.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "11ec05c52be0a07b08061f7dd003e7d7092e0472bc731b4af7bb1ef876109802"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro-error-attr2",
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.106"
|
version = "1.0.106"
|
||||||
@@ -1538,7 +1548,10 @@ dependencies = [
|
|||||||
name = "shared"
|
name = "shared"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"bytemuck",
|
||||||
|
"defmt 1.1.1",
|
||||||
"deku",
|
"deku",
|
||||||
|
"num_enum",
|
||||||
"serde",
|
"serde",
|
||||||
"usbd-hid",
|
"usbd-hid",
|
||||||
]
|
]
|
||||||
@@ -1794,12 +1807,12 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "usbd-uac2"
|
name = "usbd-uac2"
|
||||||
version = "0.1.0"
|
version = "0.1.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "117c289dcd316caa7aca9c0909117d8cf9d35f3ed2e7a5739067f0bcedc93e35"
|
checksum = "07c0e506a7eadf9dee3039a4fa00dee84b8ed0c4a5931424b81659e0800c4965"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"byteorder-embedded-io",
|
"byteorder-embedded-io",
|
||||||
"defmt 1.1.0",
|
"defmt 1.1.1",
|
||||||
"embedded-io",
|
"embedded-io",
|
||||||
"modular-bitfield",
|
"modular-bitfield",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "cli"
|
name = "cli"
|
||||||
version = "0.1.0"
|
version = "0.3.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
@@ -12,4 +12,4 @@ deku = "0.20.3"
|
|||||||
futures-lite = "2.6.1"
|
futures-lite = "2.6.1"
|
||||||
log = { version = "0.4.29", features = ["std"] }
|
log = { version = "0.4.29", features = ["std"] }
|
||||||
pollster = { version = "0.4.0", features = ["macro"] }
|
pollster = { version = "0.4.0", features = ["macro"] }
|
||||||
shared = { path = "../shared", features = ["serde"] }
|
shared = { path = "../shared", features = ["serde", "std"] }
|
||||||
+71
-26
@@ -1,10 +1,13 @@
|
|||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
use async_hid::{AsyncHidRead, HidBackend, HidResult};
|
use async_hid::{AsyncHidRead, AsyncHidWrite, Device, HidBackend, HidResult};
|
||||||
use clap::{Parser, ValueEnum};
|
use clap::{Parser, Subcommand, ValueEnum};
|
||||||
use deku::DekuContainerRead;
|
use deku::DekuContainerRead;
|
||||||
use futures_lite::StreamExt;
|
use futures_lite::StreamExt;
|
||||||
use shared::hid::AudioTelemetryReport;
|
use shared::{
|
||||||
|
AudioDeviceCommand, AudioTelemetrySnapshot,
|
||||||
|
hid::{AudioDeviceCommandReport, AudioTelemetryReport, USAGE_COMMAND, USAGE_TELEMETRY},
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, ValueEnum)]
|
#[derive(Clone, Copy, Debug, ValueEnum)]
|
||||||
enum Format {
|
enum Format {
|
||||||
@@ -12,18 +15,29 @@ enum Format {
|
|||||||
Csv,
|
Csv,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Subcommand, Clone, Copy, Debug)]
|
||||||
|
enum Command {
|
||||||
|
Monitor,
|
||||||
|
Reset {
|
||||||
|
#[arg(short, long, default_value_t = false)]
|
||||||
|
bootloader: bool,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
#[command(version, about)]
|
#[command(version, about)]
|
||||||
struct Args {
|
struct Args {
|
||||||
#[arg(short, long, default_value = "debug")]
|
#[arg(short, long, default_value = "debug")]
|
||||||
format: Format,
|
format: Format,
|
||||||
|
#[command(subcommand)]
|
||||||
|
command: Option<Command>,
|
||||||
}
|
}
|
||||||
|
|
||||||
trait StateEmitter<W: io::Write> {
|
trait StateEmitter<W: io::Write> {
|
||||||
fn from_writer(writer: W) -> Self
|
fn from_writer(writer: W) -> Self
|
||||||
where
|
where
|
||||||
Self: Sized;
|
Self: Sized;
|
||||||
fn emit(&mut self, r: &AudioTelemetryReport);
|
fn emit(&mut self, r: &AudioTelemetrySnapshot);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DebugEmitter<T: io::Write> {
|
struct DebugEmitter<T: io::Write> {
|
||||||
@@ -34,8 +48,8 @@ impl<W: io::Write> StateEmitter<W> for DebugEmitter<W> {
|
|||||||
fn from_writer(writer: W) -> Self {
|
fn from_writer(writer: W) -> Self {
|
||||||
Self { writer }
|
Self { writer }
|
||||||
}
|
}
|
||||||
fn emit(&mut self, r: &AudioTelemetryReport) {
|
fn emit(&mut self, r: &AudioTelemetrySnapshot) {
|
||||||
writeln!(self.writer, "{r:?}");
|
writeln!(self.writer, "{r:?}").ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,7 +63,7 @@ impl<W: io::Write> StateEmitter<W> for CsvEmitter<W> {
|
|||||||
csv: csv::Writer::from_writer(writer),
|
csv: csv::Writer::from_writer(writer),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn emit(&mut self, r: &AudioTelemetryReport) {
|
fn emit(&mut self, r: &AudioTelemetrySnapshot) {
|
||||||
if let Err(e) = self.csv.serialize(r) {
|
if let Err(e) = self.csv.serialize(r) {
|
||||||
eprintln!("Serialization error: {e:?}");
|
eprintln!("Serialization error: {e:?}");
|
||||||
} else {
|
} else {
|
||||||
@@ -58,31 +72,62 @@ impl<W: io::Write> StateEmitter<W> for CsvEmitter<W> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The device exposes one vendor HID interface per usage; pick the one we need.
|
||||||
|
async fn find_device(usage_id: u16) -> HidResult<Device> {
|
||||||
|
Ok(HidBackend::default()
|
||||||
|
.enumerate()
|
||||||
|
.await?
|
||||||
|
.find(|d| d.product_id == 0xcc1d && d.vendor_id == 0x1209 && d.usage_id == usage_id)
|
||||||
|
.await
|
||||||
|
.expect("GUAC device not found or not accessible (try as root?)"))
|
||||||
|
}
|
||||||
|
|
||||||
#[pollster::main]
|
#[pollster::main]
|
||||||
async fn main() -> HidResult<()> {
|
async fn main() -> HidResult<()> {
|
||||||
colog::init();
|
colog::init();
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
let usbhid = HidBackend::default();
|
|
||||||
let dev = usbhid
|
|
||||||
.enumerate()
|
|
||||||
.await?
|
|
||||||
.find(|d| d.product_id == 0xcc1d && d.vendor_id == 0x1209)
|
|
||||||
.await
|
|
||||||
.expect("GUAC device not found or not accessible (try as root?)");
|
|
||||||
let mut reader = dev.open_readable().await?;
|
|
||||||
let mut writer: Box<dyn StateEmitter<_>> = match args.format {
|
|
||||||
Format::Debug => Box::new(DebugEmitter::from_writer(io::stdout())),
|
|
||||||
Format::Csv => Box::new(CsvEmitter::from_writer(io::stdout())),
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut buf = [0u8; core::mem::size_of::<AudioTelemetryReport>()];
|
match &args.command {
|
||||||
while let Ok(r) = reader.read_input_report(&mut buf).await {
|
Some(Command::Monitor) => {
|
||||||
log::debug!("read {}: {:?}", r, &buf[..r]);
|
let dev = find_device(USAGE_TELEMETRY).await?;
|
||||||
let buf = &buf[..r];
|
let mut reader = dev.open_readable().await?;
|
||||||
match AudioTelemetryReport::from_bytes((buf, 0)) {
|
let mut writer: Box<dyn StateEmitter<_>> = match args.format {
|
||||||
Ok((_, r)) => writer.emit(&r),
|
Format::Debug => Box::new(DebugEmitter::from_writer(io::stdout())),
|
||||||
Err(e) => eprintln!("Unable to parse report: {:?}", e),
|
Format::Csv => Box::new(CsvEmitter::from_writer(io::stdout())),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut buf = [0u8; core::mem::size_of::<AudioTelemetryReport>()];
|
||||||
|
while let Ok(r) = reader.read_input_report(&mut buf).await {
|
||||||
|
log::debug!("read {}: {:?}", r, &buf[..r]);
|
||||||
|
let buf = &buf[..r];
|
||||||
|
match AudioTelemetryReport::from_bytes((buf, 0)) {
|
||||||
|
Ok((_, r)) => writer.emit(&r.into()),
|
||||||
|
Err(e) => eprintln!("Unable to parse report: {:?}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Some(Command::Reset { bootloader }) => {
|
||||||
|
let dev = find_device(USAGE_COMMAND).await?;
|
||||||
|
let mut dev_writer = dev.open_writeable().await?;
|
||||||
|
let cmd = if *bootloader {
|
||||||
|
print!("Rebooting into bootloader...");
|
||||||
|
AudioDeviceCommand::Bootloader
|
||||||
|
} else {
|
||||||
|
print!("Rebooting...");
|
||||||
|
AudioDeviceCommand::Reset
|
||||||
|
};
|
||||||
|
log::debug!("writing command: {:?}", &cmd);
|
||||||
|
let report: AudioDeviceCommandReport = cmd.into();
|
||||||
|
log::debug!(" report: {:?}", &report);
|
||||||
|
// Our descriptor has no report IDs, so hidraw expects a leading 0x00 that it strips
|
||||||
|
let mut buf = [0u8; 1 + core::mem::size_of::<AudioDeviceCommandReport>()];
|
||||||
|
buf[1..].copy_from_slice(&report.cmd);
|
||||||
|
match dev_writer.write_output_report(&buf).await {
|
||||||
|
Ok(_) => println!("write succeeded - reboot failed"),
|
||||||
|
Err(_) => println!("lost device - reboot successful"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
+7
-6
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "guac"
|
name = "guac"
|
||||||
version = "0.1.0"
|
version = "0.3.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
@@ -9,18 +9,18 @@ ak4490 = []
|
|||||||
cs4398 = []
|
cs4398 = []
|
||||||
nodac = []
|
nodac = []
|
||||||
wm8904 = []
|
wm8904 = []
|
||||||
hid = [ "dep:usbd-hid", "dep:shared" ]
|
hid = ["dep:usbd-hid", "dep:deku"]
|
||||||
evk = [ "wm8904" ]
|
evk = ["wm8904"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
shared = { path="../shared", optional = true }
|
shared = { path = "../shared" }
|
||||||
atomic = "0.6.1"
|
atomic = "0.6.1"
|
||||||
bbqueue = "0.7.0"
|
bbqueue = "0.7.0"
|
||||||
bytemuck = { version = "1.25.0", features = ["derive"] }
|
|
||||||
cortex-m = { version = "0.7.7", features = ["critical-section-single-core"] }
|
cortex-m = { version = "0.7.7", features = ["critical-section-single-core"] }
|
||||||
cortex-m-rt = "0.7.5"
|
cortex-m-rt = "0.7.5"
|
||||||
defmt = "1.0.1"
|
defmt = "1.0.1"
|
||||||
defmt-rtt = "1.1.0"
|
defmt-rtt = "1.1.0"
|
||||||
|
deku = { version = "0.20.3", optional = true, default-features = false }
|
||||||
embedded-hal = "0.2.7"
|
embedded-hal = "0.2.7"
|
||||||
embedded-io = "0.7.1"
|
embedded-io = "0.7.1"
|
||||||
log-to-defmt = "0.1.0"
|
log-to-defmt = "0.1.0"
|
||||||
@@ -32,7 +32,8 @@ panic-probe = { version = "1.0.0", features = ["print-defmt"] }
|
|||||||
static_cell = "2.1.1"
|
static_cell = "2.1.1"
|
||||||
usb-device = { version = "0.3", features = ["control-buffer-256"] }
|
usb-device = { version = "0.3", features = ["control-buffer-256"] }
|
||||||
usbd-hid = { version = "0.10.0", optional = true }
|
usbd-hid = { version = "0.10.0", optional = true }
|
||||||
usbd-uac2 = { version = "0.1.0", features = ["defmt"]}
|
usbd-uac2 = { version = "0.1.2", features = ["defmt"] }
|
||||||
|
bitfield-struct = "0.13.0"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
opt-level = "z"
|
opt-level = "z"
|
||||||
|
|||||||
@@ -130,7 +130,7 @@ where
|
|||||||
fn cr1_for_rate(&self, rate: u32) -> u16 {
|
fn cr1_for_rate(&self, rate: u32) -> u16 {
|
||||||
let fs_ratio = self.mclk / rate;
|
let fs_ratio = self.mclk / rate;
|
||||||
if !self.mclk.is_multiple_of(rate) {
|
if !self.mclk.is_multiple_of(rate) {
|
||||||
defmt::warn!("sample rate should be a multiple of mclk");
|
defmt::warn!("[wm8904] sample rate should be a multiple of mclk");
|
||||||
}
|
}
|
||||||
let clk_sys_rate: u16 = match fs_ratio {
|
let clk_sys_rate: u16 = match fs_ratio {
|
||||||
64 => 0,
|
64 => 0,
|
||||||
@@ -144,7 +144,7 @@ where
|
|||||||
1408 => 8,
|
1408 => 8,
|
||||||
1536 => 9,
|
1536 => 9,
|
||||||
_ => {
|
_ => {
|
||||||
defmt::warn!("unsupport ratio {}", fs_ratio);
|
defmt::warn!("[wm8904] unsupport ratio {}", fs_ratio);
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -181,26 +181,30 @@ where
|
|||||||
match self.i2c.write_read(WM8904_I2C_ADDRESS, &[0], &mut buf) {
|
match self.i2c.write_read(WM8904_I2C_ADDRESS, &[0], &mut buf) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
let chip_id = ((buf[0] as u16) << 8) | buf[1] as u16;
|
let chip_id = ((buf[0] as u16) << 8) | buf[1] as u16;
|
||||||
defmt::info!("Read chip ID: {:x}", chip_id)
|
defmt::info!("[wm8904] Read chip ID: {:x}", chip_id)
|
||||||
}
|
}
|
||||||
Err(_) => defmt::error!("Error reading I2C"),
|
Err(_) => defmt::error!("[wm8904] Error reading I2C"),
|
||||||
}
|
}
|
||||||
|
|
||||||
self.write_reg(RegisterAddress::ClockRates2, 0x000f); // OPCLK_ENA | CLK_SYS_ENA | CLK_DSP_ENA | TOCLK_ENA
|
self.write_reg(RegisterAddress::ClockRates2, 0x000f); // OPCLK_ENA | CLK_SYS_ENA | CLK_DSP_ENA | TOCLK_ENA
|
||||||
self.write_reg(RegisterAddress::WriteSeq0, 0x0100); // write sequencer 0 ENA
|
self.write_reg(RegisterAddress::WriteSeq0, 0x0100); // write sequencer 0 ENA
|
||||||
self.write_reg(RegisterAddress::WriteSeq3, 0x0100); // write sequencer 3 START, INDEX=0
|
self.write_reg(RegisterAddress::WriteSeq3, 0x0100); // write sequencer 3 START, INDEX=0
|
||||||
// wait on write sequencer
|
// wait on write sequencer
|
||||||
defmt::info!("[codec] waiting on write seq");
|
defmt::info!("[wm8904] waiting on write seq");
|
||||||
loop {
|
loop {
|
||||||
let mut buf = [0; 2];
|
let mut buf = [0; 2];
|
||||||
self.i2c
|
self.i2c
|
||||||
.write_read(WM8904_I2C_ADDRESS, &[0x70], &mut buf)
|
.write_read(
|
||||||
|
WM8904_I2C_ADDRESS,
|
||||||
|
&[RegisterAddress::WriteSeq4 as u8],
|
||||||
|
&mut buf,
|
||||||
|
)
|
||||||
.ok();
|
.ok();
|
||||||
if buf[1] & 1 == 0 {
|
if buf[1] & 1 == 0 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
defmt::debug!("[codec] write seq done");
|
defmt::debug!("[wm8904] write seq done");
|
||||||
self.write_reg(RegisterAddress::ClockRates0, 0);
|
self.write_reg(RegisterAddress::ClockRates0, 0);
|
||||||
self.write_reg(RegisterAddress::PowerMgmt0, 0); // IN PGAs disabled
|
self.write_reg(RegisterAddress::PowerMgmt0, 0); // IN PGAs disabled
|
||||||
self.write_reg(RegisterAddress::PowerMgmt2, 0x0003); // HPL_PGA_ENA | HPR_PGA_ENA
|
self.write_reg(RegisterAddress::PowerMgmt2, 0x0003); // HPL_PGA_ENA | HPR_PGA_ENA
|
||||||
@@ -236,7 +240,7 @@ where
|
|||||||
}
|
}
|
||||||
fn change_rate(&mut self, new_rate: u32) {
|
fn change_rate(&mut self, new_rate: u32) {
|
||||||
// TODO: mute, stop clocks etc.
|
// TODO: mute, stop clocks etc.
|
||||||
defmt::info!("dac rate -> {}", new_rate);
|
defmt::info!("[wm8904] dac rate -> {}", new_rate);
|
||||||
self.write_reg(RegisterAddress::ClockRates1, self.cr1_for_rate(new_rate));
|
self.write_reg(RegisterAddress::ClockRates1, self.cr1_for_rate(new_rate));
|
||||||
self.write_reg(
|
self.write_reg(
|
||||||
RegisterAddress::AudioInterface2,
|
RegisterAddress::AudioInterface2,
|
||||||
|
|||||||
+113
-114
@@ -1,3 +1,4 @@
|
|||||||
|
use bitfield_struct::bitfield;
|
||||||
use hal::Syscon;
|
use hal::Syscon;
|
||||||
use hal::peripherals::syscon::ClockControl;
|
use hal::peripherals::syscon::ClockControl;
|
||||||
|
|
||||||
@@ -7,8 +8,6 @@ use core::convert::Infallible;
|
|||||||
use core::ptr::copy_nonoverlapping;
|
use core::ptr::copy_nonoverlapping;
|
||||||
use core::sync::atomic::{AtomicUsize, Ordering, compiler_fence};
|
use core::sync::atomic::{AtomicUsize, Ordering, compiler_fence};
|
||||||
|
|
||||||
pub const DMA0_FLEXCOMM7_TX: u8 = 19;
|
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct DmaDescriptor {
|
pub struct DmaDescriptor {
|
||||||
@@ -18,6 +17,56 @@ pub struct DmaDescriptor {
|
|||||||
pub next: *const DmaDescriptor,
|
pub next: *const DmaDescriptor,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fn encode_transfers(transfers: u32) -> u16 {
|
||||||
|
(transfers.saturating_sub(1) & 0x3ff) as u16
|
||||||
|
}
|
||||||
|
|
||||||
|
const fn decode_transfers(raw_bits: u16) -> u32 {
|
||||||
|
(raw_bits as u32) + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
/// WIDTH code: 0 = 8-bit, 1 = 16-bit, 2 = 32-bit
|
||||||
|
const fn encode_width(width_bytes: u8) -> u8 {
|
||||||
|
match width_bytes {
|
||||||
|
2 => 1,
|
||||||
|
4 => 2,
|
||||||
|
_ => 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const fn decode_width(raw_bits: u8) -> u8 {
|
||||||
|
match raw_bits {
|
||||||
|
1 => 2,
|
||||||
|
2 => 4,
|
||||||
|
_ => 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[bitfield(u32)]
|
||||||
|
#[derive(PartialEq, Eq)]
|
||||||
|
pub struct XferCfg {
|
||||||
|
pub cfgvalid: bool, // Bit 0
|
||||||
|
pub reload: bool, // Bit 1
|
||||||
|
pub swtrig: bool, // Bit 2
|
||||||
|
pub clrtrig: bool, // Bit 3
|
||||||
|
pub inta: bool, // Bit 4
|
||||||
|
pub intb: bool, // Bit 5
|
||||||
|
#[bits(2)]
|
||||||
|
_pad1: u8, // Bits 6-7 (unused padding)
|
||||||
|
#[bits(2, from = decode_width, into = encode_width)]
|
||||||
|
pub width: u8, // Bits 8-9, in bytes (1/2/4)
|
||||||
|
#[bits(2)]
|
||||||
|
_pad2: u8, // Bits 10-11 (unused padding)
|
||||||
|
#[bits(2)]
|
||||||
|
pub src_inc: u8, // Bits 12-13
|
||||||
|
#[bits(2)]
|
||||||
|
pub dst_inc: u8, // Bits 14-15
|
||||||
|
#[bits(10, from = decode_transfers, into = encode_transfers)]
|
||||||
|
pub transfers: u32, // Bits 16-25 (fits 0x3FF max)
|
||||||
|
#[bits(6)]
|
||||||
|
_pad3: u8, // Bits 26-31 (unused padding)
|
||||||
|
}
|
||||||
|
|
||||||
impl defmt::Format for DmaDescriptor {
|
impl defmt::Format for DmaDescriptor {
|
||||||
fn format(&self, fmt: defmt::Formatter) {
|
fn format(&self, fmt: defmt::Formatter) {
|
||||||
defmt::write!(
|
defmt::write!(
|
||||||
@@ -48,7 +97,7 @@ pub struct PushResult {
|
|||||||
pub dropped: usize,
|
pub dropped: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
#[derive(Debug, Copy, Clone, defmt::Format, Eq, PartialEq)]
|
||||||
pub enum ConfigError {
|
pub enum ConfigError {
|
||||||
SlotTooLarge,
|
SlotTooLarge,
|
||||||
SlotTooSmall,
|
SlotTooSmall,
|
||||||
@@ -70,29 +119,22 @@ impl core::fmt::Display for DmaError {
|
|||||||
/// Slot-based DMA ring
|
/// Slot-based DMA ring
|
||||||
pub struct DmaRing<const N: usize, const MAX_SLOT_BYTES: usize> {
|
pub struct DmaRing<const N: usize, const MAX_SLOT_BYTES: usize> {
|
||||||
dma: pac::DMA0,
|
dma: pac::DMA0,
|
||||||
|
|
||||||
/// Destination peripheral register (FIFO write register)
|
|
||||||
dst_reg: *mut u32,
|
dst_reg: *mut u32,
|
||||||
|
|
||||||
// SAFETY: only written by USB task (on start)
|
|
||||||
pub(crate) channel_desc: UnsafeCell<DescriptorTable>,
|
pub(crate) channel_desc: UnsafeCell<DescriptorTable>,
|
||||||
// SAFETY: only written by USB task (on start)
|
|
||||||
pub(crate) desc: UnsafeCell<RingDescriptors<N>>,
|
pub(crate) desc: UnsafeCell<RingDescriptors<N>>,
|
||||||
slots: UnsafeCell<[[u8; MAX_SLOT_BYTES]; N]>,
|
slots: UnsafeCell<[[u8; MAX_SLOT_BYTES]; N]>,
|
||||||
|
|
||||||
/// Effective bytes per slot. Maybe be smaller than MAX_SLOT_BYTES (e.g. at lower sample rates), as the setup is designed for constant rate not constant size.
|
/// Effective bytes per slot (atomic for interior mutability)
|
||||||
slot_bytes: usize,
|
slot_bytes: AtomicUsize,
|
||||||
/// How many bytes to transfer to the FIFO
|
word_bytes: u8,
|
||||||
word_bytes: usize,
|
|
||||||
|
|
||||||
// SAFETY: producer only
|
|
||||||
write_slot: UnsafeCell<usize>,
|
write_slot: UnsafeCell<usize>,
|
||||||
write_off: UnsafeCell<usize>,
|
write_off: UnsafeCell<usize>,
|
||||||
|
|
||||||
produced: AtomicUsize,
|
produced: AtomicUsize,
|
||||||
consumed: AtomicUsize,
|
consumed: AtomicUsize,
|
||||||
|
|
||||||
/// Leave at least one slot empty so producer never overwrites a slot DMA may still read.
|
|
||||||
safety_gap: usize,
|
safety_gap: usize,
|
||||||
pub produced_bytes: AtomicUsize,
|
pub produced_bytes: AtomicUsize,
|
||||||
pub consumed_bytes: AtomicUsize,
|
pub consumed_bytes: AtomicUsize,
|
||||||
@@ -104,7 +146,7 @@ impl<const N: usize, const MAX_SLOT_BYTES: usize> DmaRing<N, MAX_SLOT_BYTES> {
|
|||||||
dma: pac::DMA0,
|
dma: pac::DMA0,
|
||||||
syscon: &mut Syscon,
|
syscon: &mut Syscon,
|
||||||
dst_reg: *mut u32,
|
dst_reg: *mut u32,
|
||||||
word_bytes: usize,
|
word_bytes: u8,
|
||||||
) -> Result<Self, ConfigError> {
|
) -> Result<Self, ConfigError> {
|
||||||
if word_bytes != 1 && word_bytes != 2 && word_bytes != 4 {
|
if word_bytes != 1 && word_bytes != 2 && word_bytes != 4 {
|
||||||
return Err(ConfigError::UnsupportedWidth);
|
return Err(ConfigError::UnsupportedWidth);
|
||||||
@@ -114,7 +156,7 @@ impl<const N: usize, const MAX_SLOT_BYTES: usize> DmaRing<N, MAX_SLOT_BYTES> {
|
|||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
dma,
|
dma,
|
||||||
dst_reg: dst_reg,
|
dst_reg,
|
||||||
channel_desc: UnsafeCell::new(DescriptorTable {
|
channel_desc: UnsafeCell::new(DescriptorTable {
|
||||||
d: [DmaDescriptor {
|
d: [DmaDescriptor {
|
||||||
xfercfg: 0,
|
xfercfg: 0,
|
||||||
@@ -132,7 +174,7 @@ impl<const N: usize, const MAX_SLOT_BYTES: usize> DmaRing<N, MAX_SLOT_BYTES> {
|
|||||||
}; N],
|
}; N],
|
||||||
}),
|
}),
|
||||||
slots: UnsafeCell::new([[0u8; MAX_SLOT_BYTES]; N]),
|
slots: UnsafeCell::new([[0u8; MAX_SLOT_BYTES]; N]),
|
||||||
slot_bytes: MAX_SLOT_BYTES,
|
slot_bytes: AtomicUsize::new(MAX_SLOT_BYTES),
|
||||||
word_bytes,
|
word_bytes,
|
||||||
write_slot: UnsafeCell::new(0),
|
write_slot: UnsafeCell::new(0),
|
||||||
write_off: UnsafeCell::new(0),
|
write_off: UnsafeCell::new(0),
|
||||||
@@ -145,24 +187,31 @@ impl<const N: usize, const MAX_SLOT_BYTES: usize> DmaRing<N, MAX_SLOT_BYTES> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Optional: adjust safety gap (defaults to 1 empty slot).
|
/// Optional: adjust safety gap (defaults to 1 empty slot).
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn set_safety_gap(&mut self, gap_slots: usize) {
|
pub fn set_safety_gap(&mut self, gap_slots: usize) {
|
||||||
self.safety_gap = gap_slots.min(N);
|
self.safety_gap = gap_slots.min(N);
|
||||||
}
|
}
|
||||||
pub fn slot_size(&self) -> usize {
|
pub fn slot_size(&self) -> usize {
|
||||||
self.slot_bytes
|
self.slot_bytes.load(Ordering::Acquire)
|
||||||
}
|
}
|
||||||
pub fn set_slot_size(&mut self, slot_bytes: usize) -> Result<(), ConfigError> {
|
|
||||||
|
pub fn set_slot_size(&self, slot_bytes: usize) -> Result<(), ConfigError> {
|
||||||
if slot_bytes == 0 {
|
if slot_bytes == 0 {
|
||||||
return Err(ConfigError::SlotTooSmall);
|
return Err(ConfigError::SlotTooSmall);
|
||||||
}
|
}
|
||||||
if slot_bytes > MAX_SLOT_BYTES {
|
if slot_bytes > MAX_SLOT_BYTES {
|
||||||
return Err(ConfigError::SlotTooLarge);
|
return Err(ConfigError::SlotTooLarge);
|
||||||
}
|
}
|
||||||
if slot_bytes % self.word_bytes != 0 {
|
if !slot_bytes.is_multiple_of(self.word_bytes as usize) {
|
||||||
return Err(ConfigError::SlotNotAligned);
|
return Err(ConfigError::SlotNotAligned);
|
||||||
}
|
}
|
||||||
self.slot_bytes = slot_bytes;
|
|
||||||
self.reset_producer();
|
// Update atomic size
|
||||||
|
self.slot_bytes.store(slot_bytes, Ordering::Release);
|
||||||
|
|
||||||
|
// Re-initialize descriptors and reset producer state safely through internal mutability
|
||||||
|
self.init_descriptors();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,7 +234,8 @@ impl<const N: usize, const MAX_SLOT_BYTES: usize> DmaRing<N, MAX_SLOT_BYTES> {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
let cap = self.slot_bytes - *write_off;
|
let slot_bytes = self.slot_bytes.load(Ordering::Acquire);
|
||||||
|
let cap = slot_bytes - *write_off;
|
||||||
let n = core::cmp::min(cap, data.len());
|
let n = core::cmp::min(cap, data.len());
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
@@ -197,7 +247,7 @@ impl<const N: usize, const MAX_SLOT_BYTES: usize> DmaRing<N, MAX_SLOT_BYTES> {
|
|||||||
written += n;
|
written += n;
|
||||||
data = &data[n..];
|
data = &data[n..];
|
||||||
|
|
||||||
if *write_off == self.slot_bytes {
|
if *write_off == slot_bytes {
|
||||||
// publish completed slot
|
// publish completed slot
|
||||||
compiler_fence(Ordering::Release);
|
compiler_fence(Ordering::Release);
|
||||||
self.produced.fetch_add(1, Ordering::Release);
|
self.produced.fetch_add(1, Ordering::Release);
|
||||||
@@ -221,8 +271,9 @@ impl<const N: usize, const MAX_SLOT_BYTES: usize> DmaRing<N, MAX_SLOT_BYTES> {
|
|||||||
let consumed = self.consumed.load(Ordering::Relaxed);
|
let consumed = self.consumed.load(Ordering::Relaxed);
|
||||||
if consumed < produced {
|
if consumed < produced {
|
||||||
self.consumed.fetch_add(slots, Ordering::Release);
|
self.consumed.fetch_add(slots, Ordering::Release);
|
||||||
|
let slot_bytes = self.slot_bytes.load(Ordering::Acquire);
|
||||||
self.consumed_bytes
|
self.consumed_bytes
|
||||||
.fetch_add(slots * self.slot_bytes, Ordering::Relaxed);
|
.fetch_add(slots * slot_bytes, Ordering::Relaxed);
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
defmt::error!("DMA underrun!");
|
defmt::error!("DMA underrun!");
|
||||||
@@ -243,25 +294,23 @@ impl<const N: usize, const MAX_SLOT_BYTES: usize> DmaRing<N, MAX_SLOT_BYTES> {
|
|||||||
loop {
|
loop {
|
||||||
let consumed_start = self.consumed.load(Ordering::Acquire);
|
let consumed_start = self.consumed.load(Ordering::Acquire);
|
||||||
|
|
||||||
let reg_1 = self.dma.channel19.xfercfg.read().bits() as usize >> 16 & 0x3ff;
|
let reg_1 = (self.dma.channel19.xfercfg.read().bits() >> 16) & 0x3FF;
|
||||||
let reg_2 = self.dma.channel19.xfercfg.read().bits() as usize >> 16 & 0x3ff;
|
let reg_2 = (self.dma.channel19.xfercfg.read().bits() >> 16) & 0x3FF;
|
||||||
|
|
||||||
let consumed_end = self.consumed.load(Ordering::Acquire);
|
let consumed_end = self.consumed.load(Ordering::Acquire);
|
||||||
|
|
||||||
if consumed_start == consumed_end && reg_1 == reg_2 {
|
if consumed_start == consumed_end && reg_1 == reg_2 {
|
||||||
// 1. Map the hardware remaining countdown into a clean byte count
|
let remaining_bytes = if reg_1 == 0x3FF {
|
||||||
let remaining_bytes = if reg_1 == 0x3ff {
|
0
|
||||||
0 // 0x3FF means all transfers completed, 0 bytes remaining
|
|
||||||
} else {
|
} else {
|
||||||
// Formula from NXP manual: (XFERCOUNT + 1) * Data Width
|
(reg_1 as usize + 1) * self.word_bytes as usize
|
||||||
(reg_1 + 1) * self.word_bytes
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 2. Total bytes consumed in this specific active slot
|
// Active slot consumed calculation accounts for dynamic slot size
|
||||||
let active_slot_consumed = self.slot_bytes - remaining_bytes;
|
let slot_bytes = self.slot_bytes.load(Ordering::Acquire);
|
||||||
|
let active_slot_consumed = slot_bytes.saturating_sub(remaining_bytes);
|
||||||
|
|
||||||
// 3. Combine with your software index history accumulator
|
return consumed_start * slot_bytes + active_slot_consumed;
|
||||||
return consumed_start * self.slot_bytes + active_slot_consumed;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -295,6 +344,8 @@ impl<const N: usize, const MAX_SLOT_BYTES: usize> DmaRing<N, MAX_SLOT_BYTES> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(&self) {
|
pub fn run(&self) {
|
||||||
|
self.dma.inta0.write(|w| unsafe { w.bits(1 << 19) });
|
||||||
|
self.dma.errint0.write(|w| unsafe { w.bits(1 << 19) });
|
||||||
self.dma.enableset0.write(|w| unsafe { w.bits(1 << 19) });
|
self.dma.enableset0.write(|w| unsafe { w.bits(1 << 19) });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -311,8 +362,8 @@ impl<const N: usize, const MAX_SLOT_BYTES: usize> DmaRing<N, MAX_SLOT_BYTES> {
|
|||||||
|
|
||||||
fn reset_producer(&self) {
|
fn reset_producer(&self) {
|
||||||
unsafe {
|
unsafe {
|
||||||
*(&mut *self.write_slot.get()) = 0;
|
*self.write_slot.get() = 0;
|
||||||
*(&mut *self.write_off.get()) = 0;
|
*self.write_off.get() = 0;
|
||||||
}
|
}
|
||||||
self.produced.store(0, Ordering::Relaxed);
|
self.produced.store(0, Ordering::Relaxed);
|
||||||
self.produced_bytes.store(0, Ordering::Relaxed);
|
self.produced_bytes.store(0, Ordering::Relaxed);
|
||||||
@@ -324,104 +375,52 @@ impl<const N: usize, const MAX_SLOT_BYTES: usize> DmaRing<N, MAX_SLOT_BYTES> {
|
|||||||
let fill = self.fill_slots();
|
let fill = self.fill_slots();
|
||||||
fill >= N.wrapping_sub(self.safety_gap)
|
fill >= N.wrapping_sub(self.safety_gap)
|
||||||
}
|
}
|
||||||
fn reset_producer_init_only(&self) {
|
|
||||||
unsafe {
|
|
||||||
*self.write_slot.get() = 0;
|
|
||||||
}
|
|
||||||
unsafe {
|
|
||||||
*self.write_off.get() = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.produced.store(0, Ordering::Relaxed);
|
|
||||||
self.consumed.store(0, Ordering::Relaxed);
|
|
||||||
|
|
||||||
self.produced_bytes.store(0, Ordering::Relaxed);
|
|
||||||
self.consumed_bytes.store(0, Ordering::Relaxed);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn init_descriptors(&self) {
|
fn init_descriptors(&self) {
|
||||||
|
let slot_bytes = self.slot_bytes.load(Ordering::Acquire);
|
||||||
let slots = unsafe { &mut *self.slots.get() };
|
let slots = unsafe { &mut *self.slots.get() };
|
||||||
let desc = unsafe { &mut *self.desc.get() };
|
let desc = unsafe { &mut *self.desc.get() };
|
||||||
let chan_desc = unsafe { &mut *self.channel_desc.get() };
|
let chan_desc = unsafe { &mut *self.channel_desc.get() };
|
||||||
defmt::debug!("slots base: &{:x}", self.slots.get());
|
|
||||||
|
|
||||||
// Pre-fill with silence so underrun replays silence.
|
// Pre-fill active slot regions with silence
|
||||||
for i in 0..N {
|
for slot in slots.iter_mut() {
|
||||||
slots[i][..self.slot_bytes].fill(0);
|
slot.fill(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
let transfers = (self.slot_bytes / self.word_bytes) as u32;
|
let transfers = (slot_bytes / self.word_bytes as usize) as u32;
|
||||||
|
|
||||||
for i in 0..N {
|
|
||||||
let src_start = slots[i].as_ptr() as usize;
|
|
||||||
let src_end = (src_start + self.slot_bytes - self.word_bytes) as *const u8;
|
|
||||||
|
|
||||||
|
for (i, slot) in slots.iter().enumerate() {
|
||||||
|
let src_start = slot.as_ptr() as usize;
|
||||||
|
let src_end = (src_start + slot_bytes - self.word_bytes as usize) as *const u8;
|
||||||
let next = &desc.d[(i + 1) % N] as *const DmaDescriptor;
|
let next = &desc.d[(i + 1) % N] as *const DmaDescriptor;
|
||||||
|
|
||||||
desc.d[i] = DmaDescriptor {
|
desc.d[i] = DmaDescriptor {
|
||||||
xfercfg: encode_xfercfg(
|
xfercfg: XferCfg::new()
|
||||||
true, // valid
|
.with_cfgvalid(true) // valid
|
||||||
true, // reload
|
.with_reload(true) // reload
|
||||||
false, // swtrig (we use XFERCFG SWTRIG kick)
|
.with_swtrig(false) // swtrig
|
||||||
false, // clrtrig
|
.with_clrtrig(false) // clrtrig
|
||||||
true, // intA
|
.with_inta(true) // intA
|
||||||
false, // intB
|
.with_intb(false) // intB
|
||||||
self.word_bytes as u32,
|
.with_width(self.word_bytes)
|
||||||
1, // src_inc
|
.with_src_inc(1) // src_inc
|
||||||
0, // dst_inc
|
.with_dst_inc(0) // dst_inc
|
||||||
transfers,
|
.with_transfers(transfers)
|
||||||
),
|
.into_bits(),
|
||||||
src_end,
|
src_end,
|
||||||
dst_end: self.dst_reg,
|
dst_end: self.dst_reg,
|
||||||
next,
|
next,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure memory writes complete before reloading DMA hardware pointers
|
||||||
|
compiler_fence(Ordering::Release);
|
||||||
|
|
||||||
chan_desc.d[19] = desc.d[0];
|
chan_desc.d[19] = desc.d[0];
|
||||||
chan_desc.d[19].xfercfg = 0;
|
chan_desc.d[19].xfercfg = 0;
|
||||||
|
|
||||||
// reset producer indices + counters (init-only action)
|
self.reset_producer();
|
||||||
self.reset_producer_init_only();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl<const N: usize, const MAX_SLOT_BYTES: usize> Sync for DmaRing<N, MAX_SLOT_BYTES> {}
|
unsafe impl<const N: usize, const MAX_SLOT_BYTES: usize> Sync for DmaRing<N, MAX_SLOT_BYTES> {}
|
||||||
|
|
||||||
/// XFERCFG encoding follows the common LPC DMA layout:
|
|
||||||
/// - SETINTA at bit4, SETINTB at bit5
|
|
||||||
/// - WIDTH at bits 9:8
|
|
||||||
/// - SRCINC at bits 13:12
|
|
||||||
/// - DSTINC at bits 15:14
|
|
||||||
/// - XFERCOUNT at bits 25:16
|
|
||||||
/// This layout is shown in LPC DMA examples. [5](https://www.kernel.org/doc/html/latest/core-api/dma-api-howto.html)
|
|
||||||
fn encode_xfercfg(
|
|
||||||
cfgvalid: bool,
|
|
||||||
reload: bool,
|
|
||||||
swtrig: bool,
|
|
||||||
clrtrig: bool,
|
|
||||||
inta: bool,
|
|
||||||
intb: bool,
|
|
||||||
width_bytes: u32,
|
|
||||||
src_inc: u32,
|
|
||||||
dst_inc: u32,
|
|
||||||
transfers: u32,
|
|
||||||
) -> u32 {
|
|
||||||
let width_code = match width_bytes {
|
|
||||||
1 => 0,
|
|
||||||
2 => 1,
|
|
||||||
4 => 2,
|
|
||||||
_ => 0,
|
|
||||||
};
|
|
||||||
|
|
||||||
let count_field = transfers.saturating_sub(1) & 0x3FF;
|
|
||||||
|
|
||||||
((cfgvalid as u32) << 0)
|
|
||||||
| ((reload as u32) << 1)
|
|
||||||
| ((swtrig as u32) << 2)
|
|
||||||
| ((clrtrig as u32) << 3)
|
|
||||||
| ((inta as u32) << 4)
|
|
||||||
| ((intb as u32) << 5)
|
|
||||||
| ((width_code & 0x3) << 8)
|
|
||||||
| ((src_inc & 0x3) << 12)
|
|
||||||
| ((dst_inc & 0x3) << 14)
|
|
||||||
| (count_field << 16)
|
|
||||||
}
|
|
||||||
+79
-5
@@ -1,5 +1,16 @@
|
|||||||
|
use core::cell::UnsafeCell;
|
||||||
|
use core::mem::MaybeUninit;
|
||||||
|
|
||||||
|
use crate::hal;
|
||||||
use crate::pac;
|
use crate::pac;
|
||||||
use defmt::{debug, info};
|
|
||||||
|
use defmt::debug;
|
||||||
|
use hal::{
|
||||||
|
Enabled, Iocon, Pin,
|
||||||
|
drivers::pins,
|
||||||
|
traits::wg::digital::v2::{OutputPin, ToggleableOutputPin},
|
||||||
|
typestates::pin::{gpio::direction::Output, state::Gpio},
|
||||||
|
};
|
||||||
|
|
||||||
pub(crate) struct PllConstants {
|
pub(crate) struct PllConstants {
|
||||||
pub m: u16, // 1-65535
|
pub m: u16, // 1-65535
|
||||||
@@ -113,13 +124,16 @@ pub(crate) fn init_sys_pll1() {
|
|||||||
syscon.mainclkselb.modify(|_, w| w.sel().enum_0x2()); // pll1
|
syscon.mainclkselb.modify(|_, w| w.sel().enum_0x2()); // pll1
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fo = M/(N*2*P) * Fin
|
|
||||||
// Fo = 3072/(125*2*8) * 16MHz = 24.576MHz
|
|
||||||
const AUDIO_PLL: PllConstants = PllConstants::new(125, 3072, 8);
|
|
||||||
|
|
||||||
// Set PLL0 to 24.576MHz, start, and wait for lock
|
// Set PLL0 to 24.576MHz, start, and wait for lock
|
||||||
// This is not exposed by lpc55-hal, unfortunately. Copy their implementation here.
|
// This is not exposed by lpc55-hal, unfortunately. Copy their implementation here.
|
||||||
|
#[cfg(feature = "evk")]
|
||||||
pub(crate) fn init_audio_pll() {
|
pub(crate) fn init_audio_pll() {
|
||||||
|
use defmt::info;
|
||||||
|
|
||||||
|
// Fo = M/(N*2*P) * Fin
|
||||||
|
// Fo = 3072/(125*2*8) * 16MHz = 24.576MHz
|
||||||
|
const AUDIO_PLL: PllConstants = PllConstants::new(125, 3072, 8);
|
||||||
|
|
||||||
let syscon = unsafe { &*pac::SYSCON::ptr() };
|
let syscon = unsafe { &*pac::SYSCON::ptr() };
|
||||||
let pmc = unsafe { &*pac::PMC::ptr() };
|
let pmc = unsafe { &*pac::PMC::ptr() };
|
||||||
let anactrl = unsafe { &*pac::ANACTRL::ptr() };
|
let anactrl = unsafe { &*pac::ANACTRL::ptr() };
|
||||||
@@ -184,3 +198,63 @@ pub(crate) fn init_audio_pll() {
|
|||||||
}
|
}
|
||||||
info!("pll0 locked after {} loops", i);
|
info!("pll0 locked after {} loops", i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct SharedLed<T: OutputPin> {
|
||||||
|
inner: UnsafeCell<T>,
|
||||||
|
}
|
||||||
|
unsafe impl<T: OutputPin> Sync for SharedLed<T> {}
|
||||||
|
#[allow(dead_code)]
|
||||||
|
impl<T: OutputPin> SharedLed<T> {
|
||||||
|
pub fn new(inner: T) -> Self {
|
||||||
|
Self {
|
||||||
|
inner: UnsafeCell::new(inner),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn on(&self) {
|
||||||
|
unsafe {
|
||||||
|
(*self.inner.get()).set_low().ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn off(&self) {
|
||||||
|
unsafe {
|
||||||
|
(*self.inner.get()).set_high().ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<T: OutputPin + ToggleableOutputPin> SharedLed<T> {
|
||||||
|
pub fn toggle(&self) {
|
||||||
|
unsafe {
|
||||||
|
(*self.inner.get()).toggle().ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Led1 = Pin<pins::Pio0_13, Gpio<Output>>;
|
||||||
|
type Led2 = Pin<pins::Pio0_14, Gpio<Output>>;
|
||||||
|
pub static LED1: MaybeUninit<SharedLed<Led1>> = MaybeUninit::uninit();
|
||||||
|
pub static LED2: MaybeUninit<SharedLed<Led2>> = MaybeUninit::uninit();
|
||||||
|
|
||||||
|
pub fn init_leds(iocon: &mut Iocon<Enabled>, gpio: &mut hal::Gpio<Enabled>) {
|
||||||
|
let led1 = SharedLed::new(
|
||||||
|
pins::Pio0_13::take()
|
||||||
|
.unwrap()
|
||||||
|
.into_gpio_pin(iocon, gpio)
|
||||||
|
.into_output_low(),
|
||||||
|
);
|
||||||
|
let led2 = SharedLed::new(
|
||||||
|
pins::Pio0_14::take()
|
||||||
|
.unwrap()
|
||||||
|
.into_gpio_pin(iocon, gpio)
|
||||||
|
.into_output_low(),
|
||||||
|
);
|
||||||
|
unsafe {
|
||||||
|
core::ptr::write(LED1.as_ptr() as *mut SharedLed<Led1>, led1);
|
||||||
|
core::ptr::write(LED2.as_ptr() as *mut SharedLed<Led2>, led2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn led1() -> &'static SharedLed<Led1> {
|
||||||
|
unsafe { &*LED1.as_ptr() }
|
||||||
|
}
|
||||||
|
pub fn led2() -> &'static SharedLed<Led2> {
|
||||||
|
unsafe { &*LED2.as_ptr() }
|
||||||
|
}
|
||||||
+413
-273
File diff suppressed because it is too large.
Load diff
@@ -5,6 +5,7 @@ pub trait Dac<T> {
|
|||||||
fn init(&mut self);
|
fn init(&mut self);
|
||||||
fn change_rate(&mut self, new_rate: u32);
|
fn change_rate(&mut self, new_rate: u32);
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
|
#[allow(dead_code)]
|
||||||
fn set_volume(&mut self, left: u8, right: u8) {}
|
fn set_volume(&mut self, left: u8, right: u8) {}
|
||||||
fn mute(&mut self);
|
fn mute(&mut self);
|
||||||
fn unmute(&mut self);
|
fn unmute(&mut self);
|
||||||
|
|||||||
@@ -3,9 +3,13 @@ name = "shared"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
std = ["deku/alloc"]
|
||||||
serde = ["dep:serde"]
|
serde = ["dep:serde"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
bytemuck = { version = "1.25.0", features = ["derive"] }
|
||||||
|
defmt = "1.1.1"
|
||||||
deku = { version = "0.20.3", default-features = false }
|
deku = { version = "0.20.3", default-features = false }
|
||||||
|
num_enum = { version = "0.7.6", default-features = false }
|
||||||
serde = { version = "1.0.228", optional = true, features = ["derive"] }
|
serde = { version = "1.0.228", optional = true, features = ["derive"] }
|
||||||
usbd-hid = { version = "0.10.0" }
|
usbd-hid = { version = "0.10.0" }
|
||||||
+171
-12
@@ -1,14 +1,135 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
|
use bytemuck::NoUninit;
|
||||||
|
use deku::DekuRead;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
use deku::DekuWrite;
|
||||||
|
use num_enum::TryFromPrimitive;
|
||||||
|
|
||||||
|
#[cfg(feature = "serde")]
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Default, NoUninit, Eq, PartialEq, TryFromPrimitive)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
|
#[repr(u8)]
|
||||||
|
pub enum AudioState {
|
||||||
|
/// Knowingly stopped, ie. AltSetting=0. DAC muted, I2S disabled.
|
||||||
|
///
|
||||||
|
/// AltSetting = 1 -> ARMED
|
||||||
|
#[default]
|
||||||
|
Stopped = 0,
|
||||||
|
/// Waiting for data. DAC muted, I2S running sending 0s (FIFO not serviced).
|
||||||
|
///
|
||||||
|
/// USB OUT data packet -> ARMED
|
||||||
|
/// AltSetting = 0 -> STOPPED
|
||||||
|
Armed = 1,
|
||||||
|
/// Filling the buffer before playback starts. Feedback does not run,
|
||||||
|
/// playout does not start draining the queue. Gets us better feedback
|
||||||
|
/// behaviour and a full buffer without a feedback rate spike at startup.
|
||||||
|
///
|
||||||
|
/// queue reaches <QUEUE_RUNNING_UP> -> RUNNING
|
||||||
|
/// AltSetting = 0 -> STOPPED
|
||||||
|
///
|
||||||
|
Prefill = 2,
|
||||||
|
/// Normal running state. Start servicing FIFO and begin playing out from the buffer.
|
||||||
|
///
|
||||||
|
/// queue reaches <QUEUE_RUNNING_DOWN> -> DRAINING
|
||||||
|
/// AltSetting = 0 -> DRAINING
|
||||||
|
Running = 3,
|
||||||
|
/// The queue is low. We will continue playout.
|
||||||
|
///
|
||||||
|
/// queue is empty && altSetting 1 -> NODATA
|
||||||
|
/// queue is empty && altSetting 0 -> STOPPED
|
||||||
|
/// queue reaches <QUEUE_RUNNING_UP> && altSetting 1 -> RUNNING
|
||||||
|
LowData = 4,
|
||||||
|
/// There is no data in the queue. We will count underflows for a while, send 0s, and hope the host comes back.
|
||||||
|
///
|
||||||
|
/// countdown reaches DATA_TIMEOUT -> STOPPED
|
||||||
|
/// AltSetting = 0 -> STOPPED
|
||||||
|
NoData = 5,
|
||||||
|
/// The host has asked us to stop (altSetting 0), but we need to play out the remaining buffer
|
||||||
|
///
|
||||||
|
/// queue is empty -> STOPPED
|
||||||
|
Stopping = 6,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl defmt::Format for AudioState {
|
||||||
|
fn format(&self, fmt: defmt::Formatter) {
|
||||||
|
defmt::write!(
|
||||||
|
fmt,
|
||||||
|
"{}",
|
||||||
|
match self {
|
||||||
|
Self::Stopped => "Stopped",
|
||||||
|
Self::Armed => "Armed",
|
||||||
|
Self::Prefill => "Prefill",
|
||||||
|
Self::Running => "Running",
|
||||||
|
Self::LowData => "Draining",
|
||||||
|
Self::NoData => "NoData",
|
||||||
|
Self::Stopping => "Stopping",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl core::fmt::Display for AudioState {
|
||||||
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{}",
|
||||||
|
match self {
|
||||||
|
Self::Stopped => "Stopped",
|
||||||
|
Self::Armed => "Armed",
|
||||||
|
Self::Prefill => "Prefill",
|
||||||
|
Self::Running => "Running",
|
||||||
|
Self::LowData => "Draining",
|
||||||
|
Self::NoData => "NoData",
|
||||||
|
Self::Stopping => "Stopping",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
|
pub struct AudioTelemetrySnapshot {
|
||||||
|
pub state: AudioState,
|
||||||
|
pub average_buffer_fill: u16,
|
||||||
|
pub frame_count: u32,
|
||||||
|
pub dac_underflow_count: u16,
|
||||||
|
pub usb_underflow_count: u16,
|
||||||
|
pub dac_overflow_count: u16,
|
||||||
|
pub cur_rate: u32,
|
||||||
|
pub fb_rate_estimate: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Writing is host-side only (needs deku's `alloc`, enabled by the `std` feature)
|
||||||
|
#[derive(Debug, DekuRead, Clone)]
|
||||||
|
#[cfg_attr(feature = "std", derive(DekuWrite))]
|
||||||
|
#[deku(id_type = "u8")]
|
||||||
|
pub enum AudioDeviceCommand {
|
||||||
|
#[deku(id = 0)]
|
||||||
|
Noop,
|
||||||
|
#[deku(id = 1)]
|
||||||
|
Reset,
|
||||||
|
#[deku(id = 2)]
|
||||||
|
Bootloader,
|
||||||
|
}
|
||||||
|
|
||||||
pub mod hid {
|
pub mod hid {
|
||||||
use deku::DekuRead;
|
use deku::{DekuContainerRead, DekuError, DekuRead};
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "std")]
|
||||||
use serde::{Deserialize, Serialize};
|
use deku::{DekuContainerWrite, DekuWrite};
|
||||||
use usbd_hid::descriptor::generator_prelude::*;
|
use usbd_hid::descriptor::generator_prelude::*;
|
||||||
|
|
||||||
|
use crate::{AudioDeviceCommand, AudioState, AudioTelemetrySnapshot};
|
||||||
|
|
||||||
|
/// HID usage IDs identifying each vendor interface. `gen_hid_descriptor` only accepts
|
||||||
|
/// literals, so the descriptors below repeat these values; keep them in sync.
|
||||||
|
pub const USAGE_TELEMETRY: u16 = 0x01;
|
||||||
|
pub const USAGE_COMMAND: u16 = 0x02;
|
||||||
|
|
||||||
#[derive(DekuRead)]
|
#[derive(DekuRead)]
|
||||||
#[deku(endian = "little")]
|
#[deku(endian = "little")]
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
|
||||||
#[gen_hid_descriptor(
|
#[gen_hid_descriptor(
|
||||||
(collection = APPLICATION, usage_page = VENDOR_DEFINED_START, usage = 0x01, ) = {
|
(collection = APPLICATION, usage_page = VENDOR_DEFINED_START, usage = 0x01, ) = {
|
||||||
state=input;
|
state=input;
|
||||||
@@ -17,10 +138,8 @@ pub mod hid {
|
|||||||
dac_underflow_count=input;
|
dac_underflow_count=input;
|
||||||
usb_underflow_count=input;
|
usb_underflow_count=input;
|
||||||
dac_overflow_count=input;
|
dac_overflow_count=input;
|
||||||
p=input;
|
cur_rate=input;
|
||||||
i=input;
|
fb_rate_estimate=input;
|
||||||
fb=input;
|
|
||||||
integrator=input;
|
|
||||||
}
|
}
|
||||||
)]
|
)]
|
||||||
#[repr(C, packed)]
|
#[repr(C, packed)]
|
||||||
@@ -32,9 +151,49 @@ pub mod hid {
|
|||||||
pub dac_underflow_count: u16,
|
pub dac_underflow_count: u16,
|
||||||
pub usb_underflow_count: u16,
|
pub usb_underflow_count: u16,
|
||||||
pub dac_overflow_count: u16,
|
pub dac_overflow_count: u16,
|
||||||
pub p: i32,
|
pub cur_rate: i32,
|
||||||
pub i: i32,
|
pub fb_rate_estimate: i32,
|
||||||
pub fb: i32,
|
}
|
||||||
pub integrator: i32,
|
|
||||||
|
impl From<AudioTelemetryReport> for AudioTelemetrySnapshot {
|
||||||
|
fn from(value: AudioTelemetryReport) -> Self {
|
||||||
|
AudioTelemetrySnapshot {
|
||||||
|
state: AudioState::try_from(value.state).expect("Invalid AudioState"),
|
||||||
|
average_buffer_fill: value.average_buffer_fill,
|
||||||
|
frame_count: value.frame_count.cast_unsigned(), // on firmware side is usize == u32
|
||||||
|
dac_underflow_count: value.dac_underflow_count,
|
||||||
|
usb_underflow_count: value.usb_underflow_count,
|
||||||
|
dac_overflow_count: value.dac_overflow_count,
|
||||||
|
cur_rate: value.cur_rate.cast_unsigned(),
|
||||||
|
fb_rate_estimate: (value.fb_rate_estimate as f32),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(DekuRead)]
|
||||||
|
#[cfg_attr(feature = "std", derive(DekuWrite))]
|
||||||
|
#[deku(endian = "little")]
|
||||||
|
#[gen_hid_descriptor((collection = APPLICATION, usage_page = VENDOR_DEFINED_START, usage = 0x02, ) = {
|
||||||
|
cmd=output;
|
||||||
|
})]
|
||||||
|
#[repr(C, packed)]
|
||||||
|
pub struct AudioDeviceCommandReport {
|
||||||
|
pub cmd: [u8; 32],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<AudioDeviceCommandReport> for AudioDeviceCommand {
|
||||||
|
type Error = DekuError;
|
||||||
|
fn try_from(value: AudioDeviceCommandReport) -> Result<Self, Self::Error> {
|
||||||
|
Ok(AudioDeviceCommand::from_bytes((value.cmd.as_slice(), 0))?.1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
impl From<AudioDeviceCommand> for AudioDeviceCommandReport {
|
||||||
|
fn from(value: AudioDeviceCommand) -> Self {
|
||||||
|
let mut buf = [0u8; core::mem::size_of::<Self>()];
|
||||||
|
value.to_slice(&mut buf).unwrap();
|
||||||
|
Self { cmd: buf }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user