forked for unsupported target

Dependents:   stm32-disco-example DISCO-F469NI_BD_SD_Card_Control

Fork of USBHOST by ST

Committer:
frq08711@LMECWL0871.LME.ST.COM
Date:
Wed Feb 15 10:49:44 2017 +0100
Revision:
1:ab240722d7ef
update to mbed 5.3.5

Who changed what in which revision?

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