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