Initial commit of ST Cube HID drivers

USG_1.0
Robert Fisk 9 years ago
parent 8df2be05af
commit 08068c25d4

@ -0,0 +1,350 @@
/**
******************************************************************************
* @file usbh_hid.h
* @author MCD Application Team
* @version V3.2.2
* @date 07-July-2015
* @brief This file contains all the prototypes for the usbh_hid.c
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT 2015 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.
*
******************************************************************************
*/
/* Define to prevent recursive ----------------------------------------------*/
#ifndef __USBH_HID_H
#define __USBH_HID_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "usbh_core.h"
#include "usbh_hid_mouse.h"
#include "usbh_hid_keybd.h"
/** @addtogroup USBH_LIB
* @{
*/
/** @addtogroup USBH_CLASS
* @{
*/
/** @addtogroup USBH_HID_CLASS
* @{
*/
/** @defgroup USBH_HID_CORE
* @brief This file is the Header file for usbh_hid.c
* @{
*/
/** @defgroup USBH_HID_CORE_Exported_Types
* @{
*/
#define HID_MIN_POLL 10
#define HID_REPORT_SIZE 16
#define HID_MAX_USAGE 10
#define HID_MAX_NBR_REPORT_FMT 10
#define HID_QUEUE_SIZE 10
#define HID_ITEM_LONG 0xFE
#define HID_ITEM_TYPE_MAIN 0x00
#define HID_ITEM_TYPE_GLOBAL 0x01
#define HID_ITEM_TYPE_LOCAL 0x02
#define HID_ITEM_TYPE_RESERVED 0x03
#define HID_MAIN_ITEM_TAG_INPUT 0x08
#define HID_MAIN_ITEM_TAG_OUTPUT 0x09
#define HID_MAIN_ITEM_TAG_COLLECTION 0x0A
#define HID_MAIN_ITEM_TAG_FEATURE 0x0B
#define HID_MAIN_ITEM_TAG_ENDCOLLECTION 0x0C
#define HID_GLOBAL_ITEM_TAG_USAGE_PAGE 0x00
#define HID_GLOBAL_ITEM_TAG_LOG_MIN 0x01
#define HID_GLOBAL_ITEM_TAG_LOG_MAX 0x02
#define HID_GLOBAL_ITEM_TAG_PHY_MIN 0x03
#define HID_GLOBAL_ITEM_TAG_PHY_MAX 0x04
#define HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT 0x05
#define HID_GLOBAL_ITEM_TAG_UNIT 0x06
#define HID_GLOBAL_ITEM_TAG_REPORT_SIZE 0x07
#define HID_GLOBAL_ITEM_TAG_REPORT_ID 0x08
#define HID_GLOBAL_ITEM_TAG_REPORT_COUNT 0x09
#define HID_GLOBAL_ITEM_TAG_PUSH 0x0A
#define HID_GLOBAL_ITEM_TAG_POP 0x0B
#define HID_LOCAL_ITEM_TAG_USAGE 0x00
#define HID_LOCAL_ITEM_TAG_USAGE_MIN 0x01
#define HID_LOCAL_ITEM_TAG_USAGE_MAX 0x02
#define HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX 0x03
#define HID_LOCAL_ITEM_TAG_DESIGNATOR_MIN 0x04
#define HID_LOCAL_ITEM_TAG_DESIGNATOR_MAX 0x05
#define HID_LOCAL_ITEM_TAG_STRING_INDEX 0x07
#define HID_LOCAL_ITEM_TAG_STRING_MIN 0x08
#define HID_LOCAL_ITEM_TAG_STRING_MAX 0x09
#define HID_LOCAL_ITEM_TAG_DELIMITER 0x0A
/* States for HID State Machine */
typedef enum
{
HID_INIT= 0,
HID_IDLE,
HID_SEND_DATA,
HID_BUSY,
HID_GET_DATA,
HID_SYNC,
HID_POLL,
HID_ERROR,
}
HID_StateTypeDef;
typedef enum
{
HID_REQ_INIT = 0,
HID_REQ_IDLE,
HID_REQ_GET_REPORT_DESC,
HID_REQ_GET_HID_DESC,
HID_REQ_SET_IDLE,
HID_REQ_SET_PROTOCOL,
HID_REQ_SET_REPORT,
}
HID_CtlStateTypeDef;
typedef enum
{
HID_MOUSE = 0x01,
HID_KEYBOARD = 0x02,
HID_UNKNOWN = 0xFF,
}
HID_TypeTypeDef;
typedef struct _HID_ReportData
{
uint8_t ReportID;
uint8_t ReportType;
uint16_t UsagePage;
uint32_t Usage[HID_MAX_USAGE];
uint32_t NbrUsage;
uint32_t UsageMin;
uint32_t UsageMax;
int32_t LogMin;
int32_t LogMax;
int32_t PhyMin;
int32_t PhyMax;
int32_t UnitExp;
uint32_t Unit;
uint32_t ReportSize;
uint32_t ReportCnt;
uint32_t Flag;
uint32_t PhyUsage;
uint32_t AppUsage;
uint32_t LogUsage;
}
HID_ReportDataTypeDef;
typedef struct _HID_ReportIDTypeDef {
uint8_t Size; /* Report size return by the device id */
uint8_t ReportID; /* Report Id */
uint8_t Type; /* Report Type (INPUT/OUTPUT/FEATURE) */
} HID_ReportIDTypeDef;
typedef struct _HID_CollectionTypeDef
{
uint32_t Usage;
uint8_t Type;
struct _HID_CollectionTypeDef *NextPtr;
} HID_CollectionTypeDef;
typedef struct _HID_AppCollectionTypeDef {
uint32_t Usage;
uint8_t Type;
uint8_t NbrReportFmt;
HID_ReportDataTypeDef ReportData[HID_MAX_NBR_REPORT_FMT];
} HID_AppCollectionTypeDef;
typedef struct _HIDDescriptor
{
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bcdHID; /* indicates what endpoint this descriptor is describing */
uint8_t bCountryCode; /* specifies the transfer type. */
uint8_t bNumDescriptors; /* specifies the transfer type. */
uint8_t bReportDescriptorType; /* Maximum Packet Size this endpoint is capable of sending or receiving */
uint16_t wItemLength; /* is used to specify the polling interval of certain transfers. */
}
HID_DescTypeDef;
typedef struct
{
uint8_t *buf;
uint16_t head;
uint16_t tail;
uint16_t size;
uint8_t lock;
} FIFO_TypeDef;
/* Structure for HID process */
typedef struct _HID_Process
{
uint8_t OutPipe;
uint8_t InPipe;
HID_StateTypeDef state;
uint8_t OutEp;
uint8_t InEp;
HID_CtlStateTypeDef ctl_state;
FIFO_TypeDef fifo;
uint8_t *pData;
uint16_t length;
uint8_t ep_addr;
uint16_t poll;
uint32_t timer;
uint8_t DataReady;
HID_DescTypeDef HID_Desc;
USBH_StatusTypeDef ( * Init)(USBH_HandleTypeDef *phost);
}
HID_HandleTypeDef;
/**
* @}
*/
/** @defgroup USBH_HID_CORE_Exported_Defines
* @{
*/
#define USB_HID_GET_REPORT 0x01
#define USB_HID_GET_IDLE 0x02
#define USB_HID_GET_PROTOCOL 0x03
#define USB_HID_SET_REPORT 0x09
#define USB_HID_SET_IDLE 0x0A
#define USB_HID_SET_PROTOCOL 0x0B
/* HID Class Codes */
#define USB_HID_CLASS 0x03
/* Interface Descriptor field values for HID Boot Protocol */
#define HID_BOOT_CODE 0x01
#define HID_KEYBRD_BOOT_CODE 0x01
#define HID_MOUSE_BOOT_CODE 0x02
/**
* @}
*/
/** @defgroup USBH_HID_CORE_Exported_Macros
* @{
*/
/**
* @}
*/
/** @defgroup USBH_HID_CORE_Exported_Variables
* @{
*/
extern USBH_ClassTypeDef HID_Class;
#define USBH_HID_CLASS &HID_Class
/**
* @}
*/
/** @defgroup USBH_HID_CORE_Exported_FunctionsPrototype
* @{
*/
USBH_StatusTypeDef USBH_HID_SetReport (USBH_HandleTypeDef *phost,
uint8_t reportType,
uint8_t reportId,
uint8_t* reportBuff,
uint8_t reportLen);
USBH_StatusTypeDef USBH_HID_GetReport (USBH_HandleTypeDef *phost,
uint8_t reportType,
uint8_t reportId,
uint8_t* reportBuff,
uint8_t reportLen);
USBH_StatusTypeDef USBH_HID_GetHIDReportDescriptor (USBH_HandleTypeDef *phost,
uint16_t length);
USBH_StatusTypeDef USBH_HID_GetHIDDescriptor (USBH_HandleTypeDef *phost,
uint16_t length);
USBH_StatusTypeDef USBH_HID_SetIdle (USBH_HandleTypeDef *phost,
uint8_t duration,
uint8_t reportId);
USBH_StatusTypeDef USBH_HID_SetProtocol (USBH_HandleTypeDef *phost,
uint8_t protocol);
void USBH_HID_EventCallback(USBH_HandleTypeDef *phost);
HID_TypeTypeDef USBH_HID_GetDeviceType(USBH_HandleTypeDef *phost);
uint8_t USBH_HID_GetPollInterval(USBH_HandleTypeDef *phost);
void fifo_init(FIFO_TypeDef * f, uint8_t * buf, uint16_t size);
uint16_t fifo_read(FIFO_TypeDef * f, void * buf, uint16_t nbytes);
uint16_t fifo_write(FIFO_TypeDef * f, const void * buf, uint16_t nbytes);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __USBH_HID_H */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

