blob: 8367f05df2cfa5963ff444f5fd867878856888ec [file] [log] [blame]
Brian Silverman099196d2017-06-21 23:26:02 -07001/* Teensyduino Core Library
2 * http://www.pjrc.com/teensy/
3 * Copyright (c) 2017 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 USBserial_h_
32#define USBserial_h_
33
34#include "usb_desc.h"
35
36#if (defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)) || defined(USB_DISABLED)
37
38#include <inttypes.h>
39
40#if F_CPU >= 20000000 && !defined(USB_DISABLED)
41
42// C language implementation
43#ifdef __cplusplus
44extern "C" {
45#endif
46int usb_serial_getchar(void);
47int usb_serial_peekchar(void);
48int usb_serial_available(void);
49int usb_serial_read(void *buffer, uint32_t size);
50void usb_serial_flush_input(void);
51int usb_serial_putchar(uint8_t c);
52int usb_serial_write(const void *buffer, uint32_t size);
53int usb_serial_write_buffer_free(void);
54void usb_serial_flush_output(void);
55extern uint32_t usb_cdc_line_coding[2];
56extern volatile uint32_t usb_cdc_line_rtsdtr_millis;
57extern volatile uint32_t systick_millis_count;
58extern volatile uint8_t usb_cdc_line_rtsdtr;
59extern volatile uint8_t usb_cdc_transmit_flush_timer;
60extern volatile uint8_t usb_configuration;
61#ifdef __cplusplus
62}
63#endif
64
65#define USB_SERIAL_DTR 0x01
66#define USB_SERIAL_RTS 0x02
67
68// C++ interface
69#ifdef __cplusplus
70#include "Stream.h"
71class usb_serial_class : public Stream
72{
73public:
74 constexpr usb_serial_class() {}
75 void begin(long) {
76 uint32_t millis_begin = systick_millis_count;
77 while (!(*this)) {
78 // wait up to 2.5 seconds for Arduino Serial Monitor
79 // Yes, this is a long time, but some Windows systems open
80 // the port very slowly. This wait allows programs for
81 // Arduino Uno to "just work" (without forcing a reboot when
82 // the port is opened), and when no PC is connected the user's
83 // sketch still gets to run normally after this wait time.
84 if ((uint32_t)(systick_millis_count - millis_begin) > 2500) break;
85 }
86 }
87 void end() { /* TODO: flush output and shut down USB port */ };
88 virtual int available() { return usb_serial_available(); }
89 virtual int read() { return usb_serial_getchar(); }
90 virtual int peek() { return usb_serial_peekchar(); }
91 virtual void flush() { usb_serial_flush_output(); } // TODO: actually wait for data to leave USB...
92 virtual void clear(void) { usb_serial_flush_input(); }
93 virtual size_t write(uint8_t c) { return usb_serial_putchar(c); }
94 virtual size_t write(const uint8_t *buffer, size_t size) { return usb_serial_write(buffer, size); }
95 size_t write(unsigned long n) { return write((uint8_t)n); }
96 size_t write(long n) { return write((uint8_t)n); }
97 size_t write(unsigned int n) { return write((uint8_t)n); }
98 size_t write(int n) { return write((uint8_t)n); }
99 virtual int availableForWrite() { return usb_serial_write_buffer_free(); }
100 using Print::write;
101 void send_now(void) { usb_serial_flush_output(); }
102 uint32_t baud(void) { return usb_cdc_line_coding[0]; }
103 uint8_t stopbits(void) { uint8_t b = usb_cdc_line_coding[1]; if (!b) b = 1; return b; }
104 uint8_t paritytype(void) { return usb_cdc_line_coding[1] >> 8; } // 0=none, 1=odd, 2=even
105 uint8_t numbits(void) { return usb_cdc_line_coding[1] >> 16; }
106 uint8_t dtr(void) { return (usb_cdc_line_rtsdtr & USB_SERIAL_DTR) ? 1 : 0; }
107 uint8_t rts(void) { return (usb_cdc_line_rtsdtr & USB_SERIAL_RTS) ? 1 : 0; }
108 operator bool() { return usb_configuration &&
109 (usb_cdc_line_rtsdtr & (USB_SERIAL_DTR | USB_SERIAL_RTS)) &&
110 ((uint32_t)(systick_millis_count - usb_cdc_line_rtsdtr_millis) >= 25);
111 }
112 size_t readBytes(char *buffer, size_t length) {
113 size_t count=0;
114 unsigned long startMillis = millis();
115 do {
116 count += usb_serial_read(buffer + count, length - count);
117 if (count >= length) return count;
118 } while(millis() - startMillis < _timeout);
119 setReadError();
120 return count;
121 }
122
123};
124extern usb_serial_class Serial;
125extern void serialEvent(void);
126#endif // __cplusplus
127
128
129#else // F_CPU < 20000000
130
131// Allow Arduino programs using Serial to compile, but Serial will do nothing.
132#ifdef __cplusplus
133#include "Stream.h"
134class usb_serial_class : public Stream
135{
136public:
137 constexpr usb_serial_class() {}
138 void begin(long) { };
139 void end() { };
140 virtual int available() { return 0; }
141 virtual int read() { return -1; }
142 virtual int peek() { return -1; }
143 virtual void flush() { }
144 virtual void clear() { }
145 virtual size_t write(uint8_t c) { return 1; }
146 virtual size_t write(const uint8_t *buffer, size_t size) { return size; }
147 size_t write(unsigned long n) { return 1; }
148 size_t write(long n) { return 1; }
149 size_t write(unsigned int n) { return 1; }
150 size_t write(int n) { return 1; }
151 int availableForWrite() { return 0; }
152 using Print::write;
153 void send_now(void) { }
154 uint32_t baud(void) { return 0; }
155 uint8_t stopbits(void) { return 1; }
156 uint8_t paritytype(void) { return 0; }
157 uint8_t numbits(void) { return 8; }
158 uint8_t dtr(void) { return 1; }
159 uint8_t rts(void) { return 1; }
160 operator bool() { return true; }
161};
162
163extern usb_serial_class Serial;
164extern void serialEvent(void);
165#endif // __cplusplus
166
167
168#endif // F_CPU
169
170#endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE
171
172#endif // USBserial_h_