From 577ed4808d7ee760247ef31621e9c9c17303133e Mon Sep 17 00:00:00 2001 From: Sylwester <6614616+DatanoiseTV@users.noreply.github.com> Date: Mon, 30 Jan 2023 17:27:14 +0100 Subject: [PATCH 1/7] Update Audio Config for current arduino-pico SDK. --- AudioConfigRP2040.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/AudioConfigRP2040.h b/AudioConfigRP2040.h index eed5201b4..b0b3d0ca4 100644 --- a/AudioConfigRP2040.h +++ b/AudioConfigRP2040.h @@ -36,6 +36,20 @@ #define AUDIO_BITS 16 // available values are 8, 16, 24 (LEFT ALIGN in 32 bits type!!) and 32 bits // ****** END: These are define you may want to change. Best not to touch anything outside this range. ************/ +// The arduino-pico SDK has I2S Pins predefined for some boards. +#if defined(ARDUINO_ARCH_RP2040) + +#if defined(PIN_I2S_BCLK) +#define BCLK_PIN PIN_I2S_BCLK +#define WS_PIN (PIN_I2S_BCLK+) +#endif + +#if defined(PIN_I2S_DOUT) +#define DOUT_PIN PIN_I2S_DOUT +#endif + +#endif + #define BYPASS_MOZZI_OUTPUT_BUFFER true // Configuration of the I2S port, especially DMA. Set in stone here as default of the library when this was written. From ec58a32d46af02eb313793c1f0b90740834c906a Mon Sep 17 00:00:00 2001 From: DatanoiseTV Date: Mon, 13 Feb 2023 16:59:04 +0100 Subject: [PATCH 2/7] Add initial Datanoise PicoADK board support. --- AudioConfigRP2040.h | 28 ++++++++++++++++++++++++++++ MozziGuts_impl_RP2040.hpp | 8 ++++++++ 2 files changed, 36 insertions(+) diff --git a/AudioConfigRP2040.h b/AudioConfigRP2040.h index b0b3d0ca4..624ccaf33 100644 --- a/AudioConfigRP2040.h +++ b/AudioConfigRP2040.h @@ -58,6 +58,34 @@ #define BUFFER_SIZE 256 // total size of the buffer, in samples #endif +// override all definitions if the board has dedicated I2S pins +#if defined(PIN_I2S_DOUT) +// undefine to disable compiler warnings +#undef DOUT_PIN +#define DOUT_PIN PIN_I2S_DOUT +#endif + +#if defined(PIN_I2S_BCLK) +// undefine to disable compiler warnings +#undef BCLK_PIN +#undef WS_PIN +#undef LSBJ_FORMAT +#undef AUDIO_BITS +#undef BYPASS_MOZZI_OUTPUT_BUFFER +#undef BUFFERS +#undef BUFFER_SIZE +#undef RP2040_AUDIO_OUT_MODE + +#define BCLK_PIN PIN_I2S_BCLK +#define WS_PIN (PIN_I2S_BCLK+1) +#define LSBJ_FORMAT false +#define AUDIO_BITS 16 +#define BYPASS_MOZZI_OUTPUT_BUFFER true +#define BUFFERS 8 +#define BUFFER_SIZE 64 +#define RP2040_AUDIO_OUT_MODE EXTERNAL_DAC_VIA_I2S +#endif + #define AUDIO_BITS_PER_CHANNEL AUDIO_BITS diff --git a/MozziGuts_impl_RP2040.hpp b/MozziGuts_impl_RP2040.hpp index 096a292ca..c38ed0c08 100644 --- a/MozziGuts_impl_RP2040.hpp +++ b/MozziGuts_impl_RP2040.hpp @@ -242,6 +242,14 @@ static void startAudio() { i2s.setBCLK(BCLK_PIN); i2s.setDATA(DOUT_PIN); i2s.setBitsPerSample(AUDIO_BITS); + +#if (ARDUINO_DATANOISETV_PICOADK) + // Soft mute and de-emphasis for audio codec + pinMode(25, OUTPUT); + digitalWrite(25, HIGH); + pinMode(23, OUTPUT); + digitalWrite(23, LOW); +#endif #if (AUDIO_BITS > 16) i2s.setBuffers(BUFFERS, (size_t) (BUFFER_SIZE/BUFFERS), 0); From 3c4dcb5b7474409915a6b2672a5c4cbb6dd5e515 Mon Sep 17 00:00:00 2001 From: DatanoiseTV Date: Mon, 13 Feb 2023 17:06:01 +0100 Subject: [PATCH 3/7] Add random implmentation for RP2040 using the rosc. --- mozzi_rand.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mozzi_rand.cpp b/mozzi_rand.cpp index 19d5f4a9f..76a73a429 100644 --- a/mozzi_rand.cpp +++ b/mozzi_rand.cpp @@ -9,6 +9,10 @@ extern STM32ADC adc; #include #endif +#if IS_RP2040() +#include "hardware/structs/rosc.h" +#endif + // moved these out of xorshift96() so xorshift96() can be reseeded manually static unsigned long x=132456789, y=362436069, z=521288629; // static unsigned long x= analogRead(A0)+123456789; @@ -144,6 +148,16 @@ void randSeed() { x = RANDOM_REG32; y = random (0xFFFFFFFF) ^ RANDOM_REG32; z = random (0xFFFFFFFF) ^ RANDOM_REG32; +#elif IS_RP2040() + uint32_t rand_seed; + for (int i = 0; i < 32; i++) + { + bool randomBit = rosc_hw->randombit; + rand_seed = rand_seed | (randomBit << i); + } + x = random (rand_seed); + y = random (rand_seed); + z = random (rand_seed); #else #warning Automatic random seeding not implemented on this platform #endif From 8d937794b6622fdbf8f40c3ee5571219a4662201 Mon Sep 17 00:00:00 2001 From: DatanoiseTV Date: Mon, 13 Feb 2023 17:22:41 +0100 Subject: [PATCH 4/7] Increase buffer size to avoid undderuns with more complex patches. --- AudioConfigRP2040.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AudioConfigRP2040.h b/AudioConfigRP2040.h index 624ccaf33..faaeb7194 100644 --- a/AudioConfigRP2040.h +++ b/AudioConfigRP2040.h @@ -82,7 +82,7 @@ #define AUDIO_BITS 16 #define BYPASS_MOZZI_OUTPUT_BUFFER true #define BUFFERS 8 -#define BUFFER_SIZE 64 +#define BUFFER_SIZE 128 #define RP2040_AUDIO_OUT_MODE EXTERNAL_DAC_VIA_I2S #endif From f7335791b69034b820c3cc1a3492be120d45ab51 Mon Sep 17 00:00:00 2001 From: DatanoiseTV Date: Mon, 13 Feb 2023 17:38:11 +0100 Subject: [PATCH 5/7] Increase sample rate to 32768Hz for RP2040. --- hardware_defines.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/hardware_defines.h b/hardware_defines.h index db4f0b87e..264f2c08d 100644 --- a/hardware_defines.h +++ b/hardware_defines.h @@ -46,6 +46,10 @@ #define CACHED_FUNCTION_ATTR #endif +#if IS_RP2040() +#define AUDIO_RATE_PLATFORM_DEFAULT 32768 +#endif + #if IS_STM32() // This is a little silly, but with Arduino 1.8.13, including this header inside MozziGuts.cpp does not work (fails to detect the proper include path). // Putting it here, instead, seem to work. From eb3d457f80bcf93a849145ed5116fc5f7628059e Mon Sep 17 00:00:00 2001 From: DatanoiseTV Date: Mon, 13 Feb 2023 18:34:40 +0100 Subject: [PATCH 6/7] Review after PR comments. --- AudioConfigRP2040.h | 30 +++++------------------------- MozziGuts_impl_RP2040.hpp | 6 ++++++ 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/AudioConfigRP2040.h b/AudioConfigRP2040.h index faaeb7194..1edb8a6c2 100644 --- a/AudioConfigRP2040.h +++ b/AudioConfigRP2040.h @@ -36,20 +36,6 @@ #define AUDIO_BITS 16 // available values are 8, 16, 24 (LEFT ALIGN in 32 bits type!!) and 32 bits // ****** END: These are define you may want to change. Best not to touch anything outside this range. ************/ -// The arduino-pico SDK has I2S Pins predefined for some boards. -#if defined(ARDUINO_ARCH_RP2040) - -#if defined(PIN_I2S_BCLK) -#define BCLK_PIN PIN_I2S_BCLK -#define WS_PIN (PIN_I2S_BCLK+) -#endif - -#if defined(PIN_I2S_DOUT) -#define DOUT_PIN PIN_I2S_DOUT -#endif - -#endif - #define BYPASS_MOZZI_OUTPUT_BUFFER true // Configuration of the I2S port, especially DMA. Set in stone here as default of the library when this was written. @@ -58,15 +44,9 @@ #define BUFFER_SIZE 256 // total size of the buffer, in samples #endif -// override all definitions if the board has dedicated I2S pins -#if defined(PIN_I2S_DOUT) -// undefine to disable compiler warnings -#undef DOUT_PIN -#define DOUT_PIN PIN_I2S_DOUT -#endif - -#if defined(PIN_I2S_BCLK) -// undefine to disable compiler warnings +#if defined(ARDUINO_DATANOISETV_PICOADK) +// undefined the defindes to disable compiler warnings +// about redefinition. #undef BCLK_PIN #undef WS_PIN #undef LSBJ_FORMAT @@ -78,15 +58,15 @@ #define BCLK_PIN PIN_I2S_BCLK #define WS_PIN (PIN_I2S_BCLK+1) +#define DOUT_PIN PIN_I2S_DOUT #define LSBJ_FORMAT false #define AUDIO_BITS 16 #define BYPASS_MOZZI_OUTPUT_BUFFER true #define BUFFERS 8 -#define BUFFER_SIZE 128 +#define BUFFER_SIZE 256 #define RP2040_AUDIO_OUT_MODE EXTERNAL_DAC_VIA_I2S #endif - #define AUDIO_BITS_PER_CHANNEL AUDIO_BITS #define AUDIO_BIAS ((uint16_t) 1<<(AUDIO_BITS-1)) diff --git a/MozziGuts_impl_RP2040.hpp b/MozziGuts_impl_RP2040.hpp index c38ed0c08..f593ae4aa 100644 --- a/MozziGuts_impl_RP2040.hpp +++ b/MozziGuts_impl_RP2040.hpp @@ -243,8 +243,14 @@ static void startAudio() { i2s.setDATA(DOUT_PIN); i2s.setBitsPerSample(AUDIO_BITS); +// The PicoADK won't output a tone until it is unmuted. +// Move it into the Mozzi implementation so the user won't need to change mozzi code. #if (ARDUINO_DATANOISETV_PICOADK) +#warning "DATANOISETV_PICOADK audio codec" // Soft mute and de-emphasis for audio codec + i2s.setBCLK(PIN_I2S_BCLK); + i2s.setDATA(PIN_I2S_DOUT); + pinMode(25, OUTPUT); digitalWrite(25, HIGH); pinMode(23, OUTPUT); From 49ee101716222e8a8d57fd783095ffb386af196b Mon Sep 17 00:00:00 2001 From: DatanoiseTV Date: Mon, 13 Feb 2023 18:35:16 +0100 Subject: [PATCH 7/7] Remove warning. --- MozziGuts_impl_RP2040.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/MozziGuts_impl_RP2040.hpp b/MozziGuts_impl_RP2040.hpp index f593ae4aa..dd1cf4a2e 100644 --- a/MozziGuts_impl_RP2040.hpp +++ b/MozziGuts_impl_RP2040.hpp @@ -246,7 +246,6 @@ static void startAudio() { // The PicoADK won't output a tone until it is unmuted. // Move it into the Mozzi implementation so the user won't need to change mozzi code. #if (ARDUINO_DATANOISETV_PICOADK) -#warning "DATANOISETV_PICOADK audio codec" // Soft mute and de-emphasis for audio codec i2s.setBCLK(PIN_I2S_BCLK); i2s.setDATA(PIN_I2S_DOUT);