Working version on STM32F411. Test for CLI
Dependencies: FATFileSystem mbed-rtos mbed
Fork of USBHost by
USBHostSerial/USBHostSerial.cpp@0:a554658735bf, 2013-03-06 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Mar 06 16:27:14 2013 +0000
- Revision:
- 0:a554658735bf
- Child:
- 4:b320d68e98e7
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:a554658735bf | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
mbed_official | 0:a554658735bf | 2 | * |
mbed_official | 0:a554658735bf | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
mbed_official | 0:a554658735bf | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
mbed_official | 0:a554658735bf | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
mbed_official | 0:a554658735bf | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
mbed_official | 0:a554658735bf | 7 | * Software is furnished to do so, subject to the following conditions: |
mbed_official | 0:a554658735bf | 8 | * |
mbed_official | 0:a554658735bf | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
mbed_official | 0:a554658735bf | 10 | * substantial portions of the Software. |
mbed_official | 0:a554658735bf | 11 | * |
mbed_official | 0:a554658735bf | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
mbed_official | 0:a554658735bf | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
mbed_official | 0:a554658735bf | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
mbed_official | 0:a554658735bf | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbed_official | 0:a554658735bf | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
mbed_official | 0:a554658735bf | 17 | */ |
mbed_official | 0:a554658735bf | 18 | |
mbed_official | 0:a554658735bf | 19 | #include "USBHostSerial.h" |
mbed_official | 0:a554658735bf | 20 | |
mbed_official | 0:a554658735bf | 21 | #if USBHOST_SERIAL |
mbed_official | 0:a554658735bf | 22 | |
mbed_official | 0:a554658735bf | 23 | #include "dbg.h" |
mbed_official | 0:a554658735bf | 24 | |
mbed_official | 0:a554658735bf | 25 | USBHostSerial::USBHostSerial(): circ_buf() { |
mbed_official | 0:a554658735bf | 26 | host = USBHost::getHostInst(); |
mbed_official | 0:a554658735bf | 27 | size_bulk_in = 0; |
mbed_official | 0:a554658735bf | 28 | size_bulk_out = 0; |
mbed_official | 0:a554658735bf | 29 | init(); |
mbed_official | 0:a554658735bf | 30 | } |
mbed_official | 0:a554658735bf | 31 | |
mbed_official | 0:a554658735bf | 32 | void USBHostSerial::init() { |
mbed_official | 0:a554658735bf | 33 | dev = NULL; |
mbed_official | 0:a554658735bf | 34 | bulk_in = NULL; |
mbed_official | 0:a554658735bf | 35 | bulk_out = NULL; |
mbed_official | 0:a554658735bf | 36 | dev_connected = false; |
mbed_official | 0:a554658735bf | 37 | serial_intf = -1; |
mbed_official | 0:a554658735bf | 38 | serial_device_found = false; |
mbed_official | 0:a554658735bf | 39 | } |
mbed_official | 0:a554658735bf | 40 | |
mbed_official | 0:a554658735bf | 41 | bool USBHostSerial::connected() |
mbed_official | 0:a554658735bf | 42 | { |
mbed_official | 0:a554658735bf | 43 | return dev_connected; |
mbed_official | 0:a554658735bf | 44 | } |
mbed_official | 0:a554658735bf | 45 | |
mbed_official | 0:a554658735bf | 46 | bool USBHostSerial::connect() { |
mbed_official | 0:a554658735bf | 47 | |
mbed_official | 0:a554658735bf | 48 | if (dev_connected) { |
mbed_official | 0:a554658735bf | 49 | return true; |
mbed_official | 0:a554658735bf | 50 | } |
mbed_official | 0:a554658735bf | 51 | for (int i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
mbed_official | 0:a554658735bf | 52 | if ((dev = host->getDevice(i)) != NULL) { |
mbed_official | 0:a554658735bf | 53 | |
mbed_official | 0:a554658735bf | 54 | if(host->enumerate(dev, this)) |
mbed_official | 0:a554658735bf | 55 | break; |
mbed_official | 0:a554658735bf | 56 | |
mbed_official | 0:a554658735bf | 57 | if (serial_device_found) { |
mbed_official | 0:a554658735bf | 58 | bulk_in = dev->getEndpoint(serial_intf, BULK_ENDPOINT, IN); |
mbed_official | 0:a554658735bf | 59 | bulk_out = dev->getEndpoint(serial_intf, BULK_ENDPOINT, OUT); |
mbed_official | 0:a554658735bf | 60 | |
mbed_official | 0:a554658735bf | 61 | if (!bulk_in || !bulk_out) |
mbed_official | 0:a554658735bf | 62 | break; |
mbed_official | 0:a554658735bf | 63 | |
mbed_official | 0:a554658735bf | 64 | USB_INFO("New Serial device: VID:%04x PID:%04x [dev: %p]", dev->getVid(), dev->getPid(), dev); |
mbed_official | 0:a554658735bf | 65 | dev->setName("Serial"); |
mbed_official | 0:a554658735bf | 66 | host->registerDriver(dev, serial_intf, this, &USBHostSerial::init); |
mbed_official | 0:a554658735bf | 67 | |
mbed_official | 0:a554658735bf | 68 | size_bulk_in = bulk_in->getSize(); |
mbed_official | 0:a554658735bf | 69 | size_bulk_out = bulk_out->getSize(); |
mbed_official | 0:a554658735bf | 70 | |
mbed_official | 0:a554658735bf | 71 | bulk_in->attach(this, &USBHostSerial::rxHandler); |
mbed_official | 0:a554658735bf | 72 | bulk_out->attach(this, &USBHostSerial::txHandler); |
mbed_official | 0:a554658735bf | 73 | |
mbed_official | 0:a554658735bf | 74 | host->bulkRead(dev, bulk_in, buf, size_bulk_in, false); |
mbed_official | 0:a554658735bf | 75 | dev_connected = true; |
mbed_official | 0:a554658735bf | 76 | return true; |
mbed_official | 0:a554658735bf | 77 | } |
mbed_official | 0:a554658735bf | 78 | } |
mbed_official | 0:a554658735bf | 79 | } |
mbed_official | 0:a554658735bf | 80 | init(); |
mbed_official | 0:a554658735bf | 81 | return false; |
mbed_official | 0:a554658735bf | 82 | } |
mbed_official | 0:a554658735bf | 83 | |
mbed_official | 0:a554658735bf | 84 | void USBHostSerial::rxHandler() { |
mbed_official | 0:a554658735bf | 85 | if (bulk_in) { |
mbed_official | 0:a554658735bf | 86 | int len = bulk_in->getLengthTransferred(); |
mbed_official | 0:a554658735bf | 87 | if (bulk_in->getState() == USB_TYPE_IDLE) { |
mbed_official | 0:a554658735bf | 88 | for (int i = 0; i < len; i++) { |
mbed_official | 0:a554658735bf | 89 | circ_buf.queue(buf[i]); |
mbed_official | 0:a554658735bf | 90 | } |
mbed_official | 0:a554658735bf | 91 | rx.call(); |
mbed_official | 0:a554658735bf | 92 | } |
mbed_official | 0:a554658735bf | 93 | host->bulkRead(dev, bulk_in, buf, size_bulk_in, false); |
mbed_official | 0:a554658735bf | 94 | } |
mbed_official | 0:a554658735bf | 95 | } |
mbed_official | 0:a554658735bf | 96 | |
mbed_official | 0:a554658735bf | 97 | void USBHostSerial::txHandler() { |
mbed_official | 0:a554658735bf | 98 | if (bulk_out) { |
mbed_official | 0:a554658735bf | 99 | if (bulk_out->getState() == USB_TYPE_IDLE) { |
mbed_official | 0:a554658735bf | 100 | tx.call(); |
mbed_official | 0:a554658735bf | 101 | } |
mbed_official | 0:a554658735bf | 102 | } |
mbed_official | 0:a554658735bf | 103 | } |
mbed_official | 0:a554658735bf | 104 | |
mbed_official | 0:a554658735bf | 105 | int USBHostSerial::_putc(int c) { |
mbed_official | 0:a554658735bf | 106 | if (bulk_out) { |
mbed_official | 0:a554658735bf | 107 | if (host->bulkWrite(dev, bulk_out, (uint8_t *)&c, 1) == USB_TYPE_OK) { |
mbed_official | 0:a554658735bf | 108 | return 1; |
mbed_official | 0:a554658735bf | 109 | } |
mbed_official | 0:a554658735bf | 110 | } |
mbed_official | 0:a554658735bf | 111 | return -1; |
mbed_official | 0:a554658735bf | 112 | } |
mbed_official | 0:a554658735bf | 113 | |
mbed_official | 0:a554658735bf | 114 | |
mbed_official | 0:a554658735bf | 115 | int USBHostSerial::_getc() { |
mbed_official | 0:a554658735bf | 116 | uint8_t c = 0; |
mbed_official | 0:a554658735bf | 117 | while ((bulk_in != NULL) && (circ_buf.isEmpty())); |
mbed_official | 0:a554658735bf | 118 | if (bulk_in == NULL) { |
mbed_official | 0:a554658735bf | 119 | return -1; |
mbed_official | 0:a554658735bf | 120 | } |
mbed_official | 0:a554658735bf | 121 | circ_buf.dequeue(&c); |
mbed_official | 0:a554658735bf | 122 | return c; |
mbed_official | 0:a554658735bf | 123 | } |
mbed_official | 0:a554658735bf | 124 | |
mbed_official | 0:a554658735bf | 125 | |
mbed_official | 0:a554658735bf | 126 | uint8_t USBHostSerial::available() { |
mbed_official | 0:a554658735bf | 127 | return circ_buf.available(); |
mbed_official | 0:a554658735bf | 128 | } |
mbed_official | 0:a554658735bf | 129 | |
mbed_official | 0:a554658735bf | 130 | /*virtual*/ void USBHostSerial::setVidPid(uint16_t vid, uint16_t pid) |
mbed_official | 0:a554658735bf | 131 | { |
mbed_official | 0:a554658735bf | 132 | // we don't check VID/PID for MSD driver |
mbed_official | 0:a554658735bf | 133 | } |
mbed_official | 0:a554658735bf | 134 | |
mbed_official | 0:a554658735bf | 135 | /*virtual*/ bool USBHostSerial::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed |
mbed_official | 0:a554658735bf | 136 | { |
mbed_official | 0:a554658735bf | 137 | if ((serial_intf == -1) && |
mbed_official | 0:a554658735bf | 138 | (intf_class == SERIAL_CLASS) && |
mbed_official | 0:a554658735bf | 139 | (intf_subclass == 0x00) && |
mbed_official | 0:a554658735bf | 140 | (intf_protocol == 0x00)) { |
mbed_official | 0:a554658735bf | 141 | serial_intf = intf_nb; |
mbed_official | 0:a554658735bf | 142 | return true; |
mbed_official | 0:a554658735bf | 143 | } |
mbed_official | 0:a554658735bf | 144 | return false; |
mbed_official | 0:a554658735bf | 145 | } |
mbed_official | 0:a554658735bf | 146 | |
mbed_official | 0:a554658735bf | 147 | /*virtual*/ bool USBHostSerial::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used |
mbed_official | 0:a554658735bf | 148 | { |
mbed_official | 0:a554658735bf | 149 | if (intf_nb == serial_intf) { |
mbed_official | 0:a554658735bf | 150 | if (type == BULK_ENDPOINT) { |
mbed_official | 0:a554658735bf | 151 | serial_device_found = true; |
mbed_official | 0:a554658735bf | 152 | return true; |
mbed_official | 0:a554658735bf | 153 | } |
mbed_official | 0:a554658735bf | 154 | } |
mbed_official | 0:a554658735bf | 155 | return false; |
mbed_official | 0:a554658735bf | 156 | } |
mbed_official | 0:a554658735bf | 157 | |
mbed_official | 0:a554658735bf | 158 | #endif |
mbed_official | 0:a554658735bf | 159 | |
mbed_official | 0:a554658735bf | 160 |