@@ -95,13 +95,17 @@ bool PWMAudio::setPWMFrequency(int newFreq) {
9595}
9696
9797bool 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