1 /* Copyright (c) 2010-2011 mbed.org, MIT License
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:
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
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.
22 #include "USBSerial.h"
24 #include "libs/Kernel.h"
25 #include "libs/SerialMessage.h"
26 #include "StreamOutputPool.h"
28 // extern void setled(int, bool);
29 #define setled(a, b) do {} while (0)
31 #define iprintf(...) do { } while (0)
33 USBSerial::USBSerial(USB
*u
): USBCDC(u
), rxbuf(256 + 8), txbuf(128 + 8)
37 attach
= attached
= false;
41 last_char_was_dollar
= false;
44 void USBSerial::ensure_tx_space(int space
)
46 while (txbuf
.free() < space
) {
47 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
52 int USBSerial::_putc(int c
)
59 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
63 int USBSerial::_getc()
68 setled(4, 1); while (rxbuf
.isEmpty()); setled(4, 0);
70 if (rxbuf
.free() == MAX_PACKET_SIZE_EPBULK
) {
71 usb
->endpointSetInterrupt(CDC_BulkOut
.bEndpointAddress
, true);
72 iprintf("rxbuf has room for another packet, interrupt enabled\n");
73 } else if ((rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
) && (nl_in_rx
== 0)) {
74 // handle potential deadlock where a short line, and the beginning of a very long line are bundled in one usb packet
78 usb
->endpointSetInterrupt(CDC_BulkOut
.bEndpointAddress
, true);
79 iprintf("rxbuf has room for another packet, interrupt enabled\n");
82 if (c
== '\n' || c
== '\r')
88 int USBSerial::puts(const char *str
)
96 if ((txbuf
.available() % 64) == 0)
97 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
101 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
105 uint16_t USBSerial::writeBlock(const uint8_t * buf
, uint16_t size
)
109 if (size
> txbuf
.free()) {
113 for (uint8_t i
= 0; i
< size
; i
++) {
116 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
121 bool USBSerial::USBEvent_EPIn(uint8_t bEP
, uint8_t bEPStatus
)
124 * Called in ISR context
127 // static bool needToSendNull = false;
131 if (bEP
!= CDC_BulkIn
.bEndpointAddress
)
134 iprintf("USBSerial:EpIn: 0x%02X\n", bEPStatus
);
136 uint8_t b
[MAX_PACKET_SIZE_EPBULK
];
138 int l
= txbuf
.available();
140 if (l
> MAX_PACKET_SIZE_EPBULK
)
141 l
= MAX_PACKET_SIZE_EPBULK
;
143 for (i
= 0; i
< l
; i
++) {
144 txbuf
.dequeue(&b
[i
]);
147 if (txbuf
.available() == 0)
152 iprintf("USBSerial:EpIn Complete\n");
156 bool USBSerial::USBEvent_EPOut(uint8_t bEP
, uint8_t bEPStatus
)
159 * Called in ISR context
164 iprintf("USBSerial:EpOut\n");
165 if (bEP
!= CDC_BulkOut
.bEndpointAddress
)
168 if (rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
) {
169 // usb->endpointSetInterrupt(bEP, false);
173 uint8_t c
[MAX_PACKET_SIZE_EPBULK
];
176 //we read the packet received and put it on the circular buffer
178 iprintf("Read %ld bytes:\n\t", size
);
179 for (uint8_t i
= 0; i
< size
; i
++) {
180 if(c
[i
] == 'X' - 'A' + 1) { // ^X
181 THEKERNEL
->set_feed_hold(false); // required to free stuff up
186 if(c
[i
] == '?') { // ?
191 if(THEKERNEL
->is_grbl_mode()) {
192 if(c
[i
] == '!') { // safe pause
193 THEKERNEL
->set_feed_hold(true);
197 if(c
[i
] == '~') { // safe resume
198 THEKERNEL
->set_feed_hold(false);
201 if(last_char_was_dollar
&& (c
[i
] == 'X' || c
[i
] == 'H')) {
202 // we need to do this otherwise $X/$H won't work if there was a feed hold like when stop is clicked in bCNC
203 THEKERNEL
->set_feed_hold(false);
207 last_char_was_dollar
= (c
[i
] == '$');
209 if (flush_to_nl
== false)
212 // if (c[i] >= 32 && c[i] < 128)
214 // iprintf("%c", c[i]);
218 // iprintf("\\x%02X", c[i]);
221 if (c
[i
] == '\n' || c
[i
] == '\r') {
226 } else if (rxbuf
.isFull() && (nl_in_rx
== 0)) {
227 // to avoid a deadlock with very long lines, we must dump the buffer
228 // and continue flushing to the next newline
233 iprintf("\nQueued, %d empty\n", rxbuf
.free());
235 if (rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
) {
236 // if buffer is full, stall endpoint, do not accept more data
240 // we have to check for long line deadlock here too
244 // and since our buffer is empty, we can accept more data
249 usb
->readStart(CDC_BulkOut
.bEndpointAddress
, MAX_PACKET_SIZE_EPBULK
);
250 iprintf("USBSerial:EpOut Complete\n");
254 uint8_t USBSerial::available()
256 return rxbuf
.available();
259 bool USBSerial::ready()
261 return rxbuf
.available();
264 void USBSerial::on_module_loaded()
266 this->register_for_event(ON_MAIN_LOOP
);
267 this->register_for_event(ON_IDLE
);
270 void USBSerial::on_idle(void *argument
)
274 THEKERNEL
->call_event(ON_HALT
, nullptr);
275 if(THEKERNEL
->is_grbl_mode()) {
276 puts("ALARM:Abort during cycle\r\n");
278 puts("HALTED, M999 or $X to exit HALT state\r\n");
280 rxbuf
.flush(); // flush the recieve buffer, hopefully upstream has stopped sending
286 puts(THEKERNEL
->get_query_string().c_str());
291 void USBSerial::on_main_loop(void *argument
)
293 // apparently some OSes don't assert DTR when a program opens the port
294 if (available() && !attach
)
297 if (attach
!= attached
) {
300 THEKERNEL
->streams
->append_stream(this);
301 puts("Smoothie\r\nok\r\n");
304 THEKERNEL
->streams
->remove_stream(this);
311 // if we are in feed hold we do not process anything
312 if(THEKERNEL
->get_feed_hold()) return;
316 while (available()) {
318 if( c
== '\n' || c
== '\r') {
319 struct SerialMessage message
;
320 message
.message
= received
;
321 message
.stream
= this;
322 iprintf("USBSerial Received: %s\n", message
.message
.c_str());
323 THEKERNEL
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
);
332 void USBSerial::on_attach()
337 void USBSerial::on_detach()