no more acquire_spi :)

custom
jacqueline 11 months ago
parent 39460931d8
commit 72344b5777
  1. 9
      src/drivers/display.cpp
  2. 1
      src/drivers/include/drivers/spi.hpp
  3. 6
      src/drivers/spi.cpp
  4. 3
      src/tangara/audio/fatfs_source.cpp
  5. 8
      src/tangara/audio/fatfs_stream_factory.cpp
  6. 8
      src/tangara/database/database.cpp
  7. 18
      src/tangara/database/env_esp.cpp
  8. 12
      src/tangara/database/file_gatherer.cpp
  9. 20
      src/tangara/database/tag_parser.cpp
  10. 54
      src/tangara/lua/file_iterator.cpp
  11. 12
      src/tangara/system_fsm/running.cpp

@ -284,13 +284,8 @@ void Display::SendTransaction(TransactionType type,
gpio_set_level(kDisplayDr, type); gpio_set_level(kDisplayDr, type);
esp_err_t ret; // TODO(jacqueline): Handle these errors better.
{ ESP_ERROR_CHECK(spi_device_transmit(handle_, &sTransaction));
auto lock = drivers::acquire_spi();
// TODO(jacqueline): Handle these errors.
ret = spi_device_transmit(handle_, &sTransaction);
}
ESP_ERROR_CHECK(ret);
} }
void Display::OnLvglFlush(lv_disp_drv_t* disp_drv, void Display::OnLvglFlush(lv_disp_drv_t* disp_drv,

@ -13,6 +13,5 @@ namespace drivers {
esp_err_t init_spi(void); esp_err_t init_spi(void);
esp_err_t deinit_spi(void); esp_err_t deinit_spi(void);
int acquire_spi(void);
} // namespace drivers } // namespace drivers

@ -56,10 +56,4 @@ esp_err_t deinit_spi(void) {
return spi_bus_free(kSpiHost); return spi_bus_free(kSpiHost);
} }
int acquire_spi(void) {
// Cross fingers emoji: I *think* this is now resolved???
// return std::lock_guard<std::mutex>{sSpiMutex};
return 1;
}
} // namespace drivers } // namespace drivers

