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