From eb8573093d210db3098a92e4cf9532cc579fbeec Mon Sep 17 00:00:00 2001 From: TuxSH Date: Fri, 11 May 2018 22:21:44 +0200 Subject: [PATCH] Move sd init into diskio.c, write code for latest commit --- fusee/fusee-primary/src/lib/fatfs/diskio.c | 62 +++++++++++++++++++--- fusee/fusee-primary/src/sd_utils.c | 18 ------- fusee/fusee-secondary/src/sd_utils.h | 1 - 3 files changed, 56 insertions(+), 25 deletions(-) diff --git a/fusee/fusee-primary/src/lib/fatfs/diskio.c b/fusee/fusee-primary/src/lib/fatfs/diskio.c index ad9d4159b..6749da4df 100644 --- a/fusee/fusee-primary/src/lib/fatfs/diskio.c +++ b/fusee/fusee-primary/src/lib/fatfs/diskio.c @@ -7,12 +7,23 @@ /* storage control modules to the FatFs module with a defined API. */ /*-----------------------------------------------------------------------*/ +#include #include #include "diskio.h" /* FatFs lower layer API */ #include "../../sdmmc.h" +#include "../../hwinit.h" /* Global sd struct. */ -extern struct mmc sd_mmc; +struct mmc g_sd_mmc = {0}; +static bool g_sd_initialized = false; + +static bool g_ahb_redirect_enabled = false; + +/* +Uncomment if needed: +struct mmc nand_mmc = {0}; +static bool g_nand_initialized = false; +*/ /*-----------------------------------------------------------------------*/ /* Get Drive Status */ @@ -35,7 +46,28 @@ DSTATUS disk_initialize ( BYTE pdrv /* Physical drive nmuber to identify the drive */ ) { - return 0; + if (!g_ahb_redirect_enabled) { + mc_enable_ahb_redirect(); + g_ahb_redirect_enabled = true; + } + + switch (pdrv) { + case 0: { + if (!g_sd_initialized) { + int rc = sdmmc_init(&g_sd_mmc, SWITCH_MICROSD); + if (rc == 0) { + g_sd_initialized = true; + return 0; + } else { + return rc; + } + } else { + return 0; + } + } + default: + return STA_NODISK; + } } @@ -51,12 +83,21 @@ DRESULT disk_read ( UINT count /* Number of sectors to read */ ) { - for (unsigned int i = 0; i < count; i++) { - if (sdmmc_read(&sd_mmc, buff + 0x200 * i, sector + i, 1) != 0) { +#if 1 + for (unsigned int i = 0; i < count; i++) { + if (sdmmc_read(&g_sd_mmc, buff + 0x200 * i, sector + i, 1) != 0) { return RES_ERROR; } } return RES_OK; +#else + switch (pdrv) { + case 0: + return sdmmc_read(&g_sd_mmc, buff, sector, count) == 0 ? RES_OK : RES_ERROR; + default: + return RES_PARERR; + } +#endif } @@ -72,7 +113,16 @@ DRESULT disk_write ( UINT count /* Number of sectors to write */ ) { - return RES_ERROR; +#if 0 + switch (pdrv) { + case 0: + return sdmmc_write(&g_sd_mmc, buff, sector, count) == 0 ? RES_OK : RES_ERROR; + default: + return RES_PARERR; + } +#else + return 0; +#endif } @@ -87,6 +137,6 @@ DRESULT disk_ioctl ( void *buff /* Buffer to send/receive control data */ ) { - return RES_OK; + return 0; } diff --git a/fusee/fusee-primary/src/sd_utils.c b/fusee/fusee-primary/src/sd_utils.c index 51be8f64d..bbb2bb66a 100644 --- a/fusee/fusee-primary/src/sd_utils.c +++ b/fusee/fusee-primary/src/sd_utils.c @@ -4,24 +4,9 @@ #include "lib/printk.h" #include "lib/fatfs/ff.h" -/* This is used by diskio.h. */ -struct mmc sd_mmc; FATFS sd_fs; -static int initialized_sd = 0; static int mounted_sd = 0; -int initialize_sd(void) { - if (initialized_sd) { - return 1; - } - mc_enable_ahb_redirect(); - if (sdmmc_init(&sd_mmc, SWITCH_MICROSD) == 0) { - printk("Initialized SD card!\n"); - initialized_sd = 1; - } - return initialized_sd; -} - int mount_sd(void) { if (mounted_sd) { return 1; @@ -34,9 +19,6 @@ int mount_sd(void) { } size_t read_sd_file(void *dst, size_t dst_size, const char *filename) { - if (!initialized_sd && initialize_sd() == 0) { - return 0; - } if (!mounted_sd && mount_sd() == 0) { return 0; } diff --git a/fusee/fusee-secondary/src/sd_utils.h b/fusee/fusee-secondary/src/sd_utils.h index 3cda831e7..f796e5aa9 100644 --- a/fusee/fusee-secondary/src/sd_utils.h +++ b/fusee/fusee-secondary/src/sd_utils.h @@ -4,7 +4,6 @@ #include "utils.h" #include "sdmmc.h" -int initialize_sd(void); size_t read_sd_file(void *dst, size_t dst_size, const char *filename); #endif