2018-04-07 21:43:54 +00:00
|
|
|
#include "sd_utils.h"
|
2018-05-04 17:21:15 +00:00
|
|
|
#include "hwinit.h"
|
|
|
|
#include "sdmmc.h"
|
|
|
|
#include "lib/printk.h"
|
2018-05-04 22:11:22 +00:00
|
|
|
#include "lib/fatfs/ff.h"
|
2018-05-04 17:21:15 +00:00
|
|
|
|
|
|
|
FATFS sd_fs;
|
|
|
|
static int mounted_sd = 0;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2018-04-07 21:43:54 +00:00
|
|
|
|
2018-04-26 10:47:22 +00:00
|
|
|
size_t read_sd_file(void *dst, size_t dst_size, const char *filename) {
|
2018-05-04 17:21:15 +00:00
|
|
|
if (!mounted_sd && mount_sd() == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-05-04 22:11:22 +00:00
|
|
|
|
2018-05-04 17:21:15 +00:00
|
|
|
FIL f;
|
|
|
|
if (f_open(&f, filename, FA_READ) != FR_OK) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-05-04 22:11:22 +00:00
|
|
|
|
2018-05-04 17:21:15 +00:00
|
|
|
UINT br;
|
|
|
|
int res = f_read(&f, dst, dst_size, &br);
|
|
|
|
f_close(&f);
|
2018-05-04 22:11:22 +00:00
|
|
|
|
2018-05-04 17:21:15 +00:00
|
|
|
return res == FR_OK ? (int)br : 0;
|
2018-05-04 22:11:22 +00:00
|
|
|
}
|