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