A copy of the mbed USBDevice with USBSerial library
Dependents: STM32L0_LoRa Smartage STM32L0_LoRa Turtle_RadioShuttle
targets/TARGET_STM/USBHAL_IP_DEVICE.h@8:961423d1da74, 2019-02-24 (annotated)
- Committer:
- Helmut Tschemernjak
- Date:
- Sun Feb 24 14:52:33 2019 +0100
- Revision:
- 8:961423d1da74
- Parent:
- 1:7d1a127a1d2b
Added sleep manager support to avoids sleeps while a USB CDC
connection is active
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Helmut64 | 0:a3ea811f80f2 | 1 | /* Copyright (c) 2017 mbed.org, MIT License |
Helmut64 | 0:a3ea811f80f2 | 2 | * |
Helmut64 | 0:a3ea811f80f2 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Helmut64 | 0:a3ea811f80f2 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
Helmut64 | 0:a3ea811f80f2 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
Helmut64 | 0:a3ea811f80f2 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
Helmut64 | 0:a3ea811f80f2 | 7 | * Software is furnished to do so, subject to the following conditions: |
Helmut64 | 0:a3ea811f80f2 | 8 | * |
Helmut64 | 0:a3ea811f80f2 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
Helmut64 | 0:a3ea811f80f2 | 10 | * substantial portions of the Software. |
Helmut64 | 0:a3ea811f80f2 | 11 | * |
Helmut64 | 0:a3ea811f80f2 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Helmut64 | 0:a3ea811f80f2 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Helmut64 | 0:a3ea811f80f2 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Helmut64 | 0:a3ea811f80f2 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Helmut64 | 0:a3ea811f80f2 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Helmut64 | 0:a3ea811f80f2 | 17 | */ |
Helmut64 | 0:a3ea811f80f2 | 18 | #ifndef USBHAL_IP_DEVICE_H |
Helmut64 | 0:a3ea811f80f2 | 19 | #define USBHAL_IP_DEVICE_H |
Helmut64 | 0:a3ea811f80f2 | 20 | |
Helmut64 | 0:a3ea811f80f2 | 21 | #if defined(TARGET_NUCLEO_F303ZE) |
Helmut64 | 0:a3ea811f80f2 | 22 | #define USBHAL_IRQn USB_LP_CAN_RX0_IRQn |
Helmut64 | 0:a3ea811f80f2 | 23 | |
Helmut64 | 0:a3ea811f80f2 | 24 | #elif defined(TARGET_NUCLEO_F103RB) |
Helmut64 | 0:a3ea811f80f2 | 25 | #define USBHAL_IRQn USB_LP_CAN1_RX0_IRQn |
Helmut64 | 0:a3ea811f80f2 | 26 | |
Helmut64 | 0:a3ea811f80f2 | 27 | #elif defined(TARGET_DISCO_L072CZ_LRWAN1) || \ |
Helmut Tschemernjak | 1:7d1a127a1d2b | 28 | defined(TARGET_DISCO_L053C8) || \ |
Helmut Tschemernjak | 1:7d1a127a1d2b | 29 | defined(TARGET_STM32L4) |
Helmut64 | 0:a3ea811f80f2 | 30 | #define USBHAL_IRQn USB_IRQn |
Helmut64 | 0:a3ea811f80f2 | 31 | |
Helmut64 | 0:a3ea811f80f2 | 32 | #else |
Helmut64 | 0:a3ea811f80f2 | 33 | #error "USB IRQ is not configured !" |
Helmut64 | 0:a3ea811f80f2 | 34 | #endif |
Helmut64 | 0:a3ea811f80f2 | 35 | |
Helmut Tschemernjak | 1:7d1a127a1d2b | 36 | #if defined(TARGET_STM32L4) || defined(TARGET_DISCO_L072CZ_LRWAN1) |
Helmut Tschemernjak | 1:7d1a127a1d2b | 37 | extern "C" { void pin_function(PinName pin, int function); } |
Helmut Tschemernjak | 1:7d1a127a1d2b | 38 | #endif |
Helmut Tschemernjak | 1:7d1a127a1d2b | 39 | |
Helmut Tschemernjak | 1:7d1a127a1d2b | 40 | |
Helmut64 | 0:a3ea811f80f2 | 41 | #define NB_ENDPOINT 8 // Must be a multiple of 4 bytes |
Helmut64 | 0:a3ea811f80f2 | 42 | |
Helmut64 | 0:a3ea811f80f2 | 43 | #define MAXTRANSFER_SIZE 0x200 |
Helmut64 | 0:a3ea811f80f2 | 44 | |
Helmut64 | 0:a3ea811f80f2 | 45 | #define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE + MAX_PACKET_SIZE_EP0 + MAX_PACKET_SIZE_EP1 + MAX_PACKET_SIZE_EP2 + MAX_PACKET_SIZE_EP3) |
Helmut64 | 0:a3ea811f80f2 | 46 | |
Helmut64 | 0:a3ea811f80f2 | 47 | #if (FIFO_USB_RAM_SIZE > 0x500) |
Helmut64 | 0:a3ea811f80f2 | 48 | #error "FIFO dimensioning incorrect" |
Helmut64 | 0:a3ea811f80f2 | 49 | #endif |
Helmut64 | 0:a3ea811f80f2 | 50 | |
Helmut64 | 0:a3ea811f80f2 | 51 | typedef struct |
Helmut64 | 0:a3ea811f80f2 | 52 | { |
Helmut64 | 0:a3ea811f80f2 | 53 | USBHAL *inst; |
Helmut64 | 0:a3ea811f80f2 | 54 | |
Helmut64 | 0:a3ea811f80f2 | 55 | void (USBHAL::*bus_reset)(void); |
Helmut64 | 0:a3ea811f80f2 | 56 | void (USBHAL::*sof)(int frame); |
Helmut64 | 0:a3ea811f80f2 | 57 | void (USBHAL::*connect_change)(unsigned int connected); |
Helmut64 | 0:a3ea811f80f2 | 58 | void (USBHAL::*suspend_change)(unsigned int suspended); |
Helmut64 | 0:a3ea811f80f2 | 59 | void (USBHAL::*ep0_setup)(void); |
Helmut64 | 0:a3ea811f80f2 | 60 | void (USBHAL::*ep0_in)(void); |
Helmut64 | 0:a3ea811f80f2 | 61 | void (USBHAL::*ep0_out)(void); |
Helmut64 | 0:a3ea811f80f2 | 62 | void (USBHAL::*ep0_read)(void); |
Helmut64 | 0:a3ea811f80f2 | 63 | bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags); |
Helmut64 | 0:a3ea811f80f2 | 64 | bool (USBHAL::*epCallback[(2 * NB_ENDPOINT) - 2])(void); |
Helmut64 | 0:a3ea811f80f2 | 65 | |
Helmut64 | 0:a3ea811f80f2 | 66 | uint8_t epComplete[2 * NB_ENDPOINT]; |
Helmut64 | 0:a3ea811f80f2 | 67 | |
Helmut64 | 0:a3ea811f80f2 | 68 | /* Memorize dummy buffer used for reception */ |
Helmut64 | 0:a3ea811f80f2 | 69 | uint32_t pBufRx[MAXTRANSFER_SIZE >> 2]; |
Helmut64 | 0:a3ea811f80f2 | 70 | uint32_t pBufRx0[MAX_PACKET_SIZE_EP0 >> 2]; |
Helmut64 | 0:a3ea811f80f2 | 71 | |
Helmut64 | 0:a3ea811f80f2 | 72 | gpio_t usb_switch; |
Helmut64 | 0:a3ea811f80f2 | 73 | } USBHAL_Private_t; |
Helmut64 | 0:a3ea811f80f2 | 74 | |
Helmut64 | 0:a3ea811f80f2 | 75 | uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo) |
Helmut64 | 0:a3ea811f80f2 | 76 | { |
Helmut64 | 0:a3ea811f80f2 | 77 | return 1024; |
Helmut64 | 0:a3ea811f80f2 | 78 | } |
Helmut64 | 0:a3ea811f80f2 | 79 | |
Helmut64 | 0:a3ea811f80f2 | 80 | void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) |
Helmut64 | 0:a3ea811f80f2 | 81 | { |
Helmut64 | 0:a3ea811f80f2 | 82 | USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData)); |
Helmut64 | 0:a3ea811f80f2 | 83 | USBHAL *obj= priv->inst; |
Helmut64 | 0:a3ea811f80f2 | 84 | uint32_t sofnum = (hpcd->Instance->FNR) & USB_FNR_FN; |
Helmut64 | 0:a3ea811f80f2 | 85 | void (USBHAL::*func)(int frame) = priv->sof; |
Helmut64 | 0:a3ea811f80f2 | 86 | (obj->*func)(sofnum); |
Helmut64 | 0:a3ea811f80f2 | 87 | } |
Helmut64 | 0:a3ea811f80f2 | 88 | |
Helmut64 | 0:a3ea811f80f2 | 89 | void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state) |
Helmut64 | 0:a3ea811f80f2 | 90 | { |
Helmut64 | 0:a3ea811f80f2 | 91 | USBHAL_Private_t *priv = ((USBHAL_Private_t *)(hpcd->pData)); |
Helmut64 | 0:a3ea811f80f2 | 92 | #if defined(TARGET_NUCLEO_F103RB) |
Helmut64 | 0:a3ea811f80f2 | 93 | gpio_write(&(priv->usb_switch), !state); |
Helmut64 | 0:a3ea811f80f2 | 94 | #else |
Helmut64 | 0:a3ea811f80f2 | 95 | gpio_write(&(priv->usb_switch), state); |
Helmut64 | 0:a3ea811f80f2 | 96 | #endif |
Helmut64 | 0:a3ea811f80f2 | 97 | } |
Helmut64 | 0:a3ea811f80f2 | 98 | |
Helmut64 | 0:a3ea811f80f2 | 99 | USBHAL *USBHAL::instance; |
Helmut64 | 0:a3ea811f80f2 | 100 | |
Helmut64 | 0:a3ea811f80f2 | 101 | USBHAL::USBHAL(void) { |
Helmut64 | 0:a3ea811f80f2 | 102 | USBHAL_Private_t *HALPriv = new(USBHAL_Private_t); |
Helmut64 | 0:a3ea811f80f2 | 103 | |
Helmut64 | 0:a3ea811f80f2 | 104 | hpcd.Instance = USB; |
Helmut64 | 0:a3ea811f80f2 | 105 | |
Helmut64 | 0:a3ea811f80f2 | 106 | memset(&hpcd.Init, 0, sizeof(hpcd.Init)); |
Helmut64 | 0:a3ea811f80f2 | 107 | hpcd.Init.dev_endpoints = NB_ENDPOINT; |
Helmut64 | 0:a3ea811f80f2 | 108 | hpcd.Init.ep0_mps = MAX_PACKET_SIZE_EP0; |
Helmut64 | 0:a3ea811f80f2 | 109 | hpcd.Init.phy_itface = PCD_PHY_EMBEDDED; |
Helmut64 | 0:a3ea811f80f2 | 110 | hpcd.Init.Sof_enable = 1; |
Helmut64 | 0:a3ea811f80f2 | 111 | hpcd.Init.speed = PCD_SPEED_FULL; |
Helmut64 | 0:a3ea811f80f2 | 112 | |
Helmut64 | 0:a3ea811f80f2 | 113 | // Pass instance for usage inside call back |
Helmut64 | 0:a3ea811f80f2 | 114 | HALPriv->inst = this; |
Helmut64 | 0:a3ea811f80f2 | 115 | HALPriv->bus_reset = &USBHAL::busReset; |
Helmut64 | 0:a3ea811f80f2 | 116 | HALPriv->suspend_change = &USBHAL::suspendStateChanged; |
Helmut64 | 0:a3ea811f80f2 | 117 | HALPriv->connect_change = &USBHAL::connectStateChanged; |
Helmut64 | 0:a3ea811f80f2 | 118 | HALPriv->sof = &USBHAL::SOF; |
Helmut64 | 0:a3ea811f80f2 | 119 | HALPriv->ep0_setup = &USBHAL::EP0setupCallback; |
Helmut64 | 0:a3ea811f80f2 | 120 | HALPriv->ep_realise = &USBHAL::realiseEndpoint; |
Helmut64 | 0:a3ea811f80f2 | 121 | HALPriv->ep0_in = &USBHAL::EP0in; |
Helmut64 | 0:a3ea811f80f2 | 122 | HALPriv->ep0_out = &USBHAL::EP0out; |
Helmut64 | 0:a3ea811f80f2 | 123 | HALPriv->ep0_read = &USBHAL::EP0read; |
Helmut64 | 0:a3ea811f80f2 | 124 | HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback; |
Helmut64 | 0:a3ea811f80f2 | 125 | HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback; |
Helmut64 | 0:a3ea811f80f2 | 126 | HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback; |
Helmut64 | 0:a3ea811f80f2 | 127 | HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback; |
Helmut64 | 0:a3ea811f80f2 | 128 | HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback; |
Helmut64 | 0:a3ea811f80f2 | 129 | HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback; |
Helmut64 | 0:a3ea811f80f2 | 130 | instance = this; |
Helmut64 | 0:a3ea811f80f2 | 131 | |
Helmut64 | 0:a3ea811f80f2 | 132 | // Configure USB pins and other clocks |
Helmut64 | 0:a3ea811f80f2 | 133 | #if defined(TARGET_NUCLEO_F303ZE) |
Helmut64 | 0:a3ea811f80f2 | 134 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
Helmut64 | 0:a3ea811f80f2 | 135 | pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF14_USB)); // DM |
Helmut64 | 0:a3ea811f80f2 | 136 | pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF14_USB)); // DP |
Helmut64 | 0:a3ea811f80f2 | 137 | __HAL_RCC_GPIOG_CLK_ENABLE(); |
Helmut64 | 0:a3ea811f80f2 | 138 | gpio_init_out(&HALPriv->usb_switch, PG_6); |
Helmut64 | 0:a3ea811f80f2 | 139 | |
Helmut64 | 0:a3ea811f80f2 | 140 | #elif defined(TARGET_NUCLEO_F103RB) |
Helmut64 | 0:a3ea811f80f2 | 141 | // Make sure to connect a 1.5K resistor between USB-DP PA12 pin and +3.3V |
Helmut64 | 0:a3ea811f80f2 | 142 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
Helmut64 | 0:a3ea811f80f2 | 143 | pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_MODE_AF_INPUT)); // DM |
Helmut64 | 0:a3ea811f80f2 | 144 | pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_MODE_AF_INPUT)); // DP |
Helmut64 | 0:a3ea811f80f2 | 145 | |
Helmut64 | 0:a3ea811f80f2 | 146 | #elif defined(TARGET_DISCO_L072CZ_LRWAN1) || \ |
Helmut64 | 0:a3ea811f80f2 | 147 | defined(TARGET_DISCO_L053C8) |
Helmut64 | 0:a3ea811f80f2 | 148 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
Helmut64 | 0:a3ea811f80f2 | 149 | pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_USB)); // DM |
Helmut64 | 0:a3ea811f80f2 | 150 | pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_USB)); // DP |
Helmut Tschemernjak | 1:7d1a127a1d2b | 151 | |
Helmut Tschemernjak | 1:7d1a127a1d2b | 152 | #elif defined(TARGET_STM32L4) |
Helmut Tschemernjak | 1:7d1a127a1d2b | 153 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
Helmut Tschemernjak | 1:7d1a127a1d2b | 154 | pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_USB_FS)); // DM |
Helmut Tschemernjak | 1:7d1a127a1d2b | 155 | pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_USB_FS)); // DP |
Helmut Tschemernjak | 1:7d1a127a1d2b | 156 | __HAL_RCC_PWR_CLK_ENABLE(); |
Helmut Tschemernjak | 1:7d1a127a1d2b | 157 | HAL_PWREx_EnableVddUSB(); |
Helmut64 | 0:a3ea811f80f2 | 158 | |
Helmut64 | 0:a3ea811f80f2 | 159 | #else |
Helmut64 | 0:a3ea811f80f2 | 160 | #error "USB pins are not configured !" |
Helmut64 | 0:a3ea811f80f2 | 161 | #endif |
Helmut64 | 0:a3ea811f80f2 | 162 | |
Helmut64 | 0:a3ea811f80f2 | 163 | __HAL_RCC_USB_CLK_ENABLE(); |
Helmut64 | 0:a3ea811f80f2 | 164 | __HAL_RCC_SYSCFG_CLK_ENABLE(); |
Helmut64 | 0:a3ea811f80f2 | 165 | |
Helmut64 | 0:a3ea811f80f2 | 166 | // Configure PCD and FIFOs |
Helmut64 | 0:a3ea811f80f2 | 167 | hpcd.pData = (void*)HALPriv; |
Helmut64 | 0:a3ea811f80f2 | 168 | hpcd.State = HAL_PCD_STATE_RESET; |
Helmut64 | 0:a3ea811f80f2 | 169 | HAL_PCD_Init(&hpcd); |
Helmut64 | 0:a3ea811f80f2 | 170 | |
Helmut64 | 0:a3ea811f80f2 | 171 | // EP0 |
Helmut64 | 0:a3ea811f80f2 | 172 | HAL_PCDEx_PMAConfig(&hpcd, 0x00, PCD_SNG_BUF, 0x30); |
Helmut64 | 0:a3ea811f80f2 | 173 | HAL_PCDEx_PMAConfig(&hpcd, 0x80, PCD_SNG_BUF, 0x70); |
Helmut64 | 0:a3ea811f80f2 | 174 | // EP1 |
Helmut64 | 0:a3ea811f80f2 | 175 | HAL_PCDEx_PMAConfig(&hpcd, 0x01, PCD_SNG_BUF, 0x90); |
Helmut64 | 0:a3ea811f80f2 | 176 | HAL_PCDEx_PMAConfig(&hpcd, 0x81, PCD_SNG_BUF, 0xb0); |
Helmut64 | 0:a3ea811f80f2 | 177 | // EP2 |
Helmut64 | 0:a3ea811f80f2 | 178 | HAL_PCDEx_PMAConfig(&hpcd, 0x02, PCD_SNG_BUF, 0x100); |
Helmut64 | 0:a3ea811f80f2 | 179 | HAL_PCDEx_PMAConfig(&hpcd, 0x82, PCD_SNG_BUF, 0x120); |
Helmut64 | 0:a3ea811f80f2 | 180 | // EP3 |
Helmut64 | 0:a3ea811f80f2 | 181 | HAL_PCDEx_PMAConfig(&hpcd, 0x03, PCD_DBL_BUF, 0x018000b0); |
Helmut64 | 0:a3ea811f80f2 | 182 | HAL_PCDEx_PMAConfig(&hpcd, 0x83, PCD_SNG_BUF, 0xb0); |
Helmut64 | 0:a3ea811f80f2 | 183 | |
Helmut64 | 0:a3ea811f80f2 | 184 | // Configure interrupt vector |
Helmut64 | 0:a3ea811f80f2 | 185 | NVIC_SetVector(USBHAL_IRQn, (uint32_t)&_usbisr); |
Helmut64 | 0:a3ea811f80f2 | 186 | NVIC_SetPriority(USBHAL_IRQn, 1); |
Helmut64 | 0:a3ea811f80f2 | 187 | |
Helmut64 | 0:a3ea811f80f2 | 188 | HAL_PCD_Start(&hpcd); |
Helmut64 | 0:a3ea811f80f2 | 189 | } |
Helmut64 | 0:a3ea811f80f2 | 190 | |
Helmut64 | 0:a3ea811f80f2 | 191 | #endif // USBHAL_IP_DEVICE_H |