2018.07.26

Dependencies:   FATFileSystem3 mbed-rtos

Fork of USBHost by mbed official

Committer:
sayzyas
Date:
Thu Jul 26 00:20:32 2018 +0000
Revision:
43:1675750cca08
2018.07.26

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sayzyas 43:1675750cca08 1 /* mbed USBHost Library
sayzyas 43:1675750cca08 2 * Copyright (c) 2006-2013 ARM Limited
sayzyas 43:1675750cca08 3 *
sayzyas 43:1675750cca08 4 * Licensed under the Apache License, Version 2.0 (the "License");
sayzyas 43:1675750cca08 5 * you may not use this file except in compliance with the License.
sayzyas 43:1675750cca08 6 * You may obtain a copy of the License at
sayzyas 43:1675750cca08 7 *
sayzyas 43:1675750cca08 8 * http://www.apache.org/licenses/LICENSE-2.0
sayzyas 43:1675750cca08 9 *
sayzyas 43:1675750cca08 10 * Unless required by applicable law or agreed to in writing, software
sayzyas 43:1675750cca08 11 * distributed under the License is distributed on an "AS IS" BASIS,
sayzyas 43:1675750cca08 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sayzyas 43:1675750cca08 13 * See the License for the specific language governing permissions and
sayzyas 43:1675750cca08 14 * limitations under the License.
sayzyas 43:1675750cca08 15 */
sayzyas 43:1675750cca08 16
sayzyas 43:1675750cca08 17
sayzyas 43:1675750cca08 18 #include "dbg.h"
sayzyas 43:1675750cca08 19 #include "USBEndpoint.h"
sayzyas 43:1675750cca08 20
sayzyas 43:1675750cca08 21 void USBEndpoint::init(HCED * hced_, ENDPOINT_TYPE type_, ENDPOINT_DIRECTION dir_, uint32_t size, uint8_t ep_number, HCTD* td_list_[2])
sayzyas 43:1675750cca08 22 {
sayzyas 43:1675750cca08 23 hced = hced_;
sayzyas 43:1675750cca08 24 type = type_;
sayzyas 43:1675750cca08 25 dir = dir_;
sayzyas 43:1675750cca08 26 setup = (type == CONTROL_ENDPOINT) ? true : false;
sayzyas 43:1675750cca08 27
sayzyas 43:1675750cca08 28 //TDs have been allocated by the host
sayzyas 43:1675750cca08 29 memcpy((HCTD**)td_list, td_list_, sizeof(HCTD*)*2); //TODO: Maybe should add a param for td_list size... at least a define
sayzyas 43:1675750cca08 30 memset(td_list_[0], 0, sizeof(HCTD));
sayzyas 43:1675750cca08 31 memset(td_list_[1], 0, sizeof(HCTD));
sayzyas 43:1675750cca08 32
sayzyas 43:1675750cca08 33 td_list[0]->ep = this;
sayzyas 43:1675750cca08 34 td_list[1]->ep = this;
sayzyas 43:1675750cca08 35
sayzyas 43:1675750cca08 36 hced->control = 0;
sayzyas 43:1675750cca08 37 //Empty queue
sayzyas 43:1675750cca08 38 hced->tailTD = td_list[0];
sayzyas 43:1675750cca08 39 hced->headTD = td_list[0];
sayzyas 43:1675750cca08 40 hced->nextED = 0;
sayzyas 43:1675750cca08 41
sayzyas 43:1675750cca08 42 address = (ep_number & 0x7F) | ((dir - 1) << 7);
sayzyas 43:1675750cca08 43
sayzyas 43:1675750cca08 44 hced->control = ((ep_number & 0x7F) << 7) // Endpoint address
sayzyas 43:1675750cca08 45 | (type != CONTROL_ENDPOINT ? ( dir << 11) : 0 ) // direction : Out = 1, 2 = In
sayzyas 43:1675750cca08 46 | ((size & 0x3ff) << 16); // MaxPkt Size
sayzyas 43:1675750cca08 47
sayzyas 43:1675750cca08 48 transfer_len = 0;
sayzyas 43:1675750cca08 49 transferred = 0;
sayzyas 43:1675750cca08 50 buf_start = 0;
sayzyas 43:1675750cca08 51 nextEp = NULL;
sayzyas 43:1675750cca08 52
sayzyas 43:1675750cca08 53 td_current = td_list[0];
sayzyas 43:1675750cca08 54 td_next = td_list[1];
sayzyas 43:1675750cca08 55
sayzyas 43:1675750cca08 56 intf_nb = 0;
sayzyas 43:1675750cca08 57
sayzyas 43:1675750cca08 58 state = USB_TYPE_IDLE;
sayzyas 43:1675750cca08 59 }
sayzyas 43:1675750cca08 60
sayzyas 43:1675750cca08 61 void USBEndpoint::setSize(uint32_t size)
sayzyas 43:1675750cca08 62 {
sayzyas 43:1675750cca08 63 hced->control &= ~(0x3ff << 16);
sayzyas 43:1675750cca08 64 hced->control |= (size << 16);
sayzyas 43:1675750cca08 65 }
sayzyas 43:1675750cca08 66
sayzyas 43:1675750cca08 67
sayzyas 43:1675750cca08 68 void USBEndpoint::setDeviceAddress(uint8_t addr)
sayzyas 43:1675750cca08 69 {
sayzyas 43:1675750cca08 70 hced->control &= ~(0x7f);
sayzyas 43:1675750cca08 71 hced->control |= (addr & 0x7F);
sayzyas 43:1675750cca08 72 }
sayzyas 43:1675750cca08 73
sayzyas 43:1675750cca08 74 void USBEndpoint::setSpeed(uint8_t speed)
sayzyas 43:1675750cca08 75 {
sayzyas 43:1675750cca08 76 hced->control &= ~(1 << 13);
sayzyas 43:1675750cca08 77 hced->control |= (speed << 13);
sayzyas 43:1675750cca08 78 }
sayzyas 43:1675750cca08 79
sayzyas 43:1675750cca08 80 //Only for control Eps
sayzyas 43:1675750cca08 81 void USBEndpoint::setNextToken(uint32_t token)
sayzyas 43:1675750cca08 82 {
sayzyas 43:1675750cca08 83 switch (token) {
sayzyas 43:1675750cca08 84 case TD_SETUP:
sayzyas 43:1675750cca08 85 dir = OUT;
sayzyas 43:1675750cca08 86 setup = true;
sayzyas 43:1675750cca08 87 break;
sayzyas 43:1675750cca08 88 case TD_IN:
sayzyas 43:1675750cca08 89 dir = IN;
sayzyas 43:1675750cca08 90 setup = false;
sayzyas 43:1675750cca08 91 break;
sayzyas 43:1675750cca08 92 case TD_OUT:
sayzyas 43:1675750cca08 93 dir = OUT;
sayzyas 43:1675750cca08 94 setup = false;
sayzyas 43:1675750cca08 95 break;
sayzyas 43:1675750cca08 96 }
sayzyas 43:1675750cca08 97 }
sayzyas 43:1675750cca08 98
sayzyas 43:1675750cca08 99 struct {
sayzyas 43:1675750cca08 100 USB_TYPE type;
sayzyas 43:1675750cca08 101 const char * str;
sayzyas 43:1675750cca08 102 } static type_string[] = {
sayzyas 43:1675750cca08 103 /*0*/ {USB_TYPE_OK, "USB_TYPE_OK"},
sayzyas 43:1675750cca08 104 {USB_TYPE_CRC_ERROR, "USB_TYPE_CRC_ERROR"},
sayzyas 43:1675750cca08 105 {USB_TYPE_BIT_STUFFING_ERROR, "USB_TYPE_BIT_STUFFING_ERROR"},
sayzyas 43:1675750cca08 106 {USB_TYPE_DATA_TOGGLE_MISMATCH_ERROR, "USB_TYPE_DATA_TOGGLE_MISMATCH_ERROR"},
sayzyas 43:1675750cca08 107 {USB_TYPE_STALL_ERROR, "USB_TYPE_STALL_ERROR"},
sayzyas 43:1675750cca08 108 /*5*/ {USB_TYPE_DEVICE_NOT_RESPONDING_ERROR, "USB_TYPE_DEVICE_NOT_RESPONDING_ERROR"},
sayzyas 43:1675750cca08 109 {USB_TYPE_PID_CHECK_FAILURE_ERROR, "USB_TYPE_PID_CHECK_FAILURE_ERROR"},
sayzyas 43:1675750cca08 110 {USB_TYPE_UNEXPECTED_PID_ERROR, "USB_TYPE_UNEXPECTED_PID_ERROR"},
sayzyas 43:1675750cca08 111 {USB_TYPE_DATA_OVERRUN_ERROR, "USB_TYPE_DATA_OVERRUN_ERROR"},
sayzyas 43:1675750cca08 112 {USB_TYPE_DATA_UNDERRUN_ERROR, "USB_TYPE_DATA_UNDERRUN_ERROR"},
sayzyas 43:1675750cca08 113 /*10*/ {USB_TYPE_ERROR, "USB_TYPE_ERROR"},
sayzyas 43:1675750cca08 114 {USB_TYPE_ERROR, "USB_TYPE_ERROR"},
sayzyas 43:1675750cca08 115 {USB_TYPE_BUFFER_OVERRUN_ERROR, "USB_TYPE_BUFFER_OVERRUN_ERROR"},
sayzyas 43:1675750cca08 116 {USB_TYPE_BUFFER_UNDERRUN_ERROR, "USB_TYPE_BUFFER_UNDERRUN_ERROR"},
sayzyas 43:1675750cca08 117 {USB_TYPE_DISCONNECTED, "USB_TYPE_DISCONNECTED"},
sayzyas 43:1675750cca08 118 /*15*/ {USB_TYPE_FREE, "USB_TYPE_FREE"},
sayzyas 43:1675750cca08 119 {USB_TYPE_IDLE, "USB_TYPE_IDLE"},
sayzyas 43:1675750cca08 120 {USB_TYPE_PROCESSING, "USB_TYPE_PROCESSING"},
sayzyas 43:1675750cca08 121 {USB_TYPE_ERROR, "USB_TYPE_ERROR"}
sayzyas 43:1675750cca08 122 };
sayzyas 43:1675750cca08 123
sayzyas 43:1675750cca08 124 void USBEndpoint::setState(uint8_t st) {
sayzyas 43:1675750cca08 125 if (st > 18)
sayzyas 43:1675750cca08 126 return;
sayzyas 43:1675750cca08 127 state = type_string[st].type;
sayzyas 43:1675750cca08 128 }
sayzyas 43:1675750cca08 129
sayzyas 43:1675750cca08 130
sayzyas 43:1675750cca08 131 const char * USBEndpoint::getStateString() {
sayzyas 43:1675750cca08 132 return type_string[state].str;
sayzyas 43:1675750cca08 133 }
sayzyas 43:1675750cca08 134
sayzyas 43:1675750cca08 135 void USBEndpoint::queueTransfer()
sayzyas 43:1675750cca08 136 {
sayzyas 43:1675750cca08 137 transfer_len = (uint32_t)td_current->bufEnd - (uint32_t)td_current->currBufPtr + 1;
sayzyas 43:1675750cca08 138 transferred = transfer_len;
sayzyas 43:1675750cca08 139 buf_start = (uint8_t *)td_current->currBufPtr;
sayzyas 43:1675750cca08 140
sayzyas 43:1675750cca08 141 //Now add this free TD at this end of the queue
sayzyas 43:1675750cca08 142 state = USB_TYPE_PROCESSING;
sayzyas 43:1675750cca08 143 td_current->nextTD = td_next;
sayzyas 43:1675750cca08 144 hced->tailTD = td_next;
sayzyas 43:1675750cca08 145 }
sayzyas 43:1675750cca08 146
sayzyas 43:1675750cca08 147 void USBEndpoint::unqueueTransfer(volatile HCTD * td)
sayzyas 43:1675750cca08 148 {
sayzyas 43:1675750cca08 149 td->control=0;
sayzyas 43:1675750cca08 150 td->currBufPtr=0;
sayzyas 43:1675750cca08 151 td->bufEnd=0;
sayzyas 43:1675750cca08 152 td->nextTD=0;
sayzyas 43:1675750cca08 153 hced->headTD = (HCTD *)((uint32_t)hced->tailTD | ((uint32_t)hced->headTD & 0x2)); //Carry bit
sayzyas 43:1675750cca08 154 td_current = td_next;
sayzyas 43:1675750cca08 155 td_next = td;
sayzyas 43:1675750cca08 156 }
sayzyas 43:1675750cca08 157
sayzyas 43:1675750cca08 158 void USBEndpoint::queueEndpoint(USBEndpoint * ed)
sayzyas 43:1675750cca08 159 {
sayzyas 43:1675750cca08 160 nextEp = ed;
sayzyas 43:1675750cca08 161 hced->nextED = (ed == NULL) ? 0 : ed->getHCED();
sayzyas 43:1675750cca08 162 }