From 45218a381660f9aaed15d008172a9f19ea9e889b Mon Sep 17 00:00:00 2001 From: Charlie Birks Date: Mon, 1 Jul 2024 16:55:31 +0100 Subject: [PATCH 1/8] stm32: remove disabling warning for deleted fatfs copy --- 32blit-stm32/CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/32blit-stm32/CMakeLists.txt b/32blit-stm32/CMakeLists.txt index 7ef90fb32..a288d5d92 100644 --- a/32blit-stm32/CMakeLists.txt +++ b/32blit-stm32/CMakeLists.txt @@ -146,8 +146,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} From 6e976e82e00cdb10903280df3bb77ff0cd76f6d9 Mon Sep 17 00:00:00 2001 From: Charlie Birks Date: Mon, 1 Jul 2024 16:24:45 +0100 Subject: [PATCH 2/8] Make fatfs a library instead of duplicating the sources and includes --- 32blit-config.cmake | 1 + 32blit-pico/CMakeLists.txt | 12 +++++++----- 32blit-stm32/CMakeLists.txt | 5 ++--- 3rd-party/CMakeLists.txt | 7 +++++++ 4 files changed, 17 insertions(+), 8 deletions(-) create mode 100644 3rd-party/CMakeLists.txt 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..dffd76cbe 100644 --- a/32blit-pico/CMakeLists.txt +++ b/32blit-pico/CMakeLists.txt @@ -31,9 +31,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 +40,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 + FatFs +) + 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-stm32/CMakeLists.txt b/32blit-stm32/CMakeLists.txt index a288d5d92..e3ab60e98 100644 --- a/32blit-stm32/CMakeLists.txt +++ b/32blit-stm32/CMakeLists.txt @@ -39,9 +39,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 @@ -157,6 +154,8 @@ target_compile_definitions(BlitHalSTM32 -DCDC_FIFO_BUFFERS=${CDC_FIFO_BUFFERS} ) +target_link_libraries(BlitHalSTM32 FatFs) + 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/3rd-party/CMakeLists.txt b/3rd-party/CMakeLists.txt new file mode 100644 index 000000000..c73cf9e79 --- /dev/null +++ b/3rd-party/CMakeLists.txt @@ -0,0 +1,7 @@ +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) From 1509a5f3cd6ba81b7656dc34829ff76f32d5a47c Mon Sep 17 00:00:00 2001 From: Charlie Birks Date: Mon, 1 Jul 2024 16:50:42 +0100 Subject: [PATCH 3/8] pico: use PUBLIC for pico_cxx_options linking ... so I can use PRIVATE somewhere else... --- 32blit-pico/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/32blit-pico/CMakeLists.txt b/32blit-pico/CMakeLists.txt index dffd76cbe..4fca0626d 100644 --- a/32blit-pico/CMakeLists.txt +++ b/32blit-pico/CMakeLists.txt @@ -13,7 +13,7 @@ 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) From 3a3919efb15e8aff8186e8d66ffa30dcafeed3d3 Mon Sep 17 00:00:00 2001 From: Charlie Birks Date: Mon, 1 Jul 2024 16:51:14 +0100 Subject: [PATCH 4/8] Make minimp3 an interface library too --- 32blit/CMakeLists.txt | 5 +---- 3rd-party/CMakeLists.txt | 3 +++ 2 files changed, 4 insertions(+), 4 deletions(-) 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 index c73cf9e79..f55e4f651 100644 --- a/3rd-party/CMakeLists.txt +++ b/3rd-party/CMakeLists.txt @@ -5,3 +5,6 @@ target_sources(FatFs INTERFACE ${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 From 8068233ceae3554f4bf4f0a3d7e33cf8d4bac5e6 Mon Sep 17 00:00:00 2001 From: Charlie Birks Date: Tue, 2 Jul 2024 12:57:07 +0100 Subject: [PATCH 5/8] Split a copy of (most of) 32blit-pico's fatfs glue to a library --- 32blit-shared/CMakeLists.txt | 1 + 32blit-shared/fatfs-blit-api/CMakeLists.txt | 4 + .../fatfs-blit-api/fatfs_blit_api.cpp | 151 ++++++++++++++++++ .../fatfs-blit-api/fatfs_blit_api.hpp | 23 +++ 4 files changed, 179 insertions(+) create mode 100644 32blit-shared/CMakeLists.txt create mode 100644 32blit-shared/fatfs-blit-api/CMakeLists.txt create mode 100644 32blit-shared/fatfs-blit-api/fatfs_blit_api.cpp create mode 100644 32blit-shared/fatfs-blit-api/fatfs_blit_api.hpp 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..31662ee15 --- /dev/null +++ b/32blit-shared/fatfs-blit-api/fatfs_blit_api.cpp @@ -0,0 +1,151 @@ + +#include "ff.h" +#include "diskio.h" + +#include "fatfs_blit_api.hpp" + +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) { + 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; +} \ 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); From 6d239bd0efcc7ed780c35edfd397f379ea24e526 Mon Sep 17 00:00:00 2001 From: Charlie Birks Date: Tue, 2 Jul 2024 12:58:24 +0100 Subject: [PATCH 6/8] pico: use shared fatfs glue --- 32blit-pico/CMakeLists.txt | 4 +- 32blit-pico/file.cpp | 146 ------------------------------------- 32blit-pico/file.hpp | 21 +----- 3 files changed, 4 insertions(+), 167 deletions(-) diff --git a/32blit-pico/CMakeLists.txt b/32blit-pico/CMakeLists.txt index 4fca0626d..6df715114 100644 --- a/32blit-pico/CMakeLists.txt +++ b/32blit-pico/CMakeLists.txt @@ -18,6 +18,8 @@ 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) @@ -44,7 +46,7 @@ target_link_libraries(BlitHalPico INTERFACE hardware_dma hardware_pio hardware_pwm hardware_spi pico_multicore pico_stdlib pico_unique_id pico_rand tinyusb_device - FatFs + FatFsBlitAPI ) target_include_directories(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(); From 72e8289f5e4ec3ecff4a1177e1a387fdcd74f0e8 Mon Sep 17 00:00:00 2001 From: Charlie Birks Date: Tue, 2 Jul 2024 16:10:21 +0100 Subject: [PATCH 7/8] shared: add a weak function to check if filesystem access is disabled This is the only difference between the -pico and -stm32 code --- 32blit-shared/fatfs-blit-api/fatfs_blit_api.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/32blit-shared/fatfs-blit-api/fatfs_blit_api.cpp b/32blit-shared/fatfs-blit-api/fatfs_blit_api.cpp index 31662ee15..98cb4b513 100644 --- a/32blit-shared/fatfs-blit-api/fatfs_blit_api.cpp +++ b/32blit-shared/fatfs-blit-api/fatfs_blit_api.cpp @@ -6,6 +6,11 @@ std::vector open_files; +[[gnu::weak]] +bool is_filesystem_access_disabled() { + return false; +} + bool get_files_open() { return open_files.size() > 0; } @@ -16,6 +21,9 @@ void close_open_files() { } void *open_file(const std::string &file, int mode) { + if(is_filesystem_access_disabled()) + return nullptr; + FIL *f = new FIL(); BYTE ff_mode = 0; @@ -97,6 +105,9 @@ uint32_t get_file_length(void *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) From 430d83e98a7a16647abbdfa649b90480006756cc Mon Sep 17 00:00:00 2001 From: Charlie Birks Date: Tue, 2 Jul 2024 16:11:24 +0100 Subject: [PATCH 8/8] stm32: use shared fatfs glue --- 32blit-stm32/CMakeLists.txt | 6 +- 32blit-stm32/Inc/file.hpp | 20 +---- 32blit-stm32/Src/file.cpp | 152 +----------------------------------- 3 files changed, 8 insertions(+), 170 deletions(-) diff --git a/32blit-stm32/CMakeLists.txt b/32blit-stm32/CMakeLists.txt index e3ab60e98..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 @@ -131,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) @@ -154,7 +158,7 @@ target_compile_definitions(BlitHalSTM32 -DCDC_FIFO_BUFFERS=${CDC_FIFO_BUFFERS} ) -target_link_libraries(BlitHalSTM32 FatFs) +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 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/" + "/"