From f68a81ae8375a6e4e7fd3f7802eafd4bc5784a88 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 13 Dec 2024 07:22:39 -0800 Subject: [PATCH] PWMAudio setFrequency optimization If we set the frequency to the same one running, no need to do anything. --- .../AudioBufferManager/src/AudioBufferManager.cpp | 4 ++-- libraries/PWMAudio/src/PWMAudio.cpp | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/libraries/AudioBufferManager/src/AudioBufferManager.cpp b/libraries/AudioBufferManager/src/AudioBufferManager.cpp index f47eb6953..d082aa493 100644 --- a/libraries/AudioBufferManager/src/AudioBufferManager.cpp +++ b/libraries/AudioBufferManager/src/AudioBufferManager.cpp @@ -113,8 +113,6 @@ void AudioBufferManager::setCallback(void (*fn)(void *), void *cbData) { } bool AudioBufferManager::begin(int dreq, volatile void *pioFIFOAddr) { - _running = true; - // Get ping and pong DMA channels for (auto i = 0; i < 2; i++) { _channelDMA[i] = dma_claim_unused_channel(false); @@ -126,6 +124,8 @@ bool AudioBufferManager::begin(int dreq, volatile void *pioFIFOAddr) { } } + _running = true; + // Need to know both channels to set up ping-pong, so do in 2 stages for (auto i = 0; i < 2; i++) { dma_channel_config c = dma_channel_get_default_config(_channelDMA[i]); diff --git a/libraries/PWMAudio/src/PWMAudio.cpp b/libraries/PWMAudio/src/PWMAudio.cpp index 3399dc188..b0886b3c2 100644 --- a/libraries/PWMAudio/src/PWMAudio.cpp +++ b/libraries/PWMAudio/src/PWMAudio.cpp @@ -95,13 +95,17 @@ bool PWMAudio::setPWMFrequency(int newFreq) { } bool PWMAudio::setFrequency(int frequency) { + if (frequency == _sampleRate) { + return true; // We're already at the right speed + } if (_pacer < 0) { return false; } uint16_t _pacer_D, _pacer_N; - /*Flip fraction(N for D, D for N) because we are using it as sys_clk * fraction(mechanic of dma_timer_set_fraction) for smaller than sys_clk values*/ + // Flip fraction(N for D, D for N) because we are using it as sys_clk * fraction(mechanic of dma_timer_set_fraction) for smaller than sys_clk values find_pacer_fraction(frequency, &_pacer_D, &_pacer_N); dma_timer_set_fraction(_pacer, _pacer_N, _pacer_D); + _sampleRate = frequency; return true; } @@ -140,15 +144,15 @@ bool PWMAudio::begin() { setPWMFrequency(_freq); - /*Calculate and set the DMA pacer timer. This timer will pull data on a fixed sample rate. So the actual PWM frequency can be higher or lower.*/ + // Calculate and set the DMA pacer timer. This timer will pull data on a fixed sample rate. So the actual PWM frequency can be higher or lower. _pacer = dma_claim_unused_timer(false); - /*When no unused timer is found, return*/ + // When no unused timer is found, return if (_pacer < 0) { return false; } uint16_t _pacer_D = 0; uint16_t _pacer_N = 0; - /*Flip fraction(N for D, D for N) because we are using it as sys_clk * fraction(mechanic of dma_timer_set_fraction) for smaller than sys_clk values*/ + // Flip fraction(N for D, D for N) because we are using it as sys_clk * fraction(mechanic of dma_timer_set_fraction) for smaller than sys_clk values find_pacer_fraction(_sampleRate, &_pacer_D, &_pacer_N); dma_timer_set_fraction(_pacer, _pacer_N, _pacer_D); int _pacer_dreq = dma_get_timer_dreq(_pacer);