Point Labs / USBDevice

Dependents:   Example_WatchDog_Timer

Committer:
rlanders73
Date:
Fri Nov 23 18:05:40 2018 +0000
Revision:
74:3d21c913753e
Added new target and reorged some of the files in the directory

Who changed what in which revision?

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