USBHost library. NOTE: This library is only officially supported on the LPC1768 platform. For more information, please see the handbook page.

Dependencies:   FATFileSystem mbed-rtos

Dependents:   BTstack WallbotWii SD to Flash Data Transfer USBHost-MSD_HelloWorld ... more

Legacy Warning

This is an mbed 2 library. To learn more about mbed OS 5, visit the docs.

Pull requests against this repository are no longer supported. Please raise against mbed OS 5 as documented above.

Committer:
Anna Bridge
Date:
Thu Aug 17 18:12:22 2017 +0100
Revision:
40:7c3b59bb364e
Parent:
37:f1e388e7b752
DISCO_L475VG_IOT01A: Add support of USBHost

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 8:93da8ea2708b 1 /* mbed USBHost Library
samux 8:93da8ea2708b 2 * Copyright (c) 2006-2013 ARM Limited
samux 8:93da8ea2708b 3 *
samux 8:93da8ea2708b 4 * Licensed under the Apache License, Version 2.0 (the "License");
samux 8:93da8ea2708b 5 * you may not use this file except in compliance with the License.
samux 8:93da8ea2708b 6 * You may obtain a copy of the License at
samux 8:93da8ea2708b 7 *
samux 8:93da8ea2708b 8 * http://www.apache.org/licenses/LICENSE-2.0
samux 8:93da8ea2708b 9 *
samux 8:93da8ea2708b 10 * Unless required by applicable law or agreed to in writing, software
samux 8:93da8ea2708b 11 * distributed under the License is distributed on an "AS IS" BASIS,
samux 8:93da8ea2708b 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
samux 8:93da8ea2708b 13 * See the License for the specific language governing permissions and
samux 8:93da8ea2708b 14 * limitations under the License.
samux 8:93da8ea2708b 15 */
mbed_official 0:a554658735bf 16
mbed_official 0:a554658735bf 17
mbed_official 0:a554658735bf 18 #include "dbg.h"
mbed_official 0:a554658735bf 19 #include "USBEndpoint.h"
Kojto 37:f1e388e7b752 20 #if !defined(USBHOST_OTHER)
mbed_official 0:a554658735bf 21 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 22 {
mbed_official 0:a554658735bf 23 hced = hced_;
mbed_official 0:a554658735bf 24 type = type_;
mbed_official 0:a554658735bf 25 dir = dir_;
mbed_official 0:a554658735bf 26 setup = (type == CONTROL_ENDPOINT) ? true : false;
mbed_official 0:a554658735bf 27
mbed_official 0:a554658735bf 28 //TDs have been allocated by the host
mbed_official 0:a554658735bf 29 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 13:b58a2204422f 30 memset(td_list_[0], 0, sizeof(HCTD));
mbed_official 13:b58a2204422f 31 memset(td_list_[1], 0, sizeof(HCTD));
mbed_official 24:868cbfe611a7 32
mbed_official 0:a554658735bf 33 td_list[0]->ep = this;
mbed_official 0:a554658735bf 34 td_list[1]->ep = this;
mbed_official 0:a554658735bf 35
mbed_official 0:a554658735bf 36 hced->control = 0;
mbed_official 0:a554658735bf 37 //Empty queue
mbed_official 0:a554658735bf 38 hced->tailTD = td_list[0];
mbed_official 0:a554658735bf 39 hced->headTD = td_list[0];
mbed_official 0:a554658735bf 40 hced->nextED = 0;
mbed_official 0:a554658735bf 41
mbed_official 0:a554658735bf 42 address = (ep_number & 0x7F) | ((dir - 1) << 7);
mbed_official 0:a554658735bf 43
mbed_official 0:a554658735bf 44 hced->control = ((ep_number & 0x7F) << 7) // Endpoint address
mbed_official 0:a554658735bf 45 | (type != CONTROL_ENDPOINT ? ( dir << 11) : 0 ) // direction : Out = 1, 2 = In
mbed_official 0:a554658735bf 46 | ((size & 0x3ff) << 16); // MaxPkt Size
mbed_official 0:a554658735bf 47
mbed_official 0:a554658735bf 48 transfer_len = 0;
mbed_official 0:a554658735bf 49 transferred = 0;
mbed_official 0:a554658735bf 50 buf_start = 0;
mbed_official 0:a554658735bf 51 nextEp = NULL;
mbed_official 0:a554658735bf 52
mbed_official 0:a554658735bf 53 td_current = td_list[0];
mbed_official 0:a554658735bf 54 td_next = td_list[1];
mbed_official 24:868cbfe611a7 55
samux 4:b320d68e98e7 56 intf_nb = 0;
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 }
Kojto 37:f1e388e7b752 79 #endif
mbed_official 0:a554658735bf 80
mbed_official 0:a554658735bf 81 //Only for control Eps
mbed_official 0:a554658735bf 82 void USBEndpoint::setNextToken(uint32_t token)
mbed_official 0:a554658735bf 83 {
mbed_official 0:a554658735bf 84 switch (token) {
mbed_official 0:a554658735bf 85 case TD_SETUP:
mbed_official 0:a554658735bf 86 dir = OUT;
mbed_official 0:a554658735bf 87 setup = true;
mbed_official 0:a554658735bf 88 break;
mbed_official 0:a554658735bf 89 case TD_IN:
mbed_official 0:a554658735bf 90 dir = IN;
mbed_official 0:a554658735bf 91 setup = false;
mbed_official 0:a554658735bf 92 break;
mbed_official 0:a554658735bf 93 case TD_OUT:
mbed_official 0:a554658735bf 94 dir = OUT;
mbed_official 0:a554658735bf 95 setup = false;
mbed_official 0:a554658735bf 96 break;
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 };
Kojto 37:f1e388e7b752 123 const char * USBEndpoint::getStateString() {
Kojto 37:f1e388e7b752 124 return type_string[state].str;
Kojto 37:f1e388e7b752 125 }
mbed_official 0:a554658735bf 126
Kojto 37:f1e388e7b752 127 #if !defined(USBHOST_OTHER)
mbed_official 0:a554658735bf 128 void USBEndpoint::setState(uint8_t st) {
mbed_official 0:a554658735bf 129 if (st > 18)
mbed_official 0:a554658735bf 130 return;
mbed_official 0:a554658735bf 131 state = type_string[st].type;
mbed_official 0:a554658735bf 132 }
mbed_official 0:a554658735bf 133
Kojto 37:f1e388e7b752 134 USB_TYPE USBEndpoint::queueTransfer()
mbed_official 0:a554658735bf 135 {
mbed_official 0:a554658735bf 136 transfer_len = (uint32_t)td_current->bufEnd - (uint32_t)td_current->currBufPtr + 1;
mbed_official 0:a554658735bf 137 transferred = transfer_len;
mbed_official 0:a554658735bf 138 buf_start = (uint8_t *)td_current->currBufPtr;
mbed_official 0:a554658735bf 139
mbed_official 0:a554658735bf 140 //Now add this free TD at this end of the queue
mbed_official 0:a554658735bf 141 state = USB_TYPE_PROCESSING;
mbed_official 34:028508fd50fa 142 td_current->nextTD = (hcTd*)td_next;
mbed_official 0:a554658735bf 143 hced->tailTD = td_next;
Kojto 37:f1e388e7b752 144 return USB_TYPE_PROCESSING;
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 34:028508fd50fa 161 hced->nextED = (ed == NULL) ? 0 : (hcEd*)(ed->getHCED());
mbed_official 0:a554658735bf 162 }
Kojto 37:f1e388e7b752 163 #endif