找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2936|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

STM32F4的usb cdc host通信源程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:584204 發(fā)表于 2019-7-15 09:31 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
STM32F4的usb cdc host通信

單片機(jī)源程序如下:
  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****/
復(fù)制代碼

所有資料51hei提供下載:
code_F429.7z (1.01 MB, 下載次數(shù): 42)


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:630062 發(fā)表于 2019-10-25 15:04 | 只看該作者
好東西,謝謝分享
回復(fù)

使用道具 舉報(bào)

板凳
ID:630062 發(fā)表于 2019-10-25 16:39 | 只看該作者
這個(gè)是host模式嗎?看用的是device的庫。
回復(fù)

使用道具 舉報(bào)

地板
ID:169559 發(fā)表于 2021-11-26 12:31 | 只看該作者
大姐,你這個(gè)是從吧,不是主啊
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表