88 lines
2.2 KiB
C
88 lines
2.2 KiB
C
/*
|
|
* Copyright 2020 NXP
|
|
* All rights reserved.
|
|
*
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*/
|
|
|
|
#include "fsl_common.h"
|
|
#include "ff.h"
|
|
#include "diskio.h"
|
|
#include "fsl_sd_disk.h"
|
|
#include "sdmmc_config.h"
|
|
#include "fsl_debug_console.h"
|
|
|
|
/*******************************************************************************
|
|
* Prototypes
|
|
******************************************************************************/
|
|
static status_t sdcardWaitCardInsert(void);
|
|
|
|
/*******************************************************************************
|
|
* Variables
|
|
******************************************************************************/
|
|
AT_NONCACHEABLE_SECTION(static FATFS g_fileSystem); /* File system object */
|
|
|
|
/*******************************************************************************
|
|
* Code
|
|
******************************************************************************/
|
|
int MOUNT_SDCard(void)
|
|
{
|
|
const TCHAR driverName[3U] = {SDDISK + '0', ':', '/'};
|
|
|
|
// clear FATFS manually
|
|
memset((void *)&g_fileSystem, 0, sizeof(g_fileSystem));
|
|
|
|
/* Wait for the card insert. */
|
|
if (sdcardWaitCardInsert() != kStatus_Success)
|
|
{
|
|
PRINTF("Card not inserted.\r\n");
|
|
return -1;
|
|
}
|
|
|
|
// Mount the driver
|
|
if (f_mount(&g_fileSystem, driverName, 1))
|
|
{
|
|
PRINTF("Mount volume failed.\r\n");
|
|
return -2;
|
|
}
|
|
|
|
#if (FF_FS_RPATH >= 2U)
|
|
if (f_chdrive((char const *)&driverName[0U]))
|
|
{
|
|
PRINTF("Change drive failed.\r\n");
|
|
return -3;
|
|
}
|
|
#endif
|
|
|
|
return 0;
|
|
}
|
|
|
|
static status_t sdcardWaitCardInsert(void)
|
|
{
|
|
BOARD_SD_Config(&g_sd, NULL, BOARD_SDMMC_SD_HOST_IRQ_PRIORITY, NULL);
|
|
|
|
/* SD host init function */
|
|
if (SD_HostInit(&g_sd) != kStatus_Success)
|
|
{
|
|
PRINTF("\r\nSD host init fail\r\n");
|
|
return kStatus_Fail;
|
|
}
|
|
|
|
/* wait card insert */
|
|
if (SD_PollingCardInsert(&g_sd, kSD_Inserted) == kStatus_Success)
|
|
{
|
|
PRINTF("\r\nCard inserted.\r\n");
|
|
/* power off card */
|
|
SD_SetCardPower(&g_sd, false);
|
|
/* power on the card */
|
|
SD_SetCardPower(&g_sd, true);
|
|
}
|
|
else
|
|
{
|
|
PRINTF("\r\nCard detect fail.\r\n");
|
|
return kStatus_Fail;
|
|
}
|
|
|
|
return kStatus_Success;
|
|
}
|