STM32 USB和上位机传输数据

[复制链接]
5258|18
 楼主| feima1314 发表于 2015-6-19 16:09 | 显示全部楼层 |阅读模式
跪求大牛知道STM32 USB和PC机通信,将数据发送到上位机。
chenci2013 发表于 2015-6-21 10:25 | 显示全部楼层
这个在网上搜一搜
chenci2013 发表于 2015-6-21 10:25 | 显示全部楼层
有很多的现成的例程吧。
icecut 发表于 2015-6-22 08:54 | 显示全部楼层
官方自带的 usb cdc
309030106 发表于 2015-6-22 20:34 | 显示全部楼层
官方应该有例程的
 楼主| feima1314 发表于 2015-6-24 10:31 | 显示全部楼层
icecut 发表于 2015-6-22 08:54
官方自带的 usb cdc

但程序看不太懂,怎么实现数据在上位机上实时显示,并能检测到发送速率,跪求这方面资料。
 楼主| feima1314 发表于 2015-6-24 10:32 | 显示全部楼层
chenci2013 发表于 2015-6-21 10:25
有很多的现成的例程吧。

有的看不太懂,有这方面的资料吗
lefeng 发表于 2015-6-25 21:36 | 显示全部楼层
官方的最简单了,看不懂的话,这里的恐怕。。。
insignal 发表于 2015-6-26 09:33 | 显示全部楼层
custom hid例程
免驱动
gaoyang9992006 发表于 2015-6-26 12:32 | 显示全部楼层
feima1314 发表于 2015-6-24 10:31
但程序看不太懂,怎么实现数据在上位机上实时显示,并能检测到发送速率,跪求这方面资料。 ...

看相关头文件里的说明,不要只看代码,一定看说明。
gaoyang9992006 发表于 2015-6-26 13:08 | 显示全部楼层
官方的最简单了,看不懂的话,这里的恐怕更难懂了。
PIGYONG801 发表于 2015-6-26 14:05 | 显示全部楼层
小数据量有 HID

中数据量用 CDC

大数据量用BULK
香水城 发表于 2015-6-26 15:06 | 显示全部楼层
本版置顶帖:官网STM32 USB资料汇总 + USB培训资料
598330983 发表于 2015-6-26 22:11 | 显示全部楼层

小数据量有 HID

中数据量用 CDC

