记录:usb_hid的调试过程 (调试完毕)

软件stm32cubemx按以前的帖子配置生成后,不改任何东西........
uint8_t reportfs={0x55,0x11,0x33};

int main(void)
{

/[i] USER CODE BEGIN 1 [/i]/

/[i] USER CODE END 1 [/i]/

/[i] MCU Configuration----------------------------------------------------------[/i]/

/[i] Reset of all peripherals, Initializes the Flash interface and the Systick. [/i]/
HAL_Init();

/[i] Configure the system clock [/i]/
SystemClock_Config();

/[i] Initialize all configured peripherals [/i]/
MX_GPIO_Init();
MX_USB_DEVICE_Init();

/[i] USER CODE BEGIN 2 [/i]/
uint8_t reportfs={0x55,0x11,0x33};
/[i] USER CODE END 2 [/i]/

/[i] USER CODE BEGIN 3 [/i]/
/[i] Infinite loop [/i]/
while (1)
{
if(addtimefs >=1000)
{
HAL_GPIO_TogglePin (GPIOD,GPIO_PIN_12 );
USBD_HID_SendReport(&hUsbDeviceFS,reportfs ,3);
reportfs ++;
reportfs ++;
reportfs ++;
addtimefs = 0;

}

}
/[i] USER CODE END 3 [/i]/

}

已邀请:

admin

赞同来自:

本文中移植演示的程序文件已上传到本站网盘,请使用下载
注意:以上程序如果用官方提供的UsbHidDemonstrator软件测试,
则必须设置
#define USBD_VID 1155
#define USBD_PID_FS 22352
否则不识别.
附上该软件测试图

无标题.png

admin

赞同来自:

用 BUS hound抓包可以看到,数据正常显示

无标题.png

admin

赞同来自:

黄框里面的第三行对应这个函数了里面:
该函数在usbd_desc.c文件

无标题2.png

admin

赞同来自:

第三个DI值对应于usbd_hid.c文件里面的函数,如下图函数部分

无标题3.png

admin

赞同来自:

第四个DI为以下内容:


/[i] USB HID device Configuration Descriptor [/i]/
__ALIGN_BEGIN static uint8_t USBD_HID_CfgDesc __ALIGN_END =
{
0x09, /[i] bLength: Configuration Descriptor size [/i]/
USB_DESC_TYPE_CONFIGURATION, /[i] bDescriptorType: Configuration [/i]/
USB_HID_CONFIG_DESC_SIZ,
/[i] wTotalLength: Bytes returned [/i]/
0x00,
0x01, /[i]bNumInterfaces: 1 interface[/i]/
0x01, /[i]bConfigurationValue: Configuration value[/i]/
0x00, /*iConfiguration: Index of string descriptor describing
the configuration*/
0xE0, /[i]bmAttributes: bus powered and Support Remote Wake-up [/i]/
0x32, /[i]MaxPower 100 mA: this current is used for detecting Vbus[/i]/

/******[i][b][/i][i][b][/i] Descriptor of Joystick Mouse interface [/b][/b]************/
/[i] 09 [/i]/
0x09, /[i]bLength: Interface Descriptor size[/i]/
USB_DESC_TYPE_INTERFACE,/[i]bDescriptorType: Interface descriptor type[/i]/
0x00, /[i]bInterfaceNumber: Number of Interface[/i]/
0x00, /[i]bAlternateSetting: Alternate setting[/i]/
0x01, /[i]bNumEndpoints[/i]/
0x03, /[i]bInterfaceClass: HID[/i]/
0x01, /[i]bInterfaceSubClass : 1=BOOT, 0=no boot[/i]/
0x02, /[i]nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse[/i]/
0, /[i]iInterface: Index of string descriptor[/i]/
/************[i][b][/i][i][b][/i] Descriptor of Joystick Mouse HID [/b][/b]****************/
/[i] 18 [/i]/
0x09, /[i]bLength: HID Descriptor size[/i]/
HID_DESCRIPTOR_TYPE, /[i]bDescriptorType: HID[/i]/
0x11, /[i]bcdHID: HID Class Spec release number[/i]/
0x01,
0x00, /[i]bCountryCode: Hardware target country[/i]/
0x01, /[i]bNumDescriptors: Number of HID class descriptors to follow[/i]/
0x22, /[i]bDescriptorType[/i]/
HID_MOUSE_REPORT_DESC_SIZE,/[i]wItemLength: Total length of Report descriptor[/i]/
0x00,
/************[i][b][/i][i][b][/i] Descriptor of Mouse endpoint [/b][/b]****************/
/[i] 27 [/i]/
0x07, /[i]bLength: Endpoint Descriptor size[/i]/
USB_DESC_TYPE_ENDPOINT, /[i]bDescriptorType:[/i]/

HID_EPIN_ADDR, /[i]bEndpointAddress: Endpoint Address (IN)[/i]/
0x03, /[i]bmAttributes: Interrupt endpoint[/i]/
HID_EPIN_SIZE, /[i]wMaxPacketSize: 4 Byte max [/i]/
0x00,
HID_POLLING_INTERVAL, /[i]bInterval: Polling Interval (10 ms)[/i]/
/[i] 34 [/i]/
} ;

