examples: New board_usb
[simavr] / examples / board_usb / at90usb162_cdc_loopback.h
1 /* vim: set sts=4:sw=4:ts=4:noexpandtab */
2 #ifndef usb_serial_h__
3 #define usb_serial_h__
4
5 #include <stdint.h>
6
7 // setup
8 void usb_init(void);                    // initialize everything
9 uint8_t usb_configured(void);           // is the USB port configured
10
11 // receiving data
12 int16_t usb_serial_getchar(void);       // receive a character (-1 if timeout/error)
13 uint8_t usb_serial_available(void);     // number of bytes in receive buffer
14 void usb_serial_flush_input(void);      // discard any buffered input
15
16 // transmitting data
17 int8_t usb_serial_putchar(uint8_t c);   // transmit a character
18 int8_t usb_serial_putchar_nowait(uint8_t c);  // transmit a character, do not wait
19 int8_t usb_serial_write(const uint8_t *buffer, uint16_t size); // transmit a buffer
20 void usb_serial_flush_output(void);     // immediately transmit any buffered output
21
22 // serial parameters
23 uint32_t usb_serial_get_baud(void);     // get the baud rate
24 uint8_t usb_serial_get_stopbits(void);  // get the number of stop bits
25 uint8_t usb_serial_get_paritytype(void);// get the parity type
26 uint8_t usb_serial_get_numbits(void);   // get the number of data bits
27 uint8_t usb_serial_get_control(void);   // get the RTS and DTR signal state
28 int8_t usb_serial_set_control(uint8_t signals); // set DSR, DCD, RI, etc
29
30 // constants corresponding to the various serial parameters
31 #define USB_SERIAL_DTR                  0x01
32 #define USB_SERIAL_RTS                  0x02
33 #define USB_SERIAL_1_STOP               0
34 #define USB_SERIAL_1_5_STOP             1
35 #define USB_SERIAL_2_STOP               2
36 #define USB_SERIAL_PARITY_NONE          0
37 #define USB_SERIAL_PARITY_ODD           1
38 #define USB_SERIAL_PARITY_EVEN          2
39 #define USB_SERIAL_PARITY_MARK          3
40 #define USB_SERIAL_PARITY_SPACE         4
41 #define USB_SERIAL_DCD                  0x01
42 #define USB_SERIAL_DSR                  0x02
43 #define USB_SERIAL_BREAK                0x04
44 #define USB_SERIAL_RI                   0x08
45 #define USB_SERIAL_FRAME_ERR            0x10
46 #define USB_SERIAL_PARITY_ERR           0x20
47 #define USB_SERIAL_OVERRUN_ERR          0x40
48
49 // This file does not include the HID debug functions, so these empty
50 // macros replace them with nothing, so users can compile code that
51 // has calls to these functions.
52 #define usb_debug_putchar(c)
53 #define usb_debug_flush_output()
54
55
56
57 // Everything below this point is only intended for usb_serial.c
58 #ifdef USB_SERIAL_PRIVATE_INCLUDE
59 #include <avr/io.h>
60 #include <avr/pgmspace.h>
61 #include <avr/interrupt.h>
62
63 #define EP_TYPE_CONTROL                 0x00
64 #define EP_TYPE_BULK_IN                 0x81
65 #define EP_TYPE_BULK_OUT                0x80
66 #define EP_TYPE_INTERRUPT_IN            0xC1
67 #define EP_TYPE_INTERRUPT_OUT           0xC0
68 #define EP_TYPE_ISOCHRONOUS_IN          0x41
69 #define EP_TYPE_ISOCHRONOUS_OUT         0x40
70 #define EP_SINGLE_BUFFER                0x02
71 #define EP_DOUBLE_BUFFER                0x06
72 #define EP_SIZE(s)      ((s) == 64 ? 0x30 :     \
73                         ((s) == 32 ? 0x20 :     \
74                         ((s) == 16 ? 0x10 :     \
75                                      0x00)))
76
77 #define MAX_ENDPOINT            4
78
79 #define LSB(n) (n & 255)
80 #define MSB(n) ((n >> 8) & 255)
81
82 #if defined(__AVR_AT90USB162__)
83 #define HW_CONFIG()
84 #define PLL_CONFIG() (PLLCSR = ((1<<PLLE)|(1<<PLLP0)))
85 #define USB_CONFIG() (USBCON = (1<<USBE))
86 #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
87 #elif defined(__AVR_ATmega32U4__)
88 #define HW_CONFIG() (UHWCON = 0x01)
89 #define PLL_CONFIG() (PLLCSR = 0x12)
90 #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
91 #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
92 #elif defined(__AVR_AT90USB646__)
93 #define HW_CONFIG() (UHWCON = 0x81)
94 #define PLL_CONFIG() (PLLCSR = 0x1A)
95 #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
96 #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
97 #elif defined(__AVR_AT90USB1286__)
98 #define HW_CONFIG() (UHWCON = 0x81)
99 #define PLL_CONFIG() (PLLCSR = 0x16)
100 #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
101 #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
102 #endif
103
104 // standard control endpoint request types
105 #define GET_STATUS                      0
106 #define CLEAR_FEATURE                   1
107 #define SET_FEATURE                     3
108 #define SET_ADDRESS                     5
109 #define GET_DESCRIPTOR                  6
110 #define GET_CONFIGURATION               8
111 #define SET_CONFIGURATION               9
112 #define GET_INTERFACE                   10
113 #define SET_INTERFACE                   11
114 // HID (human interface device)
115 #define HID_GET_REPORT                  1
116 #define HID_GET_PROTOCOL                3
117 #define HID_SET_REPORT                  9
118 #define HID_SET_IDLE                    10
119 #define HID_SET_PROTOCOL                11
120 // CDC (communication class device)
121 #define CDC_SET_LINE_CODING             0x20
122 #define CDC_GET_LINE_CODING             0x21
123 #define CDC_SET_CONTROL_LINE_STATE      0x22
124 #endif
125 #endif