Skip to content

Commit 028b64c

Browse files
committed
ESP32 i2s getClockSource
1 parent 61cc60f commit 028b64c

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

src/AudioTools/CoreAudio/AudioI2S/I2SESP32V1.h

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class I2SDriverESP32V1 {
143143
i2s_chan_handle_t &tx_chan,
144144
i2s_chan_handle_t &rx_chan, int txPin,
145145
int rxPin) = 0;
146-
146+
147147
virtual i2s_chan_config_t getChannelConfig(I2SConfigESP32V1 &cfg) = 0;
148148
// changes the sample rate
149149
virtual bool changeSampleRate(I2SConfigESP32V1 &cfg,
@@ -152,10 +152,9 @@ class I2SDriverESP32V1 {
152152
return false;
153153
}
154154

155-
protected:
155+
protected:
156156
/// 24 bits are stored in a 32 bit integer
157-
int get_bits_eff(int bits) { return ( bits == 24 )? 32 : bits; }
158-
157+
int get_bits_eff(int bits) { return (bits == 24) ? 32 : bits; }
159158
};
160159

161160
struct DriverI2S : public DriverCommon {
@@ -269,23 +268,56 @@ class I2SDriverESP32V1 {
269268
I2S_STD_CLK_DEFAULT_CONFIG((uint32_t)cfg.sample_rate);
270269
if (cfg.mclk_multiple > 0) {
271270
clk_cfg.mclk_multiple = (i2s_mclk_multiple_t)cfg.mclk_multiple;
271+
LOGI("mclk_multiple=%d", clk_cfg.mclk_multiple);
272272
} else {
273273
if (cfg.bits_per_sample == 24) {
274274
// mclk_multiple' should be the multiple of 3 while using 24-bit
275275
clk_cfg.mclk_multiple = I2S_MCLK_MULTIPLE_384;
276276
LOGI("mclk_multiple=384");
277+
} else {
278+
LOGI("mclk_multiple=%d", clk_cfg.mclk_multiple);
277279
}
278280
}
279281

280-
if (cfg.pin_mck != -1 && !cfg.is_master){
282+
// determine clock source
283+
clk_cfg.clk_src = getClockSource(cfg);
284+
285+
return clk_cfg;
286+
}
287+
288+
/// select clock source dependent on is_master and use_apll
289+
soc_periph_i2s_clk_src_t getClockSource(I2SConfigESP32V1 &cfg){
290+
soc_periph_i2s_clk_src_t result = I2S_CLK_SRC_DEFAULT;
291+
// use mclk pin as input in slave mode if supported
292+
bool is_pin_mck_input = false;
293+
if (cfg.pin_mck != -1) {
294+
if (!cfg.is_master) {
281295
#if SOC_I2S_HW_VERSION_2
282-
LOGI("clk_src=I2S_CLK_SRC_EXTERNAL");
283-
clk_cfg.clk_src = I2S_CLK_SRC_EXTERNAL;
296+
LOGI("pin_mclk is input");
297+
result = I2S_CLK_SRC_EXTERNAL;
298+
is_pin_mck_input = true;
284299
#else
285-
LOGE("clk_src=I2S_CLK_SRC_EXTERNAL not supported");
300+
LOGE("pin_mclk as input not supported");
286301
#endif
302+
}
303+
304+
if (!is_pin_mck_input) {
305+
// select clock source
306+
#if SOC_I2S_SUPPORTS_APLL
307+
if (cfg.use_apll) {
308+
result = I2S_CLK_SRC_APLL;
309+
LOGI("clk_src is I2S_CLK_SRC_APLL");
310+
}
311+
#elif SOC_I2S_SUPPORTS_PLL_F160M
312+
if (cfg.use_apll) {
313+
result = I2S_CLK_SRC_PLL_160M;
314+
LOGI("clk_src is I2S_CLK_SRC_PLL_160M");
315+
}
316+
#endif
317+
}
287318
}
288-
return clk_cfg;
319+
return result;
320+
289321
}
290322

291323
bool changeSampleRate(I2SConfigESP32V1 &cfg, i2s_chan_handle_t &tx_chan,

0 commit comments

Comments
 (0)