admin

赞同来自:

黄框中最后一个DI内容是如下函数中:



__ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc __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
};

admin

赞同来自: daobiao

看透了以上图片内容
再去看了看官方提供的Joystick Mouse HID的应用,
再次修改main函数内容如下;


int main(void)
{

/[i] USER CODE BEGIN 1 [/i]/

/[i] USER CODE END 1 [/i]/

/[i] MCU Configuration----------------------------------------------------------[/i]/

/[i] Reset of all peripherals, Initializes the Flash interface and the Systick. [/i]/
HAL_Init();

/[i] Configure the system clock [/i]/
SystemClock_Config();

/[i] Initialize all configured peripherals [/i]/
MX_GPIO_Init();
MX_USB_DEVICE_Init();

/[i] USER CODE BEGIN 2 [/i]/
uint8_t reportfs={0x00,0xf9,0x00,0x00};//左移
/[i] USER CODE END 2 [/i]/

/[i] USER CODE BEGIN 3 [/i]/
/[i] Infinite loop [/i]/
while (1)
{
if(addtimefs >=1000)
{
HAL_GPIO_TogglePin (GPIOD,GPIO_PIN_12 );
USBD_HID_SendReport(&hUsbDeviceFS,reportfs ,4);
// reportfs ++;
// reportfs ++;
// reportfs ++;
addtimefs = 0;

}

}

其中上面的
uint8_t reportfs={0x00,0xf9,0x00,0x00};
这两个数组这样写是因为:
经过抓包官方的Joystick Mouse HID的应用,发现鼠标左右上下移动的数据包类型是:
0x00,0xf9,0x00,0x00这个可以代表左移
0x00,0x00,0xf9,0x00这个可以代表上移
其他的可以用抓包工具自己查看

本次的效果就是1s左移一次鼠标指针.

admin

赞同来自:

以下,按照官方提供的stm32f4xG_EVAl板的程序CustomHID_Standalone,移植到stm32f407_discovery板上面使用.
增加四个文件,如下:
usb_user.c内容:


/[i] Includes ------------------------------------------------------------------[/i]/
#include "user_usb.h"
#include "usbd_core.h"
/[i] Private typedef -----------------------------------------------------------[/i]/
/[i] Private define ------------------------------------------------------------[/i]/
/[i] Private macro -------------------------------------------------------------[/i]/
/[i] Private function prototypes -----------------------------------------------[/i]/

static uint8_t USBD_CUSTOM_HID_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx);
static uint8_t USBD_CUSTOM_HID_DeInit (USBD_HandleTypeDef *pdev, uint8_t cfgidx);
static uint8_t USBD_CUSTOM_HID_Setup (USBD_HandleTypeDef [i]pdev, USBD_SetupReqTypedef [/i]req);
static uint8_t USBD_CUSTOM_HID_EP0_RxReady (USBD_HandleTypeDef *pdev);
static uint8_t USBD_CUSTOM_HID_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum);
static uint8_t USBD_CUSTOM_HID_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum);
static uint8_t [i]USBD_CUSTOM_HID_GetCfgDesc (uint16_t [/i]length);
static uint8_t [i]USBD_CUSTOM_HID_GetDeviceQualifierDesc (uint16_t [/i]length);

/[i] Private variables ---------------------------------------------------------[/i]/


/** @defgroup USBD_CUSTOM_HID_Private_Variables
* @{
*/
USBD_ClassTypeDef USBD_CUSTOM_HID =
{
USBD_CUSTOM_HID_Init,
USBD_CUSTOM_HID_DeInit,
USBD_CUSTOM_HID_Setup,
NULL, /[i]EP0_TxSent[/i]/
USBD_CUSTOM_HID_EP0_RxReady, /[i]EP0_RxReady[/i]/ /[i] STATUS STAGE IN [/i]/
USBD_CUSTOM_HID_DataIn, /[i]DataIn[/i]/
USBD_CUSTOM_HID_DataOut,
NULL, /[i]SOF [/i]/
NULL,
NULL,
USBD_CUSTOM_HID_GetCfgDesc,
USBD_CUSTOM_HID_GetCfgDesc,
USBD_CUSTOM_HID_GetCfgDesc,
USBD_CUSTOM_HID_GetDeviceQualifierDesc,
};