@ -0,0 +1,326 @@
/**
******************************************************************************
* @file usbh_hid_keybd.h
* @author MCD Application Team
* @version V3.2.2
* @date 07-July-2015
* @brief This file contains all the prototypes for the usbh_hid_keybd.c
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT 2015 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.
*
******************************************************************************
*/
/* Define to prevent recursive -----------------------------------------------*/
#ifndef __USBH_HID_KEYBD_H
#define __USBH_HID_KEYBD_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "usbh_hid.h"
#include "usbh_hid_keybd.h"
/** @addtogroup USBH_LIB
* @{
*/
/** @addtogroup USBH_CLASS
* @{
*/
/** @addtogroup USBH_HID_CLASS
* @{
*/
/** @defgroup USBH_HID_KEYBD
* @brief This file is the Header file for usbh_hid_keybd.c
* @{
*/
/** @defgroup USBH_HID_KEYBD_Exported_Types
* @{
*/
#define KEY_NONE 0x00
#define KEY_ERRORROLLOVER 0x01
#define KEY_POSTFAIL 0x02
#define KEY_ERRORUNDEFINED 0x03
#define KEY_A 0x04
#define KEY_B 0x05
#define KEY_C 0x06
#define KEY_D 0x07
#define KEY_E 0x08
#define KEY_F 0x09
#define KEY_G 0x0A
#define KEY_H 0x0B
#define KEY_I 0x0C
#define KEY_J 0x0D
#define KEY_K 0x0E
#define KEY_L 0x0F
#define KEY_M 0x10
#define KEY_N 0x11
#define KEY_O 0x12
#define KEY_P 0x13
#define KEY_Q 0x14
#define KEY_R 0x15
#define KEY_S 0x16
#define KEY_T 0x17
#define KEY_U 0x18
#define KEY_V 0x19
#define KEY_W 0x1A
#define KEY_X 0x1B
#define KEY_Y 0x1C
#define KEY_Z 0x1D
#define KEY_1_EXCLAMATION_MARK 0x1E
#define KEY_2_AT 0x1F
#define KEY_3_NUMBER_SIGN 0x20
#define KEY_4_DOLLAR 0x21
#define KEY_5_PERCENT 0x22
#define KEY_6_CARET 0x23
#define KEY_7_AMPERSAND 0x24
#define KEY_8_ASTERISK 0x25
#define KEY_9_OPARENTHESIS 0x26
#define KEY_0_CPARENTHESIS 0x27
#define KEY_ENTER 0x28
#define KEY_ESCAPE 0x29
#define KEY_BACKSPACE 0x2A
#define KEY_TAB 0x2B
#define KEY_SPACEBAR 0x2C
#define KEY_MINUS_UNDERSCORE 0x2D
#define KEY_EQUAL_PLUS 0x2E
#define KEY_OBRACKET_AND_OBRACE 0x2F
#define KEY_CBRACKET_AND_CBRACE 0x30
#define KEY_BACKSLASH_VERTICAL_BAR 0x31
#define KEY_NONUS_NUMBER_SIGN_TILDE 0x32
#define KEY_SEMICOLON_COLON 0x33
#define KEY_SINGLE_AND_DOUBLE_QUOTE 0x34
#define KEY_GRAVE ACCENT AND TILDE 0x35
#define KEY_COMMA_AND_LESS 0x36
#define KEY_DOT_GREATER 0x37
#define KEY_SLASH_QUESTION 0x38
#define KEY_CAPS LOCK 0x39
#define KEY_F1 0x3A
#define KEY_F2 0x3B
#define KEY_F3 0x3C
#define KEY_F4 0x3D
#define KEY_F5 0x3E
#define KEY_F6 0x3F
#define KEY_F7 0x40
#define KEY_F8 0x41
#define KEY_F9 0x42
#define KEY_F10 0x43
#define KEY_F11 0x44
#define KEY_F12 0x45
#define KEY_PRINTSCREEN 0x46
#define KEY_SCROLL LOCK 0x47
#define KEY_PAUSE 0x48
#define KEY_INSERT 0x49
#define KEY_HOME 0x4A
#define KEY_PAGEUP 0x4B
#define KEY_DELETE 0x4C
#define KEY_END1 0x4D
#define KEY_PAGEDOWN 0x4E
#define KEY_RIGHTARROW 0x4F
#define KEY_LEFTARROW 0x50
#define KEY_DOWNARROW 0x51
#define KEY_UPARROW 0x52
#define KEY_KEYPAD_NUM_LOCK_AND_CLEAR 0x53
#define KEY_KEYPAD_SLASH 0x54
#define KEY_KEYPAD_ASTERIKS 0x55
#define KEY_KEYPAD_MINUS 0x56
#define KEY_KEYPAD_PLUS 0x57
#define KEY_KEYPAD_ENTER 0x58
#define KEY_KEYPAD_1_END 0x59
#define KEY_KEYPAD_2_DOWN_ARROW 0x5A
#define KEY_KEYPAD_3_PAGEDN 0x5B
#define KEY_KEYPAD_4_LEFT_ARROW 0x5C
#define KEY_KEYPAD_5 0x5D
#define KEY_KEYPAD_6_RIGHT_ARROW 0x5E
#define KEY_KEYPAD_7_HOME 0x5F
#define KEY_KEYPAD_8_UP_ARROW 0x60
#define KEY_KEYPAD_9_PAGEUP 0x61
#define KEY_KEYPAD_0_INSERT 0x62
#define KEY_KEYPAD_DECIMAL_SEPARATOR_DELETE 0x63
#define KEY_NONUS_BACK_SLASH_VERTICAL_BAR 0x64
#define KEY_APPLICATION 0x65
#define KEY_POWER 0x66
#define KEY_KEYPAD_EQUAL 0x67
#define KEY_F13 0x68
#define KEY_F14 0x69
#define KEY_F15 0x6A
#define KEY_F16 0x6B
#define KEY_F17 0x6C
#define KEY_F18 0x6D
#define KEY_F19 0x6E
#define KEY_F20 0x6F
#define KEY_F21 0x70
#define KEY_F22 0x71
#define KEY_F23 0x72
#define KEY_F24 0x73
#define KEY_EXECUTE 0x74
#define KEY_HELP 0x75
#define KEY_MENU 0x76
#define KEY_SELECT 0x77
#define KEY_STOP 0x78
#define KEY_AGAIN 0x79
#define KEY_UNDO 0x7A
#define KEY_CUT 0x7B
#define KEY_COPY 0x7C
#define KEY_PASTE 0x7D
#define KEY_FIND 0x7E
#define KEY_MUTE 0x7F
#define KEY_VOLUME_UP 0x80
#define KEY_VOLUME_DOWN 0x81
#define KEY_LOCKING_CAPS_LOCK 0x82
#define KEY_LOCKING_NUM_LOCK 0x83
#define KEY_LOCKING_SCROLL_LOCK 0x84
#define KEY_KEYPAD_COMMA 0x85
#define KEY_KEYPAD_EQUAL_SIGN 0x86
#define KEY_INTERNATIONAL1 0x87
#define KEY_INTERNATIONAL2 0x88
#define KEY_INTERNATIONAL3 0x89
#define KEY_INTERNATIONAL4 0x8A
#define KEY_INTERNATIONAL5 0x8B
#define KEY_INTERNATIONAL6 0x8C
#define KEY_INTERNATIONAL7 0x8D
#define KEY_INTERNATIONAL8 0x8E
#define KEY_INTERNATIONAL9 0x8F
#define KEY_LANG1 0x90
#define KEY_LANG2 0x91
#define KEY_LANG3 0x92
#define KEY_LANG4 0x93
#define KEY_LANG5 0x94
#define KEY_LANG6 0x95
#define KEY_LANG7 0x96
#define KEY_LANG8 0x97
#define KEY_LANG9 0x98
#define KEY_ALTERNATE_ERASE 0x99
#define KEY_SYSREQ 0x9A
#define KEY_CANCEL 0x9B
#define KEY_CLEAR 0x9C
#define KEY_PRIOR 0x9D
#define KEY_RETURN 0x9E
#define KEY_SEPARATOR 0x9F
#define KEY_OUT 0xA0
#define KEY_OPER 0xA1
#define KEY_CLEAR_AGAIN 0xA2
#define KEY_CRSEL 0xA3
#define KEY_EXSEL 0xA4
#define KEY_KEYPAD_00 0xB0
#define KEY_KEYPAD_000 0xB1
#define KEY_THOUSANDS_SEPARATOR 0xB2
#define KEY_DECIMAL_SEPARATOR 0xB3
#define KEY_CURRENCY_UNIT 0xB4
#define KEY_CURRENCY_SUB_UNIT 0xB5
#define KEY_KEYPAD_OPARENTHESIS 0xB6
#define KEY_KEYPAD_CPARENTHESIS 0xB7
#define KEY_KEYPAD_OBRACE 0xB8
#define KEY_KEYPAD_CBRACE 0xB9
#define KEY_KEYPAD_TAB 0xBA
#define KEY_KEYPAD_BACKSPACE 0xBB
#define KEY_KEYPAD_A 0xBC
#define KEY_KEYPAD_B 0xBD
#define KEY_KEYPAD_C 0xBE
#define KEY_KEYPAD_D 0xBF
#define KEY_KEYPAD_E 0xC0
#define KEY_KEYPAD_F 0xC1
#define KEY_KEYPAD_XOR 0xC2
#define KEY_KEYPAD_CARET 0xC3
#define KEY_KEYPAD_PERCENT 0xC4
#define KEY_KEYPAD_LESS 0xC5
#define KEY_KEYPAD_GREATER 0xC6
#define KEY_KEYPAD_AMPERSAND 0xC7
#define KEY_KEYPAD_LOGICAL_AND 0xC8
#define KEY_KEYPAD_VERTICAL_BAR 0xC9
#define KEY_KEYPAD_LOGIACL_OR 0xCA
#define KEY_KEYPAD_COLON 0xCB
#define KEY_KEYPAD_NUMBER_SIGN 0xCC
#define KEY_KEYPAD_SPACE 0xCD
#define KEY_KEYPAD_AT 0xCE
#define KEY_KEYPAD_EXCLAMATION_MARK 0xCF
#define KEY_KEYPAD_MEMORY_STORE 0xD0
#define KEY_KEYPAD_MEMORY_RECALL 0xD1
#define KEY_KEYPAD_MEMORY_CLEAR 0xD2
#define KEY_KEYPAD_MEMORY_ADD 0xD3
#define KEY_KEYPAD_MEMORY_SUBTRACT 0xD4
#define KEY_KEYPAD_MEMORY_MULTIPLY 0xD5
#define KEY_KEYPAD_MEMORY_DIVIDE 0xD6
#define KEY_KEYPAD_PLUSMINUS 0xD7
#define KEY_KEYPAD_CLEAR 0xD8
#define KEY_KEYPAD_CLEAR_ENTRY 0xD9
#define KEY_KEYPAD_BINARY 0xDA
#define KEY_KEYPAD_OCTAL 0xDB
#define KEY_KEYPAD_DECIMAL 0xDC
#define KEY_KEYPAD_HEXADECIMAL 0xDD
#define KEY_LEFTCONTROL 0xE0
#define KEY_LEFTSHIFT 0xE1
#define KEY_LEFTALT 0xE2
#define KEY_LEFT_GUI 0xE3
#define KEY_RIGHTCONTROL 0xE4
#define KEY_RIGHTSHIFT 0xE5
#define KEY_RIGHTALT 0xE6
#define KEY_RIGHT_GUI 0xE7
typedef struct
{
uint8_t state;
uint8_t lctrl;
uint8_t lshift;
uint8_t lalt;
uint8_t lgui;
uint8_t rctrl;
uint8_t rshift;
uint8_t ralt;
uint8_t rgui;
uint8_t keys[6];
}
HID_KEYBD_Info_TypeDef;
USBH_StatusTypeDef USBH_HID_KeybdInit(USBH_HandleTypeDef *phost);
HID_KEYBD_Info_TypeDef *USBH_HID_GetKeybdInfo(USBH_HandleTypeDef *phost);
uint8_t USBH_HID_GetASCIICode(HID_KEYBD_Info_TypeDef *info);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __USBH_HID_KEYBD_H */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

@ -0,0 +1,125 @@
/**
******************************************************************************
* @file usbh_hid_mouse.h
* @author MCD Application Team
* @version V3.2.2
* @date 07-July-2015
* @brief This file contains all the prototypes for the usbh_hid_mouse.c
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT 2015 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.
*
******************************************************************************
*/
/* Define to prevent recursive ----------------------------------------------*/
#ifndef __USBH_HID_MOUSE_H
#define __USBH_HID_MOUSE_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "usbh_hid.h"
/** @addtogroup USBH_LIB
* @{
*/
/** @addtogroup USBH_CLASS
* @{
*/
/** @addtogroup USBH_HID_CLASS
* @{
*/
/** @defgroup USBH_HID_MOUSE
* @brief This file is the Header file for usbh_hid_mouse.c
* @{
*/
/** @defgroup USBH_HID_MOUSE_Exported_Types
* @{
*/
typedef struct _HID_MOUSE_Info
{
uint8_t x;
uint8_t y;
uint8_t buttons[3];
}
HID_MOUSE_Info_TypeDef;
/**
* @}
*/
/** @defgroup USBH_HID_MOUSE_Exported_Defines
* @{
*/
/**
* @}
*/
/** @defgroup USBH_HID_MOUSE_Exported_Macros
* @{
*/
/**
* @}
*/
/** @defgroup USBH_HID_MOUSE_Exported_Variables
* @{
*/
/**
* @}
*/
/** @defgroup USBH_HID_MOUSE_Exported_FunctionsPrototype
* @{
*/
USBH_StatusTypeDef USBH_HID_MouseInit(USBH_HandleTypeDef *phost);
HID_MOUSE_Info_TypeDef *USBH_HID_GetMouseInfo(USBH_HandleTypeDef *phost);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __USBH_HID_MOUSE_H */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

