找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2576|回复: 3
打印 上一主题 下一主题
收起左侧

STM32F4的usb cdc host通信源程序

[复制链接]
跳转到指定楼层
楼主
ID:584204 发表于 2019-7-15 09:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
STM32F4的usb cdc host通信

单片机源程序如下:
  1. /* Includes ------------------------------------------------------------------*/
  2. #include  "usbd_cdc_core.h"
  3. #include  "usbd_usr.h"
  4. #include  "usbd_desc.h"
  5. #include  "config.h"
  6. #include  "base_driver.h"
  7. #include  "delay.h"

  8. /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
  9.   * @{
  10.   */


  11. /** @defgroup APP_HID
  12.   * @brief Mass storage application module
  13.   * @{
  14.   */

  15. /** @defgroup APP_HID_Private_TypesDefinitions
  16.   * @{
  17.   */
  18. /**
  19.   * @}
  20.   */


  21. /** @defgroup APP_HID_Private_Defines
  22.   * @{
  23.   */


  24. /**
  25.   * @}
  26.   */


  27. /** @defgroup APP_HID_Private_Macros
  28.   * @{
  29.   */
  30. extern  uint8_t USB_StatusDataSended;
  31. extern  uint32_t USB_ReceivedCount;
  32. extern  uint8_t USB_Tx_Buffer[];
  33. extern  uint8_t USB_Rx_Buffer[];
  34. extern __IO uint8_t DeviceConfigured;
  35. /**
  36.   * @}
  37.   */


  38. /** @defgroup APP_HID_Private_Variables
  39.   * @{
  40.   */
  41.   
  42. #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
  43.   #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  44.     #pragma data_alignment=4   
  45.   #endif
  46. #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
  47. __ALIGN_BEGIN USB_OTG_CORE_HANDLE  USB_OTG_dev __ALIGN_END;

  48. /**
  49.   * @}
  50.   */


  51. /** @defgroup APP_HID_Private_FunctionPrototypes
  52.   * @{
  53.   */

  54. /**
  55.   * @}
  56.   */


  57. /** @defgroup APP_HID_Private_Functions
  58.   * @{
  59.   */

  60. /**
  61.   * @brief  Program entry point
  62.   * @param  None
  63.   * @retval None
  64.   */
  65. int main(void)
  66. {
  67.   static uint32_t SendDataNum = 0;
  68.   static uint8_t SendDataBuff[64*1024];
  69.   /*!< At this stage the microcontroller clock setting is already configured,
  70.   this is done through SystemInit() function which is called from startup
  71.   file (startup_stm32fxxx_xx.s) before to branch to application main.
  72.   To reconfigure the default setting of SystemInit() function, refer to
  73.   system_stm32fxxx.c file
  74.   */  
  75. // LED_Config();
  76.         RCC_ClocksTypeDef SYS_Clocks;

  77.   RCC_GetClocksFreq(&SYS_Clocks);

  78.   USBD_Init(&USB_OTG_dev,
  79. #ifdef USE_USB_OTG_HS
  80.             USB_OTG_HS_CORE_ID,
  81. #else            
  82.             USB_OTG_FS_CORE_ID,
  83. #endif
  84.             &USR_desc,
  85.             &USBD_CDC_cb,
  86.             &USR_cb);
  87. // while(DeviceConfigured==0);
  88.   //LED_Config();
  89.   //LED_On(LED_B);
  90.   //POWER_Config(500);
  91. // FSMC_Config();
  92.   //FSMC_DMA_Configuration((uint8_t *)SendDataBuff,(uint8_t *)FSMC_READ_ADDR,128);
  93.   delay_init(SystemCoreClock/1000000);
  94.   while(1)
  95.   {
  96.     if(USB_ReceivedCount > 0){
  97.       USB_ReceivedCount = 0;
  98.       //?????????????,???1K
  99.       SendDataNum = (USB_Rx_Buffer[0]<<24)|(USB_Rx_Buffer[1]<<16)|(USB_Rx_Buffer[2]<<8)|(USB_Rx_Buffer[3]<<0);
  100.       do{
  101.         //??????????
  102.         USB_StatusDataSended = 0;
  103.         //????
  104.         DCD_EP_Tx(&USB_OTG_dev,CDC_IN_EP,SendDataBuff,sizeof(SendDataBuff));
  105.         //????????,?????????????????????
  106.         while(USB_StatusDataSended == 0){
  107.           delay_us(1);
  108.         }
  109.         SendDataNum--;
  110.       }while(SendDataNum > 0);
  111.     }
  112. #ifdef LED_TEST
  113. /*   LED_Off(LED_B);
  114.     delay_ms(500);
  115.     LED_On(LED_B);
  116.     delay_ms(500);*/
  117. #endif
  118.   }
  119. }

  120. #ifdef USE_FULL_ASSERT
  121. /**
  122. * @brief  assert_failed
  123. *         Reports the name of the source file and the source line number
  124. *         where the assert_param error has occurred.
  125. * @param  File: pointer to the source file name
  126. * @param  Line: assert_param error line source number
  127. * @retval None
  128. */
  129. void assert_failed(uint8_t* file, uint32_t line)
  130. {
  131.   /* User can add his own implementation to report the file name and line number,
  132.   ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  133.   
  134.   /* Infinite loop */
  135.   while (1)
  136.   {}
  137. }
  138. #endif

  139. /**
  140.   * @}
  141.   */


  142. /**
  143.   * @}
  144.   */


  145. /**
  146.   * @}
  147.   */

  148. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

所有资料51hei提供下载:
code_F429.7z (1.01 MB, 下载次数: 42)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:630062 发表于 2019-10-25 15:04 | 只看该作者
好东西,谢谢分享
回复

使用道具 举报

板凳
ID:630062 发表于 2019-10-25 16:39 | 只看该作者
这个是host模式吗?看用的是device的库。
回复

使用道具 举报

地板
ID:169559 发表于 2021-11-26 12:31 | 只看该作者
大姐,你这个是从吧,不是主啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表