2018.07.26

Dependencies:   FATFileSystem2 mbed-rtos

Fork of USBHost by mbed official

Committer:
sayzyas
Date:
Thu Jul 26 00:19:16 2018 +0000
Revision:
43:78f328f311dc
2018.07.26

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sayzyas 43:78f328f311dc 1 /* mbed USBHost Library
sayzyas 43:78f328f311dc 2 * Copyright (c) 2006-2013 ARM Limited
sayzyas 43:78f328f311dc 3 *
sayzyas 43:78f328f311dc 4 * Licensed under the Apache License, Version 2.0 (the "License");
sayzyas 43:78f328f311dc 5 * you may not use this file except in compliance with the License.
sayzyas 43:78f328f311dc 6 * You may obtain a copy of the License at
sayzyas 43:78f328f311dc 7 *
sayzyas 43:78f328f311dc 8 * http://www.apache.org/licenses/LICENSE-2.0
sayzyas 43:78f328f311dc 9 *
sayzyas 43:78f328f311dc 10 * Unless required by applicable law or agreed to in writing, software
sayzyas 43:78f328f311dc 11 * distributed under the License is distributed on an "AS IS" BASIS,
sayzyas 43:78f328f311dc 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sayzyas 43:78f328f311dc 13 * See the License for the specific language governing permissions and
sayzyas 43:78f328f311dc 14 * limitations under the License.
sayzyas 43:78f328f311dc 15 */
sayzyas 43:78f328f311dc 16
sayzyas 43:78f328f311dc 17 #ifndef USBHOSTSERIAL_H
sayzyas 43:78f328f311dc 18 #define USBHOSTSERIAL_H
sayzyas 43:78f328f311dc 19
sayzyas 43:78f328f311dc 20 #include "USBHostConf.h"
sayzyas 43:78f328f311dc 21
sayzyas 43:78f328f311dc 22 #if USBHOST_SERIAL
sayzyas 43:78f328f311dc 23
sayzyas 43:78f328f311dc 24 #include "USBHost.h"
sayzyas 43:78f328f311dc 25 #include "Stream.h"
sayzyas 43:78f328f311dc 26 #include "MtxCircBuffer.h"
sayzyas 43:78f328f311dc 27
sayzyas 43:78f328f311dc 28 /**
sayzyas 43:78f328f311dc 29 * A class to communicate a USB virtual serial port
sayzyas 43:78f328f311dc 30 */
sayzyas 43:78f328f311dc 31 class USBHostSerialPort : public Stream {
sayzyas 43:78f328f311dc 32 public:
sayzyas 43:78f328f311dc 33 /**
sayzyas 43:78f328f311dc 34 * Constructor
sayzyas 43:78f328f311dc 35 */
sayzyas 43:78f328f311dc 36 USBHostSerialPort();
sayzyas 43:78f328f311dc 37
sayzyas 43:78f328f311dc 38 enum IrqType {
sayzyas 43:78f328f311dc 39 RxIrq,
sayzyas 43:78f328f311dc 40 TxIrq
sayzyas 43:78f328f311dc 41 };
sayzyas 43:78f328f311dc 42
sayzyas 43:78f328f311dc 43 enum Parity {
sayzyas 43:78f328f311dc 44 None = 0,
sayzyas 43:78f328f311dc 45 Odd,
sayzyas 43:78f328f311dc 46 Even,
sayzyas 43:78f328f311dc 47 Mark,
sayzyas 43:78f328f311dc 48 Space
sayzyas 43:78f328f311dc 49 };
sayzyas 43:78f328f311dc 50
sayzyas 43:78f328f311dc 51 void connect(USBHost* _host, USBDeviceConnected * _dev,
sayzyas 43:78f328f311dc 52 uint8_t _serial_intf, USBEndpoint* _bulk_in, USBEndpoint* _bulk_out);
sayzyas 43:78f328f311dc 53
sayzyas 43:78f328f311dc 54 /**
sayzyas 43:78f328f311dc 55 * Check the number of bytes available.
sayzyas 43:78f328f311dc 56 *
sayzyas 43:78f328f311dc 57 * @returns the number of bytes available
sayzyas 43:78f328f311dc 58 */
sayzyas 43:78f328f311dc 59 uint8_t available();
sayzyas 43:78f328f311dc 60
sayzyas 43:78f328f311dc 61 /**
sayzyas 43:78f328f311dc 62 * Attach a member function to call when a packet is received.
sayzyas 43:78f328f311dc 63 *
sayzyas 43:78f328f311dc 64 * @param tptr pointer to the object to call the member function on
sayzyas 43:78f328f311dc 65 * @param mptr pointer to the member function to be called
sayzyas 43:78f328f311dc 66 * @param irq irq type
sayzyas 43:78f328f311dc 67 */
sayzyas 43:78f328f311dc 68 template<typename T>
sayzyas 43:78f328f311dc 69 inline void attach(T* tptr, void (T::*mptr)(void), IrqType irq = RxIrq) {
sayzyas 43:78f328f311dc 70 if ((mptr != NULL) && (tptr != NULL)) {
sayzyas 43:78f328f311dc 71 if (irq == RxIrq) {
sayzyas 43:78f328f311dc 72 rx.attach(tptr, mptr);
sayzyas 43:78f328f311dc 73 } else {
sayzyas 43:78f328f311dc 74 tx.attach(tptr, mptr);
sayzyas 43:78f328f311dc 75 }
sayzyas 43:78f328f311dc 76 }
sayzyas 43:78f328f311dc 77 }
sayzyas 43:78f328f311dc 78
sayzyas 43:78f328f311dc 79 /**
sayzyas 43:78f328f311dc 80 * Attach a callback called when a packet is received
sayzyas 43:78f328f311dc 81 *
sayzyas 43:78f328f311dc 82 * @param ptr function pointer
sayzyas 43:78f328f311dc 83 */
sayzyas 43:78f328f311dc 84 inline void attach(void (*fn)(void), IrqType irq = RxIrq) {
sayzyas 43:78f328f311dc 85 if (fn != NULL) {
sayzyas 43:78f328f311dc 86 if (irq == RxIrq) {
sayzyas 43:78f328f311dc 87 rx.attach(fn);
sayzyas 43:78f328f311dc 88 } else {
sayzyas 43:78f328f311dc 89 tx.attach(fn);
sayzyas 43:78f328f311dc 90 }
sayzyas 43:78f328f311dc 91 }
sayzyas 43:78f328f311dc 92 }
sayzyas 43:78f328f311dc 93
sayzyas 43:78f328f311dc 94 /** Set the baud rate of the serial port
sayzyas 43:78f328f311dc 95 *
sayzyas 43:78f328f311dc 96 * @param baudrate The baudrate of the serial port (default = 9600).
sayzyas 43:78f328f311dc 97 */
sayzyas 43:78f328f311dc 98 void baud(int baudrate = 9600);
sayzyas 43:78f328f311dc 99
sayzyas 43:78f328f311dc 100 /** Set the transmission format used by the Serial port
sayzyas 43:78f328f311dc 101 *
sayzyas 43:78f328f311dc 102 * @param bits The number of bits in a word (default = 8)
sayzyas 43:78f328f311dc 103 * @param parity The parity used (USBHostSerialPort::None, USBHostSerialPort::Odd, USBHostSerialPort::Even, USBHostSerialPort::Mark, USBHostSerialPort::Space; default = USBHostSerialPort::None)
sayzyas 43:78f328f311dc 104 * @param stop The number of stop bits (1 or 2; default = 1)
sayzyas 43:78f328f311dc 105 */
sayzyas 43:78f328f311dc 106 void format(int bits = 8, Parity parity = USBHostSerialPort::None, int stop_bits = 1);
sayzyas 43:78f328f311dc 107 virtual int writeBuf(const char* b, int s);
sayzyas 43:78f328f311dc 108 virtual int readBuf(char* b, int s);
sayzyas 43:78f328f311dc 109
sayzyas 43:78f328f311dc 110 protected:
sayzyas 43:78f328f311dc 111 virtual int _getc();
sayzyas 43:78f328f311dc 112 virtual int _putc(int c);
sayzyas 43:78f328f311dc 113
sayzyas 43:78f328f311dc 114 private:
sayzyas 43:78f328f311dc 115 USBHost * host;
sayzyas 43:78f328f311dc 116 USBDeviceConnected * dev;
sayzyas 43:78f328f311dc 117
sayzyas 43:78f328f311dc 118 USBEndpoint * bulk_in;
sayzyas 43:78f328f311dc 119 USBEndpoint * bulk_out;
sayzyas 43:78f328f311dc 120 uint32_t size_bulk_in;
sayzyas 43:78f328f311dc 121 uint32_t size_bulk_out;
sayzyas 43:78f328f311dc 122
sayzyas 43:78f328f311dc 123 void init();
sayzyas 43:78f328f311dc 124
sayzyas 43:78f328f311dc 125 MtxCircBuffer<uint8_t, 128> circ_buf;
sayzyas 43:78f328f311dc 126
sayzyas 43:78f328f311dc 127 uint8_t buf[64];
sayzyas 43:78f328f311dc 128
sayzyas 43:78f328f311dc 129 typedef struct {
sayzyas 43:78f328f311dc 130 uint32_t baudrate;
sayzyas 43:78f328f311dc 131 uint8_t stop_bits;
sayzyas 43:78f328f311dc 132 uint8_t parity;
sayzyas 43:78f328f311dc 133 uint8_t data_bits;
sayzyas 43:78f328f311dc 134 } PACKED LINE_CODING;
sayzyas 43:78f328f311dc 135
sayzyas 43:78f328f311dc 136 LINE_CODING line_coding;
sayzyas 43:78f328f311dc 137
sayzyas 43:78f328f311dc 138 void rxHandler();
sayzyas 43:78f328f311dc 139 void txHandler();
sayzyas 43:78f328f311dc 140 FunctionPointer rx;
sayzyas 43:78f328f311dc 141 FunctionPointer tx;
sayzyas 43:78f328f311dc 142
sayzyas 43:78f328f311dc 143 uint8_t serial_intf;
sayzyas 43:78f328f311dc 144 };
sayzyas 43:78f328f311dc 145
sayzyas 43:78f328f311dc 146 #if (USBHOST_SERIAL <= 1)
sayzyas 43:78f328f311dc 147
sayzyas 43:78f328f311dc 148 class USBHostSerial : public IUSBEnumerator, public USBHostSerialPort
sayzyas 43:78f328f311dc 149 {
sayzyas 43:78f328f311dc 150 public:
sayzyas 43:78f328f311dc 151 USBHostSerial();
sayzyas 43:78f328f311dc 152
sayzyas 43:78f328f311dc 153 /**
sayzyas 43:78f328f311dc 154 * Try to connect a serial device
sayzyas 43:78f328f311dc 155 *
sayzyas 43:78f328f311dc 156 * @return true if connection was successful
sayzyas 43:78f328f311dc 157 */
sayzyas 43:78f328f311dc 158 bool connect();
sayzyas 43:78f328f311dc 159
sayzyas 43:78f328f311dc 160 void disconnect();
sayzyas 43:78f328f311dc 161
sayzyas 43:78f328f311dc 162 /**
sayzyas 43:78f328f311dc 163 * Check if a any serial port is connected
sayzyas 43:78f328f311dc 164 *
sayzyas 43:78f328f311dc 165 * @returns true if a serial device is connected
sayzyas 43:78f328f311dc 166 */
sayzyas 43:78f328f311dc 167 bool connected();
sayzyas 43:78f328f311dc 168
sayzyas 43:78f328f311dc 169 protected:
sayzyas 43:78f328f311dc 170 USBHost* host;
sayzyas 43:78f328f311dc 171 USBDeviceConnected* dev;
sayzyas 43:78f328f311dc 172 uint8_t port_intf;
sayzyas 43:78f328f311dc 173 int ports_found;
sayzyas 43:78f328f311dc 174
sayzyas 43:78f328f311dc 175 //From IUSBEnumerator
sayzyas 43:78f328f311dc 176 virtual void setVidPid(uint16_t vid, uint16_t pid);
sayzyas 43:78f328f311dc 177 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
sayzyas 43:78f328f311dc 178 virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
sayzyas 43:78f328f311dc 179
sayzyas 43:78f328f311dc 180 private:
sayzyas 43:78f328f311dc 181 bool dev_connected;
sayzyas 43:78f328f311dc 182 };
sayzyas 43:78f328f311dc 183
sayzyas 43:78f328f311dc 184 #else // (USBHOST_SERIAL > 1)
sayzyas 43:78f328f311dc 185
sayzyas 43:78f328f311dc 186 class USBHostMultiSerial : public IUSBEnumerator {
sayzyas 43:78f328f311dc 187 public:
sayzyas 43:78f328f311dc 188 USBHostMultiSerial();
sayzyas 43:78f328f311dc 189 virtual ~USBHostMultiSerial();
sayzyas 43:78f328f311dc 190
sayzyas 43:78f328f311dc 191 USBHostSerialPort* getPort(int port)
sayzyas 43:78f328f311dc 192 {
sayzyas 43:78f328f311dc 193 return port < USBHOST_SERIAL ? ports[port] : NULL;
sayzyas 43:78f328f311dc 194 }
sayzyas 43:78f328f311dc 195
sayzyas 43:78f328f311dc 196 /**
sayzyas 43:78f328f311dc 197 * Try to connect a serial device
sayzyas 43:78f328f311dc 198 *
sayzyas 43:78f328f311dc 199 * @return true if connection was successful
sayzyas 43:78f328f311dc 200 */
sayzyas 43:78f328f311dc 201 bool connect();
sayzyas 43:78f328f311dc 202
sayzyas 43:78f328f311dc 203 void disconnect();
sayzyas 43:78f328f311dc 204
sayzyas 43:78f328f311dc 205 /**
sayzyas 43:78f328f311dc 206 * Check if a any serial port is connected
sayzyas 43:78f328f311dc 207 *
sayzyas 43:78f328f311dc 208 * @returns true if a serial device is connected
sayzyas 43:78f328f311dc 209 */
sayzyas 43:78f328f311dc 210 bool connected();
sayzyas 43:78f328f311dc 211
sayzyas 43:78f328f311dc 212 protected:
sayzyas 43:78f328f311dc 213 USBHost* host;
sayzyas 43:78f328f311dc 214 USBDeviceConnected* dev;
sayzyas 43:78f328f311dc 215 USBHostSerialPort* ports[USBHOST_SERIAL];
sayzyas 43:78f328f311dc 216 uint8_t port_intf[USBHOST_SERIAL];
sayzyas 43:78f328f311dc 217 int ports_found;
sayzyas 43:78f328f311dc 218
sayzyas 43:78f328f311dc 219 //From IUSBEnumerator
sayzyas 43:78f328f311dc 220 virtual void setVidPid(uint16_t vid, uint16_t pid);
sayzyas 43:78f328f311dc 221 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
sayzyas 43:78f328f311dc 222 virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
sayzyas 43:78f328f311dc 223
sayzyas 43:78f328f311dc 224 private:
sayzyas 43:78f328f311dc 225 bool dev_connected;
sayzyas 43:78f328f311dc 226 };
sayzyas 43:78f328f311dc 227 #endif // (USBHOST_SERIAL <= 1)
sayzyas 43:78f328f311dc 228
sayzyas 43:78f328f311dc 229 #endif
sayzyas 43:78f328f311dc 230
sayzyas 43:78f328f311dc 231 #endif