Replace external SPIRAM lib w/internal optimized (#250)
* Replace external SPIRAM lib w/internal optimized Instead of requiring everyone to download and install another Arduino library to use this lib, include an optimized, self-contained one from the 8266 VM PR: https://github.com/esp8266/Arduino/pull/6994 Does change the API as it now requires a HW chip select, so that parameter is removed from the SpiRAM buffer object. * Redo the FIFO to try and be faster * Fix the SPI interface, buffering. SPI FIFO needs special care so do all work in a RAM copy and the copy it over manually w/32b writes. Ensure the SPI command is done before touching the FIFO. Fix buffering organization. Make the buffer report status as a #### bar. * Fix infinite wait on EOF * Add SW chip select support * Update URL to one that's live today * Clean up metadata printout * Clean up readme refs to obsolete lib * Slow down the DIO->SIO reset sequence * Update readme to remove old SPIRam library ref Also add the resistor to the 1T circuit to avoid transistor overheating.
This commit is contained in:
1 parent
be0ee8283f
commit
0fd2d0413b
5 files changed
+372
-132
No files matched your search
@@ -5,6 +5,9 @@
|
||||
Copyright (C) 2017 Sebastien Decourriere
|
||||
Based on AudioFileSourceBuffer class from Earle F. Philhower, III
|
||||
|
||||
Copyright (C) 2020 Earle F. Philhower, III
|
||||
Rewritten for speed and functionality
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
@@ -26,131 +29,139 @@
|
||||
|
||||
AudioFileSourceSPIRAMBuffer::AudioFileSourceSPIRAMBuffer(AudioFileSource *source, uint8_t csPin, uint32_t buffSizeBytes)
|
||||
{
|
||||
Spiram = new ESP8266Spiram(csPin, 40e6);
|
||||
Spiram->begin();
|
||||
Spiram->setSeqMode();
|
||||
ramSize = buffSizeBytes;
|
||||
writePtr = 0;
|
||||
readPtr = 0;
|
||||
src = source;
|
||||
length = 0;
|
||||
filled = false;
|
||||
bytesAvailable = 0;
|
||||
audioLogger->printf_P(PSTR("SPI RAM buffer size: %u Bytes\n"), ramSize);
|
||||
ram.begin(40, csPin);
|
||||
ramSize = buffSizeBytes;
|
||||
writePtr = 0;
|
||||
readPtr = 0;
|
||||
filled = false;
|
||||
src = source;
|
||||
audioLogger->printf_P(PSTR("SPI RAM buffer size: %u Bytes\n"), ramSize);
|
||||
}
|
||||
|
||||
AudioFileSourceSPIRAMBuffer::~AudioFileSourceSPIRAMBuffer()
|
||||
{
|
||||
ram.end();
|
||||
}
|
||||
|
||||
bool AudioFileSourceSPIRAMBuffer::seek(int32_t pos, int dir)
|
||||
{
|
||||
// Invalidate
|
||||
readPtr = 0;
|
||||
writePtr = 0;
|
||||
length = 0;
|
||||
return src->seek(pos, dir);
|
||||
// Invalidate
|
||||
readPtr = 0;
|
||||
writePtr = 0;
|
||||
filled = false;
|
||||
return src->seek(pos, dir);
|
||||
}
|
||||
|
||||
bool AudioFileSourceSPIRAMBuffer::close()
|
||||
{
|
||||
return src->close();
|
||||
return src->close();
|
||||
}
|
||||
|
||||
bool AudioFileSourceSPIRAMBuffer::isOpen()
|
||||
{
|
||||
return src->isOpen();
|
||||
return src->isOpen();
|
||||
}
|
||||
|
||||
uint32_t AudioFileSourceSPIRAMBuffer::getSize()
|
||||
{
|
||||
return src->getSize();
|
||||
return src->getSize();
|
||||
}
|
||||
|
||||
uint32_t AudioFileSourceSPIRAMBuffer::getPos()
|
||||
{
|
||||
return src->getPos();
|
||||
return src->getPos() - (writePtr - readPtr);
|
||||
}
|
||||
|
||||
uint32_t AudioFileSourceSPIRAMBuffer::read(void *data, uint32_t len)
|
||||
{
|
||||
uint32_t bytes = 0;
|
||||
// Check if the buffer isn't empty, otherwise we try to fill completely
|
||||
if (!filled) {
|
||||
uint8_t buffer[256];
|
||||
writePtr = readPtr = 0;
|
||||
uint16_t toRead = sizeof(buffer);
|
||||
// Fill up completely before returning any data at all
|
||||
audioLogger->printf_P(PSTR("Buffering...\n"));
|
||||
while (bytesAvailable!=ramSize) {
|
||||
length = src->read(buffer, toRead);
|
||||
if(length>0) {
|
||||
Spiram->write(writePtr, buffer, length);
|
||||
bytesAvailable+=length;
|
||||
writePtr = bytesAvailable % ramSize;
|
||||
if ((ramSize-bytesAvailable)<toRead) {
|
||||
toRead=ramSize-bytesAvailable;
|
||||
}
|
||||
} else {
|
||||
// EOF, break out of read loop
|
||||
break;
|
||||
}
|
||||
}
|
||||
writePtr = bytesAvailable % ramSize;
|
||||
filled = true;
|
||||
audioLogger->printf_P(PSTR("Filling Done !\n"));
|
||||
}
|
||||
uint32_t bytes = 0;
|
||||
|
||||
// audioLogger->printf("Buffer: %u%\n", bytesAvailable*100/ramSize);
|
||||
// Check if the buffer isn't empty, otherwise we try to fill completely
|
||||
if (!filled) {
|
||||
cb.st(999, PSTR("Filling buffer..."));
|
||||
uint8_t buffer[256];
|
||||
writePtr = 0;
|
||||
readPtr = 0;
|
||||
// Fill up completely before returning any data at all
|
||||
do {
|
||||
int toRead = std::min(ramSize - (writePtr - readPtr), sizeof(buffer));
|
||||
int length = src->read(buffer, toRead);
|
||||
if (length > 0) {
|
||||
#ifdef FAKERAM
|
||||
for (size_t i=0; i<length; i++) fakeRAM[(i+writePtr)%ramSize] = buffer[i];
|
||||
#else
|
||||
ram.writeBytes(writePtr % ramSize, buffer, length);
|
||||
#endif
|
||||
writePtr += length;
|
||||
} else {
|
||||
// EOF, break out of read loop
|
||||
break;
|
||||
}
|
||||
} while ((writePtr - readPtr) < ramSize);
|
||||
filled = true;
|
||||
cb.st(999, PSTR("Buffer filled..."));
|
||||
}
|
||||
|
||||
uint8_t *ptr = reinterpret_cast<uint8_t*>(data);
|
||||
uint32_t toReadFromBuffer = (len < bytesAvailable) ? len : bytesAvailable;
|
||||
if (toReadFromBuffer>0) {
|
||||
// Pull from buffer until we've got none left or we've satisfied the request
|
||||
Spiram->read(readPtr, ptr, toReadFromBuffer);
|
||||
readPtr = (readPtr+toReadFromBuffer) % ramSize;
|
||||
bytes = toReadFromBuffer;
|
||||
bytesAvailable-=toReadFromBuffer;
|
||||
len-=toReadFromBuffer;
|
||||
ptr += toReadFromBuffer;
|
||||
}
|
||||
// Read up to the entire buffer from RAM
|
||||
uint32_t toReadFromBuffer = std::min(len, writePtr - readPtr);
|
||||
uint8_t *ptr = reinterpret_cast<uint8_t*>(data);
|
||||
if (toReadFromBuffer > 0) {
|
||||
#ifdef FAKERAM
|
||||
for (size_t i=0; i<toReadFromBuffer; i++) ptr[i] = fakeRAM[(i+readPtr)%ramSize];
|
||||
#else
|
||||
ram.readBytes(readPtr % ramSize, ptr, toReadFromBuffer);
|
||||
#endif
|
||||
readPtr += toReadFromBuffer;
|
||||
ptr += toReadFromBuffer;
|
||||
bytes += toReadFromBuffer;
|
||||
len -= toReadFromBuffer;
|
||||
}
|
||||
|
||||
// If len>O there is no data left in buffer and we try to read more directly from source.
|
||||
// Then, we trigger a complete buffer refill
|
||||
if (len) {
|
||||
bytes += src->read(ptr, len);
|
||||
bytesAvailable = 0;
|
||||
filled = false;
|
||||
}
|
||||
return bytes;
|
||||
// If len>0 there is no data left in buffer and we try to read more directly from source.
|
||||
// Then, we trigger a complete buffer refill
|
||||
if (len) {
|
||||
bytes += src->read(data, len);
|
||||
filled = false;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
void AudioFileSourceSPIRAMBuffer::fill()
|
||||
{
|
||||
// Make sure the buffer is pre-filled before make partial fill.
|
||||
if (!filled) return;
|
||||
// Make sure the buffer is pre-filled before make partial fill.
|
||||
if (!filled) return;
|
||||
|
||||
// Now trying to refill SPI RAM Buffer
|
||||
uint8_t buffer[128];
|
||||
// Make sure there is at least buffer size free in RAM
|
||||
if ((ramSize - bytesAvailable)<sizeof(buffer)) {
|
||||
return;
|
||||
}
|
||||
uint16_t cnt = src->readNonBlock(buffer, sizeof(buffer));
|
||||
if (cnt) {
|
||||
Spiram->write(writePtr, buffer, cnt);
|
||||
bytesAvailable+=cnt;
|
||||
writePtr = (writePtr + cnt) % ramSize;
|
||||
#ifdef SPIBUF_DEBUG
|
||||
audioLogger->printf_P(PSTR("SockRead: %u | RamAvail: %u\n"), cnt, bytesAvailable);
|
||||
for (auto i=0; i<5; i++) {
|
||||
// Make sure there is at least buffer size free in RAM
|
||||
uint8_t buffer[128];
|
||||
if ((ramSize - (writePtr - readPtr)) < sizeof(buffer)) {
|
||||
return;
|
||||
}
|
||||
|
||||
int cnt = src->readNonBlock(buffer, sizeof(buffer));
|
||||
if (cnt) {
|
||||
#ifdef FAKERAM
|
||||
for (size_t i=0; i<cnt; i++) fakeRAM[(i+writePtr)%ramSize] = buffer[i];
|
||||
#else
|
||||
ram.writeBytes(writePtr % ramSize, buffer, cnt);
|
||||
#endif
|
||||
}
|
||||
return;
|
||||
writePtr += cnt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool AudioFileSourceSPIRAMBuffer::loop()
|
||||
{
|
||||
if (!src->loop()) return false;
|
||||
fill();
|
||||
return true;
|
||||
static uint32_t last = 0;
|
||||
if (!src->loop()) return false;
|
||||
fill();
|
||||
if ((ESP.getCycleCount() - last) > microsecondsToClockCycles(1000000)) {
|
||||
last = ESP.getCycleCount();
|
||||
char str[65];
|
||||
memset(str, '#', 64);
|
||||
str[64] = 0;
|
||||
str[((writePtr - readPtr) * 64)/ramSize] = 0;
|
||||
cb.st(((writePtr - readPtr) * 100)/ramSize, str);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -24,14 +24,18 @@
|
||||
|
||||
#include "AudioFileSource.h"
|
||||
#include <SPI.h>
|
||||
#include <ESP8266Spiram.h> // https://github.com/Gianbacchio/ESP8266_Spiram
|
||||
|
||||
#include "spiram-fast.h"
|
||||
//#define FAKERAM
|
||||
// #define SPIBUF_DEBUG
|
||||
|
||||
class AudioFileSourceSPIRAMBuffer : public AudioFileSource
|
||||
{
|
||||
public:
|
||||
AudioFileSourceSPIRAMBuffer(AudioFileSource *in, uint8_t csPin, uint32_t bufferBytes);
|
||||
#ifdef FAKERAM
|
||||
AudioFileSourceSPIRAMBuffer(AudioFileSource *in, uint8_t csPin = 15, uint32_t bufferBytes = 2048);
|
||||
#else
|
||||
AudioFileSourceSPIRAMBuffer(AudioFileSource *in, uint8_t csPin = 15, uint32_t bufferBytes = 128*1024);
|
||||
#endif
|
||||
virtual ~AudioFileSourceSPIRAMBuffer() override;
|
||||
|
||||
virtual uint32_t read(void *data, uint32_t len) override;
|
||||
@@ -47,13 +51,15 @@ class AudioFileSourceSPIRAMBuffer : public AudioFileSource
|
||||
|
||||
private:
|
||||
AudioFileSource *src;
|
||||
ESP8266Spiram *Spiram;
|
||||
uint32_t ramSize;
|
||||
uint32_t writePtr;
|
||||
uint32_t readPtr;
|
||||
uint16_t length;
|
||||
uint32_t bytesAvailable;
|
||||
bool filled;
|
||||
ESP8266SPIRAM ram;
|
||||
size_t ramSize;
|
||||
size_t writePtr;
|
||||
size_t readPtr;
|
||||
bool filled;
|
||||
|
||||
#ifdef FAKERAM
|
||||
char fakeRAM[2048];
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,236 @@
|
||||
/*
|
||||
spiram-fast - Fast, hardcoded interface for SPI-based RAMs, allowing DIO
|
||||
mode to be used and speeding up individual SPI operations
|
||||
significantly.
|
||||
|
||||
Copyright (c) 2020 Earle F. Philhower, III All rights reserved.
|
||||
|
||||
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
|
||||
|
||||
*/
|
||||
|
||||
class ESP8266SPIRAM {
|
||||
private:
|
||||
typedef struct {
|
||||
volatile uint32_t spi_cmd; // The SPI can change this behind our backs, so volatile!
|
||||
uint32_t spi_addr;
|
||||
uint32_t spi_ctrl;
|
||||
uint32_t spi_ctrl1; // undocumented? Not shown in the reg map
|
||||
uint32_t spi_rd_status;
|
||||
uint32_t spi_ctrl2;
|
||||
uint32_t spi_clock;
|
||||
uint32_t spi_user;
|
||||
uint32_t spi_user1;
|
||||
uint32_t spi_user2;
|
||||
uint32_t spi_wr_status;
|
||||
uint32_t spi_pin;
|
||||
uint32_t spi_slave;
|
||||
uint32_t spi_slave1;
|
||||
uint32_t spi_slave2;
|
||||
uint32_t spi_slave3;
|
||||
uint32_t spi_w[16]; // NOTE: You need a memory barrier before reading these after a read xaction
|
||||
uint32_t spi_ext3;
|
||||
} spi_regs;
|
||||
spi_regs *spi1 = (spi_regs*)&SPI1CMD;
|
||||
|
||||
// The standard HSPI bus pins are used
|
||||
static constexpr uint8_t cs = 15;
|
||||
static constexpr uint8_t miso = 12;
|
||||
static constexpr uint8_t mosi = 13;
|
||||
static constexpr uint8_t sck = 14;
|
||||
|
||||
uint32_t spi_clkval;
|
||||
uint32_t csPin;
|
||||
bool swCS;
|
||||
|
||||
typedef enum { sio = 0, dio = 1 } iotype;
|
||||
static constexpr iotype hspi_mode = sio;
|
||||
|
||||
static constexpr int read_delay = (hspi_mode == dio) ? 4-1 : 0;
|
||||
|
||||
void spi_init()
|
||||
{
|
||||
pinMode(sck, SPECIAL);
|
||||
pinMode(miso, SPECIAL);
|
||||
pinMode(mosi, SPECIAL);
|
||||
pinMode(csPin, swCS ? OUTPUT : SPECIAL );
|
||||
spi1->spi_cmd = 0;
|
||||
GPMUX &= ~(1 << 9);
|
||||
spi1->spi_clock = spi_clkval;
|
||||
spi1->spi_ctrl = 0 ; // MSB first + plain SPI mode
|
||||
spi1->spi_ctrl1 = 0; // undocumented, clear for safety?
|
||||
spi1->spi_ctrl2 = 0; // No add'l delays on signals
|
||||
spi1->spi_user2 = 0; // No insn or insn_bits to set
|
||||
}
|
||||
|
||||
// The SPI hardware cannot make the "command" portion dual or quad, only the addr and data
|
||||
// So using the command portion of the cycle will not work. Concatenate the address
|
||||
// and command into a single 32-bit chunk "address" which will be sent across both bits.
|
||||
void spi_writetransaction(int addr, int addr_bits, int dummy_bits, int data_bits, iotype dual)
|
||||
{
|
||||
// Ensure no writes are still ongoing
|
||||
while (spi1->spi_cmd & SPIBUSY) { delay(0); }
|
||||
|
||||
spi1->spi_addr = addr;
|
||||
spi1->spi_user = (addr_bits? SPIUADDR : 0) | (dummy_bits ? SPIUDUMMY : 0) | (data_bits ? SPIUMOSI : 0) | (dual ? SPIUFWDIO : 0);
|
||||
spi1->spi_user1 = (addr_bits << 26) | (data_bits << 17) | dummy_bits;
|
||||
// No need to set spi_user2, insn field never used
|
||||
__asm ( "" ::: "memory" );
|
||||
if (swCS) {
|
||||
pinMode(csPin, OUTPUT);
|
||||
digitalWrite(csPin, LOW);
|
||||
spi1->spi_cmd = SPIBUSY;
|
||||
while (spi1->spi_cmd & SPIBUSY) { delay(0); }
|
||||
digitalWrite(csPin, HIGH);
|
||||
} else {
|
||||
spi1->spi_cmd = SPIBUSY;
|
||||
// The write may continue on in the background
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t spi_readtransaction(int addr, int addr_bits, int dummy_bits, int data_bits, iotype dual)
|
||||
{
|
||||
// Ensure no writes are still ongoing
|
||||
while (spi1->spi_cmd & SPIBUSY) { delay(0); }
|
||||
|
||||
spi1->spi_addr = addr;
|
||||
spi1->spi_user = (addr_bits? SPIUADDR : 0) | (dummy_bits ? SPIUDUMMY : 0) | SPIUMISO | (dual ? SPIUFWDIO : 0);
|
||||
spi1->spi_user1 = (addr_bits << 26) | (data_bits << 8) | dummy_bits;
|
||||
// No need to set spi_user2, insn field never used
|
||||
__asm ( "" ::: "memory" );
|
||||
if (swCS) {
|
||||
pinMode(csPin, OUTPUT);
|
||||
digitalWrite(csPin, LOW);
|
||||
}
|
||||
spi1->spi_cmd = SPIBUSY;
|
||||
while (spi1->spi_cmd & SPIBUSY) { delay(0); }
|
||||
__asm ( "" ::: "memory" );
|
||||
if (swCS) {
|
||||
digitalWrite(csPin, HIGH);
|
||||
}
|
||||
return spi1->spi_w[0];
|
||||
}
|
||||
|
||||
public:
|
||||
ESP8266SPIRAM()
|
||||
{
|
||||
/* noop */
|
||||
}
|
||||
~ESP8266SPIRAM()
|
||||
{
|
||||
end();
|
||||
}
|
||||
void readBytes(uint32_t addr, void *destV, int count)
|
||||
{
|
||||
uint8_t *dest = (uint8_t*)destV;
|
||||
while (count > 0) {
|
||||
int toRead = std::min(count, 64);
|
||||
spi_readtransaction((0x03 << 24) | addr, 32-1, read_delay, toRead * 8 - 1, hspi_mode);
|
||||
__asm ( "" ::: "memory" );
|
||||
uint32_t work[16]; // FIFO image in RAM that we can byte address
|
||||
int toCopy = (toRead + 3) / 4; // make sure all 32b values updated
|
||||
for (auto i = 0; i < toCopy; i++) {
|
||||
work[i] = spi1->spi_w[i];
|
||||
}
|
||||
memcpy(dest, work, toRead);
|
||||
count -= toRead;
|
||||
dest += toRead;
|
||||
addr += toRead;
|
||||
}
|
||||
}
|
||||
|
||||
void writeBytes(uint32_t addr, const void *srcV, int count)
|
||||
{
|
||||
const uint8_t *src = (const uint8_t *)srcV;
|
||||
while (count > 0) {
|
||||
uint32_t work[16]; // FIFO image in RAM that we can byte address
|
||||
int toWrite = std::min(count, 64);
|
||||
memcpy((void *)work, src, toWrite);
|
||||
int toCopy = (toWrite + 3) / 4; // make sure all 32b values updated
|
||||
|
||||
// Ensure the last SPI is done so we don't overwrite unsent data
|
||||
while (spi1->spi_cmd & SPIBUSY) { delay(0); }
|
||||
for (auto i = 0; i < toCopy; i++) {
|
||||
spi1->spi_w[i] = work[i];
|
||||
}
|
||||
spi_writetransaction((0x02 << 24) | addr, 32 - 1, 0, toWrite * 8 - 1, hspi_mode);
|
||||
count -= toWrite;
|
||||
src += toWrite;
|
||||
addr += toWrite;
|
||||
}
|
||||
}
|
||||
|
||||
void begin(int freqMHz, int cs_pin)
|
||||
{
|
||||
if (freqMHz >= 40) {
|
||||
spi_clkval = 0x00001001;
|
||||
} else if (freqMHz >= 30) {
|
||||
spi_clkval = 0x00002001;
|
||||
} else if (freqMHz >= 20) {
|
||||
spi_clkval = 0x00041001;
|
||||
} else if (freqMHz >= 10) {
|
||||
spi_clkval = 0x000c1001;
|
||||
} else if (freqMHz >= 5) {
|
||||
spi_clkval = 0x001c1001;
|
||||
} else {
|
||||
spi_clkval = 0x009c1001;
|
||||
}
|
||||
csPin = cs_pin;
|
||||
swCS = csPin != cs;
|
||||
|
||||
// Manually reset chip from DIO to SIO mode (HW SPI has issues with <8 bits/clocks total output)
|
||||
digitalWrite(csPin, HIGH);
|
||||
digitalWrite(mosi, HIGH);
|
||||
digitalWrite(miso, HIGH);
|
||||
digitalWrite(sck, LOW);
|
||||
pinMode(csPin, OUTPUT);
|
||||
pinMode(miso, OUTPUT);
|
||||
pinMode(mosi, OUTPUT);
|
||||
pinMode(sck, OUTPUT);
|
||||
digitalWrite(csPin, HIGH);
|
||||
delay(100);
|
||||
digitalWrite(csPin, LOW);
|
||||
delay(1);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
digitalWrite(sck, HIGH);
|
||||
delay(1);
|
||||
digitalWrite(sck, LOW);
|
||||
delay(1);
|
||||
}
|
||||
digitalWrite(csPin, HIGH);
|
||||
|
||||
// Set up the SPI regs
|
||||
spi_init();
|
||||
|
||||
// Enable streaming read/write mode
|
||||
spi1->spi_w[0] = 0x40;
|
||||
spi_writetransaction(0x01<<24, 8-1, 0, 8-1, sio);
|
||||
|
||||
if (hspi_mode == dio) {
|
||||
// Ramp up to DIO mode
|
||||
spi_writetransaction(0x3b<<24, 8-1, 0, 0, sio);
|
||||
spi1->spi_ctrl |= SPICDIO | SPICFASTRD;
|
||||
}
|
||||
}
|
||||
|
||||
void end()
|
||||
{
|
||||
pinMode(csPin, INPUT);
|
||||
pinMode(miso, INPUT);
|
||||
pinMode(mosi, INPUT);
|
||||
pinMode(sck, INPUT);
|
||||
}
|
||||
|
||||
};
|
||||
Reference in new issue
Block a user