Add 'teensytp/' from commit 'be48e888050f18a31e788269c8f47358036a8e3b'
[megadrive.git] / teensytp / teensy3 / usb_seremu.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 #if F_CPU >= 20000000
32
33 #include "usb_dev.h"
34 #include "usb_seremu.h"
35 #include "core_pins.h" // for yield()
36 //#include "HardwareSerial.h"
37
38 #ifdef SEREMU_INTERFACE // defined by usb_dev.h -> usb_desc.h
39
40 volatile uint8_t usb_seremu_transmit_flush_timer=0;
41
42 static usb_packet_t *rx_packet=NULL;
43 static usb_packet_t *tx_packet=NULL;
44 static volatile uint8_t tx_noautoflush=0;
45
46 #define TRANSMIT_FLUSH_TIMEOUT  5   /* in milliseconds */
47
48
49 // get the next character, or -1 if nothing received
50 int usb_seremu_getchar(void)
51 {
52         unsigned int i;
53         int c;
54
55         while (1) {
56                 if (!usb_configuration) return -1;
57                 if (!rx_packet) rx_packet = usb_rx(SEREMU_RX_ENDPOINT);
58                 if (!rx_packet) return -1;
59                 i = rx_packet->index;
60                 c = rx_packet->buf[i++];
61                 if (c) {
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                 usb_free(rx_packet);
71                 rx_packet = NULL;
72         }
73 }
74
75 // peek at the next character, or -1 if nothing received
76 int usb_seremu_peekchar(void)
77 {
78         int c;
79
80         while (1) {
81                 if (!usb_configuration) return -1;
82                 if (!rx_packet) rx_packet = usb_rx(SEREMU_RX_ENDPOINT);
83                 if (!rx_packet) return -1;
84                 c = rx_packet->buf[rx_packet->index];
85                 if (c) return c;
86                 usb_free(rx_packet);
87                 rx_packet = NULL;
88         }
89 }
90
91 // number of bytes available in the receive buffer
92 int usb_seremu_available(void)
93 {
94         int i, len, count;
95
96         if (!rx_packet) {
97                 if (usb_configuration) rx_packet = usb_rx(SEREMU_RX_ENDPOINT);
98                 if (!rx_packet) return 0;
99         }
100         len = rx_packet->len;
101         i = rx_packet->index;
102         count = 0;
103         for (i = rx_packet->index; i < len; i++) {
104                 if (rx_packet->buf[i] == 0) break;
105                 count++;
106         }
107         if (count == 0) {
108                 usb_free(rx_packet);
109                 rx_packet = NULL;
110         }
111         return count;
112 }
113
114
115 // discard any buffered input
116 void usb_seremu_flush_input(void)
117 {
118         usb_packet_t *rx;
119
120         if (!usb_configuration) return;
121         if (rx_packet) {
122                 usb_free(rx_packet);
123                 rx_packet = NULL;
124         }
125         while (1) {
126                 rx = usb_rx(SEREMU_RX_ENDPOINT);
127                 if (!rx) break;
128                 usb_free(rx);
129         }
130 }
131
132
133
134 // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
135 #define TX_PACKET_LIMIT 6
136
137 // When the PC isn't listening, how long do we wait before discarding data?  If this is
138 // too short, we risk losing data during the stalls that are common with ordinary desktop
139 // software.  If it's too long, we stall the user's program when no software is running.
140 #define TX_TIMEOUT_MSEC 30
141
142 #if F_CPU == 168000000
143   #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
144 #elif F_CPU == 144000000
145   #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
146 #elif F_CPU == 120000000
147   #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
148 #elif F_CPU == 96000000
149   #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
150 #elif F_CPU == 72000000
151   #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
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
159 // When we've suffered the transmit timeout, don't wait again until the computer
160 // begins accepting data.  If no software is running to receive, we'll just discard
161 // data as rapidly as Serial.print() can generate it, until there's something to
162 // actually receive it.
163 static uint8_t transmit_previous_timeout=0;
164
165
166 // transmit a character.  0 returned on success, -1 on error
167 int usb_seremu_putchar(uint8_t c)
168 {
169         return usb_seremu_write(&c, 1);
170 }
171
172
173 int usb_seremu_write(const void *buffer, uint32_t size)
174 {
175 #if 1
176         uint32_t len;
177         uint32_t wait_count;
178         const uint8_t *src = (const uint8_t *)buffer;
179         uint8_t *dest;
180
181         tx_noautoflush = 1;
182         while (size > 0) {
183                 if (!tx_packet) {
184                         wait_count = 0;
185                         while (1) {
186                                 if (!usb_configuration) {
187                                         tx_noautoflush = 0;
188                                         return -1;
189                                 }
190                                 if (usb_tx_packet_count(SEREMU_TX_ENDPOINT) < TX_PACKET_LIMIT) {
191                                         tx_noautoflush = 1;
192                                         tx_packet = usb_malloc();
193                                         if (tx_packet) break;
194                                 }
195                                 if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
196                                         transmit_previous_timeout = 1;
197                                         tx_noautoflush = 0;
198                                         return -1;
199                                 }
200                                 tx_noautoflush = 0;
201                                 yield();
202                                 tx_noautoflush = 1;
203                         }
204                 }
205                 transmit_previous_timeout = 0;
206                 len = SEREMU_TX_SIZE - tx_packet->index;
207                 if (len > size) len = size;
208                 dest = tx_packet->buf + tx_packet->index;
209                 tx_packet->index += len;
210                 size -= len;
211                 while (len-- > 0) *dest++ = *src++;
212                 if (tx_packet->index < SEREMU_TX_SIZE) {
213                         usb_seremu_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
214                 } else {
215                         tx_packet->len = SEREMU_TX_SIZE;
216                         usb_seremu_transmit_flush_timer = 0;
217                         usb_tx(SEREMU_TX_ENDPOINT, tx_packet);
218                         tx_packet = NULL;
219                 }
220         }
221         tx_noautoflush = 0;
222         return 0;
223 #endif
224 }
225
226 int usb_seremu_write_buffer_free(void)
227 {
228         uint32_t len;
229
230         tx_noautoflush = 1;
231         if (!tx_packet) {
232                 if (!usb_configuration ||
233                   usb_tx_packet_count(SEREMU_TX_ENDPOINT) >= TX_PACKET_LIMIT ||
234                   (tx_packet = usb_malloc()) == NULL) {
235                         tx_noautoflush = 0;
236                         return 0;
237                 }
238         }
239         len = SEREMU_TX_SIZE - tx_packet->index;
240         tx_noautoflush = 0;
241         return len;
242 }
243
244 void usb_seremu_flush_output(void)
245 {
246         int i;
247
248         if (!usb_configuration) return;
249         //serial_print("usb_serial_flush_output\n");
250         if (tx_packet && tx_packet->index > 0) {
251                 usb_seremu_transmit_flush_timer = 0;
252                 for (i = tx_packet->index; i < SEREMU_TX_SIZE; i++) {
253                         tx_packet->buf[i] = 0;
254                 }
255                 tx_packet->len = SEREMU_TX_SIZE;
256                 usb_tx(SEREMU_TX_ENDPOINT, tx_packet);
257                 tx_packet = NULL;
258         }
259         // while (usb_tx_byte_count(SEREMU_TX_ENDPOINT) > 0) ; // wait
260 }
261
262 void usb_seremu_flush_callback(void)
263 {
264         int i;
265         //serial_print("C");
266         if (tx_noautoflush) return;
267         //serial_print("usb_flush_callback \n");
268         for (i = tx_packet->index; i < SEREMU_TX_SIZE; i++) {
269                 tx_packet->buf[i] = 0;
270         }
271         tx_packet->len = SEREMU_TX_SIZE;
272         usb_tx(SEREMU_TX_ENDPOINT, tx_packet);
273         tx_packet = NULL;
274         //serial_print("usb_flush_callback end\n");
275 }
276
277 #endif // SEREMU_INTERFACE
278
279 #endif // F_CPU >= 20 MHz