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_STM32L072CZ_H
Kojto 70:2c525a50f1b6 19 #define USBHAL_STM32L072CZ_H
Kojto 70:2c525a50f1b6 20
Kojto 70:2c525a50f1b6 21 #define USBHAL_IRQn USB_IRQn
Kojto 70:2c525a50f1b6 22
Kojto 70:2c525a50f1b6 23 /* must be multiple of 4 bytes */
Kojto 70:2c525a50f1b6 24 #define NB_ENDPOINT 8
Kojto 70:2c525a50f1b6 25 #define MAXTRANSFER_SIZE 0x200
Kojto 70:2c525a50f1b6 26 #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 27 #if (FIFO_USB_RAM_SIZE > 0x500)
Kojto 70:2c525a50f1b6 28 #error "FIFO dimensioning incorrect"
Kojto 70:2c525a50f1b6 29 #endif
Kojto 70:2c525a50f1b6 30
Kojto 70:2c525a50f1b6 31 typedef struct
Kojto 70:2c525a50f1b6 32 {
Kojto 70:2c525a50f1b6 33 USBHAL *inst;
Kojto 70:2c525a50f1b6 34 void (USBHAL::*bus_reset)(void);
Kojto 70:2c525a50f1b6 35 void (USBHAL::*sof)(int frame);
Kojto 70:2c525a50f1b6 36 void (USBHAL::*connect_change)(unsigned int connected);
Kojto 70:2c525a50f1b6 37 void (USBHAL::*suspend_change)(unsigned int suspended);
Kojto 70:2c525a50f1b6 38 void (USBHAL::*ep0_setup)(void);
Kojto 70:2c525a50f1b6 39 void (USBHAL::*ep0_in)(void);
Kojto 70:2c525a50f1b6 40 void (USBHAL::*ep0_out)(void);
Kojto 70:2c525a50f1b6 41 void (USBHAL::*ep0_read)(void);
Kojto 70:2c525a50f1b6 42 bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags);
Kojto 70:2c525a50f1b6 43 bool (USBHAL::*epCallback[6])(void);
Kojto 70:2c525a50f1b6 44 uint8_t epComplete[2*NB_ENDPOINT];
Kojto 70:2c525a50f1b6 45 /* memorize dummy buffer used for reception */
Kojto 70:2c525a50f1b6 46 uint32_t pBufRx[MAXTRANSFER_SIZE>>2];
Kojto 70:2c525a50f1b6 47 uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2];
Kojto 70:2c525a50f1b6 48 gpio_t usb_switch;
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 return 1024;
Kojto 70:2c525a50f1b6 54 }
Kojto 70:2c525a50f1b6 55
Kojto 70:2c525a50f1b6 56 void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state)
Kojto 70:2c525a50f1b6 57 {
Kojto 70:2c525a50f1b6 58 USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
Kojto 70:2c525a50f1b6 59 gpio_write(&(priv->usb_switch),state);
Kojto 70:2c525a50f1b6 60 }
Kojto 70:2c525a50f1b6 61
Kojto 70:2c525a50f1b6 62 void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
Kojto 70:2c525a50f1b6 63 {
Kojto 70:2c525a50f1b6 64 USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
Kojto 70:2c525a50f1b6 65 USBHAL *obj= priv->inst;
Kojto 70:2c525a50f1b6 66 uint32_t sofnum = (hpcd->Instance->FNR) & USB_FNR_FN;
Kojto 70:2c525a50f1b6 67 void (USBHAL::*func)(int frame) = priv->sof;
Kojto 70:2c525a50f1b6 68 (obj->*func)(sofnum);
Kojto 70:2c525a50f1b6 69 }
Kojto 70:2c525a50f1b6 70
Kojto 70:2c525a50f1b6 71 USBHAL * USBHAL::instance;
Kojto 70:2c525a50f1b6 72
Kojto 70:2c525a50f1b6 73 USBHAL::USBHAL(void)
Kojto 70:2c525a50f1b6 74 {
Kojto 70:2c525a50f1b6 75 /* init parameter */
Kojto 70:2c525a50f1b6 76 USBHAL_Private_t *HALPriv = new(USBHAL_Private_t);
Kojto 70:2c525a50f1b6 77 hpcd.Instance = USB;
Kojto 70:2c525a50f1b6 78 /* initialized Init to zero (constructor does not zero initialized the
Kojto 70:2c525a50f1b6 79 * area */
Kojto 70:2c525a50f1b6 80 /* initialized all field of init including 0 field */
Kojto 70:2c525a50f1b6 81 /* constructor does not fill with zero */
Kojto 70:2c525a50f1b6 82 memset(&hpcd.Init, 0, sizeof(hpcd.Init));
Kojto 70:2c525a50f1b6 83 hpcd.Init.dev_endpoints = NB_ENDPOINT;
Kojto 70:2c525a50f1b6 84 hpcd.Init.ep0_mps = MAX_PACKET_SIZE_EP0;
Kojto 70:2c525a50f1b6 85 hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
Kojto 70:2c525a50f1b6 86 hpcd.Init.Sof_enable = 1;
Kojto 70:2c525a50f1b6 87 hpcd.Init.speed = PCD_SPEED_FULL;
Kojto 70:2c525a50f1b6 88 /* pass instance for usage inside call back */
Kojto 70:2c525a50f1b6 89 HALPriv->inst = this;
Kojto 70:2c525a50f1b6 90 HALPriv->bus_reset = &USBHAL::busReset;
Kojto 70:2c525a50f1b6 91 HALPriv->suspend_change = &USBHAL::suspendStateChanged;
Kojto 70:2c525a50f1b6 92 HALPriv->connect_change = &USBHAL::connectStateChanged;
Kojto 70:2c525a50f1b6 93 HALPriv->sof = &USBHAL::SOF;
Kojto 70:2c525a50f1b6 94 HALPriv->ep0_setup = &USBHAL::EP0setupCallback;
Kojto 70:2c525a50f1b6 95 HALPriv->ep_realise = &USBHAL::realiseEndpoint;
Kojto 70:2c525a50f1b6 96 HALPriv->ep0_in = &USBHAL::EP0in;
Kojto 70:2c525a50f1b6 97 HALPriv->ep0_out = &USBHAL::EP0out;
Kojto 70:2c525a50f1b6 98 HALPriv->ep0_read = &USBHAL::EP0read;
Kojto 70:2c525a50f1b6 99 hpcd.pData = (void*)HALPriv;
Kojto 70:2c525a50f1b6 100 HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback;
Kojto 70:2c525a50f1b6 101 HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback;
Kojto 70:2c525a50f1b6 102 HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback;
Kojto 70:2c525a50f1b6 103 HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback;
Kojto 70:2c525a50f1b6 104 HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback;
Kojto 70:2c525a50f1b6 105 HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback;
Kojto 70:2c525a50f1b6 106 instance = this;
Kojto 70:2c525a50f1b6 107
Kojto 70:2c525a50f1b6 108 /* Configure USB DM pin. This is optional, and maintained only for user guidance. */
Kojto 70:2c525a50f1b6 109 __HAL_RCC_GPIOA_CLK_ENABLE();
Kojto 70:2c525a50f1b6 110 pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_USB));
Kojto 70:2c525a50f1b6 111 pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_USB));
Kojto 70:2c525a50f1b6 112
Kojto 70:2c525a50f1b6 113 /* Enable USB Clock */
Kojto 70:2c525a50f1b6 114 __HAL_RCC_USB_CLK_ENABLE();
Kojto 70:2c525a50f1b6 115
Kojto 70:2c525a50f1b6 116 /* Enable SYSCFG Clock */
Kojto 70:2c525a50f1b6 117 __HAL_RCC_SYSCFG_CLK_ENABLE();
Kojto 70:2c525a50f1b6 118 hpcd.State = HAL_PCD_STATE_RESET;
Kojto 70:2c525a50f1b6 119 HAL_PCD_Init(&hpcd);
Kojto 70:2c525a50f1b6 120
Kojto 70:2c525a50f1b6 121 /* hardcoded size of FIFO according definition*/
Kojto 70:2c525a50f1b6 122 HAL_PCDEx_PMAConfig(&hpcd , 0x00 , PCD_SNG_BUF, 0x30);
Kojto 70:2c525a50f1b6 123 HAL_PCDEx_PMAConfig(&hpcd , 0x80 , PCD_SNG_BUF, 0x70);
Kojto 70:2c525a50f1b6 124 HAL_PCDEx_PMAConfig(&hpcd , 0x3, PCD_DBL_BUF, 0x018000b0);
Kojto 70:2c525a50f1b6 125 HAL_PCDEx_PMAConfig(&hpcd , 0x83, PCD_SNG_BUF, 0xb0);
Kojto 70:2c525a50f1b6 126
Kojto 70:2c525a50f1b6 127 NVIC_SetVector(USBHAL_IRQn,(uint32_t)&_usbisr);
Kojto 70:2c525a50f1b6 128 NVIC_SetPriority(USBHAL_IRQn, 1);
Kojto 70:2c525a50f1b6 129 HAL_PCD_Start(&hpcd);
Kojto 70:2c525a50f1b6 130 }
Kojto 70:2c525a50f1b6 131 #endif