2018.07.26

Dependencies:   FATFileSystem2 mbed-rtos

Fork of USBHost by mbed official

Committer:
sayzyas
Date:
Thu Jul 26 00:29:30 2018 +0000
Revision:
44:e437b1c7c61e
Parent:
43:78f328f311dc
2018.07.26

Who changed what in which revision?

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