Add 'teensytp/' from commit 'be48e888050f18a31e788269c8f47358036a8e3b'
[megadrive.git] / teensytp / teensy3 / usb_seremu.h
1 /* Teensyduino Core Library
2  * http://www.pjrc.com/teensy/
3  * Copyright (c) 2013 PJRC.COM, LLC.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * 1. The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * 2. If the Software is incorporated into a build system that allows
17  * selection among a list of target devices, then similar target
18  * devices manufactured by PJRC.COM must be included in the list of
19  * target devices and selectable in the same manner.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
25  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
26  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  * SOFTWARE.
29  */
30
31 #ifndef USBseremu_h_
32 #define USBseremu_h_
33
34 #if defined(USB_HID) || defined(USB_MIDI) || defined(USB_RAWHID) || defined(USB_FLIGHTSIM)
35
36 #include <inttypes.h>
37
38 #if F_CPU >= 20000000
39
40 // C language implementation
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 int usb_seremu_getchar(void);
45 int usb_seremu_peekchar(void);
46 int usb_seremu_available(void);
47 void usb_seremu_flush_input(void);
48 int usb_seremu_putchar(uint8_t c);
49 int usb_seremu_write(const void *buffer, uint32_t size);
50 int usb_seremu_write_buffer_free(void);
51 void usb_seremu_flush_output(void);
52 void usb_seremu_flush_callback(void);
53 extern volatile uint8_t usb_seremu_transmit_flush_timer;
54 extern volatile uint8_t usb_configuration;
55 #ifdef __cplusplus
56 }
57 #endif
58
59 // C++ interface
60 #ifdef __cplusplus
61 #include "Stream.h"
62 class usb_seremu_class : public Stream
63 {
64 public:
65         void begin(long) { /* TODO: call a function that tries to wait for enumeration */ };
66         void end() { /* TODO: flush output and shut down USB port */ };
67         virtual int available() { return usb_seremu_available(); }
68         virtual int read() { return usb_seremu_getchar(); }
69         virtual int peek() { return usb_seremu_peekchar(); }
70         virtual void flush() { usb_seremu_flush_output(); }
71         virtual size_t write(uint8_t c) { return usb_seremu_putchar(c); }
72         virtual size_t write(const uint8_t *buffer, size_t size) { return usb_seremu_write(buffer, size); }
73         size_t write(unsigned long n) { return write((uint8_t)n); }
74         size_t write(long n) { return write((uint8_t)n); }
75         size_t write(unsigned int n) { return write((uint8_t)n); }
76         size_t write(int n) { return write((uint8_t)n); }
77         int availableForWrite() { return usb_seremu_write_buffer_free(); }
78         using Print::write;
79         void send_now(void) { usb_seremu_flush_output(); };
80         uint32_t baud(void) { return 9600; }
81         uint8_t stopbits(void) { return 1; }
82         uint8_t paritytype(void) { return 0; }
83         uint8_t numbits(void) { return 8; }
84         uint8_t dtr(void) { return 1; }
85         uint8_t rts(void) { return 1; }
86         operator bool() { return usb_configuration; }
87 };
88 extern usb_seremu_class Serial;
89 extern void serialEvent(void);
90 #endif // __cplusplus
91
92
93
94 #else  // F_CPU < 20 MHz
95
96 // Allow Arduino programs using Serial to compile, but Serial will do nothing.
97 #ifdef __cplusplus
98 #include "Stream.h"
99 class usb_seremu_class : public Stream
100 {
101 public:
102         void begin(long) { };
103         void end() { };
104         virtual int available() { return 0; }
105         virtual int read() { return -1; }
106         virtual int peek() { return -1; }
107         virtual void flush() { }
108         virtual size_t write(uint8_t c) { return 1; }
109         virtual size_t write(const uint8_t *buffer, size_t size) { return size; }
110         size_t write(unsigned long n) { return 1; }
111         size_t write(long n) { return 1; }
112         size_t write(unsigned int n) { return 1; }
113         size_t write(int n) { return 1; }
114         int availableForWrite() { return 0; }
115         using Print::write;
116         void send_now(void) { }
117         uint32_t baud(void) { return 0; }
118         uint8_t stopbits(void) { return 1; }
119         uint8_t paritytype(void) { return 0; }
120         uint8_t numbits(void) { return 8; }
121         uint8_t dtr(void) { return 1; }
122         uint8_t rts(void) { return 1; }
123         operator bool() { return true; }
124 };
125
126 extern usb_seremu_class Serial;
127 extern void serialEvent(void);
128 #endif // __cplusplus
129
130
131 #endif // F_CPU >= 20 MHz
132
133 #endif // USB_HID
134
135 #endif // USBseremu_h_