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.h Source File

USB400Serial.h

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 #ifndef USB400SERIAL_H
00022 #define USB400SERIAL_H
00023 
00024 #include "USBHostConf.h"
00025 
00026 #if USBHOST_SERIAL
00027 
00028 #include "USBHost.h"
00029 #include "Stream.h"
00030 #include "MtxCircBuffer.h"
00031 
00032 /**
00033  * A class to communicate a USB virtual serial port
00034  */
00035 class USB400SerialPort : public Stream
00036 {
00037 public:
00038     /**
00039     * Constructor
00040     */
00041     USB400SerialPort();
00042 
00043     enum IrqType {
00044         RxIrq,
00045         TxIrq
00046     };
00047 
00048     enum Parity {
00049         None = 0,
00050         Odd,
00051         Even,
00052         Mark,
00053         Space
00054     };
00055 
00056     void connect(USBHost* _host, USBDeviceConnected * _dev,
00057                  uint8_t _serial_intf, USBEndpoint* _bulk_in, USBEndpoint* _bulk_out);
00058 
00059     /**
00060     * Check the number of bytes available.
00061     *
00062     * @returns the number of bytes available
00063     */
00064     uint8_t available();
00065 
00066     /**
00067      *  Attach a member function to call when a packet is received.
00068      *
00069      *  @param tptr pointer to the object to call the member function on
00070      *  @param mptr pointer to the member function to be called
00071      *  @param irq irq type
00072      */
00073     template<typename T>
00074     inline void attach(T* tptr, void (T::*mptr)(void), IrqType irq = RxIrq) {
00075         if ((mptr != NULL) && (tptr != NULL)) {
00076             if (irq == RxIrq) {
00077                 rx.attach(tptr, mptr);
00078             } else {
00079                 tx.attach(tptr, mptr);
00080             }
00081         }
00082     }
00083 
00084     /**
00085      * Attach a callback called when a packet is received
00086      *
00087      * @param ptr function pointer
00088      */
00089     inline void attach(void (*fn)(void), IrqType irq = RxIrq) {
00090         if (fn != NULL) {
00091             if (irq == RxIrq) {
00092                 rx.attach(fn);
00093             } else {
00094                 tx.attach(fn);
00095             }
00096         }
00097     }
00098 
00099     /** Set the baud rate of the serial port
00100      *
00101      *  @param baudrate The baudrate of the serial port (default = 9600).
00102      */
00103     void baud(int baudrate = 9600);
00104 
00105     /** Set the transmission format used by the Serial port
00106      *
00107      *  @param bits The number of bits in a word (default = 8)
00108      *  @param parity The parity used (USB400SerialPort::None, USB400SerialPort::Odd, USB400SerialPort::Even, USB400SerialPort::Mark, USB400SerialPort::Space; default = USB400SerialPort::None)
00109      *  @param stop The number of stop bits (1 or 2; default = 1)
00110      */
00111     void format(int bits = 8, Parity parity = USB400SerialPort::None, int stop_bits = 1);
00112     virtual int writeBuf(const char* b, int s);
00113     virtual int readBuf(char* b, int s);
00114 
00115 protected:
00116     virtual int _getc();
00117     virtual int _putc(int c);
00118 
00119 private:
00120     USBHost * host;
00121     USBDeviceConnected * dev;
00122 
00123     USBEndpoint * bulk_in;
00124     USBEndpoint * bulk_out;
00125     uint32_t size_bulk_in;
00126     uint32_t size_bulk_out;
00127 
00128     void init();
00129 
00130     MtxCircBuffer<uint8_t, 128> circ_buf;
00131 
00132     uint8_t buf[64];
00133 
00134     typedef struct {
00135         uint32_t baudrate;
00136         uint8_t stop_bits;
00137         uint8_t parity;
00138         uint8_t data_bits;
00139     } PACKED LINE_CODING;
00140 
00141     LINE_CODING line_coding;
00142 
00143     void rxHandler();
00144     void txHandler();
00145     FunctionPointer rx;
00146     FunctionPointer tx;
00147 
00148     uint8_t serial_intf;
00149 };
00150 
00151 #if (USBHOST_SERIAL <= 1)
00152 
00153 class USB400Serial : public IUSBEnumerator, public USB400SerialPort
00154 {
00155 public:
00156     USB400Serial();
00157 
00158     /**
00159      * Try to connect a serial device
00160      *
00161      * @return true if connection was successful
00162      */
00163     bool connect();
00164 
00165     void disconnect();
00166 
00167     /**
00168     * Check if a any serial port is connected
00169     *
00170     * @returns true if a serial device is connected
00171     */
00172     bool connected();
00173 
00174 protected:
00175     USBHost* host;
00176     USBDeviceConnected* dev;
00177     uint8_t port_intf;
00178     int ports_found;
00179 
00180     //From IUSBEnumerator
00181     virtual void setVidPid(uint16_t vid, uint16_t pid);
00182     virtual bool 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
00183     virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
00184 
00185 private:
00186     bool dev_connected;
00187 };
00188 
00189 #else // (USBHOST_SERIAL > 1)
00190 
00191 #endif // (USBHOST_SERIAL <= 1)
00192 
00193 #endif
00194 
00195 #endif