USB Host Library for Sprint Dongles
Fork of USBHostWANDongleSprint by
USBHost/USBEndpoint.h@2:34c976009b70, 2012-09-24 (annotated)
- Committer:
- donatien
- Date:
- Mon Sep 24 09:08:49 2012 +0000
- Revision:
- 2:34c976009b70
- Parent:
- 0:bfed5767d0a5
Tweaks for Sprint dongle
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:bfed5767d0a5 | 1 | /* Copyright (c) 2010-2012 mbed.org, MIT License |
donatien | 0:bfed5767d0a5 | 2 | * |
donatien | 0:bfed5767d0a5 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
donatien | 0:bfed5767d0a5 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
donatien | 0:bfed5767d0a5 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
donatien | 0:bfed5767d0a5 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
donatien | 0:bfed5767d0a5 | 7 | * Software is furnished to do so, subject to the following conditions: |
donatien | 0:bfed5767d0a5 | 8 | * |
donatien | 0:bfed5767d0a5 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
donatien | 0:bfed5767d0a5 | 10 | * substantial portions of the Software. |
donatien | 0:bfed5767d0a5 | 11 | * |
donatien | 0:bfed5767d0a5 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
donatien | 0:bfed5767d0a5 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
donatien | 0:bfed5767d0a5 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
donatien | 0:bfed5767d0a5 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 0:bfed5767d0a5 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
donatien | 0:bfed5767d0a5 | 17 | */ |
donatien | 0:bfed5767d0a5 | 18 | |
donatien | 0:bfed5767d0a5 | 19 | #ifndef USBENDPOINT_H |
donatien | 0:bfed5767d0a5 | 20 | #define USBENDPOINT_H |
donatien | 0:bfed5767d0a5 | 21 | |
donatien | 0:bfed5767d0a5 | 22 | #include "stdint.h" |
donatien | 0:bfed5767d0a5 | 23 | #include "FunctionPointer.h" |
donatien | 0:bfed5767d0a5 | 24 | #include "USBHostTypes.h" |
donatien | 0:bfed5767d0a5 | 25 | |
donatien | 0:bfed5767d0a5 | 26 | |
donatien | 0:bfed5767d0a5 | 27 | enum ENDPOINT_TYPE { |
donatien | 0:bfed5767d0a5 | 28 | CONTROL_ENDPOINT = 0, |
donatien | 0:bfed5767d0a5 | 29 | ISOCHRONOUS_ENDPOINT, |
donatien | 0:bfed5767d0a5 | 30 | BULK_ENDPOINT, |
donatien | 0:bfed5767d0a5 | 31 | INTERRUPT_ENDPOINT |
donatien | 0:bfed5767d0a5 | 32 | }; |
donatien | 0:bfed5767d0a5 | 33 | |
donatien | 0:bfed5767d0a5 | 34 | enum ENDPOINT_DIRECTION { |
donatien | 0:bfed5767d0a5 | 35 | OUT = 1, |
donatien | 0:bfed5767d0a5 | 36 | IN |
donatien | 0:bfed5767d0a5 | 37 | }; |
donatien | 0:bfed5767d0a5 | 38 | |
donatien | 0:bfed5767d0a5 | 39 | class USBEndpoint { |
donatien | 0:bfed5767d0a5 | 40 | public: |
donatien | 0:bfed5767d0a5 | 41 | /* |
donatien | 0:bfed5767d0a5 | 42 | * Constructor |
donatien | 0:bfed5767d0a5 | 43 | */ |
donatien | 0:bfed5767d0a5 | 44 | USBEndpoint() {state = USB_TYPE_FREE; nextEp = NULL;}; |
donatien | 0:bfed5767d0a5 | 45 | |
donatien | 0:bfed5767d0a5 | 46 | /* |
donatien | 0:bfed5767d0a5 | 47 | * Initialize an endpoint |
donatien | 0:bfed5767d0a5 | 48 | * |
donatien | 0:bfed5767d0a5 | 49 | * @param hced hced associated to the endpoint |
donatien | 0:bfed5767d0a5 | 50 | * @param type endpoint type |
donatien | 0:bfed5767d0a5 | 51 | * @param dir endpoint direction |
donatien | 0:bfed5767d0a5 | 52 | * @param size endpoint size |
donatien | 0:bfed5767d0a5 | 53 | * @param ep_number endpoint number |
donatien | 0:bfed5767d0a5 | 54 | */ |
donatien | 0:bfed5767d0a5 | 55 | void init(HCED * hced, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint32_t size, uint8_t ep_number, HCTD* td_list[2]); |
donatien | 0:bfed5767d0a5 | 56 | |
donatien | 0:bfed5767d0a5 | 57 | /* |
donatien | 0:bfed5767d0a5 | 58 | * Set next token. Warining: only useful for the control endpoint |
donatien | 0:bfed5767d0a5 | 59 | * |
donatien | 0:bfed5767d0a5 | 60 | * @param token IN, OUT or SETUP token |
donatien | 0:bfed5767d0a5 | 61 | */ |
donatien | 0:bfed5767d0a5 | 62 | void setNextToken(uint32_t token); |
donatien | 0:bfed5767d0a5 | 63 | |
donatien | 0:bfed5767d0a5 | 64 | /* |
donatien | 0:bfed5767d0a5 | 65 | * Queue an endpoint |
donatien | 0:bfed5767d0a5 | 66 | * |
donatien | 0:bfed5767d0a5 | 67 | * endpoint endpoint which will be queued in the linked list |
donatien | 0:bfed5767d0a5 | 68 | */ |
donatien | 0:bfed5767d0a5 | 69 | void queueEndpoint(USBEndpoint * endpoint); |
donatien | 0:bfed5767d0a5 | 70 | |
donatien | 0:bfed5767d0a5 | 71 | /* |
donatien | 0:bfed5767d0a5 | 72 | * Get a td to be queued |
donatien | 0:bfed5767d0a5 | 73 | * |
donatien | 0:bfed5767d0a5 | 74 | * @returns td hctd which will be queued |
donatien | 0:bfed5767d0a5 | 75 | */ |
donatien | 0:bfed5767d0a5 | 76 | volatile HCTD* getNextTD(); |
donatien | 0:bfed5767d0a5 | 77 | |
donatien | 0:bfed5767d0a5 | 78 | /* |
donatien | 0:bfed5767d0a5 | 79 | * Queue a transfer on the endpoint |
donatien | 0:bfed5767d0a5 | 80 | * |
donatien | 0:bfed5767d0a5 | 81 | */ |
donatien | 0:bfed5767d0a5 | 82 | void queueTransfer(); |
donatien | 0:bfed5767d0a5 | 83 | |
donatien | 0:bfed5767d0a5 | 84 | /* |
donatien | 0:bfed5767d0a5 | 85 | * Get the currently processed td |
donatien | 0:bfed5767d0a5 | 86 | * |
donatien | 0:bfed5767d0a5 | 87 | * @returns td hctd that was queued |
donatien | 0:bfed5767d0a5 | 88 | */ |
donatien | 0:bfed5767d0a5 | 89 | volatile HCTD * getProcessedTD(); |
donatien | 0:bfed5767d0a5 | 90 | |
donatien | 0:bfed5767d0a5 | 91 | /* |
donatien | 0:bfed5767d0a5 | 92 | * Unqueue a transfer from the endpoint |
donatien | 0:bfed5767d0a5 | 93 | * |
donatien | 0:bfed5767d0a5 | 94 | * @param td hctd which will be unqueued |
donatien | 0:bfed5767d0a5 | 95 | */ |
donatien | 0:bfed5767d0a5 | 96 | void unqueueTransfer(volatile HCTD * td); |
donatien | 0:bfed5767d0a5 | 97 | |
donatien | 0:bfed5767d0a5 | 98 | /* |
donatien | 0:bfed5767d0a5 | 99 | * Return the next endpoint in the linked list |
donatien | 0:bfed5767d0a5 | 100 | * |
donatien | 0:bfed5767d0a5 | 101 | * @returns next endpoint |
donatien | 0:bfed5767d0a5 | 102 | */ |
donatien | 0:bfed5767d0a5 | 103 | USBEndpoint * nextEndpoint(); |
donatien | 0:bfed5767d0a5 | 104 | |
donatien | 0:bfed5767d0a5 | 105 | /** |
donatien | 0:bfed5767d0a5 | 106 | * Attach a member function to call when a transfer is finished |
donatien | 0:bfed5767d0a5 | 107 | * |
donatien | 0:bfed5767d0a5 | 108 | * @param tptr pointer to the object to call the member function on |
donatien | 0:bfed5767d0a5 | 109 | * @param mptr pointer to the member function to be called |
donatien | 0:bfed5767d0a5 | 110 | */ |
donatien | 0:bfed5767d0a5 | 111 | template<typename T> |
donatien | 0:bfed5767d0a5 | 112 | void attach(T* tptr, void (T::*mptr)(void)) { |
donatien | 0:bfed5767d0a5 | 113 | if((mptr != NULL) && (tptr != NULL)) { |
donatien | 0:bfed5767d0a5 | 114 | rx.attach(tptr, mptr); |
donatien | 0:bfed5767d0a5 | 115 | } |
donatien | 0:bfed5767d0a5 | 116 | } |
donatien | 0:bfed5767d0a5 | 117 | |
donatien | 0:bfed5767d0a5 | 118 | /** |
donatien | 0:bfed5767d0a5 | 119 | * Attach a callback called when a transfer is finished |
donatien | 0:bfed5767d0a5 | 120 | * |
donatien | 0:bfed5767d0a5 | 121 | * @param fptr function pointer |
donatien | 0:bfed5767d0a5 | 122 | */ |
donatien | 0:bfed5767d0a5 | 123 | void attach(void (*fn)(void)) { |
donatien | 0:bfed5767d0a5 | 124 | if(fn != NULL) { |
donatien | 0:bfed5767d0a5 | 125 | rx.attach(fn); |
donatien | 0:bfed5767d0a5 | 126 | } |
donatien | 0:bfed5767d0a5 | 127 | } |
donatien | 0:bfed5767d0a5 | 128 | |
donatien | 0:bfed5767d0a5 | 129 | /* |
donatien | 0:bfed5767d0a5 | 130 | * Call the handler associted to the end of a transfer |
donatien | 0:bfed5767d0a5 | 131 | */ |
donatien | 0:bfed5767d0a5 | 132 | void call() { |
donatien | 0:bfed5767d0a5 | 133 | rx.call(); |
donatien | 0:bfed5767d0a5 | 134 | }; |
donatien | 0:bfed5767d0a5 | 135 | |
donatien | 0:bfed5767d0a5 | 136 | |
donatien | 0:bfed5767d0a5 | 137 | /* |
donatien | 0:bfed5767d0a5 | 138 | * Setters |
donatien | 0:bfed5767d0a5 | 139 | */ |
donatien | 2:34c976009b70 | 140 | void setState(USB_TYPE st) { state = st;} |
donatien | 0:bfed5767d0a5 | 141 | void setDeviceAddress(uint8_t addr); |
donatien | 0:bfed5767d0a5 | 142 | void setLengthTransferred(int len); |
donatien | 0:bfed5767d0a5 | 143 | void setSpeed(uint8_t speed); |
donatien | 0:bfed5767d0a5 | 144 | void setSize(uint32_t size); |
donatien | 0:bfed5767d0a5 | 145 | void setDir(ENDPOINT_DIRECTION d) {dir = d;} |
donatien | 0:bfed5767d0a5 | 146 | |
donatien | 0:bfed5767d0a5 | 147 | /* |
donatien | 0:bfed5767d0a5 | 148 | * Getters |
donatien | 0:bfed5767d0a5 | 149 | */ |
donatien | 0:bfed5767d0a5 | 150 | USB_TYPE getState() {return state;} |
donatien | 0:bfed5767d0a5 | 151 | ENDPOINT_TYPE getType(); |
donatien | 0:bfed5767d0a5 | 152 | uint8_t getDeviceAddress(); |
donatien | 0:bfed5767d0a5 | 153 | int getLengthTransferred() {return transferred;} |
donatien | 0:bfed5767d0a5 | 154 | uint32_t getBufStart(); |
donatien | 0:bfed5767d0a5 | 155 | uint32_t getSize(); |
donatien | 0:bfed5767d0a5 | 156 | volatile HCTD * getHeadTD(); |
donatien | 0:bfed5767d0a5 | 157 | volatile HCTD** getTDList(); |
donatien | 0:bfed5767d0a5 | 158 | volatile HCED * getHCED(); |
donatien | 0:bfed5767d0a5 | 159 | ENDPOINT_DIRECTION getDir() {return dir;} |
donatien | 0:bfed5767d0a5 | 160 | bool isSetup() {return setup;} |
donatien | 0:bfed5767d0a5 | 161 | |
donatien | 0:bfed5767d0a5 | 162 | |
donatien | 0:bfed5767d0a5 | 163 | private: |
donatien | 0:bfed5767d0a5 | 164 | ENDPOINT_TYPE type; |
donatien | 0:bfed5767d0a5 | 165 | volatile USB_TYPE state; |
donatien | 0:bfed5767d0a5 | 166 | ENDPOINT_DIRECTION dir; |
donatien | 0:bfed5767d0a5 | 167 | bool setup; |
donatien | 0:bfed5767d0a5 | 168 | |
donatien | 0:bfed5767d0a5 | 169 | int transfer_len; |
donatien | 0:bfed5767d0a5 | 170 | int transferred; |
donatien | 0:bfed5767d0a5 | 171 | uint32_t buf_start; |
donatien | 0:bfed5767d0a5 | 172 | |
donatien | 0:bfed5767d0a5 | 173 | FunctionPointer rx; |
donatien | 0:bfed5767d0a5 | 174 | |
donatien | 0:bfed5767d0a5 | 175 | USBEndpoint* nextEp; |
donatien | 0:bfed5767d0a5 | 176 | |
donatien | 0:bfed5767d0a5 | 177 | // USBEndpoint descriptor |
donatien | 0:bfed5767d0a5 | 178 | volatile HCED * hced; |
donatien | 0:bfed5767d0a5 | 179 | |
donatien | 0:bfed5767d0a5 | 180 | volatile HCTD * td_list[2]; |
donatien | 0:bfed5767d0a5 | 181 | volatile HCTD * td_current; |
donatien | 0:bfed5767d0a5 | 182 | volatile HCTD * td_next; |
donatien | 0:bfed5767d0a5 | 183 | /*bool carry;*/ |
donatien | 0:bfed5767d0a5 | 184 | |
donatien | 0:bfed5767d0a5 | 185 | int count; |
donatien | 0:bfed5767d0a5 | 186 | |
donatien | 0:bfed5767d0a5 | 187 | }; |
donatien | 0:bfed5767d0a5 | 188 | |
donatien | 0:bfed5767d0a5 | 189 | #endif |