2018.07.26

Dependencies:   FATFileSystem3 mbed-rtos

Fork of USBHost by mbed official

Committer:
mbed_official
Date:
Wed Mar 06 16:27:14 2013 +0000
Revision:
0:a554658735bf
Child:
4:b320d68e98e7
first commit

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];
mbed_official 0:a554658735bf 57
mbed_official 0:a554658735bf 58 state = USB_TYPE_IDLE;
mbed_official 0:a554658735bf 59 }
mbed_official 0:a554658735bf 60
mbed_official 0:a554658735bf 61 void USBEndpoint::setSize(uint32_t size)
mbed_official 0:a554658735bf 62 {
mbed_official 0:a554658735bf 63 hced->control &= ~(0x3ff << 16);
mbed_official 0:a554658735bf 64 hced->control |= (size << 16);
mbed_official 0:a554658735bf 65 }
mbed_official 0:a554658735bf 66
mbed_official 0:a554658735bf 67
mbed_official 0:a554658735bf 68 void USBEndpoint::setDeviceAddress(uint8_t addr)
mbed_official 0:a554658735bf 69 {
mbed_official 0:a554658735bf 70 hced->control &= ~(0x7f);
mbed_official 0:a554658735bf 71 hced->control |= (addr & 0x7F);
mbed_official 0:a554658735bf 72 }
mbed_official 0:a554658735bf 73
mbed_official 0:a554658735bf 74 void USBEndpoint::setSpeed(uint8_t speed)
mbed_official 0:a554658735bf 75 {
mbed_official 0:a554658735bf 76 hced->control &= ~(1 << 13);
mbed_official 0:a554658735bf 77 hced->control |= (speed << 13);
mbed_official 0:a554658735bf 78 }
mbed_official 0:a554658735bf 79
mbed_official 0:a554658735bf 80 //Only for control Eps
mbed_official 0:a554658735bf 81 void USBEndpoint::setNextToken(uint32_t token)
mbed_official 0:a554658735bf 82 {
mbed_official 0:a554658735bf 83 switch (token) {
mbed_official 0:a554658735bf 84 case TD_SETUP:
mbed_official 0:a554658735bf 85 dir = OUT;
mbed_official 0:a554658735bf 86 setup = true;
mbed_official 0:a554658735bf 87 break;
mbed_official 0:a554658735bf 88 case TD_IN:
mbed_official 0:a554658735bf 89 dir = IN;
mbed_official 0:a554658735bf 90 setup = false;
mbed_official 0:a554658735bf 91 break;
mbed_official 0:a554658735bf 92 case TD_OUT:
mbed_official 0:a554658735bf 93 dir = OUT;
mbed_official 0:a554658735bf 94 setup = false;
mbed_official 0:a554658735bf 95 break;
mbed_official 0:a554658735bf 96 }
mbed_official 0:a554658735bf 97 }
mbed_official 0:a554658735bf 98
mbed_official 0:a554658735bf 99 struct {
mbed_official 0:a554658735bf 100 USB_TYPE type;
mbed_official 0:a554658735bf 101 const char * str;
mbed_official 0:a554658735bf 102 } static type_string[] = {
mbed_official 0:a554658735bf 103 /*0*/ {USB_TYPE_OK, "USB_TYPE_OK"},
mbed_official 0:a554658735bf 104 {USB_TYPE_CRC_ERROR, "USB_TYPE_CRC_ERROR"},
mbed_official 0:a554658735bf 105 {USB_TYPE_BIT_STUFFING_ERROR, "USB_TYPE_BIT_STUFFING_ERROR"},
mbed_official 0:a554658735bf 106 {USB_TYPE_DATA_TOGGLE_MISMATCH_ERROR, "USB_TYPE_DATA_TOGGLE_MISMATCH_ERROR"},
mbed_official 0:a554658735bf 107 {USB_TYPE_STALL_ERROR, "USB_TYPE_STALL_ERROR"},
mbed_official 0:a554658735bf 108 /*5*/ {USB_TYPE_DEVICE_NOT_RESPONDING_ERROR, "USB_TYPE_DEVICE_NOT_RESPONDING_ERROR"},
mbed_official 0:a554658735bf 109 {USB_TYPE_PID_CHECK_FAILURE_ERROR, "USB_TYPE_PID_CHECK_FAILURE_ERROR"},
mbed_official 0:a554658735bf 110 {USB_TYPE_UNEXPECTED_PID_ERROR, "USB_TYPE_UNEXPECTED_PID_ERROR"},
mbed_official 0:a554658735bf 111 {USB_TYPE_DATA_OVERRUN_ERROR, "USB_TYPE_DATA_OVERRUN_ERROR"},
mbed_official 0:a554658735bf 112 {USB_TYPE_DATA_UNDERRUN_ERROR, "USB_TYPE_DATA_UNDERRUN_ERROR"},
mbed_official 0:a554658735bf 113 /*10*/ {USB_TYPE_ERROR, "USB_TYPE_ERROR"},
mbed_official 0:a554658735bf 114 {USB_TYPE_ERROR, "USB_TYPE_ERROR"},
mbed_official 0:a554658735bf 115 {USB_TYPE_BUFFER_OVERRUN_ERROR, "USB_TYPE_BUFFER_OVERRUN_ERROR"},
mbed_official 0:a554658735bf 116 {USB_TYPE_BUFFER_UNDERRUN_ERROR, "USB_TYPE_BUFFER_UNDERRUN_ERROR"},
mbed_official 0:a554658735bf 117 {USB_TYPE_DISCONNECTED, "USB_TYPE_DISCONNECTED"},
mbed_official 0:a554658735bf 118 /*15*/ {USB_TYPE_FREE, "USB_TYPE_FREE"},
mbed_official 0:a554658735bf 119 {USB_TYPE_IDLE, "USB_TYPE_IDLE"},
mbed_official 0:a554658735bf 120 {USB_TYPE_PROCESSING, "USB_TYPE_PROCESSING"},
mbed_official 0:a554658735bf 121 {USB_TYPE_ERROR, "USB_TYPE_ERROR"}
mbed_official 0:a554658735bf 122 };
mbed_official 0:a554658735bf 123
mbed_official 0:a554658735bf 124 void USBEndpoint::setState(uint8_t st) {
mbed_official 0:a554658735bf 125 if (st > 18)
mbed_official 0:a554658735bf 126 return;
mbed_official 0:a554658735bf 127 state = type_string[st].type;
mbed_official 0:a554658735bf 128 }
mbed_official 0:a554658735bf 129
mbed_official 0:a554658735bf 130
mbed_official 0:a554658735bf 131 const char * USBEndpoint::getStateString() {
mbed_official 0:a554658735bf 132 return type_string[state].str;
mbed_official 0:a554658735bf 133 }
mbed_official 0:a554658735bf 134
mbed_official 0:a554658735bf 135 void USBEndpoint::queueTransfer()
mbed_official 0:a554658735bf 136 {
mbed_official 0:a554658735bf 137 transfer_len = (uint32_t)td_current->bufEnd - (uint32_t)td_current->currBufPtr + 1;
mbed_official 0:a554658735bf 138 transferred = transfer_len;
mbed_official 0:a554658735bf 139 buf_start = (uint8_t *)td_current->currBufPtr;
mbed_official 0:a554658735bf 140
mbed_official 0:a554658735bf 141 //Now add this free TD at this end of the queue
mbed_official 0:a554658735bf 142 state = USB_TYPE_PROCESSING;
mbed_official 0:a554658735bf 143 td_current->nextTD = (uint32_t)td_next;
mbed_official 0:a554658735bf 144 hced->tailTD = td_next;
mbed_official 0:a554658735bf 145 }
mbed_official 0:a554658735bf 146
mbed_official 0:a554658735bf 147 void USBEndpoint::unqueueTransfer(volatile HCTD * td)
mbed_official 0:a554658735bf 148 {
mbed_official 0:a554658735bf 149 td->control=0;
mbed_official 0:a554658735bf 150 td->currBufPtr=0;
mbed_official 0:a554658735bf 151 td->bufEnd=0;
mbed_official 0:a554658735bf 152 td->nextTD=0;
mbed_official 0:a554658735bf 153 hced->headTD = (HCTD *)((uint32_t)hced->tailTD | ((uint32_t)hced->headTD & 0x2)); //Carry bit
mbed_official 0:a554658735bf 154 td_current = td_next;
mbed_official 0:a554658735bf 155 td_next = td;
mbed_official 0:a554658735bf 156 }
mbed_official 0:a554658735bf 157
mbed_official 0:a554658735bf 158 void USBEndpoint::queueEndpoint(USBEndpoint * ed)
mbed_official 0:a554658735bf 159 {
mbed_official 0:a554658735bf 160 nextEp = ed;
mbed_official 0:a554658735bf 161 hced->nextED = (ed == NULL) ? 0 : (uint32_t)ed->getHCED();
mbed_official 0:a554658735bf 162 }
mbed_official 0:a554658735bf 163