Skip to content

Commit

Permalink
Adjust sample rates & stereo mode
Browse files Browse the repository at this point in the history
- Enable Stereo mode for NRF52840
- Set Stereo mode to MONO by default
- Enable 24kHz sample rates for NRF52840
  • Loading branch information
TMRh20 committed Oct 18, 2024
1 parent db738ef commit 26c71ac
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/AutoAnalogAudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 20 additions & 4 deletions src/NRF52840/AutoAnalogAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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__
Expand Down

0 comments on commit 26c71ac

Please sign in to comment.