|
| 1 | +/* Copyright (c) 2017 mbed.org, MIT License |
| 2 | +* |
| 3 | +* Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| 4 | +* and associated documentation files (the "Software"), to deal in the Software without |
| 5 | +* restriction, including without limitation the rights to use, copy, modify, merge, publish, |
| 6 | +* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
| 7 | +* Software is furnished to do so, subject to the following conditions: |
| 8 | +* |
| 9 | +* The above copyright notice and this permission notice shall be included in all copies or |
| 10 | +* substantial portions of the Software. |
| 11 | +* |
| 12 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| 13 | +* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 14 | +* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 15 | +* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 16 | +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 17 | +*/ |
| 18 | +#ifndef USBHALHOST_DISCO_L475VG_IOT01A |
| 19 | +#define USBHALHOST_DISCO_L475VG_IOT01A |
| 20 | + |
| 21 | +#define USBHAL_IRQn OTG_FS_IRQn |
| 22 | + |
| 23 | +#define HCCA_SIZE sizeof(HCD_HandleTypeDef) |
| 24 | +#define ED_SIZE sizeof(HCED) |
| 25 | +#define TD_SIZE sizeof(HCTD) |
| 26 | + |
| 27 | +#define TOTAL_SIZE (HCCA_SIZE + (MAX_ENDPOINT*ED_SIZE) + (MAX_TD*TD_SIZE)) |
| 28 | + |
| 29 | +/* STM device FS have 11 channels (definition is for 60 channels) */ |
| 30 | +static volatile uint8_t usb_buf[TOTAL_SIZE]; |
| 31 | + |
| 32 | +typedef struct { |
| 33 | + /* store the request ongoing on each endpoit */ |
| 34 | + /* 1st field of structure avoid giving knowledge of all structure to |
| 35 | + * endpoint */ |
| 36 | + volatile uint32_t addr[MAX_ENDPOINT]; |
| 37 | + USBHALHost *inst; |
| 38 | + void (USBHALHost::*deviceConnected)(int hub, int port, bool lowSpeed, USBHostHub * hub_parent); |
| 39 | + void (USBHALHost::*deviceDisconnected)(int hub, int port, USBHostHub * hub_parent, volatile uint32_t addr); |
| 40 | + void (USBHALHost::*transferCompleted)(volatile uint32_t addr); |
| 41 | +} USBHALHost_Private_t; |
| 42 | + |
| 43 | +static gpio_t gpio_powerpin; |
| 44 | + |
| 45 | +#define USB_POWER_OFF 1 |
| 46 | +#define USB_POWER_ON 0 |
| 47 | +#define USB_POWERPIN_CONFIG {gpio_init_out_ex(&gpio_powerpin, PD_12, USB_POWER_OFF);} |
| 48 | + |
| 49 | + |
| 50 | +void usb_vbus( uint8_t state) |
| 51 | +{ |
| 52 | + if (state == 0) { |
| 53 | + gpio_write(&gpio_powerpin, USB_POWER_OFF); |
| 54 | + } else { |
| 55 | + gpio_write(&gpio_powerpin, USB_POWER_ON); |
| 56 | + } |
| 57 | + wait(0.2); |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +USBHALHost::USBHALHost() |
| 62 | +{ |
| 63 | + instHost = this; |
| 64 | + HCD_HandleTypeDef *hhcd; |
| 65 | + USBHALHost_Private_t *HALPriv = new(USBHALHost_Private_t); |
| 66 | + |
| 67 | + memset(HALPriv, 0, sizeof(USBHALHost_Private_t)); |
| 68 | + memInit(); |
| 69 | + memset((void*)usb_hcca, 0, HCCA_SIZE); |
| 70 | + |
| 71 | + hhcd = (HCD_HandleTypeDef *)usb_hcca; |
| 72 | + hhcd->Instance = USB_OTG_FS; |
| 73 | + hhcd->pData = (void*)HALPriv; |
| 74 | + hhcd->Init.Host_channels = 11; |
| 75 | + |
| 76 | + /* for now failed with dma */ |
| 77 | + hhcd->Init.dma_enable = 0; |
| 78 | + hhcd->Init.speed = HCD_SPEED_FULL; |
| 79 | + hhcd->Init.phy_itface = HCD_PHY_EMBEDDED; |
| 80 | + hhcd->Init.use_external_vbus = 1; |
| 81 | + |
| 82 | + HALPriv->inst = this; |
| 83 | + HALPriv->deviceConnected = &USBHALHost::deviceConnected; |
| 84 | + HALPriv->deviceDisconnected = &USBHALHost::deviceDisconnected; |
| 85 | + HALPriv->transferCompleted = &USBHALHost::transferCompleted; |
| 86 | + |
| 87 | + for (int i = 0; i < MAX_ENDPOINT; i++) { |
| 88 | + edBufAlloc[i] = false; |
| 89 | + HALPriv->addr[i]=(uint32_t)-1; |
| 90 | + } |
| 91 | + |
| 92 | + for (int i = 0; i < MAX_TD; i++) { |
| 93 | + tdBufAlloc[i] = false; |
| 94 | + } |
| 95 | + |
| 96 | + __HAL_RCC_PWR_CLK_ENABLE(); |
| 97 | + |
| 98 | +#ifdef TARGET_STM32L4 |
| 99 | + HAL_PWREx_EnableVddUSB(); |
| 100 | +#endif |
| 101 | + |
| 102 | + /* Configure USB GPIOs */ |
| 103 | + __HAL_RCC_GPIOA_CLK_ENABLE(); |
| 104 | + pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); // DM pin |
| 105 | + pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); // DP pin |
| 106 | + pin_function(PA_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); // VBUS pin |
| 107 | + |
| 108 | + /* Configure USB POWER pin */ |
| 109 | + USB_POWERPIN_CONFIG; |
| 110 | + |
| 111 | + /* Enable USB FS Clocks */ |
| 112 | + __HAL_RCC_SYSCFG_CLK_ENABLE(); |
| 113 | + __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); |
| 114 | + |
| 115 | + /* Set USBFS Interrupt priority */ |
| 116 | + HAL_NVIC_SetPriority(USBHAL_IRQn, 5, 0); |
| 117 | + NVIC_SetVector(USBHAL_IRQn, (uint32_t)&_usbisr); |
| 118 | +} |
| 119 | + |
| 120 | +#endif // USBHALHOST_DISCO_L475VG_IOT01A |
0 commit comments