USBSerial: reduce number of packets sent when puts() is invoked
[clinton/Smoothieware.git] / src / libs / USBDevice / USBSerial / USBSerial.cpp
1 /* Copyright (c) 2010-2011 mbed.org, MIT License
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4 * and associated documentation files (the "Software"), to deal in the Software without
5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
8 *
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17 */
18
19 #include <cstdint>
20 #include <cstdio>
21
22 #include "USBSerial.h"
23
24 #include "libs/Kernel.h"
25 #include "libs/SerialMessage.h"
26
27 // extern void setled(int, bool);
28 #define setled(a, b) do {} while (0)
29
30 #define iprintf(...) do { } while (0)
31
32 USBSerial::USBSerial(USB *u): USBCDC(u), rxbuf(128), txbuf(128)
33 {
34 usb = u;
35 nl_in_rx = 0;
36 }
37
38 int USBSerial::_putc(int c)
39 {
40 // send((uint8_t *)&c, 1);
41 if (c == '\r')
42 return 1;
43 if (txbuf.free())
44 txbuf.queue(c);
45
46 usb->endpointSetInterrupt(CDC_BulkIn.bEndpointAddress, true);
47 // usb->endpointTriggerInterrupt(CDC_BulkIn.bEndpointAddress);
48 return 1;
49 }
50
51 int USBSerial::_getc()
52 {
53 uint8_t c = 0;
54 setled(4, 1); while (rxbuf.isEmpty()); setled(4, 0);
55 rxbuf.dequeue(&c);
56 if (rxbuf.free() == MAX_PACKET_SIZE_EPBULK)
57 {
58 usb->endpointSetInterrupt(CDC_BulkOut.bEndpointAddress, true);
59 iprintf("rxbuf has room for another packet, interrupt enabled\n");
60 // usb->endpointTriggerInterrupt(CDC_BulkOut.bEndpointAddress);
61 }
62 if (nl_in_rx > 0)
63 if (c == '\n')
64 nl_in_rx--;
65
66 return c;
67 }
68
69 int USBSerial::puts(const char *str)
70 {
71 int i = 0;
72 while (*str)
73 {
74 if ((*str != '\r') && txbuf.free())
75 txbuf.queue(*str);
76 i++;
77 str++;
78 }
79 usb->endpointSetInterrupt(CDC_BulkIn.bEndpointAddress, true);
80 return i;
81 }
82
83 uint16_t USBSerial::writeBlock(uint8_t * buf, uint16_t size)
84 {
85 if (size > txbuf.free())
86 {
87 size = txbuf.free();
88 }
89 if (size > 0)
90 {
91 for (uint8_t i = 0; i < size; i++)
92 {
93 txbuf.queue(buf[i]);
94 }
95 usb->endpointSetInterrupt(CDC_BulkIn.bEndpointAddress, true);
96 }
97 return size;
98 }
99
100 bool USBSerial::USBEvent_EPIn(uint8_t bEP, uint8_t bEPStatus)
101 {
102 /*
103 * Called in ISR context
104 */
105
106 // static bool needToSendNull = false;
107
108 bool r = true;
109
110 if (bEP != CDC_BulkIn.bEndpointAddress)
111 return false;
112
113 iprintf("USBSerial:EpIn: 0x%02X\n", bEPStatus);
114
115 uint8_t b[MAX_PACKET_SIZE_EPBULK];
116
117 int l = txbuf.available();
118 iprintf("%d bytes queued\n", l);
119 if (l > 0)
120 {
121 if (l > MAX_PACKET_SIZE_EPBULK)
122 l = MAX_PACKET_SIZE_EPBULK;
123 iprintf("Sending %d bytes:\n\t", l);
124 int i;
125 for (i = 0; i < l; i++) {
126 txbuf.dequeue(&b[i]);
127 if (b[i] >= 32 && b[i] < 128)
128 iprintf("%c", b[i]);
129 else {
130 iprintf("\\x%02X", b[i]);
131 }
132 }
133 iprintf("\nSending...\n");
134 send(b, l);
135 iprintf("Sent\n");
136 // if (l == 64 && txbuf.available() == 0)
137 // needToSendNull = true;
138 if (txbuf.available() == 0)
139 r = false;
140 }
141 else
142 {
143 // if (needToSendNull)
144 // {
145 // send(NULL, 0);
146 // needToSendNull = false;
147 // }
148 // else
149 // {
150 r = false;
151 // }
152 // usb->endpointSetInterrupt(bEP, false);
153 }
154 iprintf("USBSerial:EpIn Complete\n");
155 return r;
156 }
157
158 bool USBSerial::USBEvent_EPOut(uint8_t bEP, uint8_t bEPStatus)
159 {
160 /*
161 * Called in ISR context
162 */
163
164 bool r = true;
165
166 iprintf("USBSerial:EpOut\n");
167 if (bEP != CDC_BulkOut.bEndpointAddress)
168 return false;
169
170 if (rxbuf.free() < MAX_PACKET_SIZE_EPBULK)
171 {
172 // usb->endpointSetInterrupt(bEP, false);
173 return false;
174 }
175
176 uint8_t c[MAX_PACKET_SIZE_EPBULK];
177 uint32_t size = 64;
178
179 //we read the packet received and put it on the circular buffer
180 readEP(c, &size);
181 iprintf("Read %ld bytes:\n\t", size);
182 for (uint8_t i = 0; i < size; i++) {
183 rxbuf.queue(c[i]);
184 if (c[i] >= 32 && c[i] < 128)
185 {
186 iprintf("%c", c[i]);
187 }
188 else
189 {
190 iprintf("\\x%02X", c[i]);
191 }
192 if (c[i] == '\n')
193 nl_in_rx++;
194 }
195 iprintf("\nQueued, %d empty\n", rxbuf.free());
196
197 if (rxbuf.free() < MAX_PACKET_SIZE_EPBULK)
198 {
199 // usb->endpointSetInterrupt(bEP, false);
200 r = false;
201 }
202
203 usb->readStart(CDC_BulkOut.bEndpointAddress, MAX_PACKET_SIZE_EPBULK);
204 iprintf("USBSerial:EpOut Complete\n");
205 return r;
206 }
207
208 /*
209 bool USBSerial::EpCallback(uint8_t bEP, uint8_t bEPStatus) {
210 if (bEP == CDC_BulkOut.bEndpointAddress) {
211 uint8_t c[65];
212 uint32_t size = 0;
213
214 //we read the packet received and put it on the circular buffer
215 readEP(c, &size);
216 for (uint8_t i = 0; i < size; i++) {
217 buf.queue(c[i]);
218 }
219
220 //call a potential handler
221 rx.call();
222
223 // We reactivate the endpoint to receive next characters
224 usb->readStart(CDC_BulkOut.bEndpointAddress, MAX_PACKET_SIZE_EPBULK);
225 return true;
226 }
227 return false;
228 }*/
229
230 uint8_t USBSerial::available()
231 {
232 return rxbuf.available();
233 }
234
235 void USBSerial::on_module_loaded()
236 {
237 this->register_for_event(ON_MAIN_LOOP);
238 // this->kernel->streams->append_stream(this);
239 }
240
241 void USBSerial::on_main_loop(void *argument)
242 {
243 // this->kernel->streams->printf("!");
244 if (nl_in_rx)
245 {
246 string received;
247 while (available())
248 {
249 char c = _getc();
250 if( c == '\n' )
251 {
252 struct SerialMessage message;
253 message.message = received;
254 message.stream = this;
255 iprintf("USBSerial Received: %s\n", message.message.c_str());
256 this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
257 return;
258 }
259 else
260 {
261 received += c;
262 }
263 }
264 }
265 }
266
267 void USBSerial::on_attach()
268 {
269 this->kernel->streams->append_stream(this);
270 writeBlock((uint8_t *) "Smoothie\nok\n", 12);
271 }
272
273 void USBSerial::on_detach()
274 {
275 this->kernel->streams->remove_stream(this);
276 txbuf.flush();
277 rxbuf.flush();
278 nl_in_rx = 0;
279 }