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 circ_buf.queue(buf[i]); 00099 } 00100 rx.call(); 00101 host->bulkRead(dev, bulk_in, buf, size_bulk_in, false); 00102 } 00103 } 00104 } 00105 00106 void USBHostSerial::txHandler() { 00107 if (bulk_out) { 00108 if (bulk_out->getState() == USB_TYPE_IDLE) { 00109 tx.call(); 00110 } 00111 } 00112 } 00113 00114 int USBHostSerial::_putc(int c) { 00115 if (bulk_out) { 00116 if (host->bulkWrite(dev, bulk_out, (uint8_t *)&c, 1) == USB_TYPE_OK) { 00117 return 1; 00118 } 00119 } 00120 return -1; 00121 } 00122 00123 void USBHostSerial::baud(int baudrate) { 00124 line_coding.baudrate = baudrate; 00125 format(line_coding.data_bits, (Parity)line_coding.parity, line_coding.stop_bits); 00126 } 00127 00128 void USBHostSerial::format(int bits, Parity parity, int stop_bits) { 00129 line_coding.data_bits = bits; 00130 line_coding.parity = parity; 00131 line_coding.stop_bits = (stop_bits == 1) ? 0 : 2; 00132 00133 // set line coding 00134 int res = host->controlWrite( dev, 00135 USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS, 00136 SET_LINE_CODING, 00137 0, serial_intf, (uint8_t *)&line_coding, 7); 00138 } 00139 00140 int USBHostSerial::_getc() { 00141 uint8_t c = 0; 00142 if (bulk_in == NULL) { 00143 init(); 00144 return -1; 00145 } 00146 while (circ_buf.isEmpty()); 00147 circ_buf.dequeue(&c); 00148 return c; 00149 } 00150 00151 00152 uint8_t USBHostSerial::available() { 00153 return circ_buf.available(); 00154 } 00155 00156 /*virtual*/ void USBHostSerial::setVidPid(uint16_t vid, uint16_t pid) 00157 { 00158 // we don't check VID/PID for MSD driver 00159 } 00160 00161 /*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 00162 { 00163 if ((serial_intf == -1) && 00164 (intf_class == SERIAL_CLASS) && 00165 (intf_subclass == 0x00) && 00166 (intf_protocol == 0x00)) { 00167 serial_intf = intf_nb; 00168 return true; 00169 } 00170 return false; 00171 } 00172 00173 /*virtual*/ bool USBHostSerial::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used 00174 { 00175 if (intf_nb == serial_intf) { 00176 if (type == BULK_ENDPOINT) { 00177 serial_device_found = true; 00178 return true; 00179 } 00180 } 00181 return false; 00182 } 00183 00184 #endif
Generated on Fri Jul 15 2022 03:47:32 by
1.7.2