/[i] USB CUSTOM_HID device Configuration Descriptor [/i]/
__ALIGN_BEGIN static uint8_t USBD_CUSTOM_HID_CfgDesc __ALIGN_END =
{
0x09, /[i] bLength: Configuration Descriptor size [/i]/
USB_DESC_TYPE_CONFIGURATION, /[i] bDescriptorType: Configuration [/i]/
USB_CUSTOM_HID_CONFIG_DESC_SIZ,
/[i] wTotalLength: Bytes returned [/i]/
0x00,
0x01, /[i]bNumInterfaces: 1 interface[/i]/
0x01, /[i]bConfigurationValue: Configuration value[/i]/
0x00, /*iConfiguration: Index of string descriptor describing
the configuration*/
0xC0, /[i]bmAttributes: bus powered [/i]/
0x32, /[i]MaxPower 100 mA: this current is used for detecting Vbus[/i]/

/******[i][b][/i][i][b][/i] Descriptor of CUSTOM HID interface [/b][/b]************/
/[i] 09 [/i]/
0x09, /[i]bLength: Interface Descriptor size[/i]/
USB_DESC_TYPE_INTERFACE,/[i]bDescriptorType: Interface descriptor type[/i]/
0x00, /[i]bInterfaceNumber: Number of Interface[/i]/
0x00, /[i]bAlternateSetting: Alternate setting[/i]/
0x02, /[i]bNumEndpoints[/i]/
0x03, /[i]bInterfaceClass: CUSTOM_HID[/i]/
0x00, /[i]bInterfaceSubClass : 1=BOOT, 0=no boot[/i]/
0x00, /[i]nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse[/i]/
0, /[i]iInterface: Index of string descriptor[/i]/
/************[i][b][/i][i][b][/i] Descriptor of CUSTOM_HID [/b][/b]*********************/
/[i] 18 [/i]/
0x09, /[i]bLength: CUSTOM_HID Descriptor size[/i]/
CUSTOM_HID_DESCRIPTOR_TYPE, /[i]bDescriptorType: CUSTOM_HID[/i]/
0x11, /[i]bCUSTOM_HIDUSTOM_HID: CUSTOM_HID Class Spec release number[/i]/
0x01,
0x00, /[i]bCountryCode: Hardware target country[/i]/
0x01, /[i]bNumDescriptors: Number of CUSTOM_HID class descriptors to follow[/i]/
0x22, /[i]bDescriptorType[/i]/
USBD_CUSTOM_HID_REPORT_DESC_SIZE,/[i]wItemLength: Total length of Report descriptor[/i]/
0x00,
/************[i][b][/i][i][b][/i] Descriptor of Custom HID endpoints [/b][/b]****************/
/[i] 27 [/i]/
0x07, /[i]bLength: Endpoint Descriptor size[/i]/
USB_DESC_TYPE_ENDPOINT, /[i]bDescriptorType:[/i]/

CUSTOM_HID_EPIN_ADDR, /[i]bEndpointAddress: Endpoint Address (IN)[/i]/
0x03, /[i]bmAttributes: Interrupt endpoint[/i]/
CUSTOM_HID_EPIN_SIZE, /[i]wMaxPacketSize: 2 Byte max [/i]/
0x00,
0x20, /[i]bInterval: Polling Interval (20 ms)[/i]/
/[i] 34 [/i]/

0x07, /[i] bLength: Endpoint Descriptor size [/i]/
USB_DESC_TYPE_ENDPOINT, /[i] bDescriptorType: [/i]/
CUSTOM_HID_EPOUT_ADDR, /[i]bEndpointAddress: Endpoint Address (OUT)[/i]/
0x03, /[i] bmAttributes: Interrupt endpoint [/i]/
CUSTOM_HID_EPOUT_SIZE, /[i] wMaxPacketSize: 2 Bytes max [/i]/
0x00,
0x20, /[i] bInterval: Polling Interval (20 ms) [/i]/
/[i] 41 [/i]/
} ;

/[i] USB CUSTOM_HID device Configuration Descriptor [/i]/
__ALIGN_BEGIN static uint8_t USBD_CUSTOM_HID_Desc __ALIGN_END =
{
/[i] 18 [/i]/
0x09, /[i]bLength: CUSTOM_HID Descriptor size[/i]/
CUSTOM_HID_DESCRIPTOR_TYPE, /[i]bDescriptorType: CUSTOM_HID[/i]/
0x11, /[i]bCUSTOM_HIDUSTOM_HID: CUSTOM_HID Class Spec release number[/i]/
0x01,
0x00, /[i]bCountryCode: Hardware target country[/i]/
0x01, /[i]bNumDescriptors: Number of CUSTOM_HID class descriptors to follow[/i]/
0x22, /[i]bDescriptorType[/i]/
USBD_CUSTOM_HID_REPORT_DESC_SIZE,/[i]wItemLength: Total length of Report descriptor[/i]/
0x00,
};

