From 26c71ac56c4a3cf0342738ef0476f7378d9dbf41 Mon Sep 17 00:00:00 2001 From: TMRh20 Date: Fri, 18 Oct 2024 05:26:46 -0600 Subject: [PATCH] Adjust sample rates & stereo mode - Enable Stereo mode for NRF52840 - Set Stereo mode to MONO by default - Enable 24kHz sample rates for NRF52840 --- src/AutoAnalogAudio.h | 4 ++-- src/NRF52840/AutoAnalogAudio.cpp | 24 ++++++++++++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/AutoAnalogAudio.h b/src/AutoAnalogAudio.h index 0d0f376..45f0aad 100644 --- a/src/AutoAnalogAudio.h +++ b/src/AutoAnalogAudio.h @@ -110,9 +110,9 @@ class AutoAnalog /** Set sample rate. 0 enables the default rate specified in AutoAnalog_config.h * @param sampRate This sets the defined sample rate ie: 32000 is 32Khz - * @param stereo Only used for the ESP32, this sets stereo or mono output and affects the sample rate + * @param stereo Only used for the ESP32 & NRF52840 this sets stereo or mono output and affects the sample rate */ - void setSampleRate(uint32_t sampRate = 0, bool stereo = true); + void setSampleRate(uint32_t sampRate = 0, bool stereo = false); /** Function called by DAC IRQ */ void dacHandler(void); diff --git a/src/NRF52840/AutoAnalogAudio.cpp b/src/NRF52840/AutoAnalogAudio.cpp index f0a1651..0eea837 100644 --- a/src/NRF52840/AutoAnalogAudio.cpp +++ b/src/NRF52840/AutoAnalogAudio.cpp @@ -338,15 +338,23 @@ void AutoAnalog::setSampleRate(uint32_t sampRate, bool stereo){ }else if(dacBitsPerSample == 24){ NRF_I2S->CONFIG.SWIDTH = I2S_CONFIG_SWIDTH_SWIDTH_24BIT << I2S_CONFIG_SWIDTH_SWIDTH_Pos; - } */ - + } */ + + if(stereo){ + NRF_I2S->CONFIG.CHANNELS = I2S_CONFIG_CHANNELS_CHANNELS_STEREO << I2S_CONFIG_CHANNELS_CHANNELS_Pos; + } + if(sampRate <= 16000){ NRF_I2S->CONFIG.MCKFREQ = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV16 << I2S_CONFIG_MCKFREQ_MCKFREQ_Pos; NRF_I2S->CONFIG.RATIO = I2S_CONFIG_RATIO_RATIO_128X << I2S_CONFIG_RATIO_RATIO_Pos; }else if(sampRate <= 20000){ - NRF_I2S->CONFIG.MCKFREQ = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV8 << I2S_CONFIG_MCKFREQ_MCKFREQ_Pos; - NRF_I2S->CONFIG.RATIO = I2S_CONFIG_RATIO_RATIO_256X << I2S_CONFIG_RATIO_RATIO_Pos; + NRF_I2S->CONFIG.MCKFREQ = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV15 << I2S_CONFIG_MCKFREQ_MCKFREQ_Pos; + NRF_I2S->CONFIG.RATIO = I2S_CONFIG_RATIO_RATIO_128X << I2S_CONFIG_RATIO_RATIO_Pos; + }else + if(sampRate <= 24000){ + NRF_I2S->CONFIG.MCKFREQ = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV11 << I2S_CONFIG_MCKFREQ_MCKFREQ_Pos; + NRF_I2S->CONFIG.RATIO = I2S_CONFIG_RATIO_RATIO_128X << I2S_CONFIG_RATIO_RATIO_Pos; }else if(sampRate <= 32000){ NRF_I2S->CONFIG.MCKFREQ = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV16 << I2S_CONFIG_MCKFREQ_MCKFREQ_Pos; @@ -377,6 +385,14 @@ void AutoAnalog::setSampleRate(uint32_t sampRate, bool stereo){ nrf_pdm_clock_set(myPDM,NRF_PDM_FREQ_1280K); #endif }else + if(sampRate <= 24000){ + NRF_PDM->RATIO = ((PDM_RATIO_RATIO_Ratio80 << PDM_RATIO_RATIO_Pos) & PDM_RATIO_RATIO_Msk); + #if defined __MBED__ + nrf_pdm_clock_set(NRF_PDM_FREQ_2000K); + #else + nrf_pdm_clock_set(myPDM, NRF_PDM_FREQ_2000K); + #endif + }else if(sampRate <= 32000){ NRF_PDM->RATIO = ((PDM_RATIO_RATIO_Ratio64 << PDM_RATIO_RATIO_Pos) & PDM_RATIO_RATIO_Msk); #if defined __MBED__