a

Dependencies:   FATFileSystem mbed-rtos

Fork of USBHost by mbed official

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 #ifndef USBHALHOST_DISCOF429ZI
Kojto 39:d96aa62afc5b 17 #define USBHALHOST_DISCOF429ZI
Kojto 39:d96aa62afc5b 18
Kojto 39:d96aa62afc5b 19 #define USBHAL_IRQn OTG_HS_IRQn
Kojto 39:d96aa62afc5b 20
Kojto 39:d96aa62afc5b 21 #define HCCA_SIZE sizeof(HCD_HandleTypeDef)
Kojto 39:d96aa62afc5b 22 #define ED_SIZE sizeof(HCED)
Kojto 39:d96aa62afc5b 23 #define TD_SIZE sizeof(HCTD)
Kojto 39:d96aa62afc5b 24
Kojto 39:d96aa62afc5b 25 #define TOTAL_SIZE (HCCA_SIZE + (MAX_ENDPOINT*ED_SIZE) + (MAX_TD*TD_SIZE))
Kojto 39:d96aa62afc5b 26 /* STM device FS have 11 channels (definition is for 60 channels) */
Kojto 39:d96aa62afc5b 27 static volatile uint8_t usb_buf[TOTAL_SIZE];
Kojto 39:d96aa62afc5b 28 typedef struct
Kojto 39:d96aa62afc5b 29 {
Kojto 39:d96aa62afc5b 30 /* store the request ongoing on each endpoit */
Kojto 39:d96aa62afc5b 31 /* 1st field of structure avoid giving knowledge of all structure to
Kojto 39:d96aa62afc5b 32 * endpoint */
Kojto 39:d96aa62afc5b 33 volatile uint32_t addr[MAX_ENDPOINT];
Kojto 39:d96aa62afc5b 34 USBHALHost *inst;
Kojto 39:d96aa62afc5b 35 void (USBHALHost::*deviceConnected)(int hub, int port, bool lowSpeed, USBHostHub * hub_parent);
Kojto 39:d96aa62afc5b 36 void (USBHALHost::*deviceDisconnected)(int hub, int port, USBHostHub * hub_parent, volatile uint32_t addr);
Kojto 39:d96aa62afc5b 37 void (USBHALHost::*transferCompleted)(volatile uint32_t addr);
Kojto 39:d96aa62afc5b 38 }USBHALHost_Private_t;
Kojto 39:d96aa62afc5b 39 /* CONFIGURATION for USB_VBUS
Kojto 39:d96aa62afc5b 40 * on 64 bits board PC_0 is used (0 VBUS on, 1 VBUS off)
Kojto 39:d96aa62afc5b 41 * on 144 pins board PG_6 is used ( 1 VBUS on, 0 VBUS on)
Kojto 39:d96aa62afc5b 42 */
Kojto 39:d96aa62afc5b 43 static gpio_t gpio_vbus;
Kojto 39:d96aa62afc5b 44
Kojto 39:d96aa62afc5b 45 #define VBUS_OFF 1
Kojto 39:d96aa62afc5b 46 #define VBUS_ON 0
Kojto 39:d96aa62afc5b 47 #define USB_VBUS_CONFIG \
Kojto 39:d96aa62afc5b 48 do {__HAL_RCC_GPIOC_CLK_ENABLE();\
Kojto 39:d96aa62afc5b 49 gpio_init_out_ex(&gpio_vbus, PC_4, VBUS_OFF);\
Kojto 39:d96aa62afc5b 50 }while(0);
Kojto 39:d96aa62afc5b 51
Kojto 39:d96aa62afc5b 52
Kojto 39:d96aa62afc5b 53 void usb_vbus( uint8_t state)
Kojto 39:d96aa62afc5b 54 {
Kojto 39:d96aa62afc5b 55 if(state == 0)
Kojto 39:d96aa62afc5b 56 {
Kojto 39:d96aa62afc5b 57 gpio_write(&gpio_vbus, VBUS_OFF);
Kojto 39:d96aa62afc5b 58 }
Kojto 39:d96aa62afc5b 59 else
Kojto 39:d96aa62afc5b 60 {
Kojto 39:d96aa62afc5b 61 gpio_write(&gpio_vbus, VBUS_ON);
Kojto 39:d96aa62afc5b 62 }
Kojto 39:d96aa62afc5b 63 wait(0.2);
Kojto 39:d96aa62afc5b 64 }
Kojto 39:d96aa62afc5b 65
Kojto 39:d96aa62afc5b 66
Kojto 39:d96aa62afc5b 67 USBHALHost::USBHALHost() {
Kojto 39:d96aa62afc5b 68 gpio_t pin_vbus;
Kojto 39:d96aa62afc5b 69 instHost = this;
Kojto 39:d96aa62afc5b 70 HCD_HandleTypeDef *hhcd;
Kojto 39:d96aa62afc5b 71 USBHALHost_Private_t *HALPriv = new(USBHALHost_Private_t);
Kojto 39:d96aa62afc5b 72 memset(HALPriv, 0, sizeof(USBHALHost_Private_t));
Kojto 39:d96aa62afc5b 73 memInit();
Kojto 39:d96aa62afc5b 74 memset((void*)usb_hcca, 0, HCCA_SIZE);
Kojto 39:d96aa62afc5b 75 hhcd = (HCD_HandleTypeDef *)usb_hcca;
Kojto 39:d96aa62afc5b 76 hhcd->Instance = USB_OTG_HS;
Kojto 39:d96aa62afc5b 77 hhcd->pData = (void*)HALPriv;
Kojto 39:d96aa62afc5b 78 hhcd->Init.Host_channels = 11;
Kojto 39:d96aa62afc5b 79 /* for now failed with dma */
Kojto 39:d96aa62afc5b 80 hhcd->Init.dma_enable = 0;
Kojto 39:d96aa62afc5b 81 hhcd->Init.speed = HCD_SPEED_HIGH;
Kojto 39:d96aa62afc5b 82 hhcd->Init.phy_itface = HCD_PHY_EMBEDDED;
Kojto 39:d96aa62afc5b 83 hhcd->Init.use_external_vbus = 1;
Kojto 39:d96aa62afc5b 84 HALPriv->inst = this;
Kojto 39:d96aa62afc5b 85 HALPriv->deviceConnected = &USBHALHost::deviceConnected;
Kojto 39:d96aa62afc5b 86 HALPriv->deviceDisconnected = &USBHALHost::deviceDisconnected;
Kojto 39:d96aa62afc5b 87 HALPriv->transferCompleted = &USBHALHost::transferCompleted;
Kojto 39:d96aa62afc5b 88 for (int i = 0; i < MAX_ENDPOINT; i++) {
Kojto 39:d96aa62afc5b 89 edBufAlloc[i] = false;
Kojto 39:d96aa62afc5b 90 HALPriv->addr[i]=(uint32_t)-1;
Kojto 39:d96aa62afc5b 91 }
Kojto 39:d96aa62afc5b 92 for (int i = 0; i < MAX_TD; i++) {
Kojto 39:d96aa62afc5b 93 tdBufAlloc[i] = false;
Kojto 39:d96aa62afc5b 94 }
Kojto 39:d96aa62afc5b 95 /* Configure USB HS GPIOs */
Kojto 39:d96aa62afc5b 96 __HAL_RCC_GPIOB_CLK_ENABLE();
Kojto 39:d96aa62afc5b 97
Kojto 39:d96aa62afc5b 98 /*USB DM and DP */
Kojto 39:d96aa62afc5b 99 pin_function(PB_14, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF12_OTG_HS_FS));
Kojto 39:d96aa62afc5b 100 pin_function(PB_15, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF12_OTG_HS_FS));
Kojto 39:d96aa62afc5b 101 /* Configure VBUS Pin */
Kojto 39:d96aa62afc5b 102 gpio_init_in(&pin_vbus, PB_13);
Kojto 39:d96aa62afc5b 103 /* Configure POWER_SWITCH IO pin */
Kojto 39:d96aa62afc5b 104 USB_VBUS_CONFIG;
Kojto 39:d96aa62afc5b 105 /* Enable USB HS Clocks */
Kojto 39:d96aa62afc5b 106 __HAL_RCC_USB_OTG_HS_CLK_ENABLE();
Kojto 39:d96aa62afc5b 107
Kojto 39:d96aa62afc5b 108 /* Set USBFS Interrupt priority */
Kojto 39:d96aa62afc5b 109 HAL_NVIC_SetPriority(USBHAL_IRQn, 5, 0);
Kojto 39:d96aa62afc5b 110 NVIC_SetVector(USBHAL_IRQn, (uint32_t)&_usbisr);
Kojto 39:d96aa62afc5b 111 }
Kojto 39:d96aa62afc5b 112 #endif