USBSerial: don't send \r, prevent double spaced lines on serial terminals
[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(...)
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 _putc(*str);
75 i++;
76 str++;
77 }
78 return i;
79 }
80
81 uint16_t USBSerial::writeBlock(uint8_t * buf, uint16_t size)
82 {
83 if (size > txbuf.free())
84 {
85 size = txbuf.free();
86 }
87 if (size > 0)
88 {
89 for (uint8_t i = 0; i < size; i++)
90 {
91 txbuf.queue(buf[i]);
92 }
93 usb->endpointSetInterrupt(CDC_BulkIn.bEndpointAddress, true);
94 }
95 return size;
96 }
97
98 bool USBSerial::USBEvent_EPIn(uint8_t bEP, uint8_t bEPStatus)
99 {
100 /*
101 * Called in ISR context
102 */
103
104 // static bool needToSendNull = false;
105
106 bool r = true;
107
108 if (bEP != CDC_BulkIn.bEndpointAddress)
109 return false;
110
111 iprintf("USBSerial:EpIn: 0x%02X\n", bEPStatus);
112
113 uint8_t b[MAX_PACKET_SIZE_EPBULK];
114
115 int l = txbuf.available();
116 iprintf("%d bytes queued\n", l);
117 if (l > 0)
118 {
119 if (l > MAX_PACKET_SIZE_EPBULK)
120 l = MAX_PACKET_SIZE_EPBULK;
121 iprintf("Sending %d bytes:\n\t", l);
122 int i;
123 for (i = 0; i < l; i++) {
124 txbuf.dequeue(&b[i]);
125 if (b[i] >= 32 && b[i] < 128)
126 iprintf("%c", b[i]);
127 else {
128 iprintf("\\x%02X", b[i]);
129 }
130 }
131 iprintf("\nSending...\n");
132 send(b, l);
133 iprintf("Sent\n");
134 // if (l == 64 && txbuf.available() == 0)
135 // needToSendNull = true;
136 if (txbuf.available() == 0)
137 r = false;
138 }
139 else
140 {
141 // if (needToSendNull)
142 // {
143 // send(NULL, 0);
144 // needToSendNull = false;
145 // }
146 // else
147 // {
148 r = false;
149 // }
150 // usb->endpointSetInterrupt(bEP, false);
151 }
152 iprintf("USBSerial:EpIn Complete\n");
153 return r;
154 }
155
156 bool USBSerial::USBEvent_EPOut(uint8_t bEP, uint8_t bEPStatus)
157 {
158 /*
159 * Called in ISR context
160 */
161
162 bool r = true;
163
164 iprintf("USBSerial:EpOut\n");
165 if (bEP != CDC_BulkOut.bEndpointAddress)
166 return false;
167
168 if (rxbuf.free() < MAX_PACKET_SIZE_EPBULK)
169 {
170 // usb->endpointSetInterrupt(bEP, false);
171 return false;
172 }
173
174 uint8_t c[MAX_PACKET_SIZE_EPBULK];
175 uint32_t size = 64;
176
177 //we read the packet received and put it on the circular buffer
178 readEP(c, &size);
179 iprintf("Read %ld bytes:\n\t", size);
180 for (uint8_t i = 0; i < size; i++) {
181 rxbuf.queue(c[i]);
182 if (c[i] >= 32 && c[i] < 128)
183 {
184 iprintf("%c", c[i]);
185 }
186 else
187 {
188 iprintf("\\x%02X", c[i]);
189 }
190 if (c[i] == '\n')
191 nl_in_rx++;
192 }
193 iprintf("\nQueued, %d empty\n", rxbuf.free());
194
195 if (rxbuf.free() < MAX_PACKET_SIZE_EPBULK)
196 {
197 // usb->endpointSetInterrupt(bEP, false);
198 r = false;
199 }
200
201 usb->readStart(CDC_BulkOut.bEndpointAddress, MAX_PACKET_SIZE_EPBULK);
202 iprintf("USBSerial:EpOut Complete\n");
203 return r;
204 }
205
206 /*
207 bool USBSerial::EpCallback(uint8_t bEP, uint8_t bEPStatus) {
208 if (bEP == CDC_BulkOut.bEndpointAddress) {
209 uint8_t c[65];
210 uint32_t size = 0;
211
212 //we read the packet received and put it on the circular buffer
213 readEP(c, &size);
214 for (uint8_t i = 0; i < size; i++) {
215 buf.queue(c[i]);
216 }
217
218 //call a potential handler
219 rx.call();
220
221 // We reactivate the endpoint to receive next characters
222 usb->readStart(CDC_BulkOut.bEndpointAddress, MAX_PACKET_SIZE_EPBULK);
223 return true;
224 }
225 return false;
226 }*/
227
228 uint8_t USBSerial::available()
229 {
230 return rxbuf.available();
231 }
232
233 void USBSerial::on_module_loaded()
234 {
235 this->register_for_event(ON_MAIN_LOOP);
236 // this->kernel->streams->append_stream(this);
237 }
238
239 void USBSerial::on_main_loop(void *argument)
240 {
241 // this->kernel->streams->printf("!");
242 if (nl_in_rx)
243 {
244 string received;
245 while (available())
246 {
247 char c = _getc();
248 if( c == '\n' )
249 {
250 struct SerialMessage message;
251 message.message = received;
252 message.stream = this;
253 iprintf("USBSerial Received: %s\n", message.message.c_str());
254 this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message );
255 return;
256 }
257 else
258 {
259 received += c;
260 }
261 }
262 }
263 }
264
265 void USBSerial::on_attach()
266 {
267 this->kernel->streams->append_stream(this);
268 writeBlock((uint8_t *) "Smoothie\nok\n", 12);
269 }
270
271 void USBSerial::on_detach()
272 {
273 this->kernel->streams->remove_stream(this);
274 txbuf.flush();
275 rxbuf.flush();
276 nl_in_rx = 0;
277 }