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