USB device stack

Dependents:   mbed-mX-USB-TEST1 USBMSD_SD_HID_HelloWorld HidTest MIDI_usb_bridge ... more

Legacy Warning

This is an mbed 2 library. To learn more about mbed OS 5, visit the docs.

Pull requests against this repository are no longer supported. Please raise against mbed OS 5 as documented above.

Committer:
Kojto
Date:
Thu Jul 20 10:14:36 2017 +0100
Revision:
70:2c525a50f1b6
Update libraries (ed9d1da)

Who changed what in which revision?

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