Skip to content

Commit

Permalink
nRF52: Small fixes n changes
Browse files Browse the repository at this point in the history
- Enable I2S in the getADC and feedDAC functions if not enabled - Allows users to disable the DAC/ADC then just start feeding them again
- Enable double-buffering for PWM
- Remove I2S_Enable & I2S_Tasks_Start from ADC & DAC setup functions
  • Loading branch information
TMRh20 committed Oct 30, 2024
1 parent 283d6fd commit bd11619
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions src/NRF52840/AutoAnalogAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void AutoAnalog::begin(bool enADC, bool enDAC, uint8_t _useI2S){

void AutoAnalog::setSampleRate(uint32_t sampRate, bool stereo){

if(!useI2S){
if(useI2S == 0){
NRF_PWM0->TASKS_STOP = 1;
uint32_t timer = millis();
while(NRF_PWM0->EVENTS_STOPPED == 0){ if(millis() - timer > 1000){break;} }
Expand Down Expand Up @@ -254,6 +254,12 @@ bool adcWhichBuf = 0;
void AutoAnalog::getADC(uint32_t samples){

if(useI2S == 2 || useI2S == 3){

if(NRF_I2S->ENABLE == 0){
NRF_I2S->ENABLE = 1;
NRF_I2S->TASKS_START = 1;
}

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

uint8_t divider = 2;
Expand Down Expand Up @@ -375,13 +381,30 @@ void AutoAnalog::feedDAC(uint8_t dacChannel, uint32_t samples, bool startInterru
NRF_PWM0->EVENTS_SEQEND[0] = 0;

if(dacBitsPerSample > 8){
memcpy(dacBuf0, dacBuffer16, samples * 2);
if(whichBuf){
memcpy(dacBuf0, dacBuffer16, samples * 2);
}else{
memcpy(dacBuf1, dacBuffer16, samples * 2);
}
}else{
for(uint32_t i=0; i<samples; i++){
dacBuf0[i] = (uint16_t)(dacBuffer[i] << 1) ;
if(whichBuf){
for(uint32_t i=0; i<samples; i++){
dacBuf0[i] = (uint16_t)(dacBuffer[i] << 1) ;
}
}else{
for(uint32_t i=0; i<samples; i++){
dacBuf1[i] = (uint16_t)(dacBuffer[i] << 1) ;
}
}

}
NRF_PWM0->SEQ[0].PTR = ((uint32_t)(&dacBuf0[0]) << PWM_SEQ_PTR_PTR_Pos);
if(whichBuf){
NRF_PWM0->SEQ[0].PTR = ((uint32_t)(&dacBuf0[0]) << PWM_SEQ_PTR_PTR_Pos);
}else{
NRF_PWM0->SEQ[0].PTR = ((uint32_t)(&dacBuf1[0]) << PWM_SEQ_PTR_PTR_Pos);
}
whichBuf = !whichBuf;

NRF_PWM0->SEQ[0].CNT = (samples << PWM_SEQ_CNT_CNT_Pos);
NRF_PWM0->TASKS_SEQSTART[0] = 1;
}
Expand Down Expand Up @@ -440,10 +463,7 @@ if(useI2S == 2 || useI2S == 3){

// Configure data pointer
NRF_I2S->RXD.PTR = (uint32_t)adcBuf0;
NRF_I2S->RXTXD.MAXCNT = 16;// / sizeof(uint32_t);

NRF_I2S->ENABLE = 1;
NRF_I2S->TASKS_START = 1;
NRF_I2S->RXTXD.MAXCNT = 16;// / sizeof(uint32_t);

}else{

Expand Down Expand Up @@ -602,14 +622,6 @@ void AutoAnalog::dacSetup(void){
NRF_I2S->RXD.PTR = (uint32_t)dacBuf1;
NRF_I2S->RXTXD.MAXCNT = 16;// / sizeof(uint32_t);


//NRF_I2S->INTENSET = I2S_INTENSET_TXPTRUPD_Enabled << I2S_INTENSET_TXPTRUPD_Pos;
//NVIC_EnableIRQ(I2S_IRQn);
//NRF_I2S->TXD.PTR = (uint32_t)&sine_table[0];
//NRF_I2S->RXTXD.MAXCNT = sizeof(sine_table) / sizeof(uint32_t);
NRF_I2S->ENABLE = 1;
NRF_I2S->TASKS_START = 1;

}else{

NRF_PWM0->PSEL.OUT[0] = (DEFAULT_PWM_PIN << PWM_PSEL_OUT_PIN_Pos) | (PWM_PSEL_OUT_CONNECT_Connected << PWM_PSEL_OUT_CONNECT_Pos | DEFAULT_PWM_PORT << PWM_PSEL_OUT_PORT_Pos);
Expand Down

0 comments on commit bd11619

Please sign in to comment.