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
32 // ----- Includes -----
35 #include <string.h> // For memcpy
38 #include <Lib/OutputLib.h>
42 #include "usb_serial.h"
46 // ----- Defines -----
48 #define TRANSMIT_FLUSH_TIMEOUT 5 /* in milliseconds */
50 // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
51 #define TX_PACKET_LIMIT 8
53 // When the PC isn't listening, how long do we wait before discarding data? If this is
54 // too short, we risk losing data during the stalls that are common with ordinary desktop
55 // software. If it's too long, we stall the user's program when no software is running.
56 #define TX_TIMEOUT_MSEC 70
59 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
60 #elif F_CPU == 48000000
61 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
62 #elif F_CPU == 24000000
63 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
68 // ----- Variables -----
70 // serial port settings (baud rate, control signals, etc) set
71 // by the PC. These are ignored, but kept in RAM.
72 volatile uint8_t usb_cdc_line_coding[7] = { 0x00, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x08 };
73 volatile uint8_t usb_cdc_line_rtsdtr = 0;
74 volatile uint8_t usb_cdc_transmit_flush_timer = 0;
76 static usb_packet_t *rx_packet = NULL;
77 static usb_packet_t *tx_packet = NULL;
78 static volatile uint8_t tx_noautoflush = 0;
80 // When we've suffered the transmit timeout, don't wait again until the computer
81 // begins accepting data. If no software is running to receive, we'll just discard
82 // data as rapidly as Serial.print() can generate it, until there's something to
83 // actually receive it.
84 static uint8_t transmit_previous_timeout = 0;
88 // ----- Functions -----
90 // get the next character, or -1 if nothing received
91 int usb_serial_getchar()
98 if ( !usb_configuration )
100 rx_packet = usb_rx( CDC_RX_ENDPOINT );
104 i = rx_packet->index;
105 c = rx_packet->buf[i++];
106 if ( i >= rx_packet->len )
108 usb_free( rx_packet );
113 rx_packet->index = i;
118 // peek at the next character, or -1 if nothing received
119 int usb_serial_peekchar()
123 if ( !usb_configuration )
125 rx_packet = usb_rx( CDC_RX_ENDPOINT );
131 return rx_packet->buf[ rx_packet->index ];
134 // number of bytes available in the receive buffer
135 int usb_serial_available()
137 int count = usb_rx_byte_count( CDC_RX_ENDPOINT );
139 count += rx_packet->len - rx_packet->index;
143 // read a block of bytes to a buffer
144 int usb_serial_read( void *buffer, uint32_t size )
146 uint8_t *p = (uint8_t *)buffer;
147 uint32_t qty, count=0;
151 if ( !usb_configuration )
156 rx_packet = usb_rx(CDC_RX_ENDPOINT);
159 if ( rx_packet->len == 0 )
165 qty = rx_packet->len - rx_packet->index;
168 memcpy( p, rx_packet->buf + rx_packet->index, qty );
172 rx_packet->index += qty;
173 if ( rx_packet->index >= rx_packet->len )
175 usb_free( rx_packet );
182 // discard any buffered input
183 void usb_serial_flush_input()
187 if ( !usb_configuration )
191 usb_free( rx_packet );
196 rx = usb_rx( CDC_RX_ENDPOINT );
203 // transmit a character. 0 returned on success, -1 on error
204 int usb_serial_putchar( uint8_t c )
206 return usb_serial_write( &c, 1 );
209 int usb_serial_write( const void *buffer, uint32_t size )
213 const uint8_t *src = (const uint8_t *)buffer;
224 if ( !usb_configuration )
229 if ( usb_tx_packet_count( CDC_TX_ENDPOINT ) < TX_PACKET_LIMIT )
232 tx_packet = usb_malloc();
237 if ( ++wait_count > TX_TIMEOUT || transmit_previous_timeout )
239 transmit_previous_timeout = 1;
245 transmit_previous_timeout = 0;
246 len = CDC_TX_SIZE - tx_packet->index;
249 dest = tx_packet->buf + tx_packet->index;
250 tx_packet->index += len;
254 if ( tx_packet->index >= CDC_TX_SIZE )
256 tx_packet->len = CDC_TX_SIZE;
257 usb_tx( CDC_TX_ENDPOINT, tx_packet );
260 usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
266 void usb_serial_flush_output()
268 if ( !usb_configuration )
273 usb_cdc_transmit_flush_timer = 0;
274 tx_packet->len = tx_packet->index;
275 usb_tx( CDC_TX_ENDPOINT, tx_packet );
280 usb_packet_t *tx = usb_malloc();
283 usb_cdc_transmit_flush_timer = 0;
284 usb_tx( CDC_TX_ENDPOINT, tx );
288 usb_cdc_transmit_flush_timer = 1;
294 void usb_serial_flush_callback()
296 if ( tx_noautoflush )
300 tx_packet->len = tx_packet->index;
301 usb_tx( CDC_TX_ENDPOINT, tx_packet );
304 usb_packet_t *tx = usb_malloc();
307 usb_tx( CDC_TX_ENDPOINT, tx );
311 usb_cdc_transmit_flush_timer = 1;