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
Kojto 39:d96aa62afc5b 17 #ifdef TARGET_STM
Kojto 39:d96aa62afc5b 18 #include "mbed.h"
Kojto 39:d96aa62afc5b 19 #include "USBHALHost.h"
Kojto 39:d96aa62afc5b 20 #include "dbg.h"
Kojto 39:d96aa62afc5b 21 #include "pinmap.h"
Kojto 39:d96aa62afc5b 22
Kojto 39:d96aa62afc5b 23 #include "USBHALHost_STM_TARGET.h"
Kojto 39:d96aa62afc5b 24
Kojto 39:d96aa62afc5b 25 void HAL_HCD_Connect_Callback(HCD_HandleTypeDef *hhcd)
Kojto 39:d96aa62afc5b 26 {
Kojto 39:d96aa62afc5b 27 USBHALHost_Private_t *priv=(USBHALHost_Private_t *)(hhcd->pData);
Kojto 39:d96aa62afc5b 28 USBHALHost *obj= priv->inst;
Kojto 39:d96aa62afc5b 29 void (USBHALHost::*func)(int hub, int port, bool lowSpeed, USBHostHub * hub_parent ) = priv->deviceConnected;
Kojto 39:d96aa62afc5b 30 (obj->*func)(0,1,0,NULL);
Kojto 39:d96aa62afc5b 31 }
Kojto 39:d96aa62afc5b 32 void HAL_HCD_Disconnect_Callback(HCD_HandleTypeDef *hhcd)
Kojto 39:d96aa62afc5b 33 {
Kojto 39:d96aa62afc5b 34 USBHALHost_Private_t *priv=(USBHALHost_Private_t *)(hhcd->pData);
Kojto 39:d96aa62afc5b 35 USBHALHost *obj= priv->inst;
Kojto 39:d96aa62afc5b 36 void (USBHALHost::*func1)(int hub, int port, USBHostHub * hub_parent, volatile uint32_t addr)= priv->deviceDisconnected;
Kojto 39:d96aa62afc5b 37 (obj->*func1)(0,1,(USBHostHub *)NULL,0);
Kojto 39:d96aa62afc5b 38 }
Kojto 39:d96aa62afc5b 39 int HAL_HCD_HC_GetDirection(HCD_HandleTypeDef *hhcd,uint8_t chnum)
Kojto 39:d96aa62afc5b 40 {
Kojto 39:d96aa62afc5b 41 /* useful for transmission */
Kojto 39:d96aa62afc5b 42 return hhcd->hc[chnum].ep_is_in;
Kojto 39:d96aa62afc5b 43 }
Kojto 39:d96aa62afc5b 44
Kojto 39:d96aa62afc5b 45 uint32_t HAL_HCD_HC_GetMaxPacket(HCD_HandleTypeDef *hhcd,uint8_t chnum)
Kojto 39:d96aa62afc5b 46 {
Kojto 39:d96aa62afc5b 47 /* useful for transmission */
Kojto 39:d96aa62afc5b 48 return hhcd->hc[chnum].max_packet;
Kojto 39:d96aa62afc5b 49 }
Kojto 39:d96aa62afc5b 50
Kojto 39:d96aa62afc5b 51 void HAL_HCD_EnableInt(HCD_HandleTypeDef *hhcd,uint8_t chnum)
Kojto 39:d96aa62afc5b 52 {
Kojto 39:d96aa62afc5b 53 USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
Kojto 39:d96aa62afc5b 54 USBx_HOST->HAINTMSK |= (1 << chnum);
Kojto 39:d96aa62afc5b 55 }
Kojto 39:d96aa62afc5b 56
Kojto 39:d96aa62afc5b 57
Kojto 39:d96aa62afc5b 58 void HAL_HCD_DisableInt(HCD_HandleTypeDef *hhcd,uint8_t chnum)
Kojto 39:d96aa62afc5b 59 {
Kojto 39:d96aa62afc5b 60 USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
Kojto 39:d96aa62afc5b 61 USBx_HOST->HAINTMSK &= ~(1 << chnum);
Kojto 39:d96aa62afc5b 62 }
Kojto 39:d96aa62afc5b 63 uint32_t HAL_HCD_HC_GetType(HCD_HandleTypeDef *hhcd,uint8_t chnum)
Kojto 39:d96aa62afc5b 64 {
Kojto 39:d96aa62afc5b 65 /* useful for transmission */
Kojto 39:d96aa62afc5b 66 return hhcd->hc[chnum].ep_type;
Kojto 39:d96aa62afc5b 67 }
Kojto 39:d96aa62afc5b 68
Kojto 39:d96aa62afc5b 69 void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd,uint8_t chnum, HCD_URBStateTypeDef urb_state)
Kojto 39:d96aa62afc5b 70 {
Kojto 39:d96aa62afc5b 71 USBHALHost_Private_t *priv=(USBHALHost_Private_t *)(hhcd->pData);
Kojto 39:d96aa62afc5b 72 USBHALHost *obj= priv->inst;
Kojto 39:d96aa62afc5b 73 void (USBHALHost::*func)(volatile uint32_t addr)= priv->transferCompleted;
Kojto 39:d96aa62afc5b 74
Kojto 39:d96aa62afc5b 75 uint32_t addr = priv->addr[chnum];
Kojto 39:d96aa62afc5b 76 uint32_t max_size = HAL_HCD_HC_GetMaxPacket(hhcd, chnum);
Kojto 39:d96aa62afc5b 77 uint32_t type = HAL_HCD_HC_GetType(hhcd, chnum);
Kojto 39:d96aa62afc5b 78 uint32_t dir = HAL_HCD_HC_GetDirection(hhcd,chnum);
Kojto 39:d96aa62afc5b 79 uint32_t length;
Kojto 39:d96aa62afc5b 80 if ( (addr!=0)) {
Kojto 39:d96aa62afc5b 81 HCTD *td = (HCTD *)addr;
Kojto 39:d96aa62afc5b 82
Kojto 39:d96aa62afc5b 83 if ((type == EP_TYPE_BULK) || (type == EP_TYPE_CTRL )) {
Kojto 39:d96aa62afc5b 84 switch (urb_state) {
Kojto 39:d96aa62afc5b 85 case URB_DONE:
Kojto 39:d96aa62afc5b 86 #if defined(MAX_NYET_RETRY)
Kojto 39:d96aa62afc5b 87 td->retry = 0;
Kojto 39:d96aa62afc5b 88 #endif
Kojto 39:d96aa62afc5b 89 if (td->size > max_size) {
Kojto 39:d96aa62afc5b 90 /* enqueue another request */
Kojto 39:d96aa62afc5b 91 td->currBufPtr += max_size;
Kojto 39:d96aa62afc5b 92 td->size -= max_size;
Kojto 39:d96aa62afc5b 93 length = td->size <= max_size ? td->size : max_size;
Kojto 39:d96aa62afc5b 94 MBED_ASSERT(HAL_HCD_HC_SubmitRequest(hhcd, chnum, dir ,type , !td->setup,(uint8_t*) td->currBufPtr, length, 0)==HAL_OK);
Kojto 39:d96aa62afc5b 95 HAL_HCD_EnableInt(hhcd, chnum);
Kojto 39:d96aa62afc5b 96 return;
Kojto 39:d96aa62afc5b 97 }
Kojto 39:d96aa62afc5b 98 break;
Kojto 39:d96aa62afc5b 99 case URB_NOTREADY:
Kojto 39:d96aa62afc5b 100 /* try again */
Kojto 39:d96aa62afc5b 101 /* abritary limit , to avoid dead lock if other error than
Kojto 39:d96aa62afc5b 102 * slow response is */
Kojto 39:d96aa62afc5b 103 #if defined(MAX_NYET_RETRY)
Kojto 39:d96aa62afc5b 104 if (td->retry < MAX_NYET_RETRY) {
Kojto 39:d96aa62afc5b 105 /* increment retry counter */
Kojto 39:d96aa62afc5b 106 td->retry++;
Kojto 39:d96aa62afc5b 107 #endif
Kojto 39:d96aa62afc5b 108 length = td->size <= max_size ? td->size : max_size;
Kojto 39:d96aa62afc5b 109 MBED_ASSERT(HAL_HCD_HC_SubmitRequest(hhcd, chnum, dir ,type , !td->setup,(uint8_t*) td->currBufPtr, length, 0)==HAL_OK);
Kojto 39:d96aa62afc5b 110 HAL_HCD_EnableInt(hhcd, chnum);
Kojto 39:d96aa62afc5b 111 return;
Kojto 39:d96aa62afc5b 112 #if defined(MAX_NYET_RETRY)
Kojto 39:d96aa62afc5b 113 } else {
Kojto 39:d96aa62afc5b 114 USB_ERR("urb_state != URB_NOTREADY");
Kojto 39:d96aa62afc5b 115 }
Kojto 39:d96aa62afc5b 116 #endif
Kojto 39:d96aa62afc5b 117 break;
Kojto 39:d96aa62afc5b 118 }
Kojto 39:d96aa62afc5b 119 }
Kojto 39:d96aa62afc5b 120 if ((type == EP_TYPE_INTR) ) {
Kojto 39:d96aa62afc5b 121 /* reply a packet of length NULL, this will be analyse in call back
Kojto 39:d96aa62afc5b 122 * for mouse or hub */
Kojto 39:d96aa62afc5b 123 td->state =USB_TYPE_IDLE ;
Kojto 39:d96aa62afc5b 124 HAL_HCD_DisableInt(hhcd, chnum);
Kojto 39:d96aa62afc5b 125
Kojto 39:d96aa62afc5b 126 } else {
Kojto 39:d96aa62afc5b 127 td->state = (urb_state == URB_DONE) ? USB_TYPE_IDLE : USB_TYPE_ERROR;
Kojto 39:d96aa62afc5b 128 }
Kojto 39:d96aa62afc5b 129 td->currBufPtr +=HAL_HCD_HC_GetXferCount(hhcd, chnum);
Kojto 39:d96aa62afc5b 130 (obj->*func)(addr);
Kojto 39:d96aa62afc5b 131 } else {
Kojto 39:d96aa62afc5b 132 if (urb_state !=0) {
Kojto 39:d96aa62afc5b 133 USB_DBG_EVENT("spurious %d %d",chnum, urb_state);
Kojto 39:d96aa62afc5b 134 }
Kojto 39:d96aa62afc5b 135 }
Kojto 39:d96aa62afc5b 136 }
Kojto 39:d96aa62afc5b 137
Kojto 39:d96aa62afc5b 138 USBHALHost * USBHALHost::instHost;
Kojto 39:d96aa62afc5b 139
Kojto 39:d96aa62afc5b 140
Kojto 39:d96aa62afc5b 141 void USBHALHost::init()
Kojto 39:d96aa62afc5b 142 {
Kojto 39:d96aa62afc5b 143
Kojto 39:d96aa62afc5b 144 NVIC_DisableIRQ(USBHAL_IRQn);
Kojto 39:d96aa62afc5b 145 NVIC_SetVector(USBHAL_IRQn, (uint32_t)(_usbisr));
Kojto 39:d96aa62afc5b 146 HAL_HCD_Init((HCD_HandleTypeDef *) usb_hcca);
Kojto 39:d96aa62afc5b 147 NVIC_EnableIRQ(USBHAL_IRQn);
Kojto 39:d96aa62afc5b 148 control_disable = 0;
Kojto 39:d96aa62afc5b 149 HAL_HCD_Start((HCD_HandleTypeDef *) usb_hcca);
Kojto 39:d96aa62afc5b 150 usb_vbus(1);
Kojto 39:d96aa62afc5b 151 }
Kojto 39:d96aa62afc5b 152
Kojto 39:d96aa62afc5b 153 uint32_t USBHALHost::controlHeadED()
Kojto 39:d96aa62afc5b 154 {
Kojto 39:d96aa62afc5b 155 return 0xffffffff;
Kojto 39:d96aa62afc5b 156 }
Kojto 39:d96aa62afc5b 157
Kojto 39:d96aa62afc5b 158 uint32_t USBHALHost::bulkHeadED()
Kojto 39:d96aa62afc5b 159 {
Kojto 39:d96aa62afc5b 160 return 0xffffffff;
Kojto 39:d96aa62afc5b 161 }
Kojto 39:d96aa62afc5b 162
Kojto 39:d96aa62afc5b 163 uint32_t USBHALHost::interruptHeadED()
Kojto 39:d96aa62afc5b 164 {
Kojto 39:d96aa62afc5b 165 return 0xffffffff;
Kojto 39:d96aa62afc5b 166 }
Kojto 39:d96aa62afc5b 167
Kojto 39:d96aa62afc5b 168 void USBHALHost::updateBulkHeadED(uint32_t addr)
Kojto 39:d96aa62afc5b 169 {
Kojto 39:d96aa62afc5b 170 }
Kojto 39:d96aa62afc5b 171
Kojto 39:d96aa62afc5b 172
Kojto 39:d96aa62afc5b 173 void USBHALHost::updateControlHeadED(uint32_t addr)
Kojto 39:d96aa62afc5b 174 {
Kojto 39:d96aa62afc5b 175 }
Kojto 39:d96aa62afc5b 176
Kojto 39:d96aa62afc5b 177 void USBHALHost::updateInterruptHeadED(uint32_t addr)
Kojto 39:d96aa62afc5b 178 {
Kojto 39:d96aa62afc5b 179 }
Kojto 39:d96aa62afc5b 180
Kojto 39:d96aa62afc5b 181
Kojto 39:d96aa62afc5b 182 void USBHALHost::enableList(ENDPOINT_TYPE type)
Kojto 39:d96aa62afc5b 183 {
Kojto 39:d96aa62afc5b 184 /* react when the 3 lists are requested to be disabled */
Kojto 39:d96aa62afc5b 185 if (type == CONTROL_ENDPOINT) {
Kojto 39:d96aa62afc5b 186 control_disable--;
Kojto 39:d96aa62afc5b 187 if (control_disable == 0) {
Kojto 39:d96aa62afc5b 188 NVIC_EnableIRQ(USBHAL_IRQn);
Kojto 39:d96aa62afc5b 189 } else {
Kojto 39:d96aa62afc5b 190 printf("reent\n");
Kojto 39:d96aa62afc5b 191 }
Kojto 39:d96aa62afc5b 192 }
Kojto 39:d96aa62afc5b 193 }
Kojto 39:d96aa62afc5b 194
Kojto 39:d96aa62afc5b 195
Kojto 39:d96aa62afc5b 196 bool USBHALHost::disableList(ENDPOINT_TYPE type)
Kojto 39:d96aa62afc5b 197 {
Kojto 39:d96aa62afc5b 198 if (type == CONTROL_ENDPOINT) {
Kojto 39:d96aa62afc5b 199 NVIC_DisableIRQ(USBHAL_IRQn);
Kojto 39:d96aa62afc5b 200 control_disable++;
Kojto 39:d96aa62afc5b 201 if (control_disable > 1) {
Kojto 39:d96aa62afc5b 202 printf("disable reentrance !!!\n");
Kojto 39:d96aa62afc5b 203 }
Kojto 39:d96aa62afc5b 204 return true;
Kojto 39:d96aa62afc5b 205 }
Kojto 39:d96aa62afc5b 206 return false;
Kojto 39:d96aa62afc5b 207 }
Kojto 39:d96aa62afc5b 208
Kojto 39:d96aa62afc5b 209
Kojto 39:d96aa62afc5b 210 void USBHALHost::memInit()
Kojto 39:d96aa62afc5b 211 {
Kojto 39:d96aa62afc5b 212 usb_hcca = (volatile HCD_HandleTypeDef *)usb_buf;
Kojto 39:d96aa62afc5b 213 usb_edBuf = usb_buf + HCCA_SIZE;
Kojto 39:d96aa62afc5b 214 usb_tdBuf = usb_buf + HCCA_SIZE +(MAX_ENDPOINT*ED_SIZE);
Kojto 39:d96aa62afc5b 215 /* init channel */
Kojto 39:d96aa62afc5b 216 memset((void*)usb_buf,0, TOTAL_SIZE);
Kojto 39:d96aa62afc5b 217 for (int i=0; i < MAX_ENDPOINT; i++) {
Kojto 39:d96aa62afc5b 218 HCED *hced = (HCED*)(usb_edBuf + i*ED_SIZE);
Kojto 39:d96aa62afc5b 219 hced->ch_num = i;
Kojto 39:d96aa62afc5b 220 hced->hhcd = (HCCA *) usb_hcca;
Kojto 39:d96aa62afc5b 221 }
Kojto 39:d96aa62afc5b 222 }
Kojto 39:d96aa62afc5b 223
Kojto 39:d96aa62afc5b 224 volatile uint8_t * USBHALHost::getED()
Kojto 39:d96aa62afc5b 225 {
Kojto 39:d96aa62afc5b 226 for (int i = 0; i < MAX_ENDPOINT; i++) {
Kojto 39:d96aa62afc5b 227 if ( !edBufAlloc[i] ) {
Kojto 39:d96aa62afc5b 228 edBufAlloc[i] = true;
Kojto 39:d96aa62afc5b 229 return (volatile uint8_t *)(usb_edBuf + i*ED_SIZE);
Kojto 39:d96aa62afc5b 230 }
Kojto 39:d96aa62afc5b 231 }
Kojto 39:d96aa62afc5b 232 perror("Could not allocate ED\r\n");
Kojto 39:d96aa62afc5b 233 return NULL; //Could not alloc ED
Kojto 39:d96aa62afc5b 234 }
Kojto 39:d96aa62afc5b 235
Kojto 39:d96aa62afc5b 236 volatile uint8_t * USBHALHost::getTD()
Kojto 39:d96aa62afc5b 237 {
Kojto 39:d96aa62afc5b 238 int i;
Kojto 39:d96aa62afc5b 239 for (i = 0; i < MAX_TD; i++) {
Kojto 39:d96aa62afc5b 240 if ( !tdBufAlloc[i] ) {
Kojto 39:d96aa62afc5b 241 tdBufAlloc[i] = true;
Kojto 39:d96aa62afc5b 242 return (volatile uint8_t *)(usb_tdBuf + i*TD_SIZE);
Kojto 39:d96aa62afc5b 243 }
Kojto 39:d96aa62afc5b 244 }
Kojto 39:d96aa62afc5b 245 perror("Could not allocate TD\r\n");
Kojto 39:d96aa62afc5b 246 return NULL; //Could not alloc TD
Kojto 39:d96aa62afc5b 247 }
Kojto 39:d96aa62afc5b 248
Kojto 39:d96aa62afc5b 249
Kojto 39:d96aa62afc5b 250 void USBHALHost::freeED(volatile uint8_t * ed)
Kojto 39:d96aa62afc5b 251 {
Kojto 39:d96aa62afc5b 252 int i;
Kojto 39:d96aa62afc5b 253 i = (ed - usb_edBuf) / ED_SIZE;
Kojto 39:d96aa62afc5b 254 edBufAlloc[i] = false;
Kojto 39:d96aa62afc5b 255 }
Kojto 39:d96aa62afc5b 256
Kojto 39:d96aa62afc5b 257 void USBHALHost::freeTD(volatile uint8_t * td)
Kojto 39:d96aa62afc5b 258 {
Kojto 39:d96aa62afc5b 259 int i;
Kojto 39:d96aa62afc5b 260 i = (td - usb_tdBuf) / TD_SIZE;
Kojto 39:d96aa62afc5b 261 tdBufAlloc[i] = false;
Kojto 39:d96aa62afc5b 262 }
Kojto 39:d96aa62afc5b 263
Kojto 39:d96aa62afc5b 264
Kojto 39:d96aa62afc5b 265 void USBHALHost::resetRootHub()
Kojto 39:d96aa62afc5b 266 {
Kojto 39:d96aa62afc5b 267 // Initiate port reset
Kojto 39:d96aa62afc5b 268 wait(0.2);
Kojto 39:d96aa62afc5b 269 HAL_HCD_ResetPort((HCD_HandleTypeDef *)usb_hcca);
Kojto 39:d96aa62afc5b 270 }
Kojto 39:d96aa62afc5b 271
Kojto 39:d96aa62afc5b 272
Kojto 39:d96aa62afc5b 273 void USBHALHost::_usbisr(void)
Kojto 39:d96aa62afc5b 274 {
Kojto 39:d96aa62afc5b 275 if (instHost) {
Kojto 39:d96aa62afc5b 276 instHost->UsbIrqhandler();
Kojto 39:d96aa62afc5b 277 }
Kojto 39:d96aa62afc5b 278 }
Kojto 39:d96aa62afc5b 279
Kojto 39:d96aa62afc5b 280 void USBHALHost::UsbIrqhandler()
Kojto 39:d96aa62afc5b 281 {
Kojto 39:d96aa62afc5b 282 HAL_HCD_IRQHandler((HCD_HandleTypeDef *)usb_hcca);
Kojto 39:d96aa62afc5b 283 }
Kojto 39:d96aa62afc5b 284 #endif