diff --git a/32blit-config.cmake b/32blit-config.cmake index e152f7e31..18bfb9402 100644 --- a/32blit-config.cmake +++ b/32blit-config.cmake @@ -40,6 +40,7 @@ if (NOT DEFINED BLIT_ONCE) add_definitions("-DWIN32") endif() + add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/3rd-party 3rd-party) add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/32blit 32blit) function (blit_assets_yaml TARGET FILE) diff --git a/32blit-pico/CMakeLists.txt b/32blit-pico/CMakeLists.txt index 92c93ad6d..6df715114 100644 --- a/32blit-pico/CMakeLists.txt +++ b/32blit-pico/CMakeLists.txt @@ -13,11 +13,13 @@ set(PICO_SDK_VERSION_MINOR ${PICO_SDK_VERSION_MINOR} PARENT_SCOPE) set(PICO_SDK_VERSION_REVISION ${PICO_SDK_VERSION_REVISION} PARENT_SCOPE) # make sure BlitEngine is built with the right exception flags -target_link_libraries(BlitEngine pico_cxx_options) +target_link_libraries(BlitEngine PUBLIC pico_cxx_options) # also enable function/data sectons target_compile_options(BlitEngine PRIVATE -ffunction-sections -fdata-sections) +add_subdirectory(../32blit-shared ../32blit-shared) + # driver helper # can override driver choice by pre-setting BLIT_x_DRIVER function(blit_driver DRV NAME) @@ -31,9 +33,6 @@ endfunction() add_library(BlitHalPico INTERFACE) target_sources(BlitHalPico INTERFACE - ${CMAKE_CURRENT_LIST_DIR}/../3rd-party/fatfs/ff.c - ${CMAKE_CURRENT_LIST_DIR}/../3rd-party/fatfs/ffunicode.c - ${CMAKE_CURRENT_LIST_DIR}/display.cpp ${CMAKE_CURRENT_LIST_DIR}/file.cpp ${CMAKE_CURRENT_LIST_DIR}/led.cpp @@ -43,10 +42,15 @@ target_sources(BlitHalPico INTERFACE ${CMAKE_CURRENT_LIST_DIR}/usb_descriptors.c ) -target_link_libraries(BlitHalPico INTERFACE hardware_dma hardware_pio hardware_pwm hardware_spi pico_multicore pico_stdlib pico_unique_id pico_rand tinyusb_device) +target_link_libraries(BlitHalPico INTERFACE + hardware_dma hardware_pio hardware_pwm hardware_spi + pico_multicore pico_stdlib pico_unique_id pico_rand + tinyusb_device + FatFsBlitAPI +) + target_include_directories(BlitHalPico INTERFACE ${CMAKE_CURRENT_LIST_DIR} # for tusb_config - ${CMAKE_CURRENT_LIST_DIR}/../3rd-party/fatfs ) target_compile_definitions(BlitHalPico INTERFACE diff --git a/32blit-pico/file.cpp b/32blit-pico/file.cpp index 672e62294..ba43d01c2 100644 --- a/32blit-pico/file.cpp +++ b/32blit-pico/file.cpp @@ -14,8 +14,6 @@ static FATFS fs; static bool initialised = false; -std::vector open_files; - // fatfs io funcs DSTATUS disk_initialize(BYTE pdrv) { initialised = storage_init(); @@ -81,150 +79,6 @@ void init_fs() { printf("Failed to mount filesystem! (%i)\n", res); } -bool get_files_open() { - return open_files.size() > 0; -} - -void close_open_files() { - while(!open_files.empty()) - close_file(open_files.back()); -} - -void *open_file(const std::string &file, int mode) { - FIL *f = new FIL(); - - BYTE ff_mode = 0; - - if(mode & blit::OpenMode::read) - ff_mode |= FA_READ; - - if(mode & blit::OpenMode::write) - ff_mode |= FA_WRITE; - - if(mode == blit::OpenMode::write) - ff_mode |= FA_CREATE_ALWAYS; - - FRESULT r = f_open(f, file.c_str(), ff_mode); - - if(r == FR_OK) { - open_files.push_back(f); - return f; - } - - delete f; - return nullptr; -} - -int32_t read_file(void *fh, uint32_t offset, uint32_t length, char *buffer) { - FRESULT r = FR_OK; - FIL *f = (FIL *)fh; - - if(offset != f_tell(f)) - r = f_lseek(f, offset); - - if(r == FR_OK){ - unsigned int bytes_read; - r = f_read(f, buffer, length, &bytes_read); - if(r == FR_OK){ - return bytes_read; - } - } - - return -1; -} - -int32_t write_file(void *fh, uint32_t offset, uint32_t length, const char *buffer) { - FRESULT r = FR_OK; - FIL *f = (FIL *)fh; - - if(offset != f_tell(f)) - r = f_lseek(f, offset); - - if(r == FR_OK) { - unsigned int bytes_written; - r = f_write(f, buffer, length, &bytes_written); - if(r == FR_OK) { - return bytes_written; - } - } - - return -1; -} - -int32_t close_file(void *fh) { - FRESULT r; - - r = f_close((FIL *)fh); - - for(auto it = open_files.begin(); it != open_files.end(); ++it) { - if(*it == fh) { - open_files.erase(it); - break; - } - } - - delete (FIL *)fh; - return r == FR_OK ? 0 : -1; -} - -uint32_t get_file_length(void *fh) { - return f_size((FIL *)fh); -} - -void list_files(const std::string &path, std::function callback) { - DIR dir; - - if(f_opendir(&dir, path.c_str()) != FR_OK) - return; - - FILINFO ent; - - while(f_readdir(&dir, &ent) == FR_OK && ent.fname[0]) { - blit::FileInfo info; - - info.name = ent.fname; - info.flags = 0; - info.size = ent.fsize; - - if(ent.fattrib & AM_DIR) - info.flags |= blit::FileFlags::directory; - - callback(info); - } - - f_closedir(&dir); -} - -bool file_exists(const std::string &path) { - FILINFO info; - return f_stat(path.c_str(), &info) == FR_OK && !(info.fattrib & AM_DIR); -} - -bool directory_exists(const std::string &path) { - FILINFO info; - return f_stat(path.c_str(), &info) == FR_OK && (info.fattrib & AM_DIR); -} - -bool create_directory(const std::string &path) { - FRESULT r; - - // strip trailing slash - if(path.back() == '/') - r = f_mkdir(path.substr(0, path.length() - 1).c_str()); - else - r = f_mkdir(path.c_str()); - - return r == FR_OK || r == FR_EXIST; -} - -bool rename_file(const std::string &old_name, const std::string &new_name) { - return f_rename(old_name.c_str(), new_name.c_str()) == FR_OK; -} - -bool remove_file(const std::string &path) { - return f_unlink(path.c_str()) == FR_OK; -} - static char save_path[32]; // max game title length is 24 + ".blit/" + "/" const char *get_save_path() { diff --git a/32blit-pico/file.hpp b/32blit-pico/file.hpp index 6964ee212..a9c585475 100644 --- a/32blit-pico/file.hpp +++ b/32blit-pico/file.hpp @@ -1,26 +1,7 @@ #pragma once -#include -#include -#include -#include - -#include "engine/file.hpp" +#include "fatfs_blit_api.hpp" void init_fs(); -bool get_files_open(); -void close_open_files(); - -void *open_file(const std::string &file, int mode); -int32_t read_file(void *fh, uint32_t offset, uint32_t length, char *buffer); -int32_t write_file(void *fh, uint32_t offset, uint32_t length, const char *buffer); -int32_t close_file(void *fh); -uint32_t get_file_length(void *fh); -void list_files(const std::string &path, std::function callback); -bool file_exists(const std::string &path); -bool directory_exists(const std::string &path); -bool create_directory(const std::string &path); -bool rename_file(const std::string &old_name, const std::string &new_name); -bool remove_file(const std::string &path); const char *get_save_path(); diff --git a/32blit-shared/CMakeLists.txt b/32blit-shared/CMakeLists.txt new file mode 100644 index 000000000..1acbf61aa --- /dev/null +++ b/32blit-shared/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(fatfs-blit-api) \ No newline at end of file diff --git a/32blit-shared/fatfs-blit-api/CMakeLists.txt b/32blit-shared/fatfs-blit-api/CMakeLists.txt new file mode 100644 index 000000000..6688cab44 --- /dev/null +++ b/32blit-shared/fatfs-blit-api/CMakeLists.txt @@ -0,0 +1,4 @@ +add_library(FatFsBlitAPI INTERFACE) +target_sources(FatFsBlitAPI INTERFACE ${CMAKE_CURRENT_LIST_DIR}/fatfs_blit_api.cpp) +target_include_directories(FatFsBlitAPI INTERFACE ${CMAKE_CURRENT_LIST_DIR}) +target_link_libraries(FatFsBlitAPI INTERFACE FatFs) \ No newline at end of file diff --git a/32blit-shared/fatfs-blit-api/fatfs_blit_api.cpp b/32blit-shared/fatfs-blit-api/fatfs_blit_api.cpp new file mode 100644 index 000000000..98cb4b513 --- /dev/null +++ b/32blit-shared/fatfs-blit-api/fatfs_blit_api.cpp @@ -0,0 +1,162 @@ + +#include "ff.h" +#include "diskio.h" + +#include "fatfs_blit_api.hpp" + +std::vector open_files; + +[[gnu::weak]] +bool is_filesystem_access_disabled() { + return false; +} + +bool get_files_open() { + return open_files.size() > 0; +} + +void close_open_files() { + while(!open_files.empty()) + close_file(open_files.back()); +} + +void *open_file(const std::string &file, int mode) { + if(is_filesystem_access_disabled()) + return nullptr; + + FIL *f = new FIL(); + + BYTE ff_mode = 0; + + if(mode & blit::OpenMode::read) + ff_mode |= FA_READ; + + if(mode & blit::OpenMode::write) + ff_mode |= FA_WRITE; + + if(mode == blit::OpenMode::write) + ff_mode |= FA_CREATE_ALWAYS; + + FRESULT r = f_open(f, file.c_str(), ff_mode); + + if(r == FR_OK) { + open_files.push_back(f); + return f; + } + + delete f; + return nullptr; +} + +int32_t read_file(void *fh, uint32_t offset, uint32_t length, char *buffer) { + FRESULT r = FR_OK; + FIL *f = (FIL *)fh; + + if(offset != f_tell(f)) + r = f_lseek(f, offset); + + if(r == FR_OK){ + unsigned int bytes_read; + r = f_read(f, buffer, length, &bytes_read); + if(r == FR_OK){ + return bytes_read; + } + } + + return -1; +} + +int32_t write_file(void *fh, uint32_t offset, uint32_t length, const char *buffer) { + FRESULT r = FR_OK; + FIL *f = (FIL *)fh; + + if(offset != f_tell(f)) + r = f_lseek(f, offset); + + if(r == FR_OK) { + unsigned int bytes_written; + r = f_write(f, buffer, length, &bytes_written); + if(r == FR_OK) { + return bytes_written; + } + } + + return -1; +} + +int32_t close_file(void *fh) { + FRESULT r; + + r = f_close((FIL *)fh); + + for(auto it = open_files.begin(); it != open_files.end(); ++it) { + if(*it == fh) { + open_files.erase(it); + break; + } + } + + delete (FIL *)fh; + return r == FR_OK ? 0 : -1; +} + +uint32_t get_file_length(void *fh) { + return f_size((FIL *)fh); +} + +void list_files(const std::string &path, std::function callback) { + if(is_filesystem_access_disabled()) + return; + + DIR dir; + + if(f_opendir(&dir, path.c_str()) != FR_OK) + return; + + FILINFO ent; + + while(f_readdir(&dir, &ent) == FR_OK && ent.fname[0]) { + blit::FileInfo info; + + info.name = ent.fname; + info.flags = 0; + info.size = ent.fsize; + + if(ent.fattrib & AM_DIR) + info.flags |= blit::FileFlags::directory; + + callback(info); + } + + f_closedir(&dir); +} + +bool file_exists(const std::string &path) { + FILINFO info; + return f_stat(path.c_str(), &info) == FR_OK && !(info.fattrib & AM_DIR); +} + +bool directory_exists(const std::string &path) { + FILINFO info; + return f_stat(path.c_str(), &info) == FR_OK && (info.fattrib & AM_DIR); +} + +bool create_directory(const std::string &path) { + FRESULT r; + + // strip trailing slash + if(path.back() == '/') + r = f_mkdir(path.substr(0, path.length() - 1).c_str()); + else + r = f_mkdir(path.c_str()); + + return r == FR_OK || r == FR_EXIST; +} + +bool rename_file(const std::string &old_name, const std::string &new_name) { + return f_rename(old_name.c_str(), new_name.c_str()) == FR_OK; +} + +bool remove_file(const std::string &path) { + return f_unlink(path.c_str()) == FR_OK; +} \ No newline at end of file diff --git a/32blit-shared/fatfs-blit-api/fatfs_blit_api.hpp b/32blit-shared/fatfs-blit-api/fatfs_blit_api.hpp new file mode 100644 index 000000000..b72058624 --- /dev/null +++ b/32blit-shared/fatfs-blit-api/fatfs_blit_api.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include +#include +#include +#include + +#include "engine/file.hpp" + +bool get_files_open(); +void close_open_files(); + +void *open_file(const std::string &file, int mode); +int32_t read_file(void *fh, uint32_t offset, uint32_t length, char *buffer); +int32_t write_file(void *fh, uint32_t offset, uint32_t length, const char *buffer); +int32_t close_file(void *fh); +uint32_t get_file_length(void *fh); +void list_files(const std::string &path, std::function callback); +bool file_exists(const std::string &path); +bool directory_exists(const std::string &path); +bool create_directory(const std::string &path); +bool rename_file(const std::string &old_name, const std::string &new_name); +bool remove_file(const std::string &path); diff --git a/32blit-stm32/CMakeLists.txt b/32blit-stm32/CMakeLists.txt index 7ef90fb32..db87ea987 100644 --- a/32blit-stm32/CMakeLists.txt +++ b/32blit-stm32/CMakeLists.txt @@ -1,3 +1,5 @@ +add_subdirectory(../32blit-shared ../32blit-shared) + add_library(BlitHalSTM32 OBJECT startup_stm32h750xx.s Src/main.c @@ -39,9 +41,6 @@ add_library(BlitHalSTM32 OBJECT Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_jpeg.c Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_hcd.c Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_ll_usb.c - ../3rd-party/fatfs/ff.c - ../3rd-party/fatfs/ffsystem.c - ../3rd-party/fatfs/ffunicode.c Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_core.c Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ctlreq.c Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ioreq.c @@ -134,7 +133,9 @@ set(INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/Utilities ${CMAKE_CURRENT_SOURCE_DIR}/../32blit ${CMAKE_CURRENT_SOURCE_DIR}/../launcher-shared + # these are because the firmware doesn't inherit include paths ${CMAKE_CURRENT_SOURCE_DIR}/../3rd-party/fatfs + ${CMAKE_CURRENT_SOURCE_DIR}/../32blit-shared/fatfs-blit-api ) set(HAL_INCLUDE_DIRS ${INCLUDE_DIRS} PARENT_SCOPE) @@ -146,8 +147,6 @@ set(DEFINITIONS ) set(HAL_DEFINITIONS ${DEFINITIONS} PARENT_SCOPE) -set_source_files_properties(Middlewares/Third_Party/FatFs/src/ff.c PROPERTIES COMPILE_FLAGS "-Wno-misleading-indentation") - target_include_directories(BlitHalSTM32 PRIVATE ${INCLUDE_DIRS} @@ -159,6 +158,8 @@ target_compile_definitions(BlitHalSTM32 -DCDC_FIFO_BUFFERS=${CDC_FIFO_BUFFERS} ) +target_link_libraries(BlitHalSTM32 FatFsBlitAPI) + target_compile_options(BlitHalSTM32 PUBLIC "$<$:-Os>") target_compile_options(BlitHalSTM32 PUBLIC "$<$:-finline-functions-called-once>") # need at least some inlining, otherwise build is too big target_compile_options(BlitHalSTM32 PRIVATE -Wno-missing-field-initializers) diff --git a/32blit-stm32/Inc/file.hpp b/32blit-stm32/Inc/file.hpp index 7205b1ca2..513797a74 100644 --- a/32blit-stm32/Inc/file.hpp +++ b/32blit-stm32/Inc/file.hpp @@ -1,23 +1,5 @@ #pragma once -#include -#include -#include +#include "fatfs_blit_api.hpp" -#include "engine/file.hpp" - -bool get_files_open(); -void close_open_files(); - -void *open_file(const std::string &file, int mode); -int32_t read_file(void *fh, uint32_t offset, uint32_t length, char *buffer); -int32_t write_file(void *fh, uint32_t offset, uint32_t length, const char *buffer); -int32_t close_file(void *fh); -uint32_t get_file_length(void *fh); -void list_files(const std::string &path, std::function callback); -bool file_exists(const std::string &path); -bool directory_exists(const std::string &path); -bool create_directory(const std::string &path); -bool rename_file(const std::string &old_name, const std::string &new_name); -bool remove_file(const std::string &path); const char *get_save_path(); diff --git a/32blit-stm32/Src/file.cpp b/32blit-stm32/Src/file.cpp index 98ae55746..19fc3c31a 100644 --- a/32blit-stm32/Src/file.cpp +++ b/32blit-stm32/Src/file.cpp @@ -12,156 +12,8 @@ extern USBManager g_usbManager; -std::vector open_files; - -bool get_files_open() { - return open_files.size() > 0; -} - -void close_open_files() { - while(!open_files.empty()) - close_file(open_files.back()); -} - -void *open_file(const std::string &file, int mode) { - if(g_usbManager.GetType() == USBManager::usbtMSC) - return nullptr; - - FIL *f = new FIL(); - - BYTE ff_mode = 0; - - if(mode & blit::OpenMode::read) - ff_mode |= FA_READ; - - if(mode & blit::OpenMode::write) - ff_mode |= FA_WRITE; - - if(mode == blit::OpenMode::write) - ff_mode |= FA_CREATE_ALWAYS; - - FRESULT r = f_open(f, file.c_str(), ff_mode); - - if(r == FR_OK) { - open_files.push_back(f); - return f; - } - - delete f; - return nullptr; -} - -int32_t read_file(void *fh, uint32_t offset, uint32_t length, char *buffer) { - FRESULT r = FR_OK; - FIL *f = (FIL *)fh; - - if(offset != f_tell(f)) - r = f_lseek(f, offset); - - if(r == FR_OK){ - unsigned int bytes_read; - r = f_read(f, buffer, length, &bytes_read); - if(r == FR_OK){ - return bytes_read; - } - } - - return -1; -} - -int32_t write_file(void *fh, uint32_t offset, uint32_t length, const char *buffer) { - FRESULT r = FR_OK; - FIL *f = (FIL *)fh; - - if(offset != f_tell(f)) - r = f_lseek(f, offset); - - if(r == FR_OK) { - unsigned int bytes_written; - r = f_write(f, buffer, length, &bytes_written); - if(r == FR_OK) { - return bytes_written; - } - } - - return -1; -} - -int32_t close_file(void *fh) { - FRESULT r; - - r = f_close((FIL *)fh); - - for(auto it = open_files.begin(); it != open_files.end(); ++it) { - if(*it == fh) { - open_files.erase(it); - break; - } - } - - delete (FIL *)fh; - return r == FR_OK ? 0 : -1; -} - -uint32_t get_file_length(void *fh) { - return f_size((FIL *)fh); -} - -void list_files(const std::string &path, std::function callback) { - if(g_usbManager.GetType() == USBManager::usbtMSC) - return; - - DIR dir; - - if(f_opendir(&dir, path.c_str()) != FR_OK) - return; - - FILINFO ent; - - while(f_readdir(&dir, &ent) == FR_OK && ent.fname[0]) { - blit::FileInfo info; - - info.name = ent.fname; - info.flags = 0; - info.size = ent.fsize; - - if(ent.fattrib & AM_DIR) - info.flags |= blit::FileFlags::directory; - - callback(info); - } - - f_closedir(&dir); -} - -bool file_exists(const std::string &path) { - FILINFO info; - return f_stat(path.c_str(), &info) == FR_OK && !(info.fattrib & AM_DIR); -} - -bool directory_exists(const std::string &path) { - FILINFO info; - return f_stat(path.c_str(), &info) == FR_OK && (info.fattrib & AM_DIR); -} - -bool create_directory(const std::string &path) { - FRESULT r; - - // strip trailing slash - if(path.back() == '/') - r = f_mkdir(path.substr(0, path.length() - 1).c_str()); - else - r = f_mkdir(path.c_str()); - - return r == FR_OK || r == FR_EXIST; -} - -bool rename_file(const std::string &old_name, const std::string &new_name) { - return f_rename(old_name.c_str(), new_name.c_str()) == FR_OK; -} - -bool remove_file(const std::string &path) { - return f_unlink(path.c_str()) == FR_OK; +bool is_filesystem_access_disabled() { + return g_usbManager.GetType() == USBManager::usbtMSC; } static char save_path[32]; // max game title length is 24 + ".blit/" + "/" diff --git a/32blit/CMakeLists.txt b/32blit/CMakeLists.txt index d69bf2041..1df582665 100644 --- a/32blit/CMakeLists.txt +++ b/32blit/CMakeLists.txt @@ -41,10 +41,7 @@ target_include_directories(BlitEngine PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ) -get_filename_component(3RD_PARTY_DIR ../3rd-party ABSOLUTE) -target_include_directories(BlitEngine - PRIVATE ${3RD_PARTY_DIR} -) +target_link_libraries(BlitEngine LINK_PRIVATE MiniMP3) # version variables string(TIMESTAMP BUILD_DATE "%Y-%m-%d" UTC) diff --git a/3rd-party/CMakeLists.txt b/3rd-party/CMakeLists.txt new file mode 100644 index 000000000..f55e4f651 --- /dev/null +++ b/3rd-party/CMakeLists.txt @@ -0,0 +1,10 @@ +add_library(FatFs INTERFACE) +target_sources(FatFs INTERFACE + ${CMAKE_CURRENT_LIST_DIR}/fatfs/ff.c + ${CMAKE_CURRENT_LIST_DIR}/fatfs/ffsystem.c + ${CMAKE_CURRENT_LIST_DIR}/fatfs/ffunicode.c +) +target_include_directories(FatFs INTERFACE ${CMAKE_CURRENT_LIST_DIR}/fatfs) + +add_library(MiniMP3 INTERFACE) +target_include_directories(MiniMP3 INTERFACE ${CMAKE_CURRENT_LIST_DIR}) \ No newline at end of file