本帖最后由 斜阳__ 于 2017-6-8 17:35 编辑
基于CubeMX 4.21.0.
stm32F7库版本1.7.0
stm32f7的usb测试比之前f4/l4测试时候简单很多了.几乎所有代码都可以自动生成了。
usb配置:USB-OTG-FS:host_only
usb-host选择Mass Fatfs都选USB Disk
下面是时钟:
初始化顺序修改为 //usart一行可以忽略。
之后生成项目;
下面是代码修改:
在main文件中添加一下声明
- extern ApplicationTypeDef Appli_state;
- FATFS USBDISKFatFs; /* File system object for USB disk logical drive */
- FIL MyFile; /* File object */
- static void MSC_Application(void);
复制代码
添加一下函数
- static void MSC_Application(void)
- {
- 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 */
-
- /* Register the file system object to the FatFs module */
- if(f_mount(&USBDISKFatFs, (TCHAR const*)USBH_Path, 0) != FR_OK)
- {
- /* FatFs Initialization Error */
- Error_Handler();
- }
- 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 */
- Error_Handler();
- }
- 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 */
- Error_Handler();
- }
- 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 */
- Error_Handler();
- }
- 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 */
- Error_Handler();
- }
- 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 */
- Error_Handler();
- }
- else
- {
- /* Success of the demo: no error occurrence */
- // BSP_LED_On(LED_GREEN);
- }
- }
- }
- }
- }
- }
-
- /* Unlink the USB disk I/O driver */
- FATFS_UnLinkDriver(USBH_Path);
- }
复制代码
在while循环中添加:
- switch(Appli_state)
- {
- case APPLICATION_READY:
- MSC_Application();
- Appli_state = APPLICATION_IDLE;
- break;
-
- case APPLICATION_IDLE:
- default:
- break;
- }
复制代码 之后就可以make执行了。查上u盘之后会创建STM32.TXT文件。并写相应内容。
如果一次不成功,请多尝试几次
上工程
usbKey.rar
(6.26 MB, 下载次数: 39)
|
大神莫要谦虚
大神莫要谦虚