/*! * \file fatfs.c * * \brief Target FatFS implementation * * \copyright Revised BSD License, see section \ref LICENSE. * * \code * * _ __ _ ________ _ * | |/ /(_)___ / ____/ /_ (_)___ * | // / __ \/ / / __ \/ / __ \ * / |/ / / / / /___/ / / / / /_/ / * /_/|_/_/_/ /_/\____/_/ /_/_/ .___/ * /_/ * (C) 2022-2025 XinChip * * \endcode * * \author ( XinChip ) Alex-J * * \author ( XinChip ) */ /*----------------------------------------------------------------------------------- INCLUDE HEADE FILES ------------------------------------------------------------------------------------*/ #include "fatfs.h" #include "xc6xxx.h" /*------------------------------------------------------------------------------------ Local Variables -------------------------------------------------------------------------------------*/ char Path[4]; /* Logical drive path */ FATFS FatFS; /* File system object for Logical drive */ FIL File; /* File object */ DIR Dir; uint8_t buffer[_MAX_SS]; /*------------------------------------------------------------------------------------ Functions ------------------------------------------------------------------------------------*/ /** * @brief FatFS Initalization * @param void * @retval void */ void FATFS_Init(void) { FRESULT res; SPI_InitCfg_t spi_cfg = {0}; spi_cfg.Mode = SPI_MODE_MASTER; spi_cfg.DataSize = SSI_CTRL0_DFS_LEN_8BIT; spi_cfg.Direction = SSI_CTRL0_TMOD_WR; spi_cfg.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; spi_cfg.CLKPolarity = SSI_CTRL0_SCPOL_LOW; spi_cfg.CLKPhase = SPI_CPHA_LEAD; spi_cfg.FirstBit = SPI_FirstBit_MSB; xc_spi_init(XC_SPI0, &spi_cfg); xc_spi_flash_wake_up(XC_SPI0); DEBUG("FatFS Init\r\n"); res = f_mount(&FatFS, "0:", 1); DEBUG("Mount FRESULT: %d\r\n", res); res = f_opendir(&Dir, "0:"); DEBUG("Read Dir: %d\r\n", res); if (FR_NO_FILESYSTEM == res) { res = f_mkfs("0:", FM_FAT, _MAX_SS, buffer, sizeof(buffer)); DEBUG("Format FRESULT: %d\r\n", res); res = f_unmount("0:"); DEBUG("Umount FRESULT: %d\r\n", res); // res = f_setlabel( "XinChip" ); // DEBUG("Set label: %d\r\n", res); res = f_mount(&FatFS, "0:", 1); DEBUG("Remount FRESULT: %d\r\n", res); res = f_unmount("0:"); DEBUG("Umount FRESULT: %d\r\n", res); } else { res = f_unmount("0:"); DEBUG("Umount FRESULT: %d\r\n", res); } }