2018.07.26

Dependencies:   FATFileSystem3 mbed-rtos

Fork of USBHost by mbed official

Committer:
sayzyas
Date:
Thu Jul 26 00:20:32 2018 +0000
Revision:
43:1675750cca08
2018.07.26

Who changed what in which revision?

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