@ -0,0 +1,106 @@
/**
******************************************************************************
* @file usbh_hid_parser.c
* @author MCD Application Team
* @version V3.2.2
* @date 07-July-2015
* @brief This file is the header file of the usbh_hid_parser.c
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT 2015 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.
*
******************************************************************************
*/
/* Define to prevent recursive -----------------------------------------------*/
#ifndef __USBH_HID_PARSER_H
#define __USBH_HID_PARSER_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "usbh_hid.h"
#include "usbh_hid_usage.h"
/** @addtogroup USBH_LIB
* @{
*/
/** @addtogroup USBH_CLASS
* @{
*/
/** @addtogroup USBH_HID_CLASS
* @{
*/
/** @defgroup USBH_HID_PARSER
* @brief This file is the Header file for usbh_hid_parser.c
* @{
*/
/** @defgroup USBH_HID_PARSER_Exported_Types
* @{
*/
typedef struct
{
uint8_t *data;
uint32_t size;
uint8_t shift;
uint8_t count;
uint8_t sign;
uint32_t logical_min; /*min value device can return*/
uint32_t logical_max; /*max value device can return*/
uint32_t physical_min; /*min vale read can report*/
uint32_t physical_max; /*max value read can report*/
uint32_t resolution;
}
HID_Report_ItemTypedef;
uint32_t HID_ReadItem (HID_Report_ItemTypedef *ri, uint8_t ndx);
uint32_t HID_WriteItem(HID_Report_ItemTypedef *ri, uint32_t value, uint8_t ndx);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __USBH_HID_PARSER_H */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

