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), txbuf(128)
38 int USBSerial::_putc(int c
)
40 // send((uint8_t *)&c, 1);
46 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
47 // usb->endpointTriggerInterrupt(CDC_BulkIn.bEndpointAddress);
51 int USBSerial::_getc()
54 setled(4, 1); while (rxbuf
.isEmpty()); setled(4, 0);
56 if (rxbuf
.free() == MAX_PACKET_SIZE_EPBULK
)
58 usb
->endpointSetInterrupt(CDC_BulkOut
.bEndpointAddress
, true);
59 iprintf("rxbuf has room for another packet, interrupt enabled\n");
60 // usb->endpointTriggerInterrupt(CDC_BulkOut.bEndpointAddress);
69 int USBSerial::puts(const char *str
)
74 if ((*str
!= '\r') && txbuf
.free())
79 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
83 uint16_t USBSerial::writeBlock(uint8_t * buf
, uint16_t size
)
85 if (size
> txbuf
.free())
91 for (uint8_t i
= 0; i
< size
; i
++)
95 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
100 bool USBSerial::USBEvent_EPIn(uint8_t bEP
, uint8_t bEPStatus
)
103 * Called in ISR context
106 // static bool needToSendNull = false;
110 if (bEP
!= CDC_BulkIn
.bEndpointAddress
)
113 iprintf("USBSerial:EpIn: 0x%02X\n", bEPStatus
);
115 uint8_t b
[MAX_PACKET_SIZE_EPBULK
];
117 int l
= txbuf
.available();
118 iprintf("%d bytes queued\n", l
);
121 if (l
> MAX_PACKET_SIZE_EPBULK
)
122 l
= MAX_PACKET_SIZE_EPBULK
;
123 iprintf("Sending %d bytes:\n\t", l
);
125 for (i
= 0; i
< l
; i
++) {
126 txbuf
.dequeue(&b
[i
]);
127 if (b
[i
] >= 32 && b
[i
] < 128)
130 iprintf("\\x%02X", b
[i
]);
133 iprintf("\nSending...\n");
136 // if (l == 64 && txbuf.available() == 0)
137 // needToSendNull = true;
138 if (txbuf
.available() == 0)
143 // if (needToSendNull)
146 // needToSendNull = false;
152 // usb->endpointSetInterrupt(bEP, false);
154 iprintf("USBSerial:EpIn Complete\n");
158 bool USBSerial::USBEvent_EPOut(uint8_t bEP
, uint8_t bEPStatus
)
161 * Called in ISR context
166 iprintf("USBSerial:EpOut\n");
167 if (bEP
!= CDC_BulkOut
.bEndpointAddress
)
170 if (rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
)
172 // usb->endpointSetInterrupt(bEP, false);
176 uint8_t c
[MAX_PACKET_SIZE_EPBULK
];
179 //we read the packet received and put it on the circular buffer
181 iprintf("Read %ld bytes:\n\t", size
);
182 for (uint8_t i
= 0; i
< size
; i
++) {
184 if (c
[i
] >= 32 && c
[i
] < 128)
190 iprintf("\\x%02X", c
[i
]);
195 iprintf("\nQueued, %d empty\n", rxbuf
.free());
197 if (rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
)
199 // usb->endpointSetInterrupt(bEP, false);
203 usb
->readStart(CDC_BulkOut
.bEndpointAddress
, MAX_PACKET_SIZE_EPBULK
);
204 iprintf("USBSerial:EpOut Complete\n");
209 bool USBSerial::EpCallback(uint8_t bEP, uint8_t bEPStatus) {
210 if (bEP == CDC_BulkOut.bEndpointAddress) {
214 //we read the packet received and put it on the circular buffer
216 for (uint8_t i = 0; i < size; i++) {
220 //call a potential handler
223 // We reactivate the endpoint to receive next characters
224 usb->readStart(CDC_BulkOut.bEndpointAddress, MAX_PACKET_SIZE_EPBULK);
230 uint8_t USBSerial::available()
232 return rxbuf
.available();
235 void USBSerial::on_module_loaded()
237 this->register_for_event(ON_MAIN_LOOP
);
238 // this->kernel->streams->append_stream(this);
241 void USBSerial::on_main_loop(void *argument
)
243 // this->kernel->streams->printf("!");
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()
269 this->kernel
->streams
->append_stream(this);
270 writeBlock((uint8_t *) "Smoothie\nok\n", 12);
273 void USBSerial::on_detach()
275 this->kernel
->streams
->remove_stream(this);