diff --git a/libraries/PDM/examples/PDMSerialPlotter/PDMSerialPlotter.ino b/libraries/PDM/examples/PDMSerialPlotter/PDMSerialPlotter.ino index 0116d88d..7f0dc5e1 100644 --- a/libraries/PDM/examples/PDMSerialPlotter/PDMSerialPlotter.ino +++ b/libraries/PDM/examples/PDMSerialPlotter/PDMSerialPlotter.ino @@ -18,7 +18,7 @@ volatile int samplesRead; void setup() { Serial.begin(9600); - while (!Serial); + while (!Serial) yield(); // configure the data receive callback PDM.onReceive(onPDMdata); @@ -31,7 +31,7 @@ void setup() { // - a 16 kHz sample rate if (!PDM.begin(1, 16000)) { Serial.println("Failed to start PDM!"); - while (1); + while (1) yield(); } } diff --git a/libraries/PDM/src/PDM.cpp b/libraries/PDM/src/PDM.cpp index 6b8d4def..8a219193 100644 --- a/libraries/PDM/src/PDM.cpp +++ b/libraries/PDM/src/PDM.cpp @@ -58,9 +58,29 @@ int PDMClass::begin(int channels, long sampleRate) _channels = channels; // Enable high frequency oscillator if not already enabled - if (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) { - NRF_CLOCK->TASKS_HFCLKSTART = 1; - while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) { } + uint8_t sd_en = 0; + sd_softdevice_is_enabled(&sd_en); + + if (sd_en) + { + uint32_t is_running; + sd_clock_hfclk_is_running(&is_running); + + if (!is_running) { + sd_clock_hfclk_request(); + + while(!is_running) { + yield(); + sd_clock_hfclk_is_running(&is_running); + } + } + } + else + { + if (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) { + NRF_CLOCK->TASKS_HFCLKSTART = 1; + while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) yield(); + } } // configure the sample rate and channels