在之前(使用STM32CubeMX生成USB_HOST_HID工程)的基础上进行修改 在结合之前在pc上的测试 USB之HID类Set_Report Request[调试手记1] 测试代码如下: /* USER CODE BEGIN 1 */
void USBH_HID_EventCallback(USBH_HandleTypeDef *phost)
{
HID_TypeTypeDef type = HID_UNKNOWN;
USBH_ErrLog("USBH_HID_EventCallback");
type = USBH_HID_GetDeviceType(phost);
switch(type)
{
case HID_KEYBOARD:
{
HID_KEYBD_Info_TypeDef *keyboard_info;
char ascii;
uint8_t reportBuff[1];
static uint8_t led_flag =0;
keyboard_info = USBH_HID_GetKeybdInfo(phost);
if( keyboard_info != NULL )
{
ascii = USBH_HID_GetASCIICode(keyboard_info);
if( ascii != 0 )
{
USBH_UsrLog("%c",ascii);
}
// USBH_UsrLog("state %x",keyboard_info->state);
// USBH_UsrLog("lctrl %x",keyboard_info->lctrl);
// USBH_UsrLog("lshift %x",keyboard_info->lshift);
// USBH_UsrLog("lalt %x",keyboard_info->lalt);
// USBH_UsrLog("lgui %x",keyboard_info->lgui);
// USBH_UsrLog("rctrl %x",keyboard_info->rctrl);
// USBH_UsrLog("rshift %x",keyboard_info->rshift);
// USBH_UsrLog("rgui %x",keyboard_info->rgui);
USBH_UsrLog("keys[6] %x %x %x %x %x %x ",keyboard_info->keys[0],\
keyboard_info->keys[1],\
keyboard_info->keys[2],\
keyboard_info->keys[3],\
keyboard_info->keys[4],\
keyboard_info->keys[5]);
if(keyboard_info->keys[0] == 0x39)
{
led_flag=~led_flag;
if(led_flag)
{
reportBuff[0] = 0X02;
USBH_HID_SetReport (phost,2,0,reportBuff,1);
USBH_UsrLog("reportBuff %02x ",reportBuff[0]);
}
else
{
reportBuff[0] = 0X00;
USBH_HID_SetReport (phost,2,0,reportBuff,1);
USBH_UsrLog("reportBuff %02x ",reportBuff[0]);
}
}
}
}
break;
case HID_MOUSE:
{
HID_MOUSE_Info_TypeDef *mouse_info;
mouse_info = USBH_HID_GetMouseInfo(phost);
USBH_UsrLog("mouse_info X%d, Y%d ,button %d %d %d",mouse_info->x,mouse_info->y,mouse_info->buttons[2],mouse_info->buttons[1],mouse_info->buttons[0]);
}
break;
default:
break;
}
}
/* USER CODE END 1 */
通过capslock按键按下和释放等可以看到CAPS_LOCK指示灯变化。但是会发现需要按多次才能翻转CAPS_LOCK指示灯。 而我们在rtt中可以看到我们赋值reportBuff正常变化。 我们继续给USBH_HID_SetReport的返回结果加log信息。 /* USER CODE BEGIN 1 */
void USBH_HID_EventCallback(USBH_HandleTypeDef *phost)
{
HID_TypeTypeDef type = HID_UNKNOWN;
USBH_ErrLog("USBH_HID_EventCallback");
type = USBH_HID_GetDeviceType(phost);
switch(type)
{
case HID_KEYBOARD:
{
HID_KEYBD_Info_TypeDef *keyboard_info;
char ascii;
uint8_t reportBuff[1];
USBH_StatusTypeDef status = USBH_BUSY;
static uint8_t led_flag =0;
keyboard_info = USBH_HID_GetKeybdInfo(phost);
if( keyboard_info != NULL )
{
ascii = USBH_HID_GetASCIICode(keyboard_info);
if( ascii != 0 )
{
USBH_UsrLog("%c",ascii);
}
// USBH_UsrLog("state %x",keyboard_info->state);
// USBH_UsrLog("lctrl %x",keyboard_info->lctrl);
// USBH_UsrLog("lshift %x",keyboard_info->lshift);
// USBH_UsrLog("lalt %x",keyboard_info->lalt);
// USBH_UsrLog("lgui %x",keyboard_info->lgui);
// USBH_UsrLog("rctrl %x",keyboard_info->rctrl);
// USBH_UsrLog("rshift %x",keyboard_info->rshift);
// USBH_UsrLog("rgui %x",keyboard_info->rgui);
USBH_UsrLog("keys[6] %x %x %x %x %x %x ",keyboard_info->keys[0],\
keyboard_info->keys[1],\
keyboard_info->keys[2],\
keyboard_info->keys[3],\
keyboard_info->keys[4],\
keyboard_info->keys[5]);
if(keyboard_info->keys[0] == 0x39)
{
led_flag=~led_flag;
if(led_flag)
{
reportBuff[0] = 0X02;
status = USBH_HID_SetReport (phost,2,0,reportBuff,1);
USBH_UsrLog("USBH_HID_SetReport %02x reportBuff %02x ",status,reportBuff[0]);
}
else
{
reportBuff[0] = 0X00;
status = USBH_HID_SetReport (phost,2,0,reportBuff,1);
USBH_UsrLog("USBH_HID_SetReport %02x reportBuff %02x ",status,reportBuff[0]);
}
}
}
}
break;
case HID_MOUSE:
{
HID_MOUSE_Info_TypeDef *mouse_info;
mouse_info = USBH_HID_GetMouseInfo(phost);
USBH_UsrLog("mouse_info X%d, Y%d ,button %d %d %d",mouse_info->x,mouse_info->y,mouse_info->buttons[2],mouse_info->buttons[1],mouse_info->buttons[0]);
}
break;
default:
break;
}
}
/* USER CODE END 1 */
观察log,只有当USBH_HID_SetReport的返回值为 USBH_OK = 0,时候我们才能看到灯的变化。 那我们继续修改代码 /* USER CODE BEGIN 1 */
void USBH_HID_EventCallback(USBH_HandleTypeDef *phost)
{
HID_TypeTypeDef type = HID_UNKNOWN;
USBH_ErrLog("USBH_HID_EventCallback");
type = USBH_HID_GetDeviceType(phost);
switch(type)
{
case HID_KEYBOARD:
{
HID_KEYBD_Info_TypeDef *keyboard_info;
char ascii;
uint8_t reportBuff[1];
USBH_StatusTypeDef status = USBH_BUSY;
static uint8_t led_flag =0;
keyboard_info = USBH_HID_GetKeybdInfo(phost);
if( keyboard_info != NULL )
{
ascii = USBH_HID_GetASCIICode(keyboard_info);
if( ascii != 0 )
{
USBH_UsrLog("%c",ascii);
}
// USBH_UsrLog("state %x",keyboard_info->state);
// USBH_UsrLog("lctrl %x",keyboard_info->lctrl);
// USBH_UsrLog("lshift %x",keyboard_info->lshift);
// USBH_UsrLog("lalt %x",keyboard_info->lalt);
// USBH_UsrLog("lgui %x",keyboard_info->lgui);
// USBH_UsrLog("rctrl %x",keyboard_info->rctrl);
// USBH_UsrLog("rshift %x",keyboard_info->rshift);
// USBH_UsrLog("rgui %x",keyboard_info->rgui);
USBH_UsrLog("keys[6] %x %x %x %x %x %x ",keyboard_info->keys[0],\
keyboard_info->keys[1],\
keyboard_info->keys[2],\
keyboard_info->keys[3],\
keyboard_info->keys[4],\
keyboard_info->keys[5]);
if(keyboard_info->keys[0] == 0x39)
{
led_flag=~led_flag;
if(led_flag)
{
reportBuff[0] = 0X02;
do
{
status = USBH_HID_SetReport (phost,2,0,reportBuff,1);
}
while(status !=USBH_OK);
USBH_UsrLog("USBH_HID_SetReport %02x reportBuff %02x ",status,reportBuff[0]);
}
else
{
reportBuff[0] = 0X00;
do
{
status = USBH_HID_SetReport (phost,2,0,reportBuff,1);
}
while(status !=USBH_OK);
USBH_UsrLog("USBH_HID_SetReport %02x reportBuff %02x ",status,reportBuff[0]);
}
}
}
}
break;
case HID_MOUSE:
{
HID_MOUSE_Info_TypeDef *mouse_info;
mouse_info = USBH_HID_GetMouseInfo(phost);
USBH_UsrLog("mouse_info X%d, Y%d ,button %d %d %d",mouse_info->x,mouse_info->y,mouse_info->buttons[2],mouse_info->buttons[1],mouse_info->buttons[0]);
}
break;
default:
break;
}
}
/* USER CODE END 1 */
|