adds a USB serial port to your design
Dependents: Example_WatchDog_Timer
Diff: USBSerial/USBSerial.cpp
- Revision:
- 77:3e96144c2bf2
- Parent:
- 70:2c525a50f1b6
diff -r 3d21c913753e -r 3e96144c2bf2 USBSerial/USBSerial.cpp --- a/USBSerial/USBSerial.cpp Fri Nov 23 18:05:40 2018 +0000 +++ b/USBSerial/USBSerial.cpp Mon Nov 26 20:46:02 2018 +0000 @@ -33,6 +33,40 @@ return c; } +int USBSerial::_read(void* buffer, int length) +{ + int data_read = 0; + + uint8_t *ptr = static_cast<uint8_t *>(buffer); + + if (length == 0) { + return 0; + } + + if (buf.isEmpty()) { + return -EAGAIN; + } + + while (data_read < length && !buf.isEmpty()) { + buf.dequeue(ptr++); + data_read++; + } + + return data_read; +} + +int USBSerial::_write(uint8_t * buf, uint16_t size) { + uint16_t sent = size; + + // This handles the case where we have more to send than one bulk frame + while(size > MAX_PACKET_SIZE_EPBULK && send(buf, MAX_PACKET_SIZE_EPBULK)) { + size -= MAX_PACKET_SIZE_EPBULK; + } + if(!send(buf, size)) { + return -EAGAIN; + } + return sent; +} bool USBSerial::writeBlock(uint8_t * buf, uint16_t size) { if(size > MAX_PACKET_SIZE_EPBULK) {