/[i] USB Standard Device Descriptor [/i]/
__ALIGN_BEGIN static uint8_t USBD_CUSTOM_HID_DeviceQualifierDesc __ALIGN_END =
{
USB_LEN_DEV_QUALIFIER_DESC,
USB_DESC_TYPE_DEVICE_QUALIFIER,
0x00,
0x02,
0x00,
0x00,
0x00,
0x40,
0x01,
0x00,
};

/**
* @brief USBD_CUSTOM_HID_Init
* Initialize the CUSTOM_HID interface
* @param pdev: device instance
* @param cfgidx: Configuration index
* @retval status
*/
static uint8_t USBD_CUSTOM_HID_Init (USBD_HandleTypeDef *pdev,
uint8_t cfgidx)
{
uint8_t ret = 0;
USBD_CUSTOM_HID_HandleTypeDef *hhid;
/[i] Open EP IN [/i]/
USBD_LL_OpenEP(pdev,
CUSTOM_HID_EPIN_ADDR,
USBD_EP_TYPE_INTR,
CUSTOM_HID_EPIN_SIZE);

/[i] Open EP OUT [/i]/
USBD_LL_OpenEP(pdev,
CUSTOM_HID_EPOUT_ADDR,
USBD_EP_TYPE_INTR,
CUSTOM_HID_EPOUT_SIZE);

pdev->pClassData = USBD_malloc(sizeof (USBD_CUSTOM_HID_HandleTypeDef));

if(pdev->pClassData == NULL)
{
ret = 1;
}
else
{
hhid = pdev->pClassData;

hhid->state = CUSTOM_HID_IDLE;
((USBD_CUSTOM_HID_ItfTypeDef *)pdev->pUserData)->Init();
/[i] Prepare Out endpoint to receive 1st packet [/i]/
USBD_LL_PrepareReceive(pdev, CUSTOM_HID_EPOUT_ADDR, hhid->Report_buf,
USBD_CUSTOMHID_OUTREPORT_BUF_SIZE);
}

return ret;
}

