2018.07.26

Dependencies:   FATFileSystem2 mbed-rtos

Fork of USBHost by mbed official

Committer:
sayzyas
Date:
Thu Jul 26 00:19:16 2018 +0000
Revision:
43:78f328f311dc
2018.07.26

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sayzyas 43:78f328f311dc 1 /* mbed USBHost Library
sayzyas 43:78f328f311dc 2 * Copyright (c) 2006-2013 ARM Limited
sayzyas 43:78f328f311dc 3 *
sayzyas 43:78f328f311dc 4 * Licensed under the Apache License, Version 2.0 (the "License");
sayzyas 43:78f328f311dc 5 * you may not use this file except in compliance with the License.
sayzyas 43:78f328f311dc 6 * You may obtain a copy of the License at
sayzyas 43:78f328f311dc 7 *
sayzyas 43:78f328f311dc 8 * http://www.apache.org/licenses/LICENSE-2.0
sayzyas 43:78f328f311dc 9 *
sayzyas 43:78f328f311dc 10 * Unless required by applicable law or agreed to in writing, software
sayzyas 43:78f328f311dc 11 * distributed under the License is distributed on an "AS IS" BASIS,
sayzyas 43:78f328f311dc 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sayzyas 43:78f328f311dc 13 * See the License for the specific language governing permissions and
sayzyas 43:78f328f311dc 14 * limitations under the License.
sayzyas 43:78f328f311dc 15 */
sayzyas 43:78f328f311dc 16
sayzyas 43:78f328f311dc 17 #ifndef USBENDPOINT_H
sayzyas 43:78f328f311dc 18 #define USBENDPOINT_H
sayzyas 43:78f328f311dc 19
sayzyas 43:78f328f311dc 20 #include "FunctionPointer.h"
sayzyas 43:78f328f311dc 21 #include "USBHostTypes.h"
sayzyas 43:78f328f311dc 22 #include "rtos.h"
sayzyas 43:78f328f311dc 23
sayzyas 43:78f328f311dc 24 class USBDeviceConnected;
sayzyas 43:78f328f311dc 25
sayzyas 43:78f328f311dc 26 /**
sayzyas 43:78f328f311dc 27 * USBEndpoint class
sayzyas 43:78f328f311dc 28 */
sayzyas 43:78f328f311dc 29 class USBEndpoint
sayzyas 43:78f328f311dc 30 {
sayzyas 43:78f328f311dc 31 public:
sayzyas 43:78f328f311dc 32 /**
sayzyas 43:78f328f311dc 33 * Constructor
sayzyas 43:78f328f311dc 34 */
sayzyas 43:78f328f311dc 35 USBEndpoint() {
sayzyas 43:78f328f311dc 36 state = USB_TYPE_FREE;
sayzyas 43:78f328f311dc 37 nextEp = NULL;
sayzyas 43:78f328f311dc 38 };
sayzyas 43:78f328f311dc 39
sayzyas 43:78f328f311dc 40 /**
sayzyas 43:78f328f311dc 41 * Initialize an endpoint
sayzyas 43:78f328f311dc 42 *
sayzyas 43:78f328f311dc 43 * @param hced hced associated to the endpoint
sayzyas 43:78f328f311dc 44 * @param type endpoint type
sayzyas 43:78f328f311dc 45 * @param dir endpoint direction
sayzyas 43:78f328f311dc 46 * @param size endpoint size
sayzyas 43:78f328f311dc 47 * @param ep_number endpoint number
sayzyas 43:78f328f311dc 48 * @param td_list array of two allocated transfer descriptors
sayzyas 43:78f328f311dc 49 */
sayzyas 43:78f328f311dc 50 void init(HCED * hced, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint32_t size, uint8_t ep_number, HCTD* td_list[2]);
sayzyas 43:78f328f311dc 51
sayzyas 43:78f328f311dc 52 /**
sayzyas 43:78f328f311dc 53 * Set next token. Warning: only useful for the control endpoint
sayzyas 43:78f328f311dc 54 *
sayzyas 43:78f328f311dc 55 * @param token IN, OUT or SETUP token
sayzyas 43:78f328f311dc 56 */
sayzyas 43:78f328f311dc 57 void setNextToken(uint32_t token);
sayzyas 43:78f328f311dc 58
sayzyas 43:78f328f311dc 59 /**
sayzyas 43:78f328f311dc 60 * Queue an endpoint
sayzyas 43:78f328f311dc 61 *
sayzyas 43:78f328f311dc 62 * @param endpoint endpoint which will be queued in the linked list
sayzyas 43:78f328f311dc 63 */
sayzyas 43:78f328f311dc 64 void queueEndpoint(USBEndpoint * endpoint);
sayzyas 43:78f328f311dc 65
sayzyas 43:78f328f311dc 66
sayzyas 43:78f328f311dc 67 /**
sayzyas 43:78f328f311dc 68 * Queue a transfer on the endpoint
sayzyas 43:78f328f311dc 69 */
sayzyas 43:78f328f311dc 70 void queueTransfer();
sayzyas 43:78f328f311dc 71
sayzyas 43:78f328f311dc 72 /**
sayzyas 43:78f328f311dc 73 * Unqueue a transfer from the endpoint
sayzyas 43:78f328f311dc 74 *
sayzyas 43:78f328f311dc 75 * @param td hctd which will be unqueued
sayzyas 43:78f328f311dc 76 */
sayzyas 43:78f328f311dc 77 void unqueueTransfer(volatile HCTD * td);
sayzyas 43:78f328f311dc 78
sayzyas 43:78f328f311dc 79 /**
sayzyas 43:78f328f311dc 80 * Attach a member function to call when a transfer is finished
sayzyas 43:78f328f311dc 81 *
sayzyas 43:78f328f311dc 82 * @param tptr pointer to the object to call the member function on
sayzyas 43:78f328f311dc 83 * @param mptr pointer to the member function to be called
sayzyas 43:78f328f311dc 84 */
sayzyas 43:78f328f311dc 85 template<typename T>
sayzyas 43:78f328f311dc 86 inline void attach(T* tptr, void (T::*mptr)(void)) {
sayzyas 43:78f328f311dc 87 if((mptr != NULL) && (tptr != NULL)) {
sayzyas 43:78f328f311dc 88 rx.attach(tptr, mptr);
sayzyas 43:78f328f311dc 89 }
sayzyas 43:78f328f311dc 90 }
sayzyas 43:78f328f311dc 91
sayzyas 43:78f328f311dc 92 /**
sayzyas 43:78f328f311dc 93 * Attach a callback called when a transfer is finished
sayzyas 43:78f328f311dc 94 *
sayzyas 43:78f328f311dc 95 * @param fptr function pointer
sayzyas 43:78f328f311dc 96 */
sayzyas 43:78f328f311dc 97 inline void attach(void (*fptr)(void)) {
sayzyas 43:78f328f311dc 98 if(fptr != NULL) {
sayzyas 43:78f328f311dc 99 rx.attach(fptr);
sayzyas 43:78f328f311dc 100 }
sayzyas 43:78f328f311dc 101 }
sayzyas 43:78f328f311dc 102
sayzyas 43:78f328f311dc 103 /**
sayzyas 43:78f328f311dc 104 * Call the handler associted to the end of a transfer
sayzyas 43:78f328f311dc 105 */
sayzyas 43:78f328f311dc 106 inline void call() {
sayzyas 43:78f328f311dc 107 rx.call();
sayzyas 43:78f328f311dc 108 };
sayzyas 43:78f328f311dc 109
sayzyas 43:78f328f311dc 110
sayzyas 43:78f328f311dc 111 // setters
sayzyas 43:78f328f311dc 112 inline void setState(USB_TYPE st) { state = st; }
sayzyas 43:78f328f311dc 113 void setState(uint8_t st);
sayzyas 43:78f328f311dc 114 void setDeviceAddress(uint8_t addr);
sayzyas 43:78f328f311dc 115 inline void setLengthTransferred(int len) { transferred = len; };
sayzyas 43:78f328f311dc 116 void setSpeed(uint8_t speed);
sayzyas 43:78f328f311dc 117 void setSize(uint32_t size);
sayzyas 43:78f328f311dc 118 inline void setDir(ENDPOINT_DIRECTION d) { dir = d; }
sayzyas 43:78f328f311dc 119 inline void setIntfNb(uint8_t intf_nb_) { intf_nb = intf_nb_; };
sayzyas 43:78f328f311dc 120
sayzyas 43:78f328f311dc 121 // getters
sayzyas 43:78f328f311dc 122 const char * getStateString();
sayzyas 43:78f328f311dc 123 inline USB_TYPE getState() { return state; }
sayzyas 43:78f328f311dc 124 inline ENDPOINT_TYPE getType() { return type; };
sayzyas 43:78f328f311dc 125 inline uint8_t getDeviceAddress() { return hced->control & 0x7f; };
sayzyas 43:78f328f311dc 126 inline int getLengthTransferred() { return transferred; }
sayzyas 43:78f328f311dc 127 inline uint8_t * getBufStart() { return buf_start; }
sayzyas 43:78f328f311dc 128 inline uint8_t getAddress(){ return address; };
sayzyas 43:78f328f311dc 129 inline uint32_t getSize() { return (hced->control >> 16) & 0x3ff; };
sayzyas 43:78f328f311dc 130 inline volatile HCTD * getHeadTD() { return (volatile HCTD*) ((uint32_t)hced->headTD & ~0xF); };
sayzyas 43:78f328f311dc 131 inline volatile HCTD** getTDList() { return td_list; };
sayzyas 43:78f328f311dc 132 inline volatile HCED * getHCED() { return hced; };
sayzyas 43:78f328f311dc 133 inline ENDPOINT_DIRECTION getDir() { return dir; }
sayzyas 43:78f328f311dc 134 inline volatile HCTD * getProcessedTD() { return td_current; };
sayzyas 43:78f328f311dc 135 inline volatile HCTD* getNextTD() { return td_current; };
sayzyas 43:78f328f311dc 136 inline bool isSetup() { return setup; }
sayzyas 43:78f328f311dc 137 inline USBEndpoint * nextEndpoint() { return (USBEndpoint*)nextEp; };
sayzyas 43:78f328f311dc 138 inline uint8_t getIntfNb() { return intf_nb; };
sayzyas 43:78f328f311dc 139
sayzyas 43:78f328f311dc 140 USBDeviceConnected * dev;
sayzyas 43:78f328f311dc 141
sayzyas 43:78f328f311dc 142 Queue<uint8_t, 1> ep_queue;
sayzyas 43:78f328f311dc 143
sayzyas 43:78f328f311dc 144 private:
sayzyas 43:78f328f311dc 145 ENDPOINT_TYPE type;
sayzyas 43:78f328f311dc 146 volatile USB_TYPE state;
sayzyas 43:78f328f311dc 147 ENDPOINT_DIRECTION dir;
sayzyas 43:78f328f311dc 148 bool setup;
sayzyas 43:78f328f311dc 149
sayzyas 43:78f328f311dc 150 uint8_t address;
sayzyas 43:78f328f311dc 151
sayzyas 43:78f328f311dc 152 int transfer_len;
sayzyas 43:78f328f311dc 153 int transferred;
sayzyas 43:78f328f311dc 154 uint8_t * buf_start;
sayzyas 43:78f328f311dc 155
sayzyas 43:78f328f311dc 156 FunctionPointer rx;
sayzyas 43:78f328f311dc 157
sayzyas 43:78f328f311dc 158 USBEndpoint* nextEp;
sayzyas 43:78f328f311dc 159
sayzyas 43:78f328f311dc 160 // USBEndpoint descriptor
sayzyas 43:78f328f311dc 161 volatile HCED * hced;
sayzyas 43:78f328f311dc 162
sayzyas 43:78f328f311dc 163 volatile HCTD * td_list[2];
sayzyas 43:78f328f311dc 164 volatile HCTD * td_current;
sayzyas 43:78f328f311dc 165 volatile HCTD * td_next;
sayzyas 43:78f328f311dc 166
sayzyas 43:78f328f311dc 167 uint8_t intf_nb;
sayzyas 43:78f328f311dc 168
sayzyas 43:78f328f311dc 169 };
sayzyas 43:78f328f311dc 170
sayzyas 43:78f328f311dc 171 #endif