打印

STM32 USB和上位机传输数据

[复制链接]
4365|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

使用特权

评论回复
5
309030106| | 2015-6-22 20:34 | 只看该作者
官方应该有例程的

使用特权

评论回复
6
feima1314|  楼主 | 2015-6-24 10:31 | 只看该作者
icecut 发表于 2015-6-22 08:54
官方自带的 usb cdc

但程序看不太懂,怎么实现数据在上位机上实时显示,并能检测到发送速率,跪求这方面资料。

使用特权

评论回复
7
feima1314|  楼主 | 2015-6-24 10:32 | 只看该作者
chenci2013 发表于 2015-6-21 10:25
有很多的现成的例程吧。

有的看不太懂,有这方面的资料吗

使用特权

评论回复
8
lefeng| | 2015-6-25 21:36 | 只看该作者
官方的最简单了,看不懂的话,这里的恐怕。。。

使用特权

评论回复
9
insignal| | 2015-6-26 09:33 | 只看该作者
custom hid例程
免驱动

使用特权

评论回复
10
gaoyang9992006| | 2015-6-26 12:32 | 只看该作者
feima1314 发表于 2015-6-24 10:31
但程序看不太懂,怎么实现数据在上位机上实时显示,并能检测到发送速率,跪求这方面资料。 ...

看相关头文件里的说明,不要只看代码,一定看说明。

使用特权

评论回复
11
gaoyang9992006| | 2015-6-26 13:08 | 只看该作者
官方的最简单了,看不懂的话,这里的恐怕更难懂了。

使用特权

评论回复
12
PIGYONG801| | 2015-6-26 14:05 | 只看该作者
小数据量有 HID

中数据量用 CDC

大数据量用BULK

使用特权

评论回复
13
香水城| | 2015-6-26 15:06 | 只看该作者
本版置顶帖:官网STM32 USB资料汇总 + USB培训资料

使用特权

评论回复
14
598330983| | 2015-6-26 22:11 | 只看该作者

小数据量有 HID

中数据量用 CDC

大数据量用BULK;P

使用特权

评论回复
15
XinyuWang| | 2015-6-26 22:56 | 只看该作者
我觉得最简单是USB虚拟串口(VCP),上位机就用串口调试工具就ok了

使用特权

评论回复
16
libingqing| | 2015-12-25 20:07 | 只看该作者
请问用虚拟串口向上位机发送数据的速率多大呢,我发的才10k左右啊,不明白为什么?

使用特权

评论回复
17
zhuotuzi| | 2015-12-25 21:32 | 只看该作者
/**
  ******************************************************************************
  * [url=home.php?mod=space&uid=288409]@file[/url]    USB_Host/FWupgrade_Standalone/Src/main.c
  * [url=home.php?mod=space&uid=187600]@author[/url]  MCD Application Team
  * [url=home.php?mod=space&uid=895143]@version[/url] V1.0.1
  * [url=home.php?mod=space&uid=212281]@date[/url]    26-February-2014
  * [url=home.php?mod=space&uid=247401]@brief[/url]   USB host Firmware Upgrade demo main file
  ******************************************************************************
  * @attention
  *
  * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

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

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
USBH_HandleTypeDef hUSBHost;
FW_ApplicationTypeDef Appli_state = APPLICATION_DISCONNECT;
uint32_t JumpAddress;
pFunction Jump_To_Application;

/* Private function prototypes -----------------------------------------------*/
static void SystemClock_Config(void);
static void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id);
static void FW_InitApplication(void);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* Configure KEY Button */
  BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO);
  
  /* Unlock the Flash to enable the flash control register access */
  FLASH_If_FlashUnlock();
  
  /* Test if User button on the STM324xG_EVAL is pressed */
  if (BSP_PB_GetState(BUTTON_KEY) != GPIO_PIN_RESET)
  {
    /* Check Vector Table: Test if user code is programmed starting from address
    "APPLICATION_ADDRESS" */
    if ((((*(__IO uint32_t*)APPLICATION_ADDRESS) & 0xFF000000 ) == 0x20000000) || \
      (((*(__IO uint32_t*)APPLICATION_ADDRESS) & 0xFF000000 ) == 0x10000000))
    {
      /* Jump to user application */
      JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);
      Jump_To_Application = (pFunction) JumpAddress;
      /* Initialize user application's Stack Pointer */
      __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
      Jump_To_Application();
    }
  }
  
    /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Global MSP (MCU Support Package) initialization
     */
  HAL_Init();
  
  /* Configure the system clock to 168 Mhz */
  SystemClock_Config();
  
  /* Init FW upgrade Application */
  FW_InitApplication();
  
  /* Init Host Library */
  USBH_Init(&hUSBHost, USBH_UserProcess, 0);
  
  /* Add Supported Class */
  USBH_RegisterClass(&hUSBHost, USBH_MSC_CLASS);
  
  /* Start Host Process */
  USBH_Start(&hUSBHost);
  
  /* Run Application (Blocking mode)*/
  while (1)
  {
    /* USB Host Background task */
    USBH_Process(&hUSBHost);
   
    /* FW Menu Process */
    FW_UPGRADE_Process();
  }
}

/**
  * @brief  FW application Init.
  * @param  None
  * @retval None
  */
static void FW_InitApplication(void)
{
  /* Configure the LEDs */
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED4);
}

/**
  * @brief  User Process
  * @param  phost: Host Handle
  * @param  id: Host Library user message ID
  * @retval None
  */
static void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id)
{  
  switch(id)
  {
  case HOST_USER_SELECT_CONFIGURATION:
    break;
   
  case HOST_USER_DISCONNECTION:
    Appli_state = APPLICATION_DISCONNECT;
    break;
   
  case HOST_USER_CLASS_ACTIVE:
    Appli_state = APPLICATION_READY;
    break;
   
  case HOST_USER_CONNECTION:
    Appli_state = APPLICATION_CONNECT;   
    break;
     
  default:
    break;
  }
}

/**
  * @brief  System Clock Configuration
  *         The system Clock is configured as follow :
  *            System Clock source            = PLL (HSE)
  *            SYSCLK(Hz)                     = 168000000
  *            HCLK(Hz)                       = 168000000
  *            AHB Prescaler                  = 1
  *            APB1 Prescaler                 = 4
  *            APB2 Prescaler                 = 2
  *            HSE Frequency(Hz)              = 8000000
  *            PLL_M                          = 8
  *            PLL_N                          = 336
  *            PLL_P                          = 2
  *            PLL_Q                          = 7
  *            VDD(V)                         = 3.3
  *            Main regulator output voltage  = Scale1 mode
  *            Flash Latency(WS)              = 5
  * @param  None
  * @retval None
  */
static void SystemClock_Config(void)
{
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_OscInitTypeDef RCC_OscInitStruct;
  
  /* Enable HSE Oscillator and activate PLL with HSE as source */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 336;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);
  
  
  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  clocks dividers */
  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;  
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;  
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
}

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

  /* Infinite loop */
  while (1)
  {}
}
#endif

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


使用特权

评论回复
18
huangcunxiake| | 2015-12-25 23:32 | 只看该作者
手把手的看人家做,另外不会的还可以问问,这样才学的会啊,这都搞的太复杂了。

使用特权

评论回复
19
eldiy| | 2015-12-27 17:43 | 只看该作者
用CubeMx 可以配置出自己想要的USB设备,关于USB的源码资料也不少,拿来就直接用。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

5

帖子

0

粉丝