compilation support for audioreactive

putting all parts that will not compile between #ifdef .... #endif
Tested on S3, cannot say what will happen on other MCUs
This commit is contained in:
Frank 2022-09-11 00:12:51 +02:00
parent 56f2513aba
commit ac10b3a5c7
2 changed files with 53 additions and 3 deletions

View File

@ -949,10 +949,20 @@ class AudioReactive : public Usermod {
// Reset I2S peripheral for good measure // Reset I2S peripheral for good measure
i2s_driver_uninstall(I2S_NUM_0); i2s_driver_uninstall(I2S_NUM_0);
periph_module_reset(PERIPH_I2S0_MODULE); #if !defined(CONFIG_IDF_TARGET_ESP32C3)
periph_module_reset(PERIPH_I2S0_MODULE); // not possible on -C3
#endif
delay(100); // Give that poor microphone some time to setup. delay(100); // Give that poor microphone some time to setup.
switch (dmType) { switch (dmType) {
#if defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32S3)
// stub cases for not-yet-supported I2S modes on other ESP32 chips
case 0: //ADC analog
case 3: //MCLK
case 4: //SPH0645
#if defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32C3)
case 5: //PDM Microphone
#endif
#endif
case 1: case 1:
DEBUGSR_PRINT(F("AR: Generic I2S Microphone - ")); DEBUGSR_PRINTLN(F(I2S_MIC_CHANNEL_TEXT)); DEBUGSR_PRINT(F("AR: Generic I2S Microphone - ")); DEBUGSR_PRINTLN(F(I2S_MIC_CHANNEL_TEXT));
audioSource = new I2SSource(SAMPLE_RATE, BLOCK_SIZE); audioSource = new I2SSource(SAMPLE_RATE, BLOCK_SIZE);
@ -965,24 +975,34 @@ class AudioReactive : public Usermod {
delay(100); delay(100);
if (audioSource) audioSource->initialize(sdaPin, sclPin, i2swsPin, i2ssdPin, i2sckPin, mclkPin); if (audioSource) audioSource->initialize(sdaPin, sclPin, i2swsPin, i2ssdPin, i2sckPin, mclkPin);
break; break;
#if !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32C3) && !defined(CONFIG_IDF_TARGET_ESP32S3)
// SPH0645 is currently only possible on "classic" ESP32
case 3: case 3:
DEBUGSR_PRINT(F("AR: SPH0645 Microphone - ")); DEBUGSR_PRINTLN(F(I2S_MIC_CHANNEL_TEXT)); DEBUGSR_PRINT(F("AR: SPH0645 Microphone - ")); DEBUGSR_PRINTLN(F(I2S_MIC_CHANNEL_TEXT));
audioSource = new SPH0654(SAMPLE_RATE, BLOCK_SIZE); audioSource = new SPH0654(SAMPLE_RATE, BLOCK_SIZE);
delay(100); delay(100);
audioSource->initialize(i2swsPin, i2ssdPin, i2sckPin); audioSource->initialize(i2swsPin, i2ssdPin, i2sckPin);
break; break;
#endif
#if !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32C3) && !defined(CONFIG_IDF_TARGET_ESP32S3)
// MCLK routing currently only works on "classic" ESP32
case 4: case 4:
DEBUGSR_PRINT(F("AR: Generic I2S Microphone with Master Clock - ")); DEBUGSR_PRINTLN(F(I2S_MIC_CHANNEL_TEXT)); DEBUGSR_PRINT(F("AR: Generic I2S Microphone with Master Clock - ")); DEBUGSR_PRINTLN(F(I2S_MIC_CHANNEL_TEXT));
audioSource = new I2SSource(SAMPLE_RATE, BLOCK_SIZE); audioSource = new I2SSource(SAMPLE_RATE, BLOCK_SIZE);
delay(100); delay(100);
if (audioSource) audioSource->initialize(i2swsPin, i2ssdPin, i2sckPin, mclkPin); if (audioSource) audioSource->initialize(i2swsPin, i2ssdPin, i2sckPin, mclkPin);
break; break;
#endif
#if !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32C3)
case 5: case 5:
DEBUGSR_PRINT(F("AR: I2S PDM Microphone - ")); DEBUGSR_PRINTLN(F(I2S_MIC_CHANNEL_TEXT)); DEBUGSR_PRINT(F("AR: I2S PDM Microphone - ")); DEBUGSR_PRINTLN(F(I2S_MIC_CHANNEL_TEXT));
audioSource = new I2SSource(SAMPLE_RATE, BLOCK_SIZE); audioSource = new I2SSource(SAMPLE_RATE, BLOCK_SIZE);
delay(100); delay(100);
if (audioSource) audioSource->initialize(i2swsPin, i2ssdPin); if (audioSource) audioSource->initialize(i2swsPin, i2ssdPin);
break; break;
#endif
#if !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32C3) && !defined(CONFIG_IDF_TARGET_ESP32S3)
// ADC over I2S is only possible on "classic" ESP32
case 0: case 0:
default: default:
DEBUGSR_PRINTLN(F("AR: Analog Microphone (left channel only).")); DEBUGSR_PRINTLN(F("AR: Analog Microphone (left channel only)."));
@ -990,6 +1010,7 @@ class AudioReactive : public Usermod {
delay(100); delay(100);
if (audioSource) audioSource->initialize(audioPin); if (audioSource) audioSource->initialize(audioPin);
break; break;
#endif
} }
delay(250); // give microphone enough time to initialise delay(250); // give microphone enough time to initialise
@ -1529,12 +1550,18 @@ class AudioReactive : public Usermod {
void appendConfigData() void appendConfigData()
{ {
oappend(SET_F("dd=addDropdown('AudioReactive','digitalmic:type');")); oappend(SET_F("dd=addDropdown('AudioReactive','digitalmic:type');"));
#if !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32C3) && !defined(CONFIG_IDF_TARGET_ESP32S3)
oappend(SET_F("addOption(dd,'Generic Analog',0);")); oappend(SET_F("addOption(dd,'Generic Analog',0);"));
#endif
oappend(SET_F("addOption(dd,'Generic I2S',1);")); oappend(SET_F("addOption(dd,'Generic I2S',1);"));
oappend(SET_F("addOption(dd,'ES7243',2);")); oappend(SET_F("addOption(dd,'ES7243',2);"));
#if !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32C3) && !defined(CONFIG_IDF_TARGET_ESP32S3)
oappend(SET_F("addOption(dd,'SPH0654',3);")); oappend(SET_F("addOption(dd,'SPH0654',3);"));
oappend(SET_F("addOption(dd,'Generic I2S with Mclk',4);")); oappend(SET_F("addOption(dd,'Generic I2S with Mclk',4);"));
#endif
#if !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32C3)
oappend(SET_F("addOption(dd,'Generic I2S PDM',5);")); oappend(SET_F("addOption(dd,'Generic I2S PDM',5);"));
#endif
oappend(SET_F("dd=addDropdown('AudioReactive','cfg:AGC');")); oappend(SET_F("dd=addDropdown('AudioReactive','cfg:AGC');"));
oappend(SET_F("addOption(dd,'Off',0);")); oappend(SET_F("addOption(dd,'Off',0);"));
oappend(SET_F("addOption(dd,'Normal',1);")); oappend(SET_F("addOption(dd,'Normal',1);"));

View File

@ -5,14 +5,21 @@
#include <driver/i2s.h> #include <driver/i2s.h>
#include <driver/adc.h> #include <driver/adc.h>
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0) #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0)
#if !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32S3) && !defined(CONFIG_IDF_TARGET_ESP32C3)
#include <driver/adc_deprecated.h> #include <driver/adc_deprecated.h>
#include <driver/adc_types_deprecated.h> #include <driver/adc_types_deprecated.h>
#endif #endif
#endif
//#include <driver/i2s_std.h> //#include <driver/i2s_std.h>
//#include <driver/i2s_pdm.h> //#include <driver/i2s_pdm.h>
//#include <driver/gpio.h> //#include <driver/gpio.h>
// see https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/hw-reference/chip-series-comparison.html#related-documents
// and https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/api-reference/peripherals/i2s.html#overview-of-all-modes
#if defined(CONFIG_IDF_TARGET_ESP32C2) || defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32C5) || defined(CONFIG_IDF_TARGET_ESP32C6) || defined(CONFIG_IDF_TARGET_ESP32H2)
#error This audio reactive usermod does not support ESP32-C2, ESP32-C3 or ESP32-S2.
#endif
/* ToDo: remove. ES7243 is controlled via compiler defines /* ToDo: remove. ES7243 is controlled via compiler defines
Until this configuration is moved to the webinterface Until this configuration is moved to the webinterface
@ -137,10 +144,12 @@ class I2SSource : public AudioSource {
if (i2sckPin != I2S_PIN_NO_CHANGE) { if (i2sckPin != I2S_PIN_NO_CHANGE) {
if (!pinManager.allocatePin(i2sckPin, true, PinOwner::UM_Audioreactive)) return; if (!pinManager.allocatePin(i2sckPin, true, PinOwner::UM_Audioreactive)) return;
} else { } else {
#if !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32C3)
// This is an I2S PDM microphone, these microphones only use a clock and // This is an I2S PDM microphone, these microphones only use a clock and
// data line, to make it simpler to debug, use the WS pin as CLK and SD // data line, to make it simpler to debug, use the WS pin as CLK and SD
// pin as DATA // pin as DATA
_config.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM); // Change mode to pdm if clock pin not provided _config.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM); // Change mode to pdm if clock pin not provided. PDM is not supported on ESP32-S2. PDM RX not supported on ESP32-C3
#endif
} }
// Reserve the master clock pin if provided // Reserve the master clock pin if provided
@ -222,6 +231,8 @@ class I2SSource : public AudioSource {
protected: protected:
void _routeMclk(int8_t mclkPin) { void _routeMclk(int8_t mclkPin) {
#if !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32C3) && !defined(CONFIG_IDF_TARGET_ESP32S3)
// this way of MCLK routing only works on "classic" ESP32
/* Enable the mclk routing depending on the selected mclk pin /* Enable the mclk routing depending on the selected mclk pin
Only I2S_NUM_0 is supported Only I2S_NUM_0 is supported
*/ */
@ -235,6 +246,9 @@ class I2SSource : public AudioSource {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD_CLK_OUT2); PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD_CLK_OUT2);
WRITE_PERI_REG(PIN_CTRL, 0xFF00); WRITE_PERI_REG(PIN_CTRL, 0xFF00);
} }
#else
#warning FIX ME!
#endif
} }
i2s_config_t _config; i2s_config_t _config;
@ -307,6 +321,9 @@ public:
int8_t pin_ES7243_SCL; int8_t pin_ES7243_SCL;
}; };
#if !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32C3) && !defined(CONFIG_IDF_TARGET_ESP32S3)
// ADC over I2S is only availeable in "classic" ESP32
/* ADC over I2S Microphone /* ADC over I2S Microphone
This microphone is an ADC pin sampled via the I2S interval This microphone is an ADC pin sampled via the I2S interval
This allows to use the I2S API to obtain ADC samples with high sample rates This allows to use the I2S API to obtain ADC samples with high sample rates
@ -489,6 +506,7 @@ class I2SAdcSource : public I2SSource {
int8_t _audioPin; int8_t _audioPin;
int8_t _myADCchannel = 0x0F; // current ADC channel for analog input. 0x0F means "undefined" int8_t _myADCchannel = 0x0F; // current ADC channel for analog input. 0x0F means "undefined"
}; };
#endif
/* SPH0645 Microphone /* SPH0645 Microphone
This is an I2S microphone with some timing quirks that need This is an I2S microphone with some timing quirks that need
@ -502,7 +520,12 @@ class SPH0654 : public I2SSource {
void initialize(uint8_t i2swsPin, uint8_t i2ssdPin, uint8_t i2sckPin, int8_t = I2S_PIN_NO_CHANGE, int8_t = I2S_PIN_NO_CHANGE, int8_t = I2S_PIN_NO_CHANGE) { void initialize(uint8_t i2swsPin, uint8_t i2ssdPin, uint8_t i2sckPin, int8_t = I2S_PIN_NO_CHANGE, int8_t = I2S_PIN_NO_CHANGE, int8_t = I2S_PIN_NO_CHANGE) {
I2SSource::initialize(i2swsPin, i2ssdPin, i2sckPin); I2SSource::initialize(i2swsPin, i2ssdPin, i2sckPin);
#if !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32C3) && !defined(CONFIG_IDF_TARGET_ESP32S3)
// these registers are only existing in "classic" ESP32
REG_SET_BIT(I2S_TIMING_REG(I2S_NUM_0), BIT(9)); REG_SET_BIT(I2S_TIMING_REG(I2S_NUM_0), BIT(9));
REG_SET_BIT(I2S_CONF_REG(I2S_NUM_0), I2S_RX_MSB_SHIFT); REG_SET_BIT(I2S_CONF_REG(I2S_NUM_0), I2S_RX_MSB_SHIFT);
#else
#warning FIX ME! Please.
#endif
} }
}; };