/* USER CODE BEGIN 1 */
FRESULT res; /* FatFs function common result code */
uint32_t byteswritten, bytesread; /* File write/read counts */
uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */
uint8_t rtext[100]; /* File read buffer */
/* USER CODE END 1 */
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_SDIO_SD_Init();
MX_FATFS_Init();
/* USER CODE BEGIN 2 */
// BSP_SD_Init();
BSP_SD_GetCardInfo(&SDCardInfo);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if(f_mount(&SDFatFs, (TCHAR const*)SD_Path, 0) != FR_OK)
{
/* FatFs Initialization Error */
while(1);
}
else
{
/* Create and Open a new text file object with write access */
// if(f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
// {
// /* 'STM32.TXT' file Open for write Error */
// while(1);
// }
ret_Error = f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE);
if(ret_Error != FR_OK)
{
if(ret_Error == FR_NO_FILESYSTEM)
{
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12,GPIO_PIN_SET);
}
while(1);
}
else
{
/* Write data to the text file */
res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);
if((byteswritten == 0) || (res != FR_OK))
{
/* 'STM32.TXT' file Write or EOF Error */
while(1);
}
else
{
/* Close the open text file */
f_close(&MyFile);
/* Open the text file object with read access */
if(f_open(&MyFile, "STM32.TXT", FA_READ) != FR_OK)
{
/* 'STM32.TXT' file Open for read Error */
while(1);
}
else
{
/* Read data from the text file */
res = f_read(&MyFile, rtext, sizeof(rtext), (void *)&bytesread);
if((bytesread == 0) || (res != FR_OK))
{
/* 'STM32.TXT' file Read or EOF Error */
while(1);
}
else
{
/* Close the open text file */
f_close(&MyFile);
/* Compare read data with the expected data */
if((bytesread != byteswritten))
{
/* Read data is different from the expected data */
while(1);
}
else
{
/* Success of the demo: no error occurrence */
while(1);
}
}
}
}
}
}
}
/* USER CODE END 3 */
int main(void)
{
/* USER CODE BEGIN 1 */
FRESULT res; /* FatFs function common result code */
uint32_t byteswritten, bytesread; /* File write/read counts */
uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */
uint8_t rtext[100]; /* File read buffer */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_SDIO_SD_Init();
MX_FATFS_Init();
/* USER CODE BEGIN 2 */
// BSP_SD_Init();
BSP_SD_GetCardInfo(&SDCardInfo);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if(f_mount(&SDFatFs, (TCHAR const*)SD_Path, 0) != FR_OK)
{
/* FatFs Initialization Error */
while(1);
}
else
{
/* Create and Open a new text file object with write access */
// if(f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
// {
// /* 'STM32.TXT' file Open for write Error */
// while(1);
// }
ret_Error = f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE);
if(ret_Error != FR_OK)
{
if(ret_Error == FR_NO_FILESYSTEM)
{
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12,GPIO_PIN_SET);
}
while(1);
}
else
{
/* Write data to the text file */
res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);
if((byteswritten == 0) || (res != FR_OK))
{
/* 'STM32.TXT' file Write or EOF Error */
while(1);
}
else
{
/* Close the open text file */
f_close(&MyFile);
/* Open the text file object with read access */
if(f_open(&MyFile, "STM32.TXT", FA_READ) != FR_OK)
{
/* 'STM32.TXT' file Open for read Error */
while(1);
}
else
{
/* Read data from the text file */
res = f_read(&MyFile, rtext, sizeof(rtext), (void *)&bytesread);
if((bytesread == 0) || (res != FR_OK))
{
/* 'STM32.TXT' file Read or EOF Error */
while(1);
}
else
{
/* Close the open text file */
f_close(&MyFile);
/* Compare read data with the expected data */
if((bytesread != byteswritten))
{
/* Read data is different from the expected data */
while(1);
}
else
{
/* Success of the demo: no error occurrence */
while(1);
}
}
}
}
}
}
}
/* USER CODE END 3 */
}
一周热门 更多>