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
31 //#include "mk20dx128.h"
33 #include "usb_seremu.h"
34 #include "core_pins.h" // for yield()
35 //#include "HardwareSerial.h"
37 #ifdef SEREMU_INTERFACE // defined by usb_dev.h -> usb_desc.h
39 volatile uint8_t usb_seremu_transmit_flush_timer=0;
41 static usb_packet_t *rx_packet=NULL;
42 static usb_packet_t *tx_packet=NULL;
43 static volatile uint8_t tx_noautoflush=0;
45 #define TRANSMIT_FLUSH_TIMEOUT 5 /* in milliseconds */
48 // get the next character, or -1 if nothing received
49 int usb_seremu_getchar(void)
55 if (!usb_configuration) return -1;
56 if (!rx_packet) rx_packet = usb_rx(SEREMU_RX_ENDPOINT);
57 if (!rx_packet) return -1;
59 c = rx_packet->buf[i++];
61 if (i >= rx_packet->len) {
74 // peek at the next character, or -1 if nothing received
75 int usb_seremu_peekchar(void)
80 if (!usb_configuration) return -1;
81 if (!rx_packet) rx_packet = usb_rx(SEREMU_RX_ENDPOINT);
82 if (!rx_packet) return -1;
83 c = rx_packet->buf[rx_packet->index];
90 // number of bytes available in the receive buffer
91 int usb_seremu_available(void)
96 if (usb_configuration) rx_packet = usb_rx(SEREMU_RX_ENDPOINT);
97 if (!rx_packet) return 0;
100 i = rx_packet->index;
102 for (i = rx_packet->index; i < len; i++) {
103 if (rx_packet->buf[i] == 0) break;
114 // discard any buffered input
115 void usb_seremu_flush_input(void)
119 if (!usb_configuration) return;
125 rx = usb_rx(SEREMU_RX_ENDPOINT);
133 // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
134 #define TX_PACKET_LIMIT 6
136 // When the PC isn't listening, how long do we wait before discarding data? If this is
137 // too short, we risk losing data during the stalls that are common with ordinary desktop
138 // software. If it's too long, we stall the user's program when no software is running.
139 #define TX_TIMEOUT_MSEC 30
141 #if F_CPU == 96000000
142 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
143 #elif F_CPU == 48000000
144 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
145 #elif F_CPU == 24000000
146 #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
149 // When we've suffered the transmit timeout, don't wait again until the computer
150 // begins accepting data. If no software is running to receive, we'll just discard
151 // data as rapidly as Serial.print() can generate it, until there's something to
152 // actually receive it.
153 static uint8_t transmit_previous_timeout=0;
156 // transmit a character. 0 returned on success, -1 on error
157 int usb_seremu_putchar(uint8_t c)
159 return usb_seremu_write(&c, 1);
163 int usb_seremu_write(const void *buffer, uint32_t size)
168 const uint8_t *src = (const uint8_t *)buffer;
176 if (!usb_configuration) {
180 if (usb_tx_packet_count(SEREMU_TX_ENDPOINT) < TX_PACKET_LIMIT) {
182 tx_packet = usb_malloc();
183 if (tx_packet) break;
185 if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
186 transmit_previous_timeout = 1;
195 transmit_previous_timeout = 0;
196 len = SEREMU_TX_SIZE - tx_packet->index;
197 if (len > size) len = size;
198 dest = tx_packet->buf + tx_packet->index;
199 tx_packet->index += len;
201 while (len-- > 0) *dest++ = *src++;
202 if (tx_packet->index < SEREMU_TX_SIZE) {
203 usb_seremu_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
205 tx_packet->len = SEREMU_TX_SIZE;
206 usb_seremu_transmit_flush_timer = 0;
207 usb_tx(SEREMU_TX_ENDPOINT, tx_packet);
216 void usb_seremu_flush_output(void)
220 if (!usb_configuration) return;
221 //serial_print("usb_serial_flush_output\n");
222 if (tx_packet && tx_packet->index > 0) {
223 usb_seremu_transmit_flush_timer = 0;
224 for (i = tx_packet->index; i < SEREMU_TX_SIZE; i++) {
225 tx_packet->buf[i] = 0;
227 tx_packet->len = SEREMU_TX_SIZE;
228 usb_tx(SEREMU_TX_ENDPOINT, tx_packet);
231 // while (usb_tx_byte_count(SEREMU_TX_ENDPOINT) > 0) ; // wait
234 void usb_seremu_flush_callback(void)
238 if (tx_noautoflush) return;
239 //serial_print("usb_flush_callback \n");
240 for (i = tx_packet->index; i < SEREMU_TX_SIZE; i++) {
241 tx_packet->buf[i] = 0;
243 tx_packet->len = SEREMU_TX_SIZE;
244 usb_tx(SEREMU_TX_ENDPOINT, tx_packet);
246 //serial_print("usb_flush_callback end\n");
249 #endif // SEREMU_INTERFACE