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