From dbd9c8670ed2f0b1b84b70994266a7a2d052d38c Mon Sep 17 00:00:00 2001 From: TMRh20 Date: Sun, 17 Dec 2023 06:37:31 -0600 Subject: [PATCH] Minor changes - Set dacBitsPerSample in example - Set config to use large memory buffer for all NRF52 - Add calculation for setting frequency --- examples/NRF52_PDM_PWMTest/NRF52_PDM_PWMTest.ino | 1 + src/AutoAnalog_config.h | 2 +- src/NRF52840/AutoAnalogAudio.cpp | 14 ++++++++++---- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/examples/NRF52_PDM_PWMTest/NRF52_PDM_PWMTest.ino b/examples/NRF52_PDM_PWMTest/NRF52_PDM_PWMTest.ino index 53d7092..a9b1a2b 100644 --- a/examples/NRF52_PDM_PWMTest/NRF52_PDM_PWMTest.ino +++ b/examples/NRF52_PDM_PWMTest/NRF52_PDM_PWMTest.ino @@ -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); } diff --git a/src/AutoAnalog_config.h b/src/AutoAnalog_config.h index 12deec9..420cb92 100644 --- a/src/AutoAnalog_config.h +++ b/src/AutoAnalog_config.h @@ -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 diff --git a/src/NRF52840/AutoAnalogAudio.cpp b/src/NRF52840/AutoAnalogAudio.cpp index 577b89e..200688c 100644 --- a/src/NRF52840/AutoAnalogAudio.cpp +++ b/src/NRF52840/AutoAnalogAudio.cpp @@ -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); @@ -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); } @@ -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; iSEQ[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; - - } /****************************************************************************/