mbed-os
Fork of mbed-os by
features/unsupported/USBDevice/USBSerial/USBSerial.cpp@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
elessair | 0:f269e3021894 | 2 | * |
elessair | 0:f269e3021894 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
elessair | 0:f269e3021894 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
elessair | 0:f269e3021894 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
elessair | 0:f269e3021894 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
elessair | 0:f269e3021894 | 7 | * Software is furnished to do so, subject to the following conditions: |
elessair | 0:f269e3021894 | 8 | * |
elessair | 0:f269e3021894 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
elessair | 0:f269e3021894 | 10 | * substantial portions of the Software. |
elessair | 0:f269e3021894 | 11 | * |
elessair | 0:f269e3021894 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
elessair | 0:f269e3021894 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
elessair | 0:f269e3021894 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
elessair | 0:f269e3021894 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
elessair | 0:f269e3021894 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
elessair | 0:f269e3021894 | 17 | */ |
elessair | 0:f269e3021894 | 18 | |
elessair | 0:f269e3021894 | 19 | #include "stdint.h" |
elessair | 0:f269e3021894 | 20 | #include "USBSerial.h" |
elessair | 0:f269e3021894 | 21 | |
elessair | 0:f269e3021894 | 22 | int USBSerial::_putc(int c) { |
elessair | 0:f269e3021894 | 23 | if (!terminal_connected) |
elessair | 0:f269e3021894 | 24 | return 0; |
elessair | 0:f269e3021894 | 25 | send((uint8_t *)&c, 1); |
elessair | 0:f269e3021894 | 26 | return 1; |
elessair | 0:f269e3021894 | 27 | } |
elessair | 0:f269e3021894 | 28 | |
elessair | 0:f269e3021894 | 29 | int USBSerial::_getc() { |
elessair | 0:f269e3021894 | 30 | uint8_t c = 0; |
elessair | 0:f269e3021894 | 31 | while (buf.isEmpty()); |
elessair | 0:f269e3021894 | 32 | buf.dequeue(&c); |
elessair | 0:f269e3021894 | 33 | return c; |
elessair | 0:f269e3021894 | 34 | } |
elessair | 0:f269e3021894 | 35 | |
elessair | 0:f269e3021894 | 36 | |
elessair | 0:f269e3021894 | 37 | bool USBSerial::writeBlock(uint8_t * buf, uint16_t size) { |
elessair | 0:f269e3021894 | 38 | if(size > MAX_PACKET_SIZE_EPBULK) { |
elessair | 0:f269e3021894 | 39 | return false; |
elessair | 0:f269e3021894 | 40 | } |
elessair | 0:f269e3021894 | 41 | if(!send(buf, size)) { |
elessair | 0:f269e3021894 | 42 | return false; |
elessair | 0:f269e3021894 | 43 | } |
elessair | 0:f269e3021894 | 44 | return true; |
elessair | 0:f269e3021894 | 45 | } |
elessair | 0:f269e3021894 | 46 | |
elessair | 0:f269e3021894 | 47 | |
elessair | 0:f269e3021894 | 48 | |
elessair | 0:f269e3021894 | 49 | bool USBSerial::EPBULK_OUT_callback() { |
elessair | 0:f269e3021894 | 50 | uint8_t c[65]; |
elessair | 0:f269e3021894 | 51 | uint32_t size = 0; |
elessair | 0:f269e3021894 | 52 | |
elessair | 0:f269e3021894 | 53 | //we read the packet received and put it on the circular buffer |
elessair | 0:f269e3021894 | 54 | readEP(c, &size); |
elessair | 0:f269e3021894 | 55 | for (uint32_t i = 0; i < size; i++) { |
elessair | 0:f269e3021894 | 56 | buf.queue(c[i]); |
elessair | 0:f269e3021894 | 57 | } |
elessair | 0:f269e3021894 | 58 | |
elessair | 0:f269e3021894 | 59 | //call a potential handler |
elessair | 0:f269e3021894 | 60 | rx.call(); |
elessair | 0:f269e3021894 | 61 | |
elessair | 0:f269e3021894 | 62 | return true; |
elessair | 0:f269e3021894 | 63 | } |
elessair | 0:f269e3021894 | 64 | |
elessair | 0:f269e3021894 | 65 | uint8_t USBSerial::available() { |
elessair | 0:f269e3021894 | 66 | return buf.available(); |
elessair | 0:f269e3021894 | 67 | } |