From 48f24c5a643678646b0c68d156335b02aba5fe7c Mon Sep 17 00:00:00 2001 From: NinetyDays <323113474+Ninety-Days@users.noreply.github.com> Date: Fri, 20 Nov 2026 18:17:50 +0200 Subject: [PATCH] Add opt-in exact PLL3 audio clock System::Config::exact_audio_clock (and DaisySeed::exact_audio_clock, read by DaisySeed::Init()) sets PLL3 to M=5, N=192, P=25, R=25 for an exact 24.576 MHz SAI kernel clock, so 8-96 kHz run at their nominal rates instead of ~298 ppm fast. The default PLL3 setting is unchanged. SAI_192KHZ cannot be reached from that clock. SaiHandle::Init() now returns an error, before changing any state, when the kernel clock is too slow for the requested rate, instead of letting the HAL run it at a lower rate. Refs #731 --- CHANGELOG.md | 4 ++++ src/daisy_seed.cpp | 1 + src/daisy_seed.h | 6 ++++++ src/per/sai.cpp | 30 ++++++++++++++++++++++++++++++ src/sys/system.cpp | 19 +++++++++++++++---- src/sys/system.h | 33 +++++++++++++++++++++++++-------- 6 files changed, 81 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1c452bb2..4b709fbed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ +### Features + +- System: Added opt-in `exact_audio_clock` (on `System::Config` and `DaisySeed`), which sets PLL3 for an exact 24.576 MHz SAI kernel clock, so 8–96 kHz run at their nominal rates instead of ~298 ppm fast. `SAI_192KHZ` is not available in this mode: `SaiHandle::Init()` now returns an error when the SAI kernel clock is too slow for the requested sample rate, instead of running at a lower rate. + ## v9.0.0 ### Features diff --git a/src/daisy_seed.cpp b/src/daisy_seed.cpp index 769d5c3d8..3adaed28f 100644 --- a/src/daisy_seed.cpp +++ b/src/daisy_seed.cpp @@ -95,6 +95,7 @@ void DaisySeed::Init(bool boost) //dsy_system_init(); System::Config syscfg; boost ? syscfg.Boost() : syscfg.Defaults(); + syscfg.exact_audio_clock = exact_audio_clock; ConfigureQspi(); // Configure the built-in GPIOs. diff --git a/src/daisy_seed.h b/src/daisy_seed.h index 0404c9db8..5295c660f 100644 --- a/src/daisy_seed.h +++ b/src/daisy_seed.h @@ -149,6 +149,12 @@ class DaisySeed System system; Ak4556 codec; + /** Set to true before Init() to use the exact PLL3 audio clock + ** (see System::Config::exact_audio_clock): 8 kHz to 96 kHz run at + ** their nominal rates, and SAI_192KHZ is not available. + */ + bool exact_audio_clock = false; + /** Internal indices for DaisySeed-equivalent devices * This shouldn't have any effect on user-facing code, * and only needs to be checked to properly initialize diff --git a/src/per/sai.cpp b/src/per/sai.cpp index f6067444a..b6d25f42b 100644 --- a/src/per/sai.cpp +++ b/src/per/sai.cpp @@ -61,6 +61,20 @@ static void Error_Handler() while(1) {} } +static uint32_t SampleRateHz(SaiHandle::Config::SampleRate sr) +{ + switch(sr) + { + case SaiHandle::Config::SampleRate::SAI_8KHZ: return 8000; + case SaiHandle::Config::SampleRate::SAI_16KHZ: return 16000; + case SaiHandle::Config::SampleRate::SAI_32KHZ: return 32000; + case SaiHandle::Config::SampleRate::SAI_48KHZ: return 48000; + case SaiHandle::Config::SampleRate::SAI_96KHZ: return 96000; + case SaiHandle::Config::SampleRate::SAI_192KHZ: return 192000; + default: return 0; + } +} + // ================================================================ // Static References for available SaiHandle::Impls // ================================================================ @@ -78,6 +92,22 @@ SaiHandle::Result SaiHandle::Impl::Init(const SaiHandle::Config& config) if(sai_idx >= 2) return Result::ERR; + // With a master block, the HAL derives MCKDIV from the kernel clock for + // MCLK = 256 * fs, and MCKDIV = 0 divides by 1. A kernel clock too slow + // for the requested rate would run at a lower rate without any error + // (e.g. SAI_192KHZ at 96kHz with exact_audio_clock). Refuse it here, + // before any state changes, so the previous configuration stays valid. + // The test matches the HAL's rounding in HAL_SAI_Init(). + if(!(config.a_sync == Config::Sync::SLAVE + && config.b_sync == Config::Sync::SLAVE)) + { + const uint64_t kernel_clk = HAL_RCCEx_GetPeriphCLKFreq( + sai_idx == 0 ? RCC_PERIPHCLK_SAI1 : RCC_PERIPHCLK_SAI2); + const uint64_t fs = SampleRateHz(config.sr); + if(fs == 0 || (kernel_clk * 10U) / (fs * 256U) < 9U) + return Result::ERR; + } + // Default Buffer states buff_rx_ = nullptr; buff_tx_ = nullptr; diff --git a/src/sys/system.cpp b/src/sys/system.cpp index faa9eae32..48514cb69 100644 --- a/src/sys/system.cpp +++ b/src/sys/system.cpp @@ -486,11 +486,22 @@ void System::ConfigureClocks() PeriphClkInitStruct.PLL2.PLL2RGE = RCC_PLL2VCIRANGE_2; PeriphClkInitStruct.PLL2.PLL2VCOSEL = RCC_PLL2VCOWIDE; // PLL 3 - PeriphClkInitStruct.PLL3.PLL3M = 6; - PeriphClkInitStruct.PLL3.PLL3N = 295; - PeriphClkInitStruct.PLL3.PLL3P = 16; // 49.xMhz + if(cfg_.exact_audio_clock) + { + // 16 MHz / 5 * 192 = 614.4 MHz VCO + PeriphClkInitStruct.PLL3.PLL3M = 5; + PeriphClkInitStruct.PLL3.PLL3N = 192; + PeriphClkInitStruct.PLL3.PLL3P = 25; // 24.576MHz, exact 8-96kHz SAI + PeriphClkInitStruct.PLL3.PLL3R = 25; // 24.576MHz + } + else + { + PeriphClkInitStruct.PLL3.PLL3M = 6; + PeriphClkInitStruct.PLL3.PLL3N = 295; + PeriphClkInitStruct.PLL3.PLL3P = 16; // 49.xMhz + PeriphClkInitStruct.PLL3.PLL3R = 32; // 24.xMhz + } PeriphClkInitStruct.PLL3.PLL3Q = 4; - PeriphClkInitStruct.PLL3.PLL3R = 32; // 24.xMhz PeriphClkInitStruct.PLL3.PLL3RGE = RCC_PLL3VCIRANGE_1; PeriphClkInitStruct.PLL3.PLL3VCOSEL = RCC_PLL3VCOWIDE; PeriphClkInitStruct.PLL3.PLL3FRACN = 0; diff --git a/src/sys/system.h b/src/sys/system.h index 10b66ea46..07f93f650 100644 --- a/src/sys/system.h +++ b/src/sys/system.h @@ -31,31 +31,48 @@ class System /** Method to call on the struct to set to defaults ** CPU Freq set to 400MHz ** Cache Enabled + ** Stock audio clock (exact_audio_clock = false) ** */ void Defaults() { - cpu_freq = SysClkFreq::FREQ_400MHZ; - use_dcache = true; - use_icache = true; - skip_clocks = false; + cpu_freq = SysClkFreq::FREQ_400MHZ; + use_dcache = true; + use_icache = true; + skip_clocks = false; + exact_audio_clock = false; } /** Method to call on the struct to set to boost mode: ** CPU Freq set to 480MHz ** Cache Enabled + ** Stock audio clock (exact_audio_clock = false) ** */ void Boost() { - cpu_freq = SysClkFreq::FREQ_480MHZ; - use_dcache = true; - use_icache = true; - skip_clocks = false; + cpu_freq = SysClkFreq::FREQ_480MHZ; + use_dcache = true; + use_icache = true; + skip_clocks = false; + exact_audio_clock = false; } SysClkFreq cpu_freq; bool use_dcache; bool use_icache; bool skip_clocks; + + /** Opt-in PLL3 setting for an exact SAI clock. + ** false (default): PLL3 M=6, N=295, P=16, R=32. The SAI kernel clock + ** is 49.1667 MHz, and every SAI sample rate runs about 298 ppm fast + ** (48 kHz runs at 48014.3 Hz). + ** true: PLL3 M=5, N=192, P=25, R=25. The SAI kernel clock is exactly + ** 24.576 MHz, so 8, 16, 32, 48 and 96 kHz run at their nominal rates. + ** SAI_192KHZ cannot be reached from this clock, and SaiHandle::Init() + ** returns an error for it. The ADC kernel clock (PLL3R) moves from + ** 24.583 MHz to 24.576 MHz. + ** Has no effect when skip_clocks is true. + */ + bool exact_audio_clock = false; }; /** Describes the different regions of memory available to the Daisy */