Move sd init into diskio.c, write code for latest commit

This commit is contained in:
TuxSH 2018-05-11 22:21:44 +02:00
parent 2b871397c2
commit eb8573093d
3 changed files with 56 additions and 25 deletions

View file

@ -7,12 +7,23 @@
/* storage control modules to the FatFs module with a defined API. */
/*-----------------------------------------------------------------------*/
#include <stdbool.h>
#include <string.h>
#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 */
)
{
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 */
)
{
#if 1
for (unsigned int i = 0; i < count; i++) {
if (sdmmc_read(&sd_mmc, buff + 0x200 * i, sector + i, 1) != 0) {
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;
}

View file

@ -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;
}

View file

@ -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