Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
- Set dacBitsPerSample in example
- Set config to use large memory buffer for all NRF52
- Add calculation for setting frequency
  • Loading branch information
TMRh20 committed Dec 17, 2023
1 parent 686c3c8 commit dbd9c86
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions examples/NRF52_PDM_PWMTest/NRF52_PDM_PWMTest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ void setup() {
aaAudio.begin(1, 1);
aaAudio.autoAdjust = 0;
aaAudio.adcBitsPerSample = 16; // 16-bit audio at 16khz is the default on NRF52 and cannot be modified currently (in progress)
aaAudio.dacBitsPerSample = 16;
aaAudio.setSampleRate(16000);

}
Expand Down
2 changes: 1 addition & 1 deletion src/AutoAnalog_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#define MAX_BUFFER_SIZE 256

#elif defined ESP32 || defined ARDUINO_ARCH_NRF52840
#elif defined ESP32 || defined ARDUINO_ARCH_NRF52840 || defined (ARDUINO_ARCH_NRF52) || defined ARDUINO_NRF52840_FEATHER

#define MAX_BUFFER_SIZE 1600

Expand Down
14 changes: 10 additions & 4 deletions src/NRF52840/AutoAnalogAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void AutoAnalog::begin(bool enADC, bool enDAC){
NRF_PWM0->ENABLE = (PWM_ENABLE_ENABLE_Enabled << PWM_ENABLE_ENABLE_Pos);
NRF_PWM0->MODE = (PWM_MODE_UPDOWN_Up << PWM_MODE_UPDOWN_Pos);
NRF_PWM0->PRESCALER = (PWM_PRESCALER_PRESCALER_DIV_1 << PWM_PRESCALER_PRESCALER_Pos);
NRF_PWM0->COUNTERTOP = (1003 << PWM_COUNTERTOP_COUNTERTOP_Pos); //1 msec
NRF_PWM0->COUNTERTOP = (((16000000/DEFAULT_FREQUENCY) + 5) << PWM_COUNTERTOP_COUNTERTOP_Pos); //1 msec
NRF_PWM0->LOOP = (1 << PWM_LOOP_CNT_Pos);
NRF_PWM0->DECODER = (PWM_DECODER_LOAD_Common << PWM_DECODER_LOAD_Pos) | (PWM_DECODER_MODE_RefreshCount << PWM_DECODER_MODE_Pos);
NRF_PWM0->SEQ[0].PTR = ((uint32_t)(&dacBuf0[0]) << PWM_SEQ_PTR_PTR_Pos);
Expand All @@ -211,6 +211,7 @@ void AutoAnalog::begin(bool enADC, bool enDAC){

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

NRF_PWM0->COUNTERTOP = (((16000000/sampRate) + 5) << PWM_COUNTERTOP_COUNTERTOP_Pos);

}

Expand Down Expand Up @@ -272,13 +273,18 @@ void AutoAnalog::feedDAC(uint8_t dacChannel, uint32_t samples, bool startInterru
if(millis() - timer > 10){ break; }
NRF_PWM0->EVENTS_SEQEND[0] = 0;
}
memcpy(dacBuf0, dacBuffer16, samples);

if(dacBitsPerSample > 8){
memcpy(dacBuf0, dacBuffer16, samples);
}else{
for(int i=0; i<samples; i++){
dacBuf0[i] = dacBuffer[i] << 8;
}
}
NRF_PWM0->SEQ[0].PTR = ((uint32_t)(&dacBuf0[0]) << PWM_SEQ_PTR_PTR_Pos);
NRF_PWM0->SEQ[0].CNT = (samples << PWM_SEQ_CNT_CNT_Pos);
NRF_PWM0->TASKS_SEQSTART[0] = 1;



}

/****************************************************************************/
Expand Down

0 comments on commit dbd9c86

Please sign in to comment.