Skip to content

Commit

Permalink
IOC Modified: Suppress CubeMX generator from including MX_FATFS_Init(…
Browse files Browse the repository at this point in the history
…). User must call it manually
  • Loading branch information
SquadQuiz committed Apr 23, 2024
1 parent 9dea64f commit 5844f0b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 57 deletions.
114 changes: 58 additions & 56 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,98 +130,97 @@ int main(void)
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */

// REMARK: Main Setup

/*##-1- Link the micro SD disk I/O driver ##################################*/
MX_FATFS_Init();

/*##-2- Register the file system object to the FatFs module ##############*/
if (f_mount(&SDFatFS, (TCHAR const*)SDPath, 0) != FR_OK)
{
/* FatFs Initialization Error */
printf("FatFs: Failed to mount SD Card!!\n");
Error_Handler();
}
else
if (FATFS_LinkDriver(&SD_Driver, SDPath) == 0)
{
printf("FatFs: Successfully mounted SD Card\n");

/*##-3- Create a FAT file system (format) on the logical drive #########*/
/* WARNING: Formatting the uSD card will delete all content on the device */
printf("FatFs: Start formatting SD Card...\n");
if (f_mkfs((TCHAR const*)SDPath, FM_ANY, 0, workBuffer, sizeof(workBuffer)) != FR_OK)
/*##-2- Register the file system object to the FatFs module ##############*/
if (f_mount(&SDFatFS, (TCHAR const*)SDPath, 0) != FR_OK)
{
/* FatFs Format Error */
printf("FatFs: Failed to format SD Card!!\n");
/* FatFs Initialization Error */
printf("FatFs: Failed to mount SD Card!!\n");
Error_Handler();
}
else
{
/*##-4- Create and Open a new text file object with write access #####*/
printf("FatFs: Successfully to format SD Card!!\n");
if (f_open(&SDFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
printf("FatFs: Successfully mounted SD Card\n");

/*##-3- Create a FAT file system (format) on the logical drive #########*/
/* WARNING: Formatting the uSD card will delete all content on the device */
printf("FatFs: Start formatting SD Card...\n");
if (f_mkfs((TCHAR const*)SDPath, FM_ANY, 0, workBuffer, sizeof(workBuffer)) != FR_OK)
{
/* 'STM32.TXT' file Open for write Error */
printf("FatFs: Failed to open file for writing!!\n");
/* FatFs Format Error */
printf("FatFs: Failed to format SD Card!!\n");
Error_Handler();
}
else
{
printf("FatFs: Successfully to opened file!!\n");

/*##-5- Write data to the text file ################################*/
res = f_write(&SDFile, wtext, sizeof(wtext), (void*)&bytesToWritten);

if ((bytesToWritten == 0) || (res != FR_OK))
/*##-4- Create and Open a new text file object with write access #####*/
printf("FatFs: Successfully to format SD Card!!\n");
if (f_open(&SDFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
{
/* 'STM32.TXT' file Write or EOF Error */
printf("FatFs: Failed to write data to file!!\n");
/* 'STM32.TXT' file Open for write Error */
printf("FatFs: Failed to open file for writing!!\n");
Error_Handler();
}
else
{
printf("FatFs: Data successfully written to file!!\n");
printf("FatFs: Successfully to opened file!!\n");

/*##-6- Close the open text file #################################*/
f_close(&SDFile);
/*##-5- Write data to the text file ################################*/
res = f_write(&SDFile, wtext, sizeof(wtext), (void*)&bytesToWritten);

/*##-7- Open the text file object with read access ###############*/
if (f_open(&SDFile, "STM32.TXT", FA_READ) != FR_OK)
if ((bytesToWritten == 0) || (res != FR_OK))
{
/* 'STM32.TXT' file Open for read Error */
printf("FatFs: Failed to open file for reading!!\n");
/* 'STM32.TXT' file Write or EOF Error */
printf("FatFs: Failed to write data to file!!\n");
Error_Handler();
}
else
{
/*##-8- Read data from the text file ###########################*/
res = f_read(&SDFile, rtext, sizeof(rtext), (UINT*)&bytesToRead);
printf("FatFs: Data successfully written to file!!\n");

if ((bytesToRead == 0) || (res != FR_OK))
/*##-6- Close the open text file #################################*/
f_close(&SDFile);

/*##-7- Open the text file object with read access ###############*/
if (f_open(&SDFile, "STM32.TXT", FA_READ) != FR_OK)
{
/* 'STM32.TXT' file Read or EOF Error */
printf("FatFs: Failed to read data from file!!\n");
/* 'STM32.TXT' file Open for read Error */
printf("FatFs: Failed to open file for reading!!\n");
Error_Handler();
}
else
{
printf("FatFs: Data successfully read from file!!\n");

/*##-9- Close the open text file #############################*/
f_close(&SDFile);
/*##-8- Read data from the text file ###########################*/
res = f_read(&SDFile, rtext, sizeof(rtext), (UINT*)&bytesToRead);

/*##-10- Compare read data with the expected data ############*/
if ((bytesToRead != bytesToWritten))
if ((bytesToRead == 0) || (res != FR_OK))
{
/* Read data is different from the expected data */
printf("FatFs: Read data differs from the expected data!!\n");
/* 'STM32.TXT' file Read or EOF Error */
printf("FatFs: Failed to read data from file!!\n");
Error_Handler();
}
else
{
/* Success of the demo: no error occurrence */
HAL_GPIO_WritePin(LD1_GPIO_Port, LD1_Pin, GPIO_PIN_SET);
printf("FatFs: Demo executed successfully!!\n");
printf("FatFs: Data successfully read from file!!\n");

/*##-9- Close the open text file #############################*/
f_close(&SDFile);

/*##-10- Compare read data with the expected data ############*/
if ((bytesToRead != bytesToWritten))
{
/* Read data is different from the expected data */
printf("FatFs: Read data differs from the expected data!!\n");
Error_Handler();
}
else
{
/* Success of the demo: no error occurrence */
HAL_GPIO_WritePin(LD1_GPIO_Port, LD1_Pin, GPIO_PIN_SET);
printf("FatFs: Demo executed successfully!!\n");
}
}
}
}
Expand All @@ -230,6 +229,9 @@ int main(void)
}
}

/*##-11- Unlink the micro SD disk I/O driver ###############################*/
FATFS_UnLinkDriver(SDPath);

/* USER CODE END 2 */

/* Infinite loop */
Expand Down
2 changes: 1 addition & 1 deletion STM32F746NG_FatFs_uSD.ioc
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ ProjectManager.ToolChainLocation=
ProjectManager.UAScriptAfterPath=
ProjectManager.UAScriptBeforePath=
ProjectManager.UnderRoot=false
ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_DMA_Init-DMA-false-HAL-true,4-MX_FATFS_Init-FATFS-true-HAL-false,5-MX_SDMMC1_SD_Init-SDMMC1-false-HAL-true,6-MX_USART1_UART_Init-USART1-false-HAL-true,0-MX_CORTEX_M7_Init-CORTEX_M7-false-HAL-true
ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_DMA_Init-DMA-false-HAL-true,false-4-MX_FATFS_Init-FATFS-false-HAL-false,5-MX_SDMMC1_SD_Init-SDMMC1-false-HAL-true,6-MX_USART1_UART_Init-USART1-false-HAL-true,0-MX_CORTEX_M7_Init-CORTEX_M7-false-HAL-true
RCC.AHBFreq_Value=216000000
RCC.APB1CLKDivider=RCC_HCLK_DIV4
RCC.APB1Freq_Value=54000000
Expand Down

0 comments on commit 5844f0b

Please sign in to comment.