Yasuhisa KAWAMOTO / USB400Serial

Dependents:   USB400Serial_Hello MFT_IoT_demo_USB400 USB400J_app_board_demo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USB400Serial.cpp Source File

USB400Serial.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 /* USB400Serial Library
00018  * Modified mujirushi.org
00019  */
00020 
00021 #include "USB400Serial.h"
00022 
00023 #if USBHOST_SERIAL
00024 
00025 #include "dbg.h"
00026 
00027 #define CHECK_INTERFACE(cls,subcls,proto) 1 // \
00028 //        (((cls == 0xFF)         && (subcls == 0xFF) && (proto == 0xFF)) /* QUALCOM CDC */  || \
00029 //         ((cls == SERIAL_CLASS) && (subcls == 0x00) && (proto == 0x00)) /* STANDARD CDC */ )
00030 
00031 #if (USBHOST_SERIAL <= 1)
00032 
00033 USB400Serial::USB400Serial()
00034 {
00035     host = USBHost::getHostInst();
00036     ports_found = 0;
00037     dev_connected = false;
00038 }
00039 
00040 bool USB400Serial::connected()
00041 {
00042     return dev;
00043 //    return dev_connected;
00044 }
00045 
00046 void USB400Serial::disconnect(void)
00047 {
00048     ports_found = 0;
00049     dev = NULL;
00050 }
00051 
00052 bool USB400Serial::connect()
00053 {
00054 
00055     if (dev) {
00056         for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
00057             USBDeviceConnected* d = host->getDevice(i);
00058             if (dev == d)
00059                 return true;
00060         }
00061         disconnect();
00062     }
00063     for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
00064         USBDeviceConnected* d = host->getDevice(i);
00065         if (d != NULL) {
00066 
00067             USB_DBG("Trying to connect serial device \r\n");
00068             if(host->enumerate(d, this))
00069                 break;
00070 
00071             USBEndpoint* bulk_in  = d->getEndpoint(port_intf, BULK_ENDPOINT, IN);
00072             USBEndpoint* bulk_out = d->getEndpoint(port_intf, BULK_ENDPOINT, OUT);
00073             if (bulk_in && bulk_out) {
00074                 USB400SerialPort::connect(host,d,port_intf,bulk_in, bulk_out);
00075                 dev = d;
00076             }
00077         }
00078     }
00079     return dev != NULL;
00080 }
00081 
00082 /*virtual*/ void USB400Serial::setVidPid(uint16_t vid, uint16_t pid)
00083 {
00084     // we don't check VID/PID for MSD driver
00085 }
00086 
00087 /*virtual*/ bool USB400Serial::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
00088 {
00089     if (!ports_found &&
00090             CHECK_INTERFACE(intf_class, intf_subclass, intf_protocol)) {
00091         port_intf = intf_nb;
00092         ports_found = true;
00093         return true;
00094     }
00095     return false;
00096 }
00097 
00098 /*virtual*/ bool USB400Serial::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
00099 {
00100     if (ports_found && (intf_nb == port_intf)) {
00101         if (type == BULK_ENDPOINT)
00102             return true;
00103     }
00104     return false;
00105 }
00106 
00107 #else // (USBHOST_SERIAL > 1)
00108 
00109 #endif
00110 
00111 //------------------------------------------------------------------------------
00112 
00113 #define SET_LINE_CODING 0x20
00114 
00115 USB400SerialPort::USB400SerialPort(): circ_buf()
00116 {
00117     init();
00118 }
00119 
00120 void USB400SerialPort::init(void)
00121 {
00122     host = NULL;
00123     dev = NULL;
00124     serial_intf = NULL;
00125     size_bulk_in = 0;
00126     size_bulk_out = 0;
00127     bulk_in = NULL;
00128     bulk_out = NULL;
00129     line_coding.baudrate = 9600;
00130     line_coding.data_bits = 8;
00131     line_coding.parity = None;
00132     line_coding.stop_bits = 1;
00133     circ_buf.flush();
00134 }
00135 
00136 void USB400SerialPort::connect(USBHost* _host, USBDeviceConnected * _dev,
00137                                uint8_t _serial_intf, USBEndpoint* _bulk_in, USBEndpoint* _bulk_out)
00138 {
00139     host = _host;
00140     dev = _dev;
00141     serial_intf = _serial_intf;
00142     bulk_in = _bulk_in;
00143     bulk_out = _bulk_out;
00144 
00145     USB_INFO("New Serial device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, serial_intf);
00146     dev->setName("Serial", serial_intf);
00147     host->registerDriver(dev, serial_intf, this, &USB400SerialPort::init);
00148     baud(9600); //
00149     size_bulk_in = bulk_in->getSize();
00150     size_bulk_out = bulk_out->getSize();
00151     bulk_in->attach(this, &USB400SerialPort::rxHandler);
00152     bulk_out->attach(this, &USB400SerialPort::txHandler);
00153     host->bulkRead(dev, bulk_in, buf, size_bulk_in, false);
00154 }
00155 
00156 void USB400SerialPort::rxHandler()
00157 {
00158     if (bulk_in) {
00159         int len = bulk_in->getLengthTransferred();
00160         if (bulk_in->getState() == USB_TYPE_IDLE) {
00161             if(len > 2) {
00162                 for (int i = 2; i < len; i++) {
00163                     circ_buf.queue(buf[i]);
00164                 }
00165             }
00166 //            for (int i = 0; i < len; i++) {
00167 //                circ_buf.queue(buf[i]);
00168 //            }
00169             rx.call();
00170             host->bulkRead(dev, bulk_in, buf, size_bulk_in, false);
00171         }
00172     }
00173 }
00174 
00175 void USB400SerialPort::txHandler()
00176 {
00177     if (bulk_out) {
00178         if (bulk_out->getState() == USB_TYPE_IDLE) {
00179             tx.call();
00180         }
00181     }
00182 }
00183 
00184 int USB400SerialPort::_putc(int c)
00185 {
00186     if (bulk_out) {
00187         if (host->bulkWrite(dev, bulk_out, (uint8_t *)&c, 1) == USB_TYPE_OK) {
00188             return 1;
00189         }
00190     }
00191     return -1;
00192 }
00193 
00194 void USB400SerialPort::baud(int baudrate)
00195 {
00196     line_coding.baudrate = baudrate;
00197     format(line_coding.data_bits, (Parity)line_coding.parity, line_coding.stop_bits);
00198 }
00199 
00200 void USB400SerialPort::format(int bits, Parity parity, int stop_bits)
00201 {
00202     line_coding.data_bits = bits;
00203     line_coding.parity = parity;
00204     line_coding.stop_bits = (stop_bits == 1) ? 0 : 2;
00205 
00206     // set line coding
00207     host->controlWrite( dev, 0x40, 0,      0, 0, NULL, 0);
00208     host->controlWrite( dev, 0x40, 0,      1, 0, NULL, 0);
00209     host->controlWrite( dev, 0x40, 0,      2, 0, NULL, 0);
00210     host->controlWrite( dev, 0x40, 2, 0x0000, 0, NULL, 0); // flow control none
00211     host->controlWrite( dev, 0x40, 3, 0xC034, 0, NULL, 0); // baudrate 57600
00212     host->controlWrite( dev, 0x40, 4, 0x0008, 0, NULL, 0); // data bit 8, parity none, stop bit 1, tx off
00213 
00214 //    host->controlWrite( dev,
00215 //                        USB_RECIPIENT_INTERFACE | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS,
00216 //                        SET_LINE_CODING,
00217 //                        0, serial_intf, (uint8_t *)&line_coding, 7);
00218 }
00219 
00220 int USB400SerialPort::_getc()
00221 {
00222     uint8_t c = 0;
00223     if (bulk_in == NULL) {
00224         init();
00225         return -1;
00226     }
00227     while (circ_buf.isEmpty());
00228     circ_buf.dequeue(&c);
00229     return c;
00230 }
00231 
00232 int USB400SerialPort::writeBuf(const char* b, int s)
00233 {
00234     int c = 0;
00235     if (bulk_out) {
00236         while (c < s) {
00237             int i = (s < size_bulk_out) ? s : size_bulk_out;
00238             if (host->bulkWrite(dev, bulk_out, (uint8_t *)(b+c), i) == USB_TYPE_OK)
00239                 c += i;
00240         }
00241     }
00242     return s;
00243 }
00244 
00245 int USB400SerialPort::readBuf(char* b, int s)
00246 {
00247     int i = 0;
00248     if (bulk_in) {
00249         for (i = 0; i < s; )
00250             b[i++] = getc();
00251     }
00252     return i;
00253 }
00254 
00255 uint8_t USB400SerialPort::available()
00256 {
00257     return circ_buf.available();
00258 }
00259 
00260 
00261 
00262 #endif