Instead of passing FatFS structs around, unmount; other changes

This commit is contained in:
TuxSH
2018-05-05 17:33:49 +02:00
parent 0fca6c2296
commit 67b76cc8f1
12 changed files with 54 additions and 76 deletions

View File

@@ -1,3 +1,4 @@
#include <stdio.h>
#include "sd_utils.h"
#include "hwinit.h"
#include "sdmmc.h"
@@ -6,19 +7,14 @@
/* This is used by diskio.h. */
struct mmc sd_mmc;
FATFS sd_fs;
static int initialized_sd = 0;
static int mounted_sd = 0;
void save_sd_state(void **mmc, void **ff) {
void save_sd_state(void **mmc) {
*mmc = &sd_mmc;
*ff = &ff;
}
void resume_sd_state(void *mmc, void *ff) {
void resume_sd_state(void *mmc) {
sd_mmc = *(struct mmc *)mmc;
sd_fs = *(FATFS *)ff;
initialized_sd = 1;
mounted_sd = 1;
}
int initialize_sd(void) {
@@ -33,33 +29,13 @@ int initialize_sd(void) {
return initialized_sd;
}
int mount_sd(void) {
if (mounted_sd) {
return 1;
}
if (f_mount(&sd_fs, "", 1) == FR_OK) {
printk("Mounted SD card!\n");
mounted_sd = 1;
}
return mounted_sd;
}
size_t read_sd_file(void *dst, size_t dst_size, const char *filename) {
if (!initialized_sd && initialize_sd() == 0) {
FILE *file = fopen(filename, "rb");
if (file == NULL) {
return 0;
} else {
size_t sz = fread(dst, 1, dst_size, file);
fclose(file);
return sz;
}
if (!mounted_sd && mount_sd() == 0) {
return 0;
}
FIL f;
if (f_open(&f, filename, FA_READ) != FR_OK) {
return 0;
}
UINT br;
int res = f_read(&f, dst, dst_size, &br);
f_close(&f);
return res == FR_OK ? (int)br : 0;
}