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"
27 // extern void setled(int, bool);
28 #define setled(a, b) do {} while (0)
30 #define iprintf(...) do { } while (0)
32 USBSerial::USBSerial(USB
*u
): USBCDC(u
), rxbuf(256 + 8), txbuf(128 + 8)
36 attach
= attached
= false;
40 void USBSerial::ensure_tx_space(int space
)
42 while (txbuf
.free() < space
)
44 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
49 int USBSerial::_putc(int c
)
56 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
60 int USBSerial::_getc()
65 setled(4, 1); while (rxbuf
.isEmpty()); setled(4, 0);
67 if (rxbuf
.free() == MAX_PACKET_SIZE_EPBULK
)
69 usb
->endpointSetInterrupt(CDC_BulkOut
.bEndpointAddress
, true);
70 iprintf("rxbuf has room for another packet, interrupt enabled\n");
72 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
)
97 if ((txbuf
.available() % 64) == 0)
98 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
102 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
106 uint16_t USBSerial::writeBlock(const uint8_t * buf
, uint16_t size
)
110 if (size
> txbuf
.free())
116 for (uint8_t i
= 0; i
< size
; i
++)
120 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
125 bool USBSerial::USBEvent_EPIn(uint8_t bEP
, uint8_t bEPStatus
)
128 * Called in ISR context
131 // static bool needToSendNull = false;
135 if (bEP
!= CDC_BulkIn
.bEndpointAddress
)
138 iprintf("USBSerial:EpIn: 0x%02X\n", bEPStatus
);
140 uint8_t b
[MAX_PACKET_SIZE_EPBULK
];
142 int l
= txbuf
.available();
143 iprintf("%d bytes queued\n", l
);
146 if (l
> MAX_PACKET_SIZE_EPBULK
)
147 l
= MAX_PACKET_SIZE_EPBULK
;
148 iprintf("Sending %d bytes:\n\t", l
);
150 for (i
= 0; i
< l
; i
++) {
151 txbuf
.dequeue(&b
[i
]);
152 if (b
[i
] >= 32 && b
[i
] < 128)
155 iprintf("\\x%02X", b
[i
]);
158 iprintf("\nSending...\n");
161 if (txbuf
.available() == 0)
168 iprintf("USBSerial:EpIn Complete\n");
172 bool USBSerial::USBEvent_EPOut(uint8_t bEP
, uint8_t bEPStatus
)
175 * Called in ISR context
180 iprintf("USBSerial:EpOut\n");
181 if (bEP
!= CDC_BulkOut
.bEndpointAddress
)
184 if (rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
)
186 // usb->endpointSetInterrupt(bEP, false);
190 uint8_t c
[MAX_PACKET_SIZE_EPBULK
];
193 //we read the packet received and put it on the circular buffer
195 iprintf("Read %ld bytes:\n\t", size
);
196 for (uint8_t i
= 0; i
< size
; i
++) {
198 if (flush_to_nl
== false)
201 if (c
[i
] >= 32 && c
[i
] < 128)
207 iprintf("\\x%02X", c
[i
]);
210 if (c
[i
] == '\n' || c
[i
] == '\r')
217 else if (rxbuf
.isFull() && (nl_in_rx
== 0))
219 // to avoid a deadlock with very long lines, we must dump the buffer
220 // and continue flushing to the next newline
225 iprintf("\nQueued, %d empty\n", rxbuf
.free());
227 if (rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
)
229 // if buffer is full, stall endpoint, do not accept more data
234 // we have to check for long line deadlock here too
238 // and since our buffer is empty, we can accept more data
243 usb
->readStart(CDC_BulkOut
.bEndpointAddress
, MAX_PACKET_SIZE_EPBULK
);
244 iprintf("USBSerial:EpOut Complete\n");
248 uint8_t USBSerial::available()
250 return rxbuf
.available();
253 void USBSerial::on_module_loaded()
255 this->register_for_event(ON_MAIN_LOOP
);
258 void USBSerial::on_main_loop(void *argument
)
260 // apparently some OSes don't assert DTR when a program opens the port
261 if (available() && !attach
)
264 if (attach
!= attached
)
269 kernel
->streams
->append_stream(this);
270 writeBlock((const uint8_t *) "Smoothie\nok\n", 12);
275 kernel
->streams
->remove_stream(this);
287 if( c
== '\n' || c
== '\r')
289 struct SerialMessage message
;
290 message
.message
= received
;
291 message
.stream
= this;
292 iprintf("USBSerial Received: %s\n", message
.message
.c_str());
293 this->kernel
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
);
304 void USBSerial::on_attach()
309 void USBSerial::on_detach()