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(128 + 8), txbuf(128 + 8)
36 attach
= attached
= false;
39 void USBSerial::ensure_tx_space(int space
)
41 while (txbuf
.free() < space
)
43 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
48 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");
80 int USBSerial::puts(const char *str
)
90 if ((txbuf
.available() % 64) == 0)
91 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
95 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
99 uint16_t USBSerial::writeBlock(uint8_t * buf
, uint16_t size
)
103 if (size
> txbuf
.free())
109 for (uint8_t i
= 0; i
< size
; i
++)
113 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
118 bool USBSerial::USBEvent_EPIn(uint8_t bEP
, uint8_t bEPStatus
)
121 * Called in ISR context
124 // static bool needToSendNull = false;
128 if (bEP
!= CDC_BulkIn
.bEndpointAddress
)
131 iprintf("USBSerial:EpIn: 0x%02X\n", bEPStatus
);
133 uint8_t b
[MAX_PACKET_SIZE_EPBULK
];
135 int l
= txbuf
.available();
136 iprintf("%d bytes queued\n", l
);
139 if (l
> MAX_PACKET_SIZE_EPBULK
)
140 l
= MAX_PACKET_SIZE_EPBULK
;
141 iprintf("Sending %d bytes:\n\t", l
);
143 for (i
= 0; i
< l
; i
++) {
144 txbuf
.dequeue(&b
[i
]);
145 if (b
[i
] >= 32 && b
[i
] < 128)
148 iprintf("\\x%02X", b
[i
]);
151 iprintf("\nSending...\n");
154 if (txbuf
.available() == 0)
161 iprintf("USBSerial:EpIn Complete\n");
165 bool USBSerial::USBEvent_EPOut(uint8_t bEP
, uint8_t bEPStatus
)
168 * Called in ISR context
173 iprintf("USBSerial:EpOut\n");
174 if (bEP
!= CDC_BulkOut
.bEndpointAddress
)
177 if (rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
)
179 // usb->endpointSetInterrupt(bEP, false);
183 uint8_t c
[MAX_PACKET_SIZE_EPBULK
];
186 //we read the packet received and put it on the circular buffer
188 iprintf("Read %ld bytes:\n\t", size
);
189 for (uint8_t i
= 0; i
< size
; i
++) {
191 if (c
[i
] >= 32 && c
[i
] < 128)
197 iprintf("\\x%02X", c
[i
]);
202 iprintf("\nQueued, %d empty\n", rxbuf
.free());
204 if (rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
)
209 usb
->readStart(CDC_BulkOut
.bEndpointAddress
, MAX_PACKET_SIZE_EPBULK
);
210 iprintf("USBSerial:EpOut Complete\n");
214 uint8_t USBSerial::available()
216 return rxbuf
.available();
219 void USBSerial::on_module_loaded()
221 this->register_for_event(ON_MAIN_LOOP
);
224 void USBSerial::on_main_loop(void *argument
)
226 // apparently some OSes don't assert DTR when a program opens the port
227 if (available() && !attach
)
230 if (attach
!= attached
)
235 kernel
->streams
->append_stream(this);
236 writeBlock((uint8_t *) "Smoothie\nok\n", 12);
241 kernel
->streams
->remove_stream(this);
253 if( c
== '\r' ){ c
= '\n'; }
256 struct SerialMessage message
;
257 message
.message
= received
;
258 message
.stream
= this;
259 iprintf("USBSerial Received: %s\n", message
.message
.c_str());
260 this->kernel
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
);
271 void USBSerial::on_attach()
276 void USBSerial::on_detach()