2018.07.26

Dependencies:   FATFileSystem3 mbed-rtos

Fork of USBHost by mbed official

Committer:
samux
Date:
Tue Mar 12 17:23:37 2013 +0000
Revision:
4:b320d68e98e7
Parent:
0:a554658735bf
Child:
8:93da8ea2708b
bug fixes - support composite device

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:a554658735bf 1 /* Copyright (c) 2010-2012 mbed.org, MIT License
mbed_official 0:a554658735bf 2 *
mbed_official 0:a554658735bf 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
mbed_official 0:a554658735bf 4 * and associated documentation files (the "Software"), to deal in the Software without
mbed_official 0:a554658735bf 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
mbed_official 0:a554658735bf 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
mbed_official 0:a554658735bf 7 * Software is furnished to do so, subject to the following conditions:
mbed_official 0:a554658735bf 8 *
mbed_official 0:a554658735bf 9 * The above copyright notice and this permission notice shall be included in all copies or
mbed_official 0:a554658735bf 10 * substantial portions of the Software.
mbed_official 0:a554658735bf 11 *
mbed_official 0:a554658735bf 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
mbed_official 0:a554658735bf 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
mbed_official 0:a554658735bf 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mbed_official 0:a554658735bf 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mbed_official 0:a554658735bf 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mbed_official 0:a554658735bf 17 */
mbed_official 0:a554658735bf 18
mbed_official 0:a554658735bf 19
mbed_official 0:a554658735bf 20 #include "dbg.h"
mbed_official 0:a554658735bf 21 #include "USBEndpoint.h"
mbed_official 0:a554658735bf 22
mbed_official 0:a554658735bf 23 void USBEndpoint::init(HCED * hced_, ENDPOINT_TYPE type_, ENDPOINT_DIRECTION dir_, uint32_t size, uint8_t ep_number, HCTD* td_list_[2])
mbed_official 0:a554658735bf 24 {
mbed_official 0:a554658735bf 25 hced = hced_;
mbed_official 0:a554658735bf 26 type = type_;
mbed_official 0:a554658735bf 27 dir = dir_;
mbed_official 0:a554658735bf 28 setup = (type == CONTROL_ENDPOINT) ? true : false;
mbed_official 0:a554658735bf 29
mbed_official 0:a554658735bf 30 //TDs have been allocated by the host
mbed_official 0:a554658735bf 31 memcpy((HCTD**)td_list, td_list_, sizeof(HCTD*)*2); //TODO: Maybe should add a param for td_list size... at least a define
mbed_official 0:a554658735bf 32 memcpy(td_list_[0], 0, sizeof(HCTD));
mbed_official 0:a554658735bf 33 memcpy(td_list_[1], 0, sizeof(HCTD));
mbed_official 0:a554658735bf 34
mbed_official 0:a554658735bf 35 td_list[0]->ep = this;
mbed_official 0:a554658735bf 36 td_list[1]->ep = this;
mbed_official 0:a554658735bf 37
mbed_official 0:a554658735bf 38 hced->control = 0;
mbed_official 0:a554658735bf 39 //Empty queue
mbed_official 0:a554658735bf 40 hced->tailTD = td_list[0];
mbed_official 0:a554658735bf 41 hced->headTD = td_list[0];
mbed_official 0:a554658735bf 42 hced->nextED = 0;
mbed_official 0:a554658735bf 43
mbed_official 0:a554658735bf 44 address = (ep_number & 0x7F) | ((dir - 1) << 7);
mbed_official 0:a554658735bf 45
mbed_official 0:a554658735bf 46 hced->control = ((ep_number & 0x7F) << 7) // Endpoint address
mbed_official 0:a554658735bf 47 | (type != CONTROL_ENDPOINT ? ( dir << 11) : 0 ) // direction : Out = 1, 2 = In
mbed_official 0:a554658735bf 48 | ((size & 0x3ff) << 16); // MaxPkt Size
mbed_official 0:a554658735bf 49
mbed_official 0:a554658735bf 50 transfer_len = 0;
mbed_official 0:a554658735bf 51 transferred = 0;
mbed_official 0:a554658735bf 52 buf_start = 0;
mbed_official 0:a554658735bf 53 nextEp = NULL;
mbed_official 0:a554658735bf 54
mbed_official 0:a554658735bf 55 td_current = td_list[0];
mbed_official 0:a554658735bf 56 td_next = td_list[1];
samux 4:b320d68e98e7 57
samux 4:b320d68e98e7 58 intf_nb = 0;
mbed_official 0:a554658735bf 59
mbed_official 0:a554658735bf 60 state = USB_TYPE_IDLE;
mbed_official 0:a554658735bf 61 }
mbed_official 0:a554658735bf 62
mbed_official 0:a554658735bf 63 void USBEndpoint::setSize(uint32_t size)
mbed_official 0:a554658735bf 64 {
mbed_official 0:a554658735bf 65 hced->control &= ~(0x3ff << 16);
mbed_official 0:a554658735bf 66 hced->control |= (size << 16);
mbed_official 0:a554658735bf 67 }
mbed_official 0:a554658735bf 68
mbed_official 0:a554658735bf 69
mbed_official 0:a554658735bf 70 void USBEndpoint::setDeviceAddress(uint8_t addr)
mbed_official 0:a554658735bf 71 {
mbed_official 0:a554658735bf 72 hced->control &= ~(0x7f);
mbed_official 0:a554658735bf 73 hced->control |= (addr & 0x7F);
mbed_official 0:a554658735bf 74 }
mbed_official 0:a554658735bf 75
mbed_official 0:a554658735bf 76 void USBEndpoint::setSpeed(uint8_t speed)
mbed_official 0:a554658735bf 77 {
mbed_official 0:a554658735bf 78 hced->control &= ~(1 << 13);
mbed_official 0:a554658735bf 79 hced->control |= (speed << 13);
mbed_official 0:a554658735bf 80 }
mbed_official 0:a554658735bf 81
mbed_official 0:a554658735bf 82 //Only for control Eps
mbed_official 0:a554658735bf 83 void USBEndpoint::setNextToken(uint32_t token)
mbed_official 0:a554658735bf 84 {
mbed_official 0:a554658735bf 85 switch (token) {
mbed_official 0:a554658735bf 86 case TD_SETUP:
mbed_official 0:a554658735bf 87 dir = OUT;
mbed_official 0:a554658735bf 88 setup = true;
mbed_official 0:a554658735bf 89 break;
mbed_official 0:a554658735bf 90 case TD_IN:
mbed_official 0:a554658735bf 91 dir = IN;
mbed_official 0:a554658735bf 92 setup = false;
mbed_official 0:a554658735bf 93 break;
mbed_official 0:a554658735bf 94 case TD_OUT:
mbed_official 0:a554658735bf 95 dir = OUT;
mbed_official 0:a554658735bf 96 setup = false;
mbed_official 0:a554658735bf 97 break;
mbed_official 0:a554658735bf 98 }
mbed_official 0:a554658735bf 99 }
mbed_official 0:a554658735bf 100
mbed_official 0:a554658735bf 101 struct {
mbed_official 0:a554658735bf 102 USB_TYPE type;
mbed_official 0:a554658735bf 103 const char * str;
mbed_official 0:a554658735bf 104 } static type_string[] = {
mbed_official 0:a554658735bf 105 /*0*/ {USB_TYPE_OK, "USB_TYPE_OK"},
mbed_official 0:a554658735bf 106 {USB_TYPE_CRC_ERROR, "USB_TYPE_CRC_ERROR"},
mbed_official 0:a554658735bf 107 {USB_TYPE_BIT_STUFFING_ERROR, "USB_TYPE_BIT_STUFFING_ERROR"},
mbed_official 0:a554658735bf 108 {USB_TYPE_DATA_TOGGLE_MISMATCH_ERROR, "USB_TYPE_DATA_TOGGLE_MISMATCH_ERROR"},
mbed_official 0:a554658735bf 109 {USB_TYPE_STALL_ERROR, "USB_TYPE_STALL_ERROR"},
mbed_official 0:a554658735bf 110 /*5*/ {USB_TYPE_DEVICE_NOT_RESPONDING_ERROR, "USB_TYPE_DEVICE_NOT_RESPONDING_ERROR"},
mbed_official 0:a554658735bf 111 {USB_TYPE_PID_CHECK_FAILURE_ERROR, "USB_TYPE_PID_CHECK_FAILURE_ERROR"},
mbed_official 0:a554658735bf 112 {USB_TYPE_UNEXPECTED_PID_ERROR, "USB_TYPE_UNEXPECTED_PID_ERROR"},
mbed_official 0:a554658735bf 113 {USB_TYPE_DATA_OVERRUN_ERROR, "USB_TYPE_DATA_OVERRUN_ERROR"},
mbed_official 0:a554658735bf 114 {USB_TYPE_DATA_UNDERRUN_ERROR, "USB_TYPE_DATA_UNDERRUN_ERROR"},
mbed_official 0:a554658735bf 115 /*10*/ {USB_TYPE_ERROR, "USB_TYPE_ERROR"},
mbed_official 0:a554658735bf 116 {USB_TYPE_ERROR, "USB_TYPE_ERROR"},
mbed_official 0:a554658735bf 117 {USB_TYPE_BUFFER_OVERRUN_ERROR, "USB_TYPE_BUFFER_OVERRUN_ERROR"},
mbed_official 0:a554658735bf 118 {USB_TYPE_BUFFER_UNDERRUN_ERROR, "USB_TYPE_BUFFER_UNDERRUN_ERROR"},
mbed_official 0:a554658735bf 119 {USB_TYPE_DISCONNECTED, "USB_TYPE_DISCONNECTED"},
mbed_official 0:a554658735bf 120 /*15*/ {USB_TYPE_FREE, "USB_TYPE_FREE"},
mbed_official 0:a554658735bf 121 {USB_TYPE_IDLE, "USB_TYPE_IDLE"},
mbed_official 0:a554658735bf 122 {USB_TYPE_PROCESSING, "USB_TYPE_PROCESSING"},
mbed_official 0:a554658735bf 123 {USB_TYPE_ERROR, "USB_TYPE_ERROR"}
mbed_official 0:a554658735bf 124 };
mbed_official 0:a554658735bf 125
mbed_official 0:a554658735bf 126 void USBEndpoint::setState(uint8_t st) {
mbed_official 0:a554658735bf 127 if (st > 18)
mbed_official 0:a554658735bf 128 return;
mbed_official 0:a554658735bf 129 state = type_string[st].type;
mbed_official 0:a554658735bf 130 }
mbed_official 0:a554658735bf 131
mbed_official 0:a554658735bf 132
mbed_official 0:a554658735bf 133 const char * USBEndpoint::getStateString() {
mbed_official 0:a554658735bf 134 return type_string[state].str;
mbed_official 0:a554658735bf 135 }
mbed_official 0:a554658735bf 136
mbed_official 0:a554658735bf 137 void USBEndpoint::queueTransfer()
mbed_official 0:a554658735bf 138 {
mbed_official 0:a554658735bf 139 transfer_len = (uint32_t)td_current->bufEnd - (uint32_t)td_current->currBufPtr + 1;
mbed_official 0:a554658735bf 140 transferred = transfer_len;
mbed_official 0:a554658735bf 141 buf_start = (uint8_t *)td_current->currBufPtr;
mbed_official 0:a554658735bf 142
mbed_official 0:a554658735bf 143 //Now add this free TD at this end of the queue
mbed_official 0:a554658735bf 144 state = USB_TYPE_PROCESSING;
samux 4:b320d68e98e7 145 td_current->nextTD = td_next;
mbed_official 0:a554658735bf 146 hced->tailTD = td_next;
mbed_official 0:a554658735bf 147 }
mbed_official 0:a554658735bf 148
mbed_official 0:a554658735bf 149 void USBEndpoint::unqueueTransfer(volatile HCTD * td)
mbed_official 0:a554658735bf 150 {
mbed_official 0:a554658735bf 151 td->control=0;
mbed_official 0:a554658735bf 152 td->currBufPtr=0;
mbed_official 0:a554658735bf 153 td->bufEnd=0;
mbed_official 0:a554658735bf 154 td->nextTD=0;
mbed_official 0:a554658735bf 155 hced->headTD = (HCTD *)((uint32_t)hced->tailTD | ((uint32_t)hced->headTD & 0x2)); //Carry bit
mbed_official 0:a554658735bf 156 td_current = td_next;
mbed_official 0:a554658735bf 157 td_next = td;
mbed_official 0:a554658735bf 158 }
mbed_official 0:a554658735bf 159
mbed_official 0:a554658735bf 160 void USBEndpoint::queueEndpoint(USBEndpoint * ed)
mbed_official 0:a554658735bf 161 {
mbed_official 0:a554658735bf 162 nextEp = ed;
samux 4:b320d68e98e7 163 hced->nextED = (ed == NULL) ? 0 : ed->getHCED();
mbed_official 0:a554658735bf 164 }