1 /* USB Keyboard and CDC Serial Device for Teensy USB Development Board
2 * Copyright (c) 2009 PJRC.COM, LLC
3 * Modifications by Jacob Alexander (2011-2014)
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #ifndef usb_keyboard_serial_h__
25 #define usb_keyboard_serial_h__
32 #include <avr/pgmspace.h>
33 #include <avr/interrupt.h>
37 #include <util/delay.h>
40 #include "output_com.h"
43 // ----- Function Declarations -----
45 // Basic USB Configuration
46 void usb_init(void); // initialize everything
47 uint8_t usb_configured(void); // is the USB port configured
49 // Keyboard HID Functions
50 int8_t usb_keyboard_send(void);
52 // Chip Level Functions
53 void usb_debug_reload(); // Enable firmware reflash mode
54 void wdt_init(void) __attribute__((naked)) __attribute__((section(".init3"))); // Needed for software reset
56 // USB Serial CDC Functions
57 int16_t usb_serial_getchar(void); // receive a character (-1 if timeout/error)
58 uint8_t usb_serial_available(void); // number of bytes in receive buffer
59 void usb_serial_flush_input(void); // discard any buffered input
62 int8_t usb_serial_putchar(uint8_t c); // transmit a character
63 int8_t usb_serial_putchar_nowait(uint8_t c); // transmit a character, do not wait
64 int8_t usb_serial_write(const char *buffer, uint16_t size); // transmit a buffer
65 void usb_serial_flush_output(void); // immediately transmit any buffered output
68 uint32_t usb_serial_get_baud(void); // get the baud rate
69 uint8_t usb_serial_get_stopbits(void); // get the number of stop bits
70 uint8_t usb_serial_get_paritytype(void);// get the parity type
71 uint8_t usb_serial_get_numbits(void); // get the number of data bits
72 uint8_t usb_serial_get_control(void); // get the RTS and DTR signal state
73 int8_t usb_serial_set_control(uint8_t signals); // set DSR, DCD, RI, etc
79 // Software reset the chip
80 #define usb_debug_software_reset() do { wdt_enable( WDTO_15MS ); for(;;); } while(0)
82 #define EP_SIZE(s) ((s) == 64 ? 0x30 : \
87 #define LSB(n) (n & 255)
88 #define MSB(n) ((n >> 8) & 255)
92 // ----- Defines -----
94 // constants corresponding to the various serial parameters
95 #define USB_SERIAL_DTR 0x01
96 #define USB_SERIAL_RTS 0x02
97 #define USB_SERIAL_1_STOP 0
98 #define USB_SERIAL_1_5_STOP 1
99 #define USB_SERIAL_2_STOP 2
100 #define USB_SERIAL_PARITY_NONE 0
101 #define USB_SERIAL_PARITY_ODD 1
102 #define USB_SERIAL_PARITY_EVEN 2
103 #define USB_SERIAL_PARITY_MARK 3
104 #define USB_SERIAL_PARITY_SPACE 4
105 #define USB_SERIAL_DCD 0x01
106 #define USB_SERIAL_DSR 0x02
107 #define USB_SERIAL_BREAK 0x04
108 #define USB_SERIAL_RI 0x08
109 #define USB_SERIAL_FRAME_ERR 0x10
110 #define USB_SERIAL_PARITY_ERR 0x20
111 #define USB_SERIAL_OVERRUN_ERR 0x40
113 #define EP_TYPE_CONTROL 0x00
114 #define EP_TYPE_BULK_IN 0x81
115 #define EP_TYPE_BULK_OUT 0x80
116 #define EP_TYPE_INTERRUPT_IN 0xC1
117 #define EP_TYPE_INTERRUPT_OUT 0xC0
118 #define EP_TYPE_ISOCHRONOUS_IN 0x41
119 #define EP_TYPE_ISOCHRONOUS_OUT 0x40
121 #define EP_SINGLE_BUFFER 0x02
122 #define EP_DOUBLE_BUFFER 0x06
124 #define MAX_ENDPOINT 4
126 #if defined(__AVR_AT90USB162__)
128 #define PLL_CONFIG() (PLLCSR = ((1<<PLLE)|(1<<PLLP0)))
129 #define USB_CONFIG() (USBCON = (1<<USBE))
130 #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
132 #elif defined(__AVR_ATmega32U4__)
133 #define HW_CONFIG() (UHWCON = 0x01)
134 #define PLL_CONFIG() (PLLCSR = 0x12)
135 #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
136 #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
138 #elif defined(__AVR_AT90USB646__)
139 #define HW_CONFIG() (UHWCON = 0x81)
140 #define PLL_CONFIG() (PLLCSR = 0x1A)
141 #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
142 #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
144 #elif defined(__AVR_AT90USB1286__)
145 #define HW_CONFIG() (UHWCON = 0x81)
146 #define PLL_CONFIG() (PLLCSR = 0x16)
147 #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
148 #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
151 // standard control endpoint request types
153 #define CLEAR_FEATURE 1
154 #define SET_FEATURE 3
155 #define SET_ADDRESS 5
156 #define GET_DESCRIPTOR 6
157 #define GET_CONFIGURATION 8
158 #define SET_CONFIGURATION 9
159 #define GET_INTERFACE 10
160 #define SET_INTERFACE 11
162 // HID (human interface device)
163 #define HID_GET_REPORT 1
164 #define HID_GET_IDLE 2
165 #define HID_GET_PROTOCOL 3
166 #define HID_SET_REPORT 9
167 #define HID_SET_IDLE 10
168 #define HID_SET_PROTOCOL 11
170 // CDC (communication class device)
171 #define CDC_SET_LINE_CODING 0x20
172 #define CDC_GET_LINE_CODING 0x21
173 #define CDC_SET_CONTROL_LINE_STATE 0x22
177 // ----- Endpoint Configuration -----
179 #define ENDPOINT0_SIZE 32
181 #define KEYBOARD_INTERFACE 0
182 #define KEYBOARD_ENDPOINT 2
183 #define KEYBOARD_SIZE 8
184 #define KEYBOARD_INTERVAL 1
185 #define KEYBOARD_HID_BUFFER EP_DOUBLE_BUFFER
187 #define CDC_IAD_DESCRIPTOR 1
188 #define CDC_STATUS_INTERFACE 1
189 #define CDC_DATA_INTERFACE 2
190 #define CDC_ACM_ENDPOINT 3
191 #define CDC_RX_ENDPOINT 4
192 #define CDC_TX_ENDPOINT 5
193 #if defined(__AVR_AT90USB162__)
194 #define CDC_ACM_SIZE 16
195 #define CDC_ACM_BUFFER EP_SINGLE_BUFFER
196 #define CDC_RX_SIZE 32
197 #define CDC_RX_BUFFER EP_DOUBLE_BUFFER
198 #define CDC_TX_SIZE 32
199 #define CDC_TX_BUFFER EP_DOUBLE_BUFFER
201 #define CDC_ACM_SIZE 16
202 #define CDC_ACM_BUFFER EP_SINGLE_BUFFER
203 #define CDC_RX_SIZE 64
204 #define CDC_RX_BUFFER EP_DOUBLE_BUFFER
205 #define CDC_TX_SIZE 64
206 #define CDC_TX_BUFFER EP_DOUBLE_BUFFER
209 // Endpoint 0 is reserved for the control endpoint
210 // Endpoint 1 has a 256 byte buffer
211 // Endpoints 2-6 have 64 byte buffers
212 static const uint8_t PROGMEM endpoint_config_table[] = {
214 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(KEYBOARD_SIZE) | KEYBOARD_HID_BUFFER, // 64 byte
215 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(CDC_ACM_SIZE) | CDC_ACM_BUFFER, // 64 byte
216 1, EP_TYPE_BULK_OUT, EP_SIZE(CDC_RX_SIZE) | CDC_RX_BUFFER, // 64 byte
217 1, EP_TYPE_BULK_IN, EP_SIZE(CDC_TX_SIZE) | CDC_TX_BUFFER, // 64 byte
220 #endif // usb_keyboard_serial_h__