STM32配置CH375B成HID Host模式读取自定义HID设备的数据 ——STM32端口初始化

原文地址::http://blog.csdn.net/zhushengbing11090/article/details/69502280?locationNum=1&fps=1


  最近产品需要一个USB主机测试治具,所以需要做一个USB HOST去读取HID设备的数据,由于以前也没做过USB方面的项目,对这一块也不是很熟悉,因此遇到了很多困难,所幸的是经过两天半的努力,最终完成了CH375B的调试。得意不多废话,先上一张我调试的MCU管脚分配图

  

头文件

[cpp]  view plain  copy
  1. #ifndef __BSP_INIT_H__  
  2. #define __BSP_INIT_H__  
  3.   
  4. #include "stm32f0xx_hal.h"  
  5. #include "config.h"  
  6.   
  7. typedef struct  
  8. {  
  9.     struct  
  10.     {  
  11.         uint16_t USB_BaseTime;  
  12.         uint8_t  USB_Flag;  
  13.     }USB_Heart;  
  14. }Sys_Heart_Def;  
  15.   
  16. typedef struct  
  17. {  
  18.     Sys_Heart_Def   Sys_Heart_Info;  
  19.     uint8_t         USB_Connect_Status;  
  20. }Sys_Param_Def;  
  21.   
  22. extern Sys_Param_Def      SysParam_Info;  
  23. extern UART_HandleTypeDef HAL_CH375_USART;  
  24.   
  25. void Error_Handler(void);  
  26. void Bsp_Peripherals_Init(void);  
  27.   
  28. #endif  


详细的代码如下:

