Skip to content

Commit a02e188

Browse files
PWMAudio setFrequency optimization (#2683)
If we set the frequency to the same one running, no need to do anything.
1 parent e56a295 commit a02e188

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

libraries/AudioBufferManager/src/AudioBufferManager.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,6 @@ void AudioBufferManager::setCallback(void (*fn)(void *), void *cbData) {
113113
}
114114

115115
bool AudioBufferManager::begin(int dreq, volatile void *pioFIFOAddr) {
116-
_running = true;
117-
118116
// Get ping and pong DMA channels
119117
for (auto i = 0; i < 2; i++) {
120118
_channelDMA[i] = dma_claim_unused_channel(false);
@@ -126,6 +124,8 @@ bool AudioBufferManager::begin(int dreq, volatile void *pioFIFOAddr) {
126124
}
127125
}
128126

127+
_running = true;
128+
129129
// Need to know both channels to set up ping-pong, so do in 2 stages
130130
for (auto i = 0; i < 2; i++) {
131131
dma_channel_config c = dma_channel_get_default_config(_channelDMA[i]);

libraries/PWMAudio/src/PWMAudio.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,17 @@ bool PWMAudio::setPWMFrequency(int newFreq) {
9595
}
9696

9797
bool PWMAudio::setFrequency(int frequency) {
98+
if (frequency == _sampleRate) {
99+
return true; // We're already at the right speed
100+
}
98101
if (_pacer < 0) {
99102
return false;
100103
}
101104
uint16_t _pacer_D, _pacer_N;
102-
/*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*/
105+
// 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
103106
find_pacer_fraction(frequency, &_pacer_D, &_pacer_N);
104107
dma_timer_set_fraction(_pacer, _pacer_N, _pacer_D);
108+
_sampleRate = frequency;
105109
return true;
106110
}
107111

@@ -140,15 +144,15 @@ bool PWMAudio::begin() {
140144

141145
setPWMFrequency(_freq);
142146

143-
/*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.*/
147+
// 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.
144148
_pacer = dma_claim_unused_timer(false);
145-
/*When no unused timer is found, return*/
149+
// When no unused timer is found, return
146150
if (_pacer < 0) {
147151
return false;
148152
}
149153
uint16_t _pacer_D = 0;
150154
uint16_t _pacer_N = 0;
151-
/*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*/
155+
// 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
152156
find_pacer_fraction(_sampleRate, &_pacer_D, &_pacer_N);
153157
dma_timer_set_fraction(_pacer, _pacer_N, _pacer_D);
154158
int _pacer_dreq = dma_get_timer_dreq(_pacer);

0 commit comments

Comments
 (0)