USBDevice with Nucleo 32L476RG support

Dependents:   ObCP_ENSMM_V2020_Test_Accelero

Committer:
jimbaud
Date:
Wed Sep 30 11:26:16 2020 +0000
Revision:
72:14a22343c610
USBDevice with Nucleo 32L476RG support

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jimbaud 72:14a22343c610 1 /* Copyright (c) 2016 mbed.org, MIT License
jimbaud 72:14a22343c610 2 *
jimbaud 72:14a22343c610 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
jimbaud 72:14a22343c610 4 * and associated documentation files (the "Software"), to deal in the Software without
jimbaud 72:14a22343c610 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
jimbaud 72:14a22343c610 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
jimbaud 72:14a22343c610 7 * Software is furnished to do so, subject to the following conditions:
jimbaud 72:14a22343c610 8 *
jimbaud 72:14a22343c610 9 * The above copyright notice and this permission notice shall be included in all copies or
jimbaud 72:14a22343c610 10 * substantial portions of the Software.
jimbaud 72:14a22343c610 11 *
jimbaud 72:14a22343c610 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
jimbaud 72:14a22343c610 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
jimbaud 72:14a22343c610 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
jimbaud 72:14a22343c610 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jimbaud 72:14a22343c610 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
jimbaud 72:14a22343c610 17 */
jimbaud 72:14a22343c610 18 #ifndef USBHAL_STM32L476RG
jimbaud 72:14a22343c610 19 #define USBHAL_STM32L476RG
jimbaud 72:14a22343c610 20
jimbaud 72:14a22343c610 21 #define USBHAL_IRQn OTG_FS_IRQn
jimbaud 72:14a22343c610 22
jimbaud 72:14a22343c610 23
jimbaud 72:14a22343c610 24 #define NB_ENDPOINT 4
jimbaud 72:14a22343c610 25 /* must be multiple of 4 bytes */
jimbaud 72:14a22343c610 26 #define MAXTRANSFER_SIZE 0x200
jimbaud 72:14a22343c610 27 #define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3)
jimbaud 72:14a22343c610 28 #if (FIFO_USB_RAM_SIZE > 0x500)
jimbaud 72:14a22343c610 29 #error "FIFO dimensioning incorrect"
jimbaud 72:14a22343c610 30 #endif
jimbaud 72:14a22343c610 31
jimbaud 72:14a22343c610 32 typedef struct
jimbaud 72:14a22343c610 33 {
jimbaud 72:14a22343c610 34 USBHAL *inst;
jimbaud 72:14a22343c610 35 void (USBHAL::*bus_reset)(void);
jimbaud 72:14a22343c610 36 void (USBHAL::*sof)(int frame);
jimbaud 72:14a22343c610 37 void (USBHAL::*connect_change)(unsigned int connected);
jimbaud 72:14a22343c610 38 void (USBHAL::*suspend_change)(unsigned int suspended);
jimbaud 72:14a22343c610 39 void (USBHAL::*ep0_setup)(void);
jimbaud 72:14a22343c610 40 void (USBHAL::*ep0_in)(void);
jimbaud 72:14a22343c610 41 void (USBHAL::*ep0_out)(void);
jimbaud 72:14a22343c610 42 void (USBHAL::*ep0_read)(void);
jimbaud 72:14a22343c610 43 bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags);
jimbaud 72:14a22343c610 44 bool (USBHAL::*epCallback[2*NB_ENDPOINT-2])(void);
jimbaud 72:14a22343c610 45 uint8_t epComplete[8];
jimbaud 72:14a22343c610 46 /* memorize dummy buffer used for reception */
jimbaud 72:14a22343c610 47 uint32_t pBufRx[MAXTRANSFER_SIZE>>2];
jimbaud 72:14a22343c610 48 uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2];
jimbaud 72:14a22343c610 49 }USBHAL_Private_t;
jimbaud 72:14a22343c610 50
jimbaud 72:14a22343c610 51 uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo)
jimbaud 72:14a22343c610 52 {
jimbaud 72:14a22343c610 53 uint32_t len;
jimbaud 72:14a22343c610 54 if (fifo == 0) len = hpcd->Instance->DIEPTXF0_HNPTXFSIZ>>16;
jimbaud 72:14a22343c610 55 else
jimbaud 72:14a22343c610 56 len = hpcd->Instance->DIEPTXF[fifo - 1] >> 16;
jimbaud 72:14a22343c610 57 return len*4;
jimbaud 72:14a22343c610 58 }
jimbaud 72:14a22343c610 59 void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) {
jimbaud 72:14a22343c610 60 USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
jimbaud 72:14a22343c610 61 USBHAL *obj= priv->inst;
jimbaud 72:14a22343c610 62 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
jimbaud 72:14a22343c610 63 uint32_t sofnum = (USBx_DEVICE->DSTS & USB_OTG_DSTS_FNSOF) >> 8;
jimbaud 72:14a22343c610 64 void (USBHAL::*func)(int frame) = priv->sof;
jimbaud 72:14a22343c610 65 /* fix me call with same frame number */
jimbaud 72:14a22343c610 66 (obj->*func)(sofnum);
jimbaud 72:14a22343c610 67 }
jimbaud 72:14a22343c610 68
jimbaud 72:14a22343c610 69 USBHAL * USBHAL::instance;
jimbaud 72:14a22343c610 70
jimbaud 72:14a22343c610 71 USBHAL::USBHAL(void) {
jimbaud 72:14a22343c610 72 /* init parameter */
jimbaud 72:14a22343c610 73 USBHAL_Private_t *HALPriv = new(USBHAL_Private_t);
jimbaud 72:14a22343c610 74 /* initialized all field of init including 0 field */
jimbaud 72:14a22343c610 75 /* constructor does not fill with zero */
jimbaud 72:14a22343c610 76 hpcd.Instance = USB_OTG_FS;
jimbaud 72:14a22343c610 77 /* initialized all field of init including 0 field */
jimbaud 72:14a22343c610 78 /* constructor does not fill with zero */
jimbaud 72:14a22343c610 79 memset(&hpcd.Init, 0, sizeof(hpcd.Init));
jimbaud 72:14a22343c610 80 hpcd.Init.dev_endpoints = NB_ENDPOINT;
jimbaud 72:14a22343c610 81 hpcd.Init.ep0_mps = MAX_PACKET_SIZE_EP0;
jimbaud 72:14a22343c610 82 hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
jimbaud 72:14a22343c610 83 hpcd.Init.Sof_enable = 1;
jimbaud 72:14a22343c610 84 hpcd.Init.speed = PCD_SPEED_FULL;
jimbaud 72:14a22343c610 85 /* pass instance for usage inside call back */
jimbaud 72:14a22343c610 86 HALPriv->inst = this;
jimbaud 72:14a22343c610 87 HALPriv->bus_reset = &USBHAL::busReset;
jimbaud 72:14a22343c610 88 HALPriv->suspend_change = &USBHAL::suspendStateChanged;
jimbaud 72:14a22343c610 89 HALPriv->connect_change = &USBHAL::connectStateChanged;
jimbaud 72:14a22343c610 90 HALPriv->sof = &USBHAL::SOF;
jimbaud 72:14a22343c610 91 HALPriv->ep0_setup = &USBHAL::EP0setupCallback;
jimbaud 72:14a22343c610 92 HALPriv->ep_realise = &USBHAL::realiseEndpoint;
jimbaud 72:14a22343c610 93 HALPriv->ep0_in = &USBHAL::EP0in;
jimbaud 72:14a22343c610 94 HALPriv->ep0_out = &USBHAL::EP0out;
jimbaud 72:14a22343c610 95 HALPriv->ep0_read = &USBHAL::EP0read;
jimbaud 72:14a22343c610 96 hpcd.pData = (void*)HALPriv;
jimbaud 72:14a22343c610 97 HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback;
jimbaud 72:14a22343c610 98 HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback;
jimbaud 72:14a22343c610 99 HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback;
jimbaud 72:14a22343c610 100 HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback;
jimbaud 72:14a22343c610 101 HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback;
jimbaud 72:14a22343c610 102 HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback;
jimbaud 72:14a22343c610 103 instance = this;
jimbaud 72:14a22343c610 104
jimbaud 72:14a22343c610 105 __HAL_RCC_PWR_CLK_ENABLE();
jimbaud 72:14a22343c610 106
jimbaud 72:14a22343c610 107 HAL_PWREx_EnableVddUSB();
jimbaud 72:14a22343c610 108 /* Configure USB VBUS GPIO */
jimbaud 72:14a22343c610 109 __HAL_RCC_GPIOC_CLK_ENABLE();
jimbaud 72:14a22343c610 110
jimbaud 72:14a22343c610 111 /* Configure USB FS GPIOs */
jimbaud 72:14a22343c610 112 __HAL_RCC_GPIOA_CLK_ENABLE();
jimbaud 72:14a22343c610 113
jimbaud 72:14a22343c610 114 /* Configure DM DP Pins */
jimbaud 72:14a22343c610 115 pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
jimbaud 72:14a22343c610 116 pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
jimbaud 72:14a22343c610 117
jimbaud 72:14a22343c610 118 /* Configure VBUS Pin */
jimbaud 72:14a22343c610 119 pin_function(PC_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS));
jimbaud 72:14a22343c610 120
jimbaud 72:14a22343c610 121 __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
jimbaud 72:14a22343c610 122
jimbaud 72:14a22343c610 123 hpcd.State = HAL_PCD_STATE_RESET;
jimbaud 72:14a22343c610 124
jimbaud 72:14a22343c610 125 HAL_PCD_Init(&hpcd);
jimbaud 72:14a22343c610 126 /* 1.25kbytes */
jimbaud 72:14a22343c610 127 /* min value 16 (= 16 x 4 bytes) */
jimbaud 72:14a22343c610 128 /* max value 256 (= 1K bytes ) */
jimbaud 72:14a22343c610 129 /* maximum sum is 0x140 */
jimbaud 72:14a22343c610 130 HAL_PCDEx_SetRxFiFo(&hpcd, (MAXTRANSFER_SIZE/4));
jimbaud 72:14a22343c610 131 /* bulk/int 64 bytes in FS */
jimbaud 72:14a22343c610 132 HAL_PCDEx_SetTxFiFo(&hpcd, 0, (MAX_PACKET_SIZE_EP0/4)+1);
jimbaud 72:14a22343c610 133 /* bulk/int bytes in FS */
jimbaud 72:14a22343c610 134 HAL_PCDEx_SetTxFiFo(&hpcd, 1, (MAX_PACKET_SIZE_EP1/4)+1);
jimbaud 72:14a22343c610 135 HAL_PCDEx_SetTxFiFo(&hpcd, 2, (MAX_PACKET_SIZE_EP2/4));
jimbaud 72:14a22343c610 136 /* ISOchronous */
jimbaud 72:14a22343c610 137 HAL_PCDEx_SetTxFiFo(&hpcd, 3, (MAX_PACKET_SIZE_EP3/4));
jimbaud 72:14a22343c610 138
jimbaud 72:14a22343c610 139 NVIC_SetVector(USBHAL_IRQn,(uint32_t)&_usbisr);
jimbaud 72:14a22343c610 140 NVIC_SetPriority( USBHAL_IRQn, 1);
jimbaud 72:14a22343c610 141
jimbaud 72:14a22343c610 142 HAL_PCD_Start(&hpcd);
jimbaud 72:14a22343c610 143 }
jimbaud 72:14a22343c610 144
jimbaud 72:14a22343c610 145 #endif