Support MJPEG Video Demo
Dependents: RGA-MJPEG_VideoDemo
Fork of USBHost_custom by
USBHostSerial/USBHostSerial.cpp@41:d46c5a2ffbe2, 2017-11-01 (annotated)
- Committer:
- zkimike
- Date:
- Wed Nov 01 20:55:45 2017 +0000
- Revision:
- 41:d46c5a2ffbe2
- Parent:
- 40:cdc0d2ab4678
In crease baud rate
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 | 24:868cbfe611a7 | 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 | } |
mbed_official | 0:a554658735bf | 34 | |
mbed_official | 19:bd46ea19486b | 35 | void USBHostSerial::disconnect(void) |
mbed_official | 19:bd46ea19486b | 36 | { |
mbed_official | 19:bd46ea19486b | 37 | ports_found = 0; |
mbed_official | 19:bd46ea19486b | 38 | dev = NULL; |
mbed_official | 19:bd46ea19486b | 39 | } |
mbed_official | 19:bd46ea19486b | 40 | |
mbed_official | 0:a554658735bf | 41 | bool USBHostSerial::connect() { |
mbed_official | 0:a554658735bf | 42 | |
dkato | 36:f8c0979c2814 | 43 | if (dev) { |
dkato | 36:f8c0979c2814 | 44 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
mbed_official | 19:bd46ea19486b | 45 | USBDeviceConnected* d = host->getDevice(i); |
mbed_official | 19:bd46ea19486b | 46 | if (dev == d) |
mbed_official | 19:bd46ea19486b | 47 | return true; |
mbed_official | 19:bd46ea19486b | 48 | } |
mbed_official | 19:bd46ea19486b | 49 | disconnect(); |
mbed_official | 0:a554658735bf | 50 | } |
dkato | 36:f8c0979c2814 | 51 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
mbed_official | 19:bd46ea19486b | 52 | USBDeviceConnected* d = host->getDevice(i); |
mbed_official | 19:bd46ea19486b | 53 | if (d != NULL) { |
mbed_official | 24:868cbfe611a7 | 54 | |
mbed_official | 19:bd46ea19486b | 55 | USB_DBG("Trying to connect serial device \r\n"); |
mbed_official | 19:bd46ea19486b | 56 | if(host->enumerate(d, this)) |
mbed_official | 0:a554658735bf | 57 | break; |
mbed_official | 24:868cbfe611a7 | 58 | |
mbed_official | 19:bd46ea19486b | 59 | USBEndpoint* bulk_in = d->getEndpoint(port_intf, BULK_ENDPOINT, IN); |
mbed_official | 19:bd46ea19486b | 60 | USBEndpoint* bulk_out = d->getEndpoint(port_intf, BULK_ENDPOINT, OUT); |
mbed_official | 19:bd46ea19486b | 61 | if (bulk_in && bulk_out) |
mbed_official | 19:bd46ea19486b | 62 | { |
mbed_official | 19:bd46ea19486b | 63 | USBHostSerialPort::connect(host,d,port_intf,bulk_in, bulk_out); |
mbed_official | 19:bd46ea19486b | 64 | dev = d; |
mbed_official | 0:a554658735bf | 65 | } |
mbed_official | 0:a554658735bf | 66 | } |
mbed_official | 0:a554658735bf | 67 | } |
mbed_official | 19:bd46ea19486b | 68 | return dev != NULL; |
mbed_official | 19:bd46ea19486b | 69 | } |
mbed_official | 19:bd46ea19486b | 70 | |
mbed_official | 19:bd46ea19486b | 71 | /*virtual*/ void USBHostSerial::setVidPid(uint16_t vid, uint16_t pid) |
mbed_official | 19:bd46ea19486b | 72 | { |
mbed_official | 19:bd46ea19486b | 73 | // we don't check VID/PID for MSD driver |
mbed_official | 19:bd46ea19486b | 74 | } |
mbed_official | 19:bd46ea19486b | 75 | |
mbed_official | 19:bd46ea19486b | 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 |
mbed_official | 19:bd46ea19486b | 77 | { |
mbed_official | 24:868cbfe611a7 | 78 | if (!ports_found && |
mbed_official | 19:bd46ea19486b | 79 | CHECK_INTERFACE(intf_class, intf_subclass, intf_protocol)) { |
mbed_official | 19:bd46ea19486b | 80 | port_intf = intf_nb; |
mbed_official | 19:bd46ea19486b | 81 | ports_found = true; |
mbed_official | 19:bd46ea19486b | 82 | return true; |
mbed_official | 19:bd46ea19486b | 83 | } |
mbed_official | 19:bd46ea19486b | 84 | return false; |
mbed_official | 19:bd46ea19486b | 85 | } |
mbed_official | 19:bd46ea19486b | 86 | |
mbed_official | 19:bd46ea19486b | 87 | /*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 | 88 | { |
mbed_official | 19:bd46ea19486b | 89 | if (ports_found && (intf_nb == port_intf)) { |
mbed_official | 24:868cbfe611a7 | 90 | if (type == BULK_ENDPOINT) |
mbed_official | 19:bd46ea19486b | 91 | return true; |
mbed_official | 19:bd46ea19486b | 92 | } |
mbed_official | 0:a554658735bf | 93 | return false; |
mbed_official | 0:a554658735bf | 94 | } |
mbed_official | 0:a554658735bf | 95 | |
mbed_official | 19:bd46ea19486b | 96 | #else // (USBHOST_SERIAL > 1) |
mbed_official | 19:bd46ea19486b | 97 | |
mbed_official | 19:bd46ea19486b | 98 | //------------------------------------------------------------------------------ |
mbed_official | 19:bd46ea19486b | 99 | |
mbed_official | 24:868cbfe611a7 | 100 | USBHostMultiSerial::USBHostMultiSerial() |
mbed_official | 19:bd46ea19486b | 101 | { |
mbed_official | 19:bd46ea19486b | 102 | host = USBHost::getHostInst(); |
mbed_official | 19:bd46ea19486b | 103 | dev = NULL; |
mbed_official | 19:bd46ea19486b | 104 | memset(ports, NULL, sizeof(ports)); |
mbed_official | 19:bd46ea19486b | 105 | ports_found = 0; |
mbed_official | 19:bd46ea19486b | 106 | } |
mbed_official | 19:bd46ea19486b | 107 | |
mbed_official | 19:bd46ea19486b | 108 | USBHostMultiSerial::~USBHostMultiSerial() |
mbed_official | 19:bd46ea19486b | 109 | { |
mbed_official | 19:bd46ea19486b | 110 | disconnect(); |
mbed_official | 19:bd46ea19486b | 111 | } |
mbed_official | 19:bd46ea19486b | 112 | |
mbed_official | 19:bd46ea19486b | 113 | void USBHostMultiSerial::disconnect(void) |
mbed_official | 19:bd46ea19486b | 114 | { |
mbed_official | 24:868cbfe611a7 | 115 | for (int port = 0; port < USBHOST_SERIAL; port ++) |
mbed_official | 19:bd46ea19486b | 116 | { |
mbed_official | 19:bd46ea19486b | 117 | if (ports[port]) |
mbed_official | 19:bd46ea19486b | 118 | { |
mbed_official | 19:bd46ea19486b | 119 | delete ports[port]; |
mbed_official | 19:bd46ea19486b | 120 | ports[port] = NULL; |
mbed_official | 19:bd46ea19486b | 121 | } |
mbed_official | 19:bd46ea19486b | 122 | } |
mbed_official | 19:bd46ea19486b | 123 | ports_found = 0; |
mbed_official | 19:bd46ea19486b | 124 | dev = NULL; |
mbed_official | 19:bd46ea19486b | 125 | } |
mbed_official | 19:bd46ea19486b | 126 | |
mbed_official | 19:bd46ea19486b | 127 | bool USBHostMultiSerial::connect() { |
mbed_official | 19:bd46ea19486b | 128 | |
mbed_official | 24:868cbfe611a7 | 129 | if (dev) |
mbed_official | 19:bd46ea19486b | 130 | { |
mbed_official | 24:868cbfe611a7 | 131 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) |
mbed_official | 19:bd46ea19486b | 132 | { |
mbed_official | 19:bd46ea19486b | 133 | USBDeviceConnected* d = host->getDevice(i); |
mbed_official | 19:bd46ea19486b | 134 | if (dev == d) |
mbed_official | 19:bd46ea19486b | 135 | return true; |
mbed_official | 19:bd46ea19486b | 136 | } |
mbed_official | 19:bd46ea19486b | 137 | disconnect(); |
mbed_official | 19:bd46ea19486b | 138 | } |
mbed_official | 24:868cbfe611a7 | 139 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) |
mbed_official | 19:bd46ea19486b | 140 | { |
mbed_official | 19:bd46ea19486b | 141 | USBDeviceConnected* d = host->getDevice(i); |
mbed_official | 19:bd46ea19486b | 142 | if (d != NULL) { |
mbed_official | 24:868cbfe611a7 | 143 | |
mbed_official | 19:bd46ea19486b | 144 | USB_DBG("Trying to connect serial device \r\n"); |
mbed_official | 19:bd46ea19486b | 145 | if(host->enumerate(d, this)) |
mbed_official | 19:bd46ea19486b | 146 | break; |
mbed_official | 24:868cbfe611a7 | 147 | |
mbed_official | 24:868cbfe611a7 | 148 | for (int port = 0; port < ports_found; port ++) |
mbed_official | 19:bd46ea19486b | 149 | { |
mbed_official | 19:bd46ea19486b | 150 | USBEndpoint* bulk_in = d->getEndpoint(port_intf[port], BULK_ENDPOINT, IN); |
mbed_official | 19:bd46ea19486b | 151 | USBEndpoint* bulk_out = d->getEndpoint(port_intf[port], BULK_ENDPOINT, OUT); |
mbed_official | 19:bd46ea19486b | 152 | if (bulk_in && bulk_out) |
mbed_official | 19:bd46ea19486b | 153 | { |
mbed_official | 19:bd46ea19486b | 154 | ports[port] = new USBHostSerialPort(); |
mbed_official | 19:bd46ea19486b | 155 | if (ports[port]) |
mbed_official | 19:bd46ea19486b | 156 | { |
mbed_official | 19:bd46ea19486b | 157 | ports[port]->connect(host,d,port_intf[port],bulk_in, bulk_out); |
mbed_official | 19:bd46ea19486b | 158 | dev = d; |
mbed_official | 19:bd46ea19486b | 159 | } |
mbed_official | 19:bd46ea19486b | 160 | } |
mbed_official | 19:bd46ea19486b | 161 | } |
mbed_official | 19:bd46ea19486b | 162 | } |
mbed_official | 19:bd46ea19486b | 163 | } |
mbed_official | 19:bd46ea19486b | 164 | return dev != NULL; |
mbed_official | 19:bd46ea19486b | 165 | } |
mbed_official | 19:bd46ea19486b | 166 | |
mbed_official | 19:bd46ea19486b | 167 | /*virtual*/ void USBHostMultiSerial::setVidPid(uint16_t vid, uint16_t pid) |
mbed_official | 19:bd46ea19486b | 168 | { |
mbed_official | 19:bd46ea19486b | 169 | // we don't check VID/PID for MSD driver |
mbed_official | 19:bd46ea19486b | 170 | } |
mbed_official | 19:bd46ea19486b | 171 | |
mbed_official | 19:bd46ea19486b | 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 |
mbed_official | 19:bd46ea19486b | 173 | { |
mbed_official | 24:868cbfe611a7 | 174 | if ((ports_found < USBHOST_SERIAL) && |
mbed_official | 19:bd46ea19486b | 175 | CHECK_INTERFACE(intf_class, intf_subclass, intf_protocol)) { |
mbed_official | 19:bd46ea19486b | 176 | port_intf[ports_found++] = intf_nb; |
mbed_official | 19:bd46ea19486b | 177 | return true; |
mbed_official | 19:bd46ea19486b | 178 | } |
mbed_official | 19:bd46ea19486b | 179 | return false; |
mbed_official | 19:bd46ea19486b | 180 | } |
mbed_official | 19:bd46ea19486b | 181 | |
mbed_official | 19:bd46ea19486b | 182 | /*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 | 183 | { |
mbed_official | 19:bd46ea19486b | 184 | if ((ports_found > 0) && (intf_nb == port_intf[ports_found-1])) { |
mbed_official | 24:868cbfe611a7 | 185 | if (type == BULK_ENDPOINT) |
mbed_official | 19:bd46ea19486b | 186 | return true; |
mbed_official | 19:bd46ea19486b | 187 | } |
mbed_official | 19:bd46ea19486b | 188 | return false; |
mbed_official | 19:bd46ea19486b | 189 | } |
mbed_official | 19:bd46ea19486b | 190 | |
mbed_official | 19:bd46ea19486b | 191 | #endif |
mbed_official | 19:bd46ea19486b | 192 | |
mbed_official | 19:bd46ea19486b | 193 | //------------------------------------------------------------------------------ |
mbed_official | 19:bd46ea19486b | 194 | |
mbed_official | 19:bd46ea19486b | 195 | #define SET_LINE_CODING 0x20 |
mbed_official | 19:bd46ea19486b | 196 | |
dkato | 38:dd168a3e4194 | 197 | USBHostSerialPort::USBHostSerialPort() |
mbed_official | 19:bd46ea19486b | 198 | { |
dkato | 38:dd168a3e4194 | 199 | p_circ_buf = new CircBufferHostSerial<uint8_t, (1024 * 32)>; |
mbed_official | 19:bd46ea19486b | 200 | init(); |
mbed_official | 19:bd46ea19486b | 201 | } |
mbed_official | 19:bd46ea19486b | 202 | |
dkato | 38:dd168a3e4194 | 203 | USBHostSerialPort::~USBHostSerialPort() { |
dkato | 38:dd168a3e4194 | 204 | delete p_circ_buf; |
dkato | 38:dd168a3e4194 | 205 | } |
dkato | 38:dd168a3e4194 | 206 | |
mbed_official | 19:bd46ea19486b | 207 | void USBHostSerialPort::init(void) |
mbed_official | 19:bd46ea19486b | 208 | { |
dkato | 36:f8c0979c2814 | 209 | dev_connected = false; |
mbed_official | 19:bd46ea19486b | 210 | host = NULL; |
mbed_official | 19:bd46ea19486b | 211 | dev = NULL; |
dkato | 36:f8c0979c2814 | 212 | serial_intf = 0; |
mbed_official | 19:bd46ea19486b | 213 | size_bulk_in = 0; |
mbed_official | 19:bd46ea19486b | 214 | size_bulk_out = 0; |
mbed_official | 19:bd46ea19486b | 215 | bulk_in = NULL; |
mbed_official | 19:bd46ea19486b | 216 | bulk_out = NULL; |
mbed_official | 19:bd46ea19486b | 217 | line_coding.baudrate = 9600; |
mbed_official | 19:bd46ea19486b | 218 | line_coding.data_bits = 8; |
mbed_official | 19:bd46ea19486b | 219 | line_coding.parity = None; |
mbed_official | 19:bd46ea19486b | 220 | line_coding.stop_bits = 1; |
dkato | 38:dd168a3e4194 | 221 | p_circ_buf->flush(); |
mbed_official | 19:bd46ea19486b | 222 | } |
mbed_official | 19:bd46ea19486b | 223 | |
mbed_official | 24:868cbfe611a7 | 224 | void USBHostSerialPort::connect(USBHost* _host, USBDeviceConnected * _dev, |
mbed_official | 19:bd46ea19486b | 225 | uint8_t _serial_intf, USBEndpoint* _bulk_in, USBEndpoint* _bulk_out) |
mbed_official | 19:bd46ea19486b | 226 | { |
mbed_official | 19:bd46ea19486b | 227 | host = _host; |
mbed_official | 19:bd46ea19486b | 228 | dev = _dev; |
mbed_official | 19:bd46ea19486b | 229 | serial_intf = _serial_intf; |
mbed_official | 19:bd46ea19486b | 230 | bulk_in = _bulk_in; |
mbed_official | 19:bd46ea19486b | 231 | bulk_out = _bulk_out; |
mbed_official | 24:868cbfe611a7 | 232 | |
mbed_official | 19:bd46ea19486b | 233 | USB_INFO("New Serial device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, serial_intf); |
mbed_official | 19:bd46ea19486b | 234 | dev->setName("Serial", serial_intf); |
mbed_official | 19:bd46ea19486b | 235 | host->registerDriver(dev, serial_intf, this, &USBHostSerialPort::init); |
mbed_official | 27:4206883f4cb7 | 236 | baud(9600); |
mbed_official | 19:bd46ea19486b | 237 | size_bulk_in = bulk_in->getSize(); |
mbed_official | 19:bd46ea19486b | 238 | size_bulk_out = bulk_out->getSize(); |
mbed_official | 19:bd46ea19486b | 239 | bulk_in->attach(this, &USBHostSerialPort::rxHandler); |
mbed_official | 19:bd46ea19486b | 240 | bulk_out->attach(this, &USBHostSerialPort::txHandler); |
mbed_official | 19:bd46ea19486b | 241 | host->bulkRead(dev, bulk_in, buf, size_bulk_in, false); |
dkato | 36:f8c0979c2814 | 242 | dev_connected = true; |
dkato | 36:f8c0979c2814 | 243 | } |
dkato | 36:f8c0979c2814 | 244 | |
dkato | 36:f8c0979c2814 | 245 | bool USBHostSerialPort::connected() { |
dkato | 36:f8c0979c2814 | 246 | return dev_connected; |
mbed_official | 19:bd46ea19486b | 247 | } |
mbed_official | 19:bd46ea19486b | 248 | |
mbed_official | 19:bd46ea19486b | 249 | void USBHostSerialPort::rxHandler() { |
mbed_official | 0:a554658735bf | 250 | if (bulk_in) { |
mbed_official | 0:a554658735bf | 251 | int len = bulk_in->getLengthTransferred(); |
mbed_official | 0:a554658735bf | 252 | if (bulk_in->getState() == USB_TYPE_IDLE) { |
mbed_official | 0:a554658735bf | 253 | for (int i = 0; i < len; i++) { |
dkato | 38:dd168a3e4194 | 254 | while (p_circ_buf->isFull()) { |
dkato | 36:f8c0979c2814 | 255 | Thread::wait(1); |
dkato | 36:f8c0979c2814 | 256 | } |
dkato | 38:dd168a3e4194 | 257 | p_circ_buf->queue(buf[i]); |
mbed_official | 0:a554658735bf | 258 | } |
mbed_official | 0:a554658735bf | 259 | rx.call(); |
samux | 4:b320d68e98e7 | 260 | host->bulkRead(dev, bulk_in, buf, size_bulk_in, false); |
mbed_official | 0:a554658735bf | 261 | } |
mbed_official | 0:a554658735bf | 262 | } |
mbed_official | 0:a554658735bf | 263 | } |
mbed_official | 0:a554658735bf | 264 | |
mbed_official | 19:bd46ea19486b | 265 | void USBHostSerialPort::txHandler() { |
mbed_official | 0:a554658735bf | 266 | if (bulk_out) { |
mbed_official | 0:a554658735bf | 267 | if (bulk_out->getState() == USB_TYPE_IDLE) { |
mbed_official | 0:a554658735bf | 268 | tx.call(); |
mbed_official | 0:a554658735bf | 269 | } |
mbed_official | 0:a554658735bf | 270 | } |
mbed_official | 0:a554658735bf | 271 | } |
mbed_official | 0:a554658735bf | 272 | |
mbed_official | 19:bd46ea19486b | 273 | int USBHostSerialPort::_putc(int c) { |
mbed_official | 0:a554658735bf | 274 | if (bulk_out) { |
mbed_official | 0:a554658735bf | 275 | if (host->bulkWrite(dev, bulk_out, (uint8_t *)&c, 1) == USB_TYPE_OK) { |
mbed_official | 0:a554658735bf | 276 | return 1; |
mbed_official | 0:a554658735bf | 277 | } |
mbed_official | 0:a554658735bf | 278 | } |
mbed_official | 0:a554658735bf | 279 | return -1; |
mbed_official | 0:a554658735bf | 280 | } |
mbed_official | 0:a554658735bf | 281 | |
mbed_official | 19:bd46ea19486b | 282 | void USBHostSerialPort::baud(int baudrate) { |
samux | 4:b320d68e98e7 | 283 | line_coding.baudrate = baudrate; |
samux | 8:93da8ea2708b | 284 | format(line_coding.data_bits, (Parity)line_coding.parity, line_coding.stop_bits); |
samux | 4:b320d68e98e7 | 285 | } |
samux | 4:b320d68e98e7 | 286 | |
mbed_official | 19:bd46ea19486b | 287 | void USBHostSerialPort::format(int bits, Parity parity, int stop_bits) { |
samux | 4:b320d68e98e7 | 288 | line_coding.data_bits = bits; |
samux | 4:b320d68e98e7 | 289 | line_coding.parity = parity; |
samux | 4:b320d68e98e7 | 290 | line_coding.stop_bits = (stop_bits == 1) ? 0 : 2; |
mbed_official | 24:868cbfe611a7 | 291 | |
samux | 4:b320d68e98e7 | 292 | // set line coding |
mbed_official | 13:b58a2204422f | 293 | host->controlWrite( dev, |
mbed_official | 13:b58a2204422f | 294 | USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS, |
mbed_official | 13:b58a2204422f | 295 | SET_LINE_CODING, |
mbed_official | 13:b58a2204422f | 296 | 0, serial_intf, (uint8_t *)&line_coding, 7); |
samux | 4:b320d68e98e7 | 297 | } |
mbed_official | 0:a554658735bf | 298 | |
mbed_official | 19:bd46ea19486b | 299 | int USBHostSerialPort::_getc() { |
mbed_official | 0:a554658735bf | 300 | uint8_t c = 0; |
mbed_official | 0:a554658735bf | 301 | if (bulk_in == NULL) { |
samux | 4:b320d68e98e7 | 302 | init(); |
mbed_official | 0:a554658735bf | 303 | return -1; |
mbed_official | 0:a554658735bf | 304 | } |
dkato | 38:dd168a3e4194 | 305 | while (p_circ_buf->isEmpty()) { |
dkato | 36:f8c0979c2814 | 306 | if (dev_connected == false) { |
dkato | 36:f8c0979c2814 | 307 | return -1; |
dkato | 36:f8c0979c2814 | 308 | } |
dkato | 36:f8c0979c2814 | 309 | Thread::wait(1); |
dkato | 36:f8c0979c2814 | 310 | } |
dkato | 38:dd168a3e4194 | 311 | p_circ_buf->dequeue(&c); |
mbed_official | 0:a554658735bf | 312 | return c; |
mbed_official | 0:a554658735bf | 313 | } |
mbed_official | 0:a554658735bf | 314 | |
dkato | 36:f8c0979c2814 | 315 | int USBHostSerialPort::writeBuf(const char* b, int s) { |
dkato | 36:f8c0979c2814 | 316 | int i; |
mbed_official | 19:bd46ea19486b | 317 | int c = 0; |
dkato | 36:f8c0979c2814 | 318 | if (bulk_out) { |
dkato | 40:cdc0d2ab4678 | 319 | while (s > 0) { |
dkato | 36:f8c0979c2814 | 320 | if (dev_connected == false) { |
dkato | 36:f8c0979c2814 | 321 | break; |
dkato | 36:f8c0979c2814 | 322 | } |
dkato | 36:f8c0979c2814 | 323 | i = (s < size_bulk_out) ? s : size_bulk_out; |
dkato | 36:f8c0979c2814 | 324 | if (host->bulkWrite(dev, bulk_out, (uint8_t *)(b+c), i) == USB_TYPE_OK) { |
mbed_official | 19:bd46ea19486b | 325 | c += i; |
dkato | 40:cdc0d2ab4678 | 326 | s -= i; |
dkato | 36:f8c0979c2814 | 327 | } |
mbed_official | 19:bd46ea19486b | 328 | } |
mbed_official | 19:bd46ea19486b | 329 | } |
dkato | 36:f8c0979c2814 | 330 | return c; |
mbed_official | 19:bd46ea19486b | 331 | } |
mbed_official | 0:a554658735bf | 332 | |
dkato | 36:f8c0979c2814 | 333 | int USBHostSerialPort::readBuf(char* b, int s, int timeout) { |
mbed_official | 19:bd46ea19486b | 334 | int i = 0; |
dkato | 36:f8c0979c2814 | 335 | |
dkato | 38:dd168a3e4194 | 336 | if (bulk_in) { |
dkato | 36:f8c0979c2814 | 337 | for (i = 0; i < s; i++) { |
dkato | 38:dd168a3e4194 | 338 | while ((p_circ_buf->isEmpty()) && (dev_connected)) { |
dkato | 36:f8c0979c2814 | 339 | if (timeout == 0) { |
dkato | 36:f8c0979c2814 | 340 | break; |
dkato | 36:f8c0979c2814 | 341 | } else { |
dkato | 36:f8c0979c2814 | 342 | if (timeout > 0) { |
dkato | 36:f8c0979c2814 | 343 | timeout--; |
dkato | 36:f8c0979c2814 | 344 | } |
dkato | 36:f8c0979c2814 | 345 | Thread::wait(1); |
dkato | 36:f8c0979c2814 | 346 | } |
dkato | 36:f8c0979c2814 | 347 | } |
dkato | 38:dd168a3e4194 | 348 | if (!p_circ_buf->dequeue((uint8_t *)&b[i])) { |
dkato | 36:f8c0979c2814 | 349 | break; |
dkato | 36:f8c0979c2814 | 350 | } |
dkato | 36:f8c0979c2814 | 351 | } |
mbed_official | 19:bd46ea19486b | 352 | } |
mbed_official | 19:bd46ea19486b | 353 | return i; |
mbed_official | 19:bd46ea19486b | 354 | } |
mbed_official | 19:bd46ea19486b | 355 | |
dkato | 36:f8c0979c2814 | 356 | uint32_t USBHostSerialPort::available() { |
dkato | 38:dd168a3e4194 | 357 | return p_circ_buf->available(); |
mbed_official | 0:a554658735bf | 358 | } |
mbed_official | 0:a554658735bf | 359 | |
dkato | 36:f8c0979c2814 | 360 | #endif |
mbed_official | 0:a554658735bf | 361 | |
dkato | 38:dd168a3e4194 | 362 |