1 /* mbed USBHost Library
2 * Copyright (c) 2006-2013 ARM Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "USBEndpoint.h"
21 void USBEndpoint::init(HCED
* hced_
, ENDPOINT_TYPE type_
, ENDPOINT_DIRECTION dir_
, uint32_t size
, uint8_t ep_number
, HCTD
* td_list_
[2])
26 setup
= (type
== CONTROL_ENDPOINT
) ? true : false;
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
));
33 td_list
[0]->ep
= this;
34 td_list
[1]->ep
= this;
38 hced
->tailTD
= td_list
[0];
39 hced
->headTD
= td_list
[0];
42 address
= (ep_number
& 0x7F) | ((dir
- 1) << 7);
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
53 td_current
= td_list
[0];
58 state
= USB_TYPE_IDLE
;
61 void USBEndpoint::setSize(uint32_t size
)
63 hced
->control
&= ~(0x3ff << 16);
64 hced
->control
|= (size
<< 16);
68 void USBEndpoint::setDeviceAddress(uint8_t addr
)
70 hced
->control
&= ~(0x7f);
71 hced
->control
|= (addr
& 0x7F);
74 void USBEndpoint::setSpeed(uint8_t speed
)
76 hced
->control
&= ~(1 << 13);
77 hced
->control
|= (speed
<< 13);
80 //Only for control Eps
81 void USBEndpoint::setNextToken(uint32_t token
)
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"}
124 void USBEndpoint::setState(uint8_t st
) {
127 state
= type_string
[st
].type
;
131 const char * USBEndpoint::getStateString() {
132 return type_string
[state
].str
;
135 void USBEndpoint::queueTransfer()
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
;
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
;
147 void USBEndpoint::unqueueTransfer(volatile HCTD
* td
)
153 hced
->headTD
= (HCTD
*)((uint32_t)hced
->tailTD
| ((uint32_t)hced
->headTD
& 0x2)); //Carry bit
154 td_current
= td_next
;
158 void USBEndpoint::queueEndpoint(USBEndpoint
* ed
)
161 hced
->nextED
= (ed
== NULL
) ? 0 : ed
->getHCED();