/**
* @brief USBD_CUSTOM_HID_DeInit
* DeInitialize the CUSTOM_HID layer
* @param pdev: device instance
* @param cfgidx: Configuration index
* @retval status
*/
static uint8_t USBD_CUSTOM_HID_DeInit (USBD_HandleTypeDef *pdev,
uint8_t cfgidx)
{
/[i] Close CUSTOM_HID EP IN [/i]/
USBD_LL_CloseEP(pdev,CUSTOM_HID_EPIN_ADDR);

/[i] Close CUSTOM_HID EP OUT [/i]/
USBD_LL_CloseEP(pdev,CUSTOM_HID_EPOUT_ADDR);

/[i] FRee allocated memory [/i]/
if(pdev->pClassData != NULL)
{
((USBD_CUSTOM_HID_ItfTypeDef *)pdev->pUserData)->DeInit();
USBD_free(pdev->pClassData);
pdev->pClassData = NULL;
}
return USBD_OK;
}
/**
* @brief USBD_CUSTOM_HID_Setup
* Handle the CUSTOM_HID specific requests
* @param pdev: instance
* @param req: usb requests
* @retval status
*/
static uint8_t USBD_CUSTOM_HID_Setup (USBD_HandleTypeDef *pdev,
USBD_SetupReqTypedef *req)
{
uint16_t len = 0;
uint8_t *pbuf = NULL;
USBD_CUSTOM_HID_HandleTypeDef *hhid = pdev->pClassData;

switch (req->bmRequest & USB_REQ_TYPE_MASK)
{
case USB_REQ_TYPE_CLASS :
switch (req->bRequest)
{
case CUSTOM_HID_REQ_SET_PROTOCOL:
hhid->Protocol = (uint8_t)(req->wValue);
break;

case CUSTOM_HID_REQ_GET_PROTOCOL:
USBD_CtlSendData (pdev,
(uint8_t *)&hhid->Protocol,
1);
break;

case CUSTOM_HID_REQ_SET_IDLE:
hhid->IdleState = (uint8_t)(req->wValue >> 8);
break;

case CUSTOM_HID_REQ_GET_IDLE:
USBD_CtlSendData (pdev,
(uint8_t *)&hhid->IdleState,
1);
break;

case CUSTOM_HID_REQ_SET_REPORT:
hhid->IsReportAvailable = 1;
USBD_CtlPrepareRx (pdev, hhid->Report_buf, (uint8_t)(req->wLength));

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 == CUSTOM_HID_REPORT_DESC)
{
len = MIN(USBD_CUSTOM_HID_REPORT_DESC_SIZE , req->wLength);
pbuf = ((USBD_CUSTOM_HID_ItfTypeDef *)pdev->pUserData)->pReport;
}
else if( req->wValue >> 8 == CUSTOM_HID_DESCRIPTOR_TYPE)
{
pbuf = USBD_CUSTOM_HID_Desc;
len = MIN(USB_CUSTOM_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_CUSTOM_HID_EP0_RxReady
* Handles control request data.
* @param pdev: device instance
* @retval status
*/
uint8_t USBD_CUSTOM_HID_EP0_RxReady(USBD_HandleTypeDef *pdev)
{
USBD_CUSTOM_HID_HandleTypeDef *hhid = pdev->pClassData;

if (hhid->IsReportAvailable == 1)
{
((USBD_CUSTOM_HID_ItfTypeDef *)pdev->pUserData)->OutEvent(hhid->Report_buf,
hhid->Report_buf);
hhid->IsReportAvailable = 0;
}

return USBD_OK;
}
/**
* @brief USBD_CUSTOM_HID_DataIn
* handle data IN Stage
* @param pdev: device instance
* @param epnum: endpoint index
* @retval status
*/
static uint8_t USBD_CUSTOM_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_CUSTOM_HID_HandleTypeDef *)pdev->pClassData)->state = CUSTOM_HID_IDLE;

return USBD_OK;
}
/**
* @brief USBD_CUSTOM_HID_DataOut
* handle data OUT Stage
* @param pdev: device instance
* @param epnum: endpoint index
* @retval status
*/
static uint8_t USBD_CUSTOM_HID_DataOut (USBD_HandleTypeDef *pdev,
uint8_t epnum)
{

USBD_CUSTOM_HID_HandleTypeDef *hhid = pdev->pClassData;

((USBD_CUSTOM_HID_ItfTypeDef *)pdev->pUserData)->OutEvent(hhid->Report_buf,
hhid->Report_buf);

USBD_LL_PrepareReceive(pdev, CUSTOM_HID_EPOUT_ADDR , hhid->Report_buf,
USBD_CUSTOMHID_OUTREPORT_BUF_SIZE);

return USBD_OK;
}
/**
* @brief USBD_CUSTOM_HID_GetCfgDesc
* return configuration descriptor
* @param speed : current device speed
* @param length : pointer data length
* @retval pointer to descriptor buffer
*/
static uint8_t [i]USBD_CUSTOM_HID_GetCfgDesc (uint16_t [/i]length)
{
*length = sizeof (USBD_CUSTOM_HID_CfgDesc);
return USBD_CUSTOM_HID_CfgDesc;
}
/**
* @brief DeviceQualifierDescriptor
* return Device Qualifier descriptor
* @param length : pointer data length
* @retval pointer to descriptor buffer
*/
static uint8_t [i]USBD_CUSTOM_HID_GetDeviceQualifierDesc (uint16_t [/i]length)
{
*length = sizeof (USBD_CUSTOM_HID_DeviceQualifierDesc);
return USBD_CUSTOM_HID_DeviceQualifierDesc;
}
/**
* @brief USBD_CUSTOM_HID_RegisterInterface
* @param pdev: device instance
* @param fops: CUSTOMHID Interface callback
* @retval status
*/
uint8_t USBD_CUSTOM_HID_RegisterInterface (USBD_HandleTypeDef *pdev,
USBD_CUSTOM_HID_ItfTypeDef *fops)
{
uint8_t ret = USBD_FAIL;

if(fops != NULL)
{
pdev->pUserData= fops;
ret = USBD_OK;
}

return ret;
}

admin

赞同来自:

文件: usb_user_OPT.c内容如下;


/[i] Includes ------------------------------------------------------------------[/i]/
#include "user_usb_OPT.h"
#include "usbd_hid.h"
/[i] Private typedef -----------------------------------------------------------[/i]/
/[i] Private define ------------------------------------------------------------[/i]/
/[i] Private macro -------------------------------------------------------------[/i]/
/[i] Private function prototypes -----------------------------------------------[/i]/
static int8_t CustomHID_Init (void);
static int8_t CustomHID_DeInit (void);
static int8_t CustomHID_OutEvent (uint8_t event_idx, uint8_t state);
/[i] Private variables ---------------------------------------------------------[/i]/
uint8_t SendBuffer;

__ALIGN_BEGIN static uint8_t CustomHID_ReportDesc __ALIGN_END =
{
0x06, 0xFF, 0x00, /[i] USAGE_PAGE (Vendor Page: 0xFF00) [/i]/
0x09, 0x01, /[i] USAGE (Demo Kit) [/i]/
0xa1, 0x01, /[i] COLLECTION (Application) [/i]/
/[i] 6 [/i]/

/[i] Led 1 [/i]/
0x85, LED1_REPORT_ID, /[i] REPORT_ID (1) [/i]/
0x09, 0x01, /[i] USAGE (LED 1) [/i]/
0x15, 0x00, /[i] LOGICAL_MINIMUM (0) [/i]/
0x25, 0x01, /[i] LOGICAL_MAXIMUM (1) [/i]/
0x75, 0x08, /[i] REPORT_SIZE (8) [/i]/
0x95, LED1_REPORT_COUNT, /[i] REPORT_COUNT (1) [/i]/
0xB1, 0x82, /[i] FEATURE (Data,Var,Abs,Vol) [/i]/

0x85, LED1_REPORT_ID, /[i] REPORT_ID (1) [/i]/
0x09, 0x01, /[i] USAGE (LED 1) [/i]/
0x91, 0x82, /[i] OUTPUT (Data,Var,Abs,Vol) [/i]/
/[i] 26 [/i]/

/[i] Led 2 [/i]/
0x85, LED2_REPORT_ID, /[i] REPORT_ID 2 [/i]/
0x09, 0x02, /[i] USAGE (LED 2) [/i]/
0x15, 0x00, /[i] LOGICAL_MINIMUM (0) [/i]/
0x25, 0x01, /[i] LOGICAL_MAXIMUM (1) [/i]/
0x75, 0x08, /[i] REPORT_SIZE (8) [/i]/
0x95, LED2_REPORT_COUNT, /[i] REPORT_COUNT (1) [/i]/
0xB1, 0x82, /[i] FEATURE (Data,Var,Abs,Vol) [/i]/

0x85, LED2_REPORT_ID, /[i] REPORT_ID (2) [/i]/
0x09, 0x02, /[i] USAGE (LED 2) [/i]/
0x91, 0x82, /[i] OUTPUT (Data,Var,Abs,Vol) [/i]/
/[i] 46 [/i]/

/[i] Led 3 [/i]/
0x85, LED3_REPORT_ID, /[i] REPORT_ID (3) [/i]/
0x09, 0x03, /[i] USAGE (LED 3) [/i]/
0x15, 0x00, /[i] LOGICAL_MINIMUM (0) [/i]/
0x25, 0x01, /[i] LOGICAL_MAXIMUM (1) [/i]/
0x75, 0x08, /[i] REPORT_SIZE (8) [/i]/
0x95, LED3_REPORT_COUNT, /[i] REPORT_COUNT (1) [/i]/
0xB1, 0x82, /[i] FEATURE (Data,Var,Abs,Vol) [/i]/

0x85, LED3_REPORT_ID, /[i] REPORT_ID (3) [/i]/
0x09, 0x03, /[i] USAGE (LED 3) [/i]/
0x91, 0x82, /[i] OUTPUT (Data,Var,Abs,Vol) [/i]/
/[i] 66 [/i]/

/[i] Led 4 [/i]/
0x85, LED4_REPORT_ID, /[i] REPORT_ID 4) [/i]/
0x09, 0x04, /[i] USAGE (LED 4) [/i]/
0x15, 0x00, /[i] LOGICAL_MINIMUM (0) [/i]/
0x25, 0x01, /[i] LOGICAL_MAXIMUM (1) [/i]/
0x75, 0x08, /[i] REPORT_SIZE (8) [/i]/
0x95, LED4_REPORT_COUNT, /[i] REPORT_COUNT (1) [/i]/
0xB1, 0x82, /[i] FEATURE (Data,Var,Abs,Vol) [/i]/

0x85, LED4_REPORT_ID, /[i] REPORT_ID (4) [/i]/
0x09, 0x04, /[i] USAGE (LED 4) [/i]/
0x91, 0x82, /[i] OUTPUT (Data,Var,Abs,Vol) [/i]/
/[i] 86 [/i]/

0xc0 /[i] END_COLLECTION [/i]/
};

