mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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