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