From 2ee55c312d35f17216c0e770e029a2044863bdfd Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 24 Feb 2014 01:12:04 +0000 Subject: [PATCH] stm: Add option to pyb_usb_dev_init() to use USB HID interface. With this option selected, only HID on its own works, not VCP+HID. --- stm/main.c | 2 +- stm/stmusbd/usbd_pyb_core.c | 31 +++++++++++++++++++++++++++++-- stm/usb.c | 13 ++++++++++--- stm/usb.h | 5 ++++- 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/stm/main.c b/stm/main.c index 2757e7665d..8bfd6e63e9 100644 --- a/stm/main.c +++ b/stm/main.c @@ -577,7 +577,7 @@ soft_reset: pyb_usb_host_init(); #elif defined(USE_DEVICE_MODE) // USB device - pyb_usb_dev_init(); + pyb_usb_dev_init(PYB_USB_DEV_VCP_MSC); #endif // run main script diff --git a/stm/stmusbd/usbd_pyb_core.c b/stm/stmusbd/usbd_pyb_core.c index d887b89c6c..5fcf9bb182 100644 --- a/stm/stmusbd/usbd_pyb_core.c +++ b/stm/stmusbd/usbd_pyb_core.c @@ -73,6 +73,11 @@ #include "usbd_msc_bot.h" #include "usbd_msc_mem.h" +// CDC VCP is the first interface +// the option below is supposed to select between MSC (1) and HID (0) for the +// second USB interface, but it does not work (only VCP+MSC works) +// to use HID on its own (ie no VCP), the functions in usbd_pyb_core2.c can be +// used, and selected in the call to pyb_usb_dev_init(). #define USB_PYB_USE_MSC (1) #if USB_PYB_USE_MSC @@ -314,7 +319,7 @@ __ALIGN_BEGIN static uint8_t usbd_pyb_CfgDesc[USB_PYB_CONFIG_DESC_SIZ] __ALIGN_E 0x01, // bNumEndpoints 0x03, // bInterfaceClass: HID Class 0x01, // bInterfaceSubClass: 0=no boot, 1=BOOT - 0x01, // nInterfaceProtocol: 0=none, 1=keyboard, 2=mouse + 0x02, // nInterfaceProtocol: 0=none, 1=keyboard, 2=mouse 0x00, // iInterface: // Descriptor of Joystick Mouse HID @@ -340,7 +345,7 @@ __ALIGN_BEGIN static uint8_t usbd_pyb_CfgDesc[USB_PYB_CONFIG_DESC_SIZ] __ALIGN_E #endif }; -#if 0 +#if !USB_PYB_USE_MSC __ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE] __ALIGN_END = { 0x05, 0x01, @@ -559,6 +564,28 @@ static uint8_t usbd_pyb_Setup(void *pdev, USB_SETUP_REQ *req) { // Standard Interface Request ------------------------------------------ case (USB_REQ_TYPE_STANDARD | USB_REQ_RECIPIENT_INTERFACE): switch (req->bRequest) { + case USB_REQ_GET_DESCRIPTOR: // needed for HID; SU 0x81 0x06 0x2200 0x00 request + // wIndex & 0xff is the interface + if ((req->wIndex & 0xff) <= 1) { + // CDC VCP + } else { +#if USB_PYB_USE_MSC +#else + uint16_t len = 0; + uint8_t *pbuf = NULL; + if (req->wValue >> 8 == HID_REPORT_DESC) { + len = MIN(HID_MOUSE_REPORT_DESC_SIZE , req->wLength); + pbuf = HID_MOUSE_ReportDesc; + return USBD_CtlSendData(pdev, pbuf, len); + } else if( req->wValue >> 8 == HID_DESCRIPTOR_TYPE) { + pbuf = usbd_pyb_CfgDesc + /* skip VCP */ 0x09 + 0x08 + 0x09 + 0x05 + 0x05 + 0x04 + 0x05 + 0x07 + 0x09 + 0x07 + 0x07 + /* skip iface descr */ 0x09; + len = MIN(0x09, req->wLength); + return USBD_CtlSendData(pdev, pbuf, len); + } +#endif + } + break; + case USB_REQ_GET_INTERFACE: // wIndex & 0xff is the interface if ((req->wIndex & 0xff) <= 1) { diff --git a/stm/usb.c b/stm/usb.c index 718d432e61..29a5fb46d0 100644 --- a/stm/usb.c +++ b/stm/usb.c @@ -28,12 +28,19 @@ static int rx_buf_out; static int interrupt_char = VCP_CHAR_NONE; mp_obj_t mp_const_vcp_interrupt = MP_OBJ_NULL; -void pyb_usb_dev_init(void) { +void pyb_usb_dev_init(int usb_dev_type) { #ifdef USE_DEVICE_MODE if (!dev_is_enabled) { // only init USB once in the device's power-lifetime - USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_cb, &USR_cb); - //USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_HID_cb, &USR_cb); + switch (usb_dev_type) { + case PYB_USB_DEV_VCP_MSC: + USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_cb, &USR_cb); + break; + + case PYB_USB_DEV_HID: + USBD_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_PYB_HID_cb, &USR_cb); + break; + } } rx_buf_in = 0; rx_buf_out = 0; diff --git a/stm/usb.h b/stm/usb.h index 1f7fa18096..a0fb153240 100644 --- a/stm/usb.h +++ b/stm/usb.h @@ -4,7 +4,10 @@ #define VCP_CHAR_CTRL_C (3) #define VCP_CHAR_CTRL_D (4) -void pyb_usb_dev_init(void); +#define PYB_USB_DEV_VCP_MSC (0) +#define PYB_USB_DEV_HID (1) + +void pyb_usb_dev_init(int usb_dev_type); bool usb_vcp_is_enabled(void); bool usb_vcp_is_connected(void); void usb_vcp_set_interrupt_char(int c);