Skip to content

Commit 09f867e

Browse files
sandeepmistrycmaglie
authored andcommitted
I2S: Use 8 MHz oscillator source if 48MHz divider does not fit in 8 bits
1 parent e7ebbdf commit 09f867e

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

libraries/I2S/src/I2S.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -398,15 +398,24 @@ void I2SClass::onReceive(void(*function)(void))
398398

399399
void I2SClass::enableClock(int divider)
400400
{
401+
int div = SystemCoreClock / divider;
402+
int src = GCLK_GENCTRL_SRC_DFLL48M_Val;
403+
404+
if (div > 255) {
405+
// divider is too big, use 8 MHz oscillator instead
406+
div = 8000000 / divider;
407+
src = GCLK_GENCTRL_SRC_OSC8M_Val;
408+
}
409+
401410
// configure the clock divider
402411
while (GCLK->STATUS.bit.SYNCBUSY);
403412
GCLK->GENDIV.bit.ID = _clockGenerator;
404-
GCLK->GENDIV.bit.DIV = SystemCoreClock / divider;
413+
GCLK->GENDIV.bit.DIV = div;
405414

406415
// use the DFLL as the source
407416
while (GCLK->STATUS.bit.SYNCBUSY);
408417
GCLK->GENCTRL.bit.ID = _clockGenerator;
409-
GCLK->GENCTRL.bit.SRC = GCLK_GENCTRL_SRC_DFLL48M_Val;
418+
GCLK->GENCTRL.bit.SRC = src;
410419
GCLK->GENCTRL.bit.IDC = 1;
411420
GCLK->GENCTRL.bit.GENEN = 1;
412421

0 commit comments

Comments
 (0)