1 /* Teensyduino Core Library
2 * http://www.pjrc.com/teensy/
3 * Copyright (c) 2013 PJRC.COM, LLC.
4 * Modified by Jacob Alexander 2013-2015
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 == 72000000
61 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512) // XXX Correct?
62 #elif F_CPU == 48000000
63 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
64 #elif F_CPU == 24000000
65 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
70 // ----- Variables -----
72 // serial port settings (baud rate, control signals, etc) set
73 // by the PC. These are ignored, but kept in RAM.
74 volatile uint8_t usb_cdc_line_coding[7] = { 0x00, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x08 };
75 volatile uint8_t usb_cdc_line_rtsdtr = 0;
76 volatile uint8_t usb_cdc_transmit_flush_timer = 0;
78 static usb_packet_t *rx_packet = NULL;
79 static usb_packet_t *tx_packet = NULL;
80 static volatile uint8_t tx_noautoflush = 0;
82 // When we've suffered the transmit timeout, don't wait again until the computer
83 // begins accepting data. If no software is running to receive, we'll just discard
84 // data as rapidly as Serial.print() can generate it, until there's something to
85 // actually receive it.
86 static uint8_t transmit_previous_timeout = 0;
90 // ----- Functions -----
92 // get the next character, or -1 if nothing received
93 int usb_serial_getchar()
100 if ( !usb_configuration )
102 rx_packet = usb_rx( CDC_RX_ENDPOINT );
106 i = rx_packet->index;
107 c = rx_packet->buf[i++];
108 if ( i >= rx_packet->len )
110 usb_free( rx_packet );
115 rx_packet->index = i;
120 // peek at the next character, or -1 if nothing received
121 int usb_serial_peekchar()
125 if ( !usb_configuration )
127 rx_packet = usb_rx( CDC_RX_ENDPOINT );
133 return rx_packet->buf[ rx_packet->index ];
136 // number of bytes available in the receive buffer
137 int usb_serial_available()
139 int count = usb_rx_byte_count( CDC_RX_ENDPOINT );
141 count += rx_packet->len - rx_packet->index;
145 // read a block of bytes to a buffer
146 int usb_serial_read( void *buffer, uint32_t size )
148 uint8_t *p = (uint8_t *)buffer;
149 uint32_t qty, count=0;
153 if ( !usb_configuration )
158 rx_packet = usb_rx(CDC_RX_ENDPOINT);
161 if ( rx_packet->len == 0 )
167 qty = rx_packet->len - rx_packet->index;
170 memcpy( p, rx_packet->buf + rx_packet->index, qty );
174 rx_packet->index += qty;
175 if ( rx_packet->index >= rx_packet->len )
177 usb_free( rx_packet );
184 // discard any buffered input
185 void usb_serial_flush_input()
189 if ( !usb_configuration )
193 usb_free( rx_packet );
198 rx = usb_rx( CDC_RX_ENDPOINT );
205 // transmit a character. 0 returned on success, -1 on error
206 int usb_serial_putchar( uint8_t c )
208 return usb_serial_write( &c, 1 );
211 int usb_serial_write( const void *buffer, uint32_t size )
215 const uint8_t *src = (const uint8_t *)buffer;
226 if ( !usb_configuration )
231 if ( usb_tx_packet_count( CDC_TX_ENDPOINT ) < TX_PACKET_LIMIT )
234 tx_packet = usb_malloc();
239 if ( ++wait_count > TX_TIMEOUT || transmit_previous_timeout )
241 transmit_previous_timeout = 1;
247 transmit_previous_timeout = 0;
248 len = CDC_TX_SIZE - tx_packet->index;
251 dest = tx_packet->buf + tx_packet->index;
252 tx_packet->index += len;
256 if ( tx_packet->index >= CDC_TX_SIZE )
258 tx_packet->len = CDC_TX_SIZE;
259 usb_tx( CDC_TX_ENDPOINT, tx_packet );
262 usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
268 void usb_serial_flush_output()
270 if ( !usb_configuration )
275 usb_cdc_transmit_flush_timer = 0;
276 tx_packet->len = tx_packet->index;
277 usb_tx( CDC_TX_ENDPOINT, tx_packet );
282 usb_packet_t *tx = usb_malloc();
285 usb_cdc_transmit_flush_timer = 0;
286 usb_tx( CDC_TX_ENDPOINT, tx );
290 usb_cdc_transmit_flush_timer = 1;
296 void usb_serial_flush_callback()
298 if ( tx_noautoflush )
302 tx_packet->len = tx_packet->index;
303 usb_tx( CDC_TX_ENDPOINT, tx_packet );
306 usb_packet_t *tx = usb_malloc();
309 usb_tx( CDC_TX_ENDPOINT, tx );
313 usb_cdc_transmit_flush_timer = 1;