hpw422移植新的sdk

This commit is contained in:
xushaoxiang
2026-07-03 18:08:25 +08:00
commit 945a5a5b0b
2583 changed files with 713209 additions and 0 deletions
@@ -0,0 +1,179 @@
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __USB_HID_CUSTOM_H
#define __USB_HID_CUSTOM_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "usbd_ioreq.h"
/** @defgroup USBD_HID
* @brief This file is the Header file for usbd_hid.c
* @{
*/
/** @defgroup USBD_HID_Exported_Defines
* @{
*/
#ifndef HID_CUSTOM_EPIN_ADDR
#define HID_CUSTOM_EPIN_ADDR 0x83U
#endif /* HID_EPIN_ADDR */
#define HID_CUSTOM_EPIN_SIZE 0x40U
#ifndef HID_CUSTOM_EPOUT_ADDR
#define HID_CUSTOM_EPOUT_ADDR 0x03U
#endif /* HID_CUSTOM_EPOUT_ADDR */
#define HID_CUSTOM_EPOUT_SIZE 0x40U
#define USB_HID_CUSTOM_CONFIG_DESC_SIZ 41U
#define USB_HID_CUSTOM_DESC_SIZ 9U
#define HID_CUSTOM_REPORT_DESC_SIZE 38+45U
#define HID_CUSTOM_DESCRIPTOR_TYPE 0x21U
#define HID_CUSTOM_REPORT_DESC 0x22U
#ifndef HID_CUSTOM_HS_BINTERVAL
#define HID_CUSTOM_HS_BINTERVAL 0x07U
#endif /* HID_HS_BINTERVAL */
#ifndef HID_CUSTOM_FS_BINTERVAL
#define HID_CUSTOM_FS_BINTERVAL 0x0AU
#endif /* HID_FS_BINTERVAL */
#ifndef USBD_CUSTOM_OUTREPORT_BUF_SIZE
#define USBD_CUSTOM_OUTREPORT_BUF_SIZE 0x02U
#endif /* USBD_CUSTOM_OUTREPORT_BUF_SIZE */
#define HID_CUSTOM_REQ_SET_PROTOCOL 0x0BU
#define HID_CUSTOM_REQ_GET_PROTOCOL 0x03U
#define HID_CUSTOM_REQ_SET_IDLE 0x0AU
#define HID_CUSTOM_REQ_GET_IDLE 0x02U
#define HID_CUSTOM_REQ_SET_REPORT 0x09U
#define HID_CUSTOM_REQ_GET_REPORT 0x01U
#ifndef HID_CUSTOM_EP0_SET_REPORT_BUF_SIZE
#define HID_CUSTOM_EP0_SET_REPORT_BUF_SIZE 0x10U
#endif /* HID_CUSTOM_EP0_SET_REPORT_BUF_SIZE */
#ifndef HID_CUSTOM_EP0_GET_REPORT_BUF_SIZE
#define HID_CUSTOM_EP0_GET_REPORT_BUF_SIZE 0x10U
#endif /* HID_CUSTOM_EP0_GET_REPORT_BUF_SIZE */
/**
* @}
*/
#define USB_DFU_REPORT_MAP \
0x06, 0x86, 0x19, /* Usage Page (ByteX)*/\
0x09, 0x10, /* Usage (0x10)*/\
0xA1, 0x01, /* Collection (Application)*/\
0x85, 0xFA, /* Report ID (-79)*/\
0x15, 0x00, /* Logical Minimum (0)*/\
0x25, 0xFF, /* Logical Maximum (-1)*/\
0x09, 0x00, /* Usage (Unassigned)*/\
0x19, 0x00, /* Usage (Unassigned)*/\
0x29, 0xFF, /* Usage Maximum (0xFF)*/\
0x75, 0x08, /* Report Size (8)*/\
0x95, 63, /* Report Count (63)*/\
0x81, 0x02, /* Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)*/\
0x85, 0xFB, /* Report ID (-78)*/\
0x15, 0x00, /* Logical Minimum (0)*/\
0x25, 0xFF, /* Logical Maximum (-1)*/\
0x09, 0x00, /* Usage (Unassigned)*/\
0x19, 0x00, /* Usage (Unassigned)*/\
0x29, 0xFF, /* Usage Maximum (0xFF)*/\
0x75, 0x08, /* Report Size (8)*/\
0x96, 0xFF, 0x01, /* Report Count (511)*/\
0x91, 0x02, /* Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)*/\
0xC0
/** @defgroup USBD_CORE_Exported_TypesDefinitions
* @{
*/
typedef enum
{
CUSTOM_HID_IDLE = 0,
CUSTOM_HID_BUSY,
} HID_Custom_StateTypeDef;
typedef struct
{
uint8_t EP0_ReportBuff[HID_CUSTOM_EP0_SET_REPORT_BUF_SIZE];
uint8_t EP0_ReportLen;
uint8_t Report_buf[USBD_CUSTOM_OUTREPORT_BUF_SIZE];
uint32_t Protocol;
uint32_t IdleState;
uint32_t AltSetting;
HID_Custom_StateTypeDef state;
} USBD_HID_Custom_HandleTypeDef;
/*
* HID Class specification version 1.1
* 6.2.1 HID Descriptor
*/
typedef struct
{
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bcdHID;
uint8_t bCountryCode;
uint8_t bNumDescriptors;
uint8_t bHIDDescriptorType;
uint16_t wItemLength;
} __PACKED USBD_HID_CustomDescTypeDef;
/**
* @}
*/
/** @defgroup USBD_CORE_Exported_Macros
* @{
*/
/**
* @}
*/
/** @defgroup USBD_CORE_Exported_Variables
* @{
*/
extern USBD_ClassTypeDef USBD_HID_CUSTOM;
#define USBD_HID_CUSTOM_CLASS &USBD_HID_CUSTOM
/**
* @}
*/
/** @defgroup USB_CORE_Exported_Functions
* @{
*/
uint8_t USBD_HID_CUSTOM_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len);
uint32_t USBD_HID_CUSTOM_GetPollingInterval(USBD_HandleTypeDef *pdev);
uint8_t USBD_HID_Custom_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len);
uint8_t USBD_HID_Custom_FirstSendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __USB_HID_H */
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,156 @@
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __USB_HID_KEYBOARD_H
#define __USB_HID_KEYBOARD_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "usbd_ioreq.h"
/** @defgroup USBD_HID
* @brief This file is the Header file for usbd_hid.c
* @{
*/
/** @defgroup USBD_HID_Exported_Defines
* @{
*/
#ifndef HID_KEYBOARD_EPIN_ADDR
#define HID_KEYBOARD_EPIN_ADDR 0x82U
#endif /* HID_EPIN_ADDR */
#define HID_KEYBOARD_EPIN_SIZE 0x08U
#ifndef HID_KEYBOARD_EPOUT_ADDR
#define HID_KEYBOARD_EPOUT_ADDR 0x02U
#endif /* HID_EPIN_ADDR */
#define HID_KEYBOARD_EPOUT_SIZE 0x01U
#define USB_HID_KEYBOARD_CONFIG_DESC_SIZ 41U
#define USB_HID_KEYBOARD_DESC_SIZ 9U
#define HID_KEYBOARD_REPORT_DESC_SIZE 63U
#define HID_KEYBOARD_DESCRIPTOR_TYPE 0x21U
#define HID_KEYBOARD_REPORT_DESC 0x22U
#ifndef HID_KEYBOARD_HS_BINTERVAL
#define HID_KEYBOARD_HS_BINTERVAL 0x07U
#endif /* HID_HS_BINTERVAL */
#ifndef HID_KEYBOARD_FS_BINTERVAL
#define HID_KEYBOARD_FS_BINTERVAL 0x0AU
#endif /* HID_FS_BINTERVAL */
#ifndef USBD_KEYBOARD_OUTREPORT_BUF_SIZE
#define USBD_KEYBOARD_OUTREPORT_BUF_SIZE 0x02U
#endif /* USBD_KEYBOARD_OUTREPORT_BUF_SIZE */
#define HID_KEYBOARD_REQ_SET_PROTOCOL 0x0BU
#define HID_KEYBOARD_REQ_GET_PROTOCOL 0x03U
#define HID_KEYBOARD_REQ_SET_IDLE 0x0AU
#define HID_KEYBOARD_REQ_GET_IDLE 0x02U
#define HID_KEYBOARD_REQ_SET_REPORT 0x09U
#define HID_KEYBOARD_REQ_GET_REPORT 0x01U
#ifndef HID_KEYBOARD_EP0_SET_REPORT_BUF_SIZE
#define HID_KEYBOARD_EP0_SET_REPORT_BUF_SIZE 0x10U
#endif /* HID_KEYBOARD_EP0_SET_REPORT_BUF_SIZE */
#ifndef HID_KEYBOARD_EP0_GET_REPORT_BUF_SIZE
#define HID_KEYBOARD_EP0_GET_REPORT_BUF_SIZE 0x10U
#endif /* HID_KEYBOARD_EP0_GET_REPORT_BUF_SIZE */
/**
* @}
*/
/** @defgroup USBD_CORE_Exported_TypesDefinitions
* @{
*/
typedef enum
{
KEYBOARD_HID_IDLE = 0,
KEYBOARD_HID_BUSY,
} HID_Keyboard_StateTypeDef;
typedef struct
{
uint8_t EP0_ReportBuff[HID_KEYBOARD_EP0_SET_REPORT_BUF_SIZE];
uint8_t EP0_ReportLen;
uint8_t Report_buf[USBD_KEYBOARD_OUTREPORT_BUF_SIZE];
uint32_t Protocol;
uint32_t IdleState;
uint32_t AltSetting;
HID_Keyboard_StateTypeDef state;
} USBD_HID_Keyboard_HandleTypeDef;
/*
* HID Class specification version 1.1
* 6.2.1 HID Descriptor
*/
typedef struct
{
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bcdHID;
uint8_t bCountryCode;
uint8_t bNumDescriptors;
uint8_t bHIDDescriptorType;
uint16_t wItemLength;
} __PACKED USBD_HID_KeyboardDescTypeDef;
/**
* @}
*/
/** @defgroup USBD_CORE_Exported_Macros
* @{
*/
/**
* @}
*/
/** @defgroup USBD_CORE_Exported_Variables
* @{
*/
extern USBD_ClassTypeDef USBD_HID_KEYBOARD;
#define USBD_HID_KEYBOARD_CLASS &USBD_HID_KEYBOARD
/**
* @}
*/
/** @defgroup USB_CORE_Exported_Functions
* @{
*/
uint8_t USBD_HID_Keyboard_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len);
uint32_t USBD_HID_GetPollingInterval(USBD_HandleTypeDef *pdev);
uint8_t USBD_HID_Keyboard_FirstSendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __USB_HID_H */
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,172 @@
/*!
* \file usbd_hid.h
*
* \brief This file provides the HID core functions.
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
*
* _ __ _ ________ _
* | |/ /(_)___ / ____/ /_ (_)___
* | // / __ \/ / / __ \/ / __ \
* / |/ / / / / /___/ / / / / /_/ /
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
* /_/
* (C) 2022-2025 XinChip
*
* \endcode
*
* \author MCD Application Team
*
* \author ( XinChip ) Alex-J
*
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __USB_HID_MOUSE_H
#define __USB_HID_MOUSE_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "usbd_ioreq.h"
/** @defgroup USBD_HID
* @brief This file is the Header file for usbd_hid.c
* @{
*/
/** @defgroup USBD_HID_Exported_Defines
* @{
*/
#ifndef HID_MOUSE_EPIN_ADDR
#define HID_MOUSE_EPIN_ADDR 0x81U
#endif /* HID_EPIN_ADDR */
#define HID_MOUSE_EPIN_SIZE 0x04U
#define USB_HID_MOUSE_CONFIG_DESC_SIZ 34U
#define USB_HID_MOUSE_DESC_SIZ 9U
#define HID_MOUSE_REPORT_DESC_SIZE 74U
#define HID_MOUSE_DESCRIPTOR_TYPE 0x21U
#define HID_MOUSE_REPORT_DESC 0x22U
#ifndef HID_MOUSE_HS_BINTERVAL
#define HID_MOUSE_HS_BINTERVAL 0x07U
#endif /* HID_HS_BINTERVAL */
#ifndef HID_MOUSE_FS_BINTERVAL
#define HID_MOUSE_FS_BINTERVAL 0x01U
#endif /* HID_FS_BINTERVAL */
#define HID_MOUSE_REQ_SET_PROTOCOL 0x0BU
#define HID_MOUSE_REQ_GET_PROTOCOL 0x03U
#define HID_MOUSE_REQ_SET_IDLE 0x0AU
#define HID_MOUSE_REQ_GET_IDLE 0x02U
#define HID_MOUSE_REQ_SET_REPORT 0x09U
#define HID_MOUSE_REQ_GET_REPORT 0x01U
#ifndef HID_MOUSE_EP0_SET_REPORT_BUF_SIZE
#define HID_MOUSE_EP0_SET_REPORT_BUF_SIZE 0x10U
#endif /* HID_MOUSE_EP0_SET_REPORT_BUF_SIZE */
#ifndef HID_MOUSE_EP0_GET_REPORT_BUF_SIZE
#define HID_MOUSE_EP0_GET_REPORT_BUF_SIZE 0x10U
#endif /* HID_MOUSE_EP0_GET_REPORT_BUF_SIZE */
/**
* @}
*/
/** @defgroup USBD_CORE_Exported_TypesDefinitions
* @{
*/
typedef enum
{
MOUSE_HID_IDLE = 0,
MOUSE_HID_BUSY,
} HID_Mouse_StateTypeDef;
typedef struct
{
uint8_t EP0_ReportBuff[HID_MOUSE_EP0_SET_REPORT_BUF_SIZE];
uint8_t EP0_ReportLen;
uint32_t Protocol;
uint32_t IdleState;
uint32_t AltSetting;
HID_Mouse_StateTypeDef state;
} USBD_HID_Mouse_HandleTypeDef;
/*
* HID Class specification version 1.1
* 6.2.1 HID Descriptor
*/
typedef struct
{
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bcdHID;
uint8_t bCountryCode;
uint8_t bNumDescriptors;
uint8_t bHIDDescriptorType;
uint16_t wItemLength;
} __PACKED USBD_HID_MouseDescTypeDef;
/**
* @}
*/
/** @defgroup USBD_CORE_Exported_Macros
* @{
*/
/**
* @}
*/
/** @defgroup USBD_CORE_Exported_Variables
* @{
*/
extern USBD_ClassTypeDef USBD_HID_MOUSE;
#define USBD_HID_MOUSE_CLASS &USBD_HID_MOUSE
/**
* @}
*/
/** @defgroup USB_CORE_Exported_Functions
* @{
*/
uint8_t USBD_HID_Mouse_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len);
uint32_t USBD_HID_GetPollingInterval(USBD_HandleTypeDef *pdev);
uint8_t USBD_HID_Mouse_FirstSendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __USB_HID_H */
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,879 @@
/*!
* \file usbd_hid_custom.c
*
* \brief This file provides the HID core functions.
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
*
* _ __ _ ________ _
* | |/ /(_)___ / ____/ /_ (_)___
* | // / __ \/ / / __ \/ / __ \
* / |/ / / / / /___/ / / / / /_/ /
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
* /_/
* (C) 2022-2025 XinChip
*
* \endcode
*
* \author MCD Application Team
*
* \author ( XinChip ) Alex-J
*
* @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 : Custom
* - 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
*
*/
/* Includes ------------------------------------------------------------------*/
#include "usbd_hid_custom.h"
#include "usbd_ctlreq.h"
#ifdef USE_USBD_COMPOSITE
#include "usbd_composite_builder.h"
#endif
/** @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_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum);
static uint8_t USBD_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
static uint8_t USBD_HID_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum);
static uint8_t USBD_HID_EP0_RxReady(USBD_HandleTypeDef *pdev);
static uint8_t USBD_HID_Custom_SetReport_Setup_Stage(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *setup_data);
static uint8_t USBD_HID_Custom_SetReport_Token_Stage(USBD_HandleTypeDef *pdev);
static uint8_t USBD_HID_Custom_GetReport_Token_Stage(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *setup_data);
#ifndef USE_USBD_COMPOSITE
static uint8_t *USBD_HID_GetFSCfgDesc(uint16_t *length);
static uint8_t *USBD_HID_GetHSCfgDesc(uint16_t *length);
static uint8_t *USBD_HID_GetOtherSpeedCfgDesc(uint16_t *length);
static uint8_t *USBD_HID_GetDeviceQualifierDesc(uint16_t *length);
#endif /* USE_USBD_COMPOSITE */
/**
* @}
*/
/** @defgroup USBD_HID_Private_Variables
* @{
*/
USBD_ClassTypeDef USBD_HID_CUSTOM =
{
USBD_HID_Init,
USBD_HID_DeInit,
USBD_HID_Setup,
NULL, /* EP0_TxSent */
USBD_HID_EP0_RxReady, /* EP0_RxReady */
USBD_HID_DataIn, /* DataIn */
USBD_HID_DataOut, /* DataOut */
NULL, /* SOF */
NULL,
NULL,
#ifdef USE_USBD_COMPOSITE
NULL,
NULL,
NULL,
NULL,
#else
USBD_HID_GetHSCfgDesc,
USBD_HID_GetFSCfgDesc,
USBD_HID_GetOtherSpeedCfgDesc,
USBD_HID_GetDeviceQualifierDesc,
#endif /* USE_USBD_COMPOSITE */
};
#ifndef USE_USBD_COMPOSITE
/* USB HID device FS Configuration Descriptor */
__ALIGN_BEGIN static uint8_t USBD_HID_CUSTOM_CfgDesc[USB_HID_CUSTOM_CONFIG_DESC_SIZ] __ALIGN_END =
{
0x09, /* bLength: Configuration Descriptor size */
USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
USB_HID_CUSTOM_CONFIG_DESC_SIZ, /* wTotalLength: Bytes returned */
0x00,
0x01, /* bNumInterfaces: 1 interface */
0x01, /* bConfigurationValue: Configuration value */
0x00, /* iConfiguration: Index of string descriptor
describing the configuration */
#if (USBD_SELF_POWERED == 1U)
0xE0, /* bmAttributes: Bus Powered according to user configuration */
#else
0xA0, /* bmAttributes: Bus Powered according to user configuration */
#endif /* USBD_SELF_POWERED */
USBD_MAX_POWER, /* MaxPower (mA) */
/************** Descriptor of Custom interface ****************/
/* 09 */
0x09, /* bLength: Interface Descriptor size */
USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */
0x00, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x02, /* bNumEndpoints */
0x03, /* bInterfaceClass: HID */
0x00, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
0x00, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
0, /* iInterface: Index of string descriptor */
/******************** Descriptor of Joystick Custom HID ********************/
/* 18 */
0x09, /* bLength: HID Descriptor size */
HID_CUSTOM_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_CUSTOM_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
0x00,
/******************** Descriptor of Custom endpoint ********************/
/* 27 */
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType:*/
HID_CUSTOM_EPIN_ADDR, /* bEndpointAddress: Endpoint Address (IN) */
0x03, /* bmAttributes: Interrupt endpoint */
HID_CUSTOM_EPIN_SIZE, /* wMaxPacketSize: 4 Bytes max */
0x00,
HID_CUSTOM_FS_BINTERVAL, /* bInterval: Polling Interval */
/******************** Descriptor of Custom endpoint ********************/
/* 34 */
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType:*/
HID_CUSTOM_EPOUT_ADDR, /* bEndpointAddress: Endpoint Address (IN) */
0x03, /* bmAttributes: Interrupt endpoint */
HID_CUSTOM_EPOUT_SIZE, /* wMaxPacketSize: 4 Bytes max */
0x00,
HID_CUSTOM_FS_BINTERVAL, /* bInterval: Polling Interval */
/* 41 */
};
#endif /* USE_USBD_COMPOSITE */
/* USB HID device Configuration Descriptor */
__ALIGN_BEGIN static uint8_t USBD_HID_Desc[USB_HID_CUSTOM_DESC_SIZ] __ALIGN_END =
{
/* 18 */
0x09, /* bLength: HID Descriptor size */
HID_CUSTOM_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_CUSTOM_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
0x00,
};
#ifndef USE_USBD_COMPOSITE
/* 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,
};
#endif /* USE_USBD_COMPOSITE */
__ALIGN_BEGIN static uint8_t HID_Custom_ReportDesc[HID_CUSTOM_REPORT_DESC_SIZE] __ALIGN_END =
{
0x06, 0x00, 0xff, //USAGE_PAGE(Vendor Defined Page 1)
0x09, 0x01, //USAGE(Vendor Usage 1)
0xa1, 0x01, //COLLECTION (Application)
0x85, 0x01, //REPORT ID (0x01)
0x09, 0x01, //USAGE(Vendor Usage 1)
0x15, 0x00, //LOGICAL_MINIMUM (0)
0x26, 0xff, 0x00, //LOGICAL_MAXIMUM (255)
0x95, 0x40-0x01, //REPORT_COUNT (63)
0x75, 0x08, //REPORT_SIZE (8)
0x81, 0x02, //INPUT (Data,Var,Abs)
0x85, 0x02, //REPORT ID (0x02)
0x09, 0x01, //USAGE(Vendor Usage 1)
0x15, 0x00, //LOGICAL_MINIMUM (0)
0x26, 0xff, 0x00, //LOGICAL_MAXIMUM (255)
0x95, 0x40-0x01, //REPORT_COUNT (63)
0x75, 0x08, //REPORT_SIZE (8)
0x91, 0x02, //OUTPUT (Data,Var,Abs)
0xc0, //END_COLLECTION
USB_DFU_REPORT_MAP
};
static uint8_t HIDInEpAdd = HID_CUSTOM_EPIN_ADDR;
/**
* @}
*/
/** @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)
{
UNUSED(cfgidx);
USBD_HID_Custom_HandleTypeDef *hhid;
hhid = (USBD_HID_Custom_HandleTypeDef *)USBD_malloc(sizeof(USBD_HID_Custom_HandleTypeDef));
DEBUG("sizeof(USBD_HID_Custom_HandleTypeDef)=0x%x\n",sizeof(USBD_HID_Custom_HandleTypeDef));
if (hhid == NULL)
{
pdev->pClassDataCmsit[pdev->classId] = NULL;
return (uint8_t)USBD_EMEM;
}
pdev->pClassDataCmsit[pdev->classId] = (void *)hhid;
pdev->pClassData = pdev->pClassDataCmsit[pdev->classId];
#ifdef USE_USBD_COMPOSITE
/* Get the Endpoints addresses allocated for this class instance */
HIDInEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_IN, USBD_EP_TYPE_INTR);
#endif /* USE_USBD_COMPOSITE */
if (pdev->dev_speed == USBD_SPEED_HIGH)
{
pdev->ep_in[HIDInEpAdd & 0xFU].bInterval = HID_CUSTOM_HS_BINTERVAL;
}
else /* LOW and FULL-speed endpoints */
{
pdev->ep_in[HIDInEpAdd & 0xFU].bInterval = HID_CUSTOM_FS_BINTERVAL;
}
/* Open EP IN */
(void)USBD_LL_OpenEP(pdev, HIDInEpAdd, USBD_EP_TYPE_INTR, HID_CUSTOM_EPIN_SIZE);
pdev->ep_in[HIDInEpAdd & 0xFU].is_used = 1U;
/* Open EP OUT */
(void)USBD_LL_OpenEP(pdev, HID_CUSTOM_EPOUT_ADDR, USBD_EP_TYPE_INTR, HID_CUSTOM_EPOUT_SIZE);
pdev->ep_out[HID_CUSTOM_EPOUT_ADDR & 0xFU].is_used = 1U;
/* Prepare Out endpoint to receive 1st packet */
(void)USBD_LL_PrepareReceive(pdev, HID_CUSTOM_EPOUT_ADDR, hhid->Report_buf,
HID_CUSTOM_EPOUT_SIZE);
hhid->state = CUSTOM_HID_IDLE;
return (uint8_t)USBD_OK;
}
/**
* @brief USBD_HID_DeInit
* 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)
{
UNUSED(cfgidx);
#ifdef USE_USBD_COMPOSITE
/* Get the Endpoints addresses allocated for this class instance */
HIDInEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_IN, USBD_EP_TYPE_INTR);
#endif /* USE_USBD_COMPOSITE */
/* Close HID EPs */
(void)USBD_LL_CloseEP(pdev, HIDInEpAdd);
pdev->ep_in[HIDInEpAdd & 0xFU].is_used = 0U;
pdev->ep_in[HIDInEpAdd & 0xFU].bInterval = 0U;
/* Free allocated memory */
if (pdev->pClassDataCmsit[pdev->classId] != NULL)
{
(void)USBD_free(pdev->pClassDataCmsit[pdev->classId]);
pdev->pClassDataCmsit[pdev->classId] = NULL;
}
return (uint8_t)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)
{
USBD_HID_Custom_HandleTypeDef *hhid = (USBD_HID_Custom_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
USBD_StatusTypeDef ret = USBD_OK;
uint16_t len;
uint8_t *pbuf;
uint16_t status_info = 0U;
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
switch (req->bmRequest & USB_REQ_TYPE_MASK)
{
case USB_REQ_TYPE_CLASS :
switch (req->bRequest)
{
case HID_CUSTOM_REQ_SET_PROTOCOL:
hhid->Protocol = (uint8_t)(req->wValue);
break;
case HID_CUSTOM_REQ_GET_PROTOCOL:
(void)USBD_CtlSendData(pdev, (uint8_t *)&hhid->Protocol, 1U);
break;
case HID_CUSTOM_REQ_SET_IDLE:
hhid->IdleState = (uint8_t)(req->wValue >> 8);
break;
case HID_CUSTOM_REQ_GET_IDLE:
(void)USBD_CtlSendData(pdev, (uint8_t *)&hhid->IdleState, 1U);
break;
case HID_CUSTOM_REQ_SET_REPORT:
USBD_HID_Custom_SetReport_Setup_Stage(pdev, req);
break;
case HID_CUSTOM_REQ_GET_REPORT:
USBD_HID_Custom_GetReport_Token_Stage(pdev, req);
break;
default:
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
break;
}
break;
case USB_REQ_TYPE_STANDARD:
switch (req->bRequest)
{
case USB_REQ_GET_STATUS:
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
(void)USBD_CtlSendData(pdev, (uint8_t *)&status_info, 2U);
}
else
{
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
}
break;
case USB_REQ_GET_DESCRIPTOR:
if ((req->wValue >> 8) == HID_CUSTOM_REPORT_DESC)
{
len = MIN(HID_CUSTOM_REPORT_DESC_SIZE, req->wLength);
pbuf = HID_Custom_ReportDesc;
}
else if ((req->wValue >> 8) == HID_CUSTOM_DESCRIPTOR_TYPE)
{
pbuf = USBD_HID_Desc;
len = MIN(USB_HID_CUSTOM_DESC_SIZ, req->wLength);
}
else
{
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
break;
}
(void)USBD_CtlSendData(pdev, pbuf, len);
break;
case USB_REQ_GET_INTERFACE :
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
(void)USBD_CtlSendData(pdev, (uint8_t *)&hhid->AltSetting, 1U);
}
else
{
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
}
break;
case USB_REQ_SET_INTERFACE:
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
hhid->AltSetting = (uint8_t)(req->wValue);
}
else
{
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
}
break;
case USB_REQ_CLEAR_FEATURE:
break;
default:
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
break;
}
break;
default:
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
break;
}
return (uint8_t)ret;
}
/**
* @brief USBD_HID_Custom_SendReport
* Send HID Report
* @param pdev: device instance
* @param buff: pointer to report
* @retval status
*/
uint8_t USBD_HID_Custom_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len)
{
#ifdef USE_USBD_COMPOSITE
uint8_t class_id;
class_id = USBD_CMPSIT_GetClassID_ByAdd(pdev, HID_CUSTOM_EPIN_ADDR, USBD_EP_TYPE_INTR);
USBD_HID_Custom_HandleTypeDef *hhid = (USBD_HID_Custom_HandleTypeDef *)pdev->pClassDataCmsit[class_id];
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
if (hhid->state == CUSTOM_HID_IDLE)
{
hhid->state = CUSTOM_HID_BUSY;
(void)USBD_LL_Transmit(pdev, HID_CUSTOM_EPIN_ADDR, report, len);
}
}
return (uint8_t)USBD_OK;
#else
USBD_HID_Custom_HandleTypeDef *hhid = (USBD_HID_Custom_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
if (hhid->state == CUSTOM_HID_IDLE)
{
hhid->state = CUSTOM_HID_BUSY;
(void)USBD_LL_Transmit(pdev, HID_CUSTOM_EPIN_ADDR, report, len);
}
}
return (uint8_t)USBD_OK;
#endif
}
extern uint8_t epnm_flag;
/**
* @brief USBD_HID_Custom_FirstSendReport
* Send HID Report
* @param pdev: device instance
* @param buff: pointer to report
* @retval status
*/
uint8_t USBD_HID_Custom_FirstSendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len)
{
#ifdef USE_USBD_COMPOSITE
uint8_t class_id;
uint8_t ep_id = HID_CUSTOM_EPIN_ADDR & 0xF;
class_id = USBD_CMPSIT_GetClassID_ByAdd(pdev, HID_CUSTOM_EPIN_ADDR, USBD_EP_TYPE_INTR);
USBD_HID_Custom_HandleTypeDef *hhid = (USBD_HID_Custom_HandleTypeDef *)pdev->pClassDataCmsit[class_id];
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
PCD_HandleTypeDef *pcd = (PCD_HandleTypeDef *)(pdev->pData);
uint32_t USBx_BASE = (uint32_t)(PCD_HandleTypeDef *)pcd->Instance;
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
if ((hhid->state == CUSTOM_HID_IDLE)||(epnm_flag == 0x01))
{
if(USBx_INEP(ep_id)->DIEPINT & USB_OTG_DIEPINT_ITTXFE)
{
USBx_INEP(ep_id)->DIEPINT |= USB_OTG_DIEPINT_ITTXFE;
hhid->state = CUSTOM_HID_BUSY;
(void)USBD_LL_Transmit(pdev, HID_CUSTOM_EPIN_ADDR, report, len);
}
else{
if(USBx_INEP(ep_id)->DIEPINT&USB_OTG_DIEPINT_NAK){
USBx_INEP(ep_id)->DIEPINT |= USB_OTG_DIEPINT_NAK;
USBx_INEP(ep_id)->DIEPCTL |= (USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA);
}
}
if(epnm_flag == 0x01){
epnm_flag = 0x00;
}
}
}
return (uint8_t)USBD_OK;
#else
uint8_t ep_id = HID_CUSTOM_EPIN_ADDR & 0xF;
USBD_HID_Custom_HandleTypeDef *hhid = (USBD_HID_Custom_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
PCD_HandleTypeDef *pcd = (PCD_HandleTypeDef *)(pdev->pData);
uint32_t USBx_BASE = (uint32_t)(PCD_HandleTypeDef *)pcd->Instance;
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
if ((hhid->state == CUSTOM_HID_IDLE)||(epnm_flag == 0x01))
{
if(USBx_INEP(ep_id)->DIEPINT&USB_OTG_DIEPINT_ITTXFE)
{
USBx_INEP(ep_id)->DIEPINT |= USB_OTG_DIEPINT_ITTXFE;
hhid->state = CUSTOM_HID_BUSY;
(void)USBD_LL_Transmit(pdev, HID_CUSTOM_EPIN_ADDR, report, len);
}
else{
if(USBx_INEP(ep_id)->DIEPINT&USB_OTG_DIEPINT_NAK){
USBx_INEP(ep_id)->DIEPINT |= USB_OTG_DIEPINT_NAK;
USBx_INEP(ep_id)->DIEPCTL |= (USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA);
}
}
if(epnm_flag == 0x01){
epnm_flag = 0x00;
}
}
}
return (uint8_t)USBD_OK;
#endif
}
/**
* @brief USBD_HID_GetPollingInterval
* return polling interval from endpoint descriptor
* @param pdev: device instance
* @retval polling interval
*/
uint32_t USBD_HID_Custom_GetPollingInterval(USBD_HandleTypeDef *pdev)
{
uint32_t polling_interval;
/* 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 = (((1U << (HID_CUSTOM_HS_BINTERVAL - 1U))) / 8U);
}
else /* LOW and FULL-speed endpoints */
{
/* Sets the data transfer polling interval for low and full
speed transfers */
polling_interval = HID_CUSTOM_FS_BINTERVAL;
}
return ((uint32_t)(polling_interval));
}
#ifndef USE_USBD_COMPOSITE
/**
* @brief USBD_HID_GetCfgFSDesc
* return FS configuration descriptor
* @param speed : current device speed
* @param length : pointer data length
* @retval pointer to descriptor buffer
*/
static uint8_t *USBD_HID_GetFSCfgDesc(uint16_t *length)
{
USBD_EpDescTypeDef *pEpDesc = USBD_GetEpDesc(USBD_HID_CUSTOM_CfgDesc, HID_CUSTOM_EPIN_ADDR);
if (pEpDesc != NULL)
{
pEpDesc->bInterval = HID_CUSTOM_FS_BINTERVAL;
}
*length = (uint16_t)sizeof(USBD_HID_CUSTOM_CfgDesc);
return USBD_HID_CUSTOM_CfgDesc;
}
/**
* @brief USBD_HID_GetCfgHSDesc
* return HS configuration descriptor
* @param speed : current device speed
* @param length : pointer data length
* @retval pointer to descriptor buffer
*/
static uint8_t *USBD_HID_GetHSCfgDesc(uint16_t *length)
{
USBD_EpDescTypeDef *pEpDesc = USBD_GetEpDesc(USBD_HID_CUSTOM_CfgDesc, HID_CUSTOM_EPIN_ADDR);
if (pEpDesc != NULL)
{
pEpDesc->bInterval = HID_CUSTOM_HS_BINTERVAL;
}
*length = (uint16_t)sizeof(USBD_HID_CUSTOM_CfgDesc);
return USBD_HID_CUSTOM_CfgDesc;
}
/**
* @brief USBD_HID_GetOtherSpeedCfgDesc
* return other speed configuration descriptor
* @param speed : current device speed
* @param length : pointer data length
* @retval pointer to descriptor buffer
*/
static uint8_t *USBD_HID_GetOtherSpeedCfgDesc(uint16_t *length)
{
USBD_EpDescTypeDef *pEpDesc = USBD_GetEpDesc(USBD_HID_CUSTOM_CfgDesc, HID_CUSTOM_EPIN_ADDR);
if (pEpDesc != NULL)
{
pEpDesc->bInterval = HID_CUSTOM_FS_BINTERVAL;
}
*length = (uint16_t)sizeof(USBD_HID_CUSTOM_CfgDesc);
return USBD_HID_CUSTOM_CfgDesc;
}
#endif /* USE_USBD_COMPOSITE */
/**
* @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)
{
UNUSED(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_Custom_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId])->state = CUSTOM_HID_IDLE;
return (uint8_t)USBD_OK;
}
extern PCD_HandleTypeDef hpcd_USB_OTG_FS;
/**
* @brief USBD_HID_DataOut
* handle data OUT Stage
* @param pdev: device instance
* @param epnum: endpoint index
* @retval status
*/
static uint8_t USBD_HID_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum)
{
UNUSED(epnum);
uint8_t size_num;
USBD_HID_Custom_HandleTypeDef *hhid;
hhid = pdev->pClassData;
/* 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 */
size_num = HAL_PCD_EP_GetRxCount(&hpcd_USB_OTG_FS,HID_CUSTOM_EPOUT_ADDR);
(void)USBD_LL_PrepareReceive(pdev, HID_CUSTOM_EPOUT_ADDR, hhid->Report_buf,HID_CUSTOM_EPOUT_SIZE);
for(int i=0;i<size_num;i++)
{
DEBUG("0x%x ",hhid->Report_buf[i]);
}
DEBUG("\n");
hhid->state = CUSTOM_HID_IDLE;
return (uint8_t)USBD_OK;
}
static uint8_t USBD_HID_EP0_RxReady(USBD_HandleTypeDef *pdev)
{
DEBUG("Custom USBD_HID_EP0_RxReady\r\n");
USBD_HID_Custom_SetReport_Token_Stage(pdev);
return (uint8_t)USBD_OK;
}
uint8_t USBD_HID_Custom_SetReport_Setup_Stage(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *setup_data)
{
uint8_t report_type;
uint8_t report_id;
#ifdef USE_USBD_COMPOSITE
uint8_t class_id;
class_id = USBD_CMPSIT_GetClassID_ByAdd(pdev, HID_CUSTOM_EPIN_ADDR, USBD_EP_TYPE_INTR);
USBD_HID_Custom_HandleTypeDef *hhid = (USBD_HID_Custom_HandleTypeDef *)pdev->pClassDataCmsit[class_id];
#else
USBD_HID_Custom_HandleTypeDef *hhid = (USBD_HID_Custom_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
#endif
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
report_type = (setup_data->wValue >> 8) & 0xFF;
report_id = setup_data->wValue & 0xFF;
hhid->EP0_ReportLen = setup_data->wLength;
DEBUG("Setup_Stage report_type=%02x, report_id=%02x, report_len=%d\r\n",report_type, report_id, hhid->EP0_ReportLen);
(void)USBD_CtlPrepareRx(pdev, hhid->EP0_ReportBuff, hhid->EP0_ReportLen);
return (uint8_t)USBD_OK;
}
uint8_t USBD_HID_Custom_SetReport_Token_Stage(USBD_HandleTypeDef *pdev)
{
#ifdef USE_USBD_COMPOSITE
uint8_t class_id;
class_id = USBD_CMPSIT_GetClassID_ByAdd(pdev, HID_CUSTOM_EPIN_ADDR, USBD_EP_TYPE_INTR);
USBD_HID_Custom_HandleTypeDef *hhid = (USBD_HID_Custom_HandleTypeDef *)pdev->pClassDataCmsit[class_id];
#else
USBD_HID_Custom_HandleTypeDef *hhid = (USBD_HID_Custom_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
#endif
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
DEBUG("Token_Stage recv len = %d\n", hhid->EP0_ReportLen);
for(int i=0;i<hhid->EP0_ReportLen;i++){
DEBUG("0x%x ", hhid->EP0_ReportBuff[i]);
}DEBUG("\n");
return (uint8_t)USBD_OK;
}
uint8_t USBD_HID_Custom_GetReport_Token_Stage(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *setup_data)
{
uint16_t report_length;
uint8_t data[HID_CUSTOM_EP0_GET_REPORT_BUF_SIZE];
DEBUG("%s,%d\n",__func__,__LINE__);
#ifdef USE_USBD_COMPOSITE
uint8_t class_id;
class_id = USBD_CMPSIT_GetClassID_ByAdd(pdev, HID_CUSTOM_EPIN_ADDR, USBD_EP_TYPE_INTR);
USBD_HID_Custom_HandleTypeDef *hhid = (USBD_HID_Custom_HandleTypeDef *)pdev->pClassDataCmsit[class_id];
#else
USBD_HID_Custom_HandleTypeDef *hhid = (USBD_HID_Custom_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
#endif
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
for(int i=0;i<HID_CUSTOM_EP0_GET_REPORT_BUF_SIZE;i++)
{
data[i] = i;
}
report_length = setup_data->wLength;
USBD_CtlSendData(pdev, data, report_length);
return (uint8_t)USBD_OK;
}
#ifndef USE_USBD_COMPOSITE
/**
* @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 = (uint16_t)sizeof(USBD_HID_DeviceQualifierDesc);
return USBD_HID_DeviceQualifierDesc;
}
#endif /* USE_USBD_COMPOSITE */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,864 @@
/*!
* \file usbd_hid_keyboard.c
*
* \brief This file provides the HID core functions.
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
*
* _ __ _ ________ _
* | |/ /(_)___ / ____/ /_ (_)___
* | // / __ \/ / / __ \/ / __ \
* / |/ / / / / /___/ / / / / /_/ /
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
* /_/
* (C) 2022-2025 XinChip
*
* \endcode
*
* \author MCD Application Team
*
* \author ( XinChip ) Alex-J
*
* @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 : Keyboard
* - 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
*
*/
/* Includes ------------------------------------------------------------------*/
#include "usbd_hid_keyboard.h"
#include "usbd_ctlreq.h"
#ifdef USE_USBD_COMPOSITE
#include "usbd_composite_builder.h"
#endif
/** @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_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum);
static uint8_t USBD_HID_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum);
static uint8_t USBD_HID_EP0_RxReady(USBD_HandleTypeDef *pdev);
static uint8_t USBD_HID_Keyboard_SetReport_Setup_Stage(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *setup_data);
static uint8_t USBD_HID_Keyboard_SetReport_Token_Stage(USBD_HandleTypeDef *pdev);
static uint8_t USBD_HID_Keyboard_GetReport_Token_Stage(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *setup_data);
#ifndef USE_USBD_COMPOSITE
static uint8_t *USBD_HID_GetFSCfgDesc(uint16_t *length);
static uint8_t *USBD_HID_GetHSCfgDesc(uint16_t *length);
static uint8_t *USBD_HID_GetOtherSpeedCfgDesc(uint16_t *length);
static uint8_t *USBD_HID_GetDeviceQualifierDesc(uint16_t *length);
#endif /* USE_USBD_COMPOSITE */
/**
* @}
*/
/** @defgroup USBD_HID_Private_Variables
* @{
*/
USBD_ClassTypeDef USBD_HID_KEYBOARD =
{
USBD_HID_Init,
USBD_HID_DeInit,
USBD_HID_Setup,
NULL, /* EP0_TxSent */
USBD_HID_EP0_RxReady, /* EP0_RxReady */
USBD_HID_DataIn, /* DataIn */
USBD_HID_DataOut, /* DataOut */
NULL, /* SOF */
NULL,
NULL,
#ifdef USE_USBD_COMPOSITE
NULL,
NULL,
NULL,
NULL,
#else
USBD_HID_GetHSCfgDesc,
USBD_HID_GetFSCfgDesc,
USBD_HID_GetOtherSpeedCfgDesc,
USBD_HID_GetDeviceQualifierDesc,
#endif /* USE_USBD_COMPOSITE */
};
#ifndef USE_USBD_COMPOSITE
/* USB HID device FS Configuration Descriptor */
__ALIGN_BEGIN static uint8_t USBD_HID_CfgDesc[USB_HID_KEYBOARD_CONFIG_DESC_SIZ] __ALIGN_END =
{
0x09, /* bLength: Configuration Descriptor size */
USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
USB_HID_KEYBOARD_CONFIG_DESC_SIZ, /* wTotalLength: Bytes returned */
0x00,
0x01, /* bNumInterfaces: 1 interface */
0x01, /* bConfigurationValue: Configuration value */
0x00, /* iConfiguration: Index of string descriptor
describing the configuration */
#if (USBD_SELF_POWERED == 1U)
0xE0, /* bmAttributes: Bus Powered according to user configuration */
#else
0xA0, /* bmAttributes: Bus Powered according to user configuration */
#endif /* USBD_SELF_POWERED */
USBD_MAX_POWER, /* MaxPower (mA) */
/************** Descriptor of Keyboard interface ****************/
/* 09 */
0x09, /* bLength: Interface Descriptor size */
USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */
0x00, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x02, /* bNumEndpoints */
0x03, /* bInterfaceClass: HID */
0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
0x01, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
0, /* iInterface: Index of string descriptor */
/******************** Descriptor of Keyboard HID ********************/
/* 18 */
0x09, /* bLength: HID Descriptor size */
HID_KEYBOARD_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_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
0x00,
/******************** Descriptor of Keyboard endpoint ********************/
/* 27 */
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType:*/
HID_KEYBOARD_EPIN_ADDR, /* bEndpointAddress: Endpoint Address (IN) */
0x03, /* bmAttributes: Interrupt endpoint */
HID_KEYBOARD_EPIN_SIZE, /* wMaxPacketSize: 4 Byte max */
0x00,
HID_KEYBOARD_FS_BINTERVAL, /* bInterval: Polling Interval */
/* 34 */
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType:*/
HID_KEYBOARD_EPOUT_ADDR, /* bEndpointAddress: Endpoint Address (IN) */
0x03, /* bmAttributes: Interrupt endpoint */
HID_KEYBOARD_EPOUT_SIZE, /* wMaxPacketSize: 4 Byte max */
0x00,
HID_KEYBOARD_FS_BINTERVAL, /* bInterval: Polling Interval */
/* 41 */
};
#endif /* USE_USBD_COMPOSITE */
/* USB HID device Configuration Descriptor */
__ALIGN_BEGIN static uint8_t USBD_HID_Desc[USB_HID_KEYBOARD_DESC_SIZ] __ALIGN_END =
{
/* 18 */
0x09, /* bLength: HID Descriptor size */
HID_KEYBOARD_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_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor */
0x00,
};
#ifndef USE_USBD_COMPOSITE
/* 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,
};
#endif /* USE_USBD_COMPOSITE */
__ALIGN_BEGIN static uint8_t HID_Keyboard_ReportDesc[HID_KEYBOARD_REPORT_DESC_SIZE] __ALIGN_END =
{
0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
0x09, 0x06, // Usage (Keyboard)
0xA1, 0x01, // Collection (Application)
0x05, 0x07, // Usage Page (Kbrd/Keypad)
0x19, 0xE0, // Usage Minimum (0xE0)
0x29, 0xE7, // Usage Maximum (0xE7)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x75, 0x01, // Report Size (1)
0x95, 0x08, // Report Count (8)
0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x95, 0x01, // Report Count (1)
0x75, 0x08, // Report Size (8)
0x81, 0x03, // Input (Const,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x95, 0x05, // Report Count (5)
0x75, 0x01, // Report Size (1)
0x05, 0x08, // Usage Page (LEDs)
0x19, 0x01, // Usage Minimum (Num Lock)
0x29, 0x05, // Usage Maximum (Kana)
0x91, 0x02, // Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
0x95, 0x01, // Report Count (1)
0x75, 0x03, // Report Size (3)
0x91, 0x03, // Output (Const,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
0x95, 0x06, // Report Count (6)
0x75, 0x08, // Report Size (8)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x65, // Logical Maximum (101)
0x05, 0x07, // Usage Page (Kbrd/Keypad)
0x19, 0x00, // Usage Minimum (Reserved (no event indicated))
0x29, 0x65, // Usage Maximum (0x65)
0x81, 0x00, // Input (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
0xC0, // End Collection
};
static uint8_t HIDInEpAdd = HID_KEYBOARD_EPIN_ADDR;
/**
* @}
*/
/** @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)
{
UNUSED(cfgidx);
USBD_HID_Keyboard_HandleTypeDef *hhid;
hhid = (USBD_HID_Keyboard_HandleTypeDef *)USBD_malloc(sizeof(USBD_HID_Keyboard_HandleTypeDef));
DEBUG("sizeof(USBD_HID_Keyboard_HandleTypeDef)=0x%x\n",sizeof(USBD_HID_Keyboard_HandleTypeDef));
if (hhid == NULL)
{
pdev->pClassDataCmsit[pdev->classId] = NULL;
return (uint8_t)USBD_EMEM;
}
pdev->pClassDataCmsit[pdev->classId] = (void *)hhid;
pdev->pClassData = pdev->pClassDataCmsit[pdev->classId];
#ifdef USE_USBD_COMPOSITE
/* Get the Endpoints addresses allocated for this class instance */
HIDInEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_IN, USBD_EP_TYPE_INTR);
#endif /* USE_USBD_COMPOSITE */
if (pdev->dev_speed == USBD_SPEED_HIGH)
{
pdev->ep_in[HIDInEpAdd & 0xFU].bInterval = HID_KEYBOARD_HS_BINTERVAL;
}
else /* LOW and FULL-speed endpoints */
{
pdev->ep_in[HIDInEpAdd & 0xFU].bInterval = HID_KEYBOARD_FS_BINTERVAL;
}
/* Open EP IN */
(void)USBD_LL_OpenEP(pdev, HIDInEpAdd, USBD_EP_TYPE_INTR, HID_KEYBOARD_EPIN_SIZE);
pdev->ep_in[HIDInEpAdd & 0xFU].is_used = 1U;
/* Open EP OUT */
(void)USBD_LL_OpenEP(pdev, HID_KEYBOARD_EPOUT_ADDR, USBD_EP_TYPE_INTR, HID_KEYBOARD_EPOUT_SIZE);
pdev->ep_out[HID_KEYBOARD_EPOUT_ADDR & 0xFU].is_used = 1U;
/* Prepare Out endpoint to receive 1st packet */
(void)USBD_LL_PrepareReceive(pdev, HID_KEYBOARD_EPOUT_ADDR, hhid->Report_buf,
USBD_KEYBOARD_OUTREPORT_BUF_SIZE);
hhid->state = KEYBOARD_HID_IDLE;
return (uint8_t)USBD_OK;
}
/**
* @brief USBD_HID_DeInit
* 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)
{
UNUSED(cfgidx);
#ifdef USE_USBD_COMPOSITE
/* Get the Endpoints addresses allocated for this class instance */
HIDInEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_IN, USBD_EP_TYPE_INTR);
#endif /* USE_USBD_COMPOSITE */
/* Close HID EPs */
(void)USBD_LL_CloseEP(pdev, HIDInEpAdd);
pdev->ep_in[HIDInEpAdd & 0xFU].is_used = 0U;
pdev->ep_in[HIDInEpAdd & 0xFU].bInterval = 0U;
/* Free allocated memory */
if (pdev->pClassDataCmsit[pdev->classId] != NULL)
{
(void)USBD_free(pdev->pClassDataCmsit[pdev->classId]);
pdev->pClassDataCmsit[pdev->classId] = NULL;
}
return (uint8_t)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)
{
USBD_HID_Keyboard_HandleTypeDef *hhid = (USBD_HID_Keyboard_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
USBD_StatusTypeDef ret = USBD_OK;
uint16_t len;
uint8_t *pbuf;
uint16_t status_info = 0U;
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
switch (req->bmRequest & USB_REQ_TYPE_MASK)
{
case USB_REQ_TYPE_CLASS :
switch (req->bRequest)
{
case HID_KEYBOARD_REQ_SET_PROTOCOL:
hhid->Protocol = (uint8_t)(req->wValue);
break;
case HID_KEYBOARD_REQ_GET_PROTOCOL:
(void)USBD_CtlSendData(pdev, (uint8_t *)&hhid->Protocol, 1U);
break;
case HID_KEYBOARD_REQ_SET_IDLE:
hhid->IdleState = (uint8_t)(req->wValue >> 8);
break;
case HID_KEYBOARD_REQ_GET_IDLE:
(void)USBD_CtlSendData(pdev, (uint8_t *)&hhid->IdleState, 1U);
break;
case HID_KEYBOARD_REQ_SET_REPORT:
USBD_HID_Keyboard_SetReport_Setup_Stage(pdev, req);
break;
case HID_KEYBOARD_REQ_GET_REPORT:
USBD_HID_Keyboard_GetReport_Token_Stage(pdev, req);
break;
default:
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
break;
}
break;
case USB_REQ_TYPE_STANDARD:
switch (req->bRequest)
{
case USB_REQ_GET_STATUS:
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
(void)USBD_CtlSendData(pdev, (uint8_t *)&status_info, 2U);
}
else
{
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
}
break;
case USB_REQ_GET_DESCRIPTOR:
if ((req->wValue >> 8) == HID_KEYBOARD_REPORT_DESC)
{
len = MIN(HID_KEYBOARD_REPORT_DESC_SIZE, req->wLength);
pbuf = HID_Keyboard_ReportDesc;
}
else if ((req->wValue >> 8) == HID_KEYBOARD_DESCRIPTOR_TYPE)
{
pbuf = USBD_HID_Desc;
len = MIN(USB_HID_KEYBOARD_DESC_SIZ, req->wLength);
}
else
{
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
break;
}
(void)USBD_CtlSendData(pdev, pbuf, len);
break;
case USB_REQ_GET_INTERFACE :
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
(void)USBD_CtlSendData(pdev, (uint8_t *)&hhid->AltSetting, 1U);
}
else
{
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
}
break;
case USB_REQ_SET_INTERFACE:
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
hhid->AltSetting = (uint8_t)(req->wValue);
}
else
{
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
}
break;
case USB_REQ_CLEAR_FEATURE:
break;
default:
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
break;
}
break;
default:
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
break;
}
return (uint8_t)ret;
}
/**
* @brief USBD_HID_Keyboard_SendReport
* Send HID Report
* @param pdev: device instance
* @param buff: pointer to report
* @retval status
*/
uint8_t USBD_HID_Keyboard_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len)
{
#ifdef USE_USBD_COMPOSITE
uint8_t class_id;
class_id = USBD_CMPSIT_GetClassID_ByAdd(pdev, HID_KEYBOARD_EPIN_ADDR, USBD_EP_TYPE_INTR);
USBD_HID_Keyboard_HandleTypeDef *hhid = (USBD_HID_Keyboard_HandleTypeDef *)pdev->pClassDataCmsit[class_id];
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
if (hhid->state == KEYBOARD_HID_IDLE)
{
hhid->state = KEYBOARD_HID_BUSY;
(void)USBD_LL_Transmit(pdev, HID_KEYBOARD_EPIN_ADDR, report, len);
}
}
#endif
return (uint8_t)USBD_OK;
}
extern uint8_t epnm_flag;
uint8_t USBD_HID_Keyboard_FirstSendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len)
{
#ifdef USE_USBD_COMPOSITE
static uint8_t first_send_flag = 0;
uint8_t class_id;
uint8_t ep_id = HID_KEYBOARD_EPIN_ADDR & 0xF;
if(first_send_flag == 1){
return (uint8_t)USBD_OK;
}
class_id = USBD_CMPSIT_GetClassID_ByAdd(pdev, HID_KEYBOARD_EPIN_ADDR, USBD_EP_TYPE_INTR);
USBD_HID_Keyboard_HandleTypeDef *hhid = (USBD_HID_Keyboard_HandleTypeDef *)pdev->pClassDataCmsit[class_id];
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
PCD_HandleTypeDef *pcd = (PCD_HandleTypeDef *)(pdev->pData);
uint32_t USBx_BASE = (uint32_t)(PCD_HandleTypeDef *)pcd->Instance;
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
if ((hhid->state == KEYBOARD_HID_IDLE)||(epnm_flag == 0x01))
{
if(USBx_INEP(ep_id)->DIEPINT&USB_OTG_DIEPINT_ITTXFE)
{
USBx_INEP(ep_id)->DIEPINT |= USB_OTG_DIEPINT_ITTXFE;
hhid->state = KEYBOARD_HID_BUSY;
(void)USBD_LL_Transmit(pdev, HID_KEYBOARD_EPIN_ADDR, report, len);
}
else{
if(USBx_INEP(ep_id)->DIEPINT&USB_OTG_DIEPINT_NAK){
USBx_INEP(ep_id)->DIEPINT |= USB_OTG_DIEPINT_NAK;
USBx_INEP(ep_id)->DIEPCTL |= (USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA);
}
}
if(epnm_flag == 0x01){
epnm_flag = 0x00;
}
}
}
return (uint8_t)USBD_OK;
#else
uint8_t ep_id = HID_KEYBOARD_EPIN_ADDR & 0xF;
USBD_HID_Keyboard_HandleTypeDef *hhid = (USBD_HID_Keyboard_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
PCD_HandleTypeDef *pcd = (PCD_HandleTypeDef *)(pdev->pData);
uint32_t USBx_BASE = (uint32_t)(PCD_HandleTypeDef *)pcd->Instance;
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
if ((hhid->state == KEYBOARD_HID_IDLE)||(epnm_flag == 0x01))
{
if(USBx_INEP(ep_id)->DIEPINT&USB_OTG_DIEPINT_ITTXFE)
{
USBx_INEP(ep_id)->DIEPINT |= USB_OTG_DIEPINT_ITTXFE ;
hhid->state = KEYBOARD_HID_BUSY;
(void)USBD_LL_Transmit(pdev, HID_KEYBOARD_EPIN_ADDR, report, len);
}
else{
if(USBx_INEP(ep_id)->DIEPINT&USB_OTG_DIEPINT_NAK){
USBx_INEP(ep_id)->DIEPINT |= USB_OTG_DIEPINT_NAK;
USBx_INEP(ep_id)->DIEPCTL |= (USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA);
}
}
if(epnm_flag == 0x01){
epnm_flag = 0x00;
}
}
}
return (uint8_t)USBD_OK;
#endif
}
/**
* @brief USBD_HID_GetPollingInterval
* return polling interval from endpoint descriptor
* @param pdev: device instance
* @retval polling interval
*/
uint32_t USBD_HID_KEYBOARD_GetPollingInterval(USBD_HandleTypeDef *pdev)
{
uint32_t polling_interval;
/* 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 = (((1U << (HID_KEYBOARD_HS_BINTERVAL - 1U))) / 8U);
}
else /* LOW and FULL-speed endpoints */
{
/* Sets the data transfer polling interval for low and full
speed transfers */
polling_interval = HID_KEYBOARD_FS_BINTERVAL;
}
return ((uint32_t)(polling_interval));
}
#ifndef USE_USBD_COMPOSITE
/**
* @brief USBD_HID_GetCfgFSDesc
* return FS configuration descriptor
* @param speed : current device speed
* @param length : pointer data length
* @retval pointer to descriptor buffer
*/
static uint8_t *USBD_HID_GetFSCfgDesc(uint16_t *length)
{
USBD_EpDescTypeDef *pEpDesc = USBD_GetEpDesc(USBD_HID_CfgDesc, HID_KEYBOARD_EPIN_ADDR);
if (pEpDesc != NULL)
{
pEpDesc->bInterval = HID_KEYBOARD_FS_BINTERVAL;
}
*length = (uint16_t)sizeof(USBD_HID_CfgDesc);
return USBD_HID_CfgDesc;
}
/**
* @brief USBD_HID_GetCfgHSDesc
* return HS configuration descriptor
* @param speed : current device speed
* @param length : pointer data length
* @retval pointer to descriptor buffer
*/
static uint8_t *USBD_HID_GetHSCfgDesc(uint16_t *length)
{
USBD_EpDescTypeDef *pEpDesc = USBD_GetEpDesc(USBD_HID_CfgDesc, HID_KEYBOARD_EPIN_ADDR);
if (pEpDesc != NULL)
{
pEpDesc->bInterval = HID_KEYBOARD_HS_BINTERVAL;
}
*length = (uint16_t)sizeof(USBD_HID_CfgDesc);
return USBD_HID_CfgDesc;
}
/**
* @brief USBD_HID_GetOtherSpeedCfgDesc
* return other speed configuration descriptor
* @param speed : current device speed
* @param length : pointer data length
* @retval pointer to descriptor buffer
*/
static uint8_t *USBD_HID_GetOtherSpeedCfgDesc(uint16_t *length)
{
USBD_EpDescTypeDef *pEpDesc = USBD_GetEpDesc(USBD_HID_CfgDesc, HID_KEYBOARD_EPIN_ADDR);
if (pEpDesc != NULL)
{
pEpDesc->bInterval = HID_KEYBOARD_FS_BINTERVAL;
}
*length = (uint16_t)sizeof(USBD_HID_CfgDesc);
return USBD_HID_CfgDesc;
}
#endif /* USE_USBD_COMPOSITE */
/**
* @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)
{
UNUSED(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_Keyboard_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId])->state = KEYBOARD_HID_IDLE;
return (uint8_t)USBD_OK;
}
extern PCD_HandleTypeDef hpcd_USB_OTG_FS;
/**
* @brief USBD_CUSTOM_HID_DataOut
* handle data OUT Stage
* @param pdev: device instance
* @param epnum: endpoint index
* @retval status
*/
static uint8_t USBD_HID_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum)
{
DEBUG("line=%d,epnum=0x%x\n",__LINE__,epnum);
uint8_t size_num;
USBD_HID_Keyboard_HandleTypeDef *hhid;
hhid = pdev->pClassData;
/* 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 */
size_num = HAL_PCD_EP_GetRxCount(&hpcd_USB_OTG_FS,HID_KEYBOARD_EPOUT_ADDR);
(void)USBD_LL_PrepareReceive(pdev, HID_KEYBOARD_EPOUT_ADDR, hhid->Report_buf,HID_KEYBOARD_EPOUT_SIZE);
for(int i=0;i<size_num;i++)
{
DEBUG("0x%x ", hhid->Report_buf[i]);
}
DEBUG("\n");
hhid->state = KEYBOARD_HID_IDLE;
return (uint8_t)USBD_OK;
}
static uint8_t USBD_HID_EP0_RxReady(USBD_HandleTypeDef *pdev)
{
DEBUG("Keyboard USBD_HID_EP0_RxReady\r\n");
USBD_HID_Keyboard_SetReport_Token_Stage(pdev);
return (uint8_t)USBD_OK;
}
uint8_t USBD_HID_Keyboard_SetReport_Setup_Stage(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *setup_data)
{
uint8_t report_type;
uint8_t report_id;
#ifdef USE_USBD_COMPOSITE
uint8_t class_id;
class_id = USBD_CMPSIT_GetClassID_ByAdd(pdev, HID_KEYBOARD_EPIN_ADDR, USBD_EP_TYPE_INTR);
USBD_HID_Keyboard_HandleTypeDef *hhid = (USBD_HID_Keyboard_HandleTypeDef *)pdev->pClassDataCmsit[class_id];
#else
USBD_HID_Keyboard_HandleTypeDef *hhid = (USBD_HID_Keyboard_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
#endif
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
report_type = (setup_data->wValue >> 8) & 0xFF;
report_id = setup_data->wValue & 0xFF;
hhid->EP0_ReportLen = setup_data->wLength;
DEBUG("Setup_Stage report_type=%02x, report_id=%02x, report_len=%d\r\n",report_type, report_id, hhid->EP0_ReportLen);
(void)USBD_CtlPrepareRx(pdev, hhid->EP0_ReportBuff, hhid->EP0_ReportLen);
return (uint8_t)USBD_OK;
}
uint8_t USBD_HID_Keyboard_SetReport_Token_Stage(USBD_HandleTypeDef *pdev)
{
#ifdef USE_USBD_COMPOSITE
uint8_t class_id;
class_id = USBD_CMPSIT_GetClassID_ByAdd(pdev, HID_KEYBOARD_EPIN_ADDR, USBD_EP_TYPE_INTR);
USBD_HID_Keyboard_HandleTypeDef *hhid = (USBD_HID_Keyboard_HandleTypeDef *)pdev->pClassDataCmsit[class_id];
#else
USBD_HID_Keyboard_HandleTypeDef *hhid = (USBD_HID_Keyboard_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
#endif
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
DEBUG("Token_Stage recv len = %d\n", hhid->EP0_ReportLen);
for(int i=0;i<hhid->EP0_ReportLen;i++){
DEBUG("0x%x ", hhid->EP0_ReportBuff[i]);
}DEBUG("\n");
return (uint8_t)USBD_OK;
}
uint8_t USBD_HID_Keyboard_GetReport_Token_Stage(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *setup_data)
{
uint16_t report_length;
uint8_t data[HID_KEYBOARD_EP0_GET_REPORT_BUF_SIZE];
DEBUG("%s,%d\n",__func__,__LINE__);
#ifdef USE_USBD_COMPOSITE
uint8_t class_id;
class_id = USBD_CMPSIT_GetClassID_ByAdd(pdev, HID_KEYBOARD_EPIN_ADDR, USBD_EP_TYPE_INTR);
USBD_HID_Keyboard_HandleTypeDef *hhid = (USBD_HID_Keyboard_HandleTypeDef *)pdev->pClassDataCmsit[class_id];
#else
USBD_HID_Keyboard_HandleTypeDef *hhid = (USBD_HID_Keyboard_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
#endif
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
for(int i=0;i<HID_KEYBOARD_EP0_GET_REPORT_BUF_SIZE;i++)
{
data[i] = i;
}
report_length = setup_data->wLength;
USBD_CtlSendData(pdev, data, report_length);
return (uint8_t)USBD_OK;
}
#ifndef USE_USBD_COMPOSITE
/**
* @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 = (uint16_t)sizeof(USBD_HID_DeviceQualifierDesc);
return USBD_HID_DeviceQualifierDesc;
}
#endif /* USE_USBD_COMPOSITE */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,842 @@
/*!
* \file usbd_hid_mouse.c
*
* \brief This file provides the HID core functions.
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
*
* _ __ _ ________ _
* | |/ /(_)___ / ____/ /_ (_)___
* | // / __ \/ / / __ \/ / __ \
* / |/ / / / / /___/ / / / / /_/ /
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
* /_/
* (C) 2022-2025 XinChip
*
* \endcode
*
* \author MCD Application Team
*
* \author ( XinChip ) Alex-J
*
* @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 : Mouse
* - 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
*
*/
/* Includes ------------------------------------------------------------------*/
#include "usbd_hid_mouse.h"
#include "usbd_ctlreq.h"
#ifdef USE_USBD_COMPOSITE
#include "usbd_composite_builder.h"
#endif
/** @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_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum);
static uint8_t USBD_HID_EP0_RxReady(USBD_HandleTypeDef *pdev);
static uint8_t USBD_HID_Mouse_SetReport_Setup_Stage(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *setup_data);
static uint8_t USBD_HID_Mouse_SetReport_Token_Stage(USBD_HandleTypeDef *pdev);
static uint8_t USBD_HID_Mouse_GetReport_Token_Stage(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *setup_data);
#ifndef USE_USBD_COMPOSITE
static uint8_t *USBD_HID_GetFSCfgDesc(uint16_t *length);
static uint8_t *USBD_HID_GetHSCfgDesc(uint16_t *length);
static uint8_t *USBD_HID_GetOtherSpeedCfgDesc(uint16_t *length);
static uint8_t *USBD_HID_GetDeviceQualifierDesc(uint16_t *length);
#endif /* USE_USBD_COMPOSITE */
/**
* @}
*/
/** @defgroup USBD_HID_Private_Variables
* @{
*/
USBD_ClassTypeDef USBD_HID_MOUSE =
{
USBD_HID_Init,
USBD_HID_DeInit,
USBD_HID_Setup,
NULL, /* EP0_TxSent */
USBD_HID_EP0_RxReady, /* EP0_RxReady */
USBD_HID_DataIn, /* DataIn */
NULL, /* DataOut */
NULL, /* SOF */
NULL,
NULL,
#ifdef USE_USBD_COMPOSITE
NULL,
NULL,
NULL,
NULL,
#else
USBD_HID_GetHSCfgDesc,
USBD_HID_GetFSCfgDesc,
USBD_HID_GetOtherSpeedCfgDesc,
USBD_HID_GetDeviceQualifierDesc,
#endif /* USE_USBD_COMPOSITE */
};
#ifndef USE_USBD_COMPOSITE
/* USB HID device FS Configuration Descriptor */
__ALIGN_BEGIN static uint8_t USBD_HID_CfgDesc[USB_HID_MOUSE_CONFIG_DESC_SIZ] __ALIGN_END =
{
0x09, /* bLength: Configuration Descriptor size */
USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
USB_HID_MOUSE_CONFIG_DESC_SIZ, /* wTotalLength: Bytes returned */
0x00,
0x01, /* bNumInterfaces: 1 interface */
0x01, /* bConfigurationValue: Configuration value */
0x00, /* iConfiguration: Index of string descriptor
describing the configuration */
#if (USBD_SELF_POWERED == 1U)
0xE0, /* bmAttributes: Bus Powered according to user configuration */
#else
0xA0, /* bmAttributes: Bus Powered according to user configuration */
#endif /* USBD_SELF_POWERED */
USBD_MAX_POWER, /* MaxPower (mA) */
/************** 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_MOUSE_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_MOUSE_EPIN_ADDR, /* bEndpointAddress: Endpoint Address (IN) */
0x03, /* bmAttributes: Interrupt endpoint */
HID_MOUSE_EPIN_SIZE, /* wMaxPacketSize: 4 Bytes max */
0x00,
HID_MOUSE_FS_BINTERVAL, /* bInterval: Polling Interval */
/* 34 */
};
#endif /* USE_USBD_COMPOSITE */
/* USB HID device Configuration Descriptor */
__ALIGN_BEGIN static uint8_t USBD_HID_Desc[USB_HID_MOUSE_DESC_SIZ] __ALIGN_END =
{
/* 18 */
0x09, /* bLength: HID Descriptor size */
HID_MOUSE_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,
};
#ifndef USE_USBD_COMPOSITE
/* 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,
};
#endif /* USE_USBD_COMPOSITE */
__ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE] __ALIGN_END =
{
0x05, 0x01, /* Usage Page (Generic Desktop Ctrls) */
0x09, 0x02, /* Usage (Mouse) */
0xA1, 0x01, /* Collection (Application) */
0x09, 0x01, /* Usage (Pointer) */
0xA1, 0x00, /* Collection (Physical) */
0x05, 0x09, /* Usage Page (Button) */
0x19, 0x01, /* Usage Minimum (0x01) */
0x29, 0x03, /* Usage Maximum (0x03) */
0x15, 0x00, /* Logical Minimum (0) */
0x25, 0x01, /* Logical Maximum (1) */
0x95, 0x03, /* Report Count (3) */
0x75, 0x01, /* Report Size (1) */
0x81, 0x02, /* Input (Data,Var,Abs) */
0x95, 0x01, /* Report Count (1) */
0x75, 0x05, /* Report Size (5) */
0x81, 0x01, /* Input (Const,Array,Abs) */
0x05, 0x01, /* Usage Page (Generic Desktop Ctrls) */
0x09, 0x30, /* Usage (X) */
0x09, 0x31, /* Usage (Y) */
0x09, 0x38, /* Usage (Wheel) */
0x15, 0x81, /* Logical Minimum (-127) */
0x25, 0x7F, /* Logical Maximum (127) */
0x75, 0x08, /* Report Size (8) */
0x95, 0x03, /* Report Count (3) */
0x81, 0x06, /* Input (Data,Var,Rel) */
0xC0, /* End Collection */
0x09, 0x3C, /* Usage (Motion Wakeup) */
0x05, 0xFF, /* Usage Page (Reserved 0xFF) */
0x09, 0x01, /* Usage (0x01) */
0x15, 0x00, /* Logical Minimum (0) */
0x25, 0x01, /* Logical Maximum (1) */
0x75, 0x01, /* Report Size (1) */
0x95, 0x02, /* Report Count (2) */
0xB1, 0x22, /* Feature (Data,Var,Abs,NoWrp) */
0x75, 0x06, /* Report Size (6) */
0x95, 0x01, /* Report Count (1) */
0xB1, 0x01, /* Feature (Const,Array,Abs,NoWrp) */
0xC0 /* End Collection */
};
static uint8_t HIDInEpAdd = HID_MOUSE_EPIN_ADDR;
/**
* @}
*/
/** @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)
{
UNUSED(cfgidx);
USBD_HID_Mouse_HandleTypeDef *hhid;
hhid = (USBD_HID_Mouse_HandleTypeDef *)USBD_malloc(sizeof(USBD_HID_Mouse_HandleTypeDef));
printf("sizeof(USBD_HID_Mouse_HandleTypeDef)=0x%x\n",sizeof(USBD_HID_Mouse_HandleTypeDef));
if (hhid == NULL)
{
pdev->pClassDataCmsit[pdev->classId] = NULL;
return (uint8_t)USBD_EMEM;
}
pdev->pClassDataCmsit[pdev->classId] = (void *)hhid;
pdev->pClassData = pdev->pClassDataCmsit[pdev->classId];
#ifdef USE_USBD_COMPOSITE
/* Get the Endpoints addresses allocated for this class instance */
HIDInEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_IN, USBD_EP_TYPE_INTR);
#endif /* USE_USBD_COMPOSITE */
if (pdev->dev_speed == USBD_SPEED_HIGH)
{
pdev->ep_in[HIDInEpAdd & 0xFU].bInterval = HID_MOUSE_HS_BINTERVAL;
}
else /* LOW and FULL-speed endpoints */
{
pdev->ep_in[HIDInEpAdd & 0xFU].bInterval = HID_MOUSE_FS_BINTERVAL;
}
/* Open EP IN */
(void)USBD_LL_OpenEP(pdev, HIDInEpAdd, USBD_EP_TYPE_INTR, HID_MOUSE_EPIN_SIZE);
pdev->ep_in[HIDInEpAdd & 0xFU].is_used = 1U;
hhid->state = MOUSE_HID_IDLE;
return (uint8_t)USBD_OK;
}
/**
* @brief USBD_HID_DeInit
* 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)
{
UNUSED(cfgidx);
#ifdef USE_USBD_COMPOSITE
/* Get the Endpoints addresses allocated for this class instance */
HIDInEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_IN, USBD_EP_TYPE_INTR);
#endif /* USE_USBD_COMPOSITE */
/* Close HID EPs */
(void)USBD_LL_CloseEP(pdev, HIDInEpAdd);
pdev->ep_in[HIDInEpAdd & 0xFU].is_used = 0U;
pdev->ep_in[HIDInEpAdd & 0xFU].bInterval = 0U;
/* Free allocated memory */
if (pdev->pClassDataCmsit[pdev->classId] != NULL)
{
(void)USBD_free(pdev->pClassDataCmsit[pdev->classId]);
pdev->pClassDataCmsit[pdev->classId] = NULL;
}
return (uint8_t)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)
{
USBD_HID_Mouse_HandleTypeDef *hhid = (USBD_HID_Mouse_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
USBD_StatusTypeDef ret = USBD_OK;
uint16_t len;
uint8_t *pbuf;
uint16_t status_info = 0U;
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
switch (req->bmRequest & USB_REQ_TYPE_MASK)
{
case USB_REQ_TYPE_CLASS :
switch (req->bRequest)
{
case HID_MOUSE_REQ_SET_PROTOCOL:
hhid->Protocol = (uint8_t)(req->wValue);
break;
case HID_MOUSE_REQ_GET_PROTOCOL:
(void)USBD_CtlSendData(pdev, (uint8_t *)&hhid->Protocol, 1U);
break;
case HID_MOUSE_REQ_SET_IDLE:
hhid->IdleState = (uint8_t)(req->wValue >> 8);
break;
case HID_MOUSE_REQ_GET_IDLE:
(void)USBD_CtlSendData(pdev, (uint8_t *)&hhid->IdleState, 1U);
break;
case HID_MOUSE_REQ_SET_REPORT:
USBD_HID_Mouse_SetReport_Setup_Stage(pdev, req);
break;
case HID_MOUSE_REQ_GET_REPORT:
USBD_HID_Mouse_GetReport_Token_Stage(pdev, req);
break;
default:
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
break;
}
break;
case USB_REQ_TYPE_STANDARD:
switch (req->bRequest)
{
case USB_REQ_GET_STATUS:
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
(void)USBD_CtlSendData(pdev, (uint8_t *)&status_info, 2U);
}
else
{
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
}
break;
case USB_REQ_GET_DESCRIPTOR:
if ((req->wValue >> 8) == HID_MOUSE_REPORT_DESC)
{
len = MIN(HID_MOUSE_REPORT_DESC_SIZE, req->wLength);
pbuf = HID_MOUSE_ReportDesc;
}
else if ((req->wValue >> 8) == HID_MOUSE_DESCRIPTOR_TYPE)
{
pbuf = USBD_HID_Desc;
len = MIN(USB_HID_MOUSE_DESC_SIZ, req->wLength);
}
else
{
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
break;
}
(void)USBD_CtlSendData(pdev, pbuf, len);
break;
case USB_REQ_GET_INTERFACE :
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
(void)USBD_CtlSendData(pdev, (uint8_t *)&hhid->AltSetting, 1U);
}
else
{
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
}
break;
case USB_REQ_SET_INTERFACE:
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
hhid->AltSetting = (uint8_t)(req->wValue);
}
else
{
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
}
break;
case USB_REQ_CLEAR_FEATURE:
break;
default:
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
break;
}
break;
default:
USBD_CtlError(pdev, req);
ret = USBD_FAIL;
break;
}
return (uint8_t)ret;
}
/**
* @brief USBD_HID_Mouse_SendReport
* Send HID Report
* @param pdev: device instance
* @param buff: pointer to report
* @retval status
*/
uint8_t USBD_HID_Mouse_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len)
{
#ifdef USE_USBD_COMPOSITE
uint8_t class_id;
class_id = USBD_CMPSIT_GetClassID_ByAdd(pdev, HID_MOUSE_EPIN_ADDR, USBD_EP_TYPE_INTR);
USBD_HID_Mouse_HandleTypeDef *hhid = (USBD_HID_Mouse_HandleTypeDef *)pdev->pClassDataCmsit[class_id];
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
if (hhid->state == MOUSE_HID_IDLE)
{
hhid->state = MOUSE_HID_BUSY;
(void)USBD_LL_Transmit(pdev, HID_MOUSE_EPIN_ADDR, report, len);
}
}
return (uint8_t)USBD_OK;
#else
USBD_HID_Mouse_HandleTypeDef *hhid = (USBD_HID_Mouse_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
if (hhid->state == MOUSE_HID_IDLE)
{
hhid->state = MOUSE_HID_BUSY;
(void)USBD_LL_Transmit(pdev, HID_MOUSE_EPIN_ADDR, report, len);
}
}
return (uint8_t)USBD_OK;
#endif
}
extern uint8_t epnm_flag;
uint8_t USBD_HID_Mouse_FirstSendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len)
{
#ifdef USE_USBD_COMPOSITE
uint8_t class_id;
uint8_t ep_id = HID_MOUSE_EPIN_ADDR & 0xF;
class_id = USBD_CMPSIT_GetClassID_ByAdd(pdev, HID_MOUSE_EPIN_ADDR, USBD_EP_TYPE_INTR);
USBD_HID_Mouse_HandleTypeDef *hhid = (USBD_HID_Mouse_HandleTypeDef *)pdev->pClassDataCmsit[class_id];
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
PCD_HandleTypeDef *pcd = (PCD_HandleTypeDef *)(pdev->pData);
uint32_t USBx_BASE = (uint32_t)(PCD_HandleTypeDef *)pcd->Instance;
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
if ((hhid->state == MOUSE_HID_IDLE)||(epnm_flag == 0x01))
{
if(USBx_INEP(ep_id)->DIEPINT&USB_OTG_DIEPINT_ITTXFE)
{
USBx_INEP(ep_id)->DIEPINT |= USB_OTG_DIEPINT_ITTXFE;
hhid->state = MOUSE_HID_BUSY;
(void)USBD_LL_Transmit(pdev, HID_MOUSE_EPIN_ADDR, report, len);
}
else{
if(USBx_INEP(ep_id)->DIEPINT&USB_OTG_DIEPINT_NAK){
USBx_INEP(ep_id)->DIEPINT |= USB_OTG_DIEPINT_NAK;
USBx_INEP(ep_id)->DIEPCTL |= (USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA);
}
}
if(epnm_flag == 0x01){
epnm_flag = 0x00;
}
}
}
return (uint8_t)USBD_OK;
#else
uint8_t ep_id = HID_MOUSE_EPIN_ADDR & 0xF;
USBD_HID_Mouse_HandleTypeDef *hhid = (USBD_HID_Mouse_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
PCD_HandleTypeDef *pcd = (PCD_HandleTypeDef *)(pdev->pData);
uint32_t USBx_BASE = (uint32_t)(PCD_HandleTypeDef *)pcd->Instance;
if (pdev->dev_state == USBD_STATE_CONFIGURED)
{
if ((hhid->state == MOUSE_HID_IDLE)||(epnm_flag == 0x01))
{
if(USBx_INEP(ep_id)->DIEPINT&USB_OTG_DIEPINT_ITTXFE)
{
USBx_INEP(ep_id)->DIEPINT |= USB_OTG_DIEPINT_ITTXFE;
hhid->state = MOUSE_HID_BUSY;
(void)USBD_LL_Transmit(pdev, HID_MOUSE_EPIN_ADDR, report, len);
}
else{
if(USBx_INEP(ep_id)->DIEPINT&USB_OTG_DIEPINT_NAK){
USBx_INEP(ep_id)->DIEPINT |= USB_OTG_DIEPINT_NAK;
USBx_INEP(ep_id)->DIEPCTL |= (USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA);
}
}
if(epnm_flag == 0x01){
epnm_flag = 0x00;
}
}
}
return (uint8_t)USBD_OK;
#endif
}
/**
* @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;
/* 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_MOUSE_HS_BINTERVAL */
polling_interval = (((1U << (HID_MOUSE_HS_BINTERVAL - 1U))) / 8U);
}
else /* LOW and FULL-speed endpoints */
{
/* Sets the data transfer polling interval for low and full
speed transfers */
polling_interval = HID_MOUSE_FS_BINTERVAL;
}
return ((uint32_t)(polling_interval));
}
#ifndef USE_USBD_COMPOSITE
/**
* @brief USBD_HID_GetCfgFSDesc
* return FS configuration descriptor
* @param speed : current device speed
* @param length : pointer data length
* @retval pointer to descriptor buffer
*/
static uint8_t *USBD_HID_GetFSCfgDesc(uint16_t *length)
{
USBD_EpDescTypeDef *pEpDesc = USBD_GetEpDesc(USBD_HID_CfgDesc, HID_MOUSE_EPIN_ADDR);
if (pEpDesc != NULL)
{
pEpDesc->bInterval = HID_MOUSE_FS_BINTERVAL;
}
*length = (uint16_t)sizeof(USBD_HID_CfgDesc);
return USBD_HID_CfgDesc;
}
/**
* @brief USBD_HID_GetCfgHSDesc
* return HS configuration descriptor
* @param speed : current device speed
* @param length : pointer data length
* @retval pointer to descriptor buffer
*/
static uint8_t *USBD_HID_GetHSCfgDesc(uint16_t *length)
{
USBD_EpDescTypeDef *pEpDesc = USBD_GetEpDesc(USBD_HID_CfgDesc, HID_MOUSE_EPIN_ADDR);
if (pEpDesc != NULL)
{
pEpDesc->bInterval = HID_MOUSE_HS_BINTERVAL;
}
*length = (uint16_t)sizeof(USBD_HID_CfgDesc);
return USBD_HID_CfgDesc;
}
/**
* @brief USBD_HID_GetOtherSpeedCfgDesc
* return other speed configuration descriptor
* @param speed : current device speed
* @param length : pointer data length
* @retval pointer to descriptor buffer
*/
static uint8_t *USBD_HID_GetOtherSpeedCfgDesc(uint16_t *length)
{
USBD_EpDescTypeDef *pEpDesc = USBD_GetEpDesc(USBD_HID_CfgDesc, HID_MOUSE_EPIN_ADDR);
if (pEpDesc != NULL)
{
pEpDesc->bInterval = HID_MOUSE_FS_BINTERVAL;
}
*length = (uint16_t)sizeof(USBD_HID_CfgDesc);
return USBD_HID_CfgDesc;
}
#endif /* USE_USBD_COMPOSITE */
/**
* @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)
{
UNUSED(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_Mouse_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId])->state = MOUSE_HID_IDLE;
#ifdef APP_USB_MOUSE_FAST_SEND_DATA
extern void app_usb_mouse_fast_send_data(USBD_HandleTypeDef *pdev);
app_usb_mouse_fast_send_data(pdev);
#endif
return (uint8_t)USBD_OK;
}
static uint8_t USBD_HID_EP0_RxReady(USBD_HandleTypeDef *pdev)
{
DEBUG("Mouse USBD_HID_EP0_RxReady\r\n");
USBD_HID_Mouse_SetReport_Token_Stage(pdev);
return (uint8_t)USBD_OK;
}
uint8_t USBD_HID_Mouse_SetReport_Setup_Stage(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *setup_data)
{
uint8_t report_type;
uint8_t report_id;
#ifdef USE_USBD_COMPOSITE
uint8_t class_id;
class_id = USBD_CMPSIT_GetClassID_ByAdd(pdev, HID_MOUSE_EPIN_ADDR, USBD_EP_TYPE_INTR);
USBD_HID_Mouse_HandleTypeDef *hhid = (USBD_HID_Mouse_HandleTypeDef *)pdev->pClassDataCmsit[class_id];
#else
USBD_HID_Mouse_HandleTypeDef *hhid = (USBD_HID_Mouse_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
#endif
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
report_type = (setup_data->wValue >> 8) & 0xFF;
report_id = setup_data->wValue & 0xFF;
hhid->EP0_ReportLen = setup_data->wLength;
DEBUG("Setup_Stage report_type=%02x, report_id=%02x, report_len=%d\r\n",report_type, report_id, hhid->EP0_ReportLen);
(void)USBD_CtlPrepareRx(pdev, hhid->EP0_ReportBuff, hhid->EP0_ReportLen);
return (uint8_t)USBD_OK;
}
uint8_t USBD_HID_Mouse_SetReport_Token_Stage(USBD_HandleTypeDef *pdev)
{
#ifdef USE_USBD_COMPOSITE
uint8_t class_id;
class_id = USBD_CMPSIT_GetClassID_ByAdd(pdev, HID_MOUSE_EPIN_ADDR, USBD_EP_TYPE_INTR);
USBD_HID_Mouse_HandleTypeDef *hhid = (USBD_HID_Mouse_HandleTypeDef *)pdev->pClassDataCmsit[class_id];
#else
USBD_HID_Mouse_HandleTypeDef *hhid = (USBD_HID_Mouse_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
#endif
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
DEBUG("Token_Stage recv len = %d\n", hhid->EP0_ReportLen);
for(int i=0;i<hhid->EP0_ReportLen;i++){
DEBUG("0x%x ", hhid->EP0_ReportBuff[i]);
}DEBUG("\n");
return (uint8_t)USBD_OK;
}
uint8_t USBD_HID_Mouse_GetReport_Token_Stage(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *setup_data)
{
uint16_t report_length;
uint8_t data[HID_MOUSE_EP0_GET_REPORT_BUF_SIZE];
DEBUG("%s,%d\n",__func__,__LINE__);
#ifdef USE_USBD_COMPOSITE
uint8_t class_id;
class_id = USBD_CMPSIT_GetClassID_ByAdd(pdev, HID_MOUSE_EPIN_ADDR, USBD_EP_TYPE_INTR);
USBD_HID_Mouse_HandleTypeDef *hhid = (USBD_HID_Mouse_HandleTypeDef *)pdev->pClassDataCmsit[class_id];
#else
USBD_HID_Mouse_HandleTypeDef *hhid = (USBD_HID_Mouse_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
#endif
if (hhid == NULL)
{
return (uint8_t)USBD_FAIL;
}
for(int i=0;i<HID_MOUSE_EP0_GET_REPORT_BUF_SIZE;i++)
{
data[i] = i;
}
report_length = setup_data->wLength;
USBD_CtlSendData(pdev, data, report_length);
return (uint8_t)USBD_OK;
}
#ifndef USE_USBD_COMPOSITE
/**
* @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 = (uint16_t)sizeof(USBD_HID_DeviceQualifierDesc);
return USBD_HID_DeviceQualifierDesc;
}
#endif /* USE_USBD_COMPOSITE */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/