Opencv 3.1 project on GR-PEACH board
Fork of gr-peach-opencv-project by
USBHost_custom/USBHostSerial/USBHostSerial.cpp@170:54ff26da7eb6, 2017-07-04 (annotated)
- Committer:
- thedo
- Date:
- Tue Jul 04 06:23:13 2017 +0000
- Revision:
- 170:54ff26da7eb6
- Parent:
- 166:3a9487d57a5c
project opencv 3.1 on GR PEACH board, no use SD card.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thedo | 166:3a9487d57a5c | 1 | /* mbed USBHost Library |
thedo | 166:3a9487d57a5c | 2 | * Copyright (c) 2006-2013 ARM Limited |
thedo | 166:3a9487d57a5c | 3 | * |
thedo | 166:3a9487d57a5c | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
thedo | 166:3a9487d57a5c | 5 | * you may not use this file except in compliance with the License. |
thedo | 166:3a9487d57a5c | 6 | * You may obtain a copy of the License at |
thedo | 166:3a9487d57a5c | 7 | * |
thedo | 166:3a9487d57a5c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
thedo | 166:3a9487d57a5c | 9 | * |
thedo | 166:3a9487d57a5c | 10 | * Unless required by applicable law or agreed to in writing, software |
thedo | 166:3a9487d57a5c | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
thedo | 166:3a9487d57a5c | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
thedo | 166:3a9487d57a5c | 13 | * See the License for the specific language governing permissions and |
thedo | 166:3a9487d57a5c | 14 | * limitations under the License. |
thedo | 166:3a9487d57a5c | 15 | */ |
thedo | 166:3a9487d57a5c | 16 | |
thedo | 166:3a9487d57a5c | 17 | #include "USBHostSerial.h" |
thedo | 166:3a9487d57a5c | 18 | |
thedo | 166:3a9487d57a5c | 19 | #if USBHOST_SERIAL |
thedo | 166:3a9487d57a5c | 20 | |
thedo | 166:3a9487d57a5c | 21 | #include "dbg.h" |
thedo | 166:3a9487d57a5c | 22 | |
thedo | 166:3a9487d57a5c | 23 | #define CHECK_INTERFACE(cls,subcls,proto) \ |
thedo | 166:3a9487d57a5c | 24 | (((cls == 0xFF) && (subcls == 0xFF) && (proto == 0xFF)) /* QUALCOM CDC */ || \ |
thedo | 166:3a9487d57a5c | 25 | ((cls == SERIAL_CLASS) && (subcls == 0x00) && (proto == 0x00)) /* STANDARD CDC */ ) |
thedo | 166:3a9487d57a5c | 26 | |
thedo | 166:3a9487d57a5c | 27 | #if (USBHOST_SERIAL <= 1) |
thedo | 166:3a9487d57a5c | 28 | |
thedo | 166:3a9487d57a5c | 29 | USBHostSerial::USBHostSerial() |
thedo | 166:3a9487d57a5c | 30 | { |
thedo | 166:3a9487d57a5c | 31 | host = USBHost::getHostInst(); |
thedo | 166:3a9487d57a5c | 32 | ports_found = 0; |
thedo | 166:3a9487d57a5c | 33 | } |
thedo | 166:3a9487d57a5c | 34 | |
thedo | 166:3a9487d57a5c | 35 | void USBHostSerial::disconnect(void) |
thedo | 166:3a9487d57a5c | 36 | { |
thedo | 166:3a9487d57a5c | 37 | ports_found = 0; |
thedo | 166:3a9487d57a5c | 38 | dev = NULL; |
thedo | 166:3a9487d57a5c | 39 | } |
thedo | 166:3a9487d57a5c | 40 | |
thedo | 166:3a9487d57a5c | 41 | bool USBHostSerial::connect() { |
thedo | 166:3a9487d57a5c | 42 | |
thedo | 166:3a9487d57a5c | 43 | if (dev) { |
thedo | 166:3a9487d57a5c | 44 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
thedo | 166:3a9487d57a5c | 45 | USBDeviceConnected* d = host->getDevice(i); |
thedo | 166:3a9487d57a5c | 46 | if (dev == d) |
thedo | 166:3a9487d57a5c | 47 | return true; |
thedo | 166:3a9487d57a5c | 48 | } |
thedo | 166:3a9487d57a5c | 49 | disconnect(); |
thedo | 166:3a9487d57a5c | 50 | } |
thedo | 166:3a9487d57a5c | 51 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
thedo | 166:3a9487d57a5c | 52 | USBDeviceConnected* d = host->getDevice(i); |
thedo | 166:3a9487d57a5c | 53 | if (d != NULL) { |
thedo | 166:3a9487d57a5c | 54 | |
thedo | 166:3a9487d57a5c | 55 | USB_DBG("Trying to connect serial device \r\n"); |
thedo | 166:3a9487d57a5c | 56 | if(host->enumerate(d, this)) |
thedo | 166:3a9487d57a5c | 57 | break; |
thedo | 166:3a9487d57a5c | 58 | |
thedo | 166:3a9487d57a5c | 59 | USBEndpoint* bulk_in = d->getEndpoint(port_intf, BULK_ENDPOINT, IN); |
thedo | 166:3a9487d57a5c | 60 | USBEndpoint* bulk_out = d->getEndpoint(port_intf, BULK_ENDPOINT, OUT); |
thedo | 166:3a9487d57a5c | 61 | if (bulk_in && bulk_out) |
thedo | 166:3a9487d57a5c | 62 | { |
thedo | 166:3a9487d57a5c | 63 | USBHostSerialPort::connect(host,d,port_intf,bulk_in, bulk_out); |
thedo | 166:3a9487d57a5c | 64 | dev = d; |
thedo | 166:3a9487d57a5c | 65 | } |
thedo | 166:3a9487d57a5c | 66 | } |
thedo | 166:3a9487d57a5c | 67 | } |
thedo | 166:3a9487d57a5c | 68 | return dev != NULL; |
thedo | 166:3a9487d57a5c | 69 | } |
thedo | 166:3a9487d57a5c | 70 | |
thedo | 166:3a9487d57a5c | 71 | /*virtual*/ void USBHostSerial::setVidPid(uint16_t vid, uint16_t pid) |
thedo | 166:3a9487d57a5c | 72 | { |
thedo | 166:3a9487d57a5c | 73 | // we don't check VID/PID for MSD driver |
thedo | 166:3a9487d57a5c | 74 | } |
thedo | 166:3a9487d57a5c | 75 | |
thedo | 166:3a9487d57a5c | 76 | /*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 |
thedo | 166:3a9487d57a5c | 77 | { |
thedo | 166:3a9487d57a5c | 78 | if (!ports_found && |
thedo | 166:3a9487d57a5c | 79 | CHECK_INTERFACE(intf_class, intf_subclass, intf_protocol)) { |
thedo | 166:3a9487d57a5c | 80 | port_intf = intf_nb; |
thedo | 166:3a9487d57a5c | 81 | ports_found = true; |
thedo | 166:3a9487d57a5c | 82 | return true; |
thedo | 166:3a9487d57a5c | 83 | } |
thedo | 166:3a9487d57a5c | 84 | return false; |
thedo | 166:3a9487d57a5c | 85 | } |
thedo | 166:3a9487d57a5c | 86 | |
thedo | 166:3a9487d57a5c | 87 | /*virtual*/ bool USBHostSerial::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used |
thedo | 166:3a9487d57a5c | 88 | { |
thedo | 166:3a9487d57a5c | 89 | if (ports_found && (intf_nb == port_intf)) { |
thedo | 166:3a9487d57a5c | 90 | if (type == BULK_ENDPOINT) |
thedo | 166:3a9487d57a5c | 91 | return true; |
thedo | 166:3a9487d57a5c | 92 | } |
thedo | 166:3a9487d57a5c | 93 | return false; |
thedo | 166:3a9487d57a5c | 94 | } |
thedo | 166:3a9487d57a5c | 95 | |
thedo | 166:3a9487d57a5c | 96 | #else // (USBHOST_SERIAL > 1) |
thedo | 166:3a9487d57a5c | 97 | |
thedo | 166:3a9487d57a5c | 98 | //------------------------------------------------------------------------------ |
thedo | 166:3a9487d57a5c | 99 | |
thedo | 166:3a9487d57a5c | 100 | USBHostMultiSerial::USBHostMultiSerial() |
thedo | 166:3a9487d57a5c | 101 | { |
thedo | 166:3a9487d57a5c | 102 | host = USBHost::getHostInst(); |
thedo | 166:3a9487d57a5c | 103 | dev = NULL; |
thedo | 166:3a9487d57a5c | 104 | memset(ports, NULL, sizeof(ports)); |
thedo | 166:3a9487d57a5c | 105 | ports_found = 0; |
thedo | 166:3a9487d57a5c | 106 | } |
thedo | 166:3a9487d57a5c | 107 | |
thedo | 166:3a9487d57a5c | 108 | USBHostMultiSerial::~USBHostMultiSerial() |
thedo | 166:3a9487d57a5c | 109 | { |
thedo | 166:3a9487d57a5c | 110 | disconnect(); |
thedo | 166:3a9487d57a5c | 111 | } |
thedo | 166:3a9487d57a5c | 112 | |
thedo | 166:3a9487d57a5c | 113 | void USBHostMultiSerial::disconnect(void) |
thedo | 166:3a9487d57a5c | 114 | { |
thedo | 166:3a9487d57a5c | 115 | for (int port = 0; port < USBHOST_SERIAL; port ++) |
thedo | 166:3a9487d57a5c | 116 | { |
thedo | 166:3a9487d57a5c | 117 | if (ports[port]) |
thedo | 166:3a9487d57a5c | 118 | { |
thedo | 166:3a9487d57a5c | 119 | delete ports[port]; |
thedo | 166:3a9487d57a5c | 120 | ports[port] = NULL; |
thedo | 166:3a9487d57a5c | 121 | } |
thedo | 166:3a9487d57a5c | 122 | } |
thedo | 166:3a9487d57a5c | 123 | ports_found = 0; |
thedo | 166:3a9487d57a5c | 124 | dev = NULL; |
thedo | 166:3a9487d57a5c | 125 | } |
thedo | 166:3a9487d57a5c | 126 | |
thedo | 166:3a9487d57a5c | 127 | bool USBHostMultiSerial::connect() { |
thedo | 166:3a9487d57a5c | 128 | |
thedo | 166:3a9487d57a5c | 129 | if (dev) |
thedo | 166:3a9487d57a5c | 130 | { |
thedo | 166:3a9487d57a5c | 131 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) |
thedo | 166:3a9487d57a5c | 132 | { |
thedo | 166:3a9487d57a5c | 133 | USBDeviceConnected* d = host->getDevice(i); |
thedo | 166:3a9487d57a5c | 134 | if (dev == d) |
thedo | 166:3a9487d57a5c | 135 | return true; |
thedo | 166:3a9487d57a5c | 136 | } |
thedo | 166:3a9487d57a5c | 137 | disconnect(); |
thedo | 166:3a9487d57a5c | 138 | } |
thedo | 166:3a9487d57a5c | 139 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) |
thedo | 166:3a9487d57a5c | 140 | { |
thedo | 166:3a9487d57a5c | 141 | USBDeviceConnected* d = host->getDevice(i); |
thedo | 166:3a9487d57a5c | 142 | if (d != NULL) { |
thedo | 166:3a9487d57a5c | 143 | |
thedo | 166:3a9487d57a5c | 144 | USB_DBG("Trying to connect serial device \r\n"); |
thedo | 166:3a9487d57a5c | 145 | if(host->enumerate(d, this)) |
thedo | 166:3a9487d57a5c | 146 | break; |
thedo | 166:3a9487d57a5c | 147 | |
thedo | 166:3a9487d57a5c | 148 | for (int port = 0; port < ports_found; port ++) |
thedo | 166:3a9487d57a5c | 149 | { |
thedo | 166:3a9487d57a5c | 150 | USBEndpoint* bulk_in = d->getEndpoint(port_intf[port], BULK_ENDPOINT, IN); |
thedo | 166:3a9487d57a5c | 151 | USBEndpoint* bulk_out = d->getEndpoint(port_intf[port], BULK_ENDPOINT, OUT); |
thedo | 166:3a9487d57a5c | 152 | if (bulk_in && bulk_out) |
thedo | 166:3a9487d57a5c | 153 | { |
thedo | 166:3a9487d57a5c | 154 | ports[port] = new USBHostSerialPort(); |
thedo | 166:3a9487d57a5c | 155 | if (ports[port]) |
thedo | 166:3a9487d57a5c | 156 | { |
thedo | 166:3a9487d57a5c | 157 | ports[port]->connect(host,d,port_intf[port],bulk_in, bulk_out); |
thedo | 166:3a9487d57a5c | 158 | dev = d; |
thedo | 166:3a9487d57a5c | 159 | } |
thedo | 166:3a9487d57a5c | 160 | } |
thedo | 166:3a9487d57a5c | 161 | } |
thedo | 166:3a9487d57a5c | 162 | } |
thedo | 166:3a9487d57a5c | 163 | } |
thedo | 166:3a9487d57a5c | 164 | return dev != NULL; |
thedo | 166:3a9487d57a5c | 165 | } |
thedo | 166:3a9487d57a5c | 166 | |
thedo | 166:3a9487d57a5c | 167 | /*virtual*/ void USBHostMultiSerial::setVidPid(uint16_t vid, uint16_t pid) |
thedo | 166:3a9487d57a5c | 168 | { |
thedo | 166:3a9487d57a5c | 169 | // we don't check VID/PID for MSD driver |
thedo | 166:3a9487d57a5c | 170 | } |
thedo | 166:3a9487d57a5c | 171 | |
thedo | 166:3a9487d57a5c | 172 | /*virtual*/ bool USBHostMultiSerial::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 |
thedo | 166:3a9487d57a5c | 173 | { |
thedo | 166:3a9487d57a5c | 174 | if ((ports_found < USBHOST_SERIAL) && |
thedo | 166:3a9487d57a5c | 175 | CHECK_INTERFACE(intf_class, intf_subclass, intf_protocol)) { |
thedo | 166:3a9487d57a5c | 176 | port_intf[ports_found++] = intf_nb; |
thedo | 166:3a9487d57a5c | 177 | return true; |
thedo | 166:3a9487d57a5c | 178 | } |
thedo | 166:3a9487d57a5c | 179 | return false; |
thedo | 166:3a9487d57a5c | 180 | } |
thedo | 166:3a9487d57a5c | 181 | |
thedo | 166:3a9487d57a5c | 182 | /*virtual*/ bool USBHostMultiSerial::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used |
thedo | 166:3a9487d57a5c | 183 | { |
thedo | 166:3a9487d57a5c | 184 | if ((ports_found > 0) && (intf_nb == port_intf[ports_found-1])) { |
thedo | 166:3a9487d57a5c | 185 | if (type == BULK_ENDPOINT) |
thedo | 166:3a9487d57a5c | 186 | return true; |
thedo | 166:3a9487d57a5c | 187 | } |
thedo | 166:3a9487d57a5c | 188 | return false; |
thedo | 166:3a9487d57a5c | 189 | } |
thedo | 166:3a9487d57a5c | 190 | |
thedo | 166:3a9487d57a5c | 191 | #endif |
thedo | 166:3a9487d57a5c | 192 | |
thedo | 166:3a9487d57a5c | 193 | //------------------------------------------------------------------------------ |
thedo | 166:3a9487d57a5c | 194 | |
thedo | 166:3a9487d57a5c | 195 | #define SET_LINE_CODING 0x20 |
thedo | 166:3a9487d57a5c | 196 | |
thedo | 166:3a9487d57a5c | 197 | USBHostSerialPort::USBHostSerialPort() |
thedo | 166:3a9487d57a5c | 198 | { |
thedo | 166:3a9487d57a5c | 199 | p_circ_buf = new CircBufferHostSerial<uint8_t, (1024 * 32)>; |
thedo | 166:3a9487d57a5c | 200 | init(); |
thedo | 166:3a9487d57a5c | 201 | } |
thedo | 166:3a9487d57a5c | 202 | |
thedo | 166:3a9487d57a5c | 203 | USBHostSerialPort::~USBHostSerialPort() { |
thedo | 166:3a9487d57a5c | 204 | delete p_circ_buf; |
thedo | 166:3a9487d57a5c | 205 | } |
thedo | 166:3a9487d57a5c | 206 | |
thedo | 166:3a9487d57a5c | 207 | void USBHostSerialPort::init(void) |
thedo | 166:3a9487d57a5c | 208 | { |
thedo | 166:3a9487d57a5c | 209 | dev_connected = false; |
thedo | 166:3a9487d57a5c | 210 | host = NULL; |
thedo | 166:3a9487d57a5c | 211 | dev = NULL; |
thedo | 166:3a9487d57a5c | 212 | serial_intf = 0; |
thedo | 166:3a9487d57a5c | 213 | size_bulk_in = 0; |
thedo | 166:3a9487d57a5c | 214 | size_bulk_out = 0; |
thedo | 166:3a9487d57a5c | 215 | bulk_in = NULL; |
thedo | 166:3a9487d57a5c | 216 | bulk_out = NULL; |
thedo | 166:3a9487d57a5c | 217 | line_coding.baudrate = 9600; |
thedo | 166:3a9487d57a5c | 218 | line_coding.data_bits = 8; |
thedo | 166:3a9487d57a5c | 219 | line_coding.parity = None; |
thedo | 166:3a9487d57a5c | 220 | line_coding.stop_bits = 1; |
thedo | 166:3a9487d57a5c | 221 | p_circ_buf->flush(); |
thedo | 166:3a9487d57a5c | 222 | } |
thedo | 166:3a9487d57a5c | 223 | |
thedo | 166:3a9487d57a5c | 224 | void USBHostSerialPort::connect(USBHost* _host, USBDeviceConnected * _dev, |
thedo | 166:3a9487d57a5c | 225 | uint8_t _serial_intf, USBEndpoint* _bulk_in, USBEndpoint* _bulk_out) |
thedo | 166:3a9487d57a5c | 226 | { |
thedo | 166:3a9487d57a5c | 227 | host = _host; |
thedo | 166:3a9487d57a5c | 228 | dev = _dev; |
thedo | 166:3a9487d57a5c | 229 | serial_intf = _serial_intf; |
thedo | 166:3a9487d57a5c | 230 | bulk_in = _bulk_in; |
thedo | 166:3a9487d57a5c | 231 | bulk_out = _bulk_out; |
thedo | 166:3a9487d57a5c | 232 | |
thedo | 166:3a9487d57a5c | 233 | USB_INFO("New Serial device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, serial_intf); |
thedo | 166:3a9487d57a5c | 234 | dev->setName("Serial", serial_intf); |
thedo | 166:3a9487d57a5c | 235 | host->registerDriver(dev, serial_intf, this, &USBHostSerialPort::init); |
thedo | 166:3a9487d57a5c | 236 | baud(9600); |
thedo | 166:3a9487d57a5c | 237 | size_bulk_in = bulk_in->getSize(); |
thedo | 166:3a9487d57a5c | 238 | size_bulk_out = bulk_out->getSize(); |
thedo | 166:3a9487d57a5c | 239 | bulk_in->attach(this, &USBHostSerialPort::rxHandler); |
thedo | 166:3a9487d57a5c | 240 | bulk_out->attach(this, &USBHostSerialPort::txHandler); |
thedo | 166:3a9487d57a5c | 241 | host->bulkRead(dev, bulk_in, buf, size_bulk_in, false); |
thedo | 166:3a9487d57a5c | 242 | dev_connected = true; |
thedo | 166:3a9487d57a5c | 243 | } |
thedo | 166:3a9487d57a5c | 244 | |
thedo | 166:3a9487d57a5c | 245 | bool USBHostSerialPort::connected() { |
thedo | 166:3a9487d57a5c | 246 | return dev_connected; |
thedo | 166:3a9487d57a5c | 247 | } |
thedo | 166:3a9487d57a5c | 248 | |
thedo | 166:3a9487d57a5c | 249 | void USBHostSerialPort::rxHandler() { |
thedo | 166:3a9487d57a5c | 250 | if (bulk_in) { |
thedo | 166:3a9487d57a5c | 251 | int len = bulk_in->getLengthTransferred(); |
thedo | 166:3a9487d57a5c | 252 | if (bulk_in->getState() == USB_TYPE_IDLE) { |
thedo | 166:3a9487d57a5c | 253 | for (int i = 0; i < len; i++) { |
thedo | 166:3a9487d57a5c | 254 | while (p_circ_buf->isFull()) { |
thedo | 166:3a9487d57a5c | 255 | Thread::wait(1); |
thedo | 166:3a9487d57a5c | 256 | } |
thedo | 166:3a9487d57a5c | 257 | p_circ_buf->queue(buf[i]); |
thedo | 166:3a9487d57a5c | 258 | } |
thedo | 166:3a9487d57a5c | 259 | rx.call(); |
thedo | 166:3a9487d57a5c | 260 | host->bulkRead(dev, bulk_in, buf, size_bulk_in, false); |
thedo | 166:3a9487d57a5c | 261 | } |
thedo | 166:3a9487d57a5c | 262 | } |
thedo | 166:3a9487d57a5c | 263 | } |
thedo | 166:3a9487d57a5c | 264 | |
thedo | 166:3a9487d57a5c | 265 | void USBHostSerialPort::txHandler() { |
thedo | 166:3a9487d57a5c | 266 | if (bulk_out) { |
thedo | 166:3a9487d57a5c | 267 | if (bulk_out->getState() == USB_TYPE_IDLE) { |
thedo | 166:3a9487d57a5c | 268 | tx.call(); |
thedo | 166:3a9487d57a5c | 269 | } |
thedo | 166:3a9487d57a5c | 270 | } |
thedo | 166:3a9487d57a5c | 271 | } |
thedo | 166:3a9487d57a5c | 272 | |
thedo | 166:3a9487d57a5c | 273 | int USBHostSerialPort::_putc(int c) { |
thedo | 166:3a9487d57a5c | 274 | if (bulk_out) { |
thedo | 166:3a9487d57a5c | 275 | if (host->bulkWrite(dev, bulk_out, (uint8_t *)&c, 1) == USB_TYPE_OK) { |
thedo | 166:3a9487d57a5c | 276 | return 1; |
thedo | 166:3a9487d57a5c | 277 | } |
thedo | 166:3a9487d57a5c | 278 | } |
thedo | 166:3a9487d57a5c | 279 | return -1; |
thedo | 166:3a9487d57a5c | 280 | } |
thedo | 166:3a9487d57a5c | 281 | |
thedo | 166:3a9487d57a5c | 282 | void USBHostSerialPort::baud(int baudrate) { |
thedo | 166:3a9487d57a5c | 283 | line_coding.baudrate = baudrate; |
thedo | 166:3a9487d57a5c | 284 | format(line_coding.data_bits, (Parity)line_coding.parity, line_coding.stop_bits); |
thedo | 166:3a9487d57a5c | 285 | } |
thedo | 166:3a9487d57a5c | 286 | |
thedo | 166:3a9487d57a5c | 287 | void USBHostSerialPort::format(int bits, Parity parity, int stop_bits) { |
thedo | 166:3a9487d57a5c | 288 | line_coding.data_bits = bits; |
thedo | 166:3a9487d57a5c | 289 | line_coding.parity = parity; |
thedo | 166:3a9487d57a5c | 290 | line_coding.stop_bits = (stop_bits == 1) ? 0 : 2; |
thedo | 166:3a9487d57a5c | 291 | |
thedo | 166:3a9487d57a5c | 292 | // set line coding |
thedo | 166:3a9487d57a5c | 293 | host->controlWrite( dev, |
thedo | 166:3a9487d57a5c | 294 | USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS, |
thedo | 166:3a9487d57a5c | 295 | SET_LINE_CODING, |
thedo | 166:3a9487d57a5c | 296 | 0, serial_intf, (uint8_t *)&line_coding, 7); |
thedo | 166:3a9487d57a5c | 297 | } |
thedo | 166:3a9487d57a5c | 298 | |
thedo | 166:3a9487d57a5c | 299 | int USBHostSerialPort::_getc() { |
thedo | 166:3a9487d57a5c | 300 | uint8_t c = 0; |
thedo | 166:3a9487d57a5c | 301 | if (bulk_in == NULL) { |
thedo | 166:3a9487d57a5c | 302 | init(); |
thedo | 166:3a9487d57a5c | 303 | return -1; |
thedo | 166:3a9487d57a5c | 304 | } |
thedo | 166:3a9487d57a5c | 305 | while (p_circ_buf->isEmpty()) { |
thedo | 166:3a9487d57a5c | 306 | if (dev_connected == false) { |
thedo | 166:3a9487d57a5c | 307 | return -1; |
thedo | 166:3a9487d57a5c | 308 | } |
thedo | 166:3a9487d57a5c | 309 | Thread::wait(1); |
thedo | 166:3a9487d57a5c | 310 | } |
thedo | 166:3a9487d57a5c | 311 | p_circ_buf->dequeue(&c); |
thedo | 166:3a9487d57a5c | 312 | return c; |
thedo | 166:3a9487d57a5c | 313 | } |
thedo | 166:3a9487d57a5c | 314 | |
thedo | 166:3a9487d57a5c | 315 | int USBHostSerialPort::writeBuf(const char* b, int s) { |
thedo | 166:3a9487d57a5c | 316 | int i; |
thedo | 166:3a9487d57a5c | 317 | int c = 0; |
thedo | 166:3a9487d57a5c | 318 | if (bulk_out) { |
thedo | 166:3a9487d57a5c | 319 | while (s > 0) { |
thedo | 166:3a9487d57a5c | 320 | if (dev_connected == false) { |
thedo | 166:3a9487d57a5c | 321 | break; |
thedo | 166:3a9487d57a5c | 322 | } |
thedo | 166:3a9487d57a5c | 323 | i = (s < size_bulk_out) ? s : size_bulk_out; |
thedo | 166:3a9487d57a5c | 324 | if (host->bulkWrite(dev, bulk_out, (uint8_t *)(b+c), i) == USB_TYPE_OK) { |
thedo | 166:3a9487d57a5c | 325 | c += i; |
thedo | 166:3a9487d57a5c | 326 | s -= i; |
thedo | 166:3a9487d57a5c | 327 | } |
thedo | 166:3a9487d57a5c | 328 | } |
thedo | 166:3a9487d57a5c | 329 | } |
thedo | 166:3a9487d57a5c | 330 | return c; |
thedo | 166:3a9487d57a5c | 331 | } |
thedo | 166:3a9487d57a5c | 332 | |
thedo | 166:3a9487d57a5c | 333 | int USBHostSerialPort::readBuf(char* b, int s, int timeout) { |
thedo | 166:3a9487d57a5c | 334 | int i = 0; |
thedo | 166:3a9487d57a5c | 335 | |
thedo | 166:3a9487d57a5c | 336 | if (bulk_in) { |
thedo | 166:3a9487d57a5c | 337 | for (i = 0; i < s; i++) { |
thedo | 166:3a9487d57a5c | 338 | while ((p_circ_buf->isEmpty()) && (dev_connected)) { |
thedo | 166:3a9487d57a5c | 339 | if (timeout == 0) { |
thedo | 166:3a9487d57a5c | 340 | break; |
thedo | 166:3a9487d57a5c | 341 | } else { |
thedo | 166:3a9487d57a5c | 342 | if (timeout > 0) { |
thedo | 166:3a9487d57a5c | 343 | timeout--; |
thedo | 166:3a9487d57a5c | 344 | } |
thedo | 166:3a9487d57a5c | 345 | Thread::wait(1); |
thedo | 166:3a9487d57a5c | 346 | } |
thedo | 166:3a9487d57a5c | 347 | } |
thedo | 166:3a9487d57a5c | 348 | if (!p_circ_buf->dequeue((uint8_t *)&b[i])) { |
thedo | 166:3a9487d57a5c | 349 | break; |
thedo | 166:3a9487d57a5c | 350 | } |
thedo | 166:3a9487d57a5c | 351 | } |
thedo | 166:3a9487d57a5c | 352 | } |
thedo | 166:3a9487d57a5c | 353 | return i; |
thedo | 166:3a9487d57a5c | 354 | } |
thedo | 166:3a9487d57a5c | 355 | |
thedo | 166:3a9487d57a5c | 356 | uint32_t USBHostSerialPort::available() { |
thedo | 166:3a9487d57a5c | 357 | return p_circ_buf->available(); |
thedo | 166:3a9487d57a5c | 358 | } |
thedo | 166:3a9487d57a5c | 359 | |
thedo | 166:3a9487d57a5c | 360 | #endif |
thedo | 166:3a9487d57a5c | 361 | |
thedo | 166:3a9487d57a5c | 362 |