|
| 1 | +/* Copyright (c) 2016 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 USBHAL_STM32F103RB |
| 19 | +#define USBHAL_STM32F103RB |
| 20 | + |
| 21 | +#define USBHAL_IRQn USB_LP_CAN1_RX0_IRQn |
| 22 | + |
| 23 | + |
| 24 | +#define NB_ENDPOINT 8 |
| 25 | +/* must be multiple of 4 bytes */ |
| 26 | +#define MAXTRANSFER_SIZE 0x200 |
| 27 | +#define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3) |
| 28 | +#if (FIFO_USB_RAM_SIZE > 0x500) |
| 29 | +#error "FIFO dimensioning incorrect" |
| 30 | +#endif |
| 31 | + |
| 32 | +typedef struct |
| 33 | +{ |
| 34 | + USBHAL *inst; |
| 35 | + void (USBHAL::*bus_reset)(void); |
| 36 | + void (USBHAL::*sof)(int frame); |
| 37 | + void (USBHAL::*connect_change)(unsigned int connected); |
| 38 | + void (USBHAL::*suspend_change)(unsigned int suspended); |
| 39 | + void (USBHAL::*ep0_setup)(void); |
| 40 | + void (USBHAL::*ep0_in)(void); |
| 41 | + void (USBHAL::*ep0_out)(void); |
| 42 | + void (USBHAL::*ep0_read)(void); |
| 43 | + bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags); |
| 44 | + bool (USBHAL::*epCallback[2*NB_ENDPOINT-2])(void); |
| 45 | + uint8_t epComplete[8]; |
| 46 | + /* memorize dummy buffer used for reception */ |
| 47 | + uint32_t pBufRx[MAXTRANSFER_SIZE>>2]; |
| 48 | + uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2]; |
| 49 | + gpio_t usb_switch; |
| 50 | +}USBHAL_Private_t; |
| 51 | + |
| 52 | +void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state){ |
| 53 | + USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData)); |
| 54 | + gpio_write(&(priv->usb_switch),!state); |
| 55 | +} |
| 56 | + |
| 57 | +uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo) |
| 58 | +{ |
| 59 | + return 1024; |
| 60 | +} |
| 61 | +void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) { |
| 62 | + USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData)); |
| 63 | + USBHAL *obj= priv->inst; |
| 64 | + uint32_t sofnum = (hpcd->Instance->FNR) & USB_FNR_FN; |
| 65 | + void (USBHAL::*func)(int frame) = priv->sof; |
| 66 | + /* fix me call with same frame number */ |
| 67 | + (obj->*func)(sofnum); |
| 68 | +} |
| 69 | + |
| 70 | +USBHAL * USBHAL::instance; |
| 71 | + |
| 72 | +USBHAL::USBHAL(void) { |
| 73 | + /* init parameter */ |
| 74 | + USBHAL_Private_t *HALPriv = new(USBHAL_Private_t); |
| 75 | + /* initialized all field of init including 0 field */ |
| 76 | + /* constructor does not fill with zero */ |
| 77 | + hpcd.Instance = USB; |
| 78 | + /* initialized all field of init including 0 field */ |
| 79 | + /* constructor does not fill with zero */ |
| 80 | + memset(&hpcd.Init, 0, sizeof(hpcd.Init)); |
| 81 | + hpcd.Init.dev_endpoints = NB_ENDPOINT; |
| 82 | + hpcd.Init.ep0_mps = MAX_PACKET_SIZE_EP0; |
| 83 | + hpcd.Init.phy_itface = PCD_PHY_EMBEDDED; |
| 84 | + hpcd.Init.Sof_enable = 1; |
| 85 | + hpcd.Init.speed = PCD_SPEED_FULL; |
| 86 | + /* pass instance for usage inside call back */ |
| 87 | + HALPriv->inst = this; |
| 88 | + HALPriv->bus_reset = &USBHAL::busReset; |
| 89 | + HALPriv->suspend_change = &USBHAL::suspendStateChanged; |
| 90 | + HALPriv->connect_change = &USBHAL::connectStateChanged; |
| 91 | + HALPriv->sof = &USBHAL::SOF; |
| 92 | + HALPriv->ep0_setup = &USBHAL::EP0setupCallback; |
| 93 | + HALPriv->ep_realise = &USBHAL::realiseEndpoint; |
| 94 | + HALPriv->ep0_in = &USBHAL::EP0in; |
| 95 | + HALPriv->ep0_out = &USBHAL::EP0out; |
| 96 | + HALPriv->ep0_read = &USBHAL::EP0read; |
| 97 | + hpcd.pData = (void*)HALPriv; |
| 98 | + HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback; |
| 99 | + HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback; |
| 100 | + HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback; |
| 101 | + HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback; |
| 102 | + HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback; |
| 103 | + HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback; |
| 104 | + instance = this; |
| 105 | + |
| 106 | + |
| 107 | + /* Configure USB VBUS GPIO */ |
| 108 | + gpio_init_out(&HALPriv->usb_switch,PB_14); |
| 109 | + gpio_mode(&HALPriv->usb_switch,OpenDrain); |
| 110 | + /* Configure USB FS GPIOs */ |
| 111 | + |
| 112 | + /* Configure DM DP Pins */ |
| 113 | + /* - USB-DP (D+ of the USB connector) <======> PA12 (Nucleo board) |
| 114 | + Make sure to connect a 1.5KOhm pull up to USB-DP PA12 pin (permanent pull-up) |
| 115 | + - USB-DM (D- of the USB connector) <======> PA11 (Nucleo board) */ |
| 116 | + |
| 117 | + pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_MODE_AF_INPUT)); |
| 118 | + pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_MODE_AF_INPUT)); |
| 119 | + |
| 120 | + __HAL_RCC_USB_CLK_ENABLE(); |
| 121 | + |
| 122 | + hpcd.State = HAL_PCD_STATE_RESET; |
| 123 | + |
| 124 | + HAL_PCD_Init(&hpcd); |
| 125 | + /* hardcoded size of FIFO according definition*/ |
| 126 | + HAL_PCDEx_PMAConfig(&hpcd , 0x00 , PCD_SNG_BUF, 0x30); |
| 127 | + HAL_PCDEx_PMAConfig(&hpcd , 0x80 , PCD_SNG_BUF, 0x70); |
| 128 | + HAL_PCDEx_PMAConfig(&hpcd , 0x01 , PCD_SNG_BUF, 0x90); |
| 129 | + HAL_PCDEx_PMAConfig(&hpcd , 0x81 , PCD_SNG_BUF, 0xb0); |
| 130 | +#if 0 |
| 131 | + HAL_PCDEx_PMAConfig(&hpcd , 0x2, PCD_DBL_BUF, 0x018000b0); |
| 132 | +#else |
| 133 | + HAL_PCDEx_PMAConfig(&hpcd , 0x2, PCD_SNG_BUF, 0x100); |
| 134 | +#endif |
| 135 | + HAL_PCDEx_PMAConfig(&hpcd , 0x82, PCD_SNG_BUF, 0x120); |
| 136 | + |
| 137 | + NVIC_SetVector(USBHAL_IRQn,(uint32_t)&_usbisr); |
| 138 | + NVIC_SetPriority( USBHAL_IRQn, 1); |
| 139 | + |
| 140 | + HAL_PCD_Start(&hpcd); |
| 141 | +} |
| 142 | + |
| 143 | +#endif |
0 commit comments