USBHost library. NOTE: This library is only officially supported on the LPC1768 platform. For more information, please see the handbook page.
Dependencies: FATFileSystem mbed-rtos
Dependents: BTstack WallbotWii SD to Flash Data Transfer USBHost-MSD_HelloWorld ... more
Legacy Warning
This is an mbed 2 library. To learn more about mbed OS 5, visit the docs.
Pull requests against this repository are no longer supported. Please raise against mbed OS 5 as documented above.
USBHostSerial/USBHostSerial.h@40:7c3b59bb364e, 2017-08-17 (annotated)
- Committer:
- Anna Bridge
- Date:
- Thu Aug 17 18:12:22 2017 +0100
- Revision:
- 40:7c3b59bb364e
- Parent:
- 37:f1e388e7b752
DISCO_L475VG_IOT01A: Add support of USBHost
Who changed what in which revision?
User | Revision | Line number | New 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" |
Kojto | 37:f1e388e7b752 | 27 | #include "Callback.h" |
mbed_official | 0:a554658735bf | 28 | |
mbed_official | 24:868cbfe611a7 | 29 | /** |
samux | 1:0a457e34fa9e | 30 | * A class to communicate a USB virtual serial port |
samux | 1:0a457e34fa9e | 31 | */ |
mbed_official | 19:bd46ea19486b | 32 | class USBHostSerialPort : public Stream { |
mbed_official | 0:a554658735bf | 33 | public: |
mbed_official | 0:a554658735bf | 34 | /** |
mbed_official | 0:a554658735bf | 35 | * Constructor |
mbed_official | 0:a554658735bf | 36 | */ |
mbed_official | 19:bd46ea19486b | 37 | USBHostSerialPort(); |
mbed_official | 0:a554658735bf | 38 | |
mbed_official | 0:a554658735bf | 39 | enum IrqType { |
mbed_official | 0:a554658735bf | 40 | RxIrq, |
mbed_official | 0:a554658735bf | 41 | TxIrq |
mbed_official | 0:a554658735bf | 42 | }; |
mbed_official | 24:868cbfe611a7 | 43 | |
samux | 4:b320d68e98e7 | 44 | enum Parity { |
samux | 4:b320d68e98e7 | 45 | None = 0, |
samux | 4:b320d68e98e7 | 46 | Odd, |
samux | 4:b320d68e98e7 | 47 | Even, |
samux | 4:b320d68e98e7 | 48 | Mark, |
samux | 4:b320d68e98e7 | 49 | Space |
samux | 4:b320d68e98e7 | 50 | }; |
mbed_official | 0:a554658735bf | 51 | |
mbed_official | 24:868cbfe611a7 | 52 | void connect(USBHost* _host, USBDeviceConnected * _dev, |
mbed_official | 19:bd46ea19486b | 53 | uint8_t _serial_intf, USBEndpoint* _bulk_in, USBEndpoint* _bulk_out); |
mbed_official | 19:bd46ea19486b | 54 | |
mbed_official | 0:a554658735bf | 55 | /** |
mbed_official | 0:a554658735bf | 56 | * Check the number of bytes available. |
mbed_official | 0:a554658735bf | 57 | * |
mbed_official | 0:a554658735bf | 58 | * @returns the number of bytes available |
mbed_official | 0:a554658735bf | 59 | */ |
mbed_official | 24:868cbfe611a7 | 60 | uint8_t available(); |
mbed_official | 0:a554658735bf | 61 | |
mbed_official | 0:a554658735bf | 62 | /** |
mbed_official | 0:a554658735bf | 63 | * Attach a member function to call when a packet is received. |
mbed_official | 0:a554658735bf | 64 | * |
mbed_official | 0:a554658735bf | 65 | * @param tptr pointer to the object to call the member function on |
mbed_official | 0:a554658735bf | 66 | * @param mptr pointer to the member function to be called |
samux | 2:5e8fdc541b98 | 67 | * @param irq irq type |
mbed_official | 0:a554658735bf | 68 | */ |
mbed_official | 0:a554658735bf | 69 | template<typename T> |
mbed_official | 0:a554658735bf | 70 | inline void attach(T* tptr, void (T::*mptr)(void), IrqType irq = RxIrq) { |
mbed_official | 0:a554658735bf | 71 | if ((mptr != NULL) && (tptr != NULL)) { |
mbed_official | 0:a554658735bf | 72 | if (irq == RxIrq) { |
mbed_official | 0:a554658735bf | 73 | rx.attach(tptr, mptr); |
mbed_official | 0:a554658735bf | 74 | } else { |
mbed_official | 0:a554658735bf | 75 | tx.attach(tptr, mptr); |
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 | /** |
mbed_official | 0:a554658735bf | 81 | * Attach a callback called when a packet is received |
mbed_official | 0:a554658735bf | 82 | * |
samux | 2:5e8fdc541b98 | 83 | * @param ptr function pointer |
mbed_official | 0:a554658735bf | 84 | */ |
mbed_official | 0:a554658735bf | 85 | inline void attach(void (*fn)(void), IrqType irq = RxIrq) { |
mbed_official | 0:a554658735bf | 86 | if (fn != NULL) { |
mbed_official | 0:a554658735bf | 87 | if (irq == RxIrq) { |
mbed_official | 0:a554658735bf | 88 | rx.attach(fn); |
mbed_official | 0:a554658735bf | 89 | } else { |
mbed_official | 0:a554658735bf | 90 | tx.attach(fn); |
mbed_official | 0:a554658735bf | 91 | } |
mbed_official | 0:a554658735bf | 92 | } |
mbed_official | 0:a554658735bf | 93 | } |
mbed_official | 24:868cbfe611a7 | 94 | |
samux | 7:e7e63ac7ae5f | 95 | /** Set the baud rate of the serial port |
samux | 7:e7e63ac7ae5f | 96 | * |
samux | 7:e7e63ac7ae5f | 97 | * @param baudrate The baudrate of the serial port (default = 9600). |
samux | 7:e7e63ac7ae5f | 98 | */ |
samux | 7:e7e63ac7ae5f | 99 | void baud(int baudrate = 9600); |
mbed_official | 24:868cbfe611a7 | 100 | |
samux | 7:e7e63ac7ae5f | 101 | /** Set the transmission format used by the Serial port |
samux | 7:e7e63ac7ae5f | 102 | * |
samux | 7:e7e63ac7ae5f | 103 | * @param bits The number of bits in a word (default = 8) |
mbed_official | 19:bd46ea19486b | 104 | * @param parity The parity used (USBHostSerialPort::None, USBHostSerialPort::Odd, USBHostSerialPort::Even, USBHostSerialPort::Mark, USBHostSerialPort::Space; default = USBHostSerialPort::None) |
samux | 7:e7e63ac7ae5f | 105 | * @param stop The number of stop bits (1 or 2; default = 1) |
samux | 7:e7e63ac7ae5f | 106 | */ |
mbed_official | 19:bd46ea19486b | 107 | void format(int bits = 8, Parity parity = USBHostSerialPort::None, int stop_bits = 1); |
mbed_official | 19:bd46ea19486b | 108 | virtual int writeBuf(const char* b, int s); |
mbed_official | 19:bd46ea19486b | 109 | virtual int readBuf(char* b, int s); |
mbed_official | 0:a554658735bf | 110 | |
mbed_official | 0:a554658735bf | 111 | protected: |
mbed_official | 0:a554658735bf | 112 | virtual int _getc(); |
mbed_official | 0:a554658735bf | 113 | virtual int _putc(int c); |
mbed_official | 24:868cbfe611a7 | 114 | |
mbed_official | 0:a554658735bf | 115 | private: |
mbed_official | 0:a554658735bf | 116 | USBHost * host; |
mbed_official | 0:a554658735bf | 117 | USBDeviceConnected * dev; |
mbed_official | 19:bd46ea19486b | 118 | |
mbed_official | 0:a554658735bf | 119 | USBEndpoint * bulk_in; |
mbed_official | 0:a554658735bf | 120 | USBEndpoint * bulk_out; |
mbed_official | 0:a554658735bf | 121 | uint32_t size_bulk_in; |
mbed_official | 0:a554658735bf | 122 | uint32_t size_bulk_out; |
mbed_official | 0:a554658735bf | 123 | |
mbed_official | 0:a554658735bf | 124 | void init(); |
mbed_official | 0:a554658735bf | 125 | |
mbed_official | 19:bd46ea19486b | 126 | MtxCircBuffer<uint8_t, 128> circ_buf; |
mbed_official | 0:a554658735bf | 127 | |
mbed_official | 0:a554658735bf | 128 | uint8_t buf[64]; |
mbed_official | 0:a554658735bf | 129 | |
mbed_official | 13:b58a2204422f | 130 | typedef struct { |
samux | 4:b320d68e98e7 | 131 | uint32_t baudrate; |
samux | 4:b320d68e98e7 | 132 | uint8_t stop_bits; |
samux | 4:b320d68e98e7 | 133 | uint8_t parity; |
samux | 4:b320d68e98e7 | 134 | uint8_t data_bits; |
mbed_official | 13:b58a2204422f | 135 | } PACKED LINE_CODING; |
mbed_official | 24:868cbfe611a7 | 136 | |
samux | 4:b320d68e98e7 | 137 | LINE_CODING line_coding; |
samux | 4:b320d68e98e7 | 138 | |
mbed_official | 0:a554658735bf | 139 | void rxHandler(); |
mbed_official | 0:a554658735bf | 140 | void txHandler(); |
Kojto | 37:f1e388e7b752 | 141 | Callback<void()> rx; |
Kojto | 37:f1e388e7b752 | 142 | Callback<void()> tx; |
mbed_official | 0:a554658735bf | 143 | |
mbed_official | 19:bd46ea19486b | 144 | uint8_t serial_intf; |
mbed_official | 19:bd46ea19486b | 145 | }; |
mbed_official | 19:bd46ea19486b | 146 | |
mbed_official | 19:bd46ea19486b | 147 | #if (USBHOST_SERIAL <= 1) |
mbed_official | 19:bd46ea19486b | 148 | |
mbed_official | 24:868cbfe611a7 | 149 | class USBHostSerial : public IUSBEnumerator, public USBHostSerialPort |
mbed_official | 19:bd46ea19486b | 150 | { |
mbed_official | 24:868cbfe611a7 | 151 | public: |
mbed_official | 19:bd46ea19486b | 152 | USBHostSerial(); |
mbed_official | 24:868cbfe611a7 | 153 | |
mbed_official | 19:bd46ea19486b | 154 | /** |
mbed_official | 19:bd46ea19486b | 155 | * Try to connect a serial device |
mbed_official | 19:bd46ea19486b | 156 | * |
mbed_official | 19:bd46ea19486b | 157 | * @return true if connection was successful |
mbed_official | 19:bd46ea19486b | 158 | */ |
mbed_official | 19:bd46ea19486b | 159 | bool connect(); |
mbed_official | 24:868cbfe611a7 | 160 | |
mbed_official | 19:bd46ea19486b | 161 | void disconnect(); |
mbed_official | 19:bd46ea19486b | 162 | |
mbed_official | 19:bd46ea19486b | 163 | /** |
mbed_official | 19:bd46ea19486b | 164 | * Check if a any serial port is connected |
mbed_official | 19:bd46ea19486b | 165 | * |
mbed_official | 19:bd46ea19486b | 166 | * @returns true if a serial device is connected |
mbed_official | 19:bd46ea19486b | 167 | */ |
mbed_official | 19:bd46ea19486b | 168 | bool connected(); |
mbed_official | 24:868cbfe611a7 | 169 | |
mbed_official | 19:bd46ea19486b | 170 | protected: |
mbed_official | 19:bd46ea19486b | 171 | USBHost* host; |
mbed_official | 19:bd46ea19486b | 172 | USBDeviceConnected* dev; |
mbed_official | 19:bd46ea19486b | 173 | uint8_t port_intf; |
mbed_official | 19:bd46ea19486b | 174 | int ports_found; |
mbed_official | 19:bd46ea19486b | 175 | |
mbed_official | 19:bd46ea19486b | 176 | //From IUSBEnumerator |
mbed_official | 19:bd46ea19486b | 177 | virtual void setVidPid(uint16_t vid, uint16_t pid); |
mbed_official | 19:bd46ea19486b | 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 |
mbed_official | 19:bd46ea19486b | 179 | virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used |
mbed_official | 24:868cbfe611a7 | 180 | |
mbed_official | 19:bd46ea19486b | 181 | private: |
mbed_official | 19:bd46ea19486b | 182 | bool dev_connected; |
mbed_official | 19:bd46ea19486b | 183 | }; |
mbed_official | 19:bd46ea19486b | 184 | |
mbed_official | 19:bd46ea19486b | 185 | #else // (USBHOST_SERIAL > 1) |
mbed_official | 0:a554658735bf | 186 | |
mbed_official | 19:bd46ea19486b | 187 | class USBHostMultiSerial : public IUSBEnumerator { |
mbed_official | 24:868cbfe611a7 | 188 | public: |
mbed_official | 19:bd46ea19486b | 189 | USBHostMultiSerial(); |
mbed_official | 19:bd46ea19486b | 190 | virtual ~USBHostMultiSerial(); |
mbed_official | 24:868cbfe611a7 | 191 | |
mbed_official | 24:868cbfe611a7 | 192 | USBHostSerialPort* getPort(int port) |
mbed_official | 24:868cbfe611a7 | 193 | { |
mbed_official | 24:868cbfe611a7 | 194 | return port < USBHOST_SERIAL ? ports[port] : NULL; |
mbed_official | 19:bd46ea19486b | 195 | } |
mbed_official | 19:bd46ea19486b | 196 | |
mbed_official | 19:bd46ea19486b | 197 | /** |
mbed_official | 19:bd46ea19486b | 198 | * Try to connect a serial device |
mbed_official | 19:bd46ea19486b | 199 | * |
mbed_official | 19:bd46ea19486b | 200 | * @return true if connection was successful |
mbed_official | 19:bd46ea19486b | 201 | */ |
mbed_official | 19:bd46ea19486b | 202 | bool connect(); |
mbed_official | 24:868cbfe611a7 | 203 | |
mbed_official | 19:bd46ea19486b | 204 | void disconnect(); |
mbed_official | 19:bd46ea19486b | 205 | |
mbed_official | 19:bd46ea19486b | 206 | /** |
mbed_official | 19:bd46ea19486b | 207 | * Check if a any serial port is connected |
mbed_official | 19:bd46ea19486b | 208 | * |
mbed_official | 19:bd46ea19486b | 209 | * @returns true if a serial device is connected |
mbed_official | 19:bd46ea19486b | 210 | */ |
mbed_official | 19:bd46ea19486b | 211 | bool connected(); |
mbed_official | 24:868cbfe611a7 | 212 | |
mbed_official | 19:bd46ea19486b | 213 | protected: |
mbed_official | 19:bd46ea19486b | 214 | USBHost* host; |
mbed_official | 19:bd46ea19486b | 215 | USBDeviceConnected* dev; |
mbed_official | 19:bd46ea19486b | 216 | USBHostSerialPort* ports[USBHOST_SERIAL]; |
mbed_official | 19:bd46ea19486b | 217 | uint8_t port_intf[USBHOST_SERIAL]; |
mbed_official | 19:bd46ea19486b | 218 | int ports_found; |
mbed_official | 19:bd46ea19486b | 219 | |
mbed_official | 19:bd46ea19486b | 220 | //From IUSBEnumerator |
mbed_official | 19:bd46ea19486b | 221 | virtual void setVidPid(uint16_t vid, uint16_t pid); |
mbed_official | 19:bd46ea19486b | 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 |
mbed_official | 19:bd46ea19486b | 223 | virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used |
mbed_official | 24:868cbfe611a7 | 224 | |
mbed_official | 19:bd46ea19486b | 225 | private: |
mbed_official | 19:bd46ea19486b | 226 | bool dev_connected; |
mbed_official | 0:a554658735bf | 227 | }; |
mbed_official | 19:bd46ea19486b | 228 | #endif // (USBHOST_SERIAL <= 1) |
mbed_official | 0:a554658735bf | 229 | |
mbed_official | 0:a554658735bf | 230 | #endif |
mbed_official | 0:a554658735bf | 231 | |
mbed_official | 0:a554658735bf | 232 | #endif |