Anh Tran / Mbed OS GR-Boards_WebCamera

Dependencies:   HttpServer_snapshot_mbed-os

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHostSerial.h Source File

USBHostSerial.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 #ifndef USBHOSTSERIAL_H
00018 #define USBHOSTSERIAL_H
00019 
00020 #include "USBHostConf.h"
00021 
00022 #if USBHOST_SERIAL
00023 
00024 #include "USBHost.h"
00025 #include "Stream.h"
00026 #include "CircBufferHostSerial.h"
00027 #include "Callback.h"
00028 
00029 /**
00030  * A class to communicate a USB virtual serial port
00031  */
00032 class USBHostSerialPort : public Stream {
00033 public:
00034     /**
00035     * Constructor
00036     */
00037     USBHostSerialPort(uint32_t buf_size);
00038 
00039     /**
00040      * Destructor
00041      */
00042     virtual ~USBHostSerialPort();
00043 
00044     enum IrqType {
00045         RxIrq,
00046         TxIrq,
00047 
00048         IrqCnt
00049     };
00050 
00051     enum Parity {
00052         None = 0,
00053         Odd,
00054         Even,
00055         Mark,
00056         Space
00057     };
00058 
00059     void connect(USBHost* _host, USBDeviceConnected * _dev,
00060         uint8_t _serial_intf, USBEndpoint* _bulk_in, USBEndpoint* _bulk_out);
00061 
00062     bool connected();
00063 
00064     /**
00065     * Check the number of bytes available.
00066     *
00067     * @returns the number of bytes available
00068     */
00069     uint32_t available();
00070 
00071     /**
00072      *  Attach a member function to call when a packet is received.
00073      *
00074      *  @param obj pointer to the object to call the member function on
00075      *  @param method pointer to the member function to be called
00076      *  @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
00077      */
00078     template<typename T>
00079     void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) {
00080         attach(callback(obj, method), type);
00081     }
00082 
00083     /** Attach a function to call whenever a serial interrupt is generated
00084      *
00085      *  @param func A pointer to a void function, or 0 to set as none
00086      *  @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
00087      */
00088     void attach(Callback<void()> func, IrqType type=RxIrq) {
00089         if (func) {
00090             _irq[type] = func;
00091         }
00092     }
00093 
00094     /** Set the baud rate of the serial port
00095      *
00096      *  @param baudrate The baudrate of the serial port (default = 9600).
00097      */
00098     void baud(int baudrate = 9600);
00099 
00100     /** Set the transmission format used by the Serial port
00101      *
00102      *  @param bits The number of bits in a word (default = 8)
00103      *  @param parity The parity used (USBHostSerialPort::None, USBHostSerialPort::Odd, USBHostSerialPort::Even, USBHostSerialPort::Mark, USBHostSerialPort::Space; default = USBHostSerialPort::None)
00104      *  @param stop The number of stop bits (1 or 2; default = 1)
00105      */
00106     void format(int bits = 8, Parity parity = USBHostSerialPort::None, int stop_bits = 1);
00107     virtual int writeBuf(const char* b, int s);
00108     virtual int readBuf(char* b, int s, int timeout = -1);
00109 
00110 //protected:
00111     virtual int _getc();
00112     virtual int _putc(int c);
00113 
00114 private:
00115     USBHost * host;
00116     USBDeviceConnected * dev;
00117 
00118     USBEndpoint * bulk_in;
00119     USBEndpoint * bulk_out;
00120     uint32_t size_bulk_in;
00121     uint32_t size_bulk_out;
00122 
00123     void init();
00124 
00125     CircBufferHostSerial<uint8_t> * p_circ_buf;
00126 
00127     uint8_t * p_buf;
00128 
00129     typedef struct {
00130         uint32_t baudrate;
00131         uint8_t stop_bits;
00132         uint8_t parity;
00133         uint8_t data_bits;
00134     } PACKED LINE_CODING;
00135 
00136 #if defined(TARGET_RZ_A2XX)
00137     LINE_CODING * p_line_coding;
00138     uint8_t * p_buf_out;
00139     uint8_t * p_buf_out_c;
00140 #else
00141     LINE_CODING line_coding;
00142 #endif
00143 
00144     void rxHandler();
00145     void txHandler();
00146     Callback<void()> _irq[IrqCnt];
00147 
00148     uint8_t serial_intf;
00149     bool dev_connected;
00150 };
00151 
00152 #if (USBHOST_SERIAL <= 1)
00153 
00154 class USBHostSerial : public IUSBEnumerator, public USBHostSerialPort
00155 {
00156 public:
00157     USBHostSerial(uint32_t buf_size = (1024 * 32));
00158 
00159     /**
00160      * Try to connect a serial device
00161      *
00162      * @return true if connection was successful
00163      */
00164     bool connect();
00165 
00166     void disconnect();
00167 
00168     uint16_t get_vid() {
00169         return _vid;
00170     }
00171 
00172     uint16_t get_pid() {
00173         return _pid;
00174     }
00175 
00176 protected:
00177     USBHost* host;
00178     USBDeviceConnected* dev;
00179     uint8_t port_intf;
00180     int ports_found;
00181     uint16_t _vid;
00182     uint16_t _pid;
00183 
00184     //From IUSBEnumerator
00185     virtual void setVidPid(uint16_t vid, uint16_t pid);
00186     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
00187     virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
00188 
00189 };
00190 
00191 #else // (USBHOST_SERIAL > 1)
00192 
00193 class USBHostMultiSerial : public IUSBEnumerator {
00194 public:
00195     USBHostMultiSerial(uint32_t buf_size = (1024 * 32));
00196     virtual ~USBHostMultiSerial();
00197 
00198     USBHostSerialPort* getPort(int port)
00199     {
00200         return port < USBHOST_SERIAL ? ports[port] : NULL;
00201     }
00202 
00203     /**
00204      * Try to connect a serial device
00205      *
00206      * @return true if connection was successful
00207      */
00208     bool connect();
00209 
00210     void disconnect();
00211 
00212     /**
00213     * Check if a any serial port is connected
00214     *
00215     * @returns true if a serial device is connected
00216     */
00217     bool connected();
00218 
00219 protected:
00220     USBHost* host;
00221     USBDeviceConnected* dev;
00222     USBHostSerialPort* ports[USBHOST_SERIAL];
00223     uint8_t port_intf[USBHOST_SERIAL];
00224     int ports_found;
00225     uint32_t _buf_size;
00226 
00227     //From IUSBEnumerator
00228     virtual void setVidPid(uint16_t vid, uint16_t pid);
00229     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
00230     virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
00231 
00232 private:
00233     bool dev_connected;
00234 };
00235 #endif // (USBHOST_SERIAL <= 1)
00236 
00237 #endif
00238 
00239 #endif