]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - pjrc/usb.h
host interface for pjrc
[max/tmk_keyboard.git] / pjrc / usb.h
1 #ifndef USB_H
2 #define  USB_H 1
3
4 #include <stdint.h>
5 #include <stdbool.h>
6 #include <avr/io.h>
7
8
9 extern bool remote_wakeup;
10 extern bool suspend;
11
12 void usb_init(void);                    // initialize everything
13 uint8_t usb_configured(void);           // is the USB port configured
14 void usb_remote_wakeup(void);
15
16
17 #define EP_TYPE_CONTROL                 0x00
18 #define EP_TYPE_BULK_IN                 0x81
19 #define EP_TYPE_BULK_OUT                0x80
20 #define EP_TYPE_INTERRUPT_IN            0xC1
21 #define EP_TYPE_INTERRUPT_OUT           0xC0
22 #define EP_TYPE_ISOCHRONOUS_IN          0x41
23 #define EP_TYPE_ISOCHRONOUS_OUT         0x40
24
25 #define EP_SINGLE_BUFFER                0x02
26 #define EP_DOUBLE_BUFFER                0x06
27
28 #define EP_SIZE(s)      ((s) == 64 ? 0x30 :     \
29                         ((s) == 32 ? 0x20 :     \
30                         ((s) == 16 ? 0x10 :     \
31                                      0x00)))
32
33 #define MAX_ENDPOINT            4
34
35 #define LSB(n) (n & 255)
36 #define MSB(n) ((n >> 8) & 255)
37
38 #if defined(__AVR_AT90USB162__)
39 #define HW_CONFIG() 
40 #define PLL_CONFIG() (PLLCSR = ((1<<PLLE)|(1<<PLLP0)))
41 #define USB_CONFIG() (USBCON = (1<<USBE))
42 #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
43 #elif defined(__AVR_ATmega32U4__)
44 #define HW_CONFIG() (UHWCON = 0x01)
45 #define PLL_CONFIG() (PLLCSR = 0x12)
46 #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
47 #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
48 #elif defined(__AVR_AT90USB646__)
49 #define HW_CONFIG() (UHWCON = 0x81)
50 #define PLL_CONFIG() (PLLCSR = 0x1A)
51 #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
52 #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
53 #elif defined(__AVR_AT90USB1286__)
54 #define HW_CONFIG() (UHWCON = 0x81)
55 #define PLL_CONFIG() (PLLCSR = 0x16)
56 #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
57 #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
58 #endif
59
60 // standard control endpoint request types
61 #define GET_STATUS                      0
62 #define CLEAR_FEATURE                   1
63 #define SET_FEATURE                     3
64 #define SET_ADDRESS                     5
65 #define GET_DESCRIPTOR                  6
66 #define GET_CONFIGURATION               8
67 #define SET_CONFIGURATION               9
68 #define GET_INTERFACE                   10
69 #define SET_INTERFACE                   11
70 // HID (human interface device)
71 #define HID_GET_REPORT                  1
72 #define HID_GET_IDLE                    2
73 #define HID_GET_PROTOCOL                3
74 #define HID_SET_REPORT                  9
75 #define HID_SET_IDLE                    10
76 #define HID_SET_PROTOCOL                11
77 #define HID_REPORT_INPUT                1
78 #define HID_REPORT_OUTPUT               2
79 #define HID_REPORT_FEATURE              3
80 // CDC (communication class device)
81 #define CDC_SET_LINE_CODING             0x20
82 #define CDC_GET_LINE_CODING             0x21
83 #define CDC_SET_CONTROL_LINE_STATE      0x22
84 // HID feature selectors
85 #define DEVICE_REMOTE_WAKEUP            1
86 #define ENDPOINT_HALT                   0
87 #define TEST_MODE                       2
88
89
90 /*------------------------------------------------------------------*
91  * Keyboard descriptor setting
92  *------------------------------------------------------------------*/
93 #define KBD_INTERFACE           0
94 #define KBD_ENDPOINT            1
95 #define KBD_SIZE                8
96 #define KBD_BUFFER              EP_DOUBLE_BUFFER
97 #define KBD_REPORT_KEYS         (KBD_SIZE - 2)
98
99 // secondary keyboard
100 #ifdef USB_NKRO_ENABLE
101 #define KBD2_INTERFACE          4
102 #define KBD2_ENDPOINT           5
103 #define KBD2_SIZE               16
104 #define KBD2_BUFFER             EP_DOUBLE_BUFFER
105 #define KBD2_REPORT_KEYS        (KBD2_SIZE - 1)
106 #endif
107
108 #endif