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
34 #include "usb_seremu.h"
35 #include "core_pins.h" // for yield()
36 //#include "HardwareSerial.h"
38 #ifdef SEREMU_INTERFACE // defined by usb_dev.h -> usb_desc.h
40 volatile uint8_t usb_seremu_transmit_flush_timer=0;
42 static usb_packet_t *rx_packet=NULL;
43 static usb_packet_t *tx_packet=NULL;
44 static volatile uint8_t tx_noautoflush=0;
46 #define TRANSMIT_FLUSH_TIMEOUT 5 /* in milliseconds */
49 // get the next character, or -1 if nothing received
50 int usb_seremu_getchar(void)
56 if (!usb_configuration) return -1;
57 if (!rx_packet) rx_packet = usb_rx(SEREMU_RX_ENDPOINT);
58 if (!rx_packet) return -1;
60 c = rx_packet->buf[i++];
62 if (i >= rx_packet->len) {
75 // peek at the next character, or -1 if nothing received
76 int usb_seremu_peekchar(void)
81 if (!usb_configuration) return -1;
82 if (!rx_packet) rx_packet = usb_rx(SEREMU_RX_ENDPOINT);
83 if (!rx_packet) return -1;
84 c = rx_packet->buf[rx_packet->index];
91 // number of bytes available in the receive buffer
92 int usb_seremu_available(void)
97 if (usb_configuration) rx_packet = usb_rx(SEREMU_RX_ENDPOINT);
98 if (!rx_packet) return 0;
100 len = rx_packet->len;
101 i = rx_packet->index;
103 for (i = rx_packet->index; i < len; i++) {
104 if (rx_packet->buf[i] == 0) break;
115 // discard any buffered input
116 void usb_seremu_flush_input(void)
120 if (!usb_configuration) return;
126 rx = usb_rx(SEREMU_RX_ENDPOINT);
134 // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
135 #define TX_PACKET_LIMIT 6
137 // When the PC isn't listening, how long do we wait before discarding data? If this is
138 // too short, we risk losing data during the stalls that are common with ordinary desktop
139 // software. If it's too long, we stall the user's program when no software is running.
140 #define TX_TIMEOUT_MSEC 30
142 #if F_CPU == 168000000
143 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
144 #elif F_CPU == 144000000
145 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
146 #elif F_CPU == 120000000
147 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
148 #elif F_CPU == 96000000
149 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
150 #elif F_CPU == 72000000
151 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
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)
159 // When we've suffered the transmit timeout, don't wait again until the computer
160 // begins accepting data. If no software is running to receive, we'll just discard
161 // data as rapidly as Serial.print() can generate it, until there's something to
162 // actually receive it.
163 static uint8_t transmit_previous_timeout=0;
166 // transmit a character. 0 returned on success, -1 on error
167 int usb_seremu_putchar(uint8_t c)
169 return usb_seremu_write(&c, 1);
173 int usb_seremu_write(const void *buffer, uint32_t size)
178 const uint8_t *src = (const uint8_t *)buffer;
186 if (!usb_configuration) {
190 if (usb_tx_packet_count(SEREMU_TX_ENDPOINT) < TX_PACKET_LIMIT) {
192 tx_packet = usb_malloc();
193 if (tx_packet) break;
195 if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
196 transmit_previous_timeout = 1;
205 transmit_previous_timeout = 0;
206 len = SEREMU_TX_SIZE - tx_packet->index;
207 if (len > size) len = size;
208 dest = tx_packet->buf + tx_packet->index;
209 tx_packet->index += len;
211 while (len-- > 0) *dest++ = *src++;
212 if (tx_packet->index < SEREMU_TX_SIZE) {
213 usb_seremu_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
215 tx_packet->len = SEREMU_TX_SIZE;
216 usb_seremu_transmit_flush_timer = 0;
217 usb_tx(SEREMU_TX_ENDPOINT, tx_packet);
226 int usb_seremu_write_buffer_free(void)
232 if (!usb_configuration ||
233 usb_tx_packet_count(SEREMU_TX_ENDPOINT) >= TX_PACKET_LIMIT ||
234 (tx_packet = usb_malloc()) == NULL) {
239 len = SEREMU_TX_SIZE - tx_packet->index;
244 void usb_seremu_flush_output(void)
248 if (!usb_configuration) return;
249 //serial_print("usb_serial_flush_output\n");
250 if (tx_packet && tx_packet->index > 0) {
251 usb_seremu_transmit_flush_timer = 0;
252 for (i = tx_packet->index; i < SEREMU_TX_SIZE; i++) {
253 tx_packet->buf[i] = 0;
255 tx_packet->len = SEREMU_TX_SIZE;
256 usb_tx(SEREMU_TX_ENDPOINT, tx_packet);
259 // while (usb_tx_byte_count(SEREMU_TX_ENDPOINT) > 0) ; // wait
262 void usb_seremu_flush_callback(void)
266 if (tx_noautoflush) return;
267 //serial_print("usb_flush_callback \n");
268 for (i = tx_packet->index; i < SEREMU_TX_SIZE; i++) {
269 tx_packet->buf[i] = 0;
271 tx_packet->len = SEREMU_TX_SIZE;
272 usb_tx(SEREMU_TX_ENDPOINT, tx_packet);
274 //serial_print("usb_flush_callback end\n");
277 #endif // SEREMU_INTERFACE
279 #endif // F_CPU >= 20 MHz