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 27 12:14:04 2017 +0100
Revision:
71:53949e6131f6
Update libraries

Fixes the previous commmit, as some devices were not copied. USBDevice contains
now targets directory with all targets implementations

Who changed what in which revision?

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