Skip to content

Commit

Permalink
stm32: use shared fatfs glue
Browse files Browse the repository at this point in the history
  • Loading branch information
Daft-Freak committed Jul 2, 2024
1 parent 72e8289 commit 430d83e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 170 deletions.
6 changes: 5 additions & 1 deletion 32blit-stm32/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
add_subdirectory(../32blit-shared ../32blit-shared)

add_library(BlitHalSTM32 OBJECT
startup_stm32h750xx.s
Src/main.c
Expand Down Expand Up @@ -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)

Expand All @@ -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 "$<$<CONFIG:RELEASE>:-Os>")
target_compile_options(BlitHalSTM32 PUBLIC "$<$<CONFIG:Debug>:-finline-functions-called-once>") # need at least some inlining, otherwise build is too big
Expand Down
20 changes: 1 addition & 19 deletions 32blit-stm32/Inc/file.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
#pragma once

#include <cstdint>
#include <string>
#include <vector>
#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<void(blit::FileInfo &)> 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();
152 changes: 2 additions & 150 deletions 32blit-stm32/Src/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,156 +12,8 @@

extern USBManager g_usbManager;

std::vector<void *> 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<void(blit::FileInfo &)> 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/" + "/"
Expand Down

0 comments on commit 430d83e

Please sign in to comment.