USBD_CUSTOM_HID_ItfTypeDef USBD_CustomHID_fops =
{
CustomHID_ReportDesc,
CustomHID_Init,
CustomHID_DeInit,
CustomHID_OutEvent,
};

/**
* @brief CustomHID_Init
* Initializes the CUSTOM HID media low layer
* @param None
* @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL
*/
static int8_t CustomHID_Init(void)
{
/*
Add your deinitialization code here
*/
return (0);
}
/**
* @brief CustomHID_DeInit
* DeInitializes the CUSTOM HID media low layer
* @param None
* @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL
*/
static int8_t CustomHID_DeInit(void)
{
/*
Add your deinitialization code here
*/
return (0);
}
/**
* @brief CustomHID_OutEvent
* Manage the CUSTOM HID class Out Event
* @param event_idx: LED Report Number
* @param state: LED states (ON/OFF)
*/
static int8_t CustomHID_OutEvent (uint8_t event_idx, uint8_t state)
{
switch(event_idx)
{
case 1: /[i] LED1 [/i]/
(state == 1) ? HAL_GPIO_WritePin (GPIOD,GPIO_PIN_12 ,GPIO_PIN_SET ) : HAL_GPIO_WritePin (GPIOD,GPIO_PIN_12 ,GPIO_PIN_RESET );
break;

case 2: /[i] LED2 [/i]/
(state == 1) ? HAL_GPIO_WritePin (GPIOD,GPIO_PIN_13 ,GPIO_PIN_SET ) : HAL_GPIO_WritePin (GPIOD,GPIO_PIN_13 ,GPIO_PIN_RESET );
break;
case 3: /[i] LED3 [/i]/
(state == 1) ? HAL_GPIO_WritePin (GPIOD,GPIO_PIN_14 ,GPIO_PIN_SET ) : HAL_GPIO_WritePin (GPIOD,GPIO_PIN_14 ,GPIO_PIN_RESET );
break;
case 4: /[i] LED4 [/i]/
(state == 1) ? HAL_GPIO_WritePin (GPIOD,GPIO_PIN_15 ,GPIO_PIN_SET ) : HAL_GPIO_WritePin (GPIOD,GPIO_PIN_15 ,GPIO_PIN_RESET );
break;

default:
HAL_GPIO_WritePin (GPIOD,GPIO_PIN_12 ,GPIO_PIN_RESET );
HAL_GPIO_WritePin (GPIOD,GPIO_PIN_13 ,GPIO_PIN_RESET );
HAL_GPIO_WritePin (GPIOD,GPIO_PIN_14 ,GPIO_PIN_RESET );
HAL_GPIO_WritePin (GPIOD,GPIO_PIN_15 ,GPIO_PIN_RESET );
break;
}
return (0);
}
/**
* @brief EXTI line detection callbacks
* @param GPIO_Pin: Specifies the pins connected EXTI line
* @retval None
*/
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
/* NOTE: This function Should not be modified, when the callback is needed,
the HAL_GPIO_EXTI_Callback could be implemented in the user file
*/
if(GPIO_Pin == GPIO_PIN_0)
{
SendBuffer = KEY_REPORT_ID;

if(HAL_GPIO_ReadPin (GPIOA,GPIO_PIN_0) == GPIO_PIN_RESET)
{
SendBuffer = 0x01;
}
else
{
SendBuffer = 0x00;
}
USBD_HID_SendReport(&hUsbDeviceFS, SendBuffer, 2);
}

}