大数据量用BULK;P
XinyuWang 发表于 2015-6-26 22:56 | 显示全部楼层
我觉得最简单是USB虚拟串口(VCP),上位机就用串口调试工具就ok了
libingqing 发表于 2015-12-25 20:07 | 显示全部楼层
请问用虚拟串口向上位机发送数据的速率多大呢,我发的才10k左右啊,不明白为什么?
zhuotuzi 发表于 2015-12-25 21:32 | 显示全部楼层
  1. /**
  2.   ******************************************************************************
  3.   * [url=home.php?mod=space&uid=288409]@file[/url]    USB_Host/FWupgrade_Standalone/Src/main.c
  4.   * [url=home.php?mod=space&uid=187600]@author[/url]  MCD Application Team
  5.   * [url=home.php?mod=space&uid=895143]@version[/url] V1.0.1
  6.   * [url=home.php?mod=space&uid=212281]@date[/url]    26-February-2014
  7.   * [url=home.php?mod=space&uid=247401]@brief[/url]   USB host Firmware Upgrade demo main file
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  12.   *
  13.   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14.   * You may not use this file except in compliance with the License.
  15.   * You may obtain a copy of the License at:
  16.   *
  17.   *        http://www.st.com/software_license_agreement_liberty_v2
  18.   *
  19.   * Unless required by applicable law or agreed to in writing, software
  20.   * distributed under the License is distributed on an "AS IS" BASIS,
  21.   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22.   * See the License for the specific language governing permissions and
  23.   * limitations under the License.
  24.   *
  25.   ******************************************************************************
  26.   */

  27. /* Includes ------------------------------------------------------------------*/
  28. #include "main.h"

  29. /* Private typedef -----------------------------------------------------------*/
  30. /* Private define ------------------------------------------------------------*/
  31. /* Private macro -------------------------------------------------------------*/
  32. /* Private variables ---------------------------------------------------------*/
  33. USBH_HandleTypeDef hUSBHost;
  34. FW_ApplicationTypeDef Appli_state = APPLICATION_DISCONNECT;
  35. uint32_t JumpAddress;
  36. pFunction Jump_To_Application;

  37. /* Private function prototypes -----------------------------------------------*/
  38. static void SystemClock_Config(void);
  39. static void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id);
  40. static void FW_InitApplication(void);

  41. /* Private functions ---------------------------------------------------------*/

  42. /**
  43.   * @brief  Main program
  44.   * @param  None
  45.   * @retval None
  46.   */
  47. int main(void)
  48. {
  49.   /* Configure KEY Button */
  50.   BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO);
  51.   
  52.   /* Unlock the Flash to enable the flash control register access */
  53.   FLASH_If_FlashUnlock();
  54.   
  55.   /* Test if User button on the STM324xG_EVAL is pressed */
  56.   if (BSP_PB_GetState(BUTTON_KEY) != GPIO_PIN_RESET)
  57.   {
  58.     /* Check Vector Table: Test if user code is programmed starting from address
  59.     "APPLICATION_ADDRESS" */
  60.     if ((((*(__IO uint32_t*)APPLICATION_ADDRESS) & 0xFF000000 ) == 0x20000000) || \
  61.       (((*(__IO uint32_t*)APPLICATION_ADDRESS) & 0xFF000000 ) == 0x10000000))
  62.     {
  63.       /* Jump to user application */
  64.       JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);
  65.       Jump_To_Application = (pFunction) JumpAddress;
  66.       /* Initialize user application's Stack Pointer */
  67.       __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
  68.       Jump_To_Application();
  69.     }
  70.   }
  71.   
  72.     /* STM32F4xx HAL library initialization:
  73.        - Configure the Flash prefetch, instruction and Data caches
  74.        - Configure the Systick to generate an interrupt each 1 msec
  75.        - Set NVIC Group Priority to 4
  76.        - Global MSP (MCU Support Package) initialization
  77.      */
  78.   HAL_Init();
  79.   
  80.   /* Configure the system clock to 168 Mhz */
  81.   SystemClock_Config();
  82.   
  83.   /* Init FW upgrade Application */
  84.   FW_InitApplication();
  85.   
  86.   /* Init Host Library */
  87.   USBH_Init(&hUSBHost, USBH_UserProcess, 0);
  88.   
  89.   /* Add Supported Class */
  90.   USBH_RegisterClass(&hUSBHost, USBH_MSC_CLASS);
  91.   
  92.   /* Start Host Process */
  93.   USBH_Start(&hUSBHost);
  94.   
  95.   /* Run Application (Blocking mode)*/
  96.   while (1)
  97.   {
  98.     /* USB Host Background task */
  99.     USBH_Process(&hUSBHost);
  100.    
  101.     /* FW Menu Process */
  102.     FW_UPGRADE_Process();
  103.   }
  104. }

  105. /**
  106.   * @brief  FW application Init.
  107.   * @param  None
  108.   * @retval None
  109.   */
  110. static void FW_InitApplication(void)
  111. {
  112.   /* Configure the LEDs */
  113.   BSP_LED_Init(LED3);
  114.   BSP_LED_Init(LED4);
  115. }

  116. /**
  117.   * @brief  User Process
  118.   * @param  phost: Host Handle
  119.   * @param  id: Host Library user message ID
  120.   * @retval None
  121.   */
  122. static void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id)
  123. {  
  124.   switch(id)
  125.   {
  126.   case HOST_USER_SELECT_CONFIGURATION:
  127.     break;
  128.    
  129.   case HOST_USER_DISCONNECTION:
  130.     Appli_state = APPLICATION_DISCONNECT;
  131.     break;
  132.    
  133.   case HOST_USER_CLASS_ACTIVE:
  134.     Appli_state = APPLICATION_READY;
  135.     break;
  136.    
  137.   case HOST_USER_CONNECTION:
  138.     Appli_state = APPLICATION_CONNECT;   
  139.     break;
  140.      
  141.   default:
  142.     break;
  143.   }
  144. }

  145. /**
  146.   * @brief  System Clock Configuration
  147.   *         The system Clock is configured as follow :
  148.   *            System Clock source            = PLL (HSE)
  149.   *            SYSCLK(Hz)                     = 168000000
  150.   *            HCLK(Hz)                       = 168000000
  151.   *            AHB Prescaler                  = 1
  152.   *            APB1 Prescaler                 = 4
  153.   *            APB2 Prescaler                 = 2
  154.   *            HSE Frequency(Hz)              = 8000000
  155.   *            PLL_M                          = 8
  156.   *            PLL_N                          = 336
  157.   *            PLL_P                          = 2
  158.   *            PLL_Q                          = 7
  159.   *            VDD(V)                         = 3.3
  160.   *            Main regulator output voltage  = Scale1 mode
  161.   *            Flash Latency(WS)              = 5
  162.   * @param  None
  163.   * @retval None
  164.   */
  165. static void SystemClock_Config(void)
  166. {
  167.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  168.   RCC_OscInitTypeDef RCC_OscInitStruct;
  169.   
  170.   /* Enable HSE Oscillator and activate PLL with HSE as source */
  171.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  172.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  173.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  174.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  175.   RCC_OscInitStruct.PLL.PLLM = 8;
  176.   RCC_OscInitStruct.PLL.PLLN = 336;
  177.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  178.   RCC_OscInitStruct.PLL.PLLQ = 7;
  179.   HAL_RCC_OscConfig(&RCC_OscInitStruct);
  180.   
  181.   
  182.   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  183.   clocks dividers */
  184.   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  185.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  186.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  187.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;  
  188.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;  
  189.   HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
  190. }

  191. #ifdef  USE_FULL_ASSERT
  192. /**
  193.   * @brief  Reports the name of the source file and the source line number
  194.   *   where the assert_param error has occurred.
  195.   * @param  file: pointer to the source file name
  196.   * @param  line: assert_param error line source number
  197.   * @retval None
  198.   */
  199. void assert_failed(uint8_t* file, uint32_t line)
  200. {
  201.   /* User can add his own implementation to report the file name and line number,
  202.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  203.   /* Infinite loop */
  204.   while (1)
  205.   {}
  206. }
  207. #endif

  208. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/


huangcunxiake 发表于 2015-12-25 23:32 | 显示全部楼层
手把手的看人家做,另外不会的还可以问问,这样才学的会啊,这都搞的太复杂了。
eldiy 发表于 2015-12-27 17:43 | 显示全部楼层
用CubeMx 可以配置出自己想要的USB设备,关于USB的源码资料也不少,拿来就直接用。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

5

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部