From f08291b34a336c641bf34747bf5f8e4e414f72d5 Mon Sep 17 00:00:00 2001 From: TMRh20 Date: Fri, 1 Nov 2024 05:34:08 -0600 Subject: [PATCH] nRF52: Docs n small changes to examples - Allow configuration of PDM pins via variables pwrPin, dinPin, clkPin & gain - Add documentation for pin config stuff & buffer sizing - Fix examples to just choose SD card pin, not speed --- .../NRF52_SD_I2S_RadioTX.ino | 2 +- .../NRF52_SD_Playback_I2S.ino | 2 +- .../NRF52_SD_Playback_I2S_Auto.ino | 2 +- src/AutoAnalogAudio.h | 76 +++++++++++++++++-- src/NRF52840/AutoAnalogAudio.cpp | 11 +-- 5 files changed, 78 insertions(+), 15 deletions(-) diff --git a/examples/Platforms/NRF52/NRF52_Radio/NRF52_SD_I2S_RadioTX/NRF52_SD_I2S_RadioTX.ino b/examples/Platforms/NRF52/NRF52_Radio/NRF52_SD_I2S_RadioTX/NRF52_SD_I2S_RadioTX.ino index 6f23ba2..873240f 100644 --- a/examples/Platforms/NRF52/NRF52_Radio/NRF52_SD_I2S_RadioTX/NRF52_SD_I2S_RadioTX.ino +++ b/examples/Platforms/NRF52/NRF52_Radio/NRF52_SD_I2S_RadioTX/NRF52_SD_I2S_RadioTX.ino @@ -35,7 +35,7 @@ void setup() { radio.stopListening(); Serial.print("Init SD card..."); - if (!SD.begin(10000000, 2)) { + if (!SD.begin(2)) { Serial.println("init failed!"); return; } diff --git a/examples/Platforms/NRF52/NRF52_SD_Playback_I2S/NRF52_SD_Playback_I2S.ino b/examples/Platforms/NRF52/NRF52_SD_Playback_I2S/NRF52_SD_Playback_I2S.ino index c2c5420..aac2224 100644 --- a/examples/Platforms/NRF52/NRF52_SD_Playback_I2S/NRF52_SD_Playback_I2S.ino +++ b/examples/Platforms/NRF52/NRF52_SD_Playback_I2S/NRF52_SD_Playback_I2S.ino @@ -42,7 +42,7 @@ void setup() { Serial.begin(115200); Serial.print("Init SD card..."); - if (!SD.begin(10000000, 2)) { + if (!SD.begin(2)) { Serial.println("init failed!"); return; } diff --git a/examples/Platforms/NRF52/NRF52_SD_Playback_I2S_Auto/NRF52_SD_Playback_I2S_Auto.ino b/examples/Platforms/NRF52/NRF52_SD_Playback_I2S_Auto/NRF52_SD_Playback_I2S_Auto.ino index 6ba7801..68788f2 100644 --- a/examples/Platforms/NRF52/NRF52_SD_Playback_I2S_Auto/NRF52_SD_Playback_I2S_Auto.ino +++ b/examples/Platforms/NRF52/NRF52_SD_Playback_I2S_Auto/NRF52_SD_Playback_I2S_Auto.ino @@ -47,7 +47,7 @@ void setup() { Serial.begin(115200); Serial.print("Init SD card..."); - if (!SD.begin(2, 32000000)) { + if (!SD.begin(2)) { Serial.println("init failed!"); return; } diff --git a/src/AutoAnalogAudio.h b/src/AutoAnalogAudio.h index 1333fab..73f402a 100644 --- a/src/AutoAnalogAudio.h +++ b/src/AutoAnalogAudio.h @@ -195,7 +195,6 @@ class AutoAnalog TaskHandle_t dacTaskHandle; #endif - /**@}*/ #if defined (ARDUINO_ARCH_NRF52840) || defined (ARDUINO_ARCH_NRF52) && !defined ARDUINO_NRF52840_FEATHER && defined __MBED__ inline static uint8_t aCtr; @@ -224,13 +223,74 @@ class AutoAnalog uint32_t sampleCounter; //void DACC_Handler(); #endif -#if defined (ARDUINO_ARCH_NRF52840) || defined (ARDUINO_ARCH_NRF52) - bool micOn; + +#if defined (ARDUINO_ARCH_NRF52840) || defined (ARDUINO_ARCH_NRF52) || defined (DOXYGEN_FORCED) + /**@}*/ + /** + * @name Section for nRF52 Devices Only + * + * This section is for nRF52 devices only + * + * + */ + /**@{*/ + + /** + * Set the Power Pin for PDM + * By default this is PIN_PDM_PWR + * If PIN_PDM_PWR is not defined, it is set to -1 by default + * Configure this before calling `begin()` + */ int pwrPin; + + /** + * Set the Input Pin for PDM + * By default this is PIN_PDM_DIN + * If PIN_PDM_DIN is not defined, it is set to 35 by default + * Configure this before calling `begin()` + */ int dinPin; + + /** + * Set the Clock Pin for PDM + * By default this is PIN_PDM_CLK + * If PIN_PDM_CLK is not defined, it is set to 36 by default + * Configure this before calling `begin()` + */ int clkPin; + + /** + * Set the Gain for PDM + * By default this is set to 40 + * Configure this before calling `begin()` + */ int8_t gain; + + /** + * Enable I2S on nRF52 + * By default this is disabled (analog output using PWM) + * + * @code + * aaAudio.begin(1,1,3); + * @endcode + * @param Set to 1 for I2S DAC, 2 for I2S ADC, 3 for both + */ uint8_t useI2S; + + /** + * Configure the pins and ports for nRF52 using GPIO numbers before calling `begin()` + * Defaults: + * I2S_PIN_MCK = 2; + * I2S_PORT_MCK = 0; + * I2S_PIN_SCK = 3; + * I2S_PORT_SCK = 0; + * I2S_PIN_LRCK = 29; + * I2S_PORT_LRCK = 0; + * I2S_PIN_SDOUT = 5; + * I2S_PORT_SDOUT = 0; + * I2S_PIN_SDIN = 4; + * I2S_PORT_SDIN = 0; + */ uint16_t I2S_PIN_MCK; uint8_t I2S_PORT_MCK; uint16_t I2S_PIN_SCK; @@ -241,12 +301,18 @@ class AutoAnalog uint8_t I2S_PORT_SDOUT; uint16_t I2S_PIN_SDIN; uint8_t I2S_PORT_SDIN; + + /** + * Set the maximum buffer size for nRF52 + * The internal buffers are all allocated dynamically. + * By default the MAX_BUFFER_SIZE is defined in AutoAnalogAudio_config.h + * Override that value for internal buffers by setting this before calling `begin()` + */ uint32_t maxBufferSize; #endif - + /**@}*/ private: - /** * @name Internal interface. * diff --git a/src/NRF52840/AutoAnalogAudio.cpp b/src/NRF52840/AutoAnalogAudio.cpp index 1ae7cb4..d5968bb 100644 --- a/src/NRF52840/AutoAnalogAudio.cpp +++ b/src/NRF52840/AutoAnalogAudio.cpp @@ -87,6 +87,10 @@ AutoAnalog::AutoAnalog(){ adcReady = true; adcBitsPerSample = 8; + pwrPin = PIN_PDM_PWR; + dinPin = PIN_PDM_DIN; + clkPin = PIN_PDM_CLK; + gain = -1; dacBitsPerSample = 8; autoAdjust = true; @@ -96,7 +100,6 @@ AutoAnalog::AutoAnalog(){ aSize = MAX_BUFFER_SIZE; aCtr = 0; - micOn = 0; sampleCounter = 0; maxBufferSize = 0; I2S_PIN_MCK = 2; @@ -471,10 +474,6 @@ if(useI2S == 2 || useI2S == 3){ set_callback(adcCallback); - dinPin = PIN_PDM_DIN; - clkPin = PIN_PDM_CLK; - pwrPin = PIN_PDM_PWR; - gain = -1; // Enable high frequency oscillator if not already enabled if (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) { @@ -513,7 +512,6 @@ if(useI2S == 2 || useI2S == 3){ if (pwrPin > -1) { pinMode(pwrPin, OUTPUT); digitalWrite(pwrPin, HIGH); - micOn=1; }else{ } @@ -558,7 +556,6 @@ if(useI2S == 2 || useI2S == 3){ if (pwrPin > -1) { pinMode(pwrPin, OUTPUT); digitalWrite(pwrPin, HIGH); - micOn=1; }else{ }