Renesas GR-PEACH OpenCV Development / gr-peach-opencv-project-sd-card_update

Fork of gr-peach-opencv-project-sd-card by the do

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 
00028 /**
00029  * A class to communicate a USB virtual serial port
00030  */
00031 class USBHostSerialPort : public Stream {
00032 public:
00033     /**
00034     * Constructor
00035     */
00036     USBHostSerialPort();
00037 
00038     /**
00039      * Destructor
00040      */
00041     virtual ~USBHostSerialPort();
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     bool connected();
00060 
00061     /**
00062     * Check the number of bytes available.
00063     *
00064     * @returns the number of bytes available
00065     */
00066     uint32_t available();
00067 
00068     /**
00069      *  Attach a member function to call when a packet is received.
00070      *
00071      *  @param tptr pointer to the object to call the member function on
00072      *  @param mptr pointer to the member function to be called
00073      *  @param irq irq type
00074      */
00075     template<typename T>
00076     inline void attach(T* tptr, void (T::*mptr)(void), IrqType irq = RxIrq) {
00077         if ((mptr != NULL) && (tptr != NULL)) {
00078             if (irq == RxIrq) {
00079                 rx.attach(tptr, mptr);
00080             } else {
00081                 tx.attach(tptr, mptr);
00082             }
00083         }
00084     }
00085 
00086     /**
00087      * Attach a callback called when a packet is received
00088      *
00089      * @param ptr function pointer
00090      */
00091     inline void attach(void (*fn)(void), IrqType irq = RxIrq) {
00092         if (fn != NULL) {
00093             if (irq == RxIrq) {
00094                 rx.attach(fn);
00095             } else {
00096                 tx.attach(fn);
00097             }
00098         }
00099     }
00100 
00101     /** Set the baud rate of the serial port
00102      *
00103      *  @param baudrate The baudrate of the serial port (default = 9600).
00104      */
00105     void baud(int baudrate = 9600);
00106 
00107     /** Set the transmission format used by the Serial port
00108      *
00109      *  @param bits The number of bits in a word (default = 8)
00110      *  @param parity The parity used (USBHostSerialPort::None, USBHostSerialPort::Odd, USBHostSerialPort::Even, USBHostSerialPort::Mark, USBHostSerialPort::Space; default = USBHostSerialPort::None)
00111      *  @param stop The number of stop bits (1 or 2; default = 1)
00112      */
00113     void format(int bits = 8, Parity parity = USBHostSerialPort::None, int stop_bits = 1);
00114     virtual int writeBuf(const char* b, int s);
00115     virtual int readBuf(char* b, int s, int timeout = -1);
00116 
00117 protected:
00118     virtual int _getc();
00119     virtual int _putc(int c);
00120 
00121 private:
00122     USBHost * host;
00123     USBDeviceConnected * dev;
00124 
00125     USBEndpoint * bulk_in;
00126     USBEndpoint * bulk_out;
00127     uint32_t size_bulk_in;
00128     uint32_t size_bulk_out;
00129 
00130     void init();
00131 
00132     CircBufferHostSerial<uint8_t, (1024 * 32)> * p_circ_buf;
00133 
00134     uint8_t buf[64];
00135 
00136     typedef struct {
00137         uint32_t baudrate;
00138         uint8_t stop_bits;
00139         uint8_t parity;
00140         uint8_t data_bits;
00141     } PACKED LINE_CODING;
00142 
00143     LINE_CODING line_coding;
00144 
00145     void rxHandler();
00146     void txHandler();
00147     FunctionPointer rx;
00148     FunctionPointer tx;
00149 
00150     uint8_t serial_intf;
00151     bool dev_connected;
00152 };
00153 
00154 #if (USBHOST_SERIAL <= 1)
00155 
00156 class USBHostSerial : public IUSBEnumerator, public USBHostSerialPort
00157 {
00158 public:
00159     USBHostSerial();
00160 
00161     /**
00162      * Try to connect a serial device
00163      *
00164      * @return true if connection was successful
00165      */
00166     bool connect();
00167 
00168     void disconnect();
00169 
00170     /**
00171     * Check if a any serial port is connected
00172     *
00173     * @returns true if a serial device is connected
00174     */
00175 //    bool connected();
00176 
00177 protected:
00178     USBHost* host;
00179     USBDeviceConnected* dev;
00180     uint8_t port_intf;
00181     int ports_found;
00182 
00183     //From IUSBEnumerator
00184     virtual void setVidPid(uint16_t vid, uint16_t pid);
00185     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
00186     virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
00187 
00188 };
00189 
00190 #else // (USBHOST_SERIAL > 1)
00191 
00192 class USBHostMultiSerial : public IUSBEnumerator {
00193 public:
00194     USBHostMultiSerial();
00195     virtual ~USBHostMultiSerial();
00196 
00197     USBHostSerialPort* getPort(int port)
00198     {
00199         return port < USBHOST_SERIAL ? ports[port] : NULL;
00200     }
00201 
00202     /**
00203      * Try to connect a serial device
00204      *
00205      * @return true if connection was successful
00206      */
00207     bool connect();
00208 
00209     void disconnect();
00210 
00211     /**
00212     * Check if a any serial port is connected
00213     *
00214     * @returns true if a serial device is connected
00215     */
00216     bool connected();
00217 
00218 protected:
00219     USBHost* host;
00220     USBDeviceConnected* dev;
00221     USBHostSerialPort* ports[USBHOST_SERIAL];
00222     uint8_t port_intf[USBHOST_SERIAL];
00223     int ports_found;
00224 
00225     //From IUSBEnumerator
00226     virtual void setVidPid(uint16_t vid, uint16_t pid);
00227     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
00228     virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
00229 
00230 };
00231 #endif // (USBHOST_SERIAL <= 1)
00232 
00233 #endif
00234 
00235 #endif