Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

<!-- Add new changes here! -->

### 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
Expand Down
1 change: 1 addition & 0 deletions src/daisy_seed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions src/daisy_seed.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions src/per/sai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ================================================================
Expand All @@ -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;
Expand Down
19 changes: 15 additions & 4 deletions src/sys/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 25 additions & 8 deletions src/sys/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
Loading