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