I was going through WaveHC, updating it to use Timer3 on the 1284p, and I noticed something that looks like a bug:
//------------------------------------------------------------------------------
/** Set the player's sample rate.
*
* \param[in] samplerate The new sample rate in samples per second.
* No checks are done on the input parameter.
*/
void WaveHC::setSampleRate(uint32_t samplerate) {
if (samplerate < 500) samplerate = 500;
if (samplerate > 50000) samplerate = 50000;
// from ladayada's library.
cli();
while (TCNT0 != 0);
OCR3A = F_CPU / samplerate;
sei();
}
The library normally uses timer1, so why is the while loop here waiting for TCNT0? I believe TCNT0 is the counter for the timer used for millis(), and I can't see any reason why it should wait for that to trigger. I suspect that the intent was to wait for TCNT1 to hit 0.
Also, is waiting for the counter to hit 0 the right thing to even do here? Doesn't the counter continue to count in a loop even when interrupts are disabled? And couldn't this code potentially miss when it hits 0?