Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of USBHost by
USBHostSerial.cpp
00001 /* mbed USBHost Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "USBHostSerial.h" 00018 00019 #if USBHOST_SERIAL 00020 00021 #include "dbg.h" 00022 00023 #define SET_LINE_CODING 0x20 00024 00025 USBHostSerial::USBHostSerial(): circ_buf() { 00026 host = USBHost::getHostInst(); 00027 size_bulk_in = 0; 00028 size_bulk_out = 0; 00029 init(); 00030 } 00031 00032 void USBHostSerial::init() { 00033 dev = NULL; 00034 bulk_in = NULL; 00035 bulk_out = NULL; 00036 dev_connected = false; 00037 serial_intf = -1; 00038 serial_device_found = false; 00039 line_coding.baudrate = 9600; 00040 line_coding.data_bits = 8; 00041 line_coding.parity = None; 00042 line_coding.stop_bits = 1; 00043 circ_buf.flush(); 00044 } 00045 00046 bool USBHostSerial::connected() 00047 { 00048 return dev_connected; 00049 } 00050 00051 bool USBHostSerial::connect() { 00052 00053 if (dev_connected) { 00054 return true; 00055 } 00056 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { 00057 if ((dev = host->getDevice(i)) != NULL) { 00058 00059 USB_DBG("Trying to connect serial device\r\n"); 00060 00061 if(host->enumerate(dev, this)) 00062 break; 00063 00064 if (serial_device_found) { 00065 bulk_in = dev->getEndpoint(serial_intf, BULK_ENDPOINT, IN); 00066 bulk_out = dev->getEndpoint(serial_intf, BULK_ENDPOINT, OUT); 00067 00068 if (!bulk_in || !bulk_out) 00069 break; 00070 00071 USB_INFO("New Serial device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, serial_intf); 00072 dev->setName("Serial", serial_intf); 00073 host->registerDriver(dev, serial_intf, this, &USBHostSerial::init); 00074 00075 baud(9600); 00076 00077 size_bulk_in = bulk_in->getSize(); 00078 size_bulk_out = bulk_out->getSize(); 00079 00080 bulk_in->attach(this, &USBHostSerial::rxHandler); 00081 bulk_out->attach(this, &USBHostSerial::txHandler); 00082 00083 host->bulkRead(dev, bulk_in, buf, size_bulk_in, false); 00084 dev_connected = true; 00085 return true; 00086 } 00087 } 00088 } 00089 init(); 00090 return false; 00091 } 00092 00093 void USBHostSerial::rxHandler() { 00094 if (bulk_in) { 00095 int len = bulk_in->getLengthTransferred(); 00096 if (bulk_in->getState() == USB_TYPE_IDLE) { 00097 for (int i = 0; i < len; i++) { 00098 if (i > 1) 00099 circ_buf.queue(buf[i]); 00100 } 00101 rx.call(); 00102 host->bulkRead(dev, bulk_in, buf, size_bulk_in, false); 00103 } 00104 } 00105 } 00106 00107 void USBHostSerial::txHandler() { 00108 if (bulk_out) { 00109 if (bulk_out->getState() == USB_TYPE_IDLE) { 00110 tx.call(); 00111 } 00112 } 00113 } 00114 00115 int USBHostSerial::_putc(int c) { 00116 if (bulk_out) { 00117 if (host->bulkWrite(dev, bulk_out, (uint8_t *)&c, 1) == USB_TYPE_OK) { 00118 return 1; 00119 } 00120 } 00121 return -1; 00122 } 00123 00124 void USBHostSerial::baud(int baudrate) { 00125 line_coding.baudrate = baudrate; 00126 format(line_coding.data_bits, (Parity)line_coding.parity, line_coding.stop_bits); 00127 } 00128 00129 void USBHostSerial::format(int bits, Parity parity, int stop_bits) { 00130 line_coding.data_bits = bits; 00131 line_coding.parity = parity; 00132 line_coding.stop_bits = (stop_bits == 1) ? 0 : 2; 00133 00134 // set line coding 00135 int res = host->controlWrite( dev, 00136 USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS, 00137 SET_LINE_CODING, 00138 0, serial_intf, (uint8_t *)&line_coding, 7); 00139 } 00140 00141 int USBHostSerial::_getc() { 00142 uint8_t c = 0; 00143 if (bulk_in == NULL) { 00144 init(); 00145 return -1; 00146 } 00147 while (circ_buf.isEmpty()); 00148 circ_buf.dequeue(&c); 00149 return c; 00150 } 00151 00152 00153 uint8_t USBHostSerial::available() { 00154 return circ_buf.available(); 00155 } 00156 00157 /*virtual*/ void USBHostSerial::setVidPid(uint16_t vid, uint16_t pid) 00158 { 00159 // we don't check VID/PID for MSD driver 00160 } 00161 00162 /*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 00163 { 00164 USB_DBG("HENRY Parsing Interface : %d : %d : %d : %d : %d", serial_intf, intf_class, intf_subclass, intf_protocol, SERIAL_CLASS); 00165 /* 00166 if ((serial_intf == -1) && 00167 (intf_class == SERIAL_CLASS) && 00168 (intf_subclass == 0x00) && 00169 (intf_protocol == 0x00)) { 00170 serial_intf = intf_nb; 00171 return true; 00172 }*/ 00173 if ((serial_intf == -1)){ 00174 serial_intf = intf_nb; 00175 return true; 00176 } 00177 00178 return false; 00179 } 00180 00181 /*virtual*/ bool USBHostSerial::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used 00182 { 00183 if (intf_nb == serial_intf) { 00184 if (type == BULK_ENDPOINT) { 00185 serial_device_found = true; 00186 return true; 00187 } 00188 } 00189 return false; 00190 } 00191 00192 #endif
Generated on Sat Jul 16 2022 04:39:31 by
