使用STM32CubeMX生成USB_HOST_HID工程

使用开发板为STM32F105开发板

原本想将具体步骤给写出来。时间有限。直接将STM32F105_USBH_HID.IOC上传files.cnblogs.com/files/libra13179/STM32F105_USBH_HID.zip

[需要注意开发板使用的外部晶振为25mhz]

 

下面给出主要修改地方

 

usbh_conf.c中修改

/**
  * @brief  USBH_LL_DriverVBUS 
  *         Drive VBUS.
  * @param  phost: Host handle
  * @param  state : VBUS state
  *          This parameter can be one of the these values:
  *           0 : VBUS Active 
  *           1 : VBUS Inactive
  * @retval Status
  */
USBH_StatusTypeDef  USBH_LL_DriverVBUS (USBH_HandleTypeDef *phost, uint8_t state)
{ 

  /* USER CODE BEGIN 0 */
  /* USER CODE END 0*/     
  if (phost->id == HOST_FS) 
  { 
    if (state == 0)
    {   
      /* Drive high Charge pump */
      /* ToDo: Add IOE driver control */       
      /* USER CODE BEGIN DRIVE_HIGH_CHARGE_FOR_FS */
      HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_SET);
      /* USER CODE END DRIVE_HIGH_CHARGE_FOR_FS */ 
    } 
    else
    {
      /* Drive low Charge pump */
      /* ToDo: Add IOE driver control */    
      /* USER CODE BEGIN DRIVE_LOW_CHARGE_FOR_FS */
      HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET);
      /* USER CODE END DRIVE_LOW_CHARGE_FOR_FS */ 
    }
  }    
  HAL_Delay(200);
  return USBH_OK;  
}

 

在 usbh_conf.H修改使用rtt来打印信息

 /* DEBUG macros */  

#ifdef RTT_LOG_ENABLED

#include "rtt_log.h"

    #if (USBH_DEBUG_LEVEL > 0)
    #define  USBH_UsrLog   logi
    #else
    #define USBH_UsrLog(...)   
    #endif 
                                
                                
    #if (USBH_DEBUG_LEVEL > 1)

    #define  USBH_ErrLog    logw
    #else
    #define USBH_ErrLog(...)   
    #endif 
                                
                                
    #if (USBH_DEBUG_LEVEL > 2)                         
    #define  USBH_DbgLog    loge
    #else
    #define USBH_DbgLog(...)                         
    #endif
    
#else
    
    #if (USBH_DEBUG_LEVEL > 0)
    #define  USBH_UsrLog(...)   printf(__VA_ARGS__);\
                                printf("\n");
    #else
    #define USBH_UsrLog(...)   
    #endif 
                                
                                
    #if (USBH_DEBUG_LEVEL > 1)

    #define  USBH_ErrLog(...)   printf("ERROR: ") ;\
                                printf(__VA_ARGS__);\
                                printf("\n");
    #else
    #define USBH_ErrLog(...)   
    #endif 
                                
                                
    #if (USBH_DEBUG_LEVEL > 2)                         
    #define  USBH_DbgLog(...)   printf("DEBUG : ") ;\
                                printf(__VA_ARGS__);\
                                printf("\n");
    #else
    #define USBH_DbgLog(...)                         
    #endif
    
#endif //RTT_LOG_ENABLED

好了现在下载测试

使用otg线将键盘和开发板连接

好了现在开始我们添加一下怎么识别键盘的按键信息。

在usb_host.c中添加下面代码

/* USER CODE BEGIN 1 */
void USBH_HID_EventCallback(USBH_HandleTypeDef *phost)
{
   HID_TypeTypeDef   type = HID_UNKNOWN;
   
   USBH_ErrLog("USBH_HID_EventCallback");
   
   type = USBH_HID_GetDeviceType(phost);
   
   switch(type)
   {
       case HID_KEYBOARD:
       {
            HID_KEYBD_Info_TypeDef *keyboard_info;
            
            char ascii;
            keyboard_info = USBH_HID_GetKeybdInfo(phost);

            if( keyboard_info != NULL )
            {
                ascii = USBH_HID_GetASCIICode(keyboard_info);
                if( ascii != 0 )
                {
                    USBH_UsrLog("%c",ascii);
                }
            }
       }

         
       break;
       
       case HID_MOUSE:
       {
           //HID_MOUSE_Info_TypeDef *USBH_HID_GetMouseInfo(USBH_HandleTypeDef *phost)
       }
       break;
       
       default:
       break;
   }
}
/* USER CODE END 1 */

测试一下

 

现在测试鼠标

先在usb_host.c中添加下面代码

/* USER CODE BEGIN 1 */
void USBH_HID_EventCallback(USBH_HandleTypeDef *phost)
{
   HID_TypeTypeDef   type = HID_UNKNOWN;
   
   USBH_ErrLog("USBH_HID_EventCallback");
   
   type = USBH_HID_GetDeviceType(phost);
   
   switch(type)
   {
       case HID_KEYBOARD:
       {
            HID_KEYBD_Info_TypeDef *keyboard_info;
            
            char ascii;
            keyboard_info = USBH_HID_GetKeybdInfo(phost);

            if( keyboard_info != NULL )
            {
                ascii = USBH_HID_GetASCIICode(keyboard_info);
                if( ascii != 0 )
                {
                    USBH_UsrLog("%c",ascii);
                }
            }
       }

         
       break;
       
       case HID_MOUSE:
        {
  
           HID_MOUSE_Info_TypeDef *mouse_info;
           mouse_info = USBH_HID_GetMouseInfo(phost);
           USBH_UsrLog("mouse_info X%d, Y%d ,button %d %d %d",mouse_info->x,mouse_info->y,mouse_info->buttons[2],mouse_info->buttons[1],mouse_info->buttons[0]);
        
       }
       break;
       
       default:
       break;
   }
}
/* USER CODE END 1 */

 

 

转载于:https://www.cnblogs.com/libra13179/p/7371842.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值