view usb_serial.h @ 9:16a338171c99

remove random.h after programming
author Frank Kingswood <frank@kingswood-consulting.co.uk>
date Mon, 21 Apr 2014 11:14:00 +0100
parents ef38703286dd
children
line wrap: on
line source
1 /* USB Serial code for Teensy USB Development Board
2 * http://www.pjrc.com/teensy/usb_serial.html
3 * Copyright (c) 2008,2010,2011 PJRC.COM, LLC
4 */
6 #ifndef usb_serial_h__
7 #define usb_serial_h__
9 #include <stdint.h>
11 #ifdef __cplusplus
13 #include "formatting.h"
15 extern "C" {
16 #endif
18 // setup
19 void usb_init(void); // initialize everything
20 uint8_t usb_configured(void); // is the USB port configured
22 // receiving data
23 int16_t usb_serial_getchar(void); // receive a character (-1 if timeout/error)
24 uint8_t usb_serial_available(void); // number of bytes in receive buffer
25 void usb_serial_flush_input(void); // discard any buffered input
27 // transmitting data
28 int8_t usb_serial_putchar(uint8_t c); // transmit a character
29 int8_t usb_serial_putchar_nowait(uint8_t c); // transmit a character, do not wait
30 int8_t usb_serial_write(const uint8_t *buffer, uint16_t size); // transmit a buffer
31 void usb_serial_flush_output(void); // immediately transmit any buffered output
32 int8_t usb_serial_puts(const char *);
33 int8_t usb_serial_puts_P(const char *);
35 // serial parameters
36 uint32_t usb_serial_get_baud(void); // get the baud rate
37 uint8_t usb_serial_get_stopbits(void); // get the number of stop bits
38 uint8_t usb_serial_get_paritytype(void);// get the parity type
39 uint8_t usb_serial_get_numbits(void); // get the number of data bits
40 uint8_t usb_serial_get_control(void); // get the RTS and DTR signal state
41 int8_t usb_serial_set_control(uint8_t signals); // set DSR, DCD, RI, etc
43 // constants corresponding to the various serial parameters
44 #define USB_SERIAL_DTR 0x01
45 #define USB_SERIAL_RTS 0x02
46 #define USB_SERIAL_1_STOP 0
47 #define USB_SERIAL_1_5_STOP 1
48 #define USB_SERIAL_2_STOP 2
49 #define USB_SERIAL_PARITY_NONE 0
50 #define USB_SERIAL_PARITY_ODD 1
51 #define USB_SERIAL_PARITY_EVEN 2
52 #define USB_SERIAL_PARITY_MARK 3
53 #define USB_SERIAL_PARITY_SPACE 4
54 #define USB_SERIAL_DCD 0x01
55 #define USB_SERIAL_DSR 0x02
56 #define USB_SERIAL_BREAK 0x04
57 #define USB_SERIAL_RI 0x08
58 #define USB_SERIAL_FRAME_ERR 0x10
59 #define USB_SERIAL_PARITY_ERR 0x20
60 #define USB_SERIAL_OVERRUN_ERR 0x40
62 // This file does not include the HID debug functions, so these empty
63 // macros replace them with nothing, so users can compile code that
64 // has calls to these functions.
65 #define usb_debug_putchar(c)
66 #define usb_debug_flush_output()
68 // Everything below this point is only intended for usb_serial.c
69 #ifdef USB_SERIAL_PRIVATE_INCLUDE
70 #include <avr/io.h>
71 #include <avr/pgmspace.h>
72 #include <avr/interrupt.h>
74 #define EP_TYPE_CONTROL 0x00
75 #define EP_TYPE_BULK_IN 0x81
76 #define EP_TYPE_BULK_OUT 0x80
77 #define EP_TYPE_INTERRUPT_IN 0xC1
78 #define EP_TYPE_INTERRUPT_OUT 0xC0
79 #define EP_TYPE_ISOCHRONOUS_IN 0x41
80 #define EP_TYPE_ISOCHRONOUS_OUT 0x40
81 #define EP_SINGLE_BUFFER 0x02
82 #define EP_DOUBLE_BUFFER 0x06
83 #define EP_SIZE(s) ((s) == 64 ? 0x30 : ((s) == 32 ? 0x20 : ((s) == 16 ? 0x10 : 0x00)))
85 #define MAX_ENDPOINT 4
87 #define LSB(n) (n & 255)
88 #define MSB(n) ((n >> 8) & 255)
90 #if defined(__AVR_AT90USB162__)
91 #define HW_CONFIG()
92 #define PLL_CONFIG() (PLLCSR = ((1<<PLLE)|(1<<PLLP0)))
93 #define USB_CONFIG() (USBCON = (1<<USBE))
94 #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
95 #elif defined(__AVR_ATmega32U4__)
96 #define HW_CONFIG() (UHWCON = 0x01)
97 #define PLL_CONFIG() (PLLCSR = 0x12)
98 #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
99 #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
100 #elif defined(__AVR_AT90USB646__)
101 #define HW_CONFIG() (UHWCON = 0x81)
102 #define PLL_CONFIG() (PLLCSR = 0x1A)
103 #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
104 #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
105 #elif defined(__AVR_AT90USB1286__)
106 #define HW_CONFIG() (UHWCON = 0x81)
107 #define PLL_CONFIG() (PLLCSR = 0x16)
108 #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
109 #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
110 #endif
112 // standard control endpoint request types
113 #define GET_STATUS 0
114 #define CLEAR_FEATURE 1
115 #define SET_FEATURE 3
116 #define SET_ADDRESS 5
117 #define GET_DESCRIPTOR 6
118 #define GET_CONFIGURATION 8
119 #define SET_CONFIGURATION 9
120 #define GET_INTERFACE 10
121 #define SET_INTERFACE 11
122 // HID (human interface device)
123 #define HID_GET_REPORT 1
124 #define HID_GET_PROTOCOL 3
125 #define HID_SET_REPORT 9
126 #define HID_SET_IDLE 10
127 #define HID_SET_PROTOCOL 11
128 // CDC (communication class device)
129 #define CDC_SET_LINE_CODING 0x20
130 #define CDC_GET_LINE_CODING 0x21
131 #define CDC_SET_CONTROL_LINE_STATE 0x22
132 #endif
134 #ifdef __cplusplus
135 } // extern "C"
137 class UsbSerial:public Formatting<UsbSerial>
138 {
139 public:
140 static void Initialize()
141 {
142 usb_init();
143 }
145 static __attribute__((noinline)) void Send_(char c)
146 {
147 // wait forever
148 while(int8_t r=usb_serial_putchar(c))
149 if(r==-9)
150 return;
151 }
153 // immediately transmit any buffered output
154 static void Flush()
155 {
156 usb_serial_flush_output();
157 }
158 };
159 extern const UsbSerial USB;
161 #endif
163 #endif // usb_serial_h__