1 /* Teensyduino Core Library
2 * http://www.pjrc.com/teensy/
3 * Copyright (c) 2013 PJRC.COM, LLC.
4 * Modified by Jacob Alexander 2013-2014
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * 1. The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * 2. If the Software is incorporated into a build system that allows
18 * selection among a list of target devices, then similar target
19 * devices manufactured by PJRC.COM must be included in the list of
20 * target devices and selectable in the same manner.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
26 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include "usb_serial.h"
34 #include <Lib/OutputLib.h>
35 #include <string.h> // For memcpy
37 // defined by usb_dev.h -> usb_desc.h
38 #if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
40 uint32_t usb_cdc_line_coding[2];
41 volatile uint8_t usb_cdc_line_rtsdtr=0;
42 volatile uint8_t usb_cdc_transmit_flush_timer=0;
44 static usb_packet_t *rx_packet=NULL;
45 static usb_packet_t *tx_packet=NULL;
46 static volatile uint8_t tx_noautoflush=0;
48 #define TRANSMIT_FLUSH_TIMEOUT 5 /* in milliseconds */
50 // get the next character, or -1 if nothing received
51 int usb_serial_getchar(void)
57 if (!usb_configuration) return -1;
58 rx_packet = usb_rx(CDC_RX_ENDPOINT);
59 if (!rx_packet) return -1;
62 c = rx_packet->buf[i++];
63 if (i >= rx_packet->len) {
72 // peek at the next character, or -1 if nothing received
73 int usb_serial_peekchar(void)
76 if (!usb_configuration) return -1;
77 rx_packet = usb_rx(CDC_RX_ENDPOINT);
78 if (!rx_packet) return -1;
80 if (!rx_packet) return -1;
81 return rx_packet->buf[rx_packet->index];
84 // number of bytes available in the receive buffer
85 int usb_serial_available(void)
88 count = usb_rx_byte_count(CDC_RX_ENDPOINT);
89 if (rx_packet) count += rx_packet->len - rx_packet->index;
93 // read a block of bytes to a buffer
94 int usb_serial_read(void *buffer, uint32_t size)
96 uint8_t *p = (uint8_t *)buffer;
97 uint32_t qty, count=0;
100 if (!usb_configuration) break;
103 rx_packet = usb_rx(CDC_RX_ENDPOINT);
104 if (!rx_packet) break;
105 if (rx_packet->len == 0) {
110 qty = rx_packet->len - rx_packet->index;
111 if (qty > size) qty = size;
112 memcpy(p, rx_packet->buf + rx_packet->index, qty);
116 rx_packet->index += qty;
117 if (rx_packet->index >= rx_packet->len) {
125 // discard any buffered input
126 void usb_serial_flush_input(void)
130 if (!usb_configuration) return;
136 rx = usb_rx(CDC_RX_ENDPOINT);
142 // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
143 #define TX_PACKET_LIMIT 8
145 // When the PC isn't listening, how long do we wait before discarding data? If this is
146 // too short, we risk losing data during the stalls that are common with ordinary desktop
147 // software. If it's too long, we stall the user's program when no software is running.
148 #define TX_TIMEOUT_MSEC 70
150 #if F_CPU == 96000000
151 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
152 #elif F_CPU == 48000000
153 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
154 #elif F_CPU == 24000000
155 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
158 // When we've suffered the transmit timeout, don't wait again until the computer
159 // begins accepting data. If no software is running to receive, we'll just discard
160 // data as rapidly as Serial.print() can generate it, until there's something to
161 // actually receive it.
162 static uint8_t transmit_previous_timeout=0;
165 // transmit a character. 0 returned on success, -1 on error
166 int usb_serial_putchar(uint8_t c)
168 return usb_serial_write(&c, 1);
172 int usb_serial_write(const void *buffer, uint32_t size)
176 const uint8_t *src = (const uint8_t *)buffer;
184 if (!usb_configuration) {
188 if (usb_tx_packet_count(CDC_TX_ENDPOINT) < TX_PACKET_LIMIT) {
190 tx_packet = usb_malloc();
191 if (tx_packet) break;
194 if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
195 transmit_previous_timeout = 1;
201 transmit_previous_timeout = 0;
202 len = CDC_TX_SIZE - tx_packet->index;
203 if (len > size) len = size;
204 dest = tx_packet->buf + tx_packet->index;
205 tx_packet->index += len;
207 while (len-- > 0) *dest++ = *src++;
208 if (tx_packet->index >= CDC_TX_SIZE) {
209 tx_packet->len = CDC_TX_SIZE;
210 usb_tx(CDC_TX_ENDPOINT, tx_packet);
213 usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
219 void usb_serial_flush_output(void)
221 if (!usb_configuration) return;
224 usb_cdc_transmit_flush_timer = 0;
225 tx_packet->len = tx_packet->index;
226 usb_tx(CDC_TX_ENDPOINT, tx_packet);
229 usb_packet_t *tx = usb_malloc();
231 usb_cdc_transmit_flush_timer = 0;
232 usb_tx(CDC_TX_ENDPOINT, tx);
234 usb_cdc_transmit_flush_timer = 1;
240 void usb_serial_flush_callback(void)
242 if (tx_noautoflush) return;
244 tx_packet->len = tx_packet->index;
245 usb_tx(CDC_TX_ENDPOINT, tx_packet);
248 usb_packet_t *tx = usb_malloc();
250 usb_tx(CDC_TX_ENDPOINT, tx);
252 usb_cdc_transmit_flush_timer = 1;
257 #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE