From 87e61d21e5ce588c9701d92def188fa97674c3cd Mon Sep 17 00:00:00 2001 From: root Date: Sun, 2 Nov 2014 13:45:44 +0000 Subject: ifhs --- .gitignore | 7 +++ src/Makefile | 2 +- src/dfu.c | 2 +- src/hid.c | 126 ------------------------------------------ src/keyboard.c | 139 ++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 20 +++++-- src/mouse.c | 114 ++++++++++++++++++++++++++++++++++++++ src/prototypes.h | 23 ++++++-- src/tablet.c | 121 ++++++++++++++++++++++++++++++++++++++++ src/uart.c | 7 +++ src/usb.c | 164 ++++++++++++++++++++++++++++++++++++------------------- 11 files changed, 530 insertions(+), 195 deletions(-) create mode 100644 .gitignore delete mode 100644 src/hid.c create mode 100644 src/keyboard.c create mode 100644 src/mouse.c create mode 100644 src/tablet.c create mode 100644 src/uart.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..73a69ee --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*.o +*.a +*.d +*.elf +*.map +*.hex +*~ diff --git a/src/Makefile b/src/Makefile index 5927739..1a9f2b2 100644 --- a/src/Makefile +++ b/src/Makefile @@ -23,7 +23,7 @@ PROG=kvm V=1 default: ${PROG}.elf -CSRCS=dfu.c hid.c main.c usb.c +CSRCS=dfu.c mouse.c keyboard.c main.c usb.c tablet.c uart.c BINARY = ${PROG} diff --git a/src/dfu.c b/src/dfu.c index 4e10843..56420ad 100644 --- a/src/dfu.c +++ b/src/dfu.c @@ -14,7 +14,7 @@ const struct usb_dfu_descriptor dfu_function = { const struct usb_interface_descriptor dfu_iface = { .bLength = USB_DT_INTERFACE_SIZE, .bDescriptorType = USB_DT_INTERFACE, - .bInterfaceNumber = 1, + .bInterfaceNumber = 3, .bAlternateSetting = 0, .bNumEndpoints = 0, .bInterfaceClass = 0xFE, diff --git a/src/hid.c b/src/hid.c deleted file mode 100644 index 8feb1cb..0000000 --- a/src/hid.c +++ /dev/null @@ -1,126 +0,0 @@ -#include "project.h" - -static const uint8_t hid_report_descriptor[] = { - 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ - 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 (Button 1) */ - 0x29, 0x03, /* USAGE_MAXIMUM (Button 3) */ - 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 (Cnst,Ary,Abs) */ - 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ - 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 (Vendor Defined Page 1) */ - 0x09, 0x01, /* USAGE (Vendor Usage 1) */ - 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,NPrf) */ - 0x75, 0x06, /* REPORT_SIZE (6) */ - 0x95, 0x01, /* REPORT_COUNT (1) */ - 0xb1, 0x01, /* FEATURE (Cnst,Ary,Abs) */ - 0xc0 /* END_COLLECTION */ -}; - -static const struct { - struct usb_hid_descriptor hid_descriptor; - struct { - uint8_t bReportDescriptorType; - uint16_t wDescriptorLength; - } __attribute__((packed)) hid_report; -} __attribute__((packed)) hid_function = { - .hid_descriptor = { - .bLength = sizeof(hid_function), - .bDescriptorType = USB_DT_HID, - .bcdHID = 0x0100, - .bCountryCode = 0, - .bNumDescriptors = 1, - }, - .hid_report = { - .bReportDescriptorType = USB_DT_REPORT, - .wDescriptorLength = sizeof(hid_report_descriptor), - }, -}; - -const struct usb_endpoint_descriptor hid_endpoint = { - .bLength = USB_DT_ENDPOINT_SIZE, - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 0x81, - .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, - .wMaxPacketSize = 4, - .bInterval = 0x20, -}; - -const struct usb_interface_descriptor hid_iface = { - .bLength = USB_DT_INTERFACE_SIZE, - .bDescriptorType = USB_DT_INTERFACE, - .bInterfaceNumber = 0, - .bAlternateSetting = 0, - .bNumEndpoints = 1, - .bInterfaceClass = USB_CLASS_HID, - .bInterfaceSubClass = 1, /* boot */ - .bInterfaceProtocol = 2, /* mouse */ - .iInterface = 0, - - .endpoint = &hid_endpoint, - - .extra = &hid_function, - .extralen = sizeof(hid_function), -}; - -/* Buffer to be used for control requests. */ -uint8_t usbd_control_buffer[128]; - -int hid_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, - void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)) -{ - (void)complete; - (void)usbd_dev; - - if ((req->bmRequestType != 0x81) || - (req->bRequest != USB_REQ_GET_DESCRIPTOR) || - (req->wValue != 0x2200)) - return 0; - - /* Handle the HID report descriptor. */ - *buf = (uint8_t *)hid_report_descriptor; - *len = sizeof(hid_report_descriptor); - - return 1; -} - -void hid_test(void) -{ - static int x = 0; - static int dir = 1; - uint8_t buf[4] = {0, 0, 0, 0}; - - buf[1] = dir; - x += dir; - if (x > 30) - dir = -dir; - if (x < -30) - dir = -dir; - - usbd_ep_write_packet(usbd_dev, 0x81, buf, 4); -} - diff --git a/src/keyboard.c b/src/keyboard.c new file mode 100644 index 0000000..e4ed7e2 --- /dev/null +++ b/src/keyboard.c @@ -0,0 +1,139 @@ +#include "project.h" + +#if 0 +static const uint8_t keyboard_report_descriptor[] = { + 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ + 0x09, 0x01, /* USAGE (Keyboard */ + 0xa1, 0x01, /* COLLECTION (Application) */ + 0x05, 0x07, /* USAGE_PAGE (Key Codes) */ + 0x19, 0xe0, /* USAGE_MINIMUM (Control Left) */ + 0x29, 0xe7, /* USAGE_MAXIMUM (GUI Right) */ + 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) */ + 0x95, 0x01, /* REPORT_COUNT (1) */ + 0x75, 0x08, /* REPORT_SIZE (8) */ + 0x81, 0x01, /* INPUT (Cnst,Ary,Abs) */ + 0x95, 0x05, /* REPORT_COUNT (5) */ + 0x75, 0x01, /* REPORT_SIZE (1) */ +#if 0 + 0x05, 0x08, /* USAGE_PAGE (LEDs) */ + 0x19, 0x01, /* USAGE_MINIMUM (NumLock) */ + 0x29, 0x05, /* USAGE_MAXIMUM (Kana) */ + 0x ? ?, 0x02, /* OUTPUT (Data,Var,Abs) */ + 0x95, 0x01, /* REPORT_COUNT (1) */ + 0x75, 0x03, /* REPORT_SIZE (3) */ + 0x ? ?, 0x01, /* OUTPUT (Cnst,Ary,Abs) */ + 0x95, 0x06, /* REPORT_COUNT (6) */ + 0x75, 0x03, /* REPORT_SIZE (8) */ + 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ + 0x25, 0xff, 0x00, /*LOGICAL_MAXIMUM (255) */ +#endif + 0x05, 0x07, /* USAGE_PAGE (Key Codes) */ + 0x19, 0x00, /* USAGE_MINIMUM (?) */ + 0x29, 0x98, /* USAGE_MAXIMUM (LANG 9) */ + 0xc0 /* END_COLLECTION */ +}; +#endif + +static const uint8_t keyboard_report_descriptor[] = { + 0x05, 0x01, /* Usage Page (Generic Desktop) */ + 0x09, 0x06, /* Usage (Keyboard) */ + 0xA1, 0x01, /* Collection (Application) */ + 0x05, 0x07, /* Usage page (Key Codes) */ + 0x19, 0xE0, /* Usage minimum (224) */ + 0x29, 0xE7, /* Usage maximum (231) */ + 0x15, 0x00, /* Logical minimum (0) */ + 0x25, 0x01, /* Logical maximum (1) */ + 0x75, 0x01, /* Report size (1) */ + 0x95, 0x08, /* Report count (8) */ + 0x81, 0x02, /* Input (data, variable, absolute) */ + 0x95, 0x01, /* Report count (1) */ + 0x75, 0x08, /* Report size (8) */ + 0x81, 0x01, /* Input (constant) */ + 0x95, 0x06, /* Report count (6) */ + 0x75, 0x08, /* Report size (8) */ + 0x15, 0x00, /* Logical minimum (0) */ + 0x25, 0x65, /* Logical maximum (101) */ + 0x05, 0x07, /* Usage page (key codes) */ + 0x19, 0x00, /* Usage minimum (0) */ + 0x2A, 0xff, 0x00, /* Usage maximum (255) */ +// 0x29, 0x65, /* Usage maximum (101) */ +// 0x2A, 0xff, 0x03, /* Usage maximum (1023) */ + 0x81, 0x00, /* Input (data, array) */ + 0xC0 /* End Collection */ +}; + + +static const struct +{ + struct usb_hid_descriptor hid_descriptor; + struct + { + uint8_t bReportDescriptorType; + uint16_t wDescriptorLength; + } __attribute__ ((packed)) hid_report; +} __attribute__ ((packed)) keyboard_function = +{ + .hid_descriptor = + { + .bLength = sizeof (keyboard_function),.bDescriptorType = + USB_DT_HID,.bcdHID = 0x0100,.bCountryCode = 0,.bNumDescriptors = 1,} + ,.hid_report = + { + .bReportDescriptorType = USB_DT_REPORT,.wDescriptorLength = + sizeof (keyboard_report_descriptor),} +,}; + +const struct usb_endpoint_descriptor keyboard_endpoint = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x81, + .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, + .wMaxPacketSize = 8, + .bInterval = 0x20, +}; + +const struct usb_interface_descriptor keyboard_iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = USB_CLASS_HID, + .bInterfaceSubClass = 1, /* boot */ + .bInterfaceProtocol = 1, /* keyboard */ + .iInterface = 0, + + .endpoint = &keyboard_endpoint, + + .extra = &keyboard_function, + .extralen = sizeof (keyboard_function), +}; + + +void keyboard_get_descriptor(uint8_t **buf,uint16_t *len) +{ + + /* Handle the HID report descriptor. */ + *buf = (uint8_t *) keyboard_report_descriptor; + *len = sizeof (keyboard_report_descriptor); +} + +void +keyboard_test (void) +{ + static int c = 0; + uint8_t buf[8] = { 0, 0, 0, 0 ,0,0,0,0}; + + + buf[0]=(c>> 1) & 7; + + buf[2]= (c&1) ? 12:0; + + c++; + + usbd_ep_write_packet (usbd_dev, 0x81, buf, 8); +} diff --git a/src/main.c b/src/main.c index 655a0bc..6b13a82 100644 --- a/src/main.c +++ b/src/main.c @@ -2,7 +2,9 @@ void sys_tick_handler(void) { - hid_test(); + keyboard_test(); + mouse_test(); + tablet_test(); } int main(void) @@ -11,13 +13,21 @@ int main(void) rcc_clock_setup_in_hsi_out_48mhz(); - rcc_periph_clock_enable(RCC_GPIOC); + rcc_periph_clock_enable(RCC_GPIOA); + rcc_periph_clock_enable(RCC_AFIO); + rcc_periph_clock_enable(RCC_USART1); + + + uart_init(); + usb_init(); systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8); - /* SysTick interrupt every N clock pulses: set reload to N-1 */ - systick_set_reload(99999); + /* 48MHz / 8 = > 6Mhz */ + systick_set_reload(3000000); + /* 6MHz / 3000000 => 2Hz */ + systick_interrupt_enable(); systick_counter_enable(); @@ -25,8 +35,8 @@ int main(void) gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO11); + - usb_init(); for (i = 0; i < 0x80000; i++) __asm__("nop"); diff --git a/src/mouse.c b/src/mouse.c new file mode 100644 index 0000000..acb4166 --- /dev/null +++ b/src/mouse.c @@ -0,0 +1,114 @@ +#include "project.h" + +static const uint8_t mouse_report_descriptor[] = { + 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ + 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 (Button 1) */ + 0x29, 0x03, /* USAGE_MAXIMUM (Button 3) */ + 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 (Cnst,Ary,Abs) */ + 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ + 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 (Vendor Defined Page 1) */ + 0x09, 0x01, /* USAGE (Vendor Usage 1) */ + 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,NPrf) */ + 0x75, 0x06, /* REPORT_SIZE (6) */ + 0x95, 0x01, /* REPORT_COUNT (1) */ + 0xb1, 0x01, /* FEATURE (Cnst,Ary,Abs) */ + 0xc0 /* END_COLLECTION */ +}; + +static const struct +{ + struct usb_hid_descriptor hid_descriptor; + struct + { + uint8_t bReportDescriptorType; + uint16_t wDescriptorLength; + } __attribute__ ((packed)) hid_report; +} __attribute__ ((packed)) mouse_function = +{ + .hid_descriptor = + { + .bLength = sizeof (mouse_function),.bDescriptorType = USB_DT_HID,.bcdHID = + 0x0100,.bCountryCode = 0,.bNumDescriptors = 1,} + ,.hid_report = + { + .bReportDescriptorType = USB_DT_REPORT,.wDescriptorLength = + sizeof (mouse_report_descriptor),} +,}; + +const struct usb_endpoint_descriptor mouse_endpoint = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x82, + .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, + .wMaxPacketSize = 4, + .bInterval = 0x20, +}; + +const struct usb_interface_descriptor mouse_iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 1, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = USB_CLASS_HID, + .bInterfaceSubClass = 1, /* boot */ + .bInterfaceProtocol = 2, /* mouse */ + .iInterface = 0, + + .endpoint = &mouse_endpoint, + + .extra = &mouse_function, + .extralen = sizeof (mouse_function), +}; + + +void mouse_get_descriptor(uint8_t **buf,uint16_t *len) +{ + + /* Handle the HID report descriptor. */ + *buf = (uint8_t *) mouse_report_descriptor; + *len = sizeof (mouse_report_descriptor); +} + +void +mouse_test (void) +{ + static int c = 1; + uint8_t buf[4] = { 0, 0, 0, 0 }; + + buf[0] = c & 7; + + buf[1] = c &8 ? -1:1; + buf[2] = c &16 ? -1:1; + buf[3] = c &32 ? -1:1; + + c++; + + usbd_ep_write_packet (usbd_dev, 0x82, buf, 4); +} diff --git a/src/prototypes.h b/src/prototypes.h index 79c56ec..9b4d2c9 100644 --- a/src/prototypes.h +++ b/src/prototypes.h @@ -2,12 +2,16 @@ extern const struct usb_dfu_descriptor dfu_function; extern const struct usb_interface_descriptor dfu_iface; extern int dfu_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)); -/* hid.c */ -extern const struct usb_endpoint_descriptor hid_endpoint; -extern const struct usb_interface_descriptor hid_iface; -extern uint8_t usbd_control_buffer[128]; -extern int hid_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)); -extern void hid_test(void); +/* mouse.c */ +extern const struct usb_endpoint_descriptor mouse_endpoint; +extern const struct usb_interface_descriptor mouse_iface; +extern void mouse_get_descriptor(uint8_t **buf, uint16_t *len); +extern void mouse_test(void); +/* keyboard.c */ +extern const struct usb_endpoint_descriptor keyboard_endpoint; +extern const struct usb_interface_descriptor keyboard_iface; +extern void keyboard_get_descriptor(uint8_t **buf, uint16_t *len); +extern void keyboard_test(void); /* main.c */ extern void sys_tick_handler(void); extern int main(void); @@ -19,3 +23,10 @@ extern usbd_device *usbd_dev; extern void usb_set_config(usbd_device *usbd_dev, uint16_t wValue); extern void usb_init(void); extern void usb_run(void); +/* tablet.c */ +extern const struct usb_endpoint_descriptor tablet_endpoint; +extern const struct usb_interface_descriptor tablet_iface; +extern void tablet_get_descriptor(uint8_t **buf, uint16_t *len); +extern void tablet_test(void); +/* uart.c */ +extern void uart_init(void); diff --git a/src/tablet.c b/src/tablet.c new file mode 100644 index 0000000..87d2f2a --- /dev/null +++ b/src/tablet.c @@ -0,0 +1,121 @@ +#include "project.h" + +static const uint8_t tablet_report_descriptor[] = { + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x02, // USAGE (Mouse) + 0xa1, 0x01, // COLLECTION (Application) + 0x09, 0x01, // USAGE (Pointer) + 0xa1, 0x00, // COLLECTION (Physical) + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x30, // USAGE (X) + 0x09, 0x31, // USAGE (Y) + 0x15, 0x00, // LOGICAL_MINIMUM (0) + 0x26, 0xff, 0x7f, // LOGICAL_MAXIMUM (32767) + 0x75, 0x10, // REPORT_SIZE (16) + 0x95, 0x02, // REPORT_COUNT (2) + 0x81, 0x02, // INPUT (Data,Var,Abs) + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x38, // USAGE (Wheel) + 0x15, 0x81, // LOGICAL_MINIMUM (-127) + 0x25, 0x7f, // LOGICAL_MAXIMUM (127) + 0x75, 0x08, // REPORT_SIZE (8) + 0x95, 0x01, // REPORT_COUNT (1) + 0x81, 0x06, // INPUT (Data,Var,Rel) + 0x05, 0x09, // USAGE_PAGE (Button) + 0x19, 0x01, // USAGE_MINIMUM (Button 1) + 0x29, 0x03, // USAGE_MAXIMUM (Button 3) + 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 (Cnst,Ary,Abs) + 0xc0, // END_COLLECTION + 0x09, 0x3c, // USAGE (Button 60) + 0x06, 0x00, 0xff, // USAGE_PAGE (Vendor Defined Page 1) + 0x09, 0x01, // USAGE (Vendor Usage 1) + 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,NPrf) + 0x75, 0x06, // REPORT_SIZE (6) + 0x95, 0x01, // REPORT_COUNT (1) + 0xb1, 0x01, // FEATURE (Cnst,Ary,Abs) + 0xc0 // END_COLLECTION +}; + +static const struct +{ + struct usb_hid_descriptor hid_descriptor; + struct + { + uint8_t bReportDescriptorType; + uint16_t wDescriptorLength; + } __attribute__ ((packed)) hid_report; +} __attribute__ ((packed)) tablet_function = +{ + .hid_descriptor = + { + .bLength = sizeof (tablet_function),.bDescriptorType = USB_DT_HID,.bcdHID = + 0x0100,.bCountryCode = 0,.bNumDescriptors = 1,} + ,.hid_report = + { + .bReportDescriptorType = USB_DT_REPORT,.wDescriptorLength = + sizeof (tablet_report_descriptor),} +,}; + +const struct usb_endpoint_descriptor tablet_endpoint = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x83, + .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, + .wMaxPacketSize = 6, + .bInterval = 0x20, +}; + +const struct usb_interface_descriptor tablet_iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 2, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = USB_CLASS_HID, + .bInterfaceSubClass = 1, /* boot */ + .bInterfaceProtocol = 2, /* tablet */ + .iInterface = 0, + + .endpoint = &tablet_endpoint, + + .extra = &tablet_function, + .extralen = sizeof (tablet_function), +}; + + +void tablet_get_descriptor(uint8_t **buf,uint16_t *len) +{ + + /* Handle the HID report descriptor. */ + *buf = (uint8_t *) tablet_report_descriptor; + *len = sizeof (tablet_report_descriptor); +} + +void +tablet_test (void) +{ + static int c = 0; + uint8_t buf[6] = { 0, 0, 0, 0,0 }; + + buf[0]=c & 0xff; + buf[1]=(c &0x7fff) >> 8; + buf[2]=(32767 -(c &0x7fff)) & 0xff; + buf[3]=(32767 -(c & 0x7fff)) >> 8; + buf[4]=(c&8 ) ? -1:1; + buf[5]=c; + + c++; + + usbd_ep_write_packet (usbd_dev, 0x83, buf, 6); +} diff --git a/src/uart.c b/src/uart.c new file mode 100644 index 0000000..70a6334 --- /dev/null +++ b/src/uart.c @@ -0,0 +1,7 @@ +#include "project.h" + + + +void uart_init(void) +{ +} diff --git a/src/usb.c b/src/usb.c index fdefd75..b004c78 100644 --- a/src/usb.c +++ b/src/usb.c @@ -2,93 +2,145 @@ /* Define this to include the DFU APP interface. */ const struct usb_device_descriptor dev = { - .bLength = USB_DT_DEVICE_SIZE, - .bDescriptorType = USB_DT_DEVICE, - .bcdUSB = 0x0200, - .bDeviceClass = 0, - .bDeviceSubClass = 0, - .bDeviceProtocol = 0, - .bMaxPacketSize0 = 64, - .idVendor = 0x0483, - .idProduct = 0x5710, - .bcdDevice = 0x0200, - .iManufacturer = 1, - .iProduct = 2, - .iSerialNumber = 3, - .bNumConfigurations = 1, + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = 0, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = 0x1d6b, + .idProduct = 0x0ee3, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, }; -const struct usb_interface ifaces[] = {{ - .num_altsetting = 1, - .altsetting = &hid_iface, +const struct usb_interface ifaces[] = { + { + .num_altsetting = 1, + .altsetting = &keyboard_iface, + }, + { + .num_altsetting = 1, + .altsetting = &mouse_iface, + }, + { + .num_altsetting = 1, + .altsetting = &tablet_iface, + }, #ifdef INCLUDE_DFU_INTERFACE -}, { - .num_altsetting = 1, - .altsetting = &dfu_iface, + { + .num_altsetting = 1, + .altsetting = &dfu_iface, #endif -}}; + } +}; const struct usb_config_descriptor config = { - .bLength = USB_DT_CONFIGURATION_SIZE, - .bDescriptorType = USB_DT_CONFIGURATION, - .wTotalLength = 0, + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, #ifdef INCLUDE_DFU_INTERFACE - .bNumInterfaces = 2, + .bNumInterfaces = 4, #else - .bNumInterfaces = 1, + .bNumInterfaces = 3, #endif - .bConfigurationValue = 1, - .iConfiguration = 0, - .bmAttributes = 0xC0, - .bMaxPower = 0x32, + .bConfigurationValue = 1, + .iConfiguration = 0, + .bmAttributes = 0xC0, + .bMaxPower = 0x32, - .interface = ifaces, + .interface = ifaces, }; static const char *usb_strings[] = { - "Cabbages are good for you", - "fish", - "soup", + "Cabbages are good for you", + "fish", + "soup", }; usbd_device *usbd_dev; -void usb_set_config(usbd_device *usbd_dev, uint16_t wValue) + +static int +usb_control_request (usbd_device * usbd_dev, struct usb_setup_data *req, + uint8_t ** buf, uint16_t * len, + void (**complete) (usbd_device * usbd_dev, + struct usb_setup_data * req)) { - (void)wValue; - (void)usbd_dev; - usbd_ep_setup(usbd_dev, 0x81, USB_ENDPOINT_ATTR_INTERRUPT, 4, NULL); + (void) complete; + (void) usbd_dev; + + if ((req->bmRequestType != 0x81) || + (req->bRequest != USB_REQ_GET_DESCRIPTOR) || (req->wValue != 0x2200)) + return 0; + + switch (req->wIndex) + { + case 0: + keyboard_get_descriptor(buf,len); + return 1; + case 1: + mouse_get_descriptor(buf,len); + return 1; + case 2: + tablet_get_descriptor(buf,len); + return 1; + } + + *len = 0; + return 0; +} + + +void +usb_set_config (usbd_device * usbd_dev, uint16_t wValue) +{ + (void) wValue; + (void) usbd_dev; + + usbd_ep_setup (usbd_dev, 0x81, USB_ENDPOINT_ATTR_INTERRUPT, 4, NULL); + usbd_ep_setup (usbd_dev, 0x82, USB_ENDPOINT_ATTR_INTERRUPT, 4, NULL); + usbd_ep_setup (usbd_dev, 0x83, USB_ENDPOINT_ATTR_INTERRUPT, 4, NULL); + + usbd_register_control_callback (usbd_dev, + USB_REQ_TYPE_STANDARD | + USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + usb_control_request); - usbd_register_control_callback( - usbd_dev, - USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_INTERFACE, - USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, - hid_control_request); #ifdef INCLUDE_DFU_INTERFACE - usbd_register_control_callback( - usbd_dev, - USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, - USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, - dfu_control_request); + usbd_register_control_callback (usbd_dev, + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + dfu_control_request); #endif } -void usb_init(void) +/* Buffer to be used for control requests. */ +static uint8_t usbd_control_buffer[128]; + +void +usb_init (void) { - usbd_dev = usbd_init(&stm32f103_usb_driver, &dev, &config, usb_strings, 3, usbd_control_buffer, sizeof(usbd_control_buffer)); + usbd_dev = + usbd_init (&stm32f103_usb_driver, &dev, &config, usb_strings, 3, + usbd_control_buffer, sizeof (usbd_control_buffer)); - usbd_register_set_config_callback(usbd_dev, usb_set_config); + usbd_register_set_config_callback (usbd_dev, usb_set_config); } -void usb_run(void) +void +usb_run (void) { - while (1) - usbd_poll(usbd_dev); + while (1) + usbd_poll (usbd_dev); } - - -- cgit v1.2.3