Atmosphere/fusee/fusee-primary/src/fs_utils.c

120 lines
2.6 KiB
C
Raw Normal View History

2018-05-13 17:49:50 -04:00
#include "fs_utils.h"
#include "mc.h"
2018-05-04 13:21:15 -04:00
#include "lib/printk.h"
2018-05-04 18:11:22 -04:00
#include "lib/fatfs/ff.h"
2018-05-04 13:21:15 -04:00
FATFS sd_fs;
static bool g_sd_mounted = false;
static bool g_sd_initialized = false;
static bool g_ahb_redirect_enabled = false;
2018-05-04 13:21:15 -04:00
bool mount_sd(void)
{
/* Already mounted. */
if (g_sd_mounted)
return true;
/* Enable AHB redirection if necessary. */
if (!g_ahb_redirect_enabled) {
mc_enable_ahb_redirect();
g_ahb_redirect_enabled = true;
2018-05-04 13:21:15 -04:00
}
if (!g_sd_initialized) {
/* Initialize SD. */
if (sdmmc_device_sd_init(&g_sd_device, &g_sd_sdmmc, SDMMC_BUS_WIDTH_4BIT, SDMMC_SPEED_SDR104))
{
g_sd_initialized = true;
/* Mount SD. */
if (f_mount(&sd_fs, "", 1) == FR_OK) {
printk("Mounted SD card!\n");
g_sd_mounted = true;
}
}
else
fatal_error("Failed to initialize the SD card!.\n");
2018-05-04 13:21:15 -04:00
}
return g_sd_mounted;
2018-05-04 13:21:15 -04:00
}
2018-04-07 17:43:54 -04:00
void unmount_sd(void)
{
if (g_sd_mounted)
{
f_mount(NULL, "", 1);
sdmmc_device_finish(&g_sd_device);
g_sd_mounted = false;
2018-05-04 13:21:15 -04:00
}
/* Disable AHB redirection if necessary. */
if (g_ahb_redirect_enabled) {
mc_disable_ahb_redirect();
g_ahb_redirect_enabled = false;
}
}
uint32_t get_file_size(const char *filename)
{
/* SD card hasn't been mounted yet. */
if (!g_sd_mounted)
return 0;
2018-05-04 18:11:22 -04:00
/* Open the file for reading. */
2018-05-04 13:21:15 -04:00
FIL f;
if (f_open(&f, filename, FA_READ) != FR_OK)
return 0;
/* Get the file size. */
uint32_t file_size = f_size(&f);
/* Close the file. */
f_close(&f);
return file_size;
}
int read_from_file(void *dst, uint32_t dst_size, const char *filename)
{
/* SD card hasn't been mounted yet. */
if (!g_sd_mounted)
2018-05-04 13:21:15 -04:00
return 0;
2018-05-04 18:11:22 -04:00
/* Open the file for reading. */
FIL f;
if (f_open(&f, filename, FA_READ) != FR_OK)
return 0;
/* Sync. */
f_sync(&f);
/* Read from file. */
UINT br = 0;
2018-05-04 13:21:15 -04:00
int res = f_read(&f, dst, dst_size, &br);
f_close(&f);
2018-05-04 18:11:22 -04:00
return (res == FR_OK) ? (int)br : 0;
2018-05-04 18:11:22 -04:00
}
int write_to_file(void *src, uint32_t src_size, const char *filename)
{
/* SD card hasn't been mounted yet. */
if (!g_sd_mounted)
return 0;
/* Open the file for writing. */
FIL f;
if (f_open(&f, filename, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
return 0;
/* Sync. */
f_sync(&f);
/* Write to file. */
UINT bw = 0;
int res = f_write(&f, src, src_size, &bw);
f_close(&f);
return (res == FR_OK) ? (int)bw : 0;
}