找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5944|回复: 0
打印 上一主题 下一主题
收起左侧

usbh_usr.c(usb复合设备驱动)

[复制链接]
跳转到指定楼层
楼主
ID:51024 发表于 2014-8-3 00:47 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/**
  ******************************************************************************
  * @file    usbh_usr.c
  * @author  MCD Application Team
  * @version V2.1.0
  * @date    19-March-2012
  * @brief   This file includes the user application layer
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2012 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 "usbh_usr.h"
#include "usbh_hid_core.h"
#include "main.h"
#include <stdio.h>
#include <string.h>


/** @addtogroup USBH_USER
* @{
*/

/** @addtogroup USER
* @{
*/

/** @defgroup USBH_USR
* @brief    This file includes the user application layer
* @{
*/

/** @defgroup USBH_CORE_Exported_Types
* @{
*/



/** @defgroup USBH_USR_Private_Defines
* @{
*/
/**
* @}
*/


/** @defgroup USBH_USR_Private_Macros
* @{
*/
/**
* @}
*/

/** @defgroup USBH_USR_Private_Variables
* @{
*/
/*  Points to the DEVICE_PROP structure of current device */
/*  The purpose of this register is to speed up the execution */

USBH_Usr_cb_TypeDef USR_Callbacks =
{
  USBH_USR_Init,
  USBH_USR_DeInit,
  USBH_USR_DeviceAttached,
  USBH_USR_ResetDevice,
  USBH_USR_DeviceDisconnected,
  USBH_USR_OverCurrentDetected,
  USBH_USR_DeviceSpeedDetected,
  USBH_USR_Device_DescAvailable,
  USBH_USR_DeviceAddressAssigned,
  USBH_USR_Configuration_DescAvailable,
  USBH_USR_Manufacturer_String,
  USBH_USR_Product_String,
  USBH_USR_SerialNum_String,
  USBH_USR_EnumerationDone,
  USBH_USR_UserInput,
  NULL,
  USBH_USR_DeviceNotSupported,
  USBH_USR_UnrecoveredError
};

/** @defgroup USBH_USR_Private_Functions
* @{
*/

/** @defgroup USBH_HID_KEYBD_Private_Variables
* @{
*/

__ALIGN_BEGIN USB_OTG_CORE_HANDLEUSB_OTG_Core_dev __ALIGN_END ;
__ALIGN_BEGIN USBH_HOST             USB_Host  __ALIGN_END ;

enum{
USR_OTG_OFF = 0,
USR_OTG_OPENED,
USR_OTG_NOT_SUPPORT,
USR_OTG_OK
};

static u8 Usr_Otg_State;
static s8 Usr_Device_Index = -1;
void OTG_Configuration()
{
/* Init Host Library */
  if (STM32_OTG_Support > 0)
  {
  USBH_Init(&USB_OTG_Core_dev,
            USB_OTG_FS_CORE_ID,
            &USB_Host,
            &HID_cb,
            &USR_Callbacks);

  }
  memset(usb_key.key,NUM_KEYS,USBKEY_MAX);
  memset(usb_key.io,NUM_KEYS,USBKEY_MAX);
  usb_key.changed = OFF;
  Usr_Otg_State = Sys_Setting.bOTGFlag == ON ? USR_OTG_OPENED : USR_OTG_OFF;
}

void OTG_Main()
{
    /* Host Task handler */
if (STM32_OTG_Support > 0)
{
    USBH_Process(&USB_OTG_Core_dev , &USB_Host);
}
}

void OTG_Mouse_Release(void)
{
Host_Report_Event(HOST_EVENT_MOUSE_MSG,"\x0\x0\x0\x0",4);
}

void OTG_User_Event(u8 *data, u8 length)
{
#ifdef USE_OTG_FUN
if (length > 0)
{
if (data[0] == 0)
{
Sys_Setting.bOTGFlag = OFF;
Usr_Otg_State = USR_OTG_OFF;
}
else
{
Sys_Setting.bOTGFlag = ON;
Usr_Otg_State = USR_OTG_OPENED;
}

memset(usb_key.io,NUM_KEYS,USBKEY_MAX);
usb_key.changed = ON;
if (STM32_OTG_Support > 0)
{
OTG_Mouse_Release();
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_OTG_FS, DISABLE);
USBH_DeInit(&USB_OTG_Core_dev , &USB_Host);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_OTG_FS, ENABLE);
}
}
Host_Report_Event(HOST_EVENT_KEY_OTG,&Usr_Otg_State,1);
#endif
}

