NUCLEO_144-F767 USBHost简单例程
本帖最后由 斜阳__ 于 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; /* 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文件。并写相应内容。
如果一次不成功,请多尝试几次:lol
上工程
沙发,学习了。。:) 搬个板凳:loveliness:学习。。。。:loveliness: creep 发表于 2017-6-8 20:44
沙发,学习了。。
大神莫要谦虚 Paderboy 发表于 2017-6-8 22:00
搬个板凳学习。。。。
大神莫要谦虚 感谢分享,学习了 排队学习~~~ 学习学习,回去把板子测试下 咦好奇怪,为什么我生成的代码只能检测到插入和断开,没有Ready呢? 呵呵,搞定了,我的问题出在D+/D-短路了。。。:lol
页:
[1]
2