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 void USBSerial::ensure_tx_space(int space
)
43 while (txbuf
.free() < space
)
45 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
50 int USBSerial::_putc(int c
)
57 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
61 int USBSerial::_getc()
66 setled(4, 1); while (rxbuf
.isEmpty()); setled(4, 0);
68 if (rxbuf
.free() == MAX_PACKET_SIZE_EPBULK
)
70 usb
->endpointSetInterrupt(CDC_BulkOut
.bEndpointAddress
, true);
71 iprintf("rxbuf has room for another packet, interrupt enabled\n");
73 else if ((rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
) && (nl_in_rx
== 0))
75 // handle potential deadlock where a short line, and the beginning of a very long line are bundled in one usb packet
79 usb
->endpointSetInterrupt(CDC_BulkOut
.bEndpointAddress
, true);
80 iprintf("rxbuf has room for another packet, interrupt enabled\n");
83 if (c
== '\n' || c
== '\r')
89 int USBSerial::puts(const char *str
)
98 if ((txbuf
.available() % 64) == 0)
99 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
103 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
107 uint16_t USBSerial::writeBlock(const uint8_t * buf
, uint16_t size
)
111 if (size
> txbuf
.free())
117 for (uint8_t i
= 0; i
< size
; i
++)
121 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
126 bool USBSerial::USBEvent_EPIn(uint8_t bEP
, uint8_t bEPStatus
)
129 * Called in ISR context
132 // static bool needToSendNull = false;
136 if (bEP
!= CDC_BulkIn
.bEndpointAddress
)
139 iprintf("USBSerial:EpIn: 0x%02X\n", bEPStatus
);
141 uint8_t b
[MAX_PACKET_SIZE_EPBULK
];
143 int l
= txbuf
.available();
144 iprintf("%d bytes queued\n", l
);
147 if (l
> MAX_PACKET_SIZE_EPBULK
)
148 l
= MAX_PACKET_SIZE_EPBULK
;
149 iprintf("Sending %d bytes:\n\t", l
);
151 for (i
= 0; i
< l
; i
++) {
152 txbuf
.dequeue(&b
[i
]);
153 if (b
[i
] >= 32 && b
[i
] < 128)
156 iprintf("\\x%02X", b
[i
]);
159 iprintf("\nSending...\n");
162 if (txbuf
.available() == 0)
169 iprintf("USBSerial:EpIn Complete\n");
173 bool USBSerial::USBEvent_EPOut(uint8_t bEP
, uint8_t bEPStatus
)
176 * Called in ISR context
181 iprintf("USBSerial:EpOut\n");
182 if (bEP
!= CDC_BulkOut
.bEndpointAddress
)
185 if (rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
)
187 // usb->endpointSetInterrupt(bEP, false);
191 uint8_t c
[MAX_PACKET_SIZE_EPBULK
];
194 //we read the packet received and put it on the circular buffer
196 iprintf("Read %ld bytes:\n\t", size
);
197 for (uint8_t i
= 0; i
< size
; i
++) {
199 if (flush_to_nl
== false)
202 if (c
[i
] >= 32 && c
[i
] < 128)
208 iprintf("\\x%02X", c
[i
]);
211 if (c
[i
] == '\n' || c
[i
] == '\r')
218 else if (rxbuf
.isFull() && (nl_in_rx
== 0))
220 // to avoid a deadlock with very long lines, we must dump the buffer
221 // and continue flushing to the next newline
226 iprintf("\nQueued, %d empty\n", rxbuf
.free());
228 if (rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
)
230 // if buffer is full, stall endpoint, do not accept more data
235 // we have to check for long line deadlock here too
239 // and since our buffer is empty, we can accept more data
244 usb
->readStart(CDC_BulkOut
.bEndpointAddress
, MAX_PACKET_SIZE_EPBULK
);
245 iprintf("USBSerial:EpOut Complete\n");
249 uint8_t USBSerial::available()
251 return rxbuf
.available();
254 void USBSerial::on_module_loaded()
256 this->register_for_event(ON_MAIN_LOOP
);
259 void USBSerial::on_main_loop(void *argument
)
261 // apparently some OSes don't assert DTR when a program opens the port
262 if (available() && !attach
)
265 if (attach
!= attached
)
270 THEKERNEL
->streams
->append_stream(this);
271 writeBlock((const uint8_t *) "Smoothie\nok\n", 12);
276 THEKERNEL
->streams
->remove_stream(this);
288 if( c
== '\n' || c
== '\r')
290 struct SerialMessage message
;
291 message
.message
= received
;
292 message
.stream
= this;
293 iprintf("USBSerial Received: %s\n", message
.message
.c_str());
294 THEKERNEL
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
);
305 void USBSerial::on_attach()
310 void USBSerial::on_detach()