/**
* @brief  USBH_USR_Init
*         Displays the message on LCD for host lib initialization
* @param  None
* @retval None
*/
void USBH_USR_Init(void)
{
}

/**
* @brief  USBH_USR_DeviceAttached
*         Displays the message on LCD on device attached
* @param  None
* @retval None
*/
void USBH_USR_DeviceAttached(void)
{  

}

/**
* @brief  USBH_USR_UnrecoveredError
* @param  None
* @retval None
*/
void USBH_USR_UnrecoveredError (void)
{

}

/**
* @brief  USBH_DisconnectEvent
*         Device disconnect event
* @param  None
* @retval None
*/
void USBH_USR_DeviceDisconnected (void)
{
  memset(usb_key.io,NUM_KEYS,USBKEY_MAX);
  usb_key.changed = ON;
}

/**
* @brief  USBH_USR_ResetUSBDevice
*         Reset USB Device
* @param  None
* @retval None
*/
void USBH_USR_ResetDevice(void)
{
  /* Users can do their application actions here for the USB-Reset */
}


/**
* @brief  USBH_USR_DeviceSpeedDetected
*         Displays the message on LCD for device speed
* @param  Devicespeed : Device Speed
* @retval None
*/
void USBH_USR_DeviceSpeedDetected(uint8_t DeviceSpeed)
{
  if(DeviceSpeed == HPRT0_PRTSPD_HIGH_SPEED)
  {

  }  
  else if(DeviceSpeed == HPRT0_PRTSPD_FULL_SPEED)
  {

  }
  else if(DeviceSpeed == HPRT0_PRTSPD_LOW_SPEED)
  {

  }
  else
  {

  }
}

/**
* @brief  USBH_USR_Device_DescAvailable
*         Displays the message on LCD for device descriptor
* @param  DeviceDesc : device descriptor
* @retval None
*/
void USBH_USR_Device_DescAvailable(void *DeviceDesc)
{
  USBH_DevDesc_TypeDef *hs = (USBH_DevDesc_TypeDef *)DeviceDesc;
#if 0//def DEBUG_MODE
  uint8_t temp[50];
  u8 i ;

  i = sprintf((char *)temp , "VID:%04X,PID:%04X" , (uint32_t)(*hs).idVendor,(uint32_t)(*hs).idProduct);

  Host_Report_Event(HOST_EVENT_DEBUG,temp,i);
#endif
  if (hs->idVendor == 0x0C45 && hs->idProduct == 0x2602)
  {
  Usr_Device_Index = 0;
  }
  else
  {
    Usr_Device_Index = -1;
  }
}

/**
* @brief  USBH_USR_DeviceAddressAssigned
*         USB device is successfully assigned the Address
* @param  None
* @retval None
*/
void USBH_USR_DeviceAddressAssigned(void)
{
  #if 0//def DEBUG_MODE
  uint8_t temp[50];
  u8 i ;
  i = sprintf((char *)temp , "DeviceAddress" );
  Host_Report_Event(HOST_EVENT_DEBUG,temp,i);
#endif
}


/**
* @brief  USBH_USR_Conf_Desc
*         Displays the message on LCD for configuration descriptor
* @param  ConfDesc : Configuration descriptor
* @retval None
*/
void USBH_USR_Configuration_DescAvailable(USBH_CfgDesc_TypeDef * cfgDesc,
                                          USBH_InterfaceDesc_TypeDef *itfDesc,
                                          USBH_EpDesc_TypeDef *epDesc)
{
#if 0//def DEBUG_MODE
  uint8_t temp[50];
  u8 i ;  
  i = sprintf((char *)temp , "Configuration" );
  Host_Report_Event(HOST_EVENT_DEBUG,temp,i);
#endif

}

/**
* @brief  USBH_USR_Manufacturer_String
*         Displays the message on LCD for Manufacturer String
* @param  ManufacturerString : Manufacturer String of Device
* @retval None
*/
void USBH_USR_Manufacturer_String(void *ManufacturerString)
{
#if 0//def DEBUG_MODE
  u8 temp[100];
  u8 i;
  i = sprintf((char*)temp, "Manufacturer : %s\n", (char *)ManufacturerString);  
  Host_Report_Event(HOST_EVENT_DEBUG,temp,i);
#endif
}

