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;
43 void USBSerial::ensure_tx_space(int space
)
45 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
)
72 usb
->endpointSetInterrupt(CDC_BulkOut
.bEndpointAddress
, true);
73 iprintf("rxbuf has room for another packet, interrupt enabled\n");
75 else if ((rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
) && (nl_in_rx
== 0))
77 // handle potential deadlock where a short line, and the beginning of a very long line are bundled in one usb packet
81 usb
->endpointSetInterrupt(CDC_BulkOut
.bEndpointAddress
, true);
82 iprintf("rxbuf has room for another packet, interrupt enabled\n");
85 if (c
== '\n' || c
== '\r')
91 int USBSerial::puts(const char *str
)
100 if ((txbuf
.available() % 64) == 0)
101 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
105 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
109 uint16_t USBSerial::writeBlock(const uint8_t * buf
, uint16_t size
)
113 if (size
> txbuf
.free())
119 for (uint8_t i
= 0; i
< size
; i
++)
123 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
128 bool USBSerial::USBEvent_EPIn(uint8_t bEP
, uint8_t bEPStatus
)
131 * Called in ISR context
134 // static bool needToSendNull = false;
138 if (bEP
!= CDC_BulkIn
.bEndpointAddress
)
141 iprintf("USBSerial:EpIn: 0x%02X\n", bEPStatus
);
143 uint8_t b
[MAX_PACKET_SIZE_EPBULK
];
145 int l
= txbuf
.available();
146 iprintf("%d bytes queued\n", l
);
149 if (l
> MAX_PACKET_SIZE_EPBULK
)
150 l
= MAX_PACKET_SIZE_EPBULK
;
151 iprintf("Sending %d bytes:\n\t", l
);
153 for (i
= 0; i
< l
; i
++) {
154 txbuf
.dequeue(&b
[i
]);
155 if (b
[i
] >= 32 && b
[i
] < 128)
158 iprintf("\\x%02X", b
[i
]);
161 iprintf("\nSending...\n");
164 if (txbuf
.available() == 0)
171 iprintf("USBSerial:EpIn Complete\n");
175 bool USBSerial::USBEvent_EPOut(uint8_t bEP
, uint8_t bEPStatus
)
178 * Called in ISR context
183 iprintf("USBSerial:EpOut\n");
184 if (bEP
!= CDC_BulkOut
.bEndpointAddress
)
187 if (rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
)
189 // usb->endpointSetInterrupt(bEP, false);
193 uint8_t c
[MAX_PACKET_SIZE_EPBULK
];
196 //we read the packet received and put it on the circular buffer
198 iprintf("Read %ld bytes:\n\t", size
);
199 for (uint8_t i
= 0; i
< size
; i
++) {
200 if(c
[i
] == 'X'-'A'+1){ // ^X
205 if(c
[i
] == '?'){ // ?
210 if(c
[i
] == '!'){ // safe pause
211 THEKERNEL
->set_feed_hold(true);
215 if(c
[i
] == '~'){ // safe resume
216 THEKERNEL
->set_feed_hold(false);
220 if (flush_to_nl
== false)
223 if (c
[i
] >= 32 && c
[i
] < 128)
229 iprintf("\\x%02X", c
[i
]);
232 if (c
[i
] == '\n' || c
[i
] == '\r')
239 else if (rxbuf
.isFull() && (nl_in_rx
== 0))
241 // to avoid a deadlock with very long lines, we must dump the buffer
242 // and continue flushing to the next newline
247 iprintf("\nQueued, %d empty\n", rxbuf
.free());
249 if (rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
)
251 // if buffer is full, stall endpoint, do not accept more data
256 // we have to check for long line deadlock here too
260 // and since our buffer is empty, we can accept more data
265 usb
->readStart(CDC_BulkOut
.bEndpointAddress
, MAX_PACKET_SIZE_EPBULK
);
266 iprintf("USBSerial:EpOut Complete\n");
270 uint8_t USBSerial::available()
272 return rxbuf
.available();
275 bool USBSerial::ready()
277 return rxbuf
.available();
280 void USBSerial::on_module_loaded()
282 this->register_for_event(ON_MAIN_LOOP
);
283 this->register_for_event(ON_IDLE
);
286 void USBSerial::on_idle(void *argument
)
290 THEKERNEL
->call_event(ON_HALT
, nullptr);
291 if(THEKERNEL
->is_grbl_mode()) {
292 puts("ALARM:Abort during cycle\r\n");
294 puts("HALTED, M999 or $X to exit HALT state\r\n");
300 puts(THEKERNEL
->get_query_string().c_str());
305 void USBSerial::on_main_loop(void *argument
)
307 // apparently some OSes don't assert DTR when a program opens the port
308 if (available() && !attach
)
311 if (attach
!= attached
)
316 THEKERNEL
->streams
->append_stream(this);
317 puts("Smoothie\r\nok\r\n");
322 THEKERNEL
->streams
->remove_stream(this);
329 // if we are in feed hold we do not process anything
330 if(THEKERNEL
->get_feed_hold()) return;
338 if( c
== '\n' || c
== '\r')
340 struct SerialMessage message
;
341 message
.message
= received
;
342 message
.stream
= this;
343 iprintf("USBSerial Received: %s\n", message
.message
.c_str());
344 THEKERNEL
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
);
355 void USBSerial::on_attach()
360 void USBSerial::on_detach()