]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Output/pjrcUSB/arm/usb_serial.c
Adding in USB module updates from Teensyduino 1.18rc2.
[kiibohd-controller.git] / Output / pjrcUSB / arm / usb_serial.c
1 /* Teensyduino Core Library
2  * http://www.pjrc.com/teensy/
3  * Copyright (c) 2013 PJRC.COM, LLC.
4  *
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:
12  *
13  * 1. The above copyright notice and this permission notice shall be 
14  * included in all copies or substantial portions of the Software.
15  *
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.
20  *
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
28  * SOFTWARE.
29  */
30
31 #include "usb_dev.h"
32 #include "usb_serial.h"
33 #include <Lib/USBLib.h>
34 #include <string.h> // For memcpy
35
36 // defined by usb_dev.h -> usb_desc.h
37 #if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
38
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;
42
43 static usb_packet_t *rx_packet=NULL;
44 static usb_packet_t *tx_packet=NULL;
45 static volatile uint8_t tx_noautoflush=0;
46
47 #define TRANSMIT_FLUSH_TIMEOUT  5   /* in milliseconds */
48
49 // get the next character, or -1 if nothing received
50 int usb_serial_getchar(void)
51 {
52         unsigned int i;
53         int c;
54
55         if (!rx_packet) {
56                 if (!usb_configuration) return -1;
57                 rx_packet = usb_rx(CDC_RX_ENDPOINT);
58                 if (!rx_packet) return -1;
59         }
60         i = rx_packet->index;
61         c = rx_packet->buf[i++];
62         if (i >= rx_packet->len) {
63                 usb_free(rx_packet);
64                 rx_packet = NULL;
65         } else {
66                 rx_packet->index = i;
67         }
68         return c;
69 }
70
71 // peek at the next character, or -1 if nothing received
72 int usb_serial_peekchar(void)
73 {
74         if (!rx_packet) {
75                 if (!usb_configuration) return -1;
76                 rx_packet = usb_rx(CDC_RX_ENDPOINT);
77                 if (!rx_packet) return -1;
78         }
79         if (!rx_packet) return -1;
80         return rx_packet->buf[rx_packet->index];
81 }
82
83 // number of bytes available in the receive buffer
84 int usb_serial_available(void)
85 {
86         int count;
87         count = usb_rx_byte_count(CDC_RX_ENDPOINT);
88         if (rx_packet) count += rx_packet->len - rx_packet->index;
89         return count;
90 }
91
92 // read a block of bytes to a buffer
93 int usb_serial_read(void *buffer, uint32_t size)
94 {
95         uint8_t *p = (uint8_t *)buffer;
96         uint32_t qty, count=0;
97
98         while (size) {
99                 if (!usb_configuration) break;
100                 if (!rx_packet) {
101                         rx:
102                         rx_packet = usb_rx(CDC_RX_ENDPOINT);
103                         if (!rx_packet) break;
104                         if (rx_packet->len == 0) {
105                                 usb_free(rx_packet);
106                                 goto rx;
107                         }
108                 }
109                 qty = rx_packet->len - rx_packet->index;
110                 if (qty > size) qty = size;
111                 memcpy(p, rx_packet->buf + rx_packet->index, qty);
112                 p += qty;
113                 count += qty;
114                 size -= qty;
115                 rx_packet->index += qty;
116                 if (rx_packet->index >= rx_packet->len) {
117                         usb_free(rx_packet);
118                         rx_packet = NULL;
119                 }
120         }
121         return count;
122 }
123
124 // discard any buffered input
125 void usb_serial_flush_input(void)
126 {
127         usb_packet_t *rx;
128
129         if (!usb_configuration) return;
130         if (rx_packet) {
131                 usb_free(rx_packet);
132                 rx_packet = NULL;
133         }
134         while (1) {
135                 rx = usb_rx(CDC_RX_ENDPOINT);
136                 if (!rx) break;
137                 usb_free(rx);
138         }
139 }
140
141 // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
142 #define TX_PACKET_LIMIT 8
143
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
148
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)
155 #endif
156
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;
162
163
164 // transmit a character.  0 returned on success, -1 on error
165 int usb_serial_putchar(uint8_t c)
166 {
167         return usb_serial_write(&c, 1);
168 }
169
170
171 int usb_serial_write(const void *buffer, uint32_t size)
172 {
173         uint32_t len;
174         uint32_t wait_count;
175         const uint8_t *src = (const uint8_t *)buffer;
176         uint8_t *dest;
177
178         tx_noautoflush = 1;
179         while (size > 0) {
180                 if (!tx_packet) {
181                         wait_count = 0;
182                         while (1) {
183                                 if (!usb_configuration) {
184                                         tx_noautoflush = 0;
185                                         return -1;
186                                 }
187                                 if (usb_tx_packet_count(CDC_TX_ENDPOINT) < TX_PACKET_LIMIT) {
188                                         tx_noautoflush = 1;
189                                         tx_packet = usb_malloc();
190                                         if (tx_packet) break;
191                                         tx_noautoflush = 0;
192                                 }
193                                 if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
194                                         transmit_previous_timeout = 1;
195                                         return -1;
196                                 }
197                                 yield();
198                         }
199                 }
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;
205                 size -= 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);
210                         tx_packet = NULL;
211                 }
212                 usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
213         }
214         tx_noautoflush = 0;
215         return 0;
216 }
217
218 void usb_serial_flush_output(void)
219 {
220         if (!usb_configuration) return;
221         tx_noautoflush = 1;
222         if (tx_packet) {
223                 usb_cdc_transmit_flush_timer = 0;
224                 tx_packet->len = tx_packet->index;
225                 usb_tx(CDC_TX_ENDPOINT, tx_packet);
226                 tx_packet = NULL;
227         } else {
228                 usb_packet_t *tx = usb_malloc();
229                 if (tx) {
230                         usb_cdc_transmit_flush_timer = 0;
231                         usb_tx(CDC_TX_ENDPOINT, tx);
232                 } else {
233                         usb_cdc_transmit_flush_timer = 1;
234                 }
235         }
236         tx_noautoflush = 0;
237 }
238
239 void usb_serial_flush_callback(void)
240 {
241         if (tx_noautoflush) return;
242         if (tx_packet) {
243                 tx_packet->len = tx_packet->index;
244                 usb_tx(CDC_TX_ENDPOINT, tx_packet);
245                 tx_packet = NULL;
246         } else {
247                 usb_packet_t *tx = usb_malloc();
248                 if (tx) {
249                         usb_tx(CDC_TX_ENDPOINT, tx);
250                 } else {
251                         usb_cdc_transmit_flush_timer = 1;
252                 }
253         }
254 }
255
256 #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE
257