/**
* @brief  USBH_USR_Product_String
*         Displays the message on LCD for Product String
* @param  ProductString : Product String of Device
* @retval None
*/
void USBH_USR_Product_String(void *ProductString)
{
#if 0//def DEBUG_MODE
  u8 temp[100];
  u8 i;
  i = sprintf((char*)temp, "Product: %s\n", (char *)ProductString);  
  Host_Report_Event(HOST_EVENT_DEBUG,temp,i);
#endif   
}

/**
* @brief  USBH_USR_SerialNum_String
*         Displays the message on LCD for SerialNum_String
* @param  SerialNumString : SerialNum_String of device
* @retval None
*/
void USBH_USR_SerialNum_String(void *SerialNumString)
{
#if 0//def DEBUG_MODE
  u8 temp[100];
  u8 i;
  i = sprintf((char*)temp, "SerialNum: %s\n", (char *)SerialNumString);  
  Host_Report_Event(HOST_EVENT_DEBUG,temp,i);
#endif   
}

/**
* @brief  EnumerationDone
*         User response request is displayed to ask for
*         application jump to class
* @param  None
* @retval None
*/
void USBH_USR_EnumerationDone(void)
{
#if 0//def DEBUG_MODE
  u8 temp[100];
  u8 i;
  i = sprintf((char*)temp, "Enumeration done");  
  Host_Report_Event(HOST_EVENT_DEBUG,temp,i);
#endif
  /* Enumeration complete */
}

/**
* @brief  USBH_USR_DeviceNotSupported
*         Device is not supported
* @param  None
* @retval None
*/
void USBH_USR_DeviceNotSupported(void)
{
#if 0//def DEBUG_MODE
  u8 temp[100];
  u8 i;
  i = sprintf((char*)temp, "NOT support");  
  Host_Report_Event(HOST_EVENT_DEBUG,temp,i);
#endif

//Host_Report_Event(HOST_EVENT_DEBUG,"NOT support",11);
}  

/**
* @brief  USBH_USR_UserInput
*         User Action for application state entry
* @param  None
* @retval USBH_USR_Status : User response for key button
*/
USBH_USR_Status USBH_USR_UserInput(void)
{
  USBH_USR_Status state;

  if (Sys_Setting.bOTGFlag == ON
  && Usr_Device_Index >= 0)
  {
   state = USBH_USR_RESP_OK;
  }
  else
  {
   state = USBH_USR_NO_RESP;
  }

  if (Usr_Otg_State != USR_OTG_OFF)
  {
  Usr_Otg_State = (state == USBH_USR_RESP_OK) ? USR_OTG_OK : USR_OTG_NOT_SUPPORT;
  Host_Report_Event(HOST_EVENT_KEY_OTG,&Usr_Otg_State,1);
  }

  return state;
}

/**
* @brief  USBH_USR_OverCurrentDetected
*         Device Overcurrent detection event
* @param  None
* @retval None
*/
void USBH_USR_OverCurrentDetected (void)
{


}

/**
* @brief  USR_KEYBRD_Init
*         Init Keyboard window
* @param  None
* @retval None
*/
static void  USR_KeyBoard_Init (void)
{
#if 0//def DEBUG_MODE
  u8 temp[100];
  u8 i;
  i = sprintf((char*)temp, "KeyBoard_Init");
  Host_Report_Event(HOST_EVENT_DEBUG,temp,i);
#endif

}