@ -29,12 +29,10 @@ FatfsSource::FatfsSource(codecs::StreamType t, std::unique_ptr<FIL> file)
: IStream(t), file_(std::move(file)) {} : IStream(t), file_(std::move(file)) {}
FatfsSource::~FatfsSource() { FatfsSource::~FatfsSource() {
auto lock = drivers::acquire_spi();
f_close(file_.get()); f_close(file_.get());
} }
auto FatfsSource::Read(std::span<std::byte> dest) -> ssize_t { auto FatfsSource::Read(std::span<std::byte> dest) -> ssize_t {
auto lock = drivers::acquire_spi();
if (f_eof(file_.get())) { if (f_eof(file_.get())) {
return 0; return 0;
} }
@ -52,7 +50,6 @@ auto FatfsSource::CanSeek() -> bool {
} }
auto FatfsSource::SeekTo(int64_t destination, SeekFrom from) -> void { auto FatfsSource::SeekTo(int64_t destination, SeekFrom from) -> void {
auto lock = drivers::acquire_spi();
switch (from) { switch (from) {
case SeekFrom::kStartOfStream: case SeekFrom::kStartOfStream:
f_lseek(file_.get(), destination); f_lseek(file_.get(), destination);

@ -65,13 +65,7 @@ auto FatfsStreamFactory::create(std::string path, uint32_t offset)
} }
std::unique_ptr<FIL> file = std::make_unique<FIL>(); std::unique_ptr<FIL> file = std::make_unique<FIL>();
FRESULT res; FRESULT res = f_open(file.get(), path.c_str(), FA_READ);
{
auto lock = drivers::acquire_spi();
res = f_open(file.get(), path.c_str(), FA_READ);
}
if (res != FR_OK) { if (res != FR_OK) {
ESP_LOGE(kTag, "failed to open file! res: %i", res); ESP_LOGE(kTag, "failed to open file! res: %i", res);
return {}; return {};

@ -206,8 +206,6 @@ auto Database::schemaVersion() -> std::string {
} }
auto Database::sizeOnDiskBytes() -> size_t { auto Database::sizeOnDiskBytes() -> size_t {
auto lock = drivers::acquire_spi();
FF_DIR dir; FF_DIR dir;
FRESULT res = f_opendir(&dir, kDbPath); FRESULT res = f_opendir(&dir, kDbPath);
if (res != FR_OK) { if (res != FR_OK) {
@ -328,12 +326,8 @@ auto Database::updateIndexes() -> void {
continue; continue;
} }
FRESULT res;
FILINFO info; FILINFO info;
{ FRESULT res = f_stat(track->filepath.c_str(), &info);
auto lock = drivers::acquire_spi();
res = f_stat(track->filepath.c_str(), &info);
}
std::pair<uint16_t, uint16_t> modified_at{0, 0}; std::pair<uint16_t, uint16_t> modified_at{0, 0};
if (res == FR_OK) { if (res == FR_OK) {

@ -103,12 +103,10 @@ class EspSequentialFile final : public SequentialFile {
EspSequentialFile(const std::string& filename, FIL file) EspSequentialFile(const std::string& filename, FIL file)
: file_(file), filename_(filename) {} : file_(file), filename_(filename) {}
~EspSequentialFile() override { ~EspSequentialFile() override {
auto lock = drivers::acquire_spi();
f_close(&file_); f_close(&file_);
} }
Status Read(size_t n, Slice* result, char* scratch) override { Status Read(size_t n, Slice* result, char* scratch) override {
auto lock = drivers::acquire_spi();
UINT read_size = 0; UINT read_size = 0;
FRESULT res = f_read(&file_, scratch, n, &read_size); FRESULT res = f_read(&file_, scratch, n, &read_size);
if (res != FR_OK) { // Read error. if (res != FR_OK) { // Read error.
@ -119,7 +117,6 @@ class EspSequentialFile final : public SequentialFile {
} }
Status Skip(uint64_t n) override { Status Skip(uint64_t n) override {
auto lock = drivers::acquire_spi();
DWORD current_pos = f_tell(&file_); DWORD current_pos = f_tell(&file_);
FRESULT res = f_lseek(&file_, current_pos + n); FRESULT res = f_lseek(&file_, current_pos + n);
if (res != FR_OK) { if (res != FR_OK) {
@ -151,7 +148,6 @@ class EspRandomAccessFile final : public RandomAccessFile {
size_t n, size_t n,
Slice* result, Slice* result,
char* scratch) const override { char* scratch) const override {
auto lock = drivers::acquire_spi();
FIL file; FIL file;
FRESULT res = f_open(&file, filename_.c_str(), FA_READ); FRESULT res = f_open(&file, filename_.c_str(), FA_READ);
if (res != FR_OK) { if (res != FR_OK) {
@ -200,7 +196,6 @@ class EspWritableFile final : public WritableFile {
return EspError(filename_, FR_NOT_ENABLED); return EspError(filename_, FR_NOT_ENABLED);
} }
auto lock = drivers::acquire_spi();
size_t write_size = data.size(); size_t write_size = data.size();
const char* write_data = data.data(); const char* write_data = data.data();
@ -214,7 +209,6 @@ class EspWritableFile final : public WritableFile {
} }
Status Close() override { Status Close() override {
auto lock = drivers::acquire_spi();
is_open_ = false; is_open_ = false;
FRESULT res = f_close(&file_); FRESULT res = f_close(&file_);
if (res != FR_OK) { if (res != FR_OK) {
@ -229,7 +223,6 @@ class EspWritableFile final : public WritableFile {
if (!is_open_) { if (!is_open_) {
return EspError(filename_, FR_NOT_ENABLED); return EspError(filename_, FR_NOT_ENABLED);
} }
auto lock = drivers::acquire_spi();
FRESULT res = f_sync(&file_); FRESULT res = f_sync(&file_);
if (res != FR_OK) { if (res != FR_OK) {
return EspError(filename_, res); return EspError(filename_, res);
@ -285,7 +278,6 @@ EspEnv::~EspEnv() {
Status EspEnv::NewSequentialFile(const std::string& filename, Status EspEnv::NewSequentialFile(const std::string& filename,
SequentialFile** result) { SequentialFile** result) {
auto lock = drivers::acquire_spi();
FIL file; FIL file;
FRESULT res = f_open(&file, filename.c_str(), FA_READ); FRESULT res = f_open(&file, filename.c_str(), FA_READ);
if (res != FR_OK) { if (res != FR_OK) {
@ -299,7 +291,6 @@ Status EspEnv::NewSequentialFile(const std::string& filename,
Status EspEnv::NewRandomAccessFile(const std::string& filename, Status EspEnv::NewRandomAccessFile(const std::string& filename,
RandomAccessFile** result) { RandomAccessFile** result) {
auto lock = drivers::acquire_spi();
// EspRandomAccessFile doesn't try to open the file until it's needed, so // EspRandomAccessFile doesn't try to open the file until it's needed, so
// we need to first ensure the file exists to handle the NotFound case // we need to first ensure the file exists to handle the NotFound case
// correctly. // correctly.
@ -316,7 +307,6 @@ Status EspEnv::NewRandomAccessFile(const std::string& filename,
Status EspEnv::NewWritableFile(const std::string& filename, Status EspEnv::NewWritableFile(const std::string& filename,
WritableFile** result) { WritableFile** result) {
auto lock = drivers::acquire_spi();
FIL file; FIL file;
FRESULT res = f_open(&file, filename.c_str(), FA_WRITE | FA_CREATE_ALWAYS); FRESULT res = f_open(&file, filename.c_str(), FA_WRITE | FA_CREATE_ALWAYS);
if (res != FR_OK) { if (res != FR_OK) {
@ -330,7 +320,6 @@ Status EspEnv::NewWritableFile(const std::string& filename,
Status EspEnv::NewAppendableFile(const std::string& filename, Status EspEnv::NewAppendableFile(const std::string& filename,
WritableFile** result) { WritableFile** result) {
auto lock = drivers::acquire_spi();
FIL file; FIL file;
FRESULT res = f_open(&file, filename.c_str(), FA_WRITE | FA_OPEN_APPEND); FRESULT res = f_open(&file, filename.c_str(), FA_WRITE | FA_OPEN_APPEND);
if (res != FR_OK) { if (res != FR_OK) {
@ -343,7 +332,6 @@ Status EspEnv::NewAppendableFile(const std::string& filename,
} }
bool EspEnv::FileExists(const std::string& filename) { bool EspEnv::FileExists(const std::string& filename) {
auto lock = drivers::acquire_spi();
FILINFO info; FILINFO info;
return f_stat(filename.c_str(), &info) == FR_OK; return f_stat(filename.c_str(), &info) == FR_OK;
} }
@ -352,7 +340,6 @@ Status EspEnv::GetChildren(const std::string& directory_path,
std::vector<std::string>* result) { std::vector<std::string>* result) {
result->clear(); result->clear();
auto lock = drivers::acquire_spi();
FF_DIR dir; FF_DIR dir;
FRESULT res = f_opendir(&dir, directory_path.c_str()); FRESULT res = f_opendir(&dir, directory_path.c_str());
if (res != FR_OK) { if (res != FR_OK) {
@ -380,7 +367,6 @@ Status EspEnv::GetChildren(const std::string& directory_path,
} }
Status EspEnv::RemoveFile(const std::string& filename) { Status EspEnv::RemoveFile(const std::string& filename) {
auto lock = drivers::acquire_spi();
FRESULT res = f_unlink(filename.c_str()); FRESULT res = f_unlink(filename.c_str());
if (res != FR_OK) { if (res != FR_OK) {
return EspError(filename, res); return EspError(filename, res);
@ -389,7 +375,6 @@ Status EspEnv::RemoveFile(const std::string& filename) {
} }
Status EspEnv::CreateDir(const std::string& dirname) { Status EspEnv::CreateDir(const std::string& dirname) {
auto lock = drivers::acquire_spi();
FRESULT res = f_mkdir(dirname.c_str()); FRESULT res = f_mkdir(dirname.c_str());
if (res != FR_OK) { if (res != FR_OK) {
return EspError(dirname, res); return EspError(dirname, res);
@ -402,7 +387,6 @@ Status EspEnv::RemoveDir(const std::string& dirname) {
} }
Status EspEnv::GetFileSize(const std::string& filename, uint64_t* size) { Status EspEnv::GetFileSize(const std::string& filename, uint64_t* size) {
auto lock = drivers::acquire_spi();
FILINFO info; FILINFO info;
FRESULT res = f_stat(filename.c_str(), &info); FRESULT res = f_stat(filename.c_str(), &info);
if (res != FR_OK) { if (res != FR_OK) {
@ -421,7 +405,6 @@ Status EspEnv::RenameFile(const std::string& from, const std::string& to) {
return s; return s;
} }
} }
auto lock = drivers::acquire_spi();
FRESULT res = f_rename(from.c_str(), to.c_str()); FRESULT res = f_rename(from.c_str(), to.c_str());
if (res != FR_OK) { if (res != FR_OK) {
return EspError(from, res); return EspError(from, res);
@ -460,7 +443,6 @@ Status EspEnv::GetTestDirectory(std::string* result) {
} }
Status EspEnv::NewLogger(const std::string& filename, Logger** result) { Status EspEnv::NewLogger(const std::string& filename, Logger** result) {
auto lock = drivers::acquire_spi();
FIL file; FIL file;
FRESULT res = f_open(&file, filename.c_str(), FA_WRITE | FA_OPEN_APPEND); FRESULT res = f_open(&file, filename.c_str(), FA_WRITE | FA_OPEN_APPEND);
if (res != FR_OK) { if (res != FR_OK) {

@ -33,11 +33,7 @@ auto FileGathererImpl::FindFiles(
const TCHAR* next_path = static_cast<const TCHAR*>(next_path_str.c_str()); const TCHAR* next_path = static_cast<const TCHAR*>(next_path_str.c_str());
FF_DIR dir; FF_DIR dir;
FRESULT res; FRESULT res = f_opendir(&dir, next_path);
{
auto lock = drivers::acquire_spi();
res = f_opendir(&dir, next_path);
}
if (res != FR_OK) { if (res != FR_OK) {
// TODO: log. // TODO: log.
continue; continue;
@ -45,10 +41,7 @@ auto FileGathererImpl::FindFiles(
for (;;) { for (;;) {
FILINFO info; FILINFO info;
{ res = f_readdir(&dir, &info);
auto lock = drivers::acquire_spi();
res = f_readdir(&dir, &info);
}
if (res != FR_OK || info.fname[0] == 0) { if (res != FR_OK || info.fname[0] == 0) {
// No more files in the directory. // No more files in the directory.
break; break;
@ -72,7 +65,6 @@ auto FileGathererImpl::FindFiles(
} }
} }
auto lock = drivers::acquire_spi();
f_closedir(&dir); f_closedir(&dir);
} }
} }

@ -148,15 +148,13 @@ auto TagParserImpl::parseNew(std::string_view p) -> std::shared_ptr<TrackTags> {
libtags::Aux aux; libtags::Aux aux;
auto out = TrackTags::create(); auto out = TrackTags::create();
aux.tags = out.get(); aux.tags = out.get();
{
auto lock = drivers::acquire_spi();
if (f_stat(path.c_str(), &aux.info) != FR_OK || if (f_stat(path.c_str(), &aux.info) != FR_OK ||
f_open(&aux.file, path.c_str(), FA_READ) != FR_OK) { f_open(&aux.file, path.c_str(), FA_READ) != FR_OK) {
ESP_LOGW(kTag, "failed to open file %s", path.c_str()); ESP_LOGW(kTag, "failed to open file %s", path.c_str());
return {}; return {};
}
} }
// Fine to have this on the stack; this is only called on tasks with large // Fine to have this on the stack; this is only called on tasks with large
// stacks anyway, due to all the string handling. // stacks anyway, due to all the string handling.
char buf[kBufSize]; char buf[kBufSize];
@ -169,12 +167,8 @@ auto TagParserImpl::parseNew(std::string_view p) -> std::shared_ptr<TrackTags> {
ctx.buf = buf; ctx.buf = buf;
ctx.bufsz = kBufSize; ctx.bufsz = kBufSize;
int res; int res = tagsget(&ctx);
{ f_close(&aux.file);
auto lock = drivers::acquire_spi();
res = tagsget(&ctx);
f_close(&aux.file);
}
if (res != 0) { if (res != 0) {
// Parsing failed. // Parsing failed.

@ -8,20 +8,15 @@
#include <string> #include <string>
#include "ff.h"
#include "drivers/spi.hpp" #include "drivers/spi.hpp"
#include "ff.h"
namespace lua { namespace lua {
[[maybe_unused]] static const char* kTag = "FileIterator"; [[maybe_unused]] static const char* kTag = "FileIterator";
FileIterator::FileIterator(std::string filepath) FileIterator::FileIterator(std::string filepath)
: original_path_(filepath), : original_path_(filepath), current_(), offset_(-1) {
current_(),
offset_(-1)
{
auto lock = drivers::acquire_spi();
const TCHAR* path = static_cast<const TCHAR*>(filepath.c_str()); const TCHAR* path = static_cast<const TCHAR*>(filepath.c_str());
FRESULT res = f_opendir(&dir_, path); FRESULT res = f_opendir(&dir_, path);
if (res != FR_OK) { if (res != FR_OK) {
@ -30,7 +25,6 @@ FileIterator::FileIterator(std::string filepath)
} }
FileIterator::~FileIterator() { FileIterator::~FileIterator() {
auto lock = drivers::acquire_spi();
f_closedir(&dir_); f_closedir(&dir_);
} }
@ -48,7 +42,7 @@ auto FileIterator::prev() -> void {
return; return;
} }
f_rewinddir(&dir_); f_rewinddir(&dir_);
auto new_offset = offset_-1; auto new_offset = offset_ - 1;
offset_ = -1; offset_ = -1;
for (int i = 0; i <= new_offset; i++) { for (int i = 0; i <= new_offset; i++) {
iterate(false); iterate(false);
@ -56,32 +50,30 @@ auto FileIterator::prev() -> void {
} }
auto FileIterator::iterate(bool reverse) -> bool { auto FileIterator::iterate(bool reverse) -> bool {
FILINFO info; FILINFO info;
{ auto res = f_readdir(&dir_, &info);
auto lock = drivers::acquire_spi(); if (res != FR_OK) {
auto res = f_readdir(&dir_, &info); ESP_LOGE(kTag, "Error reading directory. Error: %d", res);
if (res != FR_OK) { return false;
ESP_LOGE(kTag, "Error reading directory. Error: %d", res); }
return false; if (info.fname[0] == 0) {
} // End of directory
} // Set value to nil
if (info.fname[0] == 0) { current_.reset();
// End of directory } else {
// Set value to nil // Update current value
current_.reset(); offset_++;
} else { current_ = FileEntry{
// Update current value
offset_++;
current_ = FileEntry{
.index = offset_, .index = offset_,
.isHidden = (info.fattrib & AM_HID) > 0, .isHidden = (info.fattrib & AM_HID) > 0,
.isDirectory = (info.fattrib & AM_DIR) > 0, .isDirectory = (info.fattrib & AM_DIR) > 0,
.isTrack = false, // TODO .isTrack = false, // TODO
.filepath = original_path_ + (original_path_.size()>0?"/":"") + info.fname, .filepath = original_path_ + (original_path_.size() > 0 ? "/" : "") +
info.fname,
}; };
} }
return true; return true;
} }
} // namespace lua } // namespace lua

@ -82,22 +82,14 @@ void Running::react(const SdDetectChanged& ev) {
// supriously. FIXME: Why? // supriously. FIXME: Why?
// Instead, check whether or not the card has actually gone away. // Instead, check whether or not the card has actually gone away.
if (sStorage) { if (sStorage) {
FRESULT res;
FF_DIR dir; FF_DIR dir;
{ FRESULT res = f_opendir(&dir, "/");
auto lock = drivers::acquire_spi();
res = f_opendir(&dir, "/");
}
if (res != FR_OK) { if (res != FR_OK) {
ESP_LOGW(kTag, "sd card ejected unsafely!"); ESP_LOGW(kTag, "sd card ejected unsafely!");
unmountStorage(); unmountStorage();
} }
{ f_closedir(&dir);
auto lock = drivers::acquire_spi();
f_closedir(&dir);
}
} }
} }

Loading…
Cancel
Save