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
)
55 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
59 int USBSerial::_getc()
64 setled(4, 1); while (rxbuf
.isEmpty()); setled(4, 0);
66 if (rxbuf
.free() == MAX_PACKET_SIZE_EPBULK
)
68 usb
->endpointSetInterrupt(CDC_BulkOut
.bEndpointAddress
, true);
69 iprintf("rxbuf has room for another packet, interrupt enabled\n");
78 int USBSerial::puts(const char *str
)
87 if ((txbuf
.available() % 64) == 0)
88 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
92 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
96 uint16_t USBSerial::writeBlock(const uint8_t * buf
, uint16_t size
)
100 if (size
> txbuf
.free())
106 for (uint8_t i
= 0; i
< size
; i
++)
110 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
115 bool USBSerial::USBEvent_EPIn(uint8_t bEP
, uint8_t bEPStatus
)
118 * Called in ISR context
121 // static bool needToSendNull = false;
125 if (bEP
!= CDC_BulkIn
.bEndpointAddress
)
128 iprintf("USBSerial:EpIn: 0x%02X\n", bEPStatus
);
130 uint8_t b
[MAX_PACKET_SIZE_EPBULK
];
132 int l
= txbuf
.available();
133 iprintf("%d bytes queued\n", l
);
136 if (l
> MAX_PACKET_SIZE_EPBULK
)
137 l
= MAX_PACKET_SIZE_EPBULK
;
138 iprintf("Sending %d bytes:\n\t", l
);
140 for (i
= 0; i
< l
; i
++) {
141 txbuf
.dequeue(&b
[i
]);
142 if (b
[i
] >= 32 && b
[i
] < 128)
145 iprintf("\\x%02X", b
[i
]);
148 iprintf("\nSending...\n");
151 if (txbuf
.available() == 0)
158 iprintf("USBSerial:EpIn Complete\n");
162 bool USBSerial::USBEvent_EPOut(uint8_t bEP
, uint8_t bEPStatus
)
165 * Called in ISR context
170 iprintf("USBSerial:EpOut\n");
171 if (bEP
!= CDC_BulkOut
.bEndpointAddress
)
174 if (rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
)
176 // usb->endpointSetInterrupt(bEP, false);
180 uint8_t c
[MAX_PACKET_SIZE_EPBULK
];
183 //we read the packet received and put it on the circular buffer
185 iprintf("Read %ld bytes:\n\t", size
);
186 for (uint8_t i
= 0; i
< size
; i
++) {
188 if (c
[i
] >= 32 && c
[i
] < 128)
194 iprintf("\\x%02X", c
[i
]);
199 iprintf("\nQueued, %d empty\n", rxbuf
.free());
201 if (rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
)
206 usb
->readStart(CDC_BulkOut
.bEndpointAddress
, MAX_PACKET_SIZE_EPBULK
);
207 iprintf("USBSerial:EpOut Complete\n");
211 uint8_t USBSerial::available()
213 return rxbuf
.available();
216 void USBSerial::on_module_loaded()
218 this->register_for_event(ON_MAIN_LOOP
);
221 void USBSerial::on_main_loop(void *argument
)
223 // apparently some OSes don't assert DTR when a program opens the port
224 if (available() && !attach
)
227 if (attach
!= attached
)
232 kernel
->streams
->append_stream(this);
233 writeBlock((const uint8_t *) "Smoothie\nok\n", 12);
238 kernel
->streams
->remove_stream(this);
252 struct SerialMessage message
;
253 message
.message
= received
;
254 message
.stream
= this;
255 iprintf("USBSerial Received: %s\n", message
.message
.c_str());
256 this->kernel
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
);
267 void USBSerial::on_attach()
272 void USBSerial::on_detach()