USBHost library for WAVE file player
Fork of USBHost by
USBHostSerial/USBHostSerial.h@7:e7e63ac7ae5f, 2013-03-13 (annotated)
- Committer:
- samux
- Date:
- Wed Mar 13 17:30:42 2013 +0000
- Revision:
- 7:e7e63ac7ae5f
- Parent:
- 4:b320d68e98e7
- Child:
- 8:93da8ea2708b
update serial doc
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:a554658735bf | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
mbed_official | 0:a554658735bf | 2 | * |
mbed_official | 0:a554658735bf | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
mbed_official | 0:a554658735bf | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
mbed_official | 0:a554658735bf | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
mbed_official | 0:a554658735bf | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
mbed_official | 0:a554658735bf | 7 | * Software is furnished to do so, subject to the following conditions: |
mbed_official | 0:a554658735bf | 8 | * |
mbed_official | 0:a554658735bf | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
mbed_official | 0:a554658735bf | 10 | * substantial portions of the Software. |
mbed_official | 0:a554658735bf | 11 | * |
mbed_official | 0:a554658735bf | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
mbed_official | 0:a554658735bf | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
mbed_official | 0:a554658735bf | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
mbed_official | 0:a554658735bf | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbed_official | 0:a554658735bf | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
mbed_official | 0:a554658735bf | 17 | */ |
mbed_official | 0:a554658735bf | 18 | |
mbed_official | 0:a554658735bf | 19 | #ifndef USBHOSTSERIAL_H |
mbed_official | 0:a554658735bf | 20 | #define USBHOSTSERIAL_H |
mbed_official | 0:a554658735bf | 21 | |
mbed_official | 0:a554658735bf | 22 | #include "USBHostConf.h" |
mbed_official | 0:a554658735bf | 23 | |
mbed_official | 0:a554658735bf | 24 | #if USBHOST_SERIAL |
mbed_official | 0:a554658735bf | 25 | |
mbed_official | 0:a554658735bf | 26 | #include "USBHost.h" |
mbed_official | 0:a554658735bf | 27 | #include "Stream.h" |
mbed_official | 0:a554658735bf | 28 | #include "MtxCircBuffer.h" |
mbed_official | 0:a554658735bf | 29 | |
samux | 1:0a457e34fa9e | 30 | /** |
samux | 1:0a457e34fa9e | 31 | * A class to communicate a USB virtual serial port |
samux | 1:0a457e34fa9e | 32 | */ |
mbed_official | 0:a554658735bf | 33 | class USBHostSerial : public IUSBEnumerator, public Stream { |
mbed_official | 0:a554658735bf | 34 | public: |
mbed_official | 0:a554658735bf | 35 | /** |
mbed_official | 0:a554658735bf | 36 | * Constructor |
mbed_official | 0:a554658735bf | 37 | */ |
mbed_official | 0:a554658735bf | 38 | USBHostSerial(); |
mbed_official | 0:a554658735bf | 39 | |
mbed_official | 0:a554658735bf | 40 | enum IrqType { |
mbed_official | 0:a554658735bf | 41 | RxIrq, |
mbed_official | 0:a554658735bf | 42 | TxIrq |
mbed_official | 0:a554658735bf | 43 | }; |
samux | 4:b320d68e98e7 | 44 | |
samux | 4:b320d68e98e7 | 45 | enum Parity { |
samux | 4:b320d68e98e7 | 46 | None = 0, |
samux | 4:b320d68e98e7 | 47 | Odd, |
samux | 4:b320d68e98e7 | 48 | Even, |
samux | 4:b320d68e98e7 | 49 | Mark, |
samux | 4:b320d68e98e7 | 50 | Space |
samux | 4:b320d68e98e7 | 51 | }; |
mbed_official | 0:a554658735bf | 52 | |
mbed_official | 0:a554658735bf | 53 | /** |
mbed_official | 0:a554658735bf | 54 | * Check if a virtual serial port is connected |
mbed_official | 0:a554658735bf | 55 | * |
mbed_official | 0:a554658735bf | 56 | * @returns true if a serial device is connected |
mbed_official | 0:a554658735bf | 57 | */ |
mbed_official | 0:a554658735bf | 58 | bool connected(); |
mbed_official | 0:a554658735bf | 59 | |
mbed_official | 0:a554658735bf | 60 | /** |
mbed_official | 0:a554658735bf | 61 | * Try to connect a serial device |
mbed_official | 0:a554658735bf | 62 | * |
samux | 3:0f5c32575eb8 | 63 | * @return true if connection was successful |
mbed_official | 0:a554658735bf | 64 | */ |
mbed_official | 0:a554658735bf | 65 | bool connect(); |
mbed_official | 0:a554658735bf | 66 | |
mbed_official | 0:a554658735bf | 67 | /** |
mbed_official | 0:a554658735bf | 68 | * Check the number of bytes available. |
mbed_official | 0:a554658735bf | 69 | * |
mbed_official | 0:a554658735bf | 70 | * @returns the number of bytes available |
mbed_official | 0:a554658735bf | 71 | */ |
mbed_official | 0:a554658735bf | 72 | uint8_t available(); |
mbed_official | 0:a554658735bf | 73 | |
mbed_official | 0:a554658735bf | 74 | /** |
mbed_official | 0:a554658735bf | 75 | * Attach a member function to call when a packet is received. |
mbed_official | 0:a554658735bf | 76 | * |
mbed_official | 0:a554658735bf | 77 | * @param tptr pointer to the object to call the member function on |
mbed_official | 0:a554658735bf | 78 | * @param mptr pointer to the member function to be called |
samux | 2:5e8fdc541b98 | 79 | * @param irq irq type |
mbed_official | 0:a554658735bf | 80 | */ |
mbed_official | 0:a554658735bf | 81 | template<typename T> |
mbed_official | 0:a554658735bf | 82 | inline void attach(T* tptr, void (T::*mptr)(void), IrqType irq = RxIrq) { |
mbed_official | 0:a554658735bf | 83 | if ((mptr != NULL) && (tptr != NULL)) { |
mbed_official | 0:a554658735bf | 84 | if (irq == RxIrq) { |
mbed_official | 0:a554658735bf | 85 | rx.attach(tptr, mptr); |
mbed_official | 0:a554658735bf | 86 | } else { |
mbed_official | 0:a554658735bf | 87 | tx.attach(tptr, mptr); |
mbed_official | 0:a554658735bf | 88 | } |
mbed_official | 0:a554658735bf | 89 | } |
mbed_official | 0:a554658735bf | 90 | } |
mbed_official | 0:a554658735bf | 91 | |
mbed_official | 0:a554658735bf | 92 | /** |
mbed_official | 0:a554658735bf | 93 | * Attach a callback called when a packet is received |
mbed_official | 0:a554658735bf | 94 | * |
samux | 2:5e8fdc541b98 | 95 | * @param ptr function pointer |
mbed_official | 0:a554658735bf | 96 | */ |
mbed_official | 0:a554658735bf | 97 | inline void attach(void (*fn)(void), IrqType irq = RxIrq) { |
mbed_official | 0:a554658735bf | 98 | if (fn != NULL) { |
mbed_official | 0:a554658735bf | 99 | if (irq == RxIrq) { |
mbed_official | 0:a554658735bf | 100 | rx.attach(fn); |
mbed_official | 0:a554658735bf | 101 | } else { |
mbed_official | 0:a554658735bf | 102 | tx.attach(fn); |
mbed_official | 0:a554658735bf | 103 | } |
mbed_official | 0:a554658735bf | 104 | } |
mbed_official | 0:a554658735bf | 105 | } |
samux | 4:b320d68e98e7 | 106 | |
samux | 7:e7e63ac7ae5f | 107 | /** Set the baud rate of the serial port |
samux | 7:e7e63ac7ae5f | 108 | * |
samux | 7:e7e63ac7ae5f | 109 | * @param baudrate The baudrate of the serial port (default = 9600). |
samux | 7:e7e63ac7ae5f | 110 | */ |
samux | 7:e7e63ac7ae5f | 111 | void baud(int baudrate = 9600); |
samux | 7:e7e63ac7ae5f | 112 | |
samux | 7:e7e63ac7ae5f | 113 | /** Set the transmission format used by the Serial port |
samux | 7:e7e63ac7ae5f | 114 | * |
samux | 7:e7e63ac7ae5f | 115 | * @param bits The number of bits in a word (default = 8) |
samux | 7:e7e63ac7ae5f | 116 | * @param parity The parity used (USBHostSerial::None, USBHostSerial::Odd, USBHostSerial::Even, USBHostSerial::Mark, USBHostSerial::Space; default = USBHostSerial::None) |
samux | 7:e7e63ac7ae5f | 117 | * @param stop The number of stop bits (1 or 2; default = 1) |
samux | 7:e7e63ac7ae5f | 118 | */ |
samux | 4:b320d68e98e7 | 119 | void format(int bits = 8, Parity parity = USBHostSerial::None, int stop_bits = 1); |
mbed_official | 0:a554658735bf | 120 | |
mbed_official | 0:a554658735bf | 121 | |
mbed_official | 0:a554658735bf | 122 | protected: |
mbed_official | 0:a554658735bf | 123 | //From IUSBEnumerator |
mbed_official | 0:a554658735bf | 124 | virtual void setVidPid(uint16_t vid, uint16_t pid); |
mbed_official | 0:a554658735bf | 125 | 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 | 0:a554658735bf | 126 | virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used |
mbed_official | 0:a554658735bf | 127 | |
mbed_official | 0:a554658735bf | 128 | virtual int _getc(); |
mbed_official | 0:a554658735bf | 129 | virtual int _putc(int c); |
mbed_official | 0:a554658735bf | 130 | |
mbed_official | 0:a554658735bf | 131 | private: |
mbed_official | 0:a554658735bf | 132 | USBHost * host; |
mbed_official | 0:a554658735bf | 133 | USBDeviceConnected * dev; |
mbed_official | 0:a554658735bf | 134 | USBEndpoint * bulk_in; |
mbed_official | 0:a554658735bf | 135 | USBEndpoint * bulk_out; |
mbed_official | 0:a554658735bf | 136 | uint32_t size_bulk_in; |
mbed_official | 0:a554658735bf | 137 | uint32_t size_bulk_out; |
mbed_official | 0:a554658735bf | 138 | |
mbed_official | 0:a554658735bf | 139 | bool dev_connected; |
mbed_official | 0:a554658735bf | 140 | |
mbed_official | 0:a554658735bf | 141 | void init(); |
mbed_official | 0:a554658735bf | 142 | |
mbed_official | 0:a554658735bf | 143 | MtxCircBuffer<uint8_t, 64> circ_buf; |
mbed_official | 0:a554658735bf | 144 | |
mbed_official | 0:a554658735bf | 145 | uint8_t buf[64]; |
mbed_official | 0:a554658735bf | 146 | |
samux | 4:b320d68e98e7 | 147 | typedef __packed struct { |
samux | 4:b320d68e98e7 | 148 | uint32_t baudrate; |
samux | 4:b320d68e98e7 | 149 | uint8_t stop_bits; |
samux | 4:b320d68e98e7 | 150 | uint8_t parity; |
samux | 4:b320d68e98e7 | 151 | uint8_t data_bits; |
samux | 4:b320d68e98e7 | 152 | } LINE_CODING; |
samux | 4:b320d68e98e7 | 153 | |
samux | 4:b320d68e98e7 | 154 | LINE_CODING line_coding; |
samux | 4:b320d68e98e7 | 155 | |
mbed_official | 0:a554658735bf | 156 | void rxHandler(); |
mbed_official | 0:a554658735bf | 157 | void txHandler(); |
mbed_official | 0:a554658735bf | 158 | FunctionPointer rx; |
mbed_official | 0:a554658735bf | 159 | FunctionPointer tx; |
mbed_official | 0:a554658735bf | 160 | |
mbed_official | 0:a554658735bf | 161 | int serial_intf; |
mbed_official | 0:a554658735bf | 162 | bool serial_device_found; |
mbed_official | 0:a554658735bf | 163 | |
mbed_official | 0:a554658735bf | 164 | }; |
mbed_official | 0:a554658735bf | 165 | |
mbed_official | 0:a554658735bf | 166 | #endif |
mbed_official | 0:a554658735bf | 167 | |
mbed_official | 0:a554658735bf | 168 | #endif |