diff --git a/examples/PlayOpusFromLittleFS/PlayOpusFromLittleFS.ino b/examples/PlayOpusFromLittleFS/PlayOpusFromLittleFS.ino index 8361b03..6ac4199 100644 --- a/examples/PlayOpusFromLittleFS/PlayOpusFromLittleFS.ino +++ b/examples/PlayOpusFromLittleFS/PlayOpusFromLittleFS.ino @@ -42,12 +42,8 @@ void setup() { audioLogger = &Serial; file = new AudioFileSourceLittleFS("/gs-16b-2c-44100hz.opus"); -#ifdef ARDUINO_ARCH_RP2040 - out = new AudioOutputI2S(48000, 26, 22); -#else out = new AudioOutputI2S(); // out->SetPinout(0, 1, 2); // Set the pinout if needed -#endif opus = new AudioGeneratorOpus(); opus->begin(file, out); } diff --git a/src/AudioOutputI2S.cpp b/src/AudioOutputI2S.cpp index 80e98ce..2e8419f 100644 --- a/src/AudioOutputI2S.cpp +++ b/src/AudioOutputI2S.cpp @@ -20,7 +20,7 @@ #include #ifdef ESP32 -#include "driver/i2s.h" +#include #elif defined(ARDUINO_ARCH_RP2040) || ARDUINO_ESP8266_MAJOR >= 3 #include #elif ARDUINO_ESP8266_MAJOR < 3 @@ -28,42 +28,62 @@ #endif #include "AudioOutputI2S.h" -#if defined(ESP32) || defined(ESP8266) -AudioOutputI2S::AudioOutputI2S(int port, int output_mode, int dma_buf_count, int use_apll) { - this->portNo = port; - this->i2sOn = false; - this->dma_buf_count = dma_buf_count; - if (output_mode != EXTERNAL_I2S && output_mode != INTERNAL_DAC && output_mode != INTERNAL_PDM) { - output_mode = EXTERNAL_I2S; - } - this->output_mode = output_mode; - this->use_apll = use_apll; - - //set defaults +AudioOutputI2S::AudioOutputI2S() { + i2sOn = false; mono = false; lsb_justified = false; channels = 2; - hertz = 0; // Needs to be application-set + hertz = 44100; // Needs to be application-set +#ifdef ESP32 bclkPin = 26; wclkPin = 25; doutPin = 22; - mclkPin = 0; - use_mclk = false; +#else + bclkPin = 26; + wclkPin = 27; + doutPin = 28; +#endif + mclkPin = -1; SetGain(1.0); + SetBuffers(); // Default DMA sizes } +bool AudioOutputI2S::SetBuffers(int dmaBufferCount, int dmaBufferBytes) { + if (i2sOn || (dmaBufferCount < 3) || (dmaBufferBytes & 3)) { + return false; + } + _buffers = dmaBufferCount; + _bufferWords = dmaBufferBytes / 4; + return true; +} + +#if defined(ESP32) || defined(ESP8266) +AudioOutputI2S::AudioOutputI2S(int port, int output_mode, int dma_buf_count, int use_apll) : AudioOutputI2S() { + (void) port; + (void) output_mode; + (void) use_apll; + SetBuffers(dma_buf_count, 128 * 4); +#ifdef ESP32 + _useAPLL = use_apll; +#endif +} + +#if SOC_CLK_APLL_SUPPORTED +bool AudioOutputI2S::SetUseAPLL() { + if (i2sOn) { + return false; + } + _useAPLL = true; + return true; +} +#endif + #elif defined(ARDUINO_ARCH_RP2040) -AudioOutputI2S::AudioOutputI2S(long sampleRate, pin_size_t sck, pin_size_t data) { - i2sOn = false; - mono = false; - channels = 2; - hertz = sampleRate; +AudioOutputI2S::AudioOutputI2S(long sampleRate, pin_size_t sck, pin_size_t data) : AudioOutputI2S() { + (void) sampleRate; bclkPin = sck; + wclkPin = sck + 1; doutPin = data; - mclkPin = 0; - use_mclk = false; - swap_clocks = false; - SetGain(1.0); } #endif @@ -71,68 +91,49 @@ AudioOutputI2S::~AudioOutputI2S() { stop(); } -bool AudioOutputI2S::SetPinout() { -#ifdef ESP32 - if (output_mode == INTERNAL_DAC || output_mode == INTERNAL_PDM) { - return false; // Not allowed - } - - i2s_pin_config_t pins = { -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0) - .mck_io_num = use_mclk ? mclkPin : I2S_PIN_NO_CHANGE, -#endif - .bck_io_num = bclkPin, - .ws_io_num = wclkPin, - .data_out_num = doutPin, - .data_in_num = I2S_PIN_NO_CHANGE - }; - i2s_set_pin((i2s_port_t)portNo, &pins); - return true; -#else - return false; -#endif -} - -bool AudioOutputI2S::SetPinout(int bclk, int wclk, int dout) { - bclkPin = bclk; - wclkPin = wclk; - doutPin = dout; - use_mclk = false; +bool AudioOutputI2S::SwapClocks(bool swap) { if (i2sOn) { - return SetPinout(); + return false; + } + if (swap) { + auto t = bclkPin; + bclkPin = wclkPin; + wclkPin = t; } - return true; } bool AudioOutputI2S::SetPinout(int bclk, int wclk, int dout, int mclk) { + if (i2sOn) { + return false; + } bclkPin = bclk; wclkPin = wclk; doutPin = dout; -#if defined(ESP32) || defined(ARDUINO_ARCH_RP2040) mclkPin = mclk; - use_mclk = true; - if (i2sOn) { - return SetPinout(); - } -#else - (void)mclk; -#endif return true; } bool AudioOutputI2S::SetRate(int hz) { - if (this->hertz == hz) { + if (hertz == hz) { return true; } - this->hertz = hz; + hertz = hz; if (i2sOn) { + auto adj = AdjustI2SRate(hz); // Possibly scale for NoDAC #ifdef ESP32 - i2s_set_sample_rates((i2s_port_t)portNo, AdjustI2SRate(hz)); + i2s_std_clk_config_t clk_cfg; + clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG((uint32_t)adj); +#if SOC_CLK_APLL_SUPPORTED + clk_cfg.clk_src = _useAPLL ? i2s_clock_src_t::I2S_CLK_SRC_APLL : i2s_clock_src_t::I2S_CLK_SRC_DEFAULT; +#endif + i2s_channel_disable(_tx_handle); + i2s_channel_reconfig_std_clock(_tx_handle, &clk_cfg); + i2s_channel_enable(_tx_handle); #elif defined(ESP8266) - i2s_set_rate(AdjustI2SRate(hz)); + i2s_set_rate(adj); #elif defined(ARDUINO_ARCH_RP2040) - i2s.setFrequency(hz); + i2s.setFrequency(adj); #endif } return true; @@ -152,159 +153,92 @@ bool AudioOutputI2S::SetOutputModeMono(bool mono) { } bool AudioOutputI2S::SetLsbJustified(bool lsbJustified) { + if (i2sOn) { + return false; + } this->lsb_justified = lsbJustified; return true; } -bool AudioOutputI2S::SwapClocks(bool swap_clocks) { - if (i2sOn) { - return false; // Not allowed - } - this->swap_clocks = swap_clocks; - return true; -} - -bool AudioOutputI2S::SetMclk(bool enabled) { - (void)enabled; +bool AudioOutputI2S::begin() { #ifdef ESP32 - if (output_mode == INTERNAL_DAC || output_mode == INTERNAL_PDM) { - return false; // Not allowed - } + i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_MASTER); + chan_cfg.dma_desc_num = _buffers; + chan_cfg.dma_frame_num = _bufferWords; + assert(ESP_OK == i2s_new_channel(&chan_cfg, &_tx_handle, nullptr)); - use_mclk = enabled; -#elif defined(ARDUINO_ARCH_RP2040) - use_mclk = enabled; + i2s_std_config_t std_cfg = { + .clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(hertz), + .slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO), + .gpio_cfg = { + .mclk = mclkPin < 0 ? I2S_GPIO_UNUSED : (gpio_num_t)mclkPin, + .bclk = bclkPin < 0 ? I2S_GPIO_UNUSED : (gpio_num_t)bclkPin, + .ws = wclkPin < 0 ? I2S_GPIO_UNUSED : (gpio_num_t)wclkPin, + .dout = (gpio_num_t)doutPin, + .din = I2S_GPIO_UNUSED, + .invert_flags = { + .mclk_inv = false, + .bclk_inv = false, + .ws_inv = false, + }, + }, + }; +#if SOC_CLK_APLL_SUPPORTED + std_cfg.clk_cfg.clk_src = _useAPLL ? i2s_clock_src_t::I2S_CLK_SRC_APLL : i2s_clock_src_t::I2S_CLK_SRC_DEFAULT; #endif - return true; -} + std_cfg.slot_cfg.bit_shift = !lsb_justified; // I2S = shift, LSBJ = no shift + assert(ESP_OK == i2s_channel_init_std_mode(_tx_handle, &std_cfg)); -bool AudioOutputI2S::begin(bool txDAC) { -#ifdef ESP32 - if (!i2sOn) { - if (use_apll == APLL_AUTO) { - // don't use audio pll on buggy rev0 chips - use_apll = APLL_DISABLE; - //esp_chip_info_t out_info; - //esp_chip_info(&out_info); - //if (out_info.revision > 0) - { - use_apll = APLL_ENABLE; - } - } + // Fill w/0s to start off + int16_t a[2] = {0, 0}; + size_t written = 0; + do { + i2s_channel_preload_data(_tx_handle, (void*)a, sizeof(a), &written); + } while (written); - i2s_mode_t mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX); - if (output_mode == INTERNAL_DAC) { -#if CONFIG_IDF_TARGET_ESP32 - mode = (i2s_mode_t)(mode | I2S_MODE_DAC_BUILT_IN); -#else - return false; -#endif - } else if (output_mode == INTERNAL_PDM) { -#if CONFIG_IDF_TARGET_ESP32 - mode = (i2s_mode_t)(mode | I2S_MODE_PDM); -#else - return false; -#endif - } - - i2s_comm_format_t comm_fmt; - if (output_mode == INTERNAL_DAC) { -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 2, 0) - comm_fmt = (i2s_comm_format_t) I2S_COMM_FORMAT_STAND_MSB; -#else - comm_fmt = (i2s_comm_format_t) I2S_COMM_FORMAT_I2S_MSB; -#endif - } else if (lsb_justified) { -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 2, 0) - comm_fmt = (i2s_comm_format_t) I2S_COMM_FORMAT_STAND_MSB; -#else - comm_fmt = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_LSB); -#endif - } else { -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 2, 0) - comm_fmt = (i2s_comm_format_t)(I2S_COMM_FORMAT_STAND_I2S); -#else - comm_fmt = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB); -#endif - } - - i2s_config_t i2s_config_dac = { - .mode = mode, - .sample_rate = 44100, - .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, - .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, - .communication_format = comm_fmt, - .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // lowest interrupt priority - .dma_buf_count = dma_buf_count, - .dma_buf_len = 128, - .use_apll = use_apll, // Use audio PLL - .tx_desc_auto_clear = true, // Silence on underflow - .fixed_mclk = use_mclk, // Unused -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0) - .mclk_multiple = I2S_MCLK_MULTIPLE_256, // Unused - .bits_per_chan = I2S_BITS_PER_CHAN_DEFAULT // Use bits per sample -#endif - }; - audioLogger->printf("+%d %p\n", portNo, &i2s_config_dac); - if (i2s_driver_install((i2s_port_t)portNo, &i2s_config_dac, 0, NULL) != ESP_OK) { - audioLogger->println("ERROR: Unable to install I2S drives\n"); - } - if (output_mode == INTERNAL_DAC || output_mode == INTERNAL_PDM) { -#if CONFIG_IDF_TARGET_ESP32 - i2s_set_pin((i2s_port_t)portNo, NULL); - i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN); -#else - return false; -#endif - } else { - SetPinout(); - } - i2s_zero_dma_buffer((i2s_port_t)portNo); - } + i2sOn = (ESP_OK == i2s_channel_enable(_tx_handle)); #elif defined(ESP8266) - (void)dma_buf_count; - (void)use_apll; if (!i2sOn) { orig_bck = READ_PERI_REG(PERIPHS_IO_MUX_MTDO_U); orig_ws = READ_PERI_REG(PERIPHS_IO_MUX_GPIO2_U); #ifdef I2S_HAS_BEGIN_RXTX_DRIVE_CLOCKS - if (!i2s_rxtxdrive_begin(false, true, false, txDAC)) { + if (!i2s_rxtxdrive_begin(false, true, false, true)) { return false; } #else if (!i2s_rxtx_begin(false, true)) { return false; } - if (!txDAC) { - audioLogger->printf_P(PSTR("I2SNoDAC: esp8266 arduino core should be upgraded to avoid conflicts with SPI\n")); - } #endif + i2sOn = true; } #elif defined(ARDUINO_ARCH_RP2040) - (void)txDAC; if (!i2sOn) { i2s.setSysClk(hertz); - i2s.setBCLK(bclkPin); + if (wclkPin == bclkPin + 1) { + i2s.setBCLK(bclkPin); + } else if (wclkPin == bclkPin - 1) { + // Swapped! + i2s.setBCLK(bclkPin - 1); + i2s.swapClocks(); + } else { + audioLogger->printf_P(PSTR("I2S: BCLK and WCLK must be adjacent\n")); + return false; + } i2s.setDATA(doutPin); - if (use_mclk) { + if (mclkPin >= 0) { i2s.setMCLK(mclkPin); i2s.setMCLKmult(256); } - if (swap_clocks) { - i2s.swapClocks(); - } i2s.setBitsPerSample(16); - i2s.begin(hertz); + i2sOn = i2s.begin(hertz); } #endif - i2sOn = true; SetRate(hertz ? hertz : 44100); // Default return true; } bool AudioOutputI2S::ConsumeSample(int16_t sample[2]) { - - //return if we haven't called ::begin yet if (!i2sOn) { return false; } @@ -322,18 +256,10 @@ bool AudioOutputI2S::ConsumeSample(int16_t sample[2]) { } #ifdef ESP32 uint32_t s32; - if (output_mode == INTERNAL_DAC) { - int16_t l = Amplify(ms[LEFTCHANNEL]) + 0x8000; - int16_t r = Amplify(ms[RIGHTCHANNEL]) + 0x8000; - s32 = ((r & 0xffff) << 16) | (l & 0xffff); - } else { - s32 = ((Amplify(ms[RIGHTCHANNEL])) << 16) | (Amplify(ms[LEFTCHANNEL]) & 0xffff); - } - //"i2s_write_bytes" has been removed in the ESP32 Arduino 2.0.0, use "i2s_write" instead. - // return i2s_write_bytes((i2s_port_t)portNo, (const char *)&s32, sizeof(uint32_t), 0); + s32 = ((Amplify(ms[RIGHTCHANNEL])) << 16) | (Amplify(ms[LEFTCHANNEL]) & 0xffff); - size_t i2s_bytes_written; - i2s_write((i2s_port_t)portNo, (const char*)&s32, sizeof(uint32_t), &i2s_bytes_written, 0); + size_t i2s_bytes_written = sizeof(uint32_t); + i2s_channel_write(_tx_handle, (const char*)&s32, sizeof(uint32_t), &i2s_bytes_written, 10); return i2s_bytes_written; #elif defined(ESP8266) uint32_t s32 = ((Amplify(ms[RIGHTCHANNEL])) << 16) | (Amplify(ms[LEFTCHANNEL]) & 0xffff); @@ -347,7 +273,7 @@ bool AudioOutputI2S::ConsumeSample(int16_t sample[2]) { void AudioOutputI2S::flush() { #ifdef ESP32 // makes sure that all stored DMA samples are consumed / played - int buffersize = 128 * this->dma_buf_count; + int buffersize = _buffers * _bufferWords; int16_t samples[2] = {0x0, 0x0}; for (int i = 0; i < buffersize; i++) { while (!ConsumeSample(samples)) { @@ -365,9 +291,8 @@ bool AudioOutputI2S::stop() { } #ifdef ESP32 - i2s_zero_dma_buffer((i2s_port_t)portNo); - audioLogger->printf("UNINSTALL I2S\n"); - i2s_driver_uninstall((i2s_port_t)portNo); //stop & destroy i2s driver + i2s_channel_disable(_tx_handle); + i2s_del_channel(_tx_handle); #elif defined(ESP8266) i2s_end(); #elif defined(ARDUINO_ARCH_RP2040) diff --git a/src/AudioOutputI2S.h b/src/AudioOutputI2S.h index f1ba7b3..4f23966 100644 --- a/src/AudioOutputI2S.h +++ b/src/AudioOutputI2S.h @@ -25,59 +25,80 @@ #if defined(ARDUINO_ARCH_RP2040) #include #include +#elif defined(ESP32) +#include #endif class AudioOutputI2S : public AudioOutput { public: #if defined(ESP32) || defined(ESP8266) - AudioOutputI2S(int port = 0, int output_mode = EXTERNAL_I2S, int dma_buf_count = 8, int use_apll = APLL_DISABLE); +#ifndef ESP8266 + [[deprecated("Use AudioOutputI2S(dmaBuffers, dmaBufferBytes) for normal I2S, AudioOutputPDM() for PDM mode, or AudioOutputInternalDAC() for DAC mode")]] +#endif + AudioOutputI2S(int port, int output_mode = EXTERNAL_I2S, int dma_buf_count = 8, int use_apll = APLL_DISABLE); enum : int { APLL_AUTO = -1, APLL_ENABLE = 1, APLL_DISABLE = 0 }; enum : int { EXTERNAL_I2S = 0, INTERNAL_DAC = 1, INTERNAL_PDM = 2 }; + AudioOutputI2S(); #elif defined(ARDUINO_ARCH_RP2040) - AudioOutputI2S(long sampleRate = 44100, pin_size_t sck = 26, pin_size_t data = 28); + [[deprecated]] AudioOutputI2S(long sampleRate, pin_size_t sck = 26, pin_size_t data = 28); + AudioOutputI2S(); #endif - bool SetPinout(int bclkPin, int wclkPin, int doutPin); - bool SetPinout(int bclkPin, int wclkPin, int doutPin, int mclkPin); +#if defined(ESP32) || defined(ESP8266) +#define __DMA_BUFF_BYTES (4608 / 2) +#else +#define __DMA_BUFF_BYTES 4608 +#endif + bool SetBuffers(int dmaBufferCount = 5, int dmaBufferBytes = __DMA_BUFF_BYTES); +#undef __DMA_BUFF_BYTES + bool SetPinout(int bclkPin, int wclkPin, int doutPin, int mclkPin = -1); + +#if defined(ESP32) && SOC_CLK_APLL_SUPPORTED + bool SetUseAPLL(); +#endif + virtual ~AudioOutputI2S() override; virtual bool SetRate(int hz) override; virtual bool SetChannels(int channels) override; - virtual bool begin() override { - return begin(true); - } + virtual bool begin() override; virtual bool ConsumeSample(int16_t sample[2]) override; virtual void flush() override; virtual bool stop() override; - bool begin(bool txDAC); + [[deprecated("Use AudioOutputInternalDAC() for DAC output, begin(void) for normal I2S")]] bool begin(bool txDAC); bool SetOutputModeMono(bool mono); // Force mono output no matter the input bool SetLsbJustified(bool lsbJustified); // Allow supporting non-I2S chips, e.g. PT8211 - bool SetMclk(bool enabled); // Enable MCLK output (if supported) - bool SwapClocks(bool swap_clocks); // Swap BCLK and WCLK + [[deprecated("Use SetPinout() with an MCLK pin to enable")]] bool SetMclk(bool enabled) { + (void) enabled; + return true; + } + [[deprecated("Use SetPinout() and specify the bclk/wclk to automatically set the clock swapping")]] bool SwapClocks(bool swap_clocks); // Swap BCLK and WCLK protected: - bool SetPinout(); virtual int AdjustI2SRate(int hz) { return hz; } - uint8_t portNo; - int output_mode; bool mono; - int lsb_justified; + bool lsb_justified; bool i2sOn; - int dma_buf_count; - int use_apll; - bool use_mclk; - bool swap_clocks; + + int8_t bclkPin; + int8_t wclkPin; + int8_t doutPin; + int8_t mclkPin; + + size_t _buffers; + size_t _bufferWords; + +#ifdef ESP32 + bool _useAPLL; + // I2S IDF object and info + i2s_chan_handle_t _tx_handle; +#elif defined(ARDUINO_ARCH_RP2040) + // Normal software-defined I2S + I2S i2s; +#elif defined(ESP8266) // We can restore the old values and free up these pins when in NoDAC mode uint32_t orig_bck; uint32_t orig_ws; - - uint8_t bclkPin; - uint8_t wclkPin; - uint8_t doutPin; - uint8_t mclkPin; - -#if defined(ARDUINO_ARCH_RP2040) - I2S i2s; #endif }; diff --git a/src/AudioOutputI2SNoDAC.cpp b/src/AudioOutputI2SNoDAC.cpp index 8a3efc3..5b5677b 100644 --- a/src/AudioOutputI2SNoDAC.cpp +++ b/src/AudioOutputI2SNoDAC.cpp @@ -20,7 +20,7 @@ #include #ifdef ESP32 -#include "driver/i2s.h" +#include #elif defined(ARDUINO_ARCH_RP2040) || ARDUINO_ESP8266_MAJOR >= 3 #include #elif ARDUINO_ESP8266_MAJOR < 3 @@ -39,7 +39,8 @@ // So this new constructor adds the ability to pass both port and sck to the underlying class, but // uses the same defaults in the AudioOutputI2S constructor. // -AudioOutputI2SNoDAC::AudioOutputI2SNoDAC(int port, int sck) : AudioOutputI2S(44100, sck, port) { +AudioOutputI2SNoDAC::AudioOutputI2SNoDAC(int port, int sck) : AudioOutputI2S() { + SetPinout(sck, sck + 1, port); // TODO - allow RP2040 to disable unused pins like ESP SetOversampling(32); lastSamp = 0; cumErr = 0; @@ -47,7 +48,8 @@ AudioOutputI2SNoDAC::AudioOutputI2SNoDAC(int port, int sck) : AudioOutputI2S(441 #else -AudioOutputI2SNoDAC::AudioOutputI2SNoDAC(int port) : AudioOutputI2S(port, false) { +AudioOutputI2SNoDAC::AudioOutputI2SNoDAC(int port) : AudioOutputI2S() { + SetPinout(-1, -1, port); SetOversampling(32); lastSamp = 0; cumErr = 0; @@ -59,8 +61,8 @@ AudioOutputI2SNoDAC::AudioOutputI2SNoDAC(int port) : AudioOutputI2S(port, false) } #endif - AudioOutputI2SNoDAC::~AudioOutputI2SNoDAC() { + stop(); } bool AudioOutputI2SNoDAC::SetOversampling(int os) { @@ -120,11 +122,9 @@ bool AudioOutputI2SNoDAC::ConsumeSample(int16_t sample[2]) { // Either send complete pulse stream or nothing #ifdef ESP32 - size_t i2s_bytes_written; - i2s_write((i2s_port_t)portNo, (const char *)dsBuff, sizeof(uint32_t) * (oversample / 32), &i2s_bytes_written, 0); - if (!i2s_bytes_written) { - return false; - } + size_t i2s_bytes_written = sizeof(uint32_t); + i2s_channel_write(_tx_handle, (const char *)dsBuff, sizeof(uint32_t) * (oversample / 32), &i2s_bytes_written, 10); + return i2s_bytes_written ? true : false; #elif defined(ESP8266) if (!i2s_write_sample_nb(dsBuff[0])) { return false; // No room at the inn diff --git a/src/AudioOutputI2SNoDAC.h b/src/AudioOutputI2SNoDAC.h index 7d1af34..a5aeeb4 100644 --- a/src/AudioOutputI2SNoDAC.h +++ b/src/AudioOutputI2SNoDAC.h @@ -35,9 +35,6 @@ public: #endif virtual ~AudioOutputI2SNoDAC() override; - virtual bool begin() override { - return AudioOutputI2S::begin(false); - } virtual bool ConsumeSample(int16_t sample[2]) override; bool SetOversampling(int os); diff --git a/src/AudioOutputInternalDAC.h b/src/AudioOutputInternalDAC.h index fcb31aa..e5aa067 100644 --- a/src/AudioOutputInternalDAC.h +++ b/src/AudioOutputInternalDAC.h @@ -1,3 +1,6 @@ + +#pragma once +#if defined(ESP32) && SOC_DAC_SUPPORTED #include "AudioOutput.h" #include @@ -17,7 +20,7 @@ class AudioOutputInternalDAC : public AudioOutput { dac_continuous_config_t dac_config = { .chan_mask = channels == 2 ? DAC_CHANNEL_MASK_ALL : DAC_CHANNEL_MASK_CH0, .desc_num = 2, // we dont need more - .buf_size = buffer_sz, + .buf_size = (size_t)buffer_sz, .freq_hz = hertz, .offset = 0, .clk_src = DAC_DIGI_CLK_SRC_APLL, @@ -149,3 +152,4 @@ public: return true; } }; +#endif diff --git a/src/AudioOutputSPDIF.cpp b/src/AudioOutputSPDIF.cpp index 76f76c2..4cd2b58 100644 --- a/src/AudioOutputSPDIF.cpp +++ b/src/AudioOutputSPDIF.cpp @@ -41,8 +41,8 @@ #include #if defined(ESP32) -#include "driver/i2s.h" -#include "soc/rtc.h" +#include +#include #elif defined(ESP8266) #include "driver/SinglePinI2SDriver.h" #endif @@ -84,122 +84,111 @@ static const uint16_t spdif_bmclookup[256] PROGMEM = { 0x32aa, 0xb2aa, 0xd2aa, 0x52aa, 0xcaaa, 0x4aaa, 0x2aaa, 0xaaaa }; -AudioOutputSPDIF::AudioOutputSPDIF(int dout_pin, int port, int dma_buf_count) { - this->portNo = port; -#if defined(ESP32) - // Configure ESP32 I2S to roughly compatible to ESP8266 peripheral - i2s_config_t i2s_config_spdif = { - .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX), - .sample_rate = 88200, // 2 x sampling_rate - .bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT, // 32bit words - .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, // Right than left -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 2, 0) - .communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_STAND_I2S), -#else - .communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB), -#endif - .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // lowest interrupt priority - .dma_buf_count = dma_buf_count, - .dma_buf_len = DMA_BUF_SIZE_DEFAULT, // bigger buffers, reduces interrupts - .use_apll = true, // Audio PLL is needed for low clock jitter - .tx_desc_auto_clear = true, // Silence on underflow - .fixed_mclk = 0, // Unused -#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR >= 3 - .mclk_multiple = I2S_MCLK_MULTIPLE_512, // Unused - .bits_per_chan = I2S_BITS_PER_CHAN_DEFAULT // Use bits per sample -#elif ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0) - .mclk_multiple = I2S_MCLK_MULTIPLE_DEFAULT, // Unused - .bits_per_chan = I2S_BITS_PER_CHAN_DEFAULT // Use bits per sample -#endif - }; - if (i2s_driver_install((i2s_port_t)portNo, &i2s_config_spdif, 0, NULL) != ESP_OK) { - audioLogger->println(F("ERROR: Unable to install I2S drivers")); - return; - } - i2s_zero_dma_buffer((i2s_port_t)portNo); - SetPinout(I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, dout_pin); + +AudioOutputSPDIF::AudioOutputSPDIF(int dout_pin) { + doutPin = dout_pin; +#ifdef ESP32 rate_multiplier = 2; // 2x32bit words -#elif defined(ESP8266) - (void) dout_pin; - if (!I2SDriver.begin(dma_buf_count, DMA_BUF_SIZE_DEFAULT)) { - audioLogger->println(F("ERROR: Unable to start I2S driver")); - return; - } +#else rate_multiplier = 4; // 4x16 bit words +#endif + mono = false; + channels = 2; + frame_num = 0; + SetGain(1.0); + frame_num = 0; + hertz = 44100; + i2sOn = false; + SetBuffers(DMA_BUF_COUNT_DEFAULT, DMA_BUF_SIZE_DEFAULT * 4); +} + +bool AudioOutputSPDIF::SetBuffers(int dmaBufferCount, int dmaBufferBytes) { + if (i2sOn || (dmaBufferCount < 3) || (dmaBufferBytes & 3)) { + return false; + } + _buffers = dmaBufferCount; + _bufferWords = dmaBufferBytes / 4; + return true; +} + + +AudioOutputSPDIF::AudioOutputSPDIF(int dout_pin, int port, int dma_buf_count) : AudioOutputSPDIF(dout_pin) { + (void) port; + (void) dma_buf_count; + SetBuffers(dma_buf_count, DMA_BUF_SIZE_DEFAULT * 4); +} + +bool AudioOutputSPDIF::begin() { + if (i2sOn) { + return false; + } +#ifdef ESP32 + i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_MASTER); + chan_cfg.dma_desc_num = _buffers; + chan_cfg.dma_frame_num = _bufferWords; + assert(ESP_OK == i2s_new_channel(&chan_cfg, &_tx_handle, nullptr)); + + i2s_std_config_t std_cfg = { + .clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(((uint32_t)hertz) * 2), + .slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_32BIT, I2S_SLOT_MODE_STEREO), + .gpio_cfg = { + .mclk = I2S_GPIO_UNUSED, + .bclk = I2S_GPIO_UNUSED, + .ws = I2S_GPIO_UNUSED, + .dout = (gpio_num_t)doutPin, + .din = I2S_GPIO_UNUSED, + .invert_flags = { + .mclk_inv = false, + .bclk_inv = false, + .ws_inv = false, + }, + }, + }; +#if SOC_CLK_APLL_SUPPORTED + std_cfg.clk_cfg.clk_src = i2s_clock_src_t::I2S_CLK_SRC_APLL; +#endif + assert(ESP_OK == i2s_channel_init_std_mode(_tx_handle, &std_cfg)); +#else + if (!I2SDriver.begin(_buffers, _bufferWords)) { + audioLogger->println(F("ERROR: Unable to start I2S driver")); + return false; + } #endif i2sOn = true; mono = false; channels = 2; frame_num = 0; SetGain(1.0); - hertz = 0; - SetRate(44100); + return true; } AudioOutputSPDIF::~AudioOutputSPDIF() { -#if defined(ESP32) - if (i2sOn) { - i2s_stop((i2s_port_t)this->portNo); - audioLogger->printf("UNINSTALL I2S\n"); - i2s_driver_uninstall((i2s_port_t)this->portNo); //stop & destroy i2s driver - } -#elif defined(ESP8266) - if (i2sOn) { - I2SDriver.stop(); - } -#endif - i2sOn = false; + stop(); } -bool AudioOutputSPDIF::SetPinout(int bclk, int wclk, int dout) { -#if defined(ESP32) - i2s_pin_config_t pins = { -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0) - .mck_io_num = 0, // unused -#endif - .bck_io_num = bclk, - .ws_io_num = wclk, - .data_out_num = dout, - .data_in_num = I2S_PIN_NO_CHANGE - }; - if (i2s_set_pin((i2s_port_t)portNo, &pins) != ESP_OK) { - audioLogger->println("ERROR setting up S/PDIF I2S pins\n"); +bool AudioOutputSPDIF::SetPinout(int dout) { + if (i2sOn) { return false; } + doutPin = dout; return true; -#else - (void) bclk; - (void) wclk; - (void) dout; - return false; -#endif } bool AudioOutputSPDIF::SetRate(int hz) { - if (!i2sOn) { - return false; - } if (hz < 32000) { return false; } - if (hz == this->hertz) { + if (hz == hertz) { return true; } - this->hertz = hz; + hertz = hz; int adjustedHz = AdjustI2SRate(hz); #if defined(ESP32) - if (i2s_set_sample_rates((i2s_port_t)portNo, adjustedHz) == ESP_OK) { -#if defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR < 3) - if (adjustedHz == 88200) { - // Manually fix the APLL rate for 44100. - // See: https://github.com/espressif/esp-idf/issues/2634 - // sdm0 = 28, sdm1 = 8, sdm2 = 5, odir = 0 -> 88199.977 - rtc_clk_apll_enable(1, 28, 8, 5, 0); - } -#endif - } else { - audioLogger->println("ERROR changing S/PDIF sample rate"); - } + i2s_std_clk_config_t clk_cfg; + clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG((uint32_t)adjustedHz); + i2s_channel_disable(_tx_handle); + i2s_channel_reconfig_std_clock(_tx_handle, &clk_cfg); + i2s_channel_enable(_tx_handle); #elif defined(ESP8266) I2SDriver.setRate(adjustedHz); audioLogger->printf_P(PSTR("S/PDIF rate set: %.3f\n"), I2SDriver.getActualRate() / 4); @@ -224,10 +213,6 @@ bool AudioOutputSPDIF::SetOutputModeMono(bool mono) { return true; } -bool AudioOutputSPDIF::begin() { - return true; -} - bool AudioOutputSPDIF::ConsumeSample(int16_t sample[2]) { if (!i2sOn) { return true; // Sink the data @@ -282,7 +267,7 @@ bool AudioOutputSPDIF::ConsumeSample(int16_t sample[2]) { #if defined(ESP32) // Assume DMA buffers are multiples of 16 bytes. Either we write all bytes or none. size_t bytes_written; - esp_err_t ret = i2s_write((i2s_port_t)portNo, (const char*)&buf, 8 * channels, &bytes_written, 0); + esp_err_t ret = i2s_channel_write(_tx_handle, (const char*)&buf, 8 * channels, &bytes_written, 10); // If we didn't write all bytes, return false early and do not increment frame_num if ((ret != ESP_OK) || (bytes_written != (8 * channels))) { return false; @@ -300,12 +285,16 @@ bool AudioOutputSPDIF::ConsumeSample(int16_t sample[2]) { } bool AudioOutputSPDIF::stop() { + if (i2sOn) { + i2sOn = false; #if defined(ESP32) - i2s_zero_dma_buffer((i2s_port_t)portNo); + i2s_channel_disable(_tx_handle); + i2s_del_channel(_tx_handle); #elif defined(ESP8266) - I2SDriver.stop(); + I2SDriver.stop(); #endif - frame_num = 0; + frame_num = 0; + } return true; } diff --git a/src/AudioOutputSPDIF.h b/src/AudioOutputSPDIF.h index c721a47..dbfed5f 100644 --- a/src/AudioOutputSPDIF.h +++ b/src/AudioOutputSPDIF.h @@ -30,9 +30,14 @@ along with this program. If not, see . */ -#if defined(ESP32) || defined(ESP8266) #pragma once +#if defined(ESP32) || defined(ESP8266) + +#if defined(ESP32) +#include +#endif + #include "AudioOutput.h" #if defined(ESP32) @@ -47,9 +52,16 @@ class AudioOutputSPDIF : public AudioOutput { public: - AudioOutputSPDIF(int dout_pin = SPDIF_OUT_PIN_DEFAULT, int port = 0, int dma_buf_count = DMA_BUF_COUNT_DEFAULT); + AudioOutputSPDIF(int dout_pin = SPDIF_OUT_PIN_DEFAULT); + [[deprecated("Use AudioOutputSPDIF() and SetBuffers/SetPinout to configure")]] AudioOutputSPDIF(int dout_pin, int port0, int dma_buf_count = DMA_BUF_COUNT_DEFAULT); virtual ~AudioOutputSPDIF() override; - bool SetPinout(int bclkPin, int wclkPin, int doutPin); + [[deprecated("Use SetPinout(spdifPin) instead")]] bool SetPinout(int bclkPin, int wclkPin, int doutPin) { + (void) bclkPin; + (void) wclkPin; + return SetPinout(doutPin); + } + bool SetPinout(int doutPin); + bool SetBuffers(int dmaBuffers = DMA_BUF_COUNT_DEFAULT, int dmaBufferBytes = DMA_BUF_SIZE_DEFAULT * 4); virtual bool SetRate(int hz) override; virtual bool SetChannels(int channels) override; virtual bool begin() override; @@ -66,11 +78,17 @@ protected: virtual inline int AdjustI2SRate(int hz) { return rate_multiplier * hz; } - uint8_t portNo; bool mono; bool i2sOn; + int8_t doutPin; uint8_t frame_num; uint8_t rate_multiplier; + size_t _buffers; + size_t _bufferWords; +#ifdef ESP32 + i2s_chan_handle_t _tx_handle; +#endif + }; #endif // _AUDIOOUTPUTSPDIF_H diff --git a/src/AudioOutputULP.cpp b/src/AudioOutputULP.cpp index cf5a339..b4cef8f 100644 --- a/src/AudioOutputULP.cpp +++ b/src/AudioOutputULP.cpp @@ -18,16 +18,14 @@ along with this program. If not, see . */ -#ifdef ESP32 +#if defined(ESP32) && SOC_DAC_SUPPORTED #include "AudioOutputULP.h" -#if CONFIG_IDF_TARGET_ESP32 - #include #include #include -#include +#include #include #include @@ -70,13 +68,18 @@ bool AudioOutputULP::begin() { unsigned long rtc_fast_freq_hz = 1000000ULL * (1 << RTC_CLK_CAL_FRACT) * 256 / rtc_8md256_period; //initialize DACs + dac_oneshot_handle_t d0; + dac_oneshot_handle_t d1; + if (activeDACs & 1) { - dac_output_enable(DAC_CHANNEL_1); - dac_output_voltage(DAC_CHANNEL_1, 128); + dac_oneshot_config_t a = { DAC_CHAN_0 }; + assert(ESP_OK == dac_oneshot_new_channel(&a, &d0)); + dac_oneshot_output_voltage(d0, 128); } if (activeDACs & 2) { - dac_output_enable(DAC_CHANNEL_2); - dac_output_voltage(DAC_CHANNEL_2, 128); + dac_oneshot_config_t b = { DAC_CHAN_1 }; + assert(ESP_OK == dac_oneshot_new_channel(&b, &d1)); + dac_oneshot_output_voltage(d1, 128); } int retAddress1 = 9; @@ -263,14 +266,15 @@ bool AudioOutputULP::stop() { ulp_run(0); if (activeDACs & 1) { - dac_output_voltage(DAC_CHANNEL_1, 128); + dac_oneshot_output_voltage(d0, 128); + dac_oneshot_del_channel(d0); } if (activeDACs & 2) { - dac_output_voltage(DAC_CHANNEL_2, 128); + dac_oneshot_output_voltage(d1, 128); + dac_oneshot_del_channel(d1); } return true; } #endif -#endif diff --git a/src/AudioOutputULP.h b/src/AudioOutputULP.h index e8fe95e..ac330bc 100644 --- a/src/AudioOutputULP.h +++ b/src/AudioOutputULP.h @@ -34,7 +34,9 @@ #include "AudioOutput.h" -#ifdef ESP32 +#if defined(ESP32) && SOC_DAC_SUPPORTED + +#include class AudioOutputULP : public AudioOutput { public: @@ -54,6 +56,8 @@ private: uint8_t bufferedOddSample = 128; bool waitingOddSample = true; // must be set to false for mono output int activeDACs = 3; // 1:DAC1; 2:DAC2; 3:both; + dac_oneshot_handle_t d0; + dac_oneshot_handle_t d1; bool stereoOutput = true; const int opcodeCount = 20; const uint32_t dacTableStart1 = 2048 - 512; diff --git a/tests/build-ci.sh b/tests/build-ci.sh index 9479dce..ceaa04d 100755 --- a/tests/build-ci.sh +++ b/tests/build-ci.sh @@ -21,7 +21,7 @@ while [ $(echo $sketches | wc -w) -gt 0 ]; do if [ 0"$werror" -gt 0 ]; then ./arduino-cli compile -b "$fqbn" -v --warnings all \ --build-property "compiler.c.extra_flags=-Wall -Wextra -Werror -Wno-ignored-qualifiers" \ - --build-property "compiler.cpp.extra_flags=-Wall -Wextra -Werror -Wno-ignored-qualifiers -Wno-overloaded-virtual" \ + --build-property "compiler.cpp.extra_flags=-Wall -Wextra -Werror -Wno-ignored-qualifiers -Wno-overloaded-virtual" \ --build-path "$outdir" "$sketch" || exit 255 else ./arduino-cli compile -b "$fqbn" -v --warnings all --build-path "$outdir" "$sketch" || exit 255