diff --git a/src/keyboard/controller.c b/src/keyboard/controller.c index 3de11d3..3b5bbc3 100644 --- a/src/keyboard/controller.c +++ b/src/keyboard/controller.c @@ -548,10 +548,16 @@ uint8_t twi_read(uint8_t *data) { #define EXTRA_SIZE 8 #define EXTRA_BUFFER EP_DOUBLE_BUFFER +#define DEBUG_INTERFACE 2 +#define DEBUG_TX_ENDPOINT 3 +#define DEBUG_TX_SIZE 32 +#define DEBUG_TX_BUFFER EP_DOUBLE_BUFFER static const uint8_t PROGMEM endpoint_config_table[] = { - 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(KEYBOARD_SIZE) | KEYBOARD_BUFFER, - 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(EXTRA_SIZE) | EXTRA_BUFFER, // 4 + // enable, UECFG0X(type, direction), UECFG1X(size, bank, allocation) + 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(KEYBOARD_SIZE) | KEYBOARD_BUFFER, // 1 + 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(EXTRA_SIZE) | EXTRA_BUFFER, // 2 + 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(DEBUG_TX_SIZE) | DEBUG_TX_BUFFER, // 3 0 }; @@ -614,10 +620,10 @@ static const uint8_t PROGMEM keyboard_hid_report_desc[] = { 0x95, 0x06, // Report Count (6), 0x75, 0x08, // Report Size (8), 0x15, 0x00, // Logical Minimum (0), - 0x25, 0xFF, // Logical Maximum(104), + 0x25, 0xFF, // Logical Maximum(255), 0x05, 0x07, // Usage Page (Key Codes), 0x19, 0x00, // Usage Minimum (0), - 0x29, 0xFF, // Usage Maximum (104), + 0x29, 0xFF, // Usage Maximum (255), 0x81, 0x00, // Input (Data, Array), 0xc0 // End Collection }; @@ -640,14 +646,31 @@ static const uint8_t PROGMEM extra_hid_report_desc[] = { 0xc0, // END_COLLECTION }; -#define KEYBOARD_HID_DESC_NUM 0 +// debug messages +static const uint8_t PROGMEM debug_hid_report_desc[] = { + 0x06, 0x31, 0xFF, // Usage Page 0xFF31 (vendor defined) + 0x09, 0x74, // Usage 0x74 + 0xA1, 0x53, // Collection 0x53 + 0x75, 0x08, // report size = 8 bits + 0x15, 0x00, // logical minimum = 0 + 0x26, 0xFF, 0x00, // logical maximum = 255 + 0x95, DEBUG_TX_SIZE, // report count + 0x09, 0x75, // usage + 0x81, 0x02, // Input (array) + 0xC0 // end collection +}; + + +#define KEYBOARD_HID_DESC_NUM 0 +#define EXTRA_HID_DESC_NUM 1 +#define DEBUG_HID_DESC_NUM 2 +#define NUM_INTERFACES 3 + #define KEYBOARD_HID_DESC_OFFSET (9+(9+9+7)*KEYBOARD_HID_DESC_NUM+9) +#define EXTRA_HID_DESC_OFFSET (9+(9+9+7)*EXTRA_HID_DESC_NUM+9) +#define DEBUG_HID_DESC_OFFSET (9+(9+9+7)*DEBUG_HID_DESC_NUM+9) +#define CONFIG1_DESC_SIZE (9+(9+9+7)*NUM_INTERFACES) -#define EXTRA_HID_DESC_NUM (KEYBOARD_HID_DESC_NUM + 1) -#define EXTRA_HID_DESC_OFFSET (9+(9+9+7)*EXTRA_HID_DESC_NUM+9) - -#define NUM_INTERFACES (EXTRA_HID_DESC_NUM + 1) -#define CONFIG1_DESC_SIZE (9+(9+9+7)*NUM_INTERFACES) static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = { // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10 9, // bLength; @@ -657,8 +680,9 @@ static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = { NUM_INTERFACES, // bNumInterfaces 1, // bConfigurationValue 0, // iConfiguration - 0xC0, // bmAttributes + 0xA0, // bmAttributes 50, // bMaxPower + // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 9, // bLength 4, // bDescriptorType @@ -669,7 +693,6 @@ static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = { 0x01, // bInterfaceSubClass (0x01 = Boot) 0x01, // bInterfaceProtocol (0x01 = Keyboard) 0, // iInterface - // HID descriptor, HID 1.11 spec, section 6.2.1 9, // bLength 0x21, // bDescriptorType @@ -713,6 +736,33 @@ static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = { 0x03, // bmAttributes (0x03=intr) EXTRA_SIZE, 0, // wMaxPacketSize 10, // bInterval + + // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12 + 9, // bLength + 4, // bDescriptorType + DEBUG_INTERFACE, // bInterfaceNumber + 0, // bAlternateSetting + 1, // bNumEndpoints + 0x03, // bInterfaceClass (0x03 = HID) + 0x00, // bInterfaceSubClass + 0x00, // bInterfaceProtocol + 0, // iInterface + // HID descriptor, HID 1.11 spec, section 6.2.1 + 9, // bLength + 0x21, // bDescriptorType + 0x11, 0x01, // bcdHID + 0, // bCountryCode + 1, // bNumDescriptors + 0x22, // bDescriptorType + sizeof(debug_hid_report_desc), // wDescriptorLength + 0, + // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13 + 7, // bLength + 5, // bDescriptorType + DEBUG_TX_ENDPOINT | 0x80, // bEndpointAddress + 0x03, // bmAttributes (0x03=intr) + DEBUG_TX_SIZE, 0, // wMaxPacketSize + 1, // bInterval }; // If you're desperate for a little extra code memory, these strings @@ -743,14 +793,14 @@ static struct descriptor_list_struct { // CONFIGURATION descriptor {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)}, // HID/REPORT descriptors - {0x2100, KEYBOARD_INTERFACE, config1_descriptor + KEYBOARD_HID_DESC_OFFSET, - 9}, - {0x2200, KEYBOARD_INTERFACE, keyboard_hid_report_desc, - sizeof(keyboard_hid_report_desc)}, + {0x2100, KEYBOARD_INTERFACE, config1_descriptor + KEYBOARD_HID_DESC_OFFSET, 9}, + {0x2200, KEYBOARD_INTERFACE, keyboard_hid_report_desc, sizeof(keyboard_hid_report_desc)}, // Extra HID Descriptor {0x2100, EXTRA_INTERFACE, config1_descriptor + EXTRA_HID_DESC_OFFSET, 9}, - {0x2200, EXTRA_INTERFACE, extra_hid_report_desc, - sizeof(extra_hid_report_desc)}, + {0x2200, EXTRA_INTERFACE, extra_hid_report_desc, sizeof(extra_hid_report_desc)}, + // debug descriptors + {0x2100, DEBUG_INTERFACE, config1_descriptor+DEBUG_HID_DESC_OFFSET, 9}, + {0x2200, DEBUG_INTERFACE, debug_hid_report_desc, sizeof(debug_hid_report_desc)}, // STRING descriptors {0x0300, 0x0000, (const uint8_t *)&string0, 4}, {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)}, @@ -794,6 +844,7 @@ volatile uint8_t keyboard_leds=0; uint16_t consumer_key; uint16_t last_consumer_key; +volatile uint8_t debug_flush_timer=0; /************************************************************************** * @@ -1155,3 +1206,24 @@ int8_t usb_extra_consumer_send() { } return result; } + +// -------------------------------------------------------- +// debug +// -------------------------------------------------------- + +// immediately transmit any buffered output. +void usb_debug_flush_output(void) { + uint8_t intr_state; + + intr_state = SREG; + cli(); + if (debug_flush_timer) { + UENUM = DEBUG_TX_ENDPOINT; + while ((UEINTX & (1 << RWAL))) { + UEDATX = 0; + } + UEINTX = 0x3A; + debug_flush_timer = 0; + } + SREG = intr_state; +} diff --git a/src/keyboard/controller.h b/src/keyboard/controller.h index 071c2cc..3519be9 100644 --- a/src/keyboard/controller.h +++ b/src/keyboard/controller.h @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -84,12 +85,6 @@ extern uint8_t keyboard_keys[6]; extern uint16_t consumer_key; -// This file does not include the HID debug functions, so these empty -// macros replace them with nothing, so users can compile code that -// has calls to these functions. -#define usb_debug_putchar(c) -#define usb_debug_flush_output() - // Everything below this point is only intended for usb_serial.c #define EP_TYPE_CONTROL 0x00 @@ -201,3 +196,12 @@ extern uint16_t consumer_key; { k30,k31,k32,k33,k34,k35, na, na,k38,k39,k3A,k3B,k3C,k3D }, \ { k40,k41,k42,k43,k44,k45,k46, k47,k48,k49,k4A,k4B,k4C,k4D }, \ { k50,k51,k52,k53,k54,k55,k56, k57,k58,k59,k5A,k5B,k5C,k5D }} + +// ----------------------------------------------------------------- +// debug +// ----------------------------------------------------------------- + +extern volatile uint8_t debug_flush_timer; + +#define usb_debug_putchar(c) +void usb_debug_flush_output(void); // immediately transmit any buffered output