ESP8266 SinglePinI2SDriver, SPDIF output improvements, FLAC memory optimization (#237)
* Added S/PDIF output using I2S peripheral * Added AudioOutputSPDIF to README and PROGMEM changes * SPDIF/I2S sample rate fixes * Fix SPDIF example file name * Minimal (single pin) I2S driver * AudioOutputSPDIF changes for minimal I2S driver * Documentation changes * FLAC decoding memory optimization
This commit is contained in:
1 parent
a7a8b917d2
commit
377b271edd
13 files changed
+525
-49
No files matched your search
@@ -30,6 +30,7 @@ AudioGeneratorFLAC::AudioGeneratorFLAC()
|
||||
buff[1] = NULL;
|
||||
buffPtr = 0;
|
||||
buffLen = 0;
|
||||
running = false;
|
||||
}
|
||||
|
||||
AudioGeneratorFLAC::~AudioGeneratorFLAC()
|
||||
@@ -188,9 +189,11 @@ void AudioGeneratorFLAC::metadata_cb(const FLAC__StreamDecoder *decoder, const F
|
||||
(void) metadata;
|
||||
audioLogger->printf_P(PSTR("Metadata\n"));
|
||||
}
|
||||
char AudioGeneratorFLAC::error_cb_str[64];
|
||||
void AudioGeneratorFLAC::error_cb(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status)
|
||||
{
|
||||
(void) decoder;
|
||||
cb.st((int)status, FLAC__StreamDecoderErrorStatusString[status]);
|
||||
strncpy_P(error_cb_str, FLAC__StreamDecoderErrorStatusString[status], 64);
|
||||
cb.st((int)status, error_cb_str);
|
||||
}
|
||||
|
||||
@@ -81,6 +81,7 @@ class AudioGeneratorFLAC : public AudioGenerator
|
||||
FLAC__bool eof_cb(const FLAC__StreamDecoder *decoder);
|
||||
FLAC__StreamDecoderWriteStatus write_cb(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *const buffer[]);
|
||||
void metadata_cb(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata);
|
||||
static char error_cb_str[64];
|
||||
void error_cb(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status);
|
||||
};
|
||||
|
||||
|
||||
+53
-36
@@ -14,6 +14,14 @@
|
||||
|
||||
Adapted for ESP8266Audio
|
||||
|
||||
NOTE: This module operates I2S at 4x sampling rate, as it needs to
|
||||
send out each bit as two output symbols, packed into
|
||||
32-bit words. Even for mono sound, S/PDIF is specified minimum
|
||||
for 2 channels, each as 32-bits sub-frame. This drains I2S
|
||||
buffers 4x more quickly so you may need 4x bigger output
|
||||
buffers than usual, configurable with 'dma_buf_count'
|
||||
constructor parameter.
|
||||
|
||||
Copyright (C) 2020 Ivan Kostoski
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
@@ -31,10 +39,11 @@
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
#ifdef ESP32
|
||||
#if defined(ESP32)
|
||||
#include "driver/i2s.h"
|
||||
#else
|
||||
#include <i2s.h>
|
||||
#include "soc/rtc.h"
|
||||
#elif defined(ESP8266)
|
||||
#include "driver/SinglePinI2SDriver.h"
|
||||
#endif
|
||||
#include "AudioOutputSPDIF.h"
|
||||
|
||||
@@ -77,7 +86,7 @@ static const uint16_t spdif_bmclookup[256] PROGMEM = {
|
||||
AudioOutputSPDIF::AudioOutputSPDIF(int dout_pin, int port, int dma_buf_count)
|
||||
{
|
||||
this->portNo = port;
|
||||
#ifdef ESP32
|
||||
#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),
|
||||
@@ -87,21 +96,23 @@ AudioOutputSPDIF::AudioOutputSPDIF(int dout_pin, int port, int dma_buf_count)
|
||||
.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
|
||||
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // lowest interrupt priority
|
||||
.dma_buf_count = dma_buf_count,
|
||||
.dma_buf_len = 256, // 4 x bigger buffers
|
||||
.dma_buf_len = DMA_BUF_SIZE_DEFAULT, // bigger buffers, reduces interrupts
|
||||
.use_apll = true // Audio PLL is needed for low clock jitter
|
||||
};
|
||||
if (i2s_driver_install((i2s_port_t)portNo, &i2s_config_spdif, 0, NULL) != ESP_OK) {
|
||||
audioLogger->println("ERROR: Unable to install I2S drives\n");
|
||||
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);
|
||||
SetPinout(25, 32, dout_pin);
|
||||
rate_multiplier = 2; // For 32bit words
|
||||
#else
|
||||
SetPinout(I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, dout_pin);
|
||||
rate_multiplier = 2; // 2x32bit words
|
||||
#elif defined(ESP8266)
|
||||
(void) dout_pin;
|
||||
(void) dma_buf_count;
|
||||
i2s_begin();
|
||||
rate_multiplier = 4; // For 16bit words
|
||||
if (!I2SDriver.begin(dma_buf_count, DMA_BUF_SIZE_DEFAULT)) {
|
||||
audioLogger->println(F("ERROR: Unable to start I2S driver"));
|
||||
return;
|
||||
}
|
||||
rate_multiplier = 4; // 4x16 bit words
|
||||
#endif
|
||||
i2sOn = true;
|
||||
mono = false;
|
||||
@@ -109,26 +120,27 @@ AudioOutputSPDIF::AudioOutputSPDIF(int dout_pin, int port, int dma_buf_count)
|
||||
channels = 2;
|
||||
frame_num = 0;
|
||||
SetGain(1.0);
|
||||
SetRate(44100); // Default
|
||||
hertz = 0;
|
||||
SetRate(44100);
|
||||
}
|
||||
|
||||
AudioOutputSPDIF::~AudioOutputSPDIF()
|
||||
{
|
||||
#ifdef ESP32
|
||||
#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
|
||||
}
|
||||
#else
|
||||
if (i2sOn) i2s_end();
|
||||
#elif defined(ESP8266)
|
||||
if (i2sOn) I2SDriver.stop();
|
||||
#endif
|
||||
i2sOn = false;
|
||||
}
|
||||
|
||||
bool AudioOutputSPDIF::SetPinout(int bclk, int wclk, int dout)
|
||||
{
|
||||
#ifdef ESP32
|
||||
#if defined(ESP32)
|
||||
i2s_pin_config_t pins = {
|
||||
.bck_io_num = bclk,
|
||||
.ws_io_num = wclk,
|
||||
@@ -150,15 +162,25 @@ bool AudioOutputSPDIF::SetPinout(int bclk, int wclk, int dout)
|
||||
|
||||
bool AudioOutputSPDIF::SetRate(int hz)
|
||||
{
|
||||
this->hertz = hz;
|
||||
audioLogger->printf("S/PDIF Set Rate: %d\n", AdjustI2SRate(hz));
|
||||
if (!i2sOn) return false;
|
||||
#ifdef ESP32
|
||||
if (i2s_set_sample_rates((i2s_port_t)portNo, AdjustI2SRate(hz)) != ESP_OK) {
|
||||
if (hz < 32000) return false;
|
||||
if (hz == this->hertz) return true;
|
||||
this->hertz = hz;
|
||||
int adjustedHz = AdjustI2SRate(hz);
|
||||
#if defined(ESP32)
|
||||
if (i2s_set_sample_rates((i2s_port_t)portNo, adjustedHz) == ESP_OK) {
|
||||
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);
|
||||
}
|
||||
} else {
|
||||
audioLogger->println("ERROR changing S/PDIF sample rate");
|
||||
}
|
||||
#else
|
||||
i2s_set_rate(AdjustI2SRate(hz));
|
||||
#elif defined(ESP8266)
|
||||
I2SDriver.setRate(adjustedHz);
|
||||
audioLogger->printf_P(PSTR("S/PDIF rate set: %.3f\n"), I2SDriver.getActualRate()/4);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
@@ -193,9 +215,6 @@ bool AudioOutputSPDIF::begin()
|
||||
bool AudioOutputSPDIF::ConsumeSample(int16_t sample[2])
|
||||
{
|
||||
if (!i2sOn) return true; // Sink the data
|
||||
#ifndef ESP32
|
||||
if (i2s_is_full()) return false;
|
||||
#endif
|
||||
int16_t ms[2];
|
||||
uint16_t hi, lo, aux;
|
||||
uint32_t buf[4];
|
||||
@@ -243,18 +262,14 @@ bool AudioOutputSPDIF::ConsumeSample(int16_t sample[2])
|
||||
aux = 0xb333 ^ (((uint32_t)((int16_t)lo)) >> 17);
|
||||
buf[3] = VUCP_PREAMBLE_W | aux;
|
||||
|
||||
// Assume DMA buffers are multiples of 16 bytes. Either we write all bytes or none.
|
||||
#ifdef ESP32
|
||||
#if defined(ESP32)
|
||||
// Assume DMA buffers are multiples of 16 bytes. Either we write all bytes or none.
|
||||
uint32_t bytes_written;
|
||||
esp_err_t ret = i2s_write((i2s_port_t)portNo, (const char*)&buf, 8 * channels, &bytes_written, 0);
|
||||
// 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;
|
||||
#else
|
||||
// NOTE: Order of buffer words is different for ESP8266
|
||||
i2s_write_sample_nb(buf[1]);
|
||||
i2s_write_sample_nb(buf[0]);
|
||||
i2s_write_sample_nb(buf[3]);
|
||||
if (!i2s_write_sample_nb(buf[2])) return false; // Return early and do not increment frame_num
|
||||
#elif defined(ESP8266)
|
||||
if (!I2SDriver.writeInterleaved(buf)) return false;
|
||||
#endif
|
||||
// Increment and rotate frame number
|
||||
if (++frame_num > 191) frame_num = 0;
|
||||
@@ -263,8 +278,10 @@ bool AudioOutputSPDIF::ConsumeSample(int16_t sample[2])
|
||||
|
||||
bool AudioOutputSPDIF::stop()
|
||||
{
|
||||
#ifdef ESP32
|
||||
#if defined(ESP32)
|
||||
i2s_zero_dma_buffer((i2s_port_t)portNo);
|
||||
#elif defined(ESP8266)
|
||||
I2SDriver.stop();
|
||||
#endif
|
||||
frame_num = 0;
|
||||
return true;
|
||||
|
||||
+10
-2
@@ -35,12 +35,20 @@
|
||||
|
||||
#include "AudioOutput.h"
|
||||
|
||||
#if defined(ESP32)
|
||||
#define SPDIF_OUT_PIN_DEFAULT 27
|
||||
#define DMA_BUF_COUNT_DEFAULT 8
|
||||
#define DMA_BUF_SIZE_DEFAULT 256
|
||||
#elif defined(ESP8266)
|
||||
#define SPDIF_OUT_PIN_DEFAULT 3
|
||||
#define DMA_BUF_COUNT_DEFAULT 32
|
||||
#define DMA_BUF_SIZE_DEFAULT 64
|
||||
#endif
|
||||
|
||||
class AudioOutputSPDIF : public AudioOutput
|
||||
{
|
||||
public:
|
||||
AudioOutputSPDIF(int dout_pin=SPDIF_OUT_PIN_DEFAULT, int port=0, int dma_buf_count = 8);
|
||||
AudioOutputSPDIF(int dout_pin=SPDIF_OUT_PIN_DEFAULT, int port=0, int dma_buf_count = DMA_BUF_COUNT_DEFAULT);
|
||||
virtual ~AudioOutputSPDIF() override;
|
||||
bool SetPinout(int bclkPin, int wclkPin, int doutPin);
|
||||
virtual bool SetRate(int hz) override;
|
||||
@@ -65,4 +73,4 @@ class AudioOutputSPDIF : public AudioOutput
|
||||
uint8_t rate_multiplier;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif // _AUDIOOUTPUTSPDIF_H
|
||||
@@ -0,0 +1,279 @@
|
||||
/*
|
||||
SinglePinI2SDriver
|
||||
ESP8266Audio I2S Minimal driver
|
||||
|
||||
Most of this code is taken and reworked from ESP8266 Arduino core,
|
||||
which itsef is reworked from Espessif's I2S examples.
|
||||
Original code is licensed under LGPL 2.1 or above
|
||||
|
||||
Reasons for rewrite:
|
||||
- Non-configurable size of DMA buffers
|
||||
- We only need GPIO3 connected to I2SO_DATA
|
||||
- No API to queue data from ISR. Callbacks are unusable
|
||||
as i2s_write_* functions are not in IRAM and may re-enable
|
||||
SLC interrupt before returning
|
||||
- ISR overhead is not needed
|
||||
|
||||
Copyright (C) 2020 Ivan Kostoski
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "SinglePinI2SDriver.h"
|
||||
|
||||
SinglePinI2SDriver::~SinglePinI2SDriver()
|
||||
{
|
||||
stopI2S();
|
||||
freeBuffers(bufCount);
|
||||
}
|
||||
|
||||
bool SinglePinI2SDriver::begin(uint8_t bufCount, uint16_t bufSize)
|
||||
{
|
||||
if (bufCount < 4) return false;
|
||||
if ((bufSize < 32) || (bufSize > 1020) || ((bufSize % 4) != 0)) return false;
|
||||
|
||||
stopSLC();
|
||||
|
||||
if ((bufCount != this->bufCount) || (bufSize != this->bufSize)) freeBuffers(this->bufCount);
|
||||
this->bufCount = bufCount;
|
||||
this->bufSize = bufSize;
|
||||
if (!allocateBuffers()) return false;
|
||||
|
||||
zeroBuffers();
|
||||
configureSLC();
|
||||
startI2S();
|
||||
|
||||
// Send out at least one buffer, so we have valid address of
|
||||
// finished descriptor in SLCRXEDA
|
||||
head = &descriptors[0];
|
||||
head->owner = 1;
|
||||
startSLC();
|
||||
while (!lastSentDescriptor()) delayMicroseconds(1);
|
||||
// By here, SLC should be stalled (next descriptor owner is 0)
|
||||
head->owner = 0;
|
||||
head = head->next_link_ptr;
|
||||
headPos = 0;
|
||||
underflowCount = -1;
|
||||
return true;
|
||||
};
|
||||
|
||||
void SinglePinI2SDriver::stop()
|
||||
{
|
||||
stopI2S();
|
||||
}
|
||||
|
||||
bool SinglePinI2SDriver::allocateBuffers()
|
||||
{
|
||||
// Allocate output (RXLINK) descriptors and bufferss
|
||||
if (descriptors) return true;
|
||||
descriptors = (SLCDecriptor*) calloc(bufCount, sizeof(SLCDecriptor));
|
||||
if (!descriptors) return false;
|
||||
int allocated;
|
||||
for (allocated = 0; allocated < bufCount; allocated++) {
|
||||
uint32_t* buffer = (uint32_t*)malloc(bufSize * sizeof(uint32_t));
|
||||
if (!buffer) break;
|
||||
auto descriptor = &descriptors[allocated];
|
||||
descriptor->eof = 1; // Needed for SLC to update SLCRXEDA
|
||||
descriptor->datalen = bufSize * sizeof(uint32_t);
|
||||
descriptor->blocksize = bufSize * sizeof(uint32_t);
|
||||
descriptor->buf_ptr = buffer;
|
||||
descriptor->next_link_ptr = &descriptors[(allocated+1) % bufCount];
|
||||
}
|
||||
// Release memory if not all buffers were allocated
|
||||
if (allocated < bufCount) {
|
||||
freeBuffers(allocated);
|
||||
return false;
|
||||
}
|
||||
zeroBuffers();
|
||||
return true;
|
||||
}
|
||||
|
||||
void SinglePinI2SDriver::freeBuffers(int allocated)
|
||||
{
|
||||
if (!descriptors) return;
|
||||
while (--allocated >= 0) {
|
||||
if (descriptors[allocated].buf_ptr) {
|
||||
free(descriptors[allocated].buf_ptr);
|
||||
descriptors[allocated].buf_ptr = NULL;
|
||||
}
|
||||
}
|
||||
free(descriptors);
|
||||
descriptors = NULL;
|
||||
}
|
||||
|
||||
void SinglePinI2SDriver::zeroBuffers()
|
||||
{
|
||||
for(int i = 0; i < bufCount; i++) {
|
||||
auto descriptor = &descriptors[i];
|
||||
ets_memset((void *)descriptor->buf_ptr, 0, bufSize * sizeof(uint32_t));
|
||||
}
|
||||
}
|
||||
|
||||
inline void SinglePinI2SDriver::advanceHead(const SLCDecriptor *lastSent)
|
||||
{
|
||||
if (headPos >= bufSize) {
|
||||
head->owner = 1; // Owned by SLC
|
||||
head = head->next_link_ptr;
|
||||
head->owner = 0; // Owned by CPU
|
||||
headPos = 0;
|
||||
// If SLC stopped, fill-up the buffers before (re)starting
|
||||
if (isSLCStopped() && (head->next_link_ptr == lastSent)) {
|
||||
underflowCount++;
|
||||
startSLC();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool SinglePinI2SDriver::write(uint32_t sample)
|
||||
{
|
||||
auto lastSent = lastSentDescriptor();
|
||||
if (isSLCRunning() && (lastSent->next_link_ptr == head)) return false;
|
||||
head->buf_ptr[headPos++] = sample;
|
||||
advanceHead(lastSent);
|
||||
return true;
|
||||
};
|
||||
|
||||
bool SinglePinI2SDriver::writeInterleaved(uint32_t samples[4])
|
||||
{
|
||||
auto lastSent = lastSentDescriptor();
|
||||
if (isSLCRunning() && (lastSent->next_link_ptr == head)) return false;
|
||||
uint32_t* buf = head->buf_ptr;
|
||||
buf[headPos++] = samples[1];
|
||||
buf[headPos++] = samples[0];
|
||||
buf[headPos++] = samples[3];
|
||||
buf[headPos++] = samples[2];
|
||||
advanceHead(lastSent);
|
||||
return true;
|
||||
};
|
||||
|
||||
void SinglePinI2SDriver::configureSLC()
|
||||
{
|
||||
ETS_SLC_INTR_DISABLE();
|
||||
// Reset SLC, clear interrupts
|
||||
SLCC0 |= SLCRXLR | SLCTXLR;
|
||||
SLCC0 &= ~(SLCRXLR | SLCTXLR);
|
||||
SLCIC = 0xFFFFFFFF;
|
||||
// Configure SLC DMA in mode 1
|
||||
SLCC0 &= ~(SLCMM << SLCM);
|
||||
SLCC0 |= (1 << SLCM);
|
||||
SLCRXDC |= SLCBINR | SLCBTNR;
|
||||
SLCRXDC &= ~(SLCBRXFE | SLCBRXEM | SLCBRXFM);
|
||||
// RXLINK is output DMA, but TXLINK must be configured with valid descriptr
|
||||
SLCTXL &= ~(SLCTXLAM << SLCTXLA);
|
||||
SLCRXL &= ~(SLCRXLAM << SLCRXLA);
|
||||
SLCTXL |= (uint32)&descriptors[0] << SLCTXLA;
|
||||
SLCRXL |= (uint32)&descriptors[0] << SLCRXLA;
|
||||
// Not setting up interrupts. Not starting DMA here
|
||||
}
|
||||
|
||||
void SinglePinI2SDriver::stopSLC()
|
||||
{
|
||||
// Stop SLC, reset descriptors and clear interrupts
|
||||
ETS_SLC_INTR_DISABLE();
|
||||
SLCTXL |= SLCTXLE;
|
||||
SLCRXL |= SLCRXLE;
|
||||
SLCTXL &= ~(SLCTXLAM << SLCTXLA);
|
||||
SLCRXL &= ~(SLCRXLAM << SLCRXLA);
|
||||
SLCIC = 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
void SinglePinI2SDriver::setDividers(uint8_t div1, uint8_t div2)
|
||||
{
|
||||
// Ensure dividers fit in bit fields
|
||||
div1 &= I2SBDM;
|
||||
div2 &= I2SCDM;
|
||||
// trans master(active low), recv master(active_low), !bits mod(==16 bits/chanel), clear clock dividers
|
||||
I2SC &= ~(I2STSM | I2SRSM | (I2SBMM << I2SBM) | (I2SBDM << I2SBD) | (I2SCDM << I2SCD));
|
||||
// I2SRF = Send/recv right channel first
|
||||
// I2SMR = MSB recv/xmit first
|
||||
// I2SRMS, I2STMS = We don't really care about WS delay here
|
||||
// div1, div2 = Set I2S WS clock frequency. BCLK seems to be generated from 32x this
|
||||
I2SC |= I2SRF | I2SMR | I2SRMS | I2STMS | (div1 << I2SBD) | (div2 << I2SCD);
|
||||
}
|
||||
|
||||
void SinglePinI2SDriver::setRate(const uint32_t rateHz)
|
||||
{
|
||||
if (rateHz == currentRate) return;
|
||||
currentRate = rateHz;
|
||||
|
||||
uint32_t scaledBaseFreq = I2SBASEFREQ / 32;
|
||||
float bestDelta = scaledBaseFreq;
|
||||
uint8_t sbdDivBest=1;
|
||||
uint8_t scdDivBest=1;
|
||||
for (uint8_t i = 1; i < 64; i++) {
|
||||
for (uint8_t j = i; j < 64; j++) {
|
||||
float delta = fabs(((float)scaledBaseFreq/i/j) - rateHz);
|
||||
if (delta < bestDelta){
|
||||
bestDelta = delta;
|
||||
sbdDivBest = i;
|
||||
scdDivBest = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setDividers(sbdDivBest, scdDivBest);
|
||||
}
|
||||
|
||||
float SinglePinI2SDriver::getActualRate()
|
||||
{
|
||||
return (float)I2SBASEFREQ/32/((I2SC>>I2SBD) & I2SBDM)/((I2SC >> I2SCD) & I2SCDM);
|
||||
}
|
||||
|
||||
void SinglePinI2SDriver::startI2S()
|
||||
{
|
||||
// Setup only I2S SD pin for output
|
||||
pinMode(I2SO_DATA, FUNCTION_1);
|
||||
I2S_CLK_ENABLE();
|
||||
// Reset I2S
|
||||
I2SIC = 0x3F;
|
||||
I2SIE = 0;
|
||||
I2SC &= ~(I2SRST);
|
||||
I2SC |= I2SRST;
|
||||
I2SC &= ~(I2SRST);
|
||||
// I2STXFMM, I2SRXFMM=0 => 16-bit, dual channel data
|
||||
I2SFC &= ~(I2SDE | (I2STXFMM << I2STXFM) | (I2SRXFMM << I2SRXFM));
|
||||
I2SFC |= I2SDE; // Enable DMA
|
||||
// I2STXCMM, I2SRXCMM=0 => Dual channel mode, RX/TX CHAN_MOD=0
|
||||
I2SCC &= ~((I2STXCMM << I2STXCM) | (I2SRXCMM << I2SRXCM));
|
||||
// Set dividers to something resonable
|
||||
currentRate = 0;
|
||||
setRate(44100);
|
||||
// Start I2S peripheral
|
||||
I2SC |= I2STXS;
|
||||
}
|
||||
|
||||
void SinglePinI2SDriver::stopI2S()
|
||||
{
|
||||
// Disable any I2S send or receive
|
||||
I2SC &= ~(I2STXS | I2SRXS);
|
||||
// Reset I2S
|
||||
I2SC &= ~(I2SRST);
|
||||
I2SC |= I2SRST;
|
||||
I2SC &= ~(I2SRST);
|
||||
|
||||
stopSLC();
|
||||
// Reconnect RX0 ?
|
||||
//pinMode(I2SO_DATA, SPECIAL);
|
||||
}
|
||||
|
||||
int SinglePinI2SDriver::getUnderflowCount()
|
||||
{
|
||||
int count = underflowCount;
|
||||
underflowCount = 0;
|
||||
return count;
|
||||
}
|
||||
|
||||
// Global instance
|
||||
SinglePinI2SDriver I2SDriver;
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
SinglePinI2SDriver
|
||||
ESP8266Audio I2S Minimal driver
|
||||
|
||||
Copyright (C) 2020 Ivan Kostoski
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#if !defined(ESP8266)
|
||||
#error "This driver is ESP8266 specific, only include it on ESP8266"
|
||||
#endif
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "osapi.h"
|
||||
#include "ets_sys.h"
|
||||
#include "i2s_reg.h"
|
||||
|
||||
struct SLCDecriptor {
|
||||
uint32_t blocksize : 12;
|
||||
uint32_t datalen : 12;
|
||||
uint32_t unused : 5;
|
||||
uint32_t sub_sof : 1;
|
||||
uint32_t eof : 1;
|
||||
volatile uint32_t owner : 1;
|
||||
uint32_t* buf_ptr;
|
||||
SLCDecriptor* next_link_ptr;
|
||||
};
|
||||
|
||||
class SinglePinI2SDriver
|
||||
{
|
||||
public:
|
||||
enum : uint8_t { I2SO_DATA = 3 }; // GPIO3/RX0
|
||||
|
||||
SinglePinI2SDriver(): descriptors(NULL), bufCount(0), bufSize(0) {};
|
||||
~SinglePinI2SDriver();
|
||||
|
||||
bool begin(uint8_t bufCount, uint16_t bufSize);
|
||||
void stop();
|
||||
void setRate(const uint32_t rateHz);
|
||||
float getActualRate();
|
||||
bool write(uint32_t sample);
|
||||
bool writeInterleaved(uint32_t samples[4]);
|
||||
int getUnderflowCount();
|
||||
|
||||
protected:
|
||||
bool allocateBuffers();
|
||||
void freeBuffers(int allocated);
|
||||
void zeroBuffers();
|
||||
void advanceHead(const SLCDecriptor *finished);
|
||||
void configureSLC();
|
||||
void stopSLC();
|
||||
void setDividers(uint8_t div1, uint8_t div2);
|
||||
void startI2S();
|
||||
void stopI2S();
|
||||
SLCDecriptor *descriptors;
|
||||
uint16_t bufCount;
|
||||
uint16_t bufSize;
|
||||
SLCDecriptor *head;
|
||||
uint32_t headPos;
|
||||
uint32_t currentRate;
|
||||
int underflowCount;
|
||||
|
||||
// (Re)Start transmission ("TX" DMA always needed to be enabled)
|
||||
static inline void startSLC() { SLCTXL |= SLCTXLS; SLCRXL |= SLCRXLS; }
|
||||
static inline uint32_t isSLCRunning() { return (SLCRXS & SLCRXF); };
|
||||
static inline uint32_t isSLCStopped() { return (SLCRXS & SLCRXE); };
|
||||
static inline SLCDecriptor *lastSentDescriptor() { return (SLCDecriptor*)SLCRXEDA; }
|
||||
};
|
||||
|
||||
// Global instance
|
||||
extern SinglePinI2SDriver I2SDriver;
|
||||
@@ -99,7 +99,12 @@ typedef FLAC__uint64 brword;
|
||||
* also depends on the CPU cache size and other factors; some twiddling
|
||||
* may be necessary to squeeze out the best performance.
|
||||
*/
|
||||
#if defined(ESP8266)
|
||||
/* Reduced bitreader buffer, saves some RAM */
|
||||
static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 8192u / FLAC__BITS_PER_WORD; /* in words */
|
||||
#else
|
||||
static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER_WORD; /* in words */
|
||||
#endif
|
||||
|
||||
struct FLAC__BitReader {
|
||||
/* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */
|
||||
|
||||
@@ -146,6 +146,8 @@ FLAC_API const unsigned FLAC__SUBFRAME_TYPE_VERBATIM_BYTE_ALIGNED_MASK = 0x02;
|
||||
FLAC_API const unsigned FLAC__SUBFRAME_TYPE_FIXED_BYTE_ALIGNED_MASK = 0x10;
|
||||
FLAC_API const unsigned FLAC__SUBFRAME_TYPE_LPC_BYTE_ALIGNED_MASK = 0x40;
|
||||
|
||||
// Not used in AudioGeneratorFLAC, possibly save some RAM
|
||||
/*
|
||||
FLAC_API const char * const FLAC__SubframeTypeString[] = {
|
||||
"CONSTANT",
|
||||
"VERBATIM",
|
||||
@@ -198,6 +200,7 @@ FLAC_API const char * const FLAC__StreamMetadata_Picture_TypeString[] = {
|
||||
"Band/artist logotype",
|
||||
"Publisher/Studio logotype"
|
||||
};
|
||||
*/
|
||||
|
||||
FLAC_API FLAC__bool FLAC__format_sample_rate_is_valid(unsigned sample_rate)
|
||||
{
|
||||
|
||||
@@ -33,6 +33,8 @@
|
||||
//#ifdef HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
//#endif
|
||||
#include <Arduino.h>
|
||||
#include <pgmspace.h>
|
||||
|
||||
//#include <stdio.h>
|
||||
#include <stdlib.h> /* for malloc() */
|
||||
@@ -176,6 +178,8 @@ typedef struct FLAC__StreamDecoderPrivate {
|
||||
*
|
||||
***********************************************************************/
|
||||
|
||||
// Not used in AudioGeneratorFLAC, save some RAM
|
||||
/*
|
||||
FLAC_API const char FLAC__StreamDecoderStateString[][48] = {
|
||||
"FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
|
||||
"FLAC__STREAM_DECODER_READ_METADATA",
|
||||
@@ -226,8 +230,9 @@ FLAC_API const char FLAC__StreamDecoderWriteStatusString[][48] = {
|
||||
"FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
|
||||
"FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
|
||||
};
|
||||
*/
|
||||
|
||||
FLAC_API const char FLAC__StreamDecoderErrorStatusString[][64] = {
|
||||
FLAC_API const char FLAC__StreamDecoderErrorStatusString[][64] PROGMEM = {
|
||||
"FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
|
||||
"FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
|
||||
"FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH",
|
||||
@@ -863,10 +868,12 @@ FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__Str
|
||||
return decoder->protected_->state;
|
||||
}
|
||||
|
||||
/*
|
||||
FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder)
|
||||
{
|
||||
return &FLAC__StreamDecoderStateString[decoder->protected_->state][0];
|
||||
}
|
||||
*/
|
||||
|
||||
FLAC_API FLAC__bool FLAC__stream_decoder_get_md5_checking(const FLAC__StreamDecoder *decoder)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user