]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Output/pjrcUSB/arm/usb_serial.c
f9ec66fec6ef8bbcc83e0dba7abda2e90c5d5df7
[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  * Modified by Jacob Alexander 2013-2014
5  *
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:
13  *
14  * 1. The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
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.
21  *
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
29  * SOFTWARE.
30  */
31
32 #include "usb_dev.h"
33 #include "usb_serial.h"
34 #include <Lib/OutputLib.h>
35 #include <string.h> // For memcpy
36
37 // defined by usb_dev.h -> usb_desc.h
38 #if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
39
40 uint32_t usb_cdc_line_coding[2];
41 volatile uint8_t usb_cdc_line_rtsdtr=0;
42 volatile uint8_t usb_cdc_transmit_flush_timer=0;
43
44 static usb_packet_t *rx_packet=NULL;
45 static usb_packet_t *tx_packet=NULL;
46 static volatile uint8_t tx_noautoflush=0;
47
48 #define TRANSMIT_FLUSH_TIMEOUT  5   /* in milliseconds */
49
50 // get the next character, or -1 if nothing received
51 int usb_serial_getchar(void)
52 {
53         unsigned int i;
54         int c;
55
56         if (!rx_packet) {
57                 if (!usb_configuration) return -1;
58                 rx_packet = usb_rx(CDC_RX_ENDPOINT);
59                 if (!rx_packet) return -1;
60         }
61         i = rx_packet->index;
62         c = rx_packet->buf[i++];
63         if (i >= rx_packet->len) {
64                 usb_free(rx_packet);
65                 rx_packet = NULL;
66         } else {
67                 rx_packet->index = i;
68         }
69         return c;
70 }
71
72 // peek at the next character, or -1 if nothing received
73 int usb_serial_peekchar(void)
74 {
75         if (!rx_packet) {
76                 if (!usb_configuration) return -1;
77                 rx_packet = usb_rx(CDC_RX_ENDPOINT);
78                 if (!rx_packet) return -1;
79         }
80         if (!rx_packet) return -1;
81         return rx_packet->buf[rx_packet->index];
82 }
83
84 // number of bytes available in the receive buffer
85 int usb_serial_available(void)
86 {
87         int count;
88         count = usb_rx_byte_count(CDC_RX_ENDPOINT);
89         if (rx_packet) count += rx_packet->len - rx_packet->index;
90         return count;
91 }
92
93 // read a block of bytes to a buffer
94 int usb_serial_read(void *buffer, uint32_t size)
95 {
96         uint8_t *p = (uint8_t *)buffer;
97         uint32_t qty, count=0;
98
99         while (size) {
100                 if (!usb_configuration) break;
101                 if (!rx_packet) {
102                         rx:
103                         rx_packet = usb_rx(CDC_RX_ENDPOINT);
104                         if (!rx_packet) break;
105                         if (rx_packet->len == 0) {
106                                 usb_free(rx_packet);
107                                 goto rx;
108                         }
109                 }
110                 qty = rx_packet->len - rx_packet->index;
111                 if (qty > size) qty = size;
112                 memcpy(p, rx_packet->buf + rx_packet->index, qty);
113                 p += qty;
114                 count += qty;
115                 size -= qty;
116                 rx_packet->index += qty;
117                 if (rx_packet->index >= rx_packet->len) {
118                         usb_free(rx_packet);
119                         rx_packet = NULL;
120                 }
121         }
122         return count;
123 }
124
125 // discard any buffered input
126 void usb_serial_flush_input(void)
127 {
128         usb_packet_t *rx;
129
130         if (!usb_configuration) return;
131         if (rx_packet) {
132                 usb_free(rx_packet);
133                 rx_packet = NULL;
134         }
135         while (1) {
136                 rx = usb_rx(CDC_RX_ENDPOINT);
137                 if (!rx) break;
138                 usb_free(rx);
139         }
140 }
141
142 // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
143 #define TX_PACKET_LIMIT 8
144
145 // When the PC isn't listening, how long do we wait before discarding data?  If this is
146 // too short, we risk losing data during the stalls that are common with ordinary desktop
147 // software.  If it's too long, we stall the user's program when no software is running.
148 #define TX_TIMEOUT_MSEC 70
149
150 #if F_CPU == 96000000
151   #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
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)
156 #endif
157
158 // When we've suffered the transmit timeout, don't wait again until the computer
159 // begins accepting data.  If no software is running to receive, we'll just discard
160 // data as rapidly as Serial.print() can generate it, until there's something to
161 // actually receive it.
162 static uint8_t transmit_previous_timeout=0;
163
164
165 // transmit a character.  0 returned on success, -1 on error
166 int usb_serial_putchar(uint8_t c)
167 {
168         return usb_serial_write(&c, 1);
169 }
170
171
172 int usb_serial_write(const void *buffer, uint32_t size)
173 {
174         uint32_t len;
175         uint32_t wait_count;
176         const uint8_t *src = (const uint8_t *)buffer;
177         uint8_t *dest;
178
179         tx_noautoflush = 1;
180         while (size > 0) {
181                 if (!tx_packet) {
182                         wait_count = 0;
183                         while (1) {
184                                 if (!usb_configuration) {
185                                         tx_noautoflush = 0;
186                                         return -1;
187                                 }
188                                 if (usb_tx_packet_count(CDC_TX_ENDPOINT) < TX_PACKET_LIMIT) {
189                                         tx_noautoflush = 1;
190                                         tx_packet = usb_malloc();
191                                         if (tx_packet) break;
192                                         tx_noautoflush = 0;
193                                 }
194                                 if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
195                                         transmit_previous_timeout = 1;
196                                         return -1;
197                                 }
198                                 yield();
199                         }
200                 }
201                 transmit_previous_timeout = 0;
202                 len = CDC_TX_SIZE - tx_packet->index;
203                 if (len > size) len = size;
204                 dest = tx_packet->buf + tx_packet->index;
205                 tx_packet->index += len;
206                 size -= len;
207                 while (len-- > 0) *dest++ = *src++;
208                 if (tx_packet->index >= CDC_TX_SIZE) {
209                         tx_packet->len = CDC_TX_SIZE;
210                         usb_tx(CDC_TX_ENDPOINT, tx_packet);
211                         tx_packet = NULL;
212                 }
213                 usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
214         }
215         tx_noautoflush = 0;
216         return 0;
217 }
218
219 void usb_serial_flush_output(void)
220 {
221         if (!usb_configuration) return;
222         tx_noautoflush = 1;
223         if (tx_packet) {
224                 usb_cdc_transmit_flush_timer = 0;
225                 tx_packet->len = tx_packet->index;
226                 usb_tx(CDC_TX_ENDPOINT, tx_packet);
227                 tx_packet = NULL;
228         } else {
229                 usb_packet_t *tx = usb_malloc();
230                 if (tx) {
231                         usb_cdc_transmit_flush_timer = 0;
232                         usb_tx(CDC_TX_ENDPOINT, tx);
233                 } else {
234                         usb_cdc_transmit_flush_timer = 1;
235                 }
236         }
237         tx_noautoflush = 0;
238 }
239
240 void usb_serial_flush_callback(void)
241 {
242         if (tx_noautoflush) return;
243         if (tx_packet) {
244                 tx_packet->len = tx_packet->index;
245                 usb_tx(CDC_TX_ENDPOINT, tx_packet);
246                 tx_packet = NULL;
247         } else {
248                 usb_packet_t *tx = usb_malloc();
249                 if (tx) {
250                         usb_tx(CDC_TX_ENDPOINT, tx);
251                 } else {
252                         usb_cdc_transmit_flush_timer = 1;
253                 }
254         }
255 }
256
257 #endif // CDC_STATUS_INTERFACE && CDC_DATA_INTERFACE
258