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