From bc9e93ad20f936c67d6acddb02d426783c32398e Mon Sep 17 00:00:00 2001 From: root Date: Sat, 13 Jun 2015 12:18:52 +0100 Subject: add usb resume support --- app/Makefile | 2 +- app/dfu.c | 4 +-- app/keyboard.c | 28 ++++++++++++---- app/kvm.c | 26 ++++++++++----- app/led.c | 51 +++++++++++++++++----------- app/main.c | 57 +++++++++++--------------------- app/mouse.c | 28 ++++++++++++---- app/project.h | 2 ++ app/prototypes.h | 74 +++++++++++++++++++++++++---------------- app/ring.h | 2 -- app/tablet.c | 24 +++++++++++--- app/usart.c | 32 ++++++++++-------- app/usb.c | 82 ++++++++++++++++++++++++++++++++++++++++------ board/STM32F103R_BOARD.cfg | 3 ++ id.h | 2 +- 15 files changed, 275 insertions(+), 142 deletions(-) diff --git a/app/Makefile b/app/Makefile index f2dd0fc..cf0da8d 100644 --- a/app/Makefile +++ b/app/Makefile @@ -24,7 +24,7 @@ PROG=kvm V=1 default: ${PROG}.elf -CSRCS=dfu.c mouse.c keyboard.c main.c usb.c tablet.c usart.c kvm.c ring.c led.c +CSRCS=dfu.c mouse.c keyboard.c main.c usb.c tablet.c usart.c kvm.c ring.c led.c consumer.c ticker.c BINARY = ${PROG} diff --git a/app/dfu.c b/app/dfu.c index 554aca9..2e0bbff 100644 --- a/app/dfu.c +++ b/app/dfu.c @@ -16,13 +16,13 @@ const struct usb_dfu_descriptor dfu_function = { const struct usb_interface_descriptor dfu_iface = { .bLength = USB_DT_INTERFACE_SIZE, .bDescriptorType = USB_DT_INTERFACE, - .bInterfaceNumber = 3, + .bInterfaceNumber = 4, .bAlternateSetting = 0, .bNumEndpoints = 0, .bInterfaceClass = 0xFE, .bInterfaceSubClass = 1, .bInterfaceProtocol = 1, - .iInterface = 0, + .iInterface = 9, .extra = &dfu_function, .extralen = sizeof (dfu_function), diff --git a/app/keyboard.c b/app/keyboard.c index 9799629..c9efc0a 100644 --- a/app/keyboard.c +++ b/app/keyboard.c @@ -56,13 +56,29 @@ static const uint8_t keyboard_report_descriptor[] = { 0x95, 0x06, /* Report count (6) */ 0x75, 0x08, /* Report size (8) */ 0x15, 0x00, /* Logical minimum (0) */ - 0x25, 0x65, /* Logical maximum (101) */ + //0x25, 0x65, /* Logical maximum (101) */ + 0x26, 0xff, 0x00, /* Logical maximum (255) */ + //0x26, 0x7f, 0x00, /* Logical maximum (255) */ 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) */ + 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ + 0x09, 0x80, /* USAGE (System Control) */ + 0xA1, 0x01, /* COLLECTION (Application) */ + 0x75, 0x02, /* REPORT_SIZE (2) */ + 0x95, 0x01, /* REPORT_COUNT (1) */ + 0x15, 0x01, /* LOGICAL_MIN (1) */ + 0x25, 0x03, /* LOGICAL_MAX (3) */ + 0x09, 0x82, /* USAGE (System Sleep) */ + 0x09, 0x81, /* USAGE (System Power Down) */ + 0x09, 0x83, /* USAGE (System Wake Up) */ + 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */ + 0x75, 0x06, /* REPORT_SIZE (6) */ + 0x81, 0x03, /* INPUT (Cnst Var Abs) */ + 0xc0, /* END COLLECTION */ 0xC0 /* End Collection */ }; @@ -92,8 +108,8 @@ const struct usb_endpoint_descriptor keyboard_endpoint = { .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = 0x81, .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, - .wMaxPacketSize = 8, - .bInterval = 0x1 //0x20, + .wMaxPacketSize = 9, + .bInterval = 0x1 //0x20, }; const struct usb_interface_descriptor keyboard_iface = { @@ -105,7 +121,7 @@ const struct usb_interface_descriptor keyboard_iface = { .bInterfaceClass = USB_CLASS_HID, .bInterfaceSubClass = 1, /* boot */ .bInterfaceProtocol = 1, /* keyboard */ - .iInterface = 0, + .iInterface = 5, .endpoint = &keyboard_endpoint, @@ -127,7 +143,7 @@ void keyboard_test (void) { static int c = 0; - uint8_t buf[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; + uint8_t buf[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; buf[0] = (c >> 1) & 7; @@ -136,5 +152,5 @@ keyboard_test (void) c++; - usbd_ep_write_packet (usbd_dev, 0x81, buf, 8); + usbd_ep_write_packet (usbd_dev, 0x81, buf, 9); } diff --git a/app/kvm.c b/app/kvm.c index ab27bb7..a4c3024 100644 --- a/app/kvm.c +++ b/app/kvm.c @@ -26,13 +26,21 @@ kvm_dispatch (void) addr, endpoint, len, buf[0], buf[1], buf[2], buf[3]); #endif - if (len) { - if (endpoint==0x81) - led_set(GPIO8); - else - led_set(GPIO9); - usbd_ep_write_packet (usbd_dev, endpoint, buf, len); - } + if (len) + { + if (endpoint == 0x81) + { + usb_wakeup_host (); + led_set (GPIO8); + } + else + { + led_set (GPIO9); + } + + if (!usb_is_suspended) + usbd_ep_write_packet (usbd_dev, endpoint, buf, len); + } } void @@ -89,12 +97,12 @@ kvm_recv (uint8_t d) buf[ptr++] = d; if (ptr >= len) { - if (addr==BUS_ID) + if (addr == BUS_ID) kvm_dispatch (); state = STATE_SYNC; } break; default: - break; + break; } } diff --git a/app/led.c b/app/led.c index fbe3d11..92469f4 100644 --- a/app/led.c +++ b/app/led.c @@ -1,35 +1,48 @@ #include "project.h" -static int led=0; +static int led_8 = 0; +static int led_9 = 0; -void led_init(void ) +void +led_init (void) { -gpio_set_mode (GPIOB, GPIO_MODE_OUTPUT_2_MHZ, + gpio_set_mode (GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); gpio_set_mode (GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO9); -} - -void led_clear(void) -{ - gpio_set(GPIOB,GPIO8); - gpio_set(GPIOB,GPIO9); + gpio_set (GPIOB, GPIO8); + gpio_set (GPIOB, GPIO9); } -void led_set(uint32_t v) + +void +led_set (uint32_t v) { - gpio_clear(GPIOB,v); - led=50; + gpio_clear (GPIOB, v); + if (v & GPIO8) + led_8 = 50; + if (v & GPIO9) + led_9 = 50; } -void led_tick(void) +void +led_tick (void) { -if (led) { - led--; - if (!led) led_clear(); -} + if (led_8) + { + led_8--; + if (!led_8) + gpio_set (GPIOB, GPIO8); + } + if (led_9) + { + led_9--; + if (!led_9) + gpio_set (GPIOB, GPIO9); + } + + if (usb_is_suspended) + gpio_clear (GPIOB, GPIO8 | GPIO9); } - - diff --git a/app/main.c b/app/main.c index 115a1df..a4a5394 100644 --- a/app/main.c +++ b/app/main.c @@ -1,20 +1,6 @@ #include "project.h" extern uint32_t dfu_flag; -void -sys_tick_handler (void) -{ -uint8_t d; - -while (!ring_read_byte(&rx1_ring,&d)) - kvm_recv(d); - -while (!ring_read_byte(&rx2_ring,&d)) - kvm_recv(d); - -led_tick(); -} - int main (void) { @@ -23,10 +9,10 @@ main (void) // rcc_clock_setup_in_hsi_out_48mhz (); //nvic_set_priority_grouping(NVIC_PriorityGroup_4); - /*set up pll*/ - rcc_clock_setup_in_hse_8mhz_out_72mhz(); + /*set up pll */ + rcc_clock_setup_in_hse_8mhz_out_72mhz (); - /*turn on clocks to periferals*/ + /*turn on clocks to periferals */ rcc_periph_clock_enable (RCC_GPIOA); rcc_periph_clock_enable (RCC_GPIOB); rcc_periph_clock_enable (RCC_GPIOC); @@ -35,39 +21,32 @@ main (void) rcc_periph_clock_enable (RCC_USART3); rcc_periph_clock_enable (RCC_AFIO); - led_init(); + ticker_init (); + + led_init (); usart_init (); usb_init (); - /*Start periodic timer*/ - - systick_set_clocksource (STK_CSR_CLKSOURCE_AHB_DIV8); - /* 48MHz / 8 = > 6Mhz */ - systick_set_reload (6000); - /* 6MHz / 6000 => 1kHz */ - systick_interrupt_enable (); - systick_counter_enable (); - - /*Adjust interrupt priorities so that uarts trump timer*/ - nvic_set_priority(NVIC_USART1_IRQ, 0x40); - nvic_set_priority(NVIC_USART2_IRQ, 0x40); - nvic_set_priority(NVIC_USART3_IRQ, 0x40); - nvic_set_priority(NVIC_SYSTICK_IRQ, 0xff); + /*Start periodic timer */ + /*Adjust interrupt priorities so that uarts trump timer */ + nvic_set_priority (NVIC_USART1_IRQ, 0x40); + nvic_set_priority (NVIC_USART2_IRQ, 0x40); + nvic_set_priority (NVIC_USART3_IRQ, 0x40); + nvic_set_priority (NVIC_SYSTICK_IRQ, 0xff); - led_clear(); #if 0 /*GPIO C11 was used to determine if we should enter dfu */ - /*now we use a ramdom address in ram*/ + /*now we use a ramdom address in ram */ gpio_set (GPIOC, GPIO11); gpio_set_mode (GPIOC, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO11); - + { - int i; - for (i = 0; i < 0x80000; i++) - __asm__ ("nop"); + int i; + for (i = 0; i < 0x80000; i++) + __asm__ ("nop"); } gpio_clear (GPIOC, GPIO11); @@ -78,5 +57,7 @@ main (void) usb_run (); + + return 0; } diff --git a/app/mouse.c b/app/mouse.c index 0431349..03879f0 100644 --- a/app/mouse.c +++ b/app/mouse.c @@ -38,6 +38,20 @@ static const uint8_t mouse_report_descriptor[] = { 0x75, 0x06, /* REPORT_SIZE (6) */ 0x95, 0x01, /* REPORT_COUNT (1) */ 0xb1, 0x01, /* FEATURE (Cnst,Ary,Abs) */ + 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ + 0x09, 0x80, /* USAGE (System Control) */ + 0xA1, 0x01, /* COLLECTION (Application) */ + 0x75, 0x02, /* REPORT_SIZE (2) */ + 0x95, 0x01, /* REPORT_COUNT (1) */ + 0x15, 0x01, /* LOGICAL_MIN (1) */ + 0x25, 0x03, /* LOGICAL_MAX (3) */ + 0x09, 0x82, /* USAGE (System Sleep) */ + 0x09, 0x81, /* USAGE (System Power Down) */ + 0x09, 0x83, /* USAGE (System Wake Up) */ + 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */ + 0x75, 0x06, /* REPORT_SIZE (6) */ + 0x81, 0x03, /* INPUT (Cnst Var Abs) */ + 0xc0, /* END COLLECTION */ 0xc0 /* END_COLLECTION */ }; @@ -64,22 +78,22 @@ static const struct const struct usb_endpoint_descriptor mouse_endpoint = { .bLength = USB_DT_ENDPOINT_SIZE, .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 0x82, + .bEndpointAddress = 0x84, .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, - .wMaxPacketSize = 4, - .bInterval = 0x1, //0x20, + .wMaxPacketSize = 5, + .bInterval = 0x1, //0x20, }; const struct usb_interface_descriptor mouse_iface = { .bLength = USB_DT_INTERFACE_SIZE, .bDescriptorType = USB_DT_INTERFACE, - .bInterfaceNumber = 1, + .bInterfaceNumber = 3, .bAlternateSetting = 0, .bNumEndpoints = 1, .bInterfaceClass = USB_CLASS_HID, .bInterfaceSubClass = 1, /* boot */ .bInterfaceProtocol = 2, /* mouse */ - .iInterface = 0, + .iInterface = 8, .endpoint = &mouse_endpoint, @@ -101,7 +115,7 @@ void mouse_test (void) { static int c = 1; - uint8_t buf[4] = { 0, 0, 0, 0 }; + uint8_t buf[5] = { 0, 0, 0, 0 }; buf[0] = c & 7; @@ -111,5 +125,5 @@ mouse_test (void) c++; - usbd_ep_write_packet (usbd_dev, 0x82, buf, 4); + usbd_ep_write_packet (usbd_dev, 0x84, buf, 5); } diff --git a/app/project.h b/app/project.h index a1849df..b224c78 100644 --- a/app/project.h +++ b/app/project.h @@ -2,12 +2,14 @@ #include #include #include +#include #include #include #include #include #include + #define INCLUDE_DFU_INTERFACE diff --git a/app/prototypes.h b/app/prototypes.h index f110e64..afc4f6d 100644 --- a/app/prototypes.h +++ b/app/prototypes.h @@ -1,56 +1,72 @@ /* dfu.c */ 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)); +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)); /* 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); +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); +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); +extern int main (void); /* usb.c */ extern const struct usb_device_descriptor dev; extern const struct usb_interface ifaces[]; extern const struct usb_config_descriptor config; 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); +extern int usb_is_suspended; +extern int usb_wakeup_host (void); +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); +extern void tablet_get_descriptor (uint8_t ** buf, uint16_t * len); +extern void tablet_test (void); /* usart.c */ extern ring_t rx1_ring; extern ring_t tx1_ring; extern ring_t rx2_ring; extern ring_t tx2_ring; extern ring_t tx3_ring; -extern void usart1_isr(void); -extern void usart2_isr(void); -extern void usart3_isr(void); -extern int _write(int file, char *ptr, int len); -extern void usart2_queue(uint8_t d); -extern void usart3_queue(uint8_t d); -extern void usart_init(void); +extern void usart1_isr (void); +extern void usart2_isr (void); +extern void usart3_isr (void); +extern int _write (int file, char *ptr, int len); +extern void usart2_queue (uint8_t d); +extern void usart3_queue (uint8_t d); +extern void usart_init (void); /* kvm.c */ -extern void kvm_dispatch(void); -extern void kvm_recv(uint8_t d); +extern void kvm_dispatch (void); +extern void kvm_recv (uint8_t d); /* ring.c */ -extern void ring_init(ring_t *r, uint8_t *buf, size_t len); -extern int ring_write_byte(ring_t *r, uint8_t c); -extern int ring_read_byte(ring_t *r, uint8_t *c); -extern int ring_write(ring_t *r, uint8_t *buf, size_t len); +extern void ring_init (ring_t * r, uint8_t * buf, size_t len); +extern int ring_write_byte (ring_t * r, uint8_t c); +extern int ring_read_byte (ring_t * r, uint8_t * c); +extern int ring_write (ring_t * r, uint8_t * buf, size_t len); /* led.c */ -extern void led_init(void); -extern void led_clear(void); -extern void led_set(uint32_t v); -extern void led_tick(void); +extern void led_init (void); +extern void led_set (uint32_t v); +extern void led_tick (void); +/* consumer.c */ +extern const struct usb_endpoint_descriptor consumer_endpoint; +extern const struct usb_interface_descriptor consumer_iface; +extern void consumer_get_descriptor (uint8_t ** buf, uint16_t * len); +/* ticker.c */ +extern volatile uint32_t ticks; +extern void delay_us (uint32_t d); +extern void sys_tick_handler (void); +extern void delay_ms (uint32_t d); +extern int timed_out (uint32_t then, unsigned int ms); +extern void ticker_init (void); diff --git a/app/ring.h b/app/ring.h index 4e3a22c..ba8887b 100644 --- a/app/ring.h +++ b/app/ring.h @@ -5,5 +5,3 @@ typedef struct ring size_t write; size_t read; } ring_t; - - diff --git a/app/tablet.c b/app/tablet.c index 5445c29..b3cff1d 100644 --- a/app/tablet.c +++ b/app/tablet.c @@ -44,6 +44,20 @@ static const uint8_t tablet_report_descriptor[] = { 0x75, 0x06, // REPORT_SIZE (6) 0x95, 0x01, // REPORT_COUNT (1) 0xb1, 0x01, // FEATURE (Cnst,Ary,Abs) + 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ + 0x09, 0x80, /* USAGE (System Control) */ + 0xA1, 0x01, /* COLLECTION (Application) */ + 0x75, 0x02, /* REPORT_SIZE (2) */ + 0x95, 0x01, /* REPORT_COUNT (1) */ + 0x15, 0x01, /* LOGICAL_MIN (1) */ + 0x25, 0x03, /* LOGICAL_MAX (3) */ + 0x09, 0x82, /* USAGE (System Sleep) */ + 0x09, 0x81, /* USAGE (System Power Down) */ + 0x09, 0x83, /* USAGE (System Wake Up) */ + 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */ + 0x75, 0x06, /* REPORT_SIZE (6) */ + 0x81, 0x03, /* INPUT (Cnst Var Abs) */ + 0xc0, /* END COLLECTION */ 0xc0 // END_COLLECTION }; @@ -72,8 +86,8 @@ const struct usb_endpoint_descriptor tablet_endpoint = { .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = 0x83, .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, - .wMaxPacketSize = 6, - .bInterval = 0x1 //0x20, + .wMaxPacketSize = 7, + .bInterval = 0x1 //0x20, }; const struct usb_interface_descriptor tablet_iface = { @@ -85,7 +99,7 @@ const struct usb_interface_descriptor tablet_iface = { .bInterfaceClass = USB_CLASS_HID, .bInterfaceSubClass = 1, /* boot */ .bInterfaceProtocol = 2, /* tablet */ - .iInterface = 0, + .iInterface = 7, .endpoint = &tablet_endpoint, @@ -107,7 +121,7 @@ void tablet_test (void) { static int c = 0; - uint8_t buf[6] = { 0, 0, 0, 0, 0 }; + uint8_t buf[7] = { 0, 0, 0, 0, 0, 0 }; buf[0] = c & 0xff; buf[1] = (c & 0x7fff) >> 8; @@ -118,5 +132,5 @@ tablet_test (void) c++; - usbd_ep_write_packet (usbd_dev, 0x83, buf, 6); + usbd_ep_write_packet (usbd_dev, 0x83, buf, 7); } diff --git a/app/usart.c b/app/usart.c index 4061076..24b1e9d 100644 --- a/app/usart.c +++ b/app/usart.c @@ -31,7 +31,7 @@ usart1_isr (void) data = usart_recv (USART1); - ring_write_byte(&rx1_ring,data); + ring_write_byte (&rx1_ring, data); } /* Check if we were called because of TXE. */ @@ -65,7 +65,7 @@ usart2_isr (void) /* Retrieve the data from the peripheral. */ data = usart_recv (USART2); - ring_write_byte(&rx2_ring,data); + ring_write_byte (&rx2_ring, data); } /* Check if we were called because of TXE. */ @@ -163,17 +163,23 @@ usart_init (void) nvic_enable_irq (NVIC_USART2_IRQ); nvic_enable_irq (NVIC_USART3_IRQ); - /* Map pins, and set usarts2 and 3 to have pull ups*/ - gpio_set_mode (GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX); - gpio_set_mode (GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX); - - gpio_set_mode (GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); - gpio_set_mode (GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO_USART2_RX); - gpio_set(GPIOA, GPIO_USART2_RX); - - gpio_set_mode (GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART3_TX); - gpio_set_mode (GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO_USART3_RX); - gpio_set(GPIOB, GPIO_USART3_RX); + /* Map pins, and set usarts2 and 3 to have pull ups */ + gpio_set_mode (GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX); + gpio_set_mode (GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, + GPIO_USART1_RX); + + gpio_set_mode (GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); + gpio_set_mode (GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, + GPIO_USART2_RX); + gpio_set (GPIOA, GPIO_USART2_RX); + + gpio_set_mode (GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART3_TX); + gpio_set_mode (GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, + GPIO_USART3_RX); + gpio_set (GPIOB, GPIO_USART3_RX); diff --git a/app/usb.c b/app/usb.c index 10ecd68..245de1d 100644 --- a/app/usb.c +++ b/app/usb.c @@ -25,18 +25,22 @@ const struct usb_interface ifaces[] = { }, { .num_altsetting = 1, - .altsetting = &mouse_iface, + .altsetting = &consumer_iface, }, { .num_altsetting = 1, .altsetting = &tablet_iface, }, + { + .num_altsetting = 1, + .altsetting = &mouse_iface, + }, #ifdef INCLUDE_DFU_INTERFACE { .num_altsetting = 1, .altsetting = &dfu_iface, + }, #endif - } }; const struct usb_config_descriptor config = { @@ -44,14 +48,14 @@ const struct usb_config_descriptor config = { .bDescriptorType = USB_DT_CONFIGURATION, .wTotalLength = 0, #ifdef INCLUDE_DFU_INTERFACE - .bNumInterfaces = 4, + .bNumInterfaces = 5, #else - .bNumInterfaces = 3, + .bNumInterfaces = 4, #endif .bConfigurationValue = 1, - .iConfiguration = 0, - .bmAttributes = 0xC0, - .bMaxPower = 0x32, + .iConfiguration = 4, + .bmAttributes = 0xa0, + .bMaxPower = 0x31, .interface = ifaces, }; @@ -60,6 +64,12 @@ static const char *usb_strings[] = { "Cabbages are good for you", "fish", "soup", + "kvm composite", + "kvm keyboard", + "kvm consumer", + "kvm tablet", + "kvm mouse", + "kvm dfu device", }; @@ -67,6 +77,47 @@ static const char *usb_strings[] = { usbd_device *usbd_dev; +int usb_is_suspended = 0; + +static void +usb_suspended (void) +{ + *USB_CNTR_REG |= USB_CNTR_FSUSP; + + usb_is_suspended = 1; +} + + +static void +usb_resumed (void) +{ + + *USB_CNTR_REG &= ~USB_CNTR_FSUSP; + + usb_is_suspended = 0; +} + +int +usb_wakeup_host (void) +{ + if (!usb_is_suspended) + return 1; + + + *USB_CNTR_REG |= USB_CNTR_RESUME; + + delay_us (1000); + delay_us (1000); + delay_us (1000); + delay_us (1000); + delay_us (1000); + + *USB_CNTR_REG &= ~USB_CNTR_RESUME; + + return 0; +} + + static int usb_control_request (usbd_device * usbd_dev, struct usb_setup_data *req, uint8_t ** buf, uint16_t * len, @@ -87,11 +138,14 @@ usb_control_request (usbd_device * usbd_dev, struct usb_setup_data *req, keyboard_get_descriptor (buf, len); return 1; case 1: - mouse_get_descriptor (buf, len); + consumer_get_descriptor (buf, len); return 1; case 2: tablet_get_descriptor (buf, len); return 1; + case 3: + mouse_get_descriptor (buf, len); + return 1; } *len = 0; @@ -108,6 +162,7 @@ usb_set_config (usbd_device * usbd_dev, uint16_t wValue) 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_ep_setup (usbd_dev, 0x84, USB_ENDPOINT_ATTR_INTERRUPT, 4, NULL); usbd_register_control_callback (usbd_dev, USB_REQ_TYPE_STANDARD | @@ -121,6 +176,8 @@ usb_set_config (usbd_device * usbd_dev, uint16_t wValue) USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, dfu_control_request); #endif + + } /* Buffer to be used for control requests. */ @@ -131,16 +188,21 @@ usb_init (void) { usbd_dev = - usbd_init (&stm32f103_usb_driver, &dev, &config, usb_strings, 3, + usbd_init (&stm32f103_usb_driver, &dev, &config, usb_strings, 8, usbd_control_buffer, sizeof (usbd_control_buffer)); usbd_register_set_config_callback (usbd_dev, usb_set_config); + usbd_register_suspend_callback (usbd_dev, usb_suspended); + usbd_register_resume_callback (usbd_dev, usb_resumed); + + SET_REG (USB_CNTR_REG, + USB_CNTR_RESETM | USB_CNTR_CTRM | USB_CNTR_SUSPM | USB_CNTR_WKUPM); } void usb_run (void) { - while (1) + for (;;) usbd_poll (usbd_dev); } diff --git a/board/STM32F103R_BOARD.cfg b/board/STM32F103R_BOARD.cfg index 6d1d72b..11217d2 100644 --- a/board/STM32F103R_BOARD.cfg +++ b/board/STM32F103R_BOARD.cfg @@ -1,3 +1,6 @@ # source [find target/stm32f1x.cfg] + +adapter_khz 500 + diff --git a/id.h b/id.h index 76dad09..a7bea99 100644 --- a/id.h +++ b/id.h @@ -1,5 +1,5 @@ -#define ID_PRODUCT 0xee5 +#define ID_PRODUCT 0xee0 #define BUS_ID 0x0 -- cgit v1.2.3