]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/USBHost/USBHost/USBEndpoint.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / USBHost / USBHost / USBEndpoint.cpp
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
18 #include "dbg.h"
19 #include "USBEndpoint.h"
20
21 void USBEndpoint::init(HCED * hced_, ENDPOINT_TYPE type_, ENDPOINT_DIRECTION dir_, uint32_t size, uint8_t ep_number, HCTD* td_list_[2])
22 {
23     hced = hced_;
24     type = type_;
25     dir = dir_;
26     setup = (type == CONTROL_ENDPOINT) ? true : false;
27
28     //TDs have been allocated by the host
29     memcpy((HCTD**)td_list, td_list_, sizeof(HCTD*)*2); //TODO: Maybe should add a param for td_list size... at least a define
30     memset(td_list_[0], 0, sizeof(HCTD));
31     memset(td_list_[1], 0, sizeof(HCTD));
32
33     td_list[0]->ep = this;
34     td_list[1]->ep = this;
35
36     hced->control = 0;
37     //Empty queue
38     hced->tailTD = td_list[0];
39     hced->headTD = td_list[0];
40     hced->nextED = 0;
41
42     address = (ep_number & 0x7F) | ((dir - 1) << 7);
43
44     hced->control = ((ep_number & 0x7F) << 7)                         // Endpoint address
45                     | (type != CONTROL_ENDPOINT ? ( dir << 11) : 0 )  // direction : Out = 1, 2 = In
46                     | ((size & 0x3ff) << 16);                         // MaxPkt Size
47
48     transfer_len = 0;
49     transferred = 0;
50     buf_start = 0;
51     nextEp = NULL;
52
53     td_current = td_list[0];
54     td_next = td_list[1];
55
56     intf_nb = 0;
57
58     state = USB_TYPE_IDLE;
59 }
60
61 void USBEndpoint::setSize(uint32_t size)
62 {
63     hced->control &= ~(0x3ff << 16);
64     hced->control |= (size << 16);
65 }
66
67
68 void USBEndpoint::setDeviceAddress(uint8_t addr)
69 {
70     hced->control &= ~(0x7f);
71     hced->control |= (addr & 0x7F);
72 }
73
74 void USBEndpoint::setSpeed(uint8_t speed)
75 {
76     hced->control &= ~(1 << 13);
77     hced->control |= (speed << 13);
78 }
79
80 //Only for control Eps
81 void USBEndpoint::setNextToken(uint32_t token)
82 {
83     switch (token) {
84         case TD_SETUP:
85             dir = OUT;
86             setup = true;
87             break;
88         case TD_IN:
89             dir = IN;
90             setup = false;
91             break;
92         case TD_OUT:
93             dir = OUT;
94             setup = false;
95             break;
96     }
97 }
98
99 struct {
100     USB_TYPE type;
101     const char * str;
102 } static type_string[] = {
103 /*0*/   {USB_TYPE_OK, "USB_TYPE_OK"},
104         {USB_TYPE_CRC_ERROR, "USB_TYPE_CRC_ERROR"},
105         {USB_TYPE_BIT_STUFFING_ERROR, "USB_TYPE_BIT_STUFFING_ERROR"},
106         {USB_TYPE_DATA_TOGGLE_MISMATCH_ERROR, "USB_TYPE_DATA_TOGGLE_MISMATCH_ERROR"},
107         {USB_TYPE_STALL_ERROR, "USB_TYPE_STALL_ERROR"},
108 /*5*/   {USB_TYPE_DEVICE_NOT_RESPONDING_ERROR, "USB_TYPE_DEVICE_NOT_RESPONDING_ERROR"},
109         {USB_TYPE_PID_CHECK_FAILURE_ERROR, "USB_TYPE_PID_CHECK_FAILURE_ERROR"},
110         {USB_TYPE_UNEXPECTED_PID_ERROR, "USB_TYPE_UNEXPECTED_PID_ERROR"},
111         {USB_TYPE_DATA_OVERRUN_ERROR, "USB_TYPE_DATA_OVERRUN_ERROR"},
112         {USB_TYPE_DATA_UNDERRUN_ERROR, "USB_TYPE_DATA_UNDERRUN_ERROR"},
113 /*10*/  {USB_TYPE_ERROR, "USB_TYPE_ERROR"},
114         {USB_TYPE_ERROR, "USB_TYPE_ERROR"},
115         {USB_TYPE_BUFFER_OVERRUN_ERROR, "USB_TYPE_BUFFER_OVERRUN_ERROR"},
116         {USB_TYPE_BUFFER_UNDERRUN_ERROR, "USB_TYPE_BUFFER_UNDERRUN_ERROR"},
117         {USB_TYPE_DISCONNECTED, "USB_TYPE_DISCONNECTED"},
118 /*15*/  {USB_TYPE_FREE, "USB_TYPE_FREE"},
119         {USB_TYPE_IDLE, "USB_TYPE_IDLE"},
120         {USB_TYPE_PROCESSING, "USB_TYPE_PROCESSING"},
121         {USB_TYPE_ERROR, "USB_TYPE_ERROR"}
122 };
123
124 void USBEndpoint::setState(uint8_t st) {
125     if (st > 18)
126         return;
127     state = type_string[st].type;
128 }
129
130
131 const char * USBEndpoint::getStateString() {
132     return type_string[state].str;
133 }
134
135 void USBEndpoint::queueTransfer()
136 {
137     transfer_len = (uint32_t)td_current->bufEnd - (uint32_t)td_current->currBufPtr + 1;
138     transferred = transfer_len;
139     buf_start = (uint8_t *)td_current->currBufPtr;
140
141     //Now add this free TD at this end of the queue
142     state = USB_TYPE_PROCESSING;
143     td_current->nextTD = td_next;
144     hced->tailTD = td_next;
145 }
146
147 void USBEndpoint::unqueueTransfer(volatile HCTD * td)
148 {
149     td->control=0;
150     td->currBufPtr=0;
151     td->bufEnd=0;
152     td->nextTD=0;
153     hced->headTD = (HCTD *)((uint32_t)hced->tailTD | ((uint32_t)hced->headTD & 0x2)); //Carry bit
154     td_current = td_next;
155     td_next = td;
156 }
157
158 void USBEndpoint::queueEndpoint(USBEndpoint * ed)
159 {
160     nextEp = ed;
161     hced->nextED = (ed == NULL) ? 0 : ed->getHCED();
162 }