summaryrefslogtreecommitdiff
path: root/tool/mbed/mbed-sdk/libraries/USBHost/USBHost/USBEndpoint.cpp
blob: fc372232f380e71fe1a8d6dcfab69ed7dbf5594c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* mbed USBHost Library
 * Copyright (c) 2006-2013 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include "dbg.h"
#include "USBEndpoint.h"

void USBEndpoint::init(HCED * hced_, ENDPOINT_TYPE type_, ENDPOINT_DIRECTION dir_, uint32_t size, uint8_t ep_number, HCTD* td_list_[2])
{
    hced = hced_;
    type = type_;
    dir = dir_;
    setup = (type == CONTROL_ENDPOINT) ? true : false;

    //TDs have been allocated by the host
    memcpy((HCTD**)td_list, td_list_, sizeof(HCTD*)*2); //TODO: Maybe should add a param for td_list size... at least a define
    memset(td_list_[0], 0, sizeof(HCTD));
    memset(td_list_[1], 0, sizeof(HCTD));

    td_list[0]->ep = this;
    td_list[1]->ep = this;

    hced->control = 0;
    //Empty queue
    hced->tailTD = td_list[0];
    hced->headTD = td_list[0];
    hced->nextED = 0;

    address = (ep_number & 0x7F) | ((dir - 1) << 7);

    hced->control = ((ep_number & 0x7F) << 7)                         // Endpoint address
                    | (type != CONTROL_ENDPOINT ? ( dir << 11) : 0 )  // direction : Out = 1, 2 = In
                    | ((size & 0x3ff) << 16);                         // MaxPkt Size

    transfer_len = 0;
    transferred = 0;
    buf_start = 0;
    nextEp = NULL;

    td_current = td_list[0];
    td_next = td_list[1];

    intf_nb = 0;

    state = USB_TYPE_IDLE;
}

void USBEndpoint::setSize(uint32_t size)
{
    hced->control &= ~(0x3ff << 16);
    hced->control |= (size << 16);
}


void USBEndpoint::setDeviceAddress(uint8_t addr)
{
    hced->control &= ~(0x7f);
    hced->control |= (addr & 0x7F);
}

void USBEndpoint::setSpeed(uint8_t speed)
{
    hced->control &= ~(1 << 13);
    hced->control |= (speed << 13);
}

//Only for control Eps
void USBEndpoint::setNextToken(uint32_t token)
{
    switch (token) {
        case TD_SETUP:
            dir = OUT;
            setup = true;
            break;
        case TD_IN:
            dir = IN;
            setup = false;
            break;
        case TD_OUT:
            dir = OUT;
            setup = false;
            break;
    }
}

struct {
    USB_TYPE type;
    const char * str;
} static type_string[] = {
/*0*/   {USB_TYPE_OK, "USB_TYPE_OK"},
        {USB_TYPE_CRC_ERROR, "USB_TYPE_CRC_ERROR"},
        {USB_TYPE_BIT_STUFFING_ERROR, "USB_TYPE_BIT_STUFFING_ERROR"},
        {USB_TYPE_DATA_TOGGLE_MISMATCH_ERROR, "USB_TYPE_DATA_TOGGLE_MISMATCH_ERROR"},
        {USB_TYPE_STALL_ERROR, "USB_TYPE_STALL_ERROR"},
/*5*/   {USB_TYPE_DEVICE_NOT_RESPONDING_ERROR, "USB_TYPE_DEVICE_NOT_RESPONDING_ERROR"},
        {USB_TYPE_PID_CHECK_FAILURE_ERROR, "USB_TYPE_PID_CHECK_FAILURE_ERROR"},
        {USB_TYPE_UNEXPECTED_PID_ERROR, "USB_TYPE_UNEXPECTED_PID_ERROR"},
        {USB_TYPE_DATA_OVERRUN_ERROR, "USB_TYPE_DATA_OVERRUN_ERROR"},
        {USB_TYPE_DATA_UNDERRUN_ERROR, "USB_TYPE_DATA_UNDERRUN_ERROR"},
/*10*/  {USB_TYPE_ERROR, "USB_TYPE_ERROR"},
        {USB_TYPE_ERROR, "USB_TYPE_ERROR"},
        {USB_TYPE_BUFFER_OVERRUN_ERROR, "USB_TYPE_BUFFER_OVERRUN_ERROR"},
        {USB_TYPE_BUFFER_UNDERRUN_ERROR, "USB_TYPE_BUFFER_UNDERRUN_ERROR"},
        {USB_TYPE_DISCONNECTED, "USB_TYPE_DISCONNECTED"},
/*15*/  {USB_TYPE_FREE, "USB_TYPE_FREE"},
        {USB_TYPE_IDLE, "USB_TYPE_IDLE"},
        {USB_TYPE_PROCESSING, "USB_TYPE_PROCESSING"},
        {USB_TYPE_ERROR, "USB_TYPE_ERROR"}
};

void USBEndpoint::setState(uint8_t st) {
    if (st > 18)
        return;
    state = type_string[st].type;
}


const char * USBEndpoint::getStateString() {
    return type_string[state].str;
}

void USBEndpoint::queueTransfer()
{
    transfer_len = (uint32_t)td_current->bufEnd - (uint32_t)td_current->currBufPtr + 1;
    transferred = transfer_len;
    buf_start = (uint8_t *)td_current->currBufPtr;

    //Now add this free TD at this end of the queue
    state = USB_TYPE_PROCESSING;
    td_current->nextTD = td_next;
    hced->tailTD = td_next;
}

void USBEndpoint::unqueueTransfer(volatile HCTD * td)
{
    td->control=0;
    td->currBufPtr=0;
    td->bufEnd=0;
    td->nextTD=0;
    hced->headTD = (HCTD *)((uint32_t)hced->tailTD | ((uint32_t)hced->headTD & 0x2)); //Carry bit
    td_current = td_next;
    td_next = td;
}

void USBEndpoint::queueEndpoint(USBEndpoint * ed)
{
    nextEp = ed;
    hced->nextED = (ed == NULL) ? 0 : ed->getHCED();
}