1 /* Teensyduino Core Library
2 * http://www.pjrc.com/teensy/
3 * Copyright (c) 2013 PJRC.COM, LLC.
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:
13 * 1. The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
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.
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
32 #include "usb_serial.h"
33 #include <Lib/USBLib.h>
34 #include <string.h> // For memcpy
36 // defined by usb_dev.h -> usb_desc.h
37 #if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
39 uint32_t usb_cdc_line_coding[2];
40 volatile uint8_t usb_cdc_line_rtsdtr=0;
41 volatile uint8_t usb_cdc_transmit_flush_timer=0;
43 static usb_packet_t *rx_packet=NULL;
44 static usb_packet_t *tx_packet=NULL;
45 static volatile uint8_t tx_noautoflush=0;
47 #define TRANSMIT_FLUSH_TIMEOUT 5 /* in milliseconds */
49 // get the next character, or -1 if nothing received
50 int usb_serial_getchar(void)
56 if (!usb_configuration) return -1;
57 rx_packet = usb_rx(CDC_RX_ENDPOINT);
58 if (!rx_packet) return -1;
61 c = rx_packet->buf[i++];
62 if (i >= rx_packet->len) {
71 // peek at the next character, or -1 if nothing received
72 int usb_serial_peekchar(void)
75 if (!usb_configuration) return -1;
76 rx_packet = usb_rx(CDC_RX_ENDPOINT);
77 if (!rx_packet) return -1;
79 if (!rx_packet) return -1;
80 return rx_packet->buf[rx_packet->index];
83 // number of bytes available in the receive buffer
84 int usb_serial_available(void)
87 count = usb_rx_byte_count(CDC_RX_ENDPOINT);
88 if (rx_packet) count += rx_packet->len - rx_packet->index;
92 // read a block of bytes to a buffer
93 int usb_serial_read(void *buffer, uint32_t size)
95 uint8_t *p = (uint8_t *)buffer;
96 uint32_t qty, count=0;
99 if (!usb_configuration) break;
102 rx_packet = usb_rx(CDC_RX_ENDPOINT);
103 if (!rx_packet) break;
104 if (rx_packet->len == 0) {
109 qty = rx_packet->len - rx_packet->index;
110 if (qty > size) qty = size;
111 memcpy(p, rx_packet->buf + rx_packet->index, qty);
115 rx_packet->index += qty;
116 if (rx_packet->index >= rx_packet->len) {
124 // discard any buffered input
125 void usb_serial_flush_input(void)
129 if (!usb_configuration) return;
135 rx = usb_rx(CDC_RX_ENDPOINT);
141 // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
142 #define TX_PACKET_LIMIT 8
144 // When the PC isn't listening, how long do we wait before discarding data? If this is
145 // too short, we risk losing data during the stalls that are common with ordinary desktop
146 // software. If it's too long, we stall the user's program when no software is running.
147 #define TX_TIMEOUT_MSEC 70
149 #if F_CPU == 96000000
150 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
151 #elif F_CPU == 48000000
152 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
153 #elif F_CPU == 24000000
154 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
157 // When we've suffered the transmit timeout, don't wait again until the computer
158 // begins accepting data. If no software is running to receive, we'll just discard
159 // data as rapidly as Serial.print() can generate it, until there's something to
160 // actually receive it.
161 static uint8_t transmit_previous_timeout=0;
164 // transmit a character. 0 returned on success, -1 on error
165 int usb_serial_putchar(uint8_t c)
167 return usb_serial_write(&c, 1);
171 int usb_serial_write(const void *buffer, uint32_t size)
175 const uint8_t *src = (const uint8_t *)buffer;
183 if (!usb_configuration) {
187 if (usb_tx_packet_count(CDC_TX_ENDPOINT) < TX_PACKET_LIMIT) {
189 tx_packet = usb_malloc();
190 if (tx_packet) break;
193 if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
194 transmit_previous_timeout = 1;
200 transmit_previous_timeout = 0;
201 len = CDC_TX_SIZE - tx_packet->index;
202 if (len > size) len = size;
203 dest = tx_packet->buf + tx_packet->index;
204 tx_packet->index += len;
206 while (len-- > 0) *dest++ = *src++;
207 if (tx_packet->index >= CDC_TX_SIZE) {
208 tx_packet->len = CDC_TX_SIZE;
209 usb_tx(CDC_TX_ENDPOINT, tx_packet);
212 usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
218 void usb_serial_flush_output(void)
220 if (!usb_configuration) return;
223 usb_cdc_transmit_flush_timer = 0;
224 tx_packet->len = tx_packet->index;
225 usb_tx(CDC_TX_ENDPOINT, tx_packet);
228 usb_packet_t *tx = usb_malloc();
230 usb_cdc_transmit_flush_timer = 0;
231 usb_tx(CDC_TX_ENDPOINT, tx);
233 usb_cdc_transmit_flush_timer = 1;
239 void usb_serial_flush_callback(void)
241 if (tx_noautoflush) return;
243 tx_packet->len = tx_packet->index;
244 usb_tx(CDC_TX_ENDPOINT, tx_packet);
247 usb_packet_t *tx = usb_malloc();
249 usb_tx(CDC_TX_ENDPOINT, tx);
251 usb_cdc_transmit_flush_timer = 1;
256 #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE