Adaptation of the official mbed USBHost repository to work with the LPC4088 Display Module

Dependents:   DMSupport DMSupport DMSupport DMSupport

Fork of DM_USBHost by EmbeddedArtists AB

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 "MtxCircBuffer.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     enum IrqType {
00039         RxIrq,
00040         TxIrq
00041     };
00042 
00043     enum Parity {
00044         None = 0,
00045         Odd,
00046         Even,
00047         Mark,
00048         Space
00049     };
00050 
00051     void connect(USBHost* _host, USBDeviceConnected * _dev,
00052         uint8_t _serial_intf, USBEndpoint* _bulk_in, USBEndpoint* _bulk_out);
00053 
00054     /**
00055     * Check the number of bytes available.
00056     *
00057     * @returns the number of bytes available
00058     */
00059     uint8_t available();
00060 
00061     /**
00062      *  Attach a member function to call when a packet is received.
00063      *
00064      *  @param tptr pointer to the object to call the member function on
00065      *  @param mptr pointer to the member function to be called
00066      *  @param irq irq type
00067      */
00068     template<typename T>
00069     inline void attach(T* tptr, void (T::*mptr)(void), IrqType irq = RxIrq) {
00070         if ((mptr != NULL) && (tptr != NULL)) {
00071             if (irq == RxIrq) {
00072                 rx.attach(tptr, mptr);
00073             } else {
00074                 tx.attach(tptr, mptr);
00075             }
00076         }
00077     }
00078 
00079     /**
00080      * Attach a callback called when a packet is received
00081      *
00082      * @param ptr function pointer
00083      */
00084     inline void attach(void (*fn)(void), IrqType irq = RxIrq) {
00085         if (fn != NULL) {
00086             if (irq == RxIrq) {
00087                 rx.attach(fn);
00088             } else {
00089                 tx.attach(fn);
00090             }
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);
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     MtxCircBuffer<uint8_t, 128> circ_buf;
00126 
00127     uint8_t buf[64];
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     LINE_CODING line_coding;
00137 
00138     void rxHandler();
00139     void txHandler();
00140     FunctionPointer rx;
00141     FunctionPointer tx;
00142 
00143     uint8_t serial_intf;
00144 };
00145 
00146 #if (USBHOST_SERIAL <= 1)
00147 
00148 class USBHostSerial : public IUSBEnumerator, public USBHostSerialPort
00149 {
00150 public:
00151     USBHostSerial();
00152 
00153     /**
00154      * Try to connect a serial device
00155      *
00156      * @return true if connection was successful
00157      */
00158     bool connect();
00159 
00160     void disconnect();
00161 
00162     /**
00163     * Check if a any serial port is connected
00164     *
00165     * @returns true if a serial device is connected
00166     */
00167     bool connected();
00168 
00169 protected:
00170     USBHost* host;
00171     USBDeviceConnected* dev;
00172     uint8_t port_intf;
00173     int ports_found;
00174 
00175     //From IUSBEnumerator
00176     virtual void setVidPid(uint16_t vid, uint16_t pid);
00177     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
00178     virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
00179 
00180 private:
00181     bool dev_connected;
00182 };
00183 
00184 #else // (USBHOST_SERIAL > 1)
00185 
00186 class USBHostMultiSerial : public IUSBEnumerator {
00187 public:
00188     USBHostMultiSerial();
00189     virtual ~USBHostMultiSerial();
00190 
00191     USBHostSerialPort* getPort(int port)
00192     {
00193         return port < USBHOST_SERIAL ? ports[port] : NULL;
00194     }
00195 
00196     /**
00197      * Try to connect a serial device
00198      *
00199      * @return true if connection was successful
00200      */
00201     bool connect();
00202 
00203     void disconnect();
00204 
00205     /**
00206     * Check if a any serial port is connected
00207     *
00208     * @returns true if a serial device is connected
00209     */
00210     bool connected();
00211 
00212 protected:
00213     USBHost* host;
00214     USBDeviceConnected* dev;
00215     USBHostSerialPort* ports[USBHOST_SERIAL];
00216     uint8_t port_intf[USBHOST_SERIAL];
00217     int ports_found;
00218 
00219     //From IUSBEnumerator
00220     virtual void setVidPid(uint16_t vid, uint16_t pid);
00221     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
00222     virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
00223 
00224 private:
00225     bool dev_connected;
00226 };
00227 #endif // (USBHOST_SERIAL <= 1)
00228 
00229 #endif
00230 
00231 #endif