@@ -95,13 +95,17 @@ bool PWMAudio::setPWMFrequency(int newFreq) {
95
95
}
96
96
97
97
bool PWMAudio::setFrequency (int frequency) {
98
+ if (frequency == _sampleRate) {
99
+ return true ; // We're already at the right speed
100
+ }
98
101
if (_pacer < 0 ) {
99
102
return false ;
100
103
}
101
104
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
103
106
find_pacer_fraction (frequency, &_pacer_D, &_pacer_N);
104
107
dma_timer_set_fraction (_pacer, _pacer_N, _pacer_D);
108
+ _sampleRate = frequency;
105
109
return true ;
106
110
}
107
111
@@ -140,15 +144,15 @@ bool PWMAudio::begin() {
140
144
141
145
setPWMFrequency (_freq);
142
146
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.
144
148
_pacer = dma_claim_unused_timer (false );
145
- /* When no unused timer is found, return*/
149
+ // When no unused timer is found, return
146
150
if (_pacer < 0 ) {
147
151
return false ;
148
152
}
149
153
uint16_t _pacer_D = 0 ;
150
154
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
152
156
find_pacer_fraction (_sampleRate, &_pacer_D, &_pacer_N);
153
157
dma_timer_set_fraction (_pacer, _pacer_N, _pacer_D);
154
158
int _pacer_dreq = dma_get_timer_dreq (_pacer);
0 commit comments