Support Isochronous transfer additionally
Dependents: USBHostC270_example_GR-PEACH USBHostDac_example USBHostDac_Audio_in_out
Fork of USBHost_custom by
USBHostSerial/USBHostSerial.h@36:f8c0979c2814, 2017-01-23 (annotated)
- Committer:
- dkato
- Date:
- Mon Jan 23 06:05:50 2017 +0000
- Revision:
- 36:f8c0979c2814
- Parent:
- 24:868cbfe611a7
- Child:
- 38:dd168a3e4194
Removed mbed-rtos so that it can be used with the mbed-os library.
; Partial modification of USBHostSerial.
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" |
mbed_official | 0:a554658735bf | 27 | |
mbed_official | 24:868cbfe611a7 | 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 | }; |
mbed_official | 24:868cbfe611a7 | 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 | 24:868cbfe611a7 | 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 | |
dkato | 36:f8c0979c2814 | 54 | bool connected(); |
dkato | 36:f8c0979c2814 | 55 | |
mbed_official | 0:a554658735bf | 56 | /** |
mbed_official | 0:a554658735bf | 57 | * Check the number of bytes available. |
mbed_official | 0:a554658735bf | 58 | * |
mbed_official | 0:a554658735bf | 59 | * @returns the number of bytes available |
mbed_official | 0:a554658735bf | 60 | */ |
dkato | 36:f8c0979c2814 | 61 | #if(1) |
dkato | 36:f8c0979c2814 | 62 | uint32_t available(); |
dkato | 36:f8c0979c2814 | 63 | #else |
mbed_official | 24:868cbfe611a7 | 64 | uint8_t available(); |
dkato | 36:f8c0979c2814 | 65 | #endif |
mbed_official | 0:a554658735bf | 66 | |
mbed_official | 0:a554658735bf | 67 | /** |
mbed_official | 0:a554658735bf | 68 | * Attach a member function to call when a packet is received. |
mbed_official | 0:a554658735bf | 69 | * |
mbed_official | 0:a554658735bf | 70 | * @param tptr pointer to the object to call the member function on |
mbed_official | 0:a554658735bf | 71 | * @param mptr pointer to the member function to be called |
samux | 2:5e8fdc541b98 | 72 | * @param irq irq type |
mbed_official | 0:a554658735bf | 73 | */ |
mbed_official | 0:a554658735bf | 74 | template<typename T> |
mbed_official | 0:a554658735bf | 75 | inline void attach(T* tptr, void (T::*mptr)(void), IrqType irq = RxIrq) { |
mbed_official | 0:a554658735bf | 76 | if ((mptr != NULL) && (tptr != NULL)) { |
mbed_official | 0:a554658735bf | 77 | if (irq == RxIrq) { |
mbed_official | 0:a554658735bf | 78 | rx.attach(tptr, mptr); |
mbed_official | 0:a554658735bf | 79 | } else { |
mbed_official | 0:a554658735bf | 80 | tx.attach(tptr, mptr); |
mbed_official | 0:a554658735bf | 81 | } |
mbed_official | 0:a554658735bf | 82 | } |
mbed_official | 0:a554658735bf | 83 | } |
mbed_official | 0:a554658735bf | 84 | |
mbed_official | 0:a554658735bf | 85 | /** |
mbed_official | 0:a554658735bf | 86 | * Attach a callback called when a packet is received |
mbed_official | 0:a554658735bf | 87 | * |
samux | 2:5e8fdc541b98 | 88 | * @param ptr function pointer |
mbed_official | 0:a554658735bf | 89 | */ |
mbed_official | 0:a554658735bf | 90 | inline void attach(void (*fn)(void), IrqType irq = RxIrq) { |
mbed_official | 0:a554658735bf | 91 | if (fn != NULL) { |
mbed_official | 0:a554658735bf | 92 | if (irq == RxIrq) { |
mbed_official | 0:a554658735bf | 93 | rx.attach(fn); |
mbed_official | 0:a554658735bf | 94 | } else { |
mbed_official | 0:a554658735bf | 95 | tx.attach(fn); |
mbed_official | 0:a554658735bf | 96 | } |
mbed_official | 0:a554658735bf | 97 | } |
mbed_official | 0:a554658735bf | 98 | } |
mbed_official | 24:868cbfe611a7 | 99 | |
samux | 7:e7e63ac7ae5f | 100 | /** Set the baud rate of the serial port |
samux | 7:e7e63ac7ae5f | 101 | * |
samux | 7:e7e63ac7ae5f | 102 | * @param baudrate The baudrate of the serial port (default = 9600). |
samux | 7:e7e63ac7ae5f | 103 | */ |
samux | 7:e7e63ac7ae5f | 104 | void baud(int baudrate = 9600); |
mbed_official | 24:868cbfe611a7 | 105 | |
samux | 7:e7e63ac7ae5f | 106 | /** Set the transmission format used by the Serial port |
samux | 7:e7e63ac7ae5f | 107 | * |
samux | 7:e7e63ac7ae5f | 108 | * @param bits The number of bits in a word (default = 8) |
mbed_official | 19:bd46ea19486b | 109 | * @param parity The parity used (USBHostSerialPort::None, USBHostSerialPort::Odd, USBHostSerialPort::Even, USBHostSerialPort::Mark, USBHostSerialPort::Space; default = USBHostSerialPort::None) |
samux | 7:e7e63ac7ae5f | 110 | * @param stop The number of stop bits (1 or 2; default = 1) |
samux | 7:e7e63ac7ae5f | 111 | */ |
mbed_official | 19:bd46ea19486b | 112 | void format(int bits = 8, Parity parity = USBHostSerialPort::None, int stop_bits = 1); |
mbed_official | 19:bd46ea19486b | 113 | virtual int writeBuf(const char* b, int s); |
dkato | 36:f8c0979c2814 | 114 | virtual int readBuf(char* b, int s, int timeout = -1); |
mbed_official | 0:a554658735bf | 115 | |
mbed_official | 0:a554658735bf | 116 | protected: |
mbed_official | 0:a554658735bf | 117 | virtual int _getc(); |
mbed_official | 0:a554658735bf | 118 | virtual int _putc(int c); |
mbed_official | 24:868cbfe611a7 | 119 | |
mbed_official | 0:a554658735bf | 120 | private: |
mbed_official | 0:a554658735bf | 121 | USBHost * host; |
mbed_official | 0:a554658735bf | 122 | USBDeviceConnected * dev; |
mbed_official | 19:bd46ea19486b | 123 | |
mbed_official | 0:a554658735bf | 124 | USBEndpoint * bulk_in; |
mbed_official | 0:a554658735bf | 125 | USBEndpoint * bulk_out; |
mbed_official | 0:a554658735bf | 126 | uint32_t size_bulk_in; |
mbed_official | 0:a554658735bf | 127 | uint32_t size_bulk_out; |
mbed_official | 0:a554658735bf | 128 | |
mbed_official | 0:a554658735bf | 129 | void init(); |
mbed_official | 0:a554658735bf | 130 | |
dkato | 36:f8c0979c2814 | 131 | // MtxCircBuffer<uint8_t, 128> circ_buf; |
dkato | 36:f8c0979c2814 | 132 | MtxCircBuffer<uint8_t, (1024 * 32)> circ_buf; |
mbed_official | 0:a554658735bf | 133 | |
mbed_official | 0:a554658735bf | 134 | uint8_t buf[64]; |
mbed_official | 0:a554658735bf | 135 | |
mbed_official | 13:b58a2204422f | 136 | typedef struct { |
samux | 4:b320d68e98e7 | 137 | uint32_t baudrate; |
samux | 4:b320d68e98e7 | 138 | uint8_t stop_bits; |
samux | 4:b320d68e98e7 | 139 | uint8_t parity; |
samux | 4:b320d68e98e7 | 140 | uint8_t data_bits; |
mbed_official | 13:b58a2204422f | 141 | } PACKED LINE_CODING; |
mbed_official | 24:868cbfe611a7 | 142 | |
samux | 4:b320d68e98e7 | 143 | LINE_CODING line_coding; |
samux | 4:b320d68e98e7 | 144 | |
mbed_official | 0:a554658735bf | 145 | void rxHandler(); |
mbed_official | 0:a554658735bf | 146 | void txHandler(); |
mbed_official | 0:a554658735bf | 147 | FunctionPointer rx; |
mbed_official | 0:a554658735bf | 148 | FunctionPointer tx; |
mbed_official | 0:a554658735bf | 149 | |
mbed_official | 19:bd46ea19486b | 150 | uint8_t serial_intf; |
dkato | 36:f8c0979c2814 | 151 | bool dev_connected; |
mbed_official | 19:bd46ea19486b | 152 | }; |
mbed_official | 19:bd46ea19486b | 153 | |
mbed_official | 19:bd46ea19486b | 154 | #if (USBHOST_SERIAL <= 1) |
mbed_official | 19:bd46ea19486b | 155 | |
mbed_official | 24:868cbfe611a7 | 156 | class USBHostSerial : public IUSBEnumerator, public USBHostSerialPort |
mbed_official | 19:bd46ea19486b | 157 | { |
mbed_official | 24:868cbfe611a7 | 158 | public: |
mbed_official | 19:bd46ea19486b | 159 | USBHostSerial(); |
mbed_official | 24:868cbfe611a7 | 160 | |
mbed_official | 19:bd46ea19486b | 161 | /** |
mbed_official | 19:bd46ea19486b | 162 | * Try to connect a serial device |
mbed_official | 19:bd46ea19486b | 163 | * |
mbed_official | 19:bd46ea19486b | 164 | * @return true if connection was successful |
mbed_official | 19:bd46ea19486b | 165 | */ |
mbed_official | 19:bd46ea19486b | 166 | bool connect(); |
mbed_official | 24:868cbfe611a7 | 167 | |
mbed_official | 19:bd46ea19486b | 168 | void disconnect(); |
mbed_official | 19:bd46ea19486b | 169 | |
mbed_official | 19:bd46ea19486b | 170 | /** |
mbed_official | 19:bd46ea19486b | 171 | * Check if a any serial port is connected |
mbed_official | 19:bd46ea19486b | 172 | * |
mbed_official | 19:bd46ea19486b | 173 | * @returns true if a serial device is connected |
mbed_official | 19:bd46ea19486b | 174 | */ |
dkato | 36:f8c0979c2814 | 175 | // bool connected(); |
mbed_official | 24:868cbfe611a7 | 176 | |
mbed_official | 19:bd46ea19486b | 177 | protected: |
mbed_official | 19:bd46ea19486b | 178 | USBHost* host; |
mbed_official | 19:bd46ea19486b | 179 | USBDeviceConnected* dev; |
mbed_official | 19:bd46ea19486b | 180 | uint8_t port_intf; |
mbed_official | 19:bd46ea19486b | 181 | int ports_found; |
mbed_official | 19:bd46ea19486b | 182 | |
mbed_official | 19:bd46ea19486b | 183 | //From IUSBEnumerator |
mbed_official | 19:bd46ea19486b | 184 | virtual void setVidPid(uint16_t vid, uint16_t pid); |
mbed_official | 19:bd46ea19486b | 185 | 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 | 186 | 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 | 187 | |
mbed_official | 19:bd46ea19486b | 188 | }; |
mbed_official | 19:bd46ea19486b | 189 | |
mbed_official | 19:bd46ea19486b | 190 | #else // (USBHOST_SERIAL > 1) |
mbed_official | 0:a554658735bf | 191 | |
mbed_official | 19:bd46ea19486b | 192 | class USBHostMultiSerial : public IUSBEnumerator { |
mbed_official | 24:868cbfe611a7 | 193 | public: |
mbed_official | 19:bd46ea19486b | 194 | USBHostMultiSerial(); |
mbed_official | 19:bd46ea19486b | 195 | virtual ~USBHostMultiSerial(); |
mbed_official | 24:868cbfe611a7 | 196 | |
mbed_official | 24:868cbfe611a7 | 197 | USBHostSerialPort* getPort(int port) |
mbed_official | 24:868cbfe611a7 | 198 | { |
mbed_official | 24:868cbfe611a7 | 199 | return port < USBHOST_SERIAL ? ports[port] : NULL; |
mbed_official | 19:bd46ea19486b | 200 | } |
mbed_official | 19:bd46ea19486b | 201 | |
mbed_official | 19:bd46ea19486b | 202 | /** |
mbed_official | 19:bd46ea19486b | 203 | * Try to connect a serial device |
mbed_official | 19:bd46ea19486b | 204 | * |
mbed_official | 19:bd46ea19486b | 205 | * @return true if connection was successful |
mbed_official | 19:bd46ea19486b | 206 | */ |
mbed_official | 19:bd46ea19486b | 207 | bool connect(); |
mbed_official | 24:868cbfe611a7 | 208 | |
mbed_official | 19:bd46ea19486b | 209 | void disconnect(); |
mbed_official | 19:bd46ea19486b | 210 | |
mbed_official | 19:bd46ea19486b | 211 | /** |
mbed_official | 19:bd46ea19486b | 212 | * Check if a any serial port is connected |
mbed_official | 19:bd46ea19486b | 213 | * |
mbed_official | 19:bd46ea19486b | 214 | * @returns true if a serial device is connected |
mbed_official | 19:bd46ea19486b | 215 | */ |
mbed_official | 19:bd46ea19486b | 216 | bool connected(); |
mbed_official | 24:868cbfe611a7 | 217 | |
mbed_official | 19:bd46ea19486b | 218 | protected: |
mbed_official | 19:bd46ea19486b | 219 | USBHost* host; |
mbed_official | 19:bd46ea19486b | 220 | USBDeviceConnected* dev; |
mbed_official | 19:bd46ea19486b | 221 | USBHostSerialPort* ports[USBHOST_SERIAL]; |
mbed_official | 19:bd46ea19486b | 222 | uint8_t port_intf[USBHOST_SERIAL]; |
mbed_official | 19:bd46ea19486b | 223 | int ports_found; |
mbed_official | 19:bd46ea19486b | 224 | |
mbed_official | 19:bd46ea19486b | 225 | //From IUSBEnumerator |
mbed_official | 19:bd46ea19486b | 226 | virtual void setVidPid(uint16_t vid, uint16_t pid); |
mbed_official | 19:bd46ea19486b | 227 | 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 | 228 | 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 | 229 | |
mbed_official | 0:a554658735bf | 230 | }; |
mbed_official | 19:bd46ea19486b | 231 | #endif // (USBHOST_SERIAL <= 1) |
mbed_official | 0:a554658735bf | 232 | |
mbed_official | 0:a554658735bf | 233 | #endif |
mbed_official | 0:a554658735bf | 234 | |
mbed_official | 0:a554658735bf | 235 | #endif |