added USB host

Dependencies:   FATFileSystem mbed-rtos

Dependents:   app-board-Speaker

Fork of USBHost by mbed official

Committer:
mbed_official
Date:
Mon Dec 16 09:00:18 2013 +0000
Revision:
19:bd46ea19486b
Parent:
13:b58a2204422f
Child:
24:868cbfe611a7
Synchronized with git revision 170ac6562b7b2b5bb43f8ecf82b2af18b37eeb9c

Full URL: https://github.com/mbedmicro/mbed/commit/170ac6562b7b2b5bb43f8ecf82b2af18b37eeb9c/

improve USB host library and cellular modem stack

Who changed what in which revision?

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