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())
76 if ((txbuf
.available() % 64) == 0)
77 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
81 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
85 uint16_t USBSerial::writeBlock(uint8_t * buf
, uint16_t size
)
87 if (size
> txbuf
.free())
93 for (uint8_t i
= 0; i
< size
; i
++)
97 usb
->endpointSetInterrupt(CDC_BulkIn
.bEndpointAddress
, true);
102 bool USBSerial::USBEvent_EPIn(uint8_t bEP
, uint8_t bEPStatus
)
105 * Called in ISR context
108 // static bool needToSendNull = false;
112 if (bEP
!= CDC_BulkIn
.bEndpointAddress
)
115 iprintf("USBSerial:EpIn: 0x%02X\n", bEPStatus
);
117 uint8_t b
[MAX_PACKET_SIZE_EPBULK
];
119 int l
= txbuf
.available();
120 iprintf("%d bytes queued\n", l
);
123 if (l
> MAX_PACKET_SIZE_EPBULK
)
124 l
= MAX_PACKET_SIZE_EPBULK
;
125 iprintf("Sending %d bytes:\n\t", l
);
127 for (i
= 0; i
< l
; i
++) {
128 txbuf
.dequeue(&b
[i
]);
129 if (b
[i
] >= 32 && b
[i
] < 128)
132 iprintf("\\x%02X", b
[i
]);
135 iprintf("\nSending...\n");
138 // if (l == 64 && txbuf.available() == 0)
139 // needToSendNull = true;
140 if (txbuf
.available() == 0)
145 // if (needToSendNull)
148 // needToSendNull = false;
154 // usb->endpointSetInterrupt(bEP, false);
156 iprintf("USBSerial:EpIn Complete\n");
160 bool USBSerial::USBEvent_EPOut(uint8_t bEP
, uint8_t bEPStatus
)
163 * Called in ISR context
168 iprintf("USBSerial:EpOut\n");
169 if (bEP
!= CDC_BulkOut
.bEndpointAddress
)
172 if (rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
)
174 // usb->endpointSetInterrupt(bEP, false);
178 uint8_t c
[MAX_PACKET_SIZE_EPBULK
];
181 //we read the packet received and put it on the circular buffer
183 iprintf("Read %ld bytes:\n\t", size
);
184 for (uint8_t i
= 0; i
< size
; i
++) {
186 if (c
[i
] >= 32 && c
[i
] < 128)
192 iprintf("\\x%02X", c
[i
]);
197 iprintf("\nQueued, %d empty\n", rxbuf
.free());
199 if (rxbuf
.free() < MAX_PACKET_SIZE_EPBULK
)
201 // usb->endpointSetInterrupt(bEP, false);
205 usb
->readStart(CDC_BulkOut
.bEndpointAddress
, MAX_PACKET_SIZE_EPBULK
);
206 iprintf("USBSerial:EpOut Complete\n");
211 bool USBSerial::EpCallback(uint8_t bEP, uint8_t bEPStatus) {
212 if (bEP == CDC_BulkOut.bEndpointAddress) {
216 //we read the packet received and put it on the circular buffer
218 for (uint8_t i = 0; i < size; i++) {
222 //call a potential handler
225 // We reactivate the endpoint to receive next characters
226 usb->readStart(CDC_BulkOut.bEndpointAddress, MAX_PACKET_SIZE_EPBULK);
232 uint8_t USBSerial::available()
234 return rxbuf
.available();
237 void USBSerial::on_module_loaded()
239 this->register_for_event(ON_MAIN_LOOP
);
240 // this->kernel->streams->append_stream(this);
243 void USBSerial::on_main_loop(void *argument
)
245 // this->kernel->streams->printf("!");
254 struct SerialMessage message
;
255 message
.message
= received
;
256 message
.stream
= this;
257 iprintf("USBSerial Received: %s\n", message
.message
.c_str());
258 this->kernel
->call_event(ON_CONSOLE_LINE_RECEIVED
, &message
);
269 void USBSerial::on_attach()
271 this->kernel
->streams
->append_stream(this);
272 writeBlock((uint8_t *) "Smoothie\nok\n", 12);
275 void USBSerial::on_detach()
277 this->kernel
->streams
->remove_stream(this);