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:
Kojto
Date:
Thu Jul 27 12:24:30 2017 +0100
Revision:
39:d96aa62afc5b
Update USBHost - add targets directory

This corresponds to mbed-os/master commit 9207365

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 39:d96aa62afc5b 1 /* mbed USBHost Library
Kojto 39:d96aa62afc5b 2 * Copyright (c) 2006-2013 ARM Limited
Kojto 39:d96aa62afc5b 3 *
Kojto 39:d96aa62afc5b 4 * Licensed under the Apache License, Version 2.0 (the "License");
Kojto 39:d96aa62afc5b 5 * you may not use this file except in compliance with the License.
Kojto 39:d96aa62afc5b 6 * You may obtain a copy of the License at
Kojto 39:d96aa62afc5b 7 *
Kojto 39:d96aa62afc5b 8 * http://www.apache.org/licenses/LICENSE-2.0
Kojto 39:d96aa62afc5b 9 *
Kojto 39:d96aa62afc5b 10 * Unless required by applicable law or agreed to in writing, software
Kojto 39:d96aa62afc5b 11 * distributed under the License is distributed on an "AS IS" BASIS,
Kojto 39:d96aa62afc5b 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Kojto 39:d96aa62afc5b 13 * See the License for the specific language governing permissions and
Kojto 39:d96aa62afc5b 14 * limitations under the License.
Kojto 39:d96aa62afc5b 15 */
Kojto 39:d96aa62afc5b 16 #if defined(TARGET_STM) && defined(USBHOST_OTHER)
Kojto 39:d96aa62afc5b 17
Kojto 39:d96aa62afc5b 18 #include "dbg.h"
Kojto 39:d96aa62afc5b 19 #include "USBEndpoint.h"
Kojto 39:d96aa62afc5b 20 extern uint32_t HAL_HCD_HC_GetMaxPacket(HCD_HandleTypeDef *hhcd, uint8_t chn_num);
Kojto 39:d96aa62afc5b 21 extern uint32_t HAL_HCD_HC_GetType(HCD_HandleTypeDef *hhcd, uint8_t chn_num);
Kojto 39:d96aa62afc5b 22 extern void HAL_HCD_DisableInt(HCD_HandleTypeDef* hhcd, uint8_t chn_num);
Kojto 39:d96aa62afc5b 23 extern void HAL_HCD_EnableInt(HCD_HandleTypeDef* hhcd, uint8_t chn_num);
Kojto 39:d96aa62afc5b 24
Kojto 39:d96aa62afc5b 25
Kojto 39:d96aa62afc5b 26
Kojto 39:d96aa62afc5b 27
Kojto 39:d96aa62afc5b 28 void USBEndpoint::init(HCED * hced_, ENDPOINT_TYPE type_, ENDPOINT_DIRECTION dir_, uint32_t size, uint8_t ep_number, HCTD* td_list_[2])
Kojto 39:d96aa62afc5b 29 {
Kojto 39:d96aa62afc5b 30 HCD_HandleTypeDef *hhcd;
Kojto 39:d96aa62afc5b 31 uint32_t *addr;
Kojto 39:d96aa62afc5b 32
Kojto 39:d96aa62afc5b 33 hced = hced_;
Kojto 39:d96aa62afc5b 34 type = type_;
Kojto 39:d96aa62afc5b 35 dir = dir_;
Kojto 39:d96aa62afc5b 36 setup = (type == CONTROL_ENDPOINT) ? true : false;
Kojto 39:d96aa62afc5b 37
Kojto 39:d96aa62afc5b 38 //TDs have been allocated by the host
Kojto 39:d96aa62afc5b 39 memcpy((HCTD**)td_list, td_list_, sizeof(HCTD*)*2); //TODO: Maybe should add a param for td_list size... at least a define
Kojto 39:d96aa62afc5b 40 memset(td_list_[0], 0, sizeof(HCTD));
Kojto 39:d96aa62afc5b 41 memset(td_list_[1], 0, sizeof(HCTD));
Kojto 39:d96aa62afc5b 42
Kojto 39:d96aa62afc5b 43 td_list[0]->ep = this;
Kojto 39:d96aa62afc5b 44 td_list[1]->ep = this;
Kojto 39:d96aa62afc5b 45
Kojto 39:d96aa62afc5b 46 address = (ep_number & 0x7F) | ((dir - 1) << 7);
Kojto 39:d96aa62afc5b 47 this->size = size;
Kojto 39:d96aa62afc5b 48 this->ep_number = ep_number;
Kojto 39:d96aa62afc5b 49 transfer_len = 0;
Kojto 39:d96aa62afc5b 50 transferred = 0;
Kojto 39:d96aa62afc5b 51 buf_start = 0;
Kojto 39:d96aa62afc5b 52 nextEp = NULL;
Kojto 39:d96aa62afc5b 53
Kojto 39:d96aa62afc5b 54 td_current = td_list[0];
Kojto 39:d96aa62afc5b 55 td_next = td_list[1];
Kojto 39:d96aa62afc5b 56 /* remove potential post pending from previous endpoint */
Kojto 39:d96aa62afc5b 57 ep_queue.get(0);
Kojto 39:d96aa62afc5b 58 intf_nb = 0;
Kojto 39:d96aa62afc5b 59 hhcd = (HCD_HandleTypeDef*)hced->hhcd;
Kojto 39:d96aa62afc5b 60 addr = &((uint32_t *)hhcd->pData)[hced->ch_num];
Kojto 39:d96aa62afc5b 61 *addr = 0;
Kojto 39:d96aa62afc5b 62 state = USB_TYPE_IDLE;
Kojto 39:d96aa62afc5b 63 speed =false;
Kojto 39:d96aa62afc5b 64 }
Kojto 39:d96aa62afc5b 65 void USBEndpoint::setSize(uint32_t size)
Kojto 39:d96aa62afc5b 66 {
Kojto 39:d96aa62afc5b 67 this->size = size;
Kojto 39:d96aa62afc5b 68 }
Kojto 39:d96aa62afc5b 69
Kojto 39:d96aa62afc5b 70
Kojto 39:d96aa62afc5b 71 void USBEndpoint::setDeviceAddress(uint8_t addr)
Kojto 39:d96aa62afc5b 72 {
Kojto 39:d96aa62afc5b 73 HCD_HandleTypeDef *hhcd;
Kojto 39:d96aa62afc5b 74 uint8_t hcd_speed = HCD_SPEED_FULL;
Kojto 39:d96aa62afc5b 75 /* fix me : small speed device with hub not supported
Kojto 39:d96aa62afc5b 76 if (this->speed) hcd_speed = HCD_SPEED_LOW; */
Kojto 39:d96aa62afc5b 77 if (this->speed) {
Kojto 39:d96aa62afc5b 78 USB_WARN("small speed device on hub not supported");
Kojto 39:d96aa62afc5b 79 }
Kojto 39:d96aa62afc5b 80 MBED_ASSERT(HAL_HCD_HC_Init((HCD_HandleTypeDef*)hced->hhcd,hced->ch_num, address, addr, hcd_speed, type, size)!=HAL_BUSY);
Kojto 39:d96aa62afc5b 81 this->device_address = addr;
Kojto 39:d96aa62afc5b 82
Kojto 39:d96aa62afc5b 83 }
Kojto 39:d96aa62afc5b 84
Kojto 39:d96aa62afc5b 85 void USBEndpoint::setSpeed(uint8_t speed)
Kojto 39:d96aa62afc5b 86 {
Kojto 39:d96aa62afc5b 87 this->speed = speed;
Kojto 39:d96aa62afc5b 88 }
Kojto 39:d96aa62afc5b 89
Kojto 39:d96aa62afc5b 90
Kojto 39:d96aa62afc5b 91
Kojto 39:d96aa62afc5b 92 void USBEndpoint::setState(USB_TYPE st)
Kojto 39:d96aa62afc5b 93 {
Kojto 39:d96aa62afc5b 94 /* modify this state is possible only with a plug */
Kojto 39:d96aa62afc5b 95 if (state == USB_TYPE_FREE) {
Kojto 39:d96aa62afc5b 96 return;
Kojto 39:d96aa62afc5b 97 }
Kojto 39:d96aa62afc5b 98
Kojto 39:d96aa62afc5b 99 state = st;
Kojto 39:d96aa62afc5b 100 if (st == USB_TYPE_FREE) {
Kojto 39:d96aa62afc5b 101 HCD_HandleTypeDef *hhcd = (HCD_HandleTypeDef*)hced->hhcd;
Kojto 39:d96aa62afc5b 102 uint32_t *addr = &((uint32_t *)hhcd->pData)[hced->ch_num];
Kojto 39:d96aa62afc5b 103 if ((*addr) && (type != INTERRUPT_ENDPOINT)) {
Kojto 39:d96aa62afc5b 104 this->ep_queue.put((uint8_t*)1);
Kojto 39:d96aa62afc5b 105 }
Kojto 39:d96aa62afc5b 106 MBED_ASSERT(HAL_HCD_HC_Halt((HCD_HandleTypeDef*)hced->hhcd, hced->ch_num)!=HAL_BUSY);
Kojto 39:d96aa62afc5b 107 HAL_HCD_DisableInt((HCD_HandleTypeDef*)hced->hhcd, hced->ch_num);
Kojto 39:d96aa62afc5b 108 *addr = 0;
Kojto 39:d96aa62afc5b 109
Kojto 39:d96aa62afc5b 110 }
Kojto 39:d96aa62afc5b 111 if (st == USB_TYPE_ERROR) {
Kojto 39:d96aa62afc5b 112 MBED_ASSERT(HAL_HCD_HC_Halt((HCD_HandleTypeDef*)hced->hhcd, hced->ch_num)!=HAL_BUSY);
Kojto 39:d96aa62afc5b 113 HAL_HCD_DisableInt((HCD_HandleTypeDef*)hced->hhcd, hced->ch_num);
Kojto 39:d96aa62afc5b 114
Kojto 39:d96aa62afc5b 115 }
Kojto 39:d96aa62afc5b 116 if (st == USB_TYPE_ERROR) {
Kojto 39:d96aa62afc5b 117 uint8_t hcd_speed = HCD_SPEED_FULL;
Kojto 39:d96aa62afc5b 118 /* small speed device with hub not supported
Kojto 39:d96aa62afc5b 119 if (this->speed) hcd_speed = HCD_SPEED_LOW;*/
Kojto 39:d96aa62afc5b 120 MBED_ASSERT(HAL_HCD_HC_Init((HCD_HandleTypeDef*)hced->hhcd,hced->ch_num, address, 0, hcd_speed, type, size)!=HAL_BUSY);
Kojto 39:d96aa62afc5b 121 }
Kojto 39:d96aa62afc5b 122 }
Kojto 39:d96aa62afc5b 123
Kojto 39:d96aa62afc5b 124
Kojto 39:d96aa62afc5b 125 extern uint32_t HAL_HCD_HC_GetMaxPacket(HCD_HandleTypeDef *hhcd, uint8_t chn_num);
Kojto 39:d96aa62afc5b 126 extern uint32_t HAL_HCD_HC_GetType(HCD_HandleTypeDef *hhcd, uint8_t chn_num);
Kojto 39:d96aa62afc5b 127
Kojto 39:d96aa62afc5b 128
Kojto 39:d96aa62afc5b 129 USB_TYPE USBEndpoint::queueTransfer()
Kojto 39:d96aa62afc5b 130 {
Kojto 39:d96aa62afc5b 131 HCD_HandleTypeDef *hhcd = (HCD_HandleTypeDef*)hced->hhcd;
Kojto 39:d96aa62afc5b 132 uint32_t *addr = &((uint32_t *)hhcd->pData)[hced->ch_num];
Kojto 39:d96aa62afc5b 133 uint32_t type = HAL_HCD_HC_GetType(hhcd, hced->ch_num);
Kojto 39:d96aa62afc5b 134 uint32_t max_size = HAL_HCD_HC_GetMaxPacket(hhcd, hced->ch_num);
Kojto 39:d96aa62afc5b 135 /* if a packet is queue on disconnected ; no solution for now */
Kojto 39:d96aa62afc5b 136 if (state == USB_TYPE_FREE) {
Kojto 39:d96aa62afc5b 137 td_current->state = USB_TYPE_FREE;
Kojto 39:d96aa62afc5b 138 return USB_TYPE_FREE;
Kojto 39:d96aa62afc5b 139 }
Kojto 39:d96aa62afc5b 140 ep_queue.get(0);
Kojto 39:d96aa62afc5b 141 MBED_ASSERT(*addr ==0);
Kojto 39:d96aa62afc5b 142 transfer_len = td_current->size <= max_size ? td_current->size : max_size;
Kojto 39:d96aa62afc5b 143 buf_start = (uint8_t *)td_current->currBufPtr;
Kojto 39:d96aa62afc5b 144
Kojto 39:d96aa62afc5b 145 //Now add this free TD at this end of the queue
Kojto 39:d96aa62afc5b 146 state = USB_TYPE_PROCESSING;
Kojto 39:d96aa62afc5b 147 /* one request */
Kojto 39:d96aa62afc5b 148 td_current->nextTD = (hcTd*)0;
Kojto 39:d96aa62afc5b 149 #if defined(MAX_NYET_RETRY)
Kojto 39:d96aa62afc5b 150 td_current->retry = 0;
Kojto 39:d96aa62afc5b 151 #endif
Kojto 39:d96aa62afc5b 152 td_current->setup = setup;
Kojto 39:d96aa62afc5b 153 *addr = (uint32_t)td_current;
Kojto 39:d96aa62afc5b 154 /* dir /setup is inverted for ST */
Kojto 39:d96aa62afc5b 155 /* token is useful only ctrl endpoint */
Kojto 39:d96aa62afc5b 156 /* last parameter is ping ? */
Kojto 39:d96aa62afc5b 157 MBED_ASSERT(HAL_HCD_HC_SubmitRequest((HCD_HandleTypeDef*)hced->hhcd, hced->ch_num, dir-1, type,!setup,(uint8_t*) td_current->currBufPtr, transfer_len, 0)==HAL_OK);
Kojto 39:d96aa62afc5b 158 HAL_HCD_EnableInt((HCD_HandleTypeDef*)hced->hhcd, hced->ch_num);
Kojto 39:d96aa62afc5b 159
Kojto 39:d96aa62afc5b 160 return USB_TYPE_PROCESSING;
Kojto 39:d96aa62afc5b 161 }
Kojto 39:d96aa62afc5b 162
Kojto 39:d96aa62afc5b 163 void USBEndpoint::unqueueTransfer(volatile HCTD * td)
Kojto 39:d96aa62afc5b 164 {
Kojto 39:d96aa62afc5b 165 if (state==USB_TYPE_FREE) {
Kojto 39:d96aa62afc5b 166 return;
Kojto 39:d96aa62afc5b 167 }
Kojto 39:d96aa62afc5b 168 uint32_t *addr = &((uint32_t *)((HCD_HandleTypeDef*)hced->hhcd)->pData)[hced->ch_num];
Kojto 39:d96aa62afc5b 169 td->state=0;
Kojto 39:d96aa62afc5b 170 td->currBufPtr=0;
Kojto 39:d96aa62afc5b 171 td->size=0;
Kojto 39:d96aa62afc5b 172 td->nextTD=0;
Kojto 39:d96aa62afc5b 173 *addr = 0;
Kojto 39:d96aa62afc5b 174 td_current = td_next;
Kojto 39:d96aa62afc5b 175 td_next = td;
Kojto 39:d96aa62afc5b 176 }
Kojto 39:d96aa62afc5b 177
Kojto 39:d96aa62afc5b 178 void USBEndpoint::queueEndpoint(USBEndpoint * ed)
Kojto 39:d96aa62afc5b 179 {
Kojto 39:d96aa62afc5b 180 nextEp = ed;
Kojto 39:d96aa62afc5b 181 }
Kojto 39:d96aa62afc5b 182 #endif