nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nitsshukla
Date:
Fri Nov 04 12:06:04 2016 +0000
Revision:
7:3a65ef12ba31
Parent:
1:55a6170b404f
kghj;

Who changed what in which revision?

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