[cpp]  view plain  copy
  1. #include "bsp_init.h"  
  2.   
  3. Sys_Param_Def      SysParam_Info;  
  4. /* Private variables ---------------------------------------------------------*/  
  5. UART_HandleTypeDef HAL_CH375_USART;  
  6.   
  7. void Bsp_SystemClock_Init(void)  
  8. {  
  9.   RCC_OscInitTypeDef RCC_OscInitStruct;  
  10.   RCC_ClkInitTypeDef RCC_ClkInitStruct;  
  11.   RCC_PeriphCLKInitTypeDef PeriphClkInit;  
  12.   
  13.   /* Initializes the CPU, AHB and APB busses clocks */  
  14.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;  
  15.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;  
  16.   RCC_OscInitStruct.HSICalibrationValue = 16;  
  17.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;  
  18.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;  
  19.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;  
  20.   RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;  
  21.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)  
  22.   {  
  23.     Error_Handler();  
  24.   }  
  25.   
  26.   /* Initializes the CPU, AHB and APB busses clocks */  
  27.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK  
  28.                                 |RCC_CLOCKTYPE_PCLK1;  
  29.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;  
  30.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;  
  31.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;  
  32.   
  33.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)  
  34.   {  
  35.     Error_Handler();  
  36.   }  
  37.   
  38.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;  
  39.   PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;  
  40.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)  
  41.   {  
  42.     Error_Handler();  
  43.   }  
  44.   
  45.   /* Configure the Systick interrupt time */  
  46.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);  
  47.   
  48.   /* Configure the Systick */  
  49.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);  
  50.   
  51.   /* SysTick_IRQn interrupt configuration */  
  52.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);  
  53. }  
  54.   
  55. void Bsp_GPIO_Init(void)  
  56. {  
  57.   GPIO_InitTypeDef GPIO_InitStruct;  
  58.   
  59.   /* GPIO Ports Clock Enable */  
  60.   __HAL_RCC_GPIOC_CLK_ENABLE();  
  61.   __HAL_RCC_GPIOA_CLK_ENABLE();  
  62.   __HAL_RCC_GPIOB_CLK_ENABLE();  
  63.   
  64.   /*Configure GPIO pin Output Level */  
  65.   HAL_GPIO_WritePin(CH375_D0_GPIO_Port, CH375_D0_Pin, GPIO_PIN_SET);  
  66.   HAL_GPIO_WritePin(CH375_D1_GPIO_Port, CH375_D1_Pin, GPIO_PIN_SET);  
  67.   HAL_GPIO_WritePin(CH375_D2_GPIO_Port, CH375_D2_Pin, GPIO_PIN_SET);  
  68.   HAL_GPIO_WritePin(CH375_D3_GPIO_Port, CH375_D3_Pin, GPIO_PIN_SET);  
  69.   HAL_GPIO_WritePin(CH375_D4_GPIO_Port, CH375_D4_Pin, GPIO_PIN_SET);  
  70.   HAL_GPIO_WritePin(CH375_D5_GPIO_Port, CH375_D5_Pin, GPIO_PIN_SET);  
  71.   HAL_GPIO_WritePin(CH375_D6_GPIO_Port, CH375_D6_Pin, GPIO_PIN_SET);  
  72.   HAL_GPIO_WritePin(CH375_D7_GPIO_Port, CH375_D7_Pin, GPIO_PIN_SET);  
  73.   /*Configure GPIO pin Output Level */  
  74.   HAL_GPIO_WritePin(CH375_WR_GPIO_Port, CH375_WR_Pin, GPIO_PIN_SET);  
  75.   HAL_GPIO_WritePin(CH375_RD_GPIO_Port, CH375_RD_Pin, GPIO_PIN_SET);  
  76.   HAL_GPIO_WritePin(CH375_CS_GPIO_Port, CH375_CS_Pin, GPIO_PIN_SET);  
  77.   HAL_GPIO_WritePin(CH375_A0_GPIO_Port, CH375_A0_Pin, GPIO_PIN_SET);      
  78.       
  79.   /*Configure GPIO pin : CH375_INT_Pin */  
  80.   GPIO_InitStruct.Pin = CH375_INT_Pin;  
  81.   GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;  
  82.   GPIO_InitStruct.Pull = GPIO_PULLUP;  
  83.   HAL_GPIO_Init(CH375_INT_GPIO_Port, &GPIO_InitStruct);  
  84.   
  85.   /*Configure GPIO pins : CH375_D0_Pin CH375_D1_Pin CH375_D2_Pin CH375_D3_Pin  
  86.                            CH375_D4_Pin CH375_D5_Pin CH375_D6_Pin CH375_D7_Pin */  
  87.   GPIO_InitStruct.Pin = CH375_D0_Pin;  
  88.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;  
  89.   GPIO_InitStruct.Pull = GPIO_NOPULL;  
  90.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;  
  91.   HAL_GPIO_Init(CH375_D0_GPIO_Port, &GPIO_InitStruct);    
  92.       
  93.   GPIO_InitStruct.Pin = CH375_D1_Pin;  
  94.   HAL_GPIO_Init(CH375_D1_GPIO_Port, &GPIO_InitStruct);  
  95.       
  96.   GPIO_InitStruct.Pin = CH375_D2_Pin;  
  97.   HAL_GPIO_Init(CH375_D2_GPIO_Port, &GPIO_InitStruct);  
  98.       
  99.   GPIO_InitStruct.Pin = CH375_D3_Pin;  
  100.   HAL_GPIO_Init(CH375_D3_GPIO_Port, &GPIO_InitStruct);  
  101.       
  102.   GPIO_InitStruct.Pin = CH375_D4_Pin;  
  103.   HAL_GPIO_Init(CH375_D4_GPIO_Port, &GPIO_InitStruct);  
  104.       
  105.   GPIO_InitStruct.Pin = CH375_D5_Pin;  
  106.   HAL_GPIO_Init(CH375_D5_GPIO_Port, &GPIO_InitStruct);  
  107.       
  108.   GPIO_InitStruct.Pin = CH375_D6_Pin;  
  109.   HAL_GPIO_Init(CH375_D6_GPIO_Port, &GPIO_InitStruct);  
  110.       
  111.   GPIO_InitStruct.Pin = CH375_D7_Pin;  
  112.   HAL_GPIO_Init(CH375_D7_GPIO_Port, &GPIO_InitStruct);  
  113.       
  114.   /*Configure GPIO pins : CH375_WR_Pin CH375_RD_Pin CH375_CS_Pin CH375_A0_Pin */  
  115.   GPIO_InitStruct.Pin = CH375_WR_Pin;  
  116.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;  
  117.   GPIO_InitStruct.Pull = GPIO_NOPULL;  
  118.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;  
  119.   HAL_GPIO_Init(CH375_WR_GPIO_Port, &GPIO_InitStruct);  
  120.       
  121.   GPIO_InitStruct.Pin = CH375_RD_Pin;  
  122.   HAL_GPIO_Init(CH375_RD_GPIO_Port, &GPIO_InitStruct);  
  123.       
  124.   GPIO_InitStruct.Pin = CH375_CS_Pin;  
  125.   HAL_GPIO_Init(CH375_CS_GPIO_Port, &GPIO_InitStruct);  
  126.       
  127.   GPIO_InitStruct.Pin = CH375_A0_Pin;  
  128.   HAL_GPIO_Init(CH375_A0_GPIO_Port, &GPIO_InitStruct);  
  129.       
  130.   HAL_NVIC_SetPriority(EXTI4_15_IRQn,0,0);  
  131.   HAL_NVIC_DisableIRQ(EXTI4_15_IRQn);  
  132. }  
  133.   
  134. void Bsp_CH375_USART_Init(void)  
  135. {  
  136.   
  137.   HAL_CH375_USART.Instance = USART1;  
  138.   HAL_CH375_USART.Init.BaudRate = 115200;  
  139.   HAL_CH375_USART.Init.WordLength = UART_WORDLENGTH_8B;  
  140.   HAL_CH375_USART.Init.StopBits = UART_STOPBITS_1;  
  141.   HAL_CH375_USART.Init.Parity = UART_PARITY_NONE;  
  142.   HAL_CH375_USART.Init.Mode = UART_MODE_TX_RX;  
  143.   HAL_CH375_USART.Init.HwFlowCtl = UART_HWCONTROL_NONE;  
  144.   HAL_CH375_USART.Init.OverSampling = UART_OVERSAMPLING_16;  
  145.   HAL_CH375_USART.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;  
  146.   HAL_CH375_USART.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;  
  147.   if (HAL_UART_Init(&HAL_CH375_USART) != HAL_OK)  
  148.   {  
  149.     Error_Handler();  
  150.   }  
  151.   __HAL_UART_ENABLE(&HAL_CH375_USART);  
  152. }  
  153.   
  154. void Bsp_Peripherals_Init(void)  
  155. {  
  156.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */  
  157.   HAL_Init();  
  158.   /* Configure the system clock */  
  159.   Bsp_SystemClock_Init();  
  160.   Bsp_GPIO_Init();  
  161.   Bsp_CH375_USART_Init();  
  162. }  
  163.   
  164. /** 
  165.   * @brief  This function is executed in case of error occurrence. 
  166.   * @param  None 
  167.   * @retval None 
  168.   */  
  169. void Error_Handler(void)  
  170. {  
  171.   /* USER CODE BEGIN Error_Handler */  
  172.   /* User can add his own implementation to report the HAL error return state */  
  173.   while(1)   
  174.   {  
  175.   }  
  176.   /* USER CODE END Error_Handler */   
  177. }  
  178.   
  179. /***/  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值