A web server for monitoring and controlling a MakerBot Replicator over the USB host and ethernet.
Dependencies: IAP NTPClient RTC mbed-rtos mbed Socket lwip-sys lwip BurstSPI
Fork of LPC1768_Mini-DK 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::writeBuffer(uint8_t* buffer, int buffer_length) { 00115 if (host->bulkWrite(dev, bulk_out, buffer, buffer_length) == USB_TYPE_OK) { 00116 return buffer_length; 00117 } else { 00118 return -1; 00119 } 00120 } 00121 00122 int USBHostSerial::putch(int c) { 00123 return this->_putc(c); 00124 } 00125 00126 int USBHostSerial::_putc(int c) { 00127 if (bulk_out) { 00128 if (host->bulkWrite(dev, bulk_out, (uint8_t *)&c, 1) == USB_TYPE_OK) { 00129 return 1; 00130 } 00131 } 00132 return -1; 00133 } 00134 00135 void USBHostSerial::baud(int baudrate) { 00136 line_coding.baudrate = baudrate; 00137 format(line_coding.data_bits, (Parity)line_coding.parity, line_coding.stop_bits); 00138 } 00139 00140 void USBHostSerial::format(int bits, Parity parity, int stop_bits) { 00141 line_coding.data_bits = bits; 00142 line_coding.parity = parity; 00143 line_coding.stop_bits = (stop_bits == 1) ? 0 : 2; 00144 00145 // set line coding 00146 int res = host->controlWrite( dev, 00147 USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS, 00148 SET_LINE_CODING, 00149 0, 0, (uint8_t *)&line_coding, 7); 00150 } 00151 00152 int USBHostSerial::getch() { 00153 return this->_getc(); 00154 } 00155 00156 int USBHostSerial::_getc() { 00157 uint8_t c = 0; 00158 if (bulk_in == NULL) { 00159 init(); 00160 return -1; 00161 } 00162 while (circ_buf.isEmpty()); 00163 circ_buf.dequeue(&c); 00164 return c; 00165 } 00166 00167 00168 uint8_t USBHostSerial::available() { 00169 return circ_buf.available(); 00170 } 00171 00172 /*virtual*/ void USBHostSerial::setVidPid(uint16_t vid, uint16_t pid) 00173 { 00174 // we don't check VID/PID for MSD driver 00175 } 00176 00177 /*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 00178 { 00179 if ((serial_intf == -1) && 00180 (intf_class == SERIAL_CLASS) && 00181 (intf_subclass == 0x00) && 00182 (intf_protocol == 0x00)) { 00183 serial_intf = intf_nb; 00184 return true; 00185 } 00186 return false; 00187 } 00188 00189 /*virtual*/ bool USBHostSerial::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used 00190 { 00191 if (intf_nb == serial_intf) { 00192 if (type == BULK_ENDPOINT) { 00193 serial_device_found = true; 00194 return true; 00195 } 00196 } 00197 return false; 00198 } 00199 00200 #endif
Generated on Tue Jul 12 2022 17:52:03 by 1.7.2