Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PWMAudio setFrequency optimization #2683

Merged
merged 1 commit into from
Dec 13, 2024
Merged
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: 2 additions & 2 deletions libraries/AudioBufferManager/src/AudioBufferManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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]);
Expand Down
12 changes: 8 additions & 4 deletions libraries/PWMAudio/src/PWMAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
Loading