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:
Earle F. Philhower, III authored and GitHub committed 2020-04-14 15:41:47 -07:00
1 parent be0ee8283f
commit 0fd2d0413b
5 files changed
+372 -132

No files matched your search

+97 -86
View File
@@ -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;
}