@ -0,0 +1,200 @@
/**
******************************************************************************
* @file usbh_hid_usage.c
* @author MCD Application Team
* @version V3.2.2
* @date 07-July-2015
* @brief This file contain the USAGE page codes
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT 2015 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.
*
******************************************************************************
*/
/* Define to prevent recursive ----------------------------------------------*/
#ifndef __USDH_HID_USAGE_H
#define __USDH_HID_USAGE_H
#ifdef __cplusplus
extern "C" {
#endif
/** @addtogroup USBH_LIB
* @{
*/
/** @addtogroup USBH_CLASS
* @{
*/
/** @addtogroup USBH_HID_CLASS
* @{
*/
/** @defgroup USBH_HID_USAGE
* @brief This file is the Header file for usbh_hid_usage.c
* @{
*/
/** @defgroup USBH_HID_USAGE_Exported_Types
* @{
*/
/****************************************************/
/* HID 1.11 usage pages */
/****************************************************/
#define HID_USAGE_PAGE_UNDEFINED uint16_t (0x00) /* Undefined */
/**** Top level pages */
#define HID_USAGE_PAGE_GEN_DES uint16_t (0x01) /* Generic Desktop Controls*/
#define HID_USAGE_PAGE_SIM_CTR uint16_t (0x02) /* Simulation Controls */
#define HID_USAGE_PAGE_VR_CTR uint16_t (0x03) /* VR Controls */
#define HID_USAGE_PAGE_SPORT_CTR uint16_t (0x04) /* Sport Controls */
#define HID_USAGE_PAGE_GAME_CTR uint16_t (0x05) /* Game Controls */
#define HID_USAGE_PAGE_GEN_DEV uint16_t (0x06) /* Generic Device Controls */
#define HID_USAGE_PAGE_KEYB uint16_t (0x07) /* Keyboard/Keypad */
#define HID_USAGE_PAGE_LED uint16_t (0x08) /* LEDs */
#define HID_USAGE_PAGE_BUTTON uint16_t (0x09) /* Button */
#define HID_USAGE_PAGE_ORDINAL uint16_t (0x0A) /* Ordinal */
#define HID_USAGE_PAGE_PHONE uint16_t (0x0B) /* Telephony */
#define HID_USAGE_PAGE_CONSUMER uint16_t (0x0C) /* Consumer */
#define HID_USAGE_PAGE_DIGITIZER uint16_t (0x0D) /* Digitizer*/
/* 0E Reserved */
#define HID_USAGE_PAGE_PID uint16_t (0x0F) /* PID Page (force feedback and related devices) */
#define HID_USAGE_PAGE_UNICODE uint16_t (0x10) /* Unicode */
/* 11-13 Reserved */
#define HID_USAGE_PAGE_ALNUM_DISP uint16_t (0x14) /* Alphanumeric Display */
/* 15-1f Reserved */
/**** END of top level pages */
/* 25-3f Reserved */
#define HID_USAGE_PAGE_MEDICAL uint16_t (0x40) /* Medical Instruments */
/* 41-7F Reserved */
/*80-83 Monitor pages USB Device Class Definition for Monitor Devices
84-87 Power pages USB Device Class Definition for Power Devices */
/* 88-8B Reserved */
#define HID_USAGE_PAGE_BARCODE uint16_t (0x8C) /* Bar Code Scanner page */
#define HID_USAGE_PAGE_SCALE uint16_t (0x8D) /* Scale page */
#define HID_USAGE_PAGE_MSR uint16_t (0x8E) /* Magnetic Stripe Reading (MSR) Devices */
#define HID_USAGE_PAGE_POS uint16_t (0x8F) /* Reserved Point of Sale pages */
#define HID_USAGE_PAGE_CAMERA_CTR uint16_t (0x90) /* Camera Control Page */
#define HID_USAGE_PAGE_ARCADE uint16_t (0x91) /* Arcade Page */
/****************************************************/
/* Usage definitions for the "Generic Desktop" page */
/****************************************************/
#define HID_USAGE_UNDEFINED uint16_t (0x00) /* Undefined */
#define HID_USAGE_POINTER uint16_t (0x01) /* Pointer (Physical Collection) */
#define HID_USAGE_MOUSE uint16_t (0x02) /* Mouse (Application Collection) */
/* 03 Reserved */
#define HID_USAGE_JOYSTICK uint16_t (0x04) /* Joystick (Application Collection) */
#define HID_USAGE_GAMEPAD uint16_t (0x05) /* Game Pad (Application Collection) */
#define HID_USAGE_KBD uint16_t (0x06) /* Keyboard (Application Collection) */
#define HID_USAGE_KEYPAD uint16_t (0x07) /* Keypad (Application Collection) */
#define HID_USAGE_MAX_CTR uint16_t (0x08) /* Multi-axis Controller (Application Collection) */
/* 09-2F Reserved */
#define HID_USAGE_X uint16_t (0x30) /* X (Dynamic Value) */
#define HID_USAGE_Y uint16_t (0x31) /* Y (Dynamic Value) */
#define HID_USAGE_Z uint16_t (0x32) /* Z (Dynamic Value) */
#define HID_USAGE_RX uint16_t (0x33) /* Rx (Dynamic Value) */
#define HID_USAGE_RY uint16_t (0x34) /* Ry (Dynamic Value) */
#define HID_USAGE_RZ uint16_t (0x35) /* Rz (Dynamic Value) */
#define HID_USAGE_SLIDER uint16_t (0x36) /* Slider (Dynamic Value) */
#define HID_USAGE_DIAL uint16_t (0x37) /* Dial (Dynamic Value) */
#define HID_USAGE_WHEEL uint16_t (0x38) /* Wheel (Dynamic Value) */
#define HID_USAGE_HATSW uint16_t (0x39) /* Hat switch (Dynamic Value) */
#define HID_USAGE_COUNTEDBUF uint16_t (0x3A) /* Counted Buffer (Logical Collection) */
#define HID_USAGE_BYTECOUNT uint16_t (0x3B) /* Byte Count (Dynamic Value) */
#define HID_USAGE_MOTIONWAKE uint16_t (0x3C) /* Motion Wakeup (One Shot Control) */
#define HID_USAGE_START uint16_t (0x3D) /* Start (On/Off Control) */
#define HID_USAGE_SELECT uint16_t (0x3E) /* Select (On/Off Control) */
/* 3F Reserved */
#define HID_USAGE_VX uint16_t (0x40) /* Vx (Dynamic Value) */
#define HID_USAGE_VY uint16_t (0x41) /* Vy (Dynamic Value) */
#define HID_USAGE_VZ uint16_t (0x42) /* Vz (Dynamic Value) */
#define HID_USAGE_VBRX uint16_t (0x43) /* Vbrx (Dynamic Value) */
#define HID_USAGE_VBRY uint16_t (0x44) /* Vbry (Dynamic Value) */
#define HID_USAGE_VBRZ uint16_t (0x45) /* Vbrz (Dynamic Value) */
#define HID_USAGE_VNO uint16_t (0x46) /* Vno (Dynamic Value) */
#define HID_USAGE_FEATNOTIF uint16_t (0x47) /* Feature Notification (Dynamic Value),(Dynamic Flag) */
/* 48-7F Reserved */
#define HID_USAGE_SYSCTL uint16_t (0x80) /* System Control (Application Collection) */
#define HID_USAGE_PWDOWN uint16_t (0x81) /* System Power Down (One Shot Control) */
#define HID_USAGE_SLEEP uint16_t (0x82) /* System Sleep (One Shot Control) */
#define HID_USAGE_WAKEUP uint16_t (0x83) /* System Wake Up (One Shot Control) */
#define HID_USAGE_CONTEXTM uint16_t (0x84) /* System Context Menu (One Shot Control) */
#define HID_USAGE_MAINM uint16_t (0x85) /* System Main Menu (One Shot Control) */
#define HID_USAGE_APPM uint16_t (0x86) /* System App Menu (One Shot Control) */
#define HID_USAGE_MENUHELP uint16_t (0x87) /* System Menu Help (One Shot Control) */
#define HID_USAGE_MENUEXIT uint16_t (0x88) /* System Menu Exit (One Shot Control) */
#define HID_USAGE_MENUSELECT uint16_t (0x89) /* System Menu Select (One Shot Control) */
#define HID_USAGE_SYSM_RIGHT uint16_t (0x8A) /* System Menu Right (Re-Trigger Control) */
#define HID_USAGE_SYSM_LEFT uint16_t (0x8B) /* System Menu Left (Re-Trigger Control) */
#define HID_USAGE_SYSM_UP uint16_t (0x8C) /* System Menu Up (Re-Trigger Control) */
#define HID_USAGE_SYSM_DOWN uint16_t (0x8D) /* System Menu Down (Re-Trigger Control) */
#define HID_USAGE_COLDRESET uint16_t (0x8E) /* System Cold Restart (One Shot Control) */
#define HID_USAGE_WARMRESET uint16_t (0x8F) /* System Warm Restart (One Shot Control) */
#define HID_USAGE_DUP uint16_t (0x90) /* D-pad Up (On/Off Control) */
#define HID_USAGE_DDOWN uint16_t (0x91) /* D-pad Down (On/Off Control) */
#define HID_USAGE_DRIGHT uint16_t (0x92) /* D-pad Right (On/Off Control) */
#define HID_USAGE_DLEFT uint16_t (0x93) /* D-pad Left (On/Off Control) */
/* 94-9F Reserved */
#define HID_USAGE_SYS_DOCK uint16_t (0xA0) /* System Dock (One Shot Control) */
#define HID_USAGE_SYS_UNDOCK uint16_t (0xA1) /* System Undock (One Shot Control) */
#define HID_USAGE_SYS_SETUP uint16_t (0xA2) /* System Setup (One Shot Control) */
#define HID_USAGE_SYS_BREAK uint16_t (0xA3) /* System Break (One Shot Control) */
#define HID_USAGE_SYS_DBGBRK uint16_t (0xA4) /* System Debugger Break (One Shot Control) */
#define HID_USAGE_APP_BRK uint16_t (0xA5) /* Application Break (One Shot Control) */
#define HID_USAGE_APP_DBGBRK uint16_t (0xA6) /* Application Debugger Break (One Shot Control) */
#define HID_USAGE_SYS_SPKMUTE uint16_t (0xA7) /* System Speaker Mute (One Shot Control) */
#define HID_USAGE_SYS_HIBERN uint16_t (0xA8) /* System Hibernate (One Shot Control) */
/* A9-AF Reserved */
#define HID_USAGE_SYS_SIDPINV uint16_t (0xB0) /* System Display Invert (One Shot Control) */
#define HID_USAGE_SYS_DISPINT uint16_t (0xB1) /* System Display Internal (One Shot Control) */
#define HID_USAGE_SYS_DISPEXT uint16_t (0xB2) /* System Display External (One Shot Control) */
#define HID_USAGE_SYS_DISPBOTH uint16_t (0xB3) /* System Display Both (One Shot Control) */
#define HID_USAGE_SYS_DISPDUAL uint16_t (0xB4) /* System Display Dual (One Shot Control) */
#define HID_USAGE_SYS_DISPTGLIE uint16_t (0xB5) /* System Display Toggle Int/Ext (One Shot Control) */
#define HID_USAGE_SYS_DISP_SWAP uint16_t (0xB6) /* System Display Swap Primary/Secondary (One Shot Control) */
#define HID_USAGE_SYS_DIPS_LCDA uint16_t (0xB7) /* System Display LCD Autoscale (One Shot Control) */
/* B8-FFFF Reserved */
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __USDH_HID_USAGE_H */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

@ -0,0 +1,824 @@
/**
******************************************************************************
* @file usbh_hid.c
* @author MCD Application Team
* @version V3.2.2
* @date 07-July-2015
* @brief This file is the HID Layer Handlers for USB Host HID class.
*
* @verbatim
*
* ===================================================================
* HID Class Description
* ===================================================================
* This module manages the HID class V1.11 following the "Device Class Definition
* for Human Interface Devices (HID) Version 1.11 Jun 27, 2001".
* This driver implements the following aspects of the specification:
* - The Boot Interface Subclass
* - The Mouse and Keyboard protocols
*
* @endverbatim
*
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT 2015 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_hid.h"
#include "usbh_hid_parser.h"
/** @addtogroup USBH_LIB
* @{
*/
/** @addtogroup USBH_CLASS
* @{
*/
/** @addtogroup USBH_HID_CLASS
* @{
*/
/** @defgroup USBH_HID_CORE
* @brief This file includes HID Layer Handlers for USB Host HID class.
* @{
*/
/** @defgroup USBH_HID_CORE_Private_TypesDefinitions
* @{
*/
/**
* @}
*/
/** @defgroup USBH_HID_CORE_Private_Defines
* @{
*/
/**
* @}
*/
/** @defgroup USBH_HID_CORE_Private_Macros
* @{
*/
/**
* @}
*/
/** @defgroup USBH_HID_CORE_Private_Variables
* @{
*/
/**
* @}
*/
/** @defgroup USBH_HID_CORE_Private_FunctionPrototypes
* @{
*/
static USBH_StatusTypeDef USBH_HID_InterfaceInit (USBH_HandleTypeDef *phost);
static USBH_StatusTypeDef USBH_HID_InterfaceDeInit (USBH_HandleTypeDef *phost);
static USBH_StatusTypeDef USBH_HID_ClassRequest(USBH_HandleTypeDef *phost);
static USBH_StatusTypeDef USBH_HID_Process(USBH_HandleTypeDef *phost);
static USBH_StatusTypeDef USBH_HID_SOFProcess(USBH_HandleTypeDef *phost);
static void USBH_HID_ParseHIDDesc (HID_DescTypeDef *desc, uint8_t *buf);
extern USBH_StatusTypeDef USBH_HID_MouseInit(USBH_HandleTypeDef *phost);
extern USBH_StatusTypeDef USBH_HID_KeybdInit(USBH_HandleTypeDef *phost);
USBH_ClassTypeDef HID_Class =
{
"HID",
USB_HID_CLASS,
USBH_HID_InterfaceInit,
USBH_HID_InterfaceDeInit,
USBH_HID_ClassRequest,
USBH_HID_Process,
USBH_HID_SOFProcess,
NULL,
};
/**
* @}
*/
/** @defgroup USBH_HID_CORE_Private_Functions
* @{
*/
/**
* @brief USBH_HID_InterfaceInit
* The function init the HID class.
* @param phost: Host handle
* @retval USBH Status
*/
static USBH_StatusTypeDef USBH_HID_InterfaceInit (USBH_HandleTypeDef *phost)
{
uint8_t max_ep;
uint8_t num = 0;
uint8_t interface;
USBH_StatusTypeDef status = USBH_FAIL ;
HID_HandleTypeDef *HID_Handle;
interface = USBH_FindInterface(phost, phost->pActiveClass->ClassCode, HID_BOOT_CODE, 0xFF);
if(interface == 0xFF) /* No Valid Interface */
{
status = USBH_FAIL;
USBH_DbgLog ("Cannot Find the interface for %s class.", phost->pActiveClass->Name);
}
else
{
USBH_SelectInterface (phost, interface);
phost->pActiveClass->pData = (HID_HandleTypeDef *)USBH_malloc (sizeof(HID_HandleTypeDef));
HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
HID_Handle->state = HID_ERROR;
/*Decode Bootclass Protocol: Mouse or Keyboard*/
if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bInterfaceProtocol == HID_KEYBRD_BOOT_CODE)
{
USBH_UsrLog ("KeyBoard device found!");
HID_Handle->Init = USBH_HID_KeybdInit;
}
else if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bInterfaceProtocol == HID_MOUSE_BOOT_CODE)
{
USBH_UsrLog ("Mouse device found!");
HID_Handle->Init = USBH_HID_MouseInit;
}
else
{
USBH_UsrLog ("Protocol not supported.");
return USBH_FAIL;
}
HID_Handle->state = HID_INIT;
HID_Handle->ctl_state = HID_REQ_INIT;
HID_Handle->ep_addr = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].bEndpointAddress;
HID_Handle->length = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].wMaxPacketSize;
HID_Handle->poll = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].bInterval ;
if (HID_Handle->poll < HID_MIN_POLL)
{
HID_Handle->poll = HID_MIN_POLL;
}
/* Check fo available number of endpoints */
/* Find the number of EPs in the Interface Descriptor */
/* Choose the lower number in order not to overrun the buffer allocated */
max_ep = ( (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bNumEndpoints <= USBH_MAX_NUM_ENDPOINTS) ?
phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bNumEndpoints :
USBH_MAX_NUM_ENDPOINTS);
/* Decode endpoint IN and OUT address from interface descriptor */
for ( ;num < max_ep; num++)
{
if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[num].bEndpointAddress & 0x80)
{
HID_Handle->InEp = (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[num].bEndpointAddress);
HID_Handle->InPipe =\
USBH_AllocPipe(phost, HID_Handle->InEp);
/* Open pipe for IN endpoint */
USBH_OpenPipe (phost,
HID_Handle->InPipe,
HID_Handle->InEp,
phost->device.address,
phost->device.speed,
USB_EP_TYPE_INTR,
HID_Handle->length);
USBH_LL_SetToggle (phost, HID_Handle->InPipe, 0);
}
else
{
HID_Handle->OutEp = (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[num].bEndpointAddress);
HID_Handle->OutPipe =\
USBH_AllocPipe(phost, HID_Handle->OutEp);
/* Open pipe for OUT endpoint */
USBH_OpenPipe (phost,
HID_Handle->OutPipe,
HID_Handle->OutEp,
phost->device.address,
phost->device.speed,
USB_EP_TYPE_INTR,
HID_Handle->length);
USBH_LL_SetToggle (phost, HID_Handle->OutPipe, 0);
}
}
status = USBH_OK;
}
return status;
}
/**
* @brief USBH_HID_InterfaceDeInit
* The function DeInit the Pipes used for the HID class.
* @param phost: Host handle
* @retval USBH Status
*/
USBH_StatusTypeDef USBH_HID_InterfaceDeInit (USBH_HandleTypeDef *phost )
{
HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
if(HID_Handle->InPipe != 0x00)
{
USBH_ClosePipe (phost, HID_Handle->InPipe);
USBH_FreePipe (phost, HID_Handle->InPipe);
HID_Handle->InPipe = 0; /* Reset the pipe as Free */
}
if(HID_Handle->OutPipe != 0x00)
{
USBH_ClosePipe(phost, HID_Handle->OutPipe);
USBH_FreePipe (phost, HID_Handle->OutPipe);
HID_Handle->OutPipe = 0; /* Reset the pipe as Free */
}
if(phost->pActiveClass->pData)
{
USBH_free (phost->pActiveClass->pData);
}
return USBH_OK;
}
/**
* @brief USBH_HID_ClassRequest
* The function is responsible for handling Standard requests
* for HID class.
* @param phost: Host handle
* @retval USBH Status
*/
static USBH_StatusTypeDef USBH_HID_ClassRequest(USBH_HandleTypeDef *phost)
{
USBH_StatusTypeDef status = USBH_BUSY;
USBH_StatusTypeDef classReqStatus = USBH_BUSY;
HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
/* Switch HID state machine */
switch (HID_Handle->ctl_state)
{
case HID_REQ_INIT:
case HID_REQ_GET_HID_DESC:
/* Get HID Desc */
if (USBH_HID_GetHIDDescriptor (phost, USB_HID_DESC_SIZE)== USBH_OK)
{
USBH_HID_ParseHIDDesc(&HID_Handle->HID_Desc, phost->device.Data);
HID_Handle->ctl_state = HID_REQ_GET_REPORT_DESC;
}
break;
case HID_REQ_GET_REPORT_DESC:
/* Get Report Desc */
if (USBH_HID_GetHIDReportDescriptor(phost, HID_Handle->HID_Desc.wItemLength) == USBH_OK)
{
/* The descriptor is available in phost->device.Data */
HID_Handle->ctl_state = HID_REQ_SET_IDLE;
}
break;
case HID_REQ_SET_IDLE:
classReqStatus = USBH_HID_SetIdle (phost, 0, 0);
/* set Idle */
if (classReqStatus == USBH_OK)
{
HID_Handle->ctl_state = HID_REQ_SET_PROTOCOL;
}
else if(classReqStatus == USBH_NOT_SUPPORTED)
{
HID_Handle->ctl_state = HID_REQ_SET_PROTOCOL;
}
break;
case HID_REQ_SET_PROTOCOL:
/* set protocol */
if (USBH_HID_SetProtocol (phost, 0) == USBH_OK)
{
HID_Handle->ctl_state = HID_REQ_IDLE;
/* all requests performed*/
phost->pUser(phost, HOST_USER_CLASS_ACTIVE);
status = USBH_OK;
}
break;
case HID_REQ_IDLE:
default:
break;
}
return status;
}
/**
* @brief USBH_HID_Process
* The function is for managing state machine for HID data transfers
* @param phost: Host handle
* @retval USBH Status
*/
static USBH_StatusTypeDef USBH_HID_Process(USBH_HandleTypeDef *phost)
{
USBH_StatusTypeDef status = USBH_OK;
HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
switch (HID_Handle->state)
{
case HID_INIT:
HID_Handle->Init(phost);
case HID_IDLE:
if(USBH_HID_GetReport (phost,
0x01,
0,
HID_Handle->pData,
HID_Handle->length) == USBH_OK)
{
fifo_write(&HID_Handle->fifo, HID_Handle->pData, HID_Handle->length);
HID_Handle->state = HID_SYNC;
}
break;
case HID_SYNC:
/* Sync with start of Even Frame */
if(phost->Timer & 1)
{
HID_Handle->state = HID_GET_DATA;
}
#if (USBH_USE_OS == 1)
osMessagePut ( phost->os_event, USBH_URB_EVENT, 0);
#endif
break;
case HID_GET_DATA:
USBH_InterruptReceiveData(phost,
HID_Handle->pData,
HID_Handle->length,
HID_Handle->InPipe);
HID_Handle->state = HID_POLL;
HID_Handle->timer = phost->Timer;
HID_Handle->DataReady = 0;
break;
case HID_POLL:
if(USBH_LL_GetURBState(phost , HID_Handle->InPipe) == USBH_URB_DONE)
{
if(HID_Handle->DataReady == 0)
{
fifo_write(&HID_Handle->fifo, HID_Handle->pData, HID_Handle->length);
HID_Handle->DataReady = 1;
USBH_HID_EventCallback(phost);
#if (USBH_USE_OS == 1)
osMessagePut ( phost->os_event, USBH_URB_EVENT, 0);
#endif
}
}
else if(USBH_LL_GetURBState(phost , HID_Handle->InPipe) == USBH_URB_STALL) /* IN Endpoint Stalled */
{
/* Issue Clear Feature on interrupt IN endpoint */
if(USBH_ClrFeature(phost,
HID_Handle->ep_addr) == USBH_OK)
{
/* Change state to issue next IN token */
HID_Handle->state = HID_GET_DATA;
}
}
break;
default:
break;
}
return status;
}
/**
* @brief USBH_HID_SOFProcess
* The function is for managing the SOF Process
* @param phost: Host handle
* @retval USBH Status
*/
static USBH_StatusTypeDef USBH_HID_SOFProcess(USBH_HandleTypeDef *phost)
{
HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
if(HID_Handle->state == HID_POLL)
{
if(( phost->Timer - HID_Handle->timer) >= HID_Handle->poll)
{
HID_Handle->state = HID_GET_DATA;
#if (USBH_USE_OS == 1)
osMessagePut ( phost->os_event, USBH_URB_EVENT, 0);
#endif
}
}
return USBH_OK;
}
/**
* @brief USBH_Get_HID_ReportDescriptor
* Issue report Descriptor command to the device. Once the response
* received, parse the report descriptor and update the status.
* @param phost: Host handle
* @param Length : HID Report Descriptor Length
* @retval USBH Status
*/
USBH_StatusTypeDef USBH_HID_GetHIDReportDescriptor (USBH_HandleTypeDef *phost,
uint16_t length)
{
USBH_StatusTypeDef status;
status = USBH_GetDescriptor(phost,
USB_REQ_RECIPIENT_INTERFACE | USB_REQ_TYPE_STANDARD,
USB_DESC_HID_REPORT,
phost->device.Data,
length);
/* HID report descriptor is available in phost->device.Data.
In case of USB Boot Mode devices for In report handling ,
HID report descriptor parsing is not required.
In case, for supporting Non-Boot Protocol devices and output reports,
user may parse the report descriptor*/
return status;
}
/**
* @brief USBH_Get_HID_Descriptor
* Issue HID Descriptor command to the device. Once the response
* received, parse the report descriptor and update the status.
* @param phost: Host handle
* @param Length : HID Descriptor Length
* @retval USBH Status
*/
USBH_StatusTypeDef USBH_HID_GetHIDDescriptor (USBH_HandleTypeDef *phost,
uint16_t length)
{
USBH_StatusTypeDef status;
status = USBH_GetDescriptor( phost,
USB_REQ_RECIPIENT_INTERFACE | USB_REQ_TYPE_STANDARD,
USB_DESC_HID,
phost->device.Data,
length);
return status;
}
/**
* @brief USBH_Set_Idle
* Set Idle State.
* @param phost: Host handle
* @param duration: Duration for HID Idle request
* @param reportId : Targeted report ID for Set Idle request
* @retval USBH Status
*/
USBH_StatusTypeDef USBH_HID_SetIdle (USBH_HandleTypeDef *phost,
uint8_t duration,
uint8_t reportId)
{
phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_INTERFACE |\
USB_REQ_TYPE_CLASS;
phost->Control.setup.b.bRequest = USB_HID_SET_IDLE;
phost->Control.setup.b.wValue.w = (duration << 8 ) | reportId;
phost->Control.setup.b.wIndex.w = 0;
phost->Control.setup.b.wLength.w = 0;
return USBH_CtlReq(phost, 0 , 0 );
}
/**
* @brief USBH_HID_Set_Report
* Issues Set Report
* @param phost: Host handle
* @param reportType : Report type to be sent
* @param reportId : Targeted report ID for Set Report request
* @param reportBuff : Report Buffer
* @param reportLen : Length of data report to be send
* @retval USBH Status
*/
USBH_StatusTypeDef USBH_HID_SetReport (USBH_HandleTypeDef *phost,
uint8_t reportType,
uint8_t reportId,
uint8_t* reportBuff,
uint8_t reportLen)
{
phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_INTERFACE |\
USB_REQ_TYPE_CLASS;
phost->Control.setup.b.bRequest = USB_HID_SET_REPORT;
phost->Control.setup.b.wValue.w = (reportType << 8 ) | reportId;
phost->Control.setup.b.wIndex.w = 0;
phost->Control.setup.b.wLength.w = reportLen;
return USBH_CtlReq(phost, reportBuff , reportLen );
}
/**
* @brief USBH_HID_GetReport
* retreive Set Report
* @param phost: Host handle
* @param reportType : Report type to be sent
* @param reportId : Targeted report ID for Set Report request
* @param reportBuff : Report Buffer
* @param reportLen : Length of data report to be send
* @retval USBH Status
*/
USBH_StatusTypeDef USBH_HID_GetReport (USBH_HandleTypeDef *phost,
uint8_t reportType,
uint8_t reportId,
uint8_t* reportBuff,
uint8_t reportLen)
{
phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_RECIPIENT_INTERFACE |\
USB_REQ_TYPE_CLASS;
phost->Control.setup.b.bRequest = USB_HID_GET_REPORT;
phost->Control.setup.b.wValue.w = (reportType << 8 ) | reportId;
phost->Control.setup.b.wIndex.w = 0;
phost->Control.setup.b.wLength.w = reportLen;
return USBH_CtlReq(phost, reportBuff , reportLen );
}
/**
* @brief USBH_Set_Protocol
* Set protocol State.
* @param phost: Host handle
* @param protocol : Set Protocol for HID : boot/report protocol
* @retval USBH Status
*/
USBH_StatusTypeDef USBH_HID_SetProtocol(USBH_HandleTypeDef *phost,
uint8_t protocol)
{
phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_INTERFACE |\
USB_REQ_TYPE_CLASS;
phost->Control.setup.b.bRequest = USB_HID_SET_PROTOCOL;
phost->Control.setup.b.wValue.w = protocol != 0 ? 0 : 1;
phost->Control.setup.b.wIndex.w = 0;
phost->Control.setup.b.wLength.w = 0;
return USBH_CtlReq(phost, 0 , 0 );
}
/**
* @brief USBH_ParseHIDDesc
* This function Parse the HID descriptor
* @param desc: HID Descriptor
* @param buf: Buffer where the source descriptor is available
* @retval None
*/
static void USBH_HID_ParseHIDDesc (HID_DescTypeDef *desc, uint8_t *buf)
{
desc->bLength = *(uint8_t *) (buf + 0);
desc->bDescriptorType = *(uint8_t *) (buf + 1);
desc->bcdHID = LE16 (buf + 2);
desc->bCountryCode = *(uint8_t *) (buf + 4);
desc->bNumDescriptors = *(uint8_t *) (buf + 5);
desc->bReportDescriptorType = *(uint8_t *) (buf + 6);
desc->wItemLength = LE16 (buf + 7);
}
/**
* @brief USBH_HID_GetDeviceType
* Return Device function.
* @param phost: Host handle
* @retval HID function: HID_MOUSE / HID_KEYBOARD
*/
HID_TypeTypeDef USBH_HID_GetDeviceType(USBH_HandleTypeDef *phost)
{
HID_TypeTypeDef type = HID_UNKNOWN;
if(phost->gState == HOST_CLASS)
{
if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bInterfaceProtocol \
== HID_KEYBRD_BOOT_CODE)
{
type = HID_KEYBOARD;
}
else if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bInterfaceProtocol \
== HID_MOUSE_BOOT_CODE)
{
type= HID_MOUSE;
}
}
return type;
}
/**
* @brief USBH_HID_GetPollInterval
* Return HID device poll time
* @param phost: Host handle
* @retval poll time (ms)
*/
uint8_t USBH_HID_GetPollInterval(USBH_HandleTypeDef *phost)
{
HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
if((phost->gState == HOST_CLASS_REQUEST) ||
(phost->gState == HOST_INPUT) ||
(phost->gState == HOST_SET_CONFIGURATION) ||
(phost->gState == HOST_CHECK_CLASS) ||
((phost->gState == HOST_CLASS)))
{
return (HID_Handle->poll);
}
else
{
return 0;
}
}
/**
* @brief fifo_init
* Initialize FIFO.
* @param f: Fifo address
* @param buf: Fifo buffer
* @param size: Fifo Size
* @retval none
*/
void fifo_init(FIFO_TypeDef * f, uint8_t * buf, uint16_t size)
{
f->head = 0;
f->tail = 0;
f->lock = 0;
f->size = size;
f->buf = buf;
}
/**
* @brief fifo_read
* Read from FIFO.
* @param f: Fifo address
* @param buf: read buffer
* @param nbytes: number of item to read
* @retval number of read items
*/
uint16_t fifo_read(FIFO_TypeDef * f, void * buf, uint16_t nbytes)
{
uint16_t i;
uint8_t * p;
p = (uint8_t*) buf;
if(f->lock == 0)
{
f->lock = 1;
for(i=0; i < nbytes; i++)
{
if( f->tail != f->head )
{
*p++ = f->buf[f->tail];
f->tail++;
if( f->tail == f->size )
{
f->tail = 0;
}
} else
{
f->lock = 0;
return i;
}
}
}
f->lock = 0;
return nbytes;
}
/**
* @brief fifo_write
* Read from FIFO.
* @param f: Fifo address
* @param buf: read buffer
* @param nbytes: number of item to write
* @retval number of written items
*/
uint16_t fifo_write(FIFO_TypeDef * f, const void * buf, uint16_t nbytes)
{
uint16_t i;
const uint8_t * p;
p = (const uint8_t*) buf;
if(f->lock == 0)
{
f->lock = 1;
for(i=0; i < nbytes; i++)
{
if( (f->head + 1 == f->tail) ||
( (f->head + 1 == f->size) && (f->tail == 0)) )
{
f->lock = 0;
return i;
}
else
{
f->buf[f->head] = *p++;
f->head++;
if( f->head == f->size )
{
f->head = 0;
}
}
}
}
f->lock = 0;
return nbytes;
}
/**
* @brief The function is a callback about HID Data events
* @param phost: Selected device
* @retval None
*/
__weak void USBH_HID_EventCallback(USBH_HandleTypeDef *phost)
{
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

@ -0,0 +1,418 @@
/**
******************************************************************************
* @file usbh_hid_keybd.c
* @author MCD Application Team
* @version V3.2.2
* @date 07-July-2015
* @brief This file is the application layer for USB Host HID Keyboard handling
* QWERTY and AZERTY Keyboard are supported as per the selection in
* usbh_hid_keybd.h
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT 2015 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_hid_keybd.h"
#include "usbh_hid_parser.h"
/** @addtogroup USBH_LIB
* @{
*/
/** @addtogroup USBH_CLASS
* @{
*/
/** @addtogroup USBH_HID_CLASS
* @{
*/
/** @defgroup USBH_HID_KEYBD
* @brief This file includes HID Layer Handlers for USB Host HID class.
* @{
*/
/** @defgroup USBH_HID_KEYBD_Private_TypesDefinitions
* @{
*/
/**
* @}
*/
/** @defgroup USBH_HID_KEYBD_Private_Defines
* @{
*/
/**
* @}
*/
#ifndef AZERTY_KEYBOARD
#define QWERTY_KEYBOARD
#endif
#define KBD_LEFT_CTRL 0x01
#define KBD_LEFT_SHIFT 0x02
#define KBD_LEFT_ALT 0x04
#define KBD_LEFT_GUI 0x08
#define KBD_RIGHT_CTRL 0x10
#define KBD_RIGHT_SHIFT 0x20
#define KBD_RIGHT_ALT 0x40
#define KBD_RIGHT_GUI 0x80
#define KBR_MAX_NBR_PRESSED 6
/** @defgroup USBH_HID_KEYBD_Private_Macros
* @{
*/
/**
* @}
*/
/** @defgroup USBH_HID_KEYBD_Private_FunctionPrototypes
* @{
*/
static USBH_StatusTypeDef USBH_HID_KeybdDecode(USBH_HandleTypeDef *phost);
/**
* @}
*/
/** @defgroup USBH_HID_KEYBD_Private_Variables
* @{
*/
HID_KEYBD_Info_TypeDef keybd_info;
uint32_t keybd_report_data[2];
static const HID_Report_ItemTypedef imp_0_lctrl={
(uint8_t*)keybd_report_data+0, /*data*/
1, /*size*/
0, /*shift*/
0, /*count (only for array items)*/
0, /*signed?*/
0, /*min value read can return*/
1, /*max value read can return*/
0, /*min vale device can report*/
1, /*max value device can report*/
1 /*resolution*/
};
static const HID_Report_ItemTypedef imp_0_lshift={
(uint8_t*)keybd_report_data+0, /*data*/
1, /*size*/
1, /*shift*/
0, /*count (only for array items)*/
0, /*signed?*/
0, /*min value read can return*/
1, /*max value read can return*/
0, /*min vale device can report*/
1, /*max value device can report*/
1 /*resolution*/
};
static const HID_Report_ItemTypedef imp_0_lalt={
(uint8_t*)keybd_report_data+0, /*data*/
1, /*size*/
2, /*shift*/
0, /*count (only for array items)*/
0, /*signed?*/
0, /*min value read can return*/
1, /*max value read can return*/
0, /*min vale device can report*/
1, /*max value device can report*/
1 /*resolution*/
};
static const HID_Report_ItemTypedef imp_0_lgui={
(uint8_t*)keybd_report_data+0, /*data*/
1, /*size*/
3, /*shift*/
0, /*count (only for array items)*/
0, /*signed?*/
0, /*min value read can return*/
1, /*max value read can return*/
0, /*min vale device can report*/
1, /*max value device can report*/
1 /*resolution*/
};
static const HID_Report_ItemTypedef imp_0_rctrl={
(uint8_t*)keybd_report_data+0, /*data*/
1, /*size*/
4, /*shift*/
0, /*count (only for array items)*/
0, /*signed?*/
0, /*min value read can return*/
1, /*max value read can return*/
0, /*min vale device can report*/
1, /*max value device can report*/
1 /*resolution*/
};
static const HID_Report_ItemTypedef imp_0_rshift={
(uint8_t*)keybd_report_data+0, /*data*/
1, /*size*/
5, /*shift*/
0, /*count (only for array items)*/
0, /*signed?*/
0, /*min value read can return*/
1, /*max value read can return*/
0, /*min vale device can report*/
1, /*max value device can report*/
1 /*resolution*/
};
static const HID_Report_ItemTypedef imp_0_ralt={
(uint8_t*)keybd_report_data+0, /*data*/
1, /*size*/
6, /*shift*/
0, /*count (only for array items)*/
0, /*signed?*/
0, /*min value read can return*/
1, /*max value read can return*/
0, /*min vale device can report*/
1, /*max value device can report*/
1 /*resolution*/
};
static const HID_Report_ItemTypedef imp_0_rgui={
(uint8_t*)keybd_report_data+0, /*data*/
1, /*size*/
7, /*shift*/
0, /*count (only for array items)*/
0, /*signed?*/
0, /*min value read can return*/
1, /*max value read can return*/
0, /*min vale device can report*/
1, /*max value device can report*/
1 /*resolution*/
};
static const HID_Report_ItemTypedef imp_0_key_array={
(uint8_t*)keybd_report_data+2, /*data*/
8, /*size*/
0, /*shift*/
6, /*count (only for array items)*/
0, /*signed?*/
0, /*min value read can return*/
101, /*max value read can return*/
0, /*min vale device can report*/
101, /*max value device can report*/
1 /*resolution*/
};
#ifdef QWERTY_KEYBOARD
static const int8_t HID_KEYBRD_Key[] = {
'\0', '`', '1', '2', '3', '4', '5', '6',
'7', '8', '9', '0', '-', '=', '\0', '\r',
'\t', 'q', 'w', 'e', 'r', 't', 'y', 'u',
'i', 'o', 'p', '[', ']', '\\',
'\0', 'a', 's', 'd', 'f', 'g', 'h', 'j',
'k', 'l', ';', '\'', '\0', '\n',
'\0', '\0', 'z', 'x', 'c', 'v', 'b', 'n',
'm', ',', '.', '/', '\0', '\0',
'\0', '\0', '\0', ' ', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\r', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '7', '4', '1',
'\0', '/', '8', '5', '2',
'0', '*', '9', '6', '3',
'.', '-', '+', '\0', '\n', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0'
};
static const int8_t HID_KEYBRD_ShiftKey[] = {
'\0', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
'_', '+', '\0', '\0', '\0', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U',
'I', 'O', 'P', '{', '}', '|', '\0', 'A', 'S', 'D', 'F', 'G',
'H', 'J', 'K', 'L', ':', '"', '\0', '\n', '\0', '\0', 'Z', 'X',
'C', 'V', 'B', 'N', 'M', '<', '>', '?', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
};
#else
static const int8_t HID_KEYBRD_Key[] = {
'\0', '`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
'-', '=', '\0', '\r', '\t', 'a', 'z', 'e', 'r', 't', 'y', 'u',
'i', 'o', 'p', '[', ']', '\\', '\0', 'q', 's', 'd', 'f', 'g',
'h', 'j', 'k', 'l', 'm', '\0', '\0', '\n', '\0', '\0', 'w', 'x',
'c', 'v', 'b', 'n', ',', ';', ':', '!', '\0', '\0', '\0', '\0',
'\0', ' ', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\r', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '7', '4', '1','\0', '/',
'8', '5', '2', '0', '*', '9', '6', '3', '.', '-', '+', '\0',
'\n', '\0', '\0', '\0', '\0', '\0', '\0','\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
};
static const int8_t HID_KEYBRD_ShiftKey[] = {
'\0', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_',
'+', '\0', '\0', '\0', 'A', 'Z', 'E', 'R', 'T', 'Y', 'U', 'I', 'O',
'P', '{', '}', '*', '\0', 'Q', 'S', 'D', 'F', 'G', 'H', 'J', 'K',
'L', 'M', '%', '\0', '\n', '\0', '\0', 'W', 'X', 'C', 'V', 'B', 'N',
'?', '.', '/', '\0', '\0', '\0','\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
};
#endif
static const uint8_t HID_KEYBRD_Codes[] = {
0, 0, 0, 0, 31, 50, 48, 33,
19, 34, 35, 36, 24, 37, 38, 39, /* 0x00 - 0x0F */
52, 51, 25, 26, 17, 20, 32, 21,
23, 49, 18, 47, 22, 46, 2, 3, /* 0x10 - 0x1F */
4, 5, 6, 7, 8, 9, 10, 11,
43, 110, 15, 16, 61, 12, 13, 27, /* 0x20 - 0x2F */
28, 29, 42, 40, 41, 1, 53, 54,
55, 30, 112, 113, 114, 115, 116, 117, /* 0x30 - 0x3F */
118, 119, 120, 121, 122, 123, 124, 125,
126, 75, 80, 85, 76, 81, 86, 89, /* 0x40 - 0x4F */
79, 84, 83, 90, 95, 100, 105, 106,
108, 93, 98, 103, 92, 97, 102, 91, /* 0x50 - 0x5F */
96, 101, 99, 104, 45, 129, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x6F */
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x7F */
0, 0, 0, 0, 0, 107, 0, 56,
0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 - 0x8F */
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 - 0x9F */
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, /* 0xA0 - 0xAF */
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, /* 0xB0 - 0xBF */
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, /* 0xC0 - 0xCF */
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, /* 0xD0 - 0xDF */
58, 44, 60, 127, 64, 57, 62, 128 /* 0xE0 - 0xE7 */
};
/**
* @brief USBH_HID_KeybdInit
* The function init the HID keyboard.
* @param phost: Host handle
* @retval USBH Status
*/
USBH_StatusTypeDef USBH_HID_KeybdInit(USBH_HandleTypeDef *phost)
{
uint32_t x;
HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
keybd_info.lctrl=keybd_info.lshift= 0;
keybd_info.lalt=keybd_info.lgui= 0;
keybd_info.rctrl=keybd_info.rshift= 0;
keybd_info.ralt=keybd_info.rgui=0;
for(x=0; x< (sizeof(keybd_report_data)/sizeof(uint32_t)); x++)
{
keybd_report_data[x]=0;
}
if(HID_Handle->length > (sizeof(keybd_report_data)/sizeof(uint32_t)))
{
HID_Handle->length = (sizeof(keybd_report_data)/sizeof(uint32_t));
}
HID_Handle->pData = (uint8_t*)keybd_report_data;
fifo_init(&HID_Handle->fifo, phost->device.Data, HID_QUEUE_SIZE * sizeof(keybd_report_data));
return USBH_OK;
}
/**
* @brief USBH_HID_GetKeybdInfo
* The function return keyboard information.
* @param phost: Host handle
* @retval keyboard information
*/
HID_KEYBD_Info_TypeDef *USBH_HID_GetKeybdInfo(USBH_HandleTypeDef *phost)
{
if(USBH_HID_KeybdDecode(phost) == USBH_OK)
{
return &keybd_info;
}
else
{
return NULL;
}
}
/**
* @brief USBH_HID_KeybdDecode
* The function decode keyboard data.
* @param phost: Host handle
* @retval USBH Status
*/
static USBH_StatusTypeDef USBH_HID_KeybdDecode(USBH_HandleTypeDef *phost)
{
uint8_t x;
HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
if(HID_Handle->length == 0)
{
return USBH_FAIL;
}
/*Fill report */
if(fifo_read(&HID_Handle->fifo, &keybd_report_data, HID_Handle->length) == HID_Handle->length)
{
keybd_info.lctrl=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lctrl, 0);
keybd_info.lshift=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lshift, 0);
keybd_info.lalt=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lalt, 0);
keybd_info.lgui=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lgui, 0);
keybd_info.rctrl=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_rctrl, 0);
keybd_info.rshift=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_rshift, 0);
keybd_info.ralt=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_ralt, 0);
keybd_info.rgui=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_rgui, 0);
for(x=0; x < sizeof(keybd_info.keys); x++)
{
keybd_info.keys[x]=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_key_array, x);
}
return USBH_OK;
}
return USBH_FAIL;
}
/**
* @brief USBH_HID_GetASCIICode
* The function decode keyboard data into ASCII characters.
* @param phost: Host handle
* @param info: Keyboard information
* @retval ASCII code
*/
uint8_t USBH_HID_GetASCIICode(HID_KEYBD_Info_TypeDef *info)
{
uint8_t output;
if((info->lshift == 1) || (info->rshift))
{
output = HID_KEYBRD_ShiftKey[HID_KEYBRD_Codes[info->keys[0]]];
}
else
{
output = HID_KEYBRD_Key[HID_KEYBRD_Codes[info->keys[0]]];
}
return output;
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

@ -0,0 +1,267 @@
/**
******************************************************************************
* @file usbh_hid_mouse.c
* @author MCD Application Team
* @version V3.2.2
* @date 07-July-2015
* @brief This file is the application layer for USB Host HID Mouse Handling.
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT 2015 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_hid_mouse.h"
#include "usbh_hid_parser.h"
/** @addtogroup USBH_LIB
* @{
*/
/** @addtogroup USBH_CLASS
* @{
*/
/** @addtogroup USBH_HID_CLASS
* @{
*/
/** @defgroup USBH_HID_MOUSE
* @brief This file includes HID Layer Handlers for USB Host HID class.
* @{
*/
/** @defgroup USBH_HID_MOUSE_Private_TypesDefinitions
* @{
*/
/**
* @}
*/
/** @defgroup USBH_HID_MOUSE_Private_Defines
* @{
*/
/**
* @}
*/
/** @defgroup USBH_HID_MOUSE_Private_Macros
* @{
*/
/**
* @}
*/
/** @defgroup USBH_HID_MOUSE_Private_FunctionPrototypes
* @{
*/
static USBH_StatusTypeDef USBH_HID_MouseDecode(USBH_HandleTypeDef *phost);
/**
* @}
*/
/** @defgroup USBH_HID_MOUSE_Private_Variables
* @{
*/
HID_MOUSE_Info_TypeDef mouse_info;
uint32_t mouse_report_data[1];
/* Structures defining how to access items in a HID mouse report */
/* Access button 1 state. */
static const HID_Report_ItemTypedef prop_b1={
(uint8_t *)mouse_report_data+0, /*data*/
1, /*size*/
0, /*shift*/
0, /*count (only for array items)*/
0, /*signed?*/
0, /*min value read can return*/
1, /*max value read can return*/
0, /*min value device can report*/
1, /*max value device can report*/
1 /*resolution*/
};
/* Access button 2 state. */
static const HID_Report_ItemTypedef prop_b2={
(uint8_t *)mouse_report_data+0, /*data*/
1, /*size*/
1, /*shift*/
0, /*count (only for array items)*/
0, /*signed?*/
0, /*min value read can return*/
1, /*max value read can return*/
0, /*min value device can report*/
1, /*max value device can report*/
1 /*resolution*/
};
/* Access button 3 state. */
static const HID_Report_ItemTypedef prop_b3={
(uint8_t *)mouse_report_data+0, /*data*/
1, /*size*/
2, /*shift*/
0, /*count (only for array items)*/
0, /*signed?*/
0, /*min value read can return*/
1, /*max value read can return*/
0, /*min vale device can report*/
1, /*max value device can report*/
1 /*resolution*/
};
/* Access x coordinate change. */
static const HID_Report_ItemTypedef prop_x={
(uint8_t *)mouse_report_data+1, /*data*/
8, /*size*/
0, /*shift*/
0, /*count (only for array items)*/
1, /*signed?*/
0, /*min value read can return*/
0xFFFF,/*max value read can return*/
0, /*min vale device can report*/
0xFFFF,/*max value device can report*/
1 /*resolution*/
};
/* Access y coordinate change. */
static const HID_Report_ItemTypedef prop_y={
(uint8_t *)mouse_report_data+2, /*data*/
8, /*size*/
0, /*shift*/
0, /*count (only for array items)*/
1, /*signed?*/
0, /*min value read can return*/
0xFFFF,/*max value read can return*/
0, /*min vale device can report*/
0xFFFF,/*max value device can report*/
1 /*resolution*/
};
/**
* @}
*/
/** @defgroup USBH_HID_MOUSE_Private_Functions
* @{
*/
/**
* @brief USBH_HID_MouseInit
* The function init the HID mouse.
* @param phost: Host handle
* @retval USBH Status
*/
USBH_StatusTypeDef USBH_HID_MouseInit(USBH_HandleTypeDef *phost)
{
HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
mouse_info.x=0;
mouse_info.y=0;
mouse_info.buttons[0]=0;
mouse_info.buttons[1]=0;
mouse_info.buttons[2]=0;
mouse_report_data[0]=0;
if(HID_Handle->length > sizeof(mouse_report_data))
{
HID_Handle->length = sizeof(mouse_report_data);
}
HID_Handle->pData = (uint8_t *)mouse_report_data;
fifo_init(&HID_Handle->fifo, phost->device.Data, HID_QUEUE_SIZE * sizeof(mouse_report_data));
return USBH_OK;
}
/**
* @brief USBH_HID_GetMouseInfo
* The function return mouse information.
* @param phost: Host handle
* @retval mouse information
*/
HID_MOUSE_Info_TypeDef *USBH_HID_GetMouseInfo(USBH_HandleTypeDef *phost)
{
if(USBH_HID_MouseDecode(phost)== USBH_OK)
{
return &mouse_info;
}
else
{
return NULL;
}
}
/**
* @brief USBH_HID_MouseDecode
* The function decode mouse data.
* @param phost: Host handle
* @retval USBH Status
*/
static USBH_StatusTypeDef USBH_HID_MouseDecode(USBH_HandleTypeDef *phost)
{
HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData;
if(HID_Handle->length == 0)
{
return USBH_FAIL;
}
/*Fill report */
if(fifo_read(&HID_Handle->fifo, &mouse_report_data, HID_Handle->length) == HID_Handle->length)
{
/*Decode report */
mouse_info.x = (int16_t )HID_ReadItem((HID_Report_ItemTypedef *) &prop_x, 0);
mouse_info.y = (int16_t )HID_ReadItem((HID_Report_ItemTypedef *) &prop_y, 0);
mouse_info.buttons[0]=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &prop_b1, 0);
mouse_info.buttons[1]=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &prop_b2, 0);
mouse_info.buttons[2]=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &prop_b3, 0);
return USBH_OK;
}
return USBH_FAIL;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

