old netservices
Fork of NetServices by
drv/serial/usb/UsbSerial.h@0:ac1725ba162c, 2010-11-09 (annotated)
- Committer:
- segundo
- Date:
- Tue Nov 09 20:54:15 2010 +0000
- Revision:
- 0:ac1725ba162c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
segundo | 0:ac1725ba162c | 1 | |
segundo | 0:ac1725ba162c | 2 | /* |
segundo | 0:ac1725ba162c | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
segundo | 0:ac1725ba162c | 4 | |
segundo | 0:ac1725ba162c | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
segundo | 0:ac1725ba162c | 6 | of this software and associated documentation files (the "Software"), to deal |
segundo | 0:ac1725ba162c | 7 | in the Software without restriction, including without limitation the rights |
segundo | 0:ac1725ba162c | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
segundo | 0:ac1725ba162c | 9 | copies of the Software, and to permit persons to whom the Software is |
segundo | 0:ac1725ba162c | 10 | furnished to do so, subject to the following conditions: |
segundo | 0:ac1725ba162c | 11 | |
segundo | 0:ac1725ba162c | 12 | The above copyright notice and this permission notice shall be included in |
segundo | 0:ac1725ba162c | 13 | all copies or substantial portions of the Software. |
segundo | 0:ac1725ba162c | 14 | |
segundo | 0:ac1725ba162c | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
segundo | 0:ac1725ba162c | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
segundo | 0:ac1725ba162c | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
segundo | 0:ac1725ba162c | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
segundo | 0:ac1725ba162c | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
segundo | 0:ac1725ba162c | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
segundo | 0:ac1725ba162c | 21 | THE SOFTWARE. |
segundo | 0:ac1725ba162c | 22 | */ |
segundo | 0:ac1725ba162c | 23 | |
segundo | 0:ac1725ba162c | 24 | #ifndef USB_SERIAL_H |
segundo | 0:ac1725ba162c | 25 | #define USB_SERIAL_H |
segundo | 0:ac1725ba162c | 26 | |
segundo | 0:ac1725ba162c | 27 | //DG 2010 |
segundo | 0:ac1725ba162c | 28 | //Essentially a clone of Serial if |
segundo | 0:ac1725ba162c | 29 | #include "Stream.h" |
segundo | 0:ac1725ba162c | 30 | #include "mbed.h" |
segundo | 0:ac1725ba162c | 31 | |
segundo | 0:ac1725ba162c | 32 | #include "drv/usb/UsbDevice.h" |
segundo | 0:ac1725ba162c | 33 | #include "drv/usb/UsbEndpoint.h" |
segundo | 0:ac1725ba162c | 34 | |
segundo | 0:ac1725ba162c | 35 | namespace mbed { |
segundo | 0:ac1725ba162c | 36 | |
segundo | 0:ac1725ba162c | 37 | class UsbSerial : public Stream { |
segundo | 0:ac1725ba162c | 38 | |
segundo | 0:ac1725ba162c | 39 | public: |
segundo | 0:ac1725ba162c | 40 | |
segundo | 0:ac1725ba162c | 41 | UsbSerial(UsbDevice* pDevice, int epIn, int epOut, const char* name = NULL); |
segundo | 0:ac1725ba162c | 42 | virtual ~UsbSerial(); |
segundo | 0:ac1725ba162c | 43 | //Apart from the ctor/dtor, exactly the same protos as Serial |
segundo | 0:ac1725ba162c | 44 | |
segundo | 0:ac1725ba162c | 45 | void baud(int baudrate); |
segundo | 0:ac1725ba162c | 46 | |
segundo | 0:ac1725ba162c | 47 | enum Parity { |
segundo | 0:ac1725ba162c | 48 | None = 0, |
segundo | 0:ac1725ba162c | 49 | Odd = 1, |
segundo | 0:ac1725ba162c | 50 | Even = 2, |
segundo | 0:ac1725ba162c | 51 | Forced1 = 3, |
segundo | 0:ac1725ba162c | 52 | Forced0 = 4 |
segundo | 0:ac1725ba162c | 53 | }; |
segundo | 0:ac1725ba162c | 54 | |
segundo | 0:ac1725ba162c | 55 | enum IrqType { |
segundo | 0:ac1725ba162c | 56 | RxIrq = 0 |
segundo | 0:ac1725ba162c | 57 | , TxIrq |
segundo | 0:ac1725ba162c | 58 | }; |
segundo | 0:ac1725ba162c | 59 | |
segundo | 0:ac1725ba162c | 60 | void format(int bits, int parity, int stop); |
segundo | 0:ac1725ba162c | 61 | |
segundo | 0:ac1725ba162c | 62 | class CDummy; |
segundo | 0:ac1725ba162c | 63 | template <class T> |
segundo | 0:ac1725ba162c | 64 | void attach(T* pCbItem, void (T::*pCbMeth)(), IrqType type = RxIrq) |
segundo | 0:ac1725ba162c | 65 | { |
segundo | 0:ac1725ba162c | 66 | if(type == RxIrq) |
segundo | 0:ac1725ba162c | 67 | { |
segundo | 0:ac1725ba162c | 68 | m_pInCbItem = (CDummy*) pCbItem; |
segundo | 0:ac1725ba162c | 69 | m_pInCbMeth = (void (CDummy::*)()) pCbMeth; |
segundo | 0:ac1725ba162c | 70 | } |
segundo | 0:ac1725ba162c | 71 | else |
segundo | 0:ac1725ba162c | 72 | { |
segundo | 0:ac1725ba162c | 73 | m_pOutCbItem = (CDummy*) pCbItem; |
segundo | 0:ac1725ba162c | 74 | m_pOutCbMeth = (void (CDummy::*)()) pCbMeth; |
segundo | 0:ac1725ba162c | 75 | } |
segundo | 0:ac1725ba162c | 76 | } |
segundo | 0:ac1725ba162c | 77 | |
segundo | 0:ac1725ba162c | 78 | #if 0 // Inhereted from Stream, for documentation only |
segundo | 0:ac1725ba162c | 79 | |
segundo | 0:ac1725ba162c | 80 | /* Function: putc |
segundo | 0:ac1725ba162c | 81 | * Write a character |
segundo | 0:ac1725ba162c | 82 | * |
segundo | 0:ac1725ba162c | 83 | * Variables: |
segundo | 0:ac1725ba162c | 84 | * c - The character to write to the serial port |
segundo | 0:ac1725ba162c | 85 | */ |
segundo | 0:ac1725ba162c | 86 | int putc(int c); |
segundo | 0:ac1725ba162c | 87 | |
segundo | 0:ac1725ba162c | 88 | /* Function: getc |
segundo | 0:ac1725ba162c | 89 | * Read a character |
segundo | 0:ac1725ba162c | 90 | * |
segundo | 0:ac1725ba162c | 91 | * Variables: |
segundo | 0:ac1725ba162c | 92 | * returns - The character read from the serial port |
segundo | 0:ac1725ba162c | 93 | */ |
segundo | 0:ac1725ba162c | 94 | int getc(); |
segundo | 0:ac1725ba162c | 95 | |
segundo | 0:ac1725ba162c | 96 | /* Function: printf |
segundo | 0:ac1725ba162c | 97 | * Write a formated string |
segundo | 0:ac1725ba162c | 98 | * |
segundo | 0:ac1725ba162c | 99 | * Variables: |
segundo | 0:ac1725ba162c | 100 | * format - A printf-style format string, followed by the |
segundo | 0:ac1725ba162c | 101 | * variables to use in formating the string. |
segundo | 0:ac1725ba162c | 102 | */ |
segundo | 0:ac1725ba162c | 103 | int printf(const char* format, ...); |
segundo | 0:ac1725ba162c | 104 | |
segundo | 0:ac1725ba162c | 105 | /* Function: scanf |
segundo | 0:ac1725ba162c | 106 | * Read a formated string |
segundo | 0:ac1725ba162c | 107 | * |
segundo | 0:ac1725ba162c | 108 | * Variables: |
segundo | 0:ac1725ba162c | 109 | * format - A scanf-style format string, |
segundo | 0:ac1725ba162c | 110 | * followed by the pointers to variables to store the results. |
segundo | 0:ac1725ba162c | 111 | */ |
segundo | 0:ac1725ba162c | 112 | int scanf(const char* format, ...); |
segundo | 0:ac1725ba162c | 113 | |
segundo | 0:ac1725ba162c | 114 | #endif |
segundo | 0:ac1725ba162c | 115 | |
segundo | 0:ac1725ba162c | 116 | /* Function: readable |
segundo | 0:ac1725ba162c | 117 | * Determine if there is a character available to read |
segundo | 0:ac1725ba162c | 118 | * |
segundo | 0:ac1725ba162c | 119 | * Variables: |
segundo | 0:ac1725ba162c | 120 | * returns - 1 if there is a character available to read, else 0 |
segundo | 0:ac1725ba162c | 121 | */ |
segundo | 0:ac1725ba162c | 122 | int readable(); |
segundo | 0:ac1725ba162c | 123 | |
segundo | 0:ac1725ba162c | 124 | /* Function: writeable |
segundo | 0:ac1725ba162c | 125 | * Determine if there is space available to write a character |
segundo | 0:ac1725ba162c | 126 | * |
segundo | 0:ac1725ba162c | 127 | * Variables: |
segundo | 0:ac1725ba162c | 128 | * returns - 1 if there is space to write a character, else 0 |
segundo | 0:ac1725ba162c | 129 | */ |
segundo | 0:ac1725ba162c | 130 | int writeable(); |
segundo | 0:ac1725ba162c | 131 | |
segundo | 0:ac1725ba162c | 132 | #ifdef MBED_RPC |
segundo | 0:ac1725ba162c | 133 | virtual const struct rpc_method *get_rpc_methods(); |
segundo | 0:ac1725ba162c | 134 | static struct rpc_class *get_rpc_class(); |
segundo | 0:ac1725ba162c | 135 | #endif |
segundo | 0:ac1725ba162c | 136 | |
segundo | 0:ac1725ba162c | 137 | protected: |
segundo | 0:ac1725ba162c | 138 | |
segundo | 0:ac1725ba162c | 139 | virtual int _getc(); |
segundo | 0:ac1725ba162c | 140 | virtual int _putc(int c); |
segundo | 0:ac1725ba162c | 141 | |
segundo | 0:ac1725ba162c | 142 | void onReadable(); |
segundo | 0:ac1725ba162c | 143 | void onWriteable(); |
segundo | 0:ac1725ba162c | 144 | |
segundo | 0:ac1725ba162c | 145 | void onEpInTransfer(); |
segundo | 0:ac1725ba162c | 146 | void onEpOutTransfer(); |
segundo | 0:ac1725ba162c | 147 | |
segundo | 0:ac1725ba162c | 148 | private: |
segundo | 0:ac1725ba162c | 149 | |
segundo | 0:ac1725ba162c | 150 | UsbEndpoint m_epIn; |
segundo | 0:ac1725ba162c | 151 | UsbEndpoint m_epOut; |
segundo | 0:ac1725ba162c | 152 | |
segundo | 0:ac1725ba162c | 153 | CDummy* m_pInCbItem; |
segundo | 0:ac1725ba162c | 154 | void (CDummy::*m_pInCbMeth)(); |
segundo | 0:ac1725ba162c | 155 | |
segundo | 0:ac1725ba162c | 156 | CDummy* m_pOutCbItem; |
segundo | 0:ac1725ba162c | 157 | void (CDummy::*m_pOutCbMeth)(); |
segundo | 0:ac1725ba162c | 158 | |
segundo | 0:ac1725ba162c | 159 | void startTx(); |
segundo | 0:ac1725ba162c | 160 | void startRx(); |
segundo | 0:ac1725ba162c | 161 | |
segundo | 0:ac1725ba162c | 162 | Timeout m_txTimeout; |
segundo | 0:ac1725ba162c | 163 | volatile bool m_timeout; |
segundo | 0:ac1725ba162c | 164 | |
segundo | 0:ac1725ba162c | 165 | volatile char* m_inBufEven; |
segundo | 0:ac1725ba162c | 166 | volatile char* m_inBufOdd; |
segundo | 0:ac1725ba162c | 167 | volatile char* m_inBufUsr; |
segundo | 0:ac1725ba162c | 168 | volatile char* m_inBufTrmt; |
segundo | 0:ac1725ba162c | 169 | |
segundo | 0:ac1725ba162c | 170 | volatile char* m_outBufEven; |
segundo | 0:ac1725ba162c | 171 | volatile char* m_outBufOdd; |
segundo | 0:ac1725ba162c | 172 | volatile char* m_outBufUsr; |
segundo | 0:ac1725ba162c | 173 | volatile char* m_outBufTrmt; |
segundo | 0:ac1725ba162c | 174 | |
segundo | 0:ac1725ba162c | 175 | volatile int m_inBufLen; |
segundo | 0:ac1725ba162c | 176 | volatile int m_outBufLen; |
segundo | 0:ac1725ba162c | 177 | |
segundo | 0:ac1725ba162c | 178 | volatile char* m_pInBufPos; |
segundo | 0:ac1725ba162c | 179 | volatile char* m_pOutBufPos; |
segundo | 0:ac1725ba162c | 180 | |
segundo | 0:ac1725ba162c | 181 | |
segundo | 0:ac1725ba162c | 182 | |
segundo | 0:ac1725ba162c | 183 | }; |
segundo | 0:ac1725ba162c | 184 | |
segundo | 0:ac1725ba162c | 185 | } |
segundo | 0:ac1725ba162c | 186 | |
segundo | 0:ac1725ba162c | 187 | |
segundo | 0:ac1725ba162c | 188 | |
segundo | 0:ac1725ba162c | 189 | #endif |