Skip to content

Commit

Permalink
- Adjustments for nrf52
Browse files Browse the repository at this point in the history
- Remove tasks_stop/start and en/disable from `setSampleRate()` function
- Enable double-buffering for 8-bit samples
  • Loading branch information
TMRh20 committed Oct 20, 2024
1 parent e8ab9ae commit a9a58c6
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions src/NRF52840/AutoAnalogAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ AutoAnalog::AutoAnalog(){
}

void AutoAnalog::begin(bool enADC, bool enDAC, uint8_t _useI2S){


if(enADC){
adcSetup();
Expand All @@ -115,9 +114,7 @@ void AutoAnalog::begin(bool enADC, bool enDAC, uint8_t _useI2S){
useI2S = _useI2S;
dacSetup();
}


//maybe need to call __WFE(); from the main loop for DAC

}

/****************************************************************************/
Expand All @@ -132,9 +129,6 @@ void AutoAnalog::setSampleRate(uint32_t sampRate, bool stereo){
NRF_PWM0->COUNTERTOP = (((uint16_t)((16000000/sampRate))) << PWM_COUNTERTOP_COUNTERTOP_Pos);
NRF_PWM0->TASKS_SEQSTART[0] = 1;
}else{
// Stop transmitting I2S data
NRF_I2S->TASKS_STOP = 1;
NRF_I2S->ENABLE = 0;

if(stereo){
NRF_I2S->CONFIG.CHANNELS = I2S_CONFIG_CHANNELS_CHANNELS_STEREO << I2S_CONFIG_CHANNELS_CHANNELS_Pos;
Expand All @@ -160,8 +154,6 @@ void AutoAnalog::setSampleRate(uint32_t sampRate, bool stereo){
NRF_I2S->CONFIG.MCKFREQ = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV23 << I2S_CONFIG_MCKFREQ_MCKFREQ_Pos;
NRF_I2S->CONFIG.RATIO = I2S_CONFIG_RATIO_RATIO_32X << I2S_CONFIG_RATIO_RATIO_Pos;
}
NRF_I2S->ENABLE = 1;
NRF_I2S->TASKS_START = 1;
}

if(sampRate <= 16000){
Expand Down Expand Up @@ -253,14 +245,20 @@ void AutoAnalog::feedDAC(uint8_t dacChannel, uint32_t samples, bool startInterru

if(useI2S){

while(NRF_I2S->EVENTS_TXPTRUPD == 0){}


while(NRF_I2S->EVENTS_TXPTRUPD == 0){}
if(dacBitsPerSample == 8){
for(uint32_t i=0; i< samples; i++){
dacBuf0[i] = dacBuffer[i] << 7;
if(whichBuf){
for(uint32_t i=0; i< samples; i++){
dacBuf0[i] = dacBuffer[i] << 7;
}
NRF_I2S->TXD.PTR = (uint32_t)&dacBuf0[0];
}else{
for(uint32_t i=0; i< samples; i++){
dacBuf1[i] = dacBuffer[i] << 7;
}
NRF_I2S->TXD.PTR = (uint32_t)&dacBuf1[0];
}
NRF_I2S->TXD.PTR = (uint32_t)&dacBuf0[0];

}else
if(dacBitsPerSample == 16){
if(whichBuf){
Expand All @@ -270,9 +268,10 @@ void AutoAnalog::feedDAC(uint8_t dacChannel, uint32_t samples, bool startInterru
memcpy(dacBuf1,dacBuffer16,samples * 2);
NRF_I2S->TXD.PTR = (uint32_t)&dacBuf1[0];
}
whichBuf = !whichBuf;
}

whichBuf = !whichBuf;

uint8_t divider = 2;
if(dacBitsPerSample == 16){
divider = 2;
Expand Down

0 comments on commit a9a58c6

Please sign in to comment.