@ -0,0 +1,235 @@
/**
******************************************************************************
* @file usbh_hid_parser.c
* @author MCD Application Team
* @version V3.2.2
* @date 07-July-2015
* @brief This file is the HID Layer Handlers for USB Host HID class.
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT 2015 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_hid_parser.h"
/** @addtogroup USBH_LIB
* @{
*/
/** @addtogroup USBH_CLASS
* @{
*/
/** @addtogroup USBH_HID_CLASS
* @{
*/
/** @defgroup USBH_HID_PARSER
* @brief This file includes HID parsers for USB Host HID class.
* @{
*/
/** @defgroup USBH_HID_PARSER_Private_TypesDefinitions
* @{
*/
/**
* @}
*/
/** @defgroup USBH_HID_PARSER_Private_Defines
* @{
*/
/**
* @}
*/
/** @defgroup USBH_HID_PARSER_Private_Macros
* @{
*/
/**
* @}
*/
/** @defgroup USBH_HID_PARSER_Private_FunctionPrototypes
* @{
*/
/**
* @}
*/
/** @defgroup USBH_HID_PARSER_Private_Variables
* @{
*/
/**
* @}
*/
/** @defgroup USBH_HID_PARSER_Private_Functions
* @{
*/
/**
* @brief HID_ReadItem
* The function read a report item.
* @param ri: report item
* @param ndx: report index
* @retval status (0 : fail / otherwise: item value)
*/
uint32_t HID_ReadItem(HID_Report_ItemTypedef *ri, uint8_t ndx)
{
uint32_t val=0;
uint32_t x=0;
uint32_t bofs;
uint8_t *data=ri->data;
uint8_t shift=ri->shift;
/* get the logical value of the item */
/* if this is an array, wee may need to offset ri->data.*/
if (ri->count > 0)
{
/* If app tries to read outside of the array. */
if (ri->count <= ndx)
{
return(0);
}
/* calculate bit offset */
bofs = ndx*ri->size;
bofs += shift;
/* calculate byte offset + shift pair from bit offset. */
data+=bofs/8;
shift=(uint8_t)(bofs%8);
}
/* read data bytes in little endian order */
for(x=0; x < ((ri->size & 0x7) ? (ri->size/8)+1 : (ri->size/8)); x++)
{
val=(uint32_t)(*data << (x*8));
}
val=(val >> shift) & ((1<<ri->size)-1);
if (val < ri->logical_min || val > ri->logical_max)
{
return(0);
}
/* convert logical value to physical value */
/* See if the number is negative or not. */
if ((ri->sign) && (val & (1<<(ri->size-1))))
{
/* yes, so sign extend value to 32 bits. */
int vs=(int)((-1 & ~((1<<(ri->size))-1)) | val);
if(ri->resolution == 1)
{
return((uint32_t)vs);
}
return((uint32_t)(vs*ri->resolution));
}
else
{
if(ri->resolution == 1)
{
return(val);
}
return(val*ri->resolution);
}
}
/**
* @brief HID_WriteItem
* The function write a report item.
* @param ri: report item
* @param ndx: report index
* @retval status (1: fail/ 0 : Ok)
*/
uint32_t HID_WriteItem(HID_Report_ItemTypedef *ri, uint32_t value, uint8_t ndx)
{
uint32_t x;
uint32_t mask;
uint32_t bofs;
uint8_t *data=ri->data;
uint8_t shift=ri->shift;
if (value < ri->physical_min || value > ri->physical_max)
{
return(1);
}
/* if this is an array, wee may need to offset ri->data.*/
if (ri->count > 0)
{
/* If app tries to read outside of the array. */
if (ri->count >= ndx)
{
return(0);
}
/* calculate bit offset */
bofs = ndx*ri->size;
bofs += shift;
/* calculate byte offset + shift pair from bit offset. */
data+=bofs/8;
shift=(uint8_t)(bofs%8);
}
/* Convert physical value to logical value. */
if (ri->resolution != 1)
{
value=value/ri->resolution;
}
/* Write logical value to report in little endian order. */
mask=(uint32_t)((1<<ri->size)-1);
value = (value & mask) << shift;
for(x=0; x < ((ri->size & 0x7) ? (ri->size/8)+1 : (ri->size/8)); x++)
{
*(ri->data+x)=(uint8_t)((*(ri->data+x) & ~(mask>>(x*8))) | ((value>>(x*8)) & (mask>>(x*8))));
}
return(0);
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

@ -0,0 +1,148 @@
/**
******************************************************************************
* @file usbd_hid.h
* @author MCD Application Team
* @version V2.4.1
* @date 19-June-2015
* @brief Header file for the usbd_hid_core.c file.
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT 2015 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.
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __USB_HID_H
#define __USB_HID_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "usbd_ioreq.h"
/** @addtogroup STM32_USB_DEVICE_LIBRARY
* @{
*/
/** @defgroup USBD_HID
* @brief This file is the Header file for usbd_hid.c
* @{
*/
/** @defgroup USBD_HID_Exported_Defines
* @{
*/
#define HID_EPIN_ADDR 0x81
#define HID_EPIN_SIZE 0x04
#define USB_HID_CONFIG_DESC_SIZ 34
#define USB_HID_DESC_SIZ 9
#define HID_MOUSE_REPORT_DESC_SIZE 74
#define HID_DESCRIPTOR_TYPE 0x21
#define HID_REPORT_DESC 0x22
#define HID_HS_BINTERVAL 0x07
#define HID_FS_BINTERVAL 0x0A
#define HID_POLLING_INTERVAL 0x0A
#define HID_REQ_SET_PROTOCOL 0x0B
#define HID_REQ_GET_PROTOCOL 0x03
#define HID_REQ_SET_IDLE 0x0A
#define HID_REQ_GET_IDLE 0x02
#define HID_REQ_SET_REPORT 0x09
#define HID_REQ_GET_REPORT 0x01
/**
* @}
*/
/** @defgroup USBD_CORE_Exported_TypesDefinitions
* @{
*/
typedef enum
{
HID_IDLE = 0,
HID_BUSY,
}
HID_StateTypeDef;
typedef struct
{
uint32_t Protocol;
uint32_t IdleState;
uint32_t AltSetting;
HID_StateTypeDef state;
}
USBD_HID_HandleTypeDef;
/**
* @}
*/
/** @defgroup USBD_CORE_Exported_Macros
* @{
*/
/**
* @}
*/
/** @defgroup USBD_CORE_Exported_Variables
* @{
*/
extern USBD_ClassTypeDef USBD_HID;
#define USBD_HID_CLASS &USBD_HID
/**
* @}
*/
/** @defgroup USB_CORE_Exported_Functions
* @{
*/
uint8_t USBD_HID_SendReport (USBD_HandleTypeDef *pdev,
uint8_t *report,
uint16_t len);
uint32_t USBD_HID_GetPollingInterval (USBD_HandleTypeDef *pdev);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __USB_HID_H */
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

@ -0,0 +1,525 @@
/**
******************************************************************************
* @file usbd_hid.c
* @author MCD Application Team
* @version V2.4.1
* @date 19-June-2015
* @brief This file provides the HID core functions.
*
* @verbatim
*
* ===================================================================
* HID Class Description
* ===================================================================
* This module manages the HID class V1.11 following the "Device Class Definition
* for Human Interface Devices (HID) Version 1.11 Jun 27, 2001".
* This driver implements the following aspects of the specification:
* - The Boot Interface Subclass
* - The Mouse protocol
* - Usage Page : Generic Desktop
* - Usage : Joystick
* - Collection : Application
*
* @note In HS mode and when the DMA is used, all variables and data structures
* dealing with the DMA during the transaction process should be 32-bit aligned.
*
*
* @endverbatim
*
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT 2015 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 "usbd_hid.h"
#include "usbd_desc.h"
#include "usbd_ctlreq.h"
/** @addtogroup STM32_USB_DEVICE_LIBRARY
* @{
*/
/** @defgroup USBD_HID
* @brief usbd core module
* @{
*/
/** @defgroup USBD_HID_Private_TypesDefinitions
* @{
*/
/**
* @}
*/
/** @defgroup USBD_HID_Private_Defines
* @{
*/
/**
* @}
*/
/** @defgroup USBD_HID_Private_Macros
* @{
*/
/**
* @}
*/
/** @defgroup USBD_HID_Private_FunctionPrototypes
* @{
*/
static uint8_t USBD_HID_Init (USBD_HandleTypeDef *pdev,
uint8_t cfgidx);
static uint8_t USBD_HID_DeInit (USBD_HandleTypeDef *pdev,
uint8_t cfgidx);
static uint8_t USBD_HID_Setup (USBD_HandleTypeDef *pdev,
USBD_SetupReqTypedef *req);
static uint8_t *USBD_HID_GetCfgDesc (uint16_t *length);
static uint8_t *USBD_HID_GetDeviceQualifierDesc (uint16_t *length);
static uint8_t USBD_HID_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum);
/**
* @}
*/
/** @defgroup USBD_HID_Private_Variables
* @{
*/
USBD_ClassTypeDef USBD_HID =
{
USBD_HID_Init,
USBD_HID_DeInit,
USBD_HID_Setup,
NULL, /*EP0_TxSent*/
NULL, /*EP0_RxReady*/
USBD_HID_DataIn, /*DataIn*/
NULL, /*DataOut*/
NULL, /*SOF */
NULL,
NULL,
USBD_HID_GetCfgDesc,
USBD_HID_GetCfgDesc,
USBD_HID_GetCfgDesc,
USBD_HID_GetDeviceQualifierDesc,
};
/* USB HID device Configuration Descriptor */
__ALIGN_BEGIN static uint8_t USBD_HID_CfgDesc[USB_HID_CONFIG_DESC_SIZ] __ALIGN_END =
{
0x09, /* bLength: Configuration Descriptor size */
USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
USB_HID_CONFIG_DESC_SIZ,
/* wTotalLength: Bytes returned */
0x00,
0x01, /*bNumInterfaces: 1 interface*/
0x01, /*bConfigurationValue: Configuration value*/
0x00, /*iConfiguration: Index of string descriptor describing
the configuration*/
0xE0, /*bmAttributes: bus powered and Support Remote Wake-up */
0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/
/************** Descriptor of Joystick Mouse interface ****************/
/* 09 */
0x09, /*bLength: Interface Descriptor size*/
USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/
0x00, /*bInterfaceNumber: Number of Interface*/
0x00, /*bAlternateSetting: Alternate setting*/
0x01, /*bNumEndpoints*/
0x03, /*bInterfaceClass: HID*/
0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/
0x02, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/
0, /*iInterface: Index of string descriptor*/
/******************** Descriptor of Joystick Mouse HID ********************/
/* 18 */
0x09, /*bLength: HID Descriptor size*/
HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/
0x11, /*bcdHID: HID Class Spec release number*/
0x01,
0x00, /*bCountryCode: Hardware target country*/
0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/
0x22, /*bDescriptorType*/
HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/
0x00,
/******************** Descriptor of Mouse endpoint ********************/
/* 27 */
0x07, /*bLength: Endpoint Descriptor size*/
USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/
HID_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/
0x03, /*bmAttributes: Interrupt endpoint*/
HID_EPIN_SIZE, /*wMaxPacketSize: 4 Byte max */
0x00,
HID_FS_BINTERVAL, /*bInterval: Polling Interval (10 ms)*/
/* 34 */
} ;
/* USB HID device Configuration Descriptor */
__ALIGN_BEGIN static uint8_t USBD_HID_Desc[USB_HID_DESC_SIZ] __ALIGN_END =
{
/* 18 */
0x09, /*bLength: HID Descriptor size*/
HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/
0x11, /*bcdHID: HID Class Spec release number*/
0x01,
0x00, /*bCountryCode: Hardware target country*/
0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/
0x22, /*bDescriptorType*/
HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/
0x00,
};
/* USB Standard Device Descriptor */
__ALIGN_BEGIN static uint8_t USBD_HID_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END =
{
USB_LEN_DEV_QUALIFIER_DESC,
USB_DESC_TYPE_DEVICE_QUALIFIER,
0x00,
0x02,
0x00,
0x00,
0x00,
0x40,
0x01,
0x00,
};
__ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE] __ALIGN_END =
{
0x05, 0x01,
0x09, 0x02,
0xA1, 0x01,
0x09, 0x01,
0xA1, 0x00,
0x05, 0x09,
0x19, 0x01,
0x29, 0x03,
0x15, 0x00,
0x25, 0x01,
0x95, 0x03,
0x75, 0x01,
0x81, 0x02,
0x95, 0x01,
0x75, 0x05,
0x81, 0x01,
0x05, 0x01,
0x09, 0x30,
0x09, 0x31,
0x09, 0x38,
0x15, 0x81,
0x25, 0x7F,
0x75, 0x08,
0x95, 0x03,
0x81, 0x06,
0xC0, 0x09,
0x3c, 0x05,
0xff, 0x09,
0x01, 0x15,
0x00, 0x25,
0x01, 0x75,
0x01, 0x95,
0x02, 0xb1,
0x22, 0x75,
0x06, 0x95,
0x01, 0xb1,
0x01, 0xc0
};
/**
* @}
*/
/** @defgroup USBD_HID_Private_Functions
* @{
*/
/**
* @brief USBD_HID_Init
* Initialize the HID interface
* @param pdev: device instance
* @param cfgidx: Configuration index
* @retval status
*/
static uint8_t USBD_HID_Init (USBD_HandleTypeDef *pdev,
uint8_t cfgidx)
{
uint8_t ret = 0;
/* Open EP IN */
USBD_LL_OpenEP(pdev,
HID_EPIN_ADDR,
USBD_EP_TYPE_INTR,
HID_EPIN_SIZE);
pdev->pClassData = USBD_malloc(sizeof (USBD_HID_HandleTypeDef));
if(pdev->pClassData == NULL)
{
ret = 1;
}
else
{
((USBD_HID_HandleTypeDef *)pdev->pClassData)->state = HID_IDLE;
}
return ret;
}
/**
* @brief USBD_HID_Init
* DeInitialize the HID layer
* @param pdev: device instance
* @param cfgidx: Configuration index
* @retval status
*/
static uint8_t USBD_HID_DeInit (USBD_HandleTypeDef *pdev,
uint8_t cfgidx)
{
/* Close HID EPs */
USBD_LL_CloseEP(pdev,
HID_EPIN_ADDR);
/* FRee allocated memory */
if(pdev->pClassData != NULL)
{
USBD_free(pdev->pClassData);
pdev->pClassData = NULL;
}
return USBD_OK;
}
/**
* @brief USBD_HID_Setup
* Handle the HID specific requests
* @param pdev: instance
* @param req: usb requests
* @retval status
*/
static uint8_t USBD_HID_Setup (USBD_HandleTypeDef *pdev,
USBD_SetupReqTypedef *req)
{
uint16_t len = 0;
uint8_t *pbuf = NULL;
USBD_HID_HandleTypeDef *hhid = (USBD_HID_HandleTypeDef*) pdev->pClassData;
switch (req->bmRequest & USB_REQ_TYPE_MASK)
{
case USB_REQ_TYPE_CLASS :
switch (req->bRequest)
{
case HID_REQ_SET_PROTOCOL:
hhid->Protocol = (uint8_t)(req->wValue);
break;
case HID_REQ_GET_PROTOCOL:
USBD_CtlSendData (pdev,
(uint8_t *)&hhid->Protocol,
1);
break;
case HID_REQ_SET_IDLE:
hhid->IdleState = (uint8_t)(req->wValue >> 8);
break;
case HID_REQ_GET_IDLE:
USBD_CtlSendData (pdev,
(uint8_t *)&hhid->IdleState,
1);
break;
default:
USBD_CtlError (pdev, req);
return USBD_FAIL;
}
break;
case USB_REQ_TYPE_STANDARD:
switch (req->bRequest)
{
case USB_REQ_GET_DESCRIPTOR:
if( req->wValue >> 8 == HID_REPORT_DESC)
{
len = MIN(HID_MOUSE_REPORT_DESC_SIZE , req->wLength);
pbuf = HID_MOUSE_ReportDesc;
}
else if( req->wValue >> 8 == HID_DESCRIPTOR_TYPE)
{
pbuf = USBD_HID_Desc;
len = MIN(USB_HID_DESC_SIZ , req->wLength);
}
USBD_CtlSendData (pdev,
pbuf,
len);
break;
case USB_REQ_GET_INTERFACE :
USBD_CtlSendData (pdev,
(uint8_t *)&hhid->AltSetting,
1);
break;
case USB_REQ_SET_INTERFACE :
hhid->AltSetting = (uint8_t)(req->wValue);
break;
}
}
return USBD_OK;
}
/**
* @brief USBD_HID_SendReport
* Send HID Report
* @param pdev: device instance
* @param buff: pointer to report
* @retval status
*/
uint8_t USBD_HID_SendReport (USBD_HandleTypeDef *pdev,
uint8_t *report,
uint16_t len)
{
USBD_HID_HandleTypeDef *hhid = (USBD_HID_HandleTypeDef*)pdev->pClassData;
if (pdev->dev_state == USBD_STATE_CONFIGURED )
{
if(hhid->state == HID_IDLE)
{
hhid->state = HID_BUSY;
USBD_LL_Transmit (pdev,
HID_EPIN_ADDR,
report,
len);
}
}
return USBD_OK;
}
/**
* @brief USBD_HID_GetPollingInterval
* return polling interval from endpoint descriptor
* @param pdev: device instance
* @retval polling interval
*/
uint32_t USBD_HID_GetPollingInterval (USBD_HandleTypeDef *pdev)
{
uint32_t polling_interval = 0;
/* HIGH-speed endpoints */
if(pdev->dev_speed == USBD_SPEED_HIGH)
{
/* Sets the data transfer polling interval for high speed transfers.
Values between 1..16 are allowed. Values correspond to interval
of 2 ^ (bInterval-1). This option (8 ms, corresponds to HID_HS_BINTERVAL */
polling_interval = (((1 <<(HID_HS_BINTERVAL - 1)))/8);
}
else /* LOW and FULL-speed endpoints */
{
/* Sets the data transfer polling interval for low and full
speed transfers */
polling_interval = HID_FS_BINTERVAL;
}
return ((uint32_t)(polling_interval));
}
/**
* @brief USBD_HID_GetCfgDesc
* return configuration descriptor
* @param speed : current device speed
* @param length : pointer data length
* @retval pointer to descriptor buffer
*/
static uint8_t *USBD_HID_GetCfgDesc (uint16_t *length)
{
*length = sizeof (USBD_HID_CfgDesc);
return USBD_HID_CfgDesc;
}
/**
* @brief USBD_HID_DataIn
* handle data IN Stage
* @param pdev: device instance
* @param epnum: endpoint index
* @retval status
*/
static uint8_t USBD_HID_DataIn (USBD_HandleTypeDef *pdev,
uint8_t epnum)
{
/* Ensure that the FIFO is empty before a new transfer, this condition could
be caused by a new transfer before the end of the previous transfer */
((USBD_HID_HandleTypeDef *)pdev->pClassData)->state = HID_IDLE;
return USBD_OK;
}
/**
* @brief DeviceQualifierDescriptor
* return Device Qualifier descriptor
* @param length : pointer data length
* @retval pointer to descriptor buffer
*/
static uint8_t *USBD_HID_GetDeviceQualifierDesc (uint16_t *length)
{
*length = sizeof (USBD_HID_DeviceQualifierDesc);
return USBD_HID_DeviceQualifierDesc;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Loading…
Cancel
Save