|
| 1 | +/* mbed USBHost Library |
| 2 | + * Copyright (c) 2006-2013 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#ifndef USBHALHOST_DISCOF429ZI |
| 17 | +#define USBHALHOST_DISCOF429ZI |
| 18 | + |
| 19 | +#define USBHAL_IRQn OTG_HS_IRQn |
| 20 | + |
| 21 | +#define HCCA_SIZE sizeof(HCD_HandleTypeDef) |
| 22 | +#define ED_SIZE sizeof(HCED) |
| 23 | +#define TD_SIZE sizeof(HCTD) |
| 24 | + |
| 25 | +#define TOTAL_SIZE (HCCA_SIZE + (MAX_ENDPOINT*ED_SIZE) + (MAX_TD*TD_SIZE)) |
| 26 | +/* STM device FS have 11 channels (definition is for 60 channels) */ |
| 27 | +static volatile uint8_t usb_buf[TOTAL_SIZE]; |
| 28 | +typedef struct |
| 29 | +{ |
| 30 | + /* store the request ongoing on each endpoit */ |
| 31 | + /* 1st field of structure avoid giving knowledge of all structure to |
| 32 | + * endpoint */ |
| 33 | + volatile uint32_t addr[MAX_ENDPOINT]; |
| 34 | + USBHALHost *inst; |
| 35 | + void (USBHALHost::*deviceConnected)(int hub, int port, bool lowSpeed, USBHostHub * hub_parent); |
| 36 | + void (USBHALHost::*deviceDisconnected)(int hub, int port, USBHostHub * hub_parent, volatile uint32_t addr); |
| 37 | + void (USBHALHost::*transferCompleted)(volatile uint32_t addr); |
| 38 | +}USBHALHost_Private_t; |
| 39 | +/* CONFIGURATION for USB_VBUS |
| 40 | + * on 64 bits board PC_0 is used (0 VBUS on, 1 VBUS off) |
| 41 | + * on 144 pins board PG_6 is used ( 1 VBUS on, 0 VBUS on) |
| 42 | + */ |
| 43 | +static gpio_t gpio_vbus; |
| 44 | + |
| 45 | +#define VBUS_OFF 1 |
| 46 | +#define VBUS_ON 0 |
| 47 | +#define USB_VBUS_CONFIG \ |
| 48 | + do {__HAL_RCC_GPIOC_CLK_ENABLE();\ |
| 49 | + gpio_init_out_ex(&gpio_vbus, PC_4, VBUS_OFF);\ |
| 50 | + }while(0); |
| 51 | + |
| 52 | + |
| 53 | +void usb_vbus( uint8_t state) |
| 54 | +{ |
| 55 | + if(state == 0) |
| 56 | + { |
| 57 | + gpio_write(&gpio_vbus, VBUS_OFF); |
| 58 | + } |
| 59 | + else |
| 60 | + { |
| 61 | + gpio_write(&gpio_vbus, VBUS_ON); |
| 62 | + } |
| 63 | + wait(0.2); |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +USBHALHost::USBHALHost() { |
| 68 | + gpio_t pin_vbus; |
| 69 | + instHost = this; |
| 70 | + HCD_HandleTypeDef *hhcd; |
| 71 | + USBHALHost_Private_t *HALPriv = new(USBHALHost_Private_t); |
| 72 | + memset(HALPriv, 0, sizeof(USBHALHost_Private_t)); |
| 73 | + memInit(); |
| 74 | + memset((void*)usb_hcca, 0, HCCA_SIZE); |
| 75 | + hhcd = (HCD_HandleTypeDef *)usb_hcca; |
| 76 | + hhcd->Instance = USB_OTG_HS; |
| 77 | + hhcd->pData = (void*)HALPriv; |
| 78 | + hhcd->Init.Host_channels = 11; |
| 79 | + /* for now failed with dma */ |
| 80 | + hhcd->Init.dma_enable = 0; |
| 81 | + hhcd->Init.speed = HCD_SPEED_HIGH; |
| 82 | + hhcd->Init.phy_itface = HCD_PHY_EMBEDDED; |
| 83 | + hhcd->Init.use_external_vbus = 1; |
| 84 | + HALPriv->inst = this; |
| 85 | + HALPriv->deviceConnected = &USBHALHost::deviceConnected; |
| 86 | + HALPriv->deviceDisconnected = &USBHALHost::deviceDisconnected; |
| 87 | + HALPriv->transferCompleted = &USBHALHost::transferCompleted; |
| 88 | + for (int i = 0; i < MAX_ENDPOINT; i++) { |
| 89 | + edBufAlloc[i] = false; |
| 90 | + HALPriv->addr[i]=(uint32_t)-1; |
| 91 | + } |
| 92 | + for (int i = 0; i < MAX_TD; i++) { |
| 93 | + tdBufAlloc[i] = false; |
| 94 | + } |
| 95 | + /* Configure USB HS GPIOs */ |
| 96 | + __HAL_RCC_GPIOB_CLK_ENABLE(); |
| 97 | + |
| 98 | + /*USB DM and DP */ |
| 99 | + pin_function(PB_14, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF12_OTG_HS_FS)); |
| 100 | + pin_function(PB_15, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF12_OTG_HS_FS)); |
| 101 | + /* Configure VBUS Pin */ |
| 102 | + gpio_init_in(&pin_vbus, PB_13); |
| 103 | + /* Configure POWER_SWITCH IO pin */ |
| 104 | + USB_VBUS_CONFIG; |
| 105 | + /* Enable USB HS Clocks */ |
| 106 | + __HAL_RCC_USB_OTG_HS_CLK_ENABLE(); |
| 107 | + |
| 108 | + /* Set USBFS Interrupt priority */ |
| 109 | + HAL_NVIC_SetPriority(USBHAL_IRQn, 5, 0); |
| 110 | + NVIC_SetVector(USBHAL_IRQn, (uint32_t)&_usbisr); |
| 111 | +} |
| 112 | +#endif |
0 commit comments