/**
* @brief  USR_KEYBRD_ProcessData
*         Process Keyboard data
* @param  data : Keyboard data to be displayed
* @retval None:按键解析函数,用户根据自己实际情况定义按键
*/
static void  USR_KeyBoard_Event0(uint8_t *data)
{
u8 i,key = NUM_KEYS;
OEM_TYPE changed = OFF;
if (data[0] == 1)
{
if (data[1] == 0x23 && data[2] == 0x02)
key = KEY_HOME;
else if (data[1] == 0x8A && data[2] == 0x00)
key = KEY_BACK;
else if (data[1] == 0xE2 && data[2] == 0x00)
key = KEY_MUTE;
else if (data[1] == 0xEA && data[2] == 0x00)
key = KEY_VOLDOWN;
else if (data[1] == 0xE9 && data[2] == 0x00)
key = KEY_VOLUP;
if (usb_key.io[0] != key)
{
usb_key.io[0] = key;
changed = ON;
}
}
else if (data[0] == 2)
{
if (data[1] == 0x01)
{
key = KEY_VOICECMD;
}
if (usb_key.io[1] != key)
{
usb_key.io[1] = key;
changed = ON;
}
}
else if (data[0] == 3)
{
for (i=3; i<8 && i<USBKEY_MAX; i++)
{
if (data == 0x28)
key = KEY_ENTER;
else if (data == 0x65)
key = KEY_MENU;
else if (data == 0x52)
key = KEY_UP;
else if (data == 0x51)
key = KEY_DOWN;
else if (data == 0x50)
key = KEY_LEFT;
else if (data == 0x4F)
key = KEY_RIGHT;
else if (data == 0x05)
key = KEY_PREV;
else if (data == 0x1B)
key = KEY_NEXT;
else if (data == 0x1C)
key = KEY_AUX;
else if (data == 0x04)
key = KEY_OFFLIGHT;
else
key = NUM_KEYS;

if (usb_key.io != key)
{
usb_key.io = key;
changed = ON;
}
}
}
usb_key.changed = changed;
if (usb_key.changed == ON)
{
SysTick_Delayms(10);
}
}

static void USR_KeyBoard_ProcessData (uint8_t *data)
{
#if 0//def DEBUG_MODE
  uint8_t temp[50];
  u8 i,count = 8 ;  
  u8 len;
  len = sprintf((char *)temp , "KEY:");
  for (i=0; i<count; i++)
  len += sprintf((char *)&temp[len] , "%02X," , data);

  Host_Report_Event(HOST_EVENT_DEBUG,temp,len);
#endif
if (Usr_Device_Index == 0)
{
   USR_KeyBoard_Event0(data);
}
}

//鼠标解析函数,用户根据自己实际情况定义按键

static void USR_Mouse_Event0(uint8_t *data)
{
static u8 pressed = 0;

if (data[0] == 0x01)
  return;

#ifdef DEBUG_MODE
if (data[1] == 0x02)
Beep(1);
#endif
if (Sys_Run.bRun_Task == TASK_CMMB)
{
if (data[1] == 0x01)
{
if (pressed == 0)
{
pressed = 1;
CMMB_Key_Process(KEY_ENTER);
}
}
else
{
pressed = 0;
}
}
else if (Sys_Run.bRun_Task == TASK_ANDROID)
{
Host_Report_Event(HOST_EVENT_MOUSE_MSG,&data[1],4);
}
}

/**
* @brief  USR_KEYBRD_Init
*         Init Keyboard window
* @param  None
* @retval None
*/
static void  USR_Mouse_Init (void)
{
//#ifdef DEBUG_MODE
  u8 temp[100];
  u8 i;
  i = sprintf((char*)temp, "Mouse_Init");
  Host_Report_Event(HOST_EVENT_DEBUG,temp,i);
//#endif

}


/**
* @brief  USR_KEYBRD_ProcessData
*         Process Keyboard data
* @param  data : Keyboard data to be displayed
* @retval None
*/
static void  USR_Mouse_ProcessData (uint8_t *data)
{
#if 0//def DEBUG_MODE  
  uint8_t temp[50];
  u8 len;

  if (data[0] == 0x01)
  return;

  len = 0;

  len += sprintf((char *)&temp[len] , "Button:%02X," , data[1]);

  len += sprintf((char *)&temp[len] , "X:%d," , (s8)data[2]);

  len += sprintf((char *)&temp[len] , "Y:%d," , (s8)data[3]);

  len += sprintf((char *)&temp[len] , "Z:%d" , (s8)data[4]);

  Host_Report_Event(HOST_EVENT_DEBUG,temp,len);
#endif

  if (Usr_Device_Index == 0)
  {
     USR_Mouse_Event0(data);
  }
}

HID_cb_TypeDef HID_KeyBoard_cb =
{
  /* Call User Init*/
  USR_KeyBoard_Init,       // void  (*Init)   (void);
  USR_KeyBoard_ProcessData // void  (*Decode) (uint8_t *data);               
        
};

HID_cb_TypeDef HID_Mouse_cb =
{
  /* Call User Init*/
  USR_Mouse_Init,
  USR_Mouse_ProcessData
};

/**
* @brief  USBH_USR_DeInit
*         Deint User state and associated variables
* @param  None
* @retval None
*/
void USBH_USR_DeInit(void)
{
}

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

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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