]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Output/pjrcUSB/arm/usb_serial.c
28d5de75d913d341315f1ad4392cf2409018074f
[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 // ----- Includes -----
33
34 // Compiler Includes
35 #include <string.h> // For memcpy
36
37 // Project Includes
38 #include <Lib/OutputLib.h>
39
40 // Local Includes
41 #include "usb_dev.h"
42 #include "usb_serial.h"
43
44
45
46 // ----- Defines -----
47
48 #define TRANSMIT_FLUSH_TIMEOUT  5   /* in milliseconds */
49
50 // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
51 #define TX_PACKET_LIMIT 8
52
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
57
58 #if F_CPU == 96000000
59   #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
60 #elif F_CPU == 48000000
61   #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
62 #elif F_CPU == 24000000
63   #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
64 #endif
65
66
67
68 // ----- Variables -----
69
70 // serial port settings (baud rate, control signals, etc) set
71 // by the PC.  These are ignored, but kept in RAM.
72 volatile uint8_t usb_cdc_line_coding[7] = { 0x00, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x08 };
73 volatile uint8_t usb_cdc_line_rtsdtr = 0;
74 volatile uint8_t usb_cdc_transmit_flush_timer = 0;
75
76 static usb_packet_t *rx_packet = NULL;
77 static usb_packet_t *tx_packet = NULL;
78 static volatile uint8_t tx_noautoflush = 0;
79
80 // When we've suffered the transmit timeout, don't wait again until the computer
81 // begins accepting data.  If no software is running to receive, we'll just discard
82 // data as rapidly as Serial.print() can generate it, until there's something to
83 // actually receive it.
84 static uint8_t transmit_previous_timeout = 0;
85
86
87
88 // ----- Functions -----
89
90 // get the next character, or -1 if nothing received
91 int usb_serial_getchar()
92 {
93         unsigned int i;
94         int c;
95
96         if ( !rx_packet )
97         {
98                 if ( !usb_configuration )
99                         return -1;
100                 rx_packet = usb_rx( CDC_RX_ENDPOINT );
101                 if ( !rx_packet )
102                         return -1;
103         }
104         i = rx_packet->index;
105         c = rx_packet->buf[i++];
106         if ( i >= rx_packet->len )
107         {
108                 usb_free( rx_packet );
109                 rx_packet = NULL;
110         }
111         else
112         {
113                 rx_packet->index = i;
114         }
115         return c;
116 }
117
118 // peek at the next character, or -1 if nothing received
119 int usb_serial_peekchar()
120 {
121         if ( !rx_packet )
122         {
123                 if ( !usb_configuration )
124                         return -1;
125                 rx_packet = usb_rx( CDC_RX_ENDPOINT );
126                 if ( !rx_packet )
127                         return -1;
128         }
129         if ( !rx_packet )
130                 return -1;
131         return rx_packet->buf[ rx_packet->index ];
132 }
133
134 // number of bytes available in the receive buffer
135 int usb_serial_available()
136 {
137         int count = usb_rx_byte_count( CDC_RX_ENDPOINT );
138         if ( rx_packet )
139                 count += rx_packet->len - rx_packet->index;
140         return count;
141 }
142
143 // read a block of bytes to a buffer
144 int usb_serial_read( void *buffer, uint32_t size )
145 {
146         uint8_t *p = (uint8_t *)buffer;
147         uint32_t qty, count=0;
148
149         while ( size )
150         {
151                 if ( !usb_configuration )
152                         break;
153                 if ( !rx_packet )
154                 {
155                         rx:
156                                 rx_packet = usb_rx(CDC_RX_ENDPOINT);
157                                 if ( !rx_packet )
158                                         break;
159                                 if ( rx_packet->len == 0 )
160                                 {
161                                         usb_free(rx_packet);
162                                         goto rx;
163                                 }
164                 }
165                 qty = rx_packet->len - rx_packet->index;
166                 if ( qty > size )
167                         qty = size;
168                 memcpy( p, rx_packet->buf + rx_packet->index, qty );
169                 p += qty;
170                 count += qty;
171                 size -= qty;
172                 rx_packet->index += qty;
173                 if ( rx_packet->index >= rx_packet->len )
174                 {
175                         usb_free( rx_packet );
176                         rx_packet = NULL;
177                 }
178         }
179         return count;
180 }
181
182 // discard any buffered input
183 void usb_serial_flush_input()
184 {
185         usb_packet_t *rx;
186
187         if ( !usb_configuration )
188                 return;
189         if ( rx_packet )
190         {
191                 usb_free( rx_packet );
192                 rx_packet = NULL;
193         }
194         while (1)
195         {
196                 rx = usb_rx( CDC_RX_ENDPOINT );
197                 if ( !rx )
198                         break;
199                 usb_free( rx );
200         }
201 }
202
203 // transmit a character.  0 returned on success, -1 on error
204 int usb_serial_putchar( uint8_t c )
205 {
206         return usb_serial_write( &c, 1 );
207 }
208
209 int usb_serial_write( const void *buffer, uint32_t size )
210 {
211         uint32_t len;
212         uint32_t wait_count;
213         const uint8_t *src = (const uint8_t *)buffer;
214         uint8_t *dest;
215
216         tx_noautoflush = 1;
217         while ( size > 0 )
218         {
219                 if ( !tx_packet )
220                 {
221                         wait_count = 0;
222                         while ( 1 )
223                         {
224                                 if ( !usb_configuration )
225                                 {
226                                         tx_noautoflush = 0;
227                                         return -1;
228                                 }
229                                 if ( usb_tx_packet_count( CDC_TX_ENDPOINT ) < TX_PACKET_LIMIT )
230                                 {
231                                         tx_noautoflush = 1;
232                                         tx_packet = usb_malloc();
233                                         if ( tx_packet )
234                                                 break;
235                                         tx_noautoflush = 0;
236                                 }
237                                 if ( ++wait_count > TX_TIMEOUT || transmit_previous_timeout )
238                                 {
239                                         transmit_previous_timeout = 1;
240                                         return -1;
241                                 }
242                                 yield();
243                         }
244                 }
245                 transmit_previous_timeout = 0;
246                 len = CDC_TX_SIZE - tx_packet->index;
247                 if ( len > size )
248                         len = size;
249                 dest = tx_packet->buf + tx_packet->index;
250                 tx_packet->index += len;
251                 size -= len;
252                 while ( len-- > 0 )
253                         *dest++ = *src++;
254                 if ( tx_packet->index >= CDC_TX_SIZE )
255                 {
256                         tx_packet->len = CDC_TX_SIZE;
257                         usb_tx( CDC_TX_ENDPOINT, tx_packet );
258                         tx_packet = NULL;
259                 }
260                 usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
261         }
262         tx_noautoflush = 0;
263         return 0;
264 }
265
266 void usb_serial_flush_output()
267 {
268         if ( !usb_configuration )
269                 return;
270         tx_noautoflush = 1;
271         if ( tx_packet )
272         {
273                 usb_cdc_transmit_flush_timer = 0;
274                 tx_packet->len = tx_packet->index;
275                 usb_tx( CDC_TX_ENDPOINT, tx_packet );
276                 tx_packet = NULL;
277         }
278         else
279         {
280                 usb_packet_t *tx = usb_malloc();
281                 if ( tx )
282                 {
283                         usb_cdc_transmit_flush_timer = 0;
284                         usb_tx( CDC_TX_ENDPOINT, tx );
285                 }
286                 else
287                 {
288                         usb_cdc_transmit_flush_timer = 1;
289                 }
290         }
291         tx_noautoflush = 0;
292 }
293
294 void usb_serial_flush_callback()
295 {
296         if ( tx_noautoflush )
297                 return;
298         if ( tx_packet )
299         {
300                 tx_packet->len = tx_packet->index;
301                 usb_tx( CDC_TX_ENDPOINT, tx_packet );
302                 tx_packet = NULL;
303         } else {
304                 usb_packet_t *tx = usb_malloc();
305                 if ( tx )
306                 {
307                         usb_tx( CDC_TX_ENDPOINT, tx );
308                 }
309                 else
310                 {
311                         usb_cdc_transmit_flush_timer = 1;
312                 }
313         }
314 }
315