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