Skip to content

Commit bd11619

Browse files
committed
nRF52: Small fixes n changes
- 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
1 parent 283d6fd commit bd11619

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

src/NRF52840/AutoAnalogAudio.cpp

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ void AutoAnalog::begin(bool enADC, bool enDAC, uint8_t _useI2S){
138138

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

141-
if(!useI2S){
141+
if(useI2S == 0){
142142
NRF_PWM0->TASKS_STOP = 1;
143143
uint32_t timer = millis();
144144
while(NRF_PWM0->EVENTS_STOPPED == 0){ if(millis() - timer > 1000){break;} }
@@ -254,6 +254,12 @@ bool adcWhichBuf = 0;
254254
void AutoAnalog::getADC(uint32_t samples){
255255

256256
if(useI2S == 2 || useI2S == 3){
257+
258+
if(NRF_I2S->ENABLE == 0){
259+
NRF_I2S->ENABLE = 1;
260+
NRF_I2S->TASKS_START = 1;
261+
}
262+
257263
while(NRF_I2S->EVENTS_RXPTRUPD == 0){}
258264

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

377383
if(dacBitsPerSample > 8){
378-
memcpy(dacBuf0, dacBuffer16, samples * 2);
384+
if(whichBuf){
385+
memcpy(dacBuf0, dacBuffer16, samples * 2);
386+
}else{
387+
memcpy(dacBuf1, dacBuffer16, samples * 2);
388+
}
379389
}else{
380-
for(uint32_t i=0; i<samples; i++){
381-
dacBuf0[i] = (uint16_t)(dacBuffer[i] << 1) ;
390+
if(whichBuf){
391+
for(uint32_t i=0; i<samples; i++){
392+
dacBuf0[i] = (uint16_t)(dacBuffer[i] << 1) ;
393+
}
394+
}else{
395+
for(uint32_t i=0; i<samples; i++){
396+
dacBuf1[i] = (uint16_t)(dacBuffer[i] << 1) ;
397+
}
382398
}
399+
383400
}
384-
NRF_PWM0->SEQ[0].PTR = ((uint32_t)(&dacBuf0[0]) << PWM_SEQ_PTR_PTR_Pos);
401+
if(whichBuf){
402+
NRF_PWM0->SEQ[0].PTR = ((uint32_t)(&dacBuf0[0]) << PWM_SEQ_PTR_PTR_Pos);
403+
}else{
404+
NRF_PWM0->SEQ[0].PTR = ((uint32_t)(&dacBuf1[0]) << PWM_SEQ_PTR_PTR_Pos);
405+
}
406+
whichBuf = !whichBuf;
407+
385408
NRF_PWM0->SEQ[0].CNT = (samples << PWM_SEQ_CNT_CNT_Pos);
386409
NRF_PWM0->TASKS_SEQSTART[0] = 1;
387410
}
@@ -440,10 +463,7 @@ if(useI2S == 2 || useI2S == 3){
440463

441464
// Configure data pointer
442465
NRF_I2S->RXD.PTR = (uint32_t)adcBuf0;
443-
NRF_I2S->RXTXD.MAXCNT = 16;// / sizeof(uint32_t);
444-
445-
NRF_I2S->ENABLE = 1;
446-
NRF_I2S->TASKS_START = 1;
466+
NRF_I2S->RXTXD.MAXCNT = 16;// / sizeof(uint32_t);
447467

448468
}else{
449469

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

605-
606-
//NRF_I2S->INTENSET = I2S_INTENSET_TXPTRUPD_Enabled << I2S_INTENSET_TXPTRUPD_Pos;
607-
//NVIC_EnableIRQ(I2S_IRQn);
608-
//NRF_I2S->TXD.PTR = (uint32_t)&sine_table[0];
609-
//NRF_I2S->RXTXD.MAXCNT = sizeof(sine_table) / sizeof(uint32_t);
610-
NRF_I2S->ENABLE = 1;
611-
NRF_I2S->TASKS_START = 1;
612-
613625
}else{
614626

615627
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);

0 commit comments

Comments
 (0)