admin

赞同来自:

文件usb_user.h文件如下:


/[i] Define to prevent recursive inclusion -------------------------------------[/i]/
#ifndef __USER_USB_H
#define __USER_USB_H

#include "usb_device.h"
/** @defgroup USBD_CUSTOM_HID_Exported_Defines
* @{
*/
#define CUSTOM_HID_EPIN_ADDR 0x81 //USBD_CUSTOM_HID_Init
#define CUSTOM_HID_EPIN_SIZE 0x02

#define CUSTOM_HID_EPOUT_ADDR 0x01 //USBD_CUSTOM_HID_Init
#define CUSTOM_HID_EPOUT_SIZE 0x02

#define CUSTOM_HID_REQ_SET_PROTOCOL 0x0B //USBD_CUSTOM_HID_Setup
#define CUSTOM_HID_REQ_GET_PROTOCOL 0x03

#define CUSTOM_HID_REQ_SET_IDLE 0x0A //USBD_CUSTOM_HID_Setup
#define CUSTOM_HID_REQ_GET_IDLE 0x02

#define CUSTOM_HID_REQ_SET_REPORT 0x09 //USBD_CUSTOM_HID_Setup
#define CUSTOM_HID_REQ_GET_REPORT 0x01

#define CUSTOM_HID_DESCRIPTOR_TYPE 0x21 //USBD_CUSTOM_HID_Setup
#define CUSTOM_HID_REPORT_DESC 0x22

#define USB_CUSTOM_HID_CONFIG_DESC_SIZ 41 //USBD_CUSTOM_HID_Desc
#define USB_CUSTOM_HID_DESC_SIZ 9


#define USBD_CUSTOMHID_OUTREPORT_BUF_SIZE 0x02
#define USBD_CUSTOM_HID_REPORT_DESC_SIZE 88

/** @defgroup USBD_CORE_Exported_TypesDefinitions
* @{
*/
typedef enum
{
CUSTOM_HID_IDLE = 0,
CUSTOM_HID_BUSY,
}
CUSTOM_HID_StateTypeDef;

typedef struct _USBD_CUSTOM_HID_Itf
{
uint8_t *pReport;
int8_t (* Init) (void);
int8_t (* DeInit) (void);
int8_t (* OutEvent) (uint8_t, uint8_t );

}USBD_CUSTOM_HID_ItfTypeDef;

typedef struct
{
uint8_t Report_buf;
uint32_t Protocol;
uint32_t IdleState;
uint32_t AltSetting;
uint32_t IsReportAvailable;
CUSTOM_HID_StateTypeDef state;
}
USBD_CUSTOM_HID_HandleTypeDef;


/** @defgroup USBD_CORE_Exported_Variables
* @{
*/

extern USBD_ClassTypeDef USBD_CUSTOM_HID;
//#define USBD_CUSTOM_HID_CLASS &USBD_CUSTOM_HID

/** @defgroup USB_CORE_Exported_Functions
* @{
*/
uint8_t USBD_CUSTOM_HID_RegisterInterface (USBD_HandleTypeDef *pdev,
USBD_CUSTOM_HID_ItfTypeDef *fops);

#endif /[i] __USER_USB_H [/i]/



文件usb_user_OPT.h内容如下;


/[i] Define to prevent recursive inclusion -------------------------------------[/i]/
#ifndef __USER_USB_OPT_H
#define __USER_USB_OPT_H

///[i] Includes ------------------------------------------------------------------[/i]/
#include "user_usb.h"
/[i] Exported types ------------------------------------------------------------[/i]/
/[i] Exported constants --------------------------------------------------------[/i]/

#define LED1_REPORT_ID 0x01
#define LED1_REPORT_COUNT 0x01

#define LED2_REPORT_ID 0x02
#define LED2_REPORT_COUNT 0x01

#define LED3_REPORT_ID 0x03
#define LED3_REPORT_COUNT 0x01

#define LED4_REPORT_ID 0x04
#define LED4_REPORT_COUNT 0x01

#define KEY_REPORT_ID 0x05


/[i] Exported macro ------------------------------------------------------------[/i]/
/[i] Exported functions ------------------------------------------------------- [/i]/
extern USBD_CUSTOM_HID_ItfTypeDef USBD_CustomHID_fops;


#endif /[i] __USER_USB_OPT_H [/i]/

admin

赞同来自: binggefly

以上添加到工程之后,修改usb_drive.c文件内容:

void MX_USB_DEVICE_Init(void)
{
/[i] Init Device Library,Add Supported Class and Start the library[/i]/
USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS);

USBD_RegisterClass(&hUsbDeviceFS, &USBD_CUSTOM_HID);

/[i] Add Custom HID callbacks [/i]/
USBD_CUSTOM_HID_RegisterInterface(&hUsbDeviceFS, &USBD_CustomHID_fops);

USBD_Start(&hUsbDeviceFS);

}

编译,用UsbHidDemonstrator软件测试.
打开软件,自动查找到相应的设备,点击左边第三个按钮Graphic view,
点选右边的对应led灯,板卡上面相应的led会点亮.
注 : 按键发送指令到PC还未添加.

admin

赞同来自:

开启按键中断PA0后,main函数中的while中添加代码

		if(key_v>1) key_v = 0;
if(key_s != key_v)
{
key_s = key_v;
if(key_s == 1) datat = 0x01;
else datat = 0x00;
USBD_HID_SendReport(&hUsbDeviceFS , datat, 2);
}
当按键的时候会指示按键状态.实验结束

春水长天

赞同来自:

请问楼主,define CUSTOM_HID_EPIN_ADDR 0x81 //USBD_CUSTOM_HID_Init,
把CUSTOM_HID_EPIN_ADDR改成0x82,USBD_HID_SendReport(&hUsbDeviceFS , datat, 2);就发送不出去了,哪里还要改吗?

一枝梅

赞同来自:

HID接收的有没有相关资料啊?网上要不是旧的库,要不就只有发,没有收部分

Seabiscuit

赞同来自:

F1 cubeMX生成的HID 有问题 表征为识别为Unknown Derice 如何解决,使用F4 DIS 板卡的话直接默认默认设置,就能成功枚举成USB 鼠标。请问哪里有问题吗?

Once

赞同来自:

可以发下你的usb硬件图不

一毛

赞同来自:

这个变量是在哪定义的 addtimefs
 

野蛮少男 - 电子

赞同来自:

想请教下HID协议的报告,控制性传输的获得报告请求,主机会发送报告ID给设备,我再回复相应ID的报告?还是我要一次性把所有的报告内容全部上传呢?QQ图片20161013175028.png



shantoucen2

赞同来自:

请问定义成键盘需要修改和注意什么吗

一叶清风

赞同来自:

谢谢 好东西

要回复问题请先登录注册