USBHost library
Dependents: x4180_Tank Totaleprogramma Handmatig Totaleprogramma1
Fork of USBHost by
USBHostSerial/USBHostSerial.h@8:93da8ea2708b, 2013-03-14 (annotated)
- Committer:
- samux
- Date:
- Thu Mar 14 11:49:53 2013 +0000
- Revision:
- 8:93da8ea2708b
- Parent:
- 7:e7e63ac7ae5f
- Child:
- 13:b58a2204422f
update doc + license
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 | |
samux | 1:0a457e34fa9e | 28 | /** |
samux | 1:0a457e34fa9e | 29 | * A class to communicate a USB virtual serial port |
samux | 1:0a457e34fa9e | 30 | */ |
mbed_official | 0:a554658735bf | 31 | class USBHostSerial : public IUSBEnumerator, 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 | 0:a554658735bf | 36 | USBHostSerial(); |
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 | 0:a554658735bf | 51 | /** |
mbed_official | 0:a554658735bf | 52 | * Check if a virtual serial port is connected |
mbed_official | 0:a554658735bf | 53 | * |
mbed_official | 0:a554658735bf | 54 | * @returns true if a serial device is connected |
mbed_official | 0:a554658735bf | 55 | */ |
mbed_official | 0:a554658735bf | 56 | bool connected(); |
mbed_official | 0:a554658735bf | 57 | |
mbed_official | 0:a554658735bf | 58 | /** |
mbed_official | 0:a554658735bf | 59 | * Try to connect a serial device |
mbed_official | 0:a554658735bf | 60 | * |
samux | 3:0f5c32575eb8 | 61 | * @return true if connection was successful |
mbed_official | 0:a554658735bf | 62 | */ |
mbed_official | 0:a554658735bf | 63 | bool connect(); |
mbed_official | 0:a554658735bf | 64 | |
mbed_official | 0:a554658735bf | 65 | /** |
mbed_official | 0:a554658735bf | 66 | * Check the number of bytes available. |
mbed_official | 0:a554658735bf | 67 | * |
mbed_official | 0:a554658735bf | 68 | * @returns the number of bytes available |
mbed_official | 0:a554658735bf | 69 | */ |
mbed_official | 0:a554658735bf | 70 | uint8_t available(); |
mbed_official | 0:a554658735bf | 71 | |
mbed_official | 0:a554658735bf | 72 | /** |
mbed_official | 0:a554658735bf | 73 | * Attach a member function to call when a packet is received. |
mbed_official | 0:a554658735bf | 74 | * |
mbed_official | 0:a554658735bf | 75 | * @param tptr pointer to the object to call the member function on |
mbed_official | 0:a554658735bf | 76 | * @param mptr pointer to the member function to be called |
samux | 2:5e8fdc541b98 | 77 | * @param irq irq type |
mbed_official | 0:a554658735bf | 78 | */ |
mbed_official | 0:a554658735bf | 79 | template<typename T> |
mbed_official | 0:a554658735bf | 80 | inline void attach(T* tptr, void (T::*mptr)(void), IrqType irq = RxIrq) { |
mbed_official | 0:a554658735bf | 81 | if ((mptr != NULL) && (tptr != NULL)) { |
mbed_official | 0:a554658735bf | 82 | if (irq == RxIrq) { |
mbed_official | 0:a554658735bf | 83 | rx.attach(tptr, mptr); |
mbed_official | 0:a554658735bf | 84 | } else { |
mbed_official | 0:a554658735bf | 85 | tx.attach(tptr, mptr); |
mbed_official | 0:a554658735bf | 86 | } |
mbed_official | 0:a554658735bf | 87 | } |
mbed_official | 0:a554658735bf | 88 | } |
mbed_official | 0:a554658735bf | 89 | |
mbed_official | 0:a554658735bf | 90 | /** |
mbed_official | 0:a554658735bf | 91 | * Attach a callback called when a packet is received |
mbed_official | 0:a554658735bf | 92 | * |
samux | 2:5e8fdc541b98 | 93 | * @param ptr function pointer |
mbed_official | 0:a554658735bf | 94 | */ |
mbed_official | 0:a554658735bf | 95 | inline void attach(void (*fn)(void), IrqType irq = RxIrq) { |
mbed_official | 0:a554658735bf | 96 | if (fn != NULL) { |
mbed_official | 0:a554658735bf | 97 | if (irq == RxIrq) { |
mbed_official | 0:a554658735bf | 98 | rx.attach(fn); |
mbed_official | 0:a554658735bf | 99 | } else { |
mbed_official | 0:a554658735bf | 100 | tx.attach(fn); |
mbed_official | 0:a554658735bf | 101 | } |
mbed_official | 0:a554658735bf | 102 | } |
mbed_official | 0:a554658735bf | 103 | } |
samux | 4:b320d68e98e7 | 104 | |
samux | 7:e7e63ac7ae5f | 105 | /** Set the baud rate of the serial port |
samux | 7:e7e63ac7ae5f | 106 | * |
samux | 7:e7e63ac7ae5f | 107 | * @param baudrate The baudrate of the serial port (default = 9600). |
samux | 7:e7e63ac7ae5f | 108 | */ |
samux | 7:e7e63ac7ae5f | 109 | void baud(int baudrate = 9600); |
samux | 7:e7e63ac7ae5f | 110 | |
samux | 7:e7e63ac7ae5f | 111 | /** Set the transmission format used by the Serial port |
samux | 7:e7e63ac7ae5f | 112 | * |
samux | 7:e7e63ac7ae5f | 113 | * @param bits The number of bits in a word (default = 8) |
samux | 7:e7e63ac7ae5f | 114 | * @param parity The parity used (USBHostSerial::None, USBHostSerial::Odd, USBHostSerial::Even, USBHostSerial::Mark, USBHostSerial::Space; default = USBHostSerial::None) |
samux | 7:e7e63ac7ae5f | 115 | * @param stop The number of stop bits (1 or 2; default = 1) |
samux | 7:e7e63ac7ae5f | 116 | */ |
samux | 4:b320d68e98e7 | 117 | void format(int bits = 8, Parity parity = USBHostSerial::None, int stop_bits = 1); |
mbed_official | 0:a554658735bf | 118 | |
mbed_official | 0:a554658735bf | 119 | |
mbed_official | 0:a554658735bf | 120 | protected: |
mbed_official | 0:a554658735bf | 121 | //From IUSBEnumerator |
mbed_official | 0:a554658735bf | 122 | virtual void setVidPid(uint16_t vid, uint16_t pid); |
mbed_official | 0:a554658735bf | 123 | 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 | 124 | 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 | 125 | |
mbed_official | 0:a554658735bf | 126 | virtual int _getc(); |
mbed_official | 0:a554658735bf | 127 | virtual int _putc(int c); |
mbed_official | 0:a554658735bf | 128 | |
mbed_official | 0:a554658735bf | 129 | private: |
mbed_official | 0:a554658735bf | 130 | USBHost * host; |
mbed_official | 0:a554658735bf | 131 | USBDeviceConnected * dev; |
mbed_official | 0:a554658735bf | 132 | USBEndpoint * bulk_in; |
mbed_official | 0:a554658735bf | 133 | USBEndpoint * bulk_out; |
mbed_official | 0:a554658735bf | 134 | uint32_t size_bulk_in; |
mbed_official | 0:a554658735bf | 135 | uint32_t size_bulk_out; |
mbed_official | 0:a554658735bf | 136 | |
mbed_official | 0:a554658735bf | 137 | bool dev_connected; |
mbed_official | 0:a554658735bf | 138 | |
mbed_official | 0:a554658735bf | 139 | void init(); |
mbed_official | 0:a554658735bf | 140 | |
mbed_official | 0:a554658735bf | 141 | MtxCircBuffer<uint8_t, 64> circ_buf; |
mbed_official | 0:a554658735bf | 142 | |
mbed_official | 0:a554658735bf | 143 | uint8_t buf[64]; |
mbed_official | 0:a554658735bf | 144 | |
samux | 4:b320d68e98e7 | 145 | typedef __packed struct { |
samux | 4:b320d68e98e7 | 146 | uint32_t baudrate; |
samux | 4:b320d68e98e7 | 147 | uint8_t stop_bits; |
samux | 4:b320d68e98e7 | 148 | uint8_t parity; |
samux | 4:b320d68e98e7 | 149 | uint8_t data_bits; |
samux | 4:b320d68e98e7 | 150 | } LINE_CODING; |
samux | 4:b320d68e98e7 | 151 | |
samux | 4:b320d68e98e7 | 152 | LINE_CODING line_coding; |
samux | 4:b320d68e98e7 | 153 | |
mbed_official | 0:a554658735bf | 154 | void rxHandler(); |
mbed_official | 0:a554658735bf | 155 | void txHandler(); |
mbed_official | 0:a554658735bf | 156 | FunctionPointer rx; |
mbed_official | 0:a554658735bf | 157 | FunctionPointer tx; |
mbed_official | 0:a554658735bf | 158 | |
mbed_official | 0:a554658735bf | 159 | int serial_intf; |
mbed_official | 0:a554658735bf | 160 | bool serial_device_found; |
mbed_official | 0:a554658735bf | 161 | |
mbed_official | 0:a554658735bf | 162 | }; |
mbed_official | 0:a554658735bf | 163 | |
mbed_official | 0:a554658735bf | 164 | #endif |
mbed_official | 0:a554658735bf | 165 | |
mbed_official | 0:a554658735bf | 166 | #endif |