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