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(...)
32 USBSerial::USBSerial(USB
*u
): USBCDC(u
), rxbuf(128), txbuf(128)
38 int USBSerial::_putc(int c
)
40 // send((uint8_t *)&c, 1);
44 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
45 // usb->endpointTriggerInterrupt(CDC_BulkIn.bEndpointAddress);
49 int USBSerial::_getc()
52 setled(4, 1); while (rxbuf
.isEmpty()); setled(4, 0);
54 if (rxbuf
.free() == MAX_PACKET_SIZE_EPBULK
)
56 usb
->endpointSetInterrupt(CDC_BulkOut
.bEndpointAddress
, true);
57 iprintf("rxbuf has room for another packet, interrupt enabled\n");
58 // usb->endpointTriggerInterrupt(CDC_BulkOut.bEndpointAddress);
67 uint16_t USBSerial::writeBlock(uint8_t * buf
, uint16_t size
)
69 if (size
> txbuf
.free())
75 for (uint8_t i
= 0; i
< size
; i
++)
79 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
84 bool USBSerial::USBEvent_EPIn(uint8_t bEP
, uint8_t bEPStatus
)
87 * Called in ISR context
90 // static bool needToSendNull = false;
94 if (bEP
!= CDC_BulkIn
.bEndpointAddress
)
97 iprintf("USBSerial:EpIn: 0x%02X\n", bEPStatus
);
99 uint8_t b
[MAX_PACKET_SIZE_EPBULK
];
101 int l
= txbuf
.available();
102 iprintf("%d bytes queued\n", l
);
105 if (l
> MAX_PACKET_SIZE_EPBULK
)
106 l
= MAX_PACKET_SIZE_EPBULK
;
107 iprintf("Sending %d bytes:\n\t", l
);
109 for (i
= 0; i
< l
; i
++) {
110 txbuf
.dequeue(&b
[i
]);
111 if (b
[i
] >= 32 && b
[i
] < 128)
114 iprintf("\\x%02X", b
[i
]);
117 iprintf("\nSending...\n");
120 // if (l == 64 && txbuf.available() == 0)
121 // needToSendNull = true;
122 if (txbuf
.available() == 0)
127 // if (needToSendNull)
130 // needToSendNull = false;
136 // usb->endpointSetInterrupt(bEP, false);
138 iprintf("USBSerial:EpIn Complete\n");
142 bool USBSerial::USBEvent_EPOut(uint8_t bEP
, uint8_t bEPStatus
)
145 * Called in ISR context
150 iprintf("USBSerial:EpOut\n");
151 if (bEP
!= CDC_BulkOut
.bEndpointAddress
)
154 if (rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
)
156 // usb->endpointSetInterrupt(bEP, false);
160 uint8_t c
[MAX_PACKET_SIZE_EPBULK
];
163 //we read the packet received and put it on the circular buffer
165 iprintf("Read %ld bytes:\n\t", size
);
166 for (uint8_t i
= 0; i
< size
; i
++) {
168 if (c
[i
] >= 32 && c
[i
] < 128)
174 iprintf("\\x%02X", c
[i
]);
179 iprintf("\nQueued, %d empty\n", rxbuf
.free());
181 if (rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
)
183 // usb->endpointSetInterrupt(bEP, false);
187 usb
->readStart(CDC_BulkOut
.bEndpointAddress
, MAX_PACKET_SIZE_EPBULK
);
188 iprintf("USBSerial:EpOut Complete\n");
193 bool USBSerial::EpCallback(uint8_t bEP, uint8_t bEPStatus) {
194 if (bEP == CDC_BulkOut.bEndpointAddress) {
198 //we read the packet received and put it on the circular buffer
200 for (uint8_t i = 0; i < size; i++) {
204 //call a potential handler
207 // We reactivate the endpoint to receive next characters
208 usb->readStart(CDC_BulkOut.bEndpointAddress, MAX_PACKET_SIZE_EPBULK);
214 uint8_t USBSerial::available()
216 return rxbuf
.available();
219 void USBSerial::on_module_loaded()
221 this->register_for_event(ON_MAIN_LOOP
);
222 // this->kernel->streams->append_stream(this);
225 void USBSerial::on_main_loop(void *argument
)
227 // this->kernel->streams->printf("!");
236 struct SerialMessage message
;
237 message
.message
= received
;
238 message
.stream
= this;
239 this->kernel
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
);
250 void USBSerial::on_attach()
252 this->kernel
->streams
->append_stream(this);
253 writeBlock((uint8_t *) "Smoothie\nok\n", 12);
256 void USBSerial::on_detach()
258 this->kernel
->streams
->remove_stream(this);