From 72344b5777dd78bbad6bbc8b52c0fa271671cf90 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 4 Jun 2024 08:09:40 +1000 Subject: [PATCH] no more acquire_spi :) --- src/drivers/display.cpp | 9 +--- src/drivers/include/drivers/spi.hpp | 1 - src/drivers/spi.cpp | 6 --- src/tangara/audio/fatfs_source.cpp | 3 -- src/tangara/audio/fatfs_stream_factory.cpp | 8 +-- src/tangara/database/database.cpp | 8 +-- src/tangara/database/env_esp.cpp | 18 ------- src/tangara/database/file_gatherer.cpp | 12 +---- src/tangara/database/tag_parser.cpp | 20 +++----- src/tangara/lua/file_iterator.cpp | 58 ++++++++++------------ src/tangara/system_fsm/running.cpp | 12 +---- 11 files changed, 40 insertions(+), 115 deletions(-) diff --git a/src/drivers/display.cpp b/src/drivers/display.cpp index e28b6b47..bd219f4b 100644 --- a/src/drivers/display.cpp +++ b/src/drivers/display.cpp @@ -284,13 +284,8 @@ void Display::SendTransaction(TransactionType type, gpio_set_level(kDisplayDr, type); - esp_err_t ret; - { - auto lock = drivers::acquire_spi(); - // TODO(jacqueline): Handle these errors. - ret = spi_device_transmit(handle_, &sTransaction); - } - ESP_ERROR_CHECK(ret); + // TODO(jacqueline): Handle these errors better. + ESP_ERROR_CHECK(spi_device_transmit(handle_, &sTransaction)); } void Display::OnLvglFlush(lv_disp_drv_t* disp_drv, diff --git a/src/drivers/include/drivers/spi.hpp b/src/drivers/include/drivers/spi.hpp index 95b002b4..4a63d7cc 100644 --- a/src/drivers/include/drivers/spi.hpp +++ b/src/drivers/include/drivers/spi.hpp @@ -13,6 +13,5 @@ namespace drivers { esp_err_t init_spi(void); esp_err_t deinit_spi(void); -int acquire_spi(void); } // namespace drivers diff --git a/src/drivers/spi.cpp b/src/drivers/spi.cpp index 50799c6e..632fe89f 100644 --- a/src/drivers/spi.cpp +++ b/src/drivers/spi.cpp @@ -56,10 +56,4 @@ esp_err_t deinit_spi(void) { return spi_bus_free(kSpiHost); } -int acquire_spi(void) { - // Cross fingers emoji: I *think* this is now resolved??? - // return std::lock_guard{sSpiMutex}; - return 1; -} - } // namespace drivers diff --git a/src/tangara/audio/fatfs_source.cpp b/src/tangara/audio/fatfs_source.cpp index fb6a684d..4b517af8 100644 --- a/src/tangara/audio/fatfs_source.cpp +++ b/src/tangara/audio/fatfs_source.cpp @@ -29,12 +29,10 @@ FatfsSource::FatfsSource(codecs::StreamType t, std::unique_ptr file) : IStream(t), file_(std::move(file)) {} FatfsSource::~FatfsSource() { - auto lock = drivers::acquire_spi(); f_close(file_.get()); } auto FatfsSource::Read(std::span dest) -> ssize_t { - auto lock = drivers::acquire_spi(); if (f_eof(file_.get())) { return 0; } @@ -52,7 +50,6 @@ auto FatfsSource::CanSeek() -> bool { } auto FatfsSource::SeekTo(int64_t destination, SeekFrom from) -> void { - auto lock = drivers::acquire_spi(); switch (from) { case SeekFrom::kStartOfStream: f_lseek(file_.get(), destination); diff --git a/src/tangara/audio/fatfs_stream_factory.cpp b/src/tangara/audio/fatfs_stream_factory.cpp index db08e68c..80677b2d 100644 --- a/src/tangara/audio/fatfs_stream_factory.cpp +++ b/src/tangara/audio/fatfs_stream_factory.cpp @@ -65,13 +65,7 @@ auto FatfsStreamFactory::create(std::string path, uint32_t offset) } std::unique_ptr file = std::make_unique(); - FRESULT res; - - { - auto lock = drivers::acquire_spi(); - res = f_open(file.get(), path.c_str(), FA_READ); - } - + FRESULT res = f_open(file.get(), path.c_str(), FA_READ); if (res != FR_OK) { ESP_LOGE(kTag, "failed to open file! res: %i", res); return {}; diff --git a/src/tangara/database/database.cpp b/src/tangara/database/database.cpp index 00dda5ec..3258405b 100644 --- a/src/tangara/database/database.cpp +++ b/src/tangara/database/database.cpp @@ -206,8 +206,6 @@ auto Database::schemaVersion() -> std::string { } auto Database::sizeOnDiskBytes() -> size_t { - auto lock = drivers::acquire_spi(); - FF_DIR dir; FRESULT res = f_opendir(&dir, kDbPath); if (res != FR_OK) { @@ -328,12 +326,8 @@ auto Database::updateIndexes() -> void { continue; } - FRESULT res; FILINFO info; - { - auto lock = drivers::acquire_spi(); - res = f_stat(track->filepath.c_str(), &info); - } + FRESULT res = f_stat(track->filepath.c_str(), &info); std::pair modified_at{0, 0}; if (res == FR_OK) { diff --git a/src/tangara/database/env_esp.cpp b/src/tangara/database/env_esp.cpp index 86a30613..843be91d 100644 --- a/src/tangara/database/env_esp.cpp +++ b/src/tangara/database/env_esp.cpp @@ -103,12 +103,10 @@ class EspSequentialFile final : public SequentialFile { EspSequentialFile(const std::string& filename, FIL file) : file_(file), filename_(filename) {} ~EspSequentialFile() override { - auto lock = drivers::acquire_spi(); f_close(&file_); } Status Read(size_t n, Slice* result, char* scratch) override { - auto lock = drivers::acquire_spi(); UINT read_size = 0; FRESULT res = f_read(&file_, scratch, n, &read_size); if (res != FR_OK) { // Read error. @@ -119,7 +117,6 @@ class EspSequentialFile final : public SequentialFile { } Status Skip(uint64_t n) override { - auto lock = drivers::acquire_spi(); DWORD current_pos = f_tell(&file_); FRESULT res = f_lseek(&file_, current_pos + n); if (res != FR_OK) { @@ -151,7 +148,6 @@ class EspRandomAccessFile final : public RandomAccessFile { size_t n, Slice* result, char* scratch) const override { - auto lock = drivers::acquire_spi(); FIL file; FRESULT res = f_open(&file, filename_.c_str(), FA_READ); if (res != FR_OK) { @@ -200,7 +196,6 @@ class EspWritableFile final : public WritableFile { return EspError(filename_, FR_NOT_ENABLED); } - auto lock = drivers::acquire_spi(); size_t write_size = data.size(); const char* write_data = data.data(); @@ -214,7 +209,6 @@ class EspWritableFile final : public WritableFile { } Status Close() override { - auto lock = drivers::acquire_spi(); is_open_ = false; FRESULT res = f_close(&file_); if (res != FR_OK) { @@ -229,7 +223,6 @@ class EspWritableFile final : public WritableFile { if (!is_open_) { return EspError(filename_, FR_NOT_ENABLED); } - auto lock = drivers::acquire_spi(); FRESULT res = f_sync(&file_); if (res != FR_OK) { return EspError(filename_, res); @@ -285,7 +278,6 @@ EspEnv::~EspEnv() { Status EspEnv::NewSequentialFile(const std::string& filename, SequentialFile** result) { - auto lock = drivers::acquire_spi(); FIL file; FRESULT res = f_open(&file, filename.c_str(), FA_READ); if (res != FR_OK) { @@ -299,7 +291,6 @@ Status EspEnv::NewSequentialFile(const std::string& filename, Status EspEnv::NewRandomAccessFile(const std::string& filename, RandomAccessFile** result) { - auto lock = drivers::acquire_spi(); // 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 // correctly. @@ -316,7 +307,6 @@ Status EspEnv::NewRandomAccessFile(const std::string& filename, Status EspEnv::NewWritableFile(const std::string& filename, WritableFile** result) { - auto lock = drivers::acquire_spi(); FIL file; FRESULT res = f_open(&file, filename.c_str(), FA_WRITE | FA_CREATE_ALWAYS); if (res != FR_OK) { @@ -330,7 +320,6 @@ Status EspEnv::NewWritableFile(const std::string& filename, Status EspEnv::NewAppendableFile(const std::string& filename, WritableFile** result) { - auto lock = drivers::acquire_spi(); FIL file; FRESULT res = f_open(&file, filename.c_str(), FA_WRITE | FA_OPEN_APPEND); if (res != FR_OK) { @@ -343,7 +332,6 @@ Status EspEnv::NewAppendableFile(const std::string& filename, } bool EspEnv::FileExists(const std::string& filename) { - auto lock = drivers::acquire_spi(); FILINFO info; return f_stat(filename.c_str(), &info) == FR_OK; } @@ -352,7 +340,6 @@ Status EspEnv::GetChildren(const std::string& directory_path, std::vector* result) { result->clear(); - auto lock = drivers::acquire_spi(); FF_DIR dir; FRESULT res = f_opendir(&dir, directory_path.c_str()); if (res != FR_OK) { @@ -380,7 +367,6 @@ Status EspEnv::GetChildren(const std::string& directory_path, } Status EspEnv::RemoveFile(const std::string& filename) { - auto lock = drivers::acquire_spi(); FRESULT res = f_unlink(filename.c_str()); if (res != FR_OK) { return EspError(filename, res); @@ -389,7 +375,6 @@ Status EspEnv::RemoveFile(const std::string& filename) { } Status EspEnv::CreateDir(const std::string& dirname) { - auto lock = drivers::acquire_spi(); FRESULT res = f_mkdir(dirname.c_str()); if (res != FR_OK) { 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) { - auto lock = drivers::acquire_spi(); FILINFO info; FRESULT res = f_stat(filename.c_str(), &info); if (res != FR_OK) { @@ -421,7 +405,6 @@ Status EspEnv::RenameFile(const std::string& from, const std::string& to) { return s; } } - auto lock = drivers::acquire_spi(); FRESULT res = f_rename(from.c_str(), to.c_str()); if (res != FR_OK) { return EspError(from, res); @@ -460,7 +443,6 @@ Status EspEnv::GetTestDirectory(std::string* result) { } Status EspEnv::NewLogger(const std::string& filename, Logger** result) { - auto lock = drivers::acquire_spi(); FIL file; FRESULT res = f_open(&file, filename.c_str(), FA_WRITE | FA_OPEN_APPEND); if (res != FR_OK) { diff --git a/src/tangara/database/file_gatherer.cpp b/src/tangara/database/file_gatherer.cpp index 75a1af27..dd4b1138 100644 --- a/src/tangara/database/file_gatherer.cpp +++ b/src/tangara/database/file_gatherer.cpp @@ -33,11 +33,7 @@ auto FileGathererImpl::FindFiles( const TCHAR* next_path = static_cast(next_path_str.c_str()); FF_DIR dir; - FRESULT res; - { - auto lock = drivers::acquire_spi(); - res = f_opendir(&dir, next_path); - } + FRESULT res = f_opendir(&dir, next_path); if (res != FR_OK) { // TODO: log. continue; @@ -45,10 +41,7 @@ auto FileGathererImpl::FindFiles( for (;;) { FILINFO info; - { - auto lock = drivers::acquire_spi(); - res = f_readdir(&dir, &info); - } + res = f_readdir(&dir, &info); if (res != FR_OK || info.fname[0] == 0) { // No more files in the directory. break; @@ -72,7 +65,6 @@ auto FileGathererImpl::FindFiles( } } - auto lock = drivers::acquire_spi(); f_closedir(&dir); } } diff --git a/src/tangara/database/tag_parser.cpp b/src/tangara/database/tag_parser.cpp index 2df2d90f..d377adb1 100644 --- a/src/tangara/database/tag_parser.cpp +++ b/src/tangara/database/tag_parser.cpp @@ -148,15 +148,13 @@ auto TagParserImpl::parseNew(std::string_view p) -> std::shared_ptr { libtags::Aux aux; auto out = TrackTags::create(); aux.tags = out.get(); - { - auto lock = drivers::acquire_spi(); - if (f_stat(path.c_str(), &aux.info) != FR_OK || - f_open(&aux.file, path.c_str(), FA_READ) != FR_OK) { - ESP_LOGW(kTag, "failed to open file %s", path.c_str()); - return {}; - } + if (f_stat(path.c_str(), &aux.info) != FR_OK || + f_open(&aux.file, path.c_str(), FA_READ) != FR_OK) { + ESP_LOGW(kTag, "failed to open file %s", path.c_str()); + return {}; } + // Fine to have this on the stack; this is only called on tasks with large // stacks anyway, due to all the string handling. char buf[kBufSize]; @@ -169,12 +167,8 @@ auto TagParserImpl::parseNew(std::string_view p) -> std::shared_ptr { ctx.buf = buf; ctx.bufsz = kBufSize; - int res; - { - auto lock = drivers::acquire_spi(); - res = tagsget(&ctx); - f_close(&aux.file); - } + int res = tagsget(&ctx); + f_close(&aux.file); if (res != 0) { // Parsing failed. diff --git a/src/tangara/lua/file_iterator.cpp b/src/tangara/lua/file_iterator.cpp index d0eb0bae..c3d63a16 100644 --- a/src/tangara/lua/file_iterator.cpp +++ b/src/tangara/lua/file_iterator.cpp @@ -8,20 +8,15 @@ #include -#include "ff.h" #include "drivers/spi.hpp" +#include "ff.h" namespace lua { [[maybe_unused]] static const char* kTag = "FileIterator"; -FileIterator::FileIterator(std::string filepath) -: original_path_(filepath), - current_(), - offset_(-1) - { - auto lock = drivers::acquire_spi(); - +FileIterator::FileIterator(std::string filepath) + : original_path_(filepath), current_(), offset_(-1) { const TCHAR* path = static_cast(filepath.c_str()); FRESULT res = f_opendir(&dir_, path); if (res != FR_OK) { @@ -30,7 +25,6 @@ FileIterator::FileIterator(std::string filepath) } FileIterator::~FileIterator() { - auto lock = drivers::acquire_spi(); f_closedir(&dir_); } @@ -48,7 +42,7 @@ auto FileIterator::prev() -> void { return; } f_rewinddir(&dir_); - auto new_offset = offset_-1; + auto new_offset = offset_ - 1; offset_ = -1; for (int i = 0; i <= new_offset; i++) { iterate(false); @@ -56,32 +50,30 @@ auto FileIterator::prev() -> void { } auto FileIterator::iterate(bool reverse) -> bool { - FILINFO info; - { - auto lock = drivers::acquire_spi(); - auto res = f_readdir(&dir_, &info); - if (res != FR_OK) { - ESP_LOGE(kTag, "Error reading directory. Error: %d", res); - return false; - } - } - if (info.fname[0] == 0) { - // End of directory - // Set value to nil - current_.reset(); - } else { - // Update current value - offset_++; - current_ = FileEntry{ + FILINFO info; + auto res = f_readdir(&dir_, &info); + if (res != FR_OK) { + ESP_LOGE(kTag, "Error reading directory. Error: %d", res); + return false; + } + if (info.fname[0] == 0) { + // End of directory + // Set value to nil + current_.reset(); + } else { + // Update current value + offset_++; + current_ = FileEntry{ .index = offset_, .isHidden = (info.fattrib & AM_HID) > 0, .isDirectory = (info.fattrib & AM_DIR) > 0, - .isTrack = false, // TODO - .filepath = original_path_ + (original_path_.size()>0?"/":"") + info.fname, + .isTrack = false, // TODO + .filepath = original_path_ + (original_path_.size() > 0 ? "/" : "") + + info.fname, - }; - } - return true; + }; + } + return true; } -} // namespace lua \ No newline at end of file +} // namespace lua diff --git a/src/tangara/system_fsm/running.cpp b/src/tangara/system_fsm/running.cpp index ac36ec64..c808e9da 100644 --- a/src/tangara/system_fsm/running.cpp +++ b/src/tangara/system_fsm/running.cpp @@ -82,22 +82,14 @@ void Running::react(const SdDetectChanged& ev) { // supriously. FIXME: Why? // Instead, check whether or not the card has actually gone away. if (sStorage) { - FRESULT res; FF_DIR dir; - { - auto lock = drivers::acquire_spi(); - res = f_opendir(&dir, "/"); - } - + FRESULT res = f_opendir(&dir, "/"); if (res != FR_OK) { ESP_LOGW(kTag, "sd card ejected unsafely!"); unmountStorage(); } - { - auto lock = drivers::acquire_spi(); - f_closedir(&dir); - } + f_closedir(&dir); } }