找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3784|回复: 2
收起左侧

STM32 HAL库实现USB bulk数据传输程序

[复制链接]
ID:489280 发表于 2019-3-12 15:56 | 显示全部楼层 |阅读模式
MCU型号为STM32RCT6  
库函数为 HAL库
初始化代码生成方式为CUBEMX配置
详情请见附件源代码

单片机源程序如下:
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include "stm32f1xx_hal.h"
  4. #include "usb_device.h"
  5. #include "gpio.h"

  6. /* USER CODE BEGIN Includes */
  7. #include "usbd_conf.h"
  8. #include "usbd_cdc.h"
  9. #include "usbd_desc.h"
  10. #include "usbd_ctlreq.h"
  11. /* USER CODE END Includes */

  12. /* Private variables ---------------------------------------------------------*/

  13. /* USER CODE BEGIN PV */
  14. /* Private variables ---------------------------------------------------------*/
  15. uint8_t USBD_RxCnt = 0;
  16. uint8_t USBD_RXBuffer[64];
  17. /* USER CODE END PV */

  18. /* Private function prototypes -----------------------------------------------*/
  19. void SystemClock_Config(void);

  20. /* USER CODE BEGIN PFP */
  21. /* Private function prototypes -----------------------------------------------*/

  22. /* USER CODE END PFP */

  23. /* USER CODE BEGIN 0 */

  24. /* USER CODE END 0 */

  25. /**
  26.   * @brief  The application entry point.
  27.   *
  28.   * @retval None
  29.   */
  30. int main(void) {
  31.     /* USER CODE BEGIN 1 */
  32.     /* USER CODE END 1 */

  33.     /* MCU Configuration----------------------------------------------------------*/

  34.     /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  35.     HAL_Init();

  36.     /* USER CODE BEGIN Init */

  37.     /* USER CODE END Init */

  38.     /* Configure the system clock */
  39.     SystemClock_Config();

  40.     /* USER CODE BEGIN SysInit */

  41.     /* USER CODE END SysInit */

  42.     /* Initialize all configured peripherals */
  43.     MX_GPIO_Init();
  44.     MX_USB_DEVICE_Init();
  45.     /* USER CODE BEGIN 2 */
  46.     /* USER CODE END 2 */

  47.     /* Infinite loop */
  48.     /* USER CODE BEGIN WHILE */
  49.     while (1) {

  50.         /* USER CODE END WHILE */
  51.         if(USBD_RxCnt > 0) {
  52.             USBD_LL_Transmit(&hUsbDeviceFS,
  53.                              CDC_IN_EP,
  54.                              USBD_RXBuffer,
  55.                              USBD_RxCnt);
  56.             USBD_RxCnt = 0;
  57.         }
  58.         /* USER CODE BEGIN 3 */

  59.     }
  60.     /* USER CODE END 3 */

  61. }

  62. /**
  63.   * @brief System Clock Configuration
  64.   * @retval None
  65.   */
  66. void SystemClock_Config(void) {

  67.     RCC_OscInitTypeDef RCC_OscInitStruct;
  68.     RCC_ClkInitTypeDef RCC_ClkInitStruct;
  69.     RCC_PeriphCLKInitTypeDef PeriphClkInit;

  70.     /**Initializes the CPU, AHB and APB busses clocks
  71.     */
  72.     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  73.     RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  74.     RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  75.     RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  76.     RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  77.     RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  78.     RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  79.     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  80.         _Error_Handler(__FILE__, __LINE__);
  81.     }

  82.     /**Initializes the CPU, AHB and APB busses clocks
  83.     */
  84.     RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
  85.                                   | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  86.     RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  87.     RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  88.     RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  89.     RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  90.     if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
  91.         _Error_Handler(__FILE__, __LINE__);
  92.     }

  93.     PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
  94.     PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
  95.     if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
  96.         _Error_Handler(__FILE__, __LINE__);
  97.     }

  98.     /**Configure the Systick interrupt time
  99.     */
  100.     HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);

  101.     /**Configure the Systick
  102.     */
  103.     HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  104.     /* SysTick_IRQn interrupt configuration */
  105.     HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  106. }

  107. /* USER CODE BEGIN 4 */

  108. /* USER CODE END 4 */

  109. /**
  110.   * @brief  This function is executed in case of error occurrence.
  111.   * @param  file: The file name as string.
  112.   * @param  line: The line in file as a number.
  113.   * @retval None
  114.   */
  115. void _Error_Handler(char *file, int line) {
  116.     /* USER CODE BEGIN Error_Handler_Debug */
  117.     /* User can add his own implementation to report the HAL error return state */
  118.     while(1) {
  119.     }
  120.     /* USER CODE END Error_Handler_Debug */
  121. }

  122. #ifdef  USE_FULL_ASSERT
  123. /**
  124.   * @brief  Reports the name of the source file and the source line number
  125.   *         where the assert_param error has occurred.
  126. ……………………

  127. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
USB_BULK.7z (277.72 KB, 下载次数: 42)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:498026 发表于 2019-5-30 13:49 | 显示全部楼层
楼主你好,请问你这个怎么安装驱动啊,怎么测试通讯
回复

使用道具 举报

ID:489280 发表于 2019-5-31 11:33 | 显示全部楼层
九天揽月 发表于 2019-5-30 13:49
楼主你好,请问你这个怎么安装驱动啊,怎么测试通讯

www点stm32cube点com/article/178  请看这篇文章,也是我发的里面有驱动 数据通信测试,不过当时MDK的背景设置成了黑色所以贴图出来看不清,可以将图片下载到桌面上用看图软件打开看比较清楚,或者在下面的评论里面我有放附件源代码也可以,驱动制作采用的是NI VISA来制作的,当时只是验证个东西而已
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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