7) 修改 main.c。参考\STM32Cube_FW_F4_V1.11.0\Projects\STM324x9I_EVAL\Applications\FatFs\FatFs_uSD\Src\main.c,
修改此文件,如下:- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- /** @addtogroup STM32F4xx_StdPeriph_Examples
- * @{
- */
- /** @addtogroup SDIO_uSDCard
- * @{
- */
- /* Private typedef -----------------------------------------------------------*/
- typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*//* Private variables ---------------------------------------------------------*/
- FATFS SDFatFs; /* File system object for SD card logical drive */
- FIL MyFile; /* File object */
- char SDPath[4]; /* SD card logical drive path */
- /* Private function prototypes -----------------------------------------------*/
- static void NVIC_Configuration(void);
- static void Error_Handler(void);
- /* Private functions ---------------------------------------------------------*/
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] Main program
- * @param None
- * @retval None
- */
- int main(void)
- {
- /*!< At this stage the microcontroller clock setting is already configured,
- this is done through SystemInit() function which is called from startup
- files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s)
- before to branch to application main. To reconfigure the default setting
- of SystemInit() function, refer to system_stm32f4xx.c file
- */
- FRESULT res; /* FatFs function common result
- code */
- uint32_t byteswritten, bytesread; /* File write/read counts */
- uint8_t wtext[] = "STM32 with FatFs demo [url=home.php?mod=space&uid=72445]@[/url] std lib"; /* File write buffer */
- uint8_t rtext[100]; /* File read buffer */
- /* Initialize LEDs available on EVAL board */
- STM_EVAL_LEDInit(LED1);
- STM_EVAL_LEDInit(LED3);
- /* NVIC Configuration */
- NVIC_Configuration();
- /*##-1- Link the micro SD disk I/O driver ##################################*/
- if(FATFS_LinkDriver(&SD_Driver, SDPath) == 0)
- {
- /*##-2- Register the file system object to the FatFs module ##############*/
- if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) != FR_OK)
- {
- /* FatFs Initialization Error */
- Error_Handler();
- }
- else
- {
- /*##-3- Create a FAT file system (format) on the logical drive #########*//* WARNING: Formatting the uSD card will delete all content on the device */
- if(f_mkfs((TCHAR const*)SDPath, 0, 0) != FR_OK)
- {
- /* FatFs Format Error */
- Error_Handler();
- }
- else
- {
- /*##-4- Create and Open a new text file object with write access #####*/
- if(f_open(&MyFile, "STM32STD.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
- {
- /* 'STM32STD.TXT' file Open for write Error */
- Error_Handler();
- }
- else
- {
- /*##-5- Write data to the text file ################################*/
- res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);
- if((byteswritten == 0) || (res != FR_OK))
- {
- /* 'STM32STD.TXT' file Write or EOF Error */
- Error_Handler();
- }
- else
- {
- /*##-6- Close the open text file #################################*/
- f_close(&MyFile);
- /*##-7- Open the text file object with read access ###############*/
- if(f_open(&MyFile, "STM32STD.TXT", FA_READ) != FR_OK)
- {
- /* 'STM32STD.TXT' file Open for read Error */
- Error_Handler();
- }
- else
- {
- /*##-8- Read data from the text file ###########################*/
- res = f_read(&MyFile, rtext, sizeof(rtext), (UINT*)&bytesread);
- if((bytesread == 0) || (res != FR_OK))
- {
- /* 'STM32STD.TXT' file Read or EOF Error */
- Error_Handler();
- }
- else
- {
- /*##-9- Close the open text file #############################*/
- f_close(&MyFile);/*##-10- Compare read data with the expected data ############*/
- if((bytesread != byteswritten))
- {
- /* Read data is different from the expected data */
- Error_Handler();
- }
- else
- {
- /* Success of the demo: no error occurrence */
- STM_EVAL_LEDOn(LED1);
- }
- }
- }
- }
- }
- }
- }
- }
- /*##-11- Unlink the micro SD disk I/O driver ###############################*/
- FATFS_UnLinkDriver(SDPath);
- /* Infinite loop */
- while (1)
- {
- }
- }
- /**
- * @brief Configures SDIO IRQ channel.
- * @param None
- * @retval None
- */
- static void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- /* Configure the NVIC Preemption Priority Bits */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- NVIC_InitStructure.NVIC_IRQChannel = SD_SDIO_DMA_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_Init(&NVIC_InitStructure);}
- /**
- * @brief This function is executed in case of error occurrence.
- * @param None
- * @retval None
- */
- static void Error_Handler(void)
- {
- /* Turn LED3 on */
- STM_EVAL_LEDOn(LED3);
- while(1)
- {
- }
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
还需要在 main.h 中加入:
- /* FatFs includes component */
- #include "ff_gen_drv.h"
- #include "sd_diskio.h"
此主函数实现功能为:创建 STM32STD.TXT 文件,并向其写入“ STM32 with FatFs demo @ std lib”
8) 编译,成功。
|