USB HID是USB的应用之一,它的好处是不用驱动程序就可以同上位机通讯。
我打开了USB HID鼠标的那个例程,看了一下,USB总是同中断相联系的要不然处理不了那么烦索的协议。
而关于USB0先启动然后转到USB1口的过程在应用笔记中已经写得很明确了。我想要说的是如果用到mouse例程在例程基础之上改一下,这样就达到了应用的目地。
mouse例程通过一些回调函数来实现HID的,
static usb_sts_type class_init_handler(void *udev);
static usb_sts_type class_clear_handler(void *udev);
static usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup);
static usb_sts_type class_ept0_tx_handler(void *udev);
static usb_sts_type class_ept0_rx_handler(void *udev);
static usb_sts_type class_in_handler(void *udev, uint8_t ept_num);
static usb_sts_type class_out_handler(void *udev, uint8_t ept_num);
static usb_sts_type class_sof_handler(void *udev);
static usb_sts_type class_event_handler(void *udev, usbd_event_type event);
而处理事件的集中在这里:
void usb_hid_mouse_send(void *udev, uint8_t op)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
mouse_type *pmouse = (mouse_type *)pudev->class_handler->pdata;
int8_t posx = 0, posy = 0, button = 0;
switch(op)
{
case LEFT_BUTTON:
button = 0x01;
break;
case RIGHT_BUTTON:
button = 0x2;
break;
case UP_MOVE:
posy -= MOVE_STEP;
break;
case DOWN_MOVE:
posy += MOVE_STEP;
break;
case LEFT_MOVE:
posx -= MOVE_STEP;
break;
case RIGHT_MOVE:
posx += MOVE_STEP;
break;
default:
break;
}
pmouse->mouse_buffer[0] = button;
pmouse->mouse_buffer[1] = posx;
pmouse->mouse_buffer[2] = posy;
usb_mouse_class_send_report(udev, pmouse->mouse_buffer, 4);
}
程序下载到开发板也只在USER键好用,相当于确认键。
上位机软件可以识别HID-Mouse
接着又跑一下custom_hid,用镊子直接短PB7,PB8,PB9,则上位机软件有指示,这时按下按键按键灯亮。
而键值处理是在:
static void usb_hid_buf_process(void *udev, uint8_t *report, uint16_t len)
{
uint32_t i_index;
usbd_core_type *pudev = (usbd_core_type *)udev;
custom_hid_type *pcshid = (custom_hid_type *)pudev->class_handler->pdata;
switch(report[0])
{
case HID_REPORT_ID_2:
if(pcshid->g_rxhid_buff[1] == 0)
{
at32_led_off(LED2);
}
else
{
at32_led_on(LED2);
}
break;
case HID_REPORT_ID_3:
if(pcshid->g_rxhid_buff[1] == 0)
{
at32_led_off(LED3);
}
else
{
at32_led_on(LED3);
}
break;
case HID_REPORT_ID_4:
if(pcshid->g_rxhid_buff[1] == 0)
{
at32_led_off(LED4);
}
else
{
at32_led_on(LED4);
}
break;
case HID_REPORT_ID_6:
for(i_index = 0; i_index < len; i_index ++)
{
pcshid->g_txhid_buff[i_index] = report[i_index];
}
usbd_ept_send(pudev, USBD_CUSTOM_HID_IN_EPT, pcshid->g_txhid_buff, len);
break;
default:
break;
}
}
运行结果如下:
|