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
)
48 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
53 int USBSerial::_putc(int c
)
60 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
64 int USBSerial::_getc()
69 setled(4, 1); while (rxbuf
.isEmpty()); setled(4, 0);
71 if (rxbuf
.free() == MAX_PACKET_SIZE_EPBULK
)
73 usb
->endpointSetInterrupt(CDC_BulkOut
.bEndpointAddress
, true);
74 iprintf("rxbuf has room for another packet, interrupt enabled\n");
76 else if ((rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
) && (nl_in_rx
== 0))
78 // handle potential deadlock where a short line, and the beginning of a very long line are bundled in one usb packet
82 usb
->endpointSetInterrupt(CDC_BulkOut
.bEndpointAddress
, true);
83 iprintf("rxbuf has room for another packet, interrupt enabled\n");
86 if (c
== '\n' || c
== '\r')
92 int USBSerial::puts(const char *str
)
101 if ((txbuf
.available() % 64) == 0)
102 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
106 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
110 uint16_t USBSerial::writeBlock(const uint8_t * buf
, uint16_t size
)
114 if (size
> txbuf
.free())
120 for (uint8_t i
= 0; i
< size
; i
++)
124 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
129 bool USBSerial::USBEvent_EPIn(uint8_t bEP
, uint8_t bEPStatus
)
132 * Called in ISR context
135 // static bool needToSendNull = false;
139 if (bEP
!= CDC_BulkIn
.bEndpointAddress
)
142 iprintf("USBSerial:EpIn: 0x%02X\n", bEPStatus
);
144 uint8_t b
[MAX_PACKET_SIZE_EPBULK
];
146 int l
= txbuf
.available();
147 iprintf("%d bytes queued\n", l
);
150 if (l
> MAX_PACKET_SIZE_EPBULK
)
151 l
= MAX_PACKET_SIZE_EPBULK
;
152 iprintf("Sending %d bytes:\n\t", l
);
154 for (i
= 0; i
< l
; i
++) {
155 txbuf
.dequeue(&b
[i
]);
156 if (b
[i
] >= 32 && b
[i
] < 128)
159 iprintf("\\x%02X", b
[i
]);
162 iprintf("\nSending...\n");
165 if (txbuf
.available() == 0)
172 iprintf("USBSerial:EpIn Complete\n");
176 bool USBSerial::USBEvent_EPOut(uint8_t bEP
, uint8_t bEPStatus
)
179 * Called in ISR context
184 iprintf("USBSerial:EpOut\n");
185 if (bEP
!= CDC_BulkOut
.bEndpointAddress
)
188 if (rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
)
190 // usb->endpointSetInterrupt(bEP, false);
194 uint8_t c
[MAX_PACKET_SIZE_EPBULK
];
197 //we read the packet received and put it on the circular buffer
199 iprintf("Read %ld bytes:\n\t", size
);
200 for (uint8_t i
= 0; i
< size
; i
++) {
201 if(c
[i
] == 'X'-'A'+1){ // ^X
202 THEKERNEL
->set_feed_hold(false); // required to free stuff up
207 if(c
[i
] == '?'){ // ?
212 if(THEKERNEL
->is_grbl_mode()) {
213 if(c
[i
] == '!'){ // safe pause
214 THEKERNEL
->set_feed_hold(true);
218 if(c
[i
] == '~'){ // safe resume
219 THEKERNEL
->set_feed_hold(false);
222 if(last_char_was_dollar
&& (c
[i
] == 'X' || c
[i
] == 'H')) {
223 // we need to do this otherwise $X/$H won't work if there was a feed hold like when stop is clicked in bCNC
224 THEKERNEL
->set_feed_hold(false);
228 last_char_was_dollar
= (c
[i
] == '$');
230 if (flush_to_nl
== false)
233 // if (c[i] >= 32 && c[i] < 128)
235 // iprintf("%c", c[i]);
239 // iprintf("\\x%02X", c[i]);
242 if (c
[i
] == '\n' || c
[i
] == '\r')
249 else if (rxbuf
.isFull() && (nl_in_rx
== 0))
251 // to avoid a deadlock with very long lines, we must dump the buffer
252 // and continue flushing to the next newline
257 iprintf("\nQueued, %d empty\n", rxbuf
.free());
259 if (rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
)
261 // if buffer is full, stall endpoint, do not accept more data
266 // we have to check for long line deadlock here too
270 // and since our buffer is empty, we can accept more data
275 usb
->readStart(CDC_BulkOut
.bEndpointAddress
, MAX_PACKET_SIZE_EPBULK
);
276 iprintf("USBSerial:EpOut Complete\n");
280 uint8_t USBSerial::available()
282 return rxbuf
.available();
285 bool USBSerial::ready()
287 return rxbuf
.available();
290 void USBSerial::on_module_loaded()
292 this->register_for_event(ON_MAIN_LOOP
);
293 this->register_for_event(ON_IDLE
);
296 void USBSerial::on_idle(void *argument
)
300 THEKERNEL
->call_event(ON_HALT
, nullptr);
301 if(THEKERNEL
->is_grbl_mode()) {
302 puts("ALARM:Abort during cycle\r\n");
304 puts("HALTED, M999 or $X to exit HALT state\r\n");
310 puts(THEKERNEL
->get_query_string().c_str());
315 void USBSerial::on_main_loop(void *argument
)
317 // apparently some OSes don't assert DTR when a program opens the port
318 if (available() && !attach
)
321 if (attach
!= attached
)
326 THEKERNEL
->streams
->append_stream(this);
327 puts("Smoothie\r\nok\r\n");
332 THEKERNEL
->streams
->remove_stream(this);
339 // if we are in feed hold we do not process anything
340 if(THEKERNEL
->get_feed_hold()) return;
348 if( c
== '\n' || c
== '\r')
350 struct SerialMessage message
;
351 message
.message
= received
;
352 message
.stream
= this;
353 iprintf("USBSerial Received: %s\n", message
.message
.c_str());
354 THEKERNEL
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
);
365 void USBSerial::on_attach()
370 void USBSerial::on_detach()