USBDevice with support for STM32F3
Fork of F042K6_USBDevice by
USBDevice/USBHAL_STM32L1.cpp@75:a394ea726048, 2018-10-11 (annotated)
- Committer:
- Bradley Scott
- Date:
- Thu Oct 11 11:12:35 2018 -0400
- Revision:
- 75:a394ea726048
- Parent:
- 71:b17ecdbfb0ce
Add USB suspend support for STM32L1 (and other STM32 devices with same USB peripheral)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 |
58:68fad4f36f1c | 1 | /* Copyright (c) 2010-2015 mbed.org, MIT License |
va009039 |
58:68fad4f36f1c | 2 | * |
va009039 |
58:68fad4f36f1c | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
va009039 |
58:68fad4f36f1c | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
va009039 |
58:68fad4f36f1c | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
va009039 |
58:68fad4f36f1c | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
va009039 |
58:68fad4f36f1c | 7 | * Software is furnished to do so, subject to the following conditions: |
va009039 |
58:68fad4f36f1c | 8 | * |
va009039 |
58:68fad4f36f1c | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
va009039 |
58:68fad4f36f1c | 10 | * substantial portions of the Software. |
va009039 |
58:68fad4f36f1c | 11 | * |
va009039 |
58:68fad4f36f1c | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
va009039 |
58:68fad4f36f1c | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
va009039 |
58:68fad4f36f1c | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
va009039 |
58:68fad4f36f1c | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
va009039 |
58:68fad4f36f1c | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
va009039 |
58:68fad4f36f1c | 17 | */ |
va009039 |
58:68fad4f36f1c | 18 | |
Bradley Scott |
66:b129e98ab7a0 | 19 | #if defined(TARGET_STM32L1)||defined(TARGET_STM32F3)||defined(TARGET_STM32F1)||defined(TARGET_STM32F0) |
Bradley Scott |
66:b129e98ab7a0 | 20 | #include "pinmap.h" |
va009039 |
58:68fad4f36f1c | 21 | #include "USBDevice.h" |
va009039 |
58:68fad4f36f1c | 22 | |
va009039 |
62:23cb405e1ee5 | 23 | #if defined(TARGET_STM32F1) |
va009039 |
62:23cb405e1ee5 | 24 | #define USB_LP_IRQn USB_LP_CAN1_RX0_IRQn |
va009039 |
62:23cb405e1ee5 | 25 | const uint8_t PCD_EP_TYPE_CTRL = EP_TYPE_CTRL; |
va009039 |
62:23cb405e1ee5 | 26 | const uint8_t PCD_EP_TYPE_INTR = EP_TYPE_INTR; |
va009039 |
62:23cb405e1ee5 | 27 | const uint8_t PCD_EP_TYPE_BULK = EP_TYPE_BULK; |
va009039 |
62:23cb405e1ee5 | 28 | const uint8_t PCD_EP_TYPE_ISOC = EP_TYPE_ISOC; |
va009039 |
62:23cb405e1ee5 | 29 | |
va009039 |
62:23cb405e1ee5 | 30 | #elif defined(TARGET_STM32L1) |
va009039 |
62:23cb405e1ee5 | 31 | void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state) { |
va009039 |
62:23cb405e1ee5 | 32 | __SYSCFG_CLK_ENABLE(); // for SYSCFG_PMC_USB_PU |
va009039 |
62:23cb405e1ee5 | 33 | if (state == 1) { |
va009039 |
62:23cb405e1ee5 | 34 | __HAL_SYSCFG_USBPULLUP_ENABLE(); |
va009039 |
62:23cb405e1ee5 | 35 | } else { |
va009039 |
62:23cb405e1ee5 | 36 | __HAL_SYSCFG_USBPULLUP_DISABLE(); |
va009039 |
62:23cb405e1ee5 | 37 | } |
va009039 |
62:23cb405e1ee5 | 38 | } |
va009039 |
63:05e2f2e4dc3e | 39 | |
va009039 |
63:05e2f2e4dc3e | 40 | #elif defined(TARGET_STM32L0)||defined(TARGET_STM32F0) |
va009039 |
63:05e2f2e4dc3e | 41 | #define USB_LP_IRQn USB_IRQn |
va009039 |
63:05e2f2e4dc3e | 42 | void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state) { |
va009039 |
63:05e2f2e4dc3e | 43 | if (state == 1) { |
va009039 |
63:05e2f2e4dc3e | 44 | SET_BIT(USB->BCDR, USB_BCDR_DPPU); // DP Pull-up |
va009039 |
63:05e2f2e4dc3e | 45 | } else { |
va009039 |
63:05e2f2e4dc3e | 46 | CLEAR_BIT(USB->BCDR, USB_BCDR_DPPU); |
va009039 |
63:05e2f2e4dc3e | 47 | } |
va009039 |
63:05e2f2e4dc3e | 48 | } |
va009039 |
63:05e2f2e4dc3e | 49 | |
va009039 |
63:05e2f2e4dc3e | 50 | #elif defined(TARGET_STM32F3) |
gte1 | 71:b17ecdbfb0ce | 51 | // use remapped USB interrupts, so that CAN peripheral can also be used if needed |
gte1 | 71:b17ecdbfb0ce | 52 | //#define USB_LP_IRQn USB_LP_CAN_RX0_IRQn |
va009039 |
62:23cb405e1ee5 | 53 | #endif |
va009039 |
62:23cb405e1ee5 | 54 | |
va009039 |
58:68fad4f36f1c | 55 | static PCD_HandleTypeDef hpcd_USB_FS; |
va009039 |
58:68fad4f36f1c | 56 | static volatile int epComplete = 0; |
va009039 |
58:68fad4f36f1c | 57 | |
va009039 |
58:68fad4f36f1c | 58 | USBHAL * USBHAL::instance; |
va009039 |
58:68fad4f36f1c | 59 | uint32_t USBHAL::endpointReadcore(uint8_t endpoint, uint8_t *buffer) {return 0;} |
va009039 |
58:68fad4f36f1c | 60 | |
gte1 | 69:f8305faf7917 | 61 | USBHAL::USBHAL(void) : PktBufArea(512) { |
Bradley Scott |
66:b129e98ab7a0 | 62 | #if defined(TARGET_STM32F3) |
Bradley Scott |
66:b129e98ab7a0 | 63 | // Configure USB pins |
Bradley Scott |
66:b129e98ab7a0 | 64 | pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF14_USB)); |
Bradley Scott |
66:b129e98ab7a0 | 65 | pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF14_USB)); |
Bradley Scott |
66:b129e98ab7a0 | 66 | #endif |
va009039 |
58:68fad4f36f1c | 67 | hpcd_USB_FS.pData = this; |
va009039 |
58:68fad4f36f1c | 68 | hpcd_USB_FS.Instance = USB; |
va009039 |
58:68fad4f36f1c | 69 | hpcd_USB_FS.Init.dev_endpoints = 8; |
va009039 |
58:68fad4f36f1c | 70 | hpcd_USB_FS.Init.speed = PCD_SPEED_FULL; |
va009039 |
58:68fad4f36f1c | 71 | hpcd_USB_FS.Init.ep0_mps = DEP0CTL_MPS_8; |
va009039 |
58:68fad4f36f1c | 72 | hpcd_USB_FS.Init.phy_itface = PCD_PHY_EMBEDDED; |
va009039 |
58:68fad4f36f1c | 73 | hpcd_USB_FS.Init.Sof_enable = DISABLE; |
va009039 |
58:68fad4f36f1c | 74 | hpcd_USB_FS.Init.low_power_enable = DISABLE; |
va009039 |
58:68fad4f36f1c | 75 | hpcd_USB_FS.Init.battery_charging_enable = DISABLE; |
gte1 | 71:b17ecdbfb0ce | 76 | #if defined(TARGET_STM32F3) |
gte1 | 71:b17ecdbfb0ce | 77 | __HAL_RCC_SYSCFG_CLK_ENABLE(); |
gte1 | 71:b17ecdbfb0ce | 78 | __HAL_REMAPINTERRUPT_USB_ENABLE(); |
gte1 | 71:b17ecdbfb0ce | 79 | #endif |
va009039 |
62:23cb405e1ee5 | 80 | NVIC_SetVector(USB_LP_IRQn, (uint32_t)&_usbisr); |
va009039 |
58:68fad4f36f1c | 81 | HAL_PCD_Init(&hpcd_USB_FS); |
va009039 |
62:23cb405e1ee5 | 82 | HAL_PCD_Start(&hpcd_USB_FS); |
va009039 |
58:68fad4f36f1c | 83 | } |
va009039 |
58:68fad4f36f1c | 84 | |
va009039 |
58:68fad4f36f1c | 85 | void HAL_PCD_MspInit(PCD_HandleTypeDef* hpcd) { |
Bradley Scott |
75:a394ea726048 | 86 | __HAL_RCC_USB_CLK_ENABLE(); |
va009039 |
58:68fad4f36f1c | 87 | HAL_NVIC_SetPriority(USB_LP_IRQn, 0, 0); |
va009039 |
58:68fad4f36f1c | 88 | } |
va009039 |
58:68fad4f36f1c | 89 | |
va009039 |
58:68fad4f36f1c | 90 | void HAL_PCD_MspDeInit(PCD_HandleTypeDef* hpcd) { |
Bradley Scott |
67:6099ce7e71c9 | 91 | HAL_NVIC_DisableIRQ(USB_LP_IRQn); // Peripheral interrupt Deinit |
Bradley Scott |
75:a394ea726048 | 92 | __HAL_RCC_USB_CLK_DISABLE(); // Peripheral clock disable |
va009039 |
58:68fad4f36f1c | 93 | } |
va009039 |
58:68fad4f36f1c | 94 | |
va009039 |
58:68fad4f36f1c | 95 | USBHAL::~USBHAL(void) { |
va009039 |
58:68fad4f36f1c | 96 | HAL_PCD_DeInit(&hpcd_USB_FS); |
va009039 |
58:68fad4f36f1c | 97 | } |
va009039 |
58:68fad4f36f1c | 98 | |
va009039 |
58:68fad4f36f1c | 99 | void USBHAL::connect(void) { |
Bradley Scott |
67:6099ce7e71c9 | 100 | HAL_NVIC_EnableIRQ(USB_LP_IRQn); |
va009039 |
58:68fad4f36f1c | 101 | HAL_PCD_DevConnect(&hpcd_USB_FS); |
va009039 |
58:68fad4f36f1c | 102 | } |
va009039 |
58:68fad4f36f1c | 103 | |
va009039 |
58:68fad4f36f1c | 104 | void USBHAL::disconnect(void) { |
va009039 |
58:68fad4f36f1c | 105 | HAL_PCD_DevDisconnect(&hpcd_USB_FS); |
Bradley Scott |
67:6099ce7e71c9 | 106 | HAL_NVIC_DisableIRQ(USB_LP_IRQn); |
va009039 |
58:68fad4f36f1c | 107 | } |
va009039 |
58:68fad4f36f1c | 108 | |
va009039 |
58:68fad4f36f1c | 109 | void USBHAL::configureDevice(void) { |
va009039 |
58:68fad4f36f1c | 110 | // Not needed |
va009039 |
58:68fad4f36f1c | 111 | } |
va009039 |
58:68fad4f36f1c | 112 | void USBHAL::unconfigureDevice(void) { |
va009039 |
58:68fad4f36f1c | 113 | // Not needed |
va009039 |
58:68fad4f36f1c | 114 | } |
va009039 |
58:68fad4f36f1c | 115 | |
va009039 |
58:68fad4f36f1c | 116 | void USBHAL::setAddress(uint8_t address) { |
va009039 |
58:68fad4f36f1c | 117 | HAL_PCD_SetAddress(&hpcd_USB_FS, address); |
va009039 |
58:68fad4f36f1c | 118 | } |
va009039 |
58:68fad4f36f1c | 119 | |
va009039 |
58:68fad4f36f1c | 120 | bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t flags) { |
va009039 |
58:68fad4f36f1c | 121 | int pmaadress = PktBufArea.allocBuf(maxPacket); |
va009039 |
58:68fad4f36f1c | 122 | MBED_ASSERT(pmaadress != 0); |
va009039 |
58:68fad4f36f1c | 123 | if (pmaadress == 0) { |
va009039 |
58:68fad4f36f1c | 124 | return false; |
va009039 |
58:68fad4f36f1c | 125 | } |
va009039 |
58:68fad4f36f1c | 126 | PCD_HandleTypeDef *hpcd = &hpcd_USB_FS; |
va009039 |
58:68fad4f36f1c | 127 | uint8_t ep_type; |
va009039 |
58:68fad4f36f1c | 128 | switch(endpoint) { |
va009039 |
58:68fad4f36f1c | 129 | case EP0OUT: |
va009039 |
58:68fad4f36f1c | 130 | HAL_PCDEx_PMAConfig(hpcd, 0x00, PCD_SNG_BUF, pmaadress); |
va009039 |
58:68fad4f36f1c | 131 | HAL_PCD_EP_Open(hpcd, 0x00, maxPacket, PCD_EP_TYPE_CTRL); |
va009039 |
58:68fad4f36f1c | 132 | break; |
va009039 |
58:68fad4f36f1c | 133 | case EP0IN: |
va009039 |
58:68fad4f36f1c | 134 | HAL_PCDEx_PMAConfig(hpcd, 0x80, PCD_SNG_BUF, pmaadress); |
va009039 |
58:68fad4f36f1c | 135 | HAL_PCD_EP_Open(hpcd, 0x80, maxPacket, PCD_EP_TYPE_CTRL); |
va009039 |
58:68fad4f36f1c | 136 | break; |
va009039 |
58:68fad4f36f1c | 137 | case EPINT_OUT: |
va009039 |
58:68fad4f36f1c | 138 | HAL_PCDEx_PMAConfig(hpcd, 0x01, PCD_SNG_BUF, pmaadress); |
va009039 |
58:68fad4f36f1c | 139 | HAL_PCD_EP_Open(hpcd, 0x01, maxPacket, PCD_EP_TYPE_INTR); |
va009039 |
58:68fad4f36f1c | 140 | break; |
va009039 |
58:68fad4f36f1c | 141 | case EPINT_IN: |
va009039 |
58:68fad4f36f1c | 142 | HAL_PCDEx_PMAConfig(hpcd, 0x81, PCD_SNG_BUF, pmaadress); |
va009039 |
58:68fad4f36f1c | 143 | HAL_PCD_EP_Open(hpcd, 0x81, maxPacket, PCD_EP_TYPE_INTR); |
va009039 |
58:68fad4f36f1c | 144 | break; |
va009039 |
58:68fad4f36f1c | 145 | case EPBULK_OUT: |
va009039 |
58:68fad4f36f1c | 146 | HAL_PCDEx_PMAConfig(hpcd, 0x02, PCD_SNG_BUF, pmaadress); |
va009039 |
58:68fad4f36f1c | 147 | HAL_PCD_EP_Open(hpcd, 0x02, maxPacket, PCD_EP_TYPE_BULK); |
va009039 |
58:68fad4f36f1c | 148 | break; |
va009039 |
58:68fad4f36f1c | 149 | case EPBULK_IN: |
va009039 |
58:68fad4f36f1c | 150 | HAL_PCDEx_PMAConfig(hpcd, 0x82, PCD_SNG_BUF, pmaadress); |
va009039 |
58:68fad4f36f1c | 151 | HAL_PCD_EP_Open(hpcd, 0x82, maxPacket, PCD_EP_TYPE_BULK); |
va009039 |
58:68fad4f36f1c | 152 | break; |
va009039 |
58:68fad4f36f1c | 153 | case EP3OUT: |
va009039 |
58:68fad4f36f1c | 154 | HAL_PCDEx_PMAConfig(hpcd, 0x03, PCD_SNG_BUF, pmaadress); |
va009039 |
58:68fad4f36f1c | 155 | ep_type = (flags & ISOCHRONOUS) ? PCD_EP_TYPE_ISOC : PCD_EP_TYPE_BULK; |
va009039 |
58:68fad4f36f1c | 156 | HAL_PCD_EP_Open(hpcd, 0x03, maxPacket, ep_type); |
va009039 |
58:68fad4f36f1c | 157 | break; |
va009039 |
58:68fad4f36f1c | 158 | case EP3IN: |
va009039 |
58:68fad4f36f1c | 159 | HAL_PCDEx_PMAConfig(hpcd, 0x83, PCD_SNG_BUF, pmaadress); |
va009039 |
58:68fad4f36f1c | 160 | ep_type = (flags & ISOCHRONOUS) ? PCD_EP_TYPE_ISOC : PCD_EP_TYPE_BULK; |
va009039 |
58:68fad4f36f1c | 161 | HAL_PCD_EP_Open(hpcd, 0x83, maxPacket, ep_type); |
va009039 |
58:68fad4f36f1c | 162 | break; |
va009039 |
58:68fad4f36f1c | 163 | default: |
va009039 |
58:68fad4f36f1c | 164 | MBED_ASSERT(0); |
va009039 |
58:68fad4f36f1c | 165 | return false; |
va009039 |
58:68fad4f36f1c | 166 | } |
va009039 |
58:68fad4f36f1c | 167 | return true; |
va009039 |
58:68fad4f36f1c | 168 | } |
va009039 |
58:68fad4f36f1c | 169 | |
va009039 |
58:68fad4f36f1c | 170 | // read setup packet |
va009039 |
58:68fad4f36f1c | 171 | void USBHAL::EP0setup(uint8_t *buffer) { |
va009039 |
58:68fad4f36f1c | 172 | memcpy(buffer, hpcd_USB_FS.Setup, 8); |
va009039 |
58:68fad4f36f1c | 173 | } |
va009039 |
58:68fad4f36f1c | 174 | |
va009039 |
58:68fad4f36f1c | 175 | void USBHAL::EP0readStage(void) { |
va009039 |
58:68fad4f36f1c | 176 | } |
va009039 |
58:68fad4f36f1c | 177 | |
va009039 |
58:68fad4f36f1c | 178 | void USBHAL::EP0read(void) { |
va009039 |
58:68fad4f36f1c | 179 | endpointRead(EP0OUT, MAX_PACKET_SIZE_EP0); |
va009039 |
58:68fad4f36f1c | 180 | } |
va009039 |
58:68fad4f36f1c | 181 | |
va009039 |
59:5d5e3685bd60 | 182 | class rxTempBufferManager { |
va009039 |
59:5d5e3685bd60 | 183 | uint8_t buf0[MAX_PACKET_SIZE_EP0]; |
va009039 |
59:5d5e3685bd60 | 184 | uint8_t buf1[MAX_PACKET_SIZE_EP1]; |
va009039 |
59:5d5e3685bd60 | 185 | uint8_t buf2[MAX_PACKET_SIZE_EP2]; |
va009039 |
59:5d5e3685bd60 | 186 | uint8_t buf3[MAX_PACKET_SIZE_EP3_ISO]; |
va009039 |
59:5d5e3685bd60 | 187 | public: |
va009039 |
59:5d5e3685bd60 | 188 | uint8_t* ptr(uint8_t endpoint, int maxPacketSize) { |
va009039 |
59:5d5e3685bd60 | 189 | switch(endpoint) { |
va009039 |
59:5d5e3685bd60 | 190 | case EP0OUT: |
va009039 |
59:5d5e3685bd60 | 191 | MBED_ASSERT(maxPacketSize <= MAX_PACKET_SIZE_EP0); |
va009039 |
59:5d5e3685bd60 | 192 | break; |
va009039 |
59:5d5e3685bd60 | 193 | case EP1OUT: |
va009039 |
59:5d5e3685bd60 | 194 | MBED_ASSERT(maxPacketSize <= MAX_PACKET_SIZE_EP1); |
va009039 |
59:5d5e3685bd60 | 195 | break; |
va009039 |
59:5d5e3685bd60 | 196 | case EP2OUT: |
va009039 |
59:5d5e3685bd60 | 197 | MBED_ASSERT(maxPacketSize <= MAX_PACKET_SIZE_EP2); |
va009039 |
59:5d5e3685bd60 | 198 | break; |
va009039 |
59:5d5e3685bd60 | 199 | case EP3OUT: |
va009039 |
59:5d5e3685bd60 | 200 | MBED_ASSERT(maxPacketSize <= MAX_PACKET_SIZE_EP3_ISO); |
va009039 |
59:5d5e3685bd60 | 201 | break; |
va009039 |
59:5d5e3685bd60 | 202 | } |
va009039 |
59:5d5e3685bd60 | 203 | return ptr(endpoint); |
va009039 |
58:68fad4f36f1c | 204 | } |
va009039 |
59:5d5e3685bd60 | 205 | uint8_t* ptr(uint8_t endpoint) { |
va009039 |
59:5d5e3685bd60 | 206 | switch(endpoint) { |
va009039 |
59:5d5e3685bd60 | 207 | case EP0OUT: return buf0; |
va009039 |
59:5d5e3685bd60 | 208 | case EP1OUT: return buf1; |
va009039 |
59:5d5e3685bd60 | 209 | case EP2OUT: return buf2; |
va009039 |
59:5d5e3685bd60 | 210 | case EP3OUT: return buf3; |
va009039 |
59:5d5e3685bd60 | 211 | } |
va009039 |
59:5d5e3685bd60 | 212 | MBED_ASSERT(0); |
va009039 |
59:5d5e3685bd60 | 213 | return NULL; |
va009039 |
59:5d5e3685bd60 | 214 | } |
va009039 |
59:5d5e3685bd60 | 215 | } rxtmp; |
va009039 |
58:68fad4f36f1c | 216 | |
va009039 |
58:68fad4f36f1c | 217 | uint32_t USBHAL::EP0getReadResult(uint8_t *buffer) { |
va009039 |
58:68fad4f36f1c | 218 | const uint8_t endpoint = EP0OUT; |
va009039 |
58:68fad4f36f1c | 219 | uint32_t length = HAL_PCD_EP_GetRxCount(&hpcd_USB_FS, endpoint>>1); |
va009039 |
59:5d5e3685bd60 | 220 | memcpy(buffer, rxtmp.ptr(endpoint), length); |
va009039 |
58:68fad4f36f1c | 221 | return length; |
va009039 |
58:68fad4f36f1c | 222 | } |
va009039 |
58:68fad4f36f1c | 223 | |
va009039 |
58:68fad4f36f1c | 224 | void USBHAL::EP0write(uint8_t *buffer, uint32_t size) { |
va009039 |
58:68fad4f36f1c | 225 | endpointWrite(EP0IN, buffer, size); |
va009039 |
58:68fad4f36f1c | 226 | } |
va009039 |
58:68fad4f36f1c | 227 | |
va009039 |
58:68fad4f36f1c | 228 | void USBHAL::EP0getWriteResult(void) { |
va009039 |
58:68fad4f36f1c | 229 | } |
va009039 |
58:68fad4f36f1c | 230 | |
va009039 |
58:68fad4f36f1c | 231 | void USBHAL::EP0stall(void) { |
va009039 |
58:68fad4f36f1c | 232 | // If we stall the out endpoint here then we have problems transferring |
va009039 |
58:68fad4f36f1c | 233 | // and setup requests after the (stalled) get device qualifier requests. |
va009039 |
58:68fad4f36f1c | 234 | // TODO: Find out if this is correct behavior, or whether we are doing |
va009039 |
58:68fad4f36f1c | 235 | // something else wrong |
va009039 |
58:68fad4f36f1c | 236 | stallEndpoint(EP0IN); |
va009039 |
58:68fad4f36f1c | 237 | // stallEndpoint(EP0OUT); |
va009039 |
58:68fad4f36f1c | 238 | } |
va009039 |
58:68fad4f36f1c | 239 | |
va009039 |
58:68fad4f36f1c | 240 | EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize) { |
Bradley Scott |
67:6099ce7e71c9 | 241 | core_util_critical_section_enter(); |
va009039 |
59:5d5e3685bd60 | 242 | HAL_PCD_EP_Receive(&hpcd_USB_FS, endpoint>>1, rxtmp.ptr(endpoint, maximumSize), maximumSize); |
va009039 |
58:68fad4f36f1c | 243 | epComplete &= ~(1 << endpoint); |
Bradley Scott |
67:6099ce7e71c9 | 244 | core_util_critical_section_exit(); |
va009039 |
58:68fad4f36f1c | 245 | return EP_PENDING; |
va009039 |
58:68fad4f36f1c | 246 | } |
va009039 |
58:68fad4f36f1c | 247 | |
va009039 |
58:68fad4f36f1c | 248 | EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t * buffer, uint32_t *bytesRead) { |
va009039 |
58:68fad4f36f1c | 249 | if (!(epComplete & (1 << endpoint))) { |
va009039 |
58:68fad4f36f1c | 250 | return EP_PENDING; |
va009039 |
58:68fad4f36f1c | 251 | } |
va009039 |
58:68fad4f36f1c | 252 | int len = HAL_PCD_EP_GetRxCount(&hpcd_USB_FS, endpoint>>1); |
va009039 |
59:5d5e3685bd60 | 253 | memcpy(buffer, rxtmp.ptr(endpoint), len); |
va009039 |
58:68fad4f36f1c | 254 | *bytesRead = len; |
va009039 |
58:68fad4f36f1c | 255 | return EP_COMPLETED; |
va009039 |
58:68fad4f36f1c | 256 | } |
va009039 |
58:68fad4f36f1c | 257 | |
va009039 |
58:68fad4f36f1c | 258 | EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size) { |
Bradley Scott |
67:6099ce7e71c9 | 259 | core_util_critical_section_enter(); |
va009039 |
58:68fad4f36f1c | 260 | HAL_PCD_EP_Transmit(&hpcd_USB_FS, endpoint>>1, data, size); |
va009039 |
58:68fad4f36f1c | 261 | epComplete &= ~(1 << endpoint); |
Bradley Scott |
67:6099ce7e71c9 | 262 | core_util_critical_section_exit(); |
va009039 |
58:68fad4f36f1c | 263 | return EP_PENDING; |
va009039 |
58:68fad4f36f1c | 264 | } |
va009039 |
58:68fad4f36f1c | 265 | |
va009039 |
58:68fad4f36f1c | 266 | EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint) { |
va009039 |
58:68fad4f36f1c | 267 | if (epComplete & (1 << endpoint)) { |
Bradley Scott |
67:6099ce7e71c9 | 268 | core_util_critical_section_enter(); |
va009039 |
58:68fad4f36f1c | 269 | epComplete &= ~(1 << endpoint); |
Bradley Scott |
67:6099ce7e71c9 | 270 | core_util_critical_section_exit(); |
va009039 |
58:68fad4f36f1c | 271 | return EP_COMPLETED; |
va009039 |
58:68fad4f36f1c | 272 | } |
va009039 |
58:68fad4f36f1c | 273 | return EP_PENDING; |
va009039 |
58:68fad4f36f1c | 274 | } |
va009039 |
58:68fad4f36f1c | 275 | |
va009039 |
58:68fad4f36f1c | 276 | void USBHAL::stallEndpoint(uint8_t endpoint) { |
va009039 |
58:68fad4f36f1c | 277 | PCD_HandleTypeDef *hpcd = &hpcd_USB_FS; |
va009039 |
58:68fad4f36f1c | 278 | switch(endpoint) { |
va009039 |
58:68fad4f36f1c | 279 | case EP0IN: |
va009039 |
58:68fad4f36f1c | 280 | HAL_PCD_EP_SetStall(hpcd, 0x80); |
va009039 |
58:68fad4f36f1c | 281 | break; |
va009039 |
58:68fad4f36f1c | 282 | case EP0OUT: |
va009039 |
58:68fad4f36f1c | 283 | HAL_PCD_EP_SetStall(hpcd, 0x00); |
va009039 |
58:68fad4f36f1c | 284 | break; |
va009039 |
58:68fad4f36f1c | 285 | default: |
va009039 |
58:68fad4f36f1c | 286 | break; |
va009039 |
58:68fad4f36f1c | 287 | } |
va009039 |
58:68fad4f36f1c | 288 | } |
va009039 |
58:68fad4f36f1c | 289 | |
va009039 |
58:68fad4f36f1c | 290 | void USBHAL::unstallEndpoint(uint8_t endpoint) { |
va009039 |
58:68fad4f36f1c | 291 | } |
va009039 |
58:68fad4f36f1c | 292 | |
va009039 |
58:68fad4f36f1c | 293 | bool USBHAL::getEndpointStallState(uint8_t endpoint) { |
va009039 |
58:68fad4f36f1c | 294 | return false; |
va009039 |
58:68fad4f36f1c | 295 | } |
va009039 |
58:68fad4f36f1c | 296 | |
va009039 |
58:68fad4f36f1c | 297 | void USBHAL::remoteWakeup(void) {} |
va009039 |
62:23cb405e1ee5 | 298 | |
va009039 |
62:23cb405e1ee5 | 299 | void USBHAL::_usbisr(void) { |
va009039 |
62:23cb405e1ee5 | 300 | HAL_PCD_IRQHandler(&hpcd_USB_FS); |
va009039 |
62:23cb405e1ee5 | 301 | } |
va009039 |
62:23cb405e1ee5 | 302 | |
va009039 |
58:68fad4f36f1c | 303 | void USBHAL::usbisr(void) {} |
va009039 |
58:68fad4f36f1c | 304 | |
va009039 |
58:68fad4f36f1c | 305 | void USBHAL::SetupStageCallback() { |
va009039 |
58:68fad4f36f1c | 306 | EP0setupCallback(); |
va009039 |
58:68fad4f36f1c | 307 | } |
va009039 |
58:68fad4f36f1c | 308 | |
va009039 |
58:68fad4f36f1c | 309 | void USBHAL::DataInStageCallback(uint8_t epnum) { |
va009039 |
58:68fad4f36f1c | 310 | switch(epnum) { |
va009039 |
58:68fad4f36f1c | 311 | case 0: // EP0IN |
va009039 |
58:68fad4f36f1c | 312 | EP0in(); |
va009039 |
58:68fad4f36f1c | 313 | break; |
va009039 |
58:68fad4f36f1c | 314 | case 1: |
va009039 |
58:68fad4f36f1c | 315 | epComplete |= (1<<EP1IN); |
va009039 |
58:68fad4f36f1c | 316 | if (EP1_IN_callback()) { |
va009039 |
58:68fad4f36f1c | 317 | epComplete &= ~(1<<EP1IN); |
va009039 |
58:68fad4f36f1c | 318 | } |
va009039 |
58:68fad4f36f1c | 319 | break; |
va009039 |
58:68fad4f36f1c | 320 | case 2: |
va009039 |
58:68fad4f36f1c | 321 | epComplete |= (1<<EP2IN); |
va009039 |
58:68fad4f36f1c | 322 | if (EP2_IN_callback()) { |
va009039 |
58:68fad4f36f1c | 323 | epComplete &= ~(1<<EP2IN); |
va009039 |
58:68fad4f36f1c | 324 | } |
va009039 |
58:68fad4f36f1c | 325 | break; |
va009039 |
58:68fad4f36f1c | 326 | case 3: |
va009039 |
58:68fad4f36f1c | 327 | epComplete |= (1<<EP3IN); |
va009039 |
58:68fad4f36f1c | 328 | if (EP3_IN_callback()) { |
va009039 |
58:68fad4f36f1c | 329 | epComplete &= ~(1<<EP3IN); |
va009039 |
58:68fad4f36f1c | 330 | } |
va009039 |
58:68fad4f36f1c | 331 | break; |
va009039 |
58:68fad4f36f1c | 332 | default: |
va009039 |
58:68fad4f36f1c | 333 | MBED_ASSERT(0); |
va009039 |
58:68fad4f36f1c | 334 | break; |
va009039 |
58:68fad4f36f1c | 335 | } |
va009039 |
58:68fad4f36f1c | 336 | } |
va009039 |
58:68fad4f36f1c | 337 | |
va009039 |
58:68fad4f36f1c | 338 | void USBHAL::DataOutStageCallback(uint8_t epnum) { |
va009039 |
58:68fad4f36f1c | 339 | switch(epnum) { |
va009039 |
58:68fad4f36f1c | 340 | case 0: // EP0OUT |
va009039 |
59:5d5e3685bd60 | 341 | if ((hpcd_USB_FS.Setup[0]&0x80) == 0x00) { // host to device ? |
va009039 |
59:5d5e3685bd60 | 342 | EP0out(); |
va009039 |
59:5d5e3685bd60 | 343 | } |
va009039 |
58:68fad4f36f1c | 344 | break; |
va009039 |
58:68fad4f36f1c | 345 | case 1: |
va009039 |
58:68fad4f36f1c | 346 | epComplete |= (1<<EP1OUT); |
va009039 |
58:68fad4f36f1c | 347 | if (EP1_OUT_callback()) { |
va009039 |
58:68fad4f36f1c | 348 | epComplete &= ~(1<<EP1OUT); |
va009039 |
58:68fad4f36f1c | 349 | } |
va009039 |
58:68fad4f36f1c | 350 | break; |
va009039 |
58:68fad4f36f1c | 351 | case 2: |
va009039 |
58:68fad4f36f1c | 352 | epComplete |= (1<<EP2OUT); |
va009039 |
58:68fad4f36f1c | 353 | if (EP2_OUT_callback()) { |
va009039 |
58:68fad4f36f1c | 354 | epComplete &= ~(1<<EP2OUT); |
va009039 |
58:68fad4f36f1c | 355 | } |
va009039 |
58:68fad4f36f1c | 356 | break; |
va009039 |
58:68fad4f36f1c | 357 | case 3: |
va009039 |
58:68fad4f36f1c | 358 | epComplete |= (1<<EP3OUT); |
va009039 |
58:68fad4f36f1c | 359 | if (EP3_OUT_callback()) { |
va009039 |
58:68fad4f36f1c | 360 | epComplete &= ~(1<<EP3OUT); |
va009039 |
58:68fad4f36f1c | 361 | } |
va009039 |
58:68fad4f36f1c | 362 | break; |
va009039 |
58:68fad4f36f1c | 363 | default: |
va009039 |
58:68fad4f36f1c | 364 | MBED_ASSERT(0); |
va009039 |
58:68fad4f36f1c | 365 | break; |
va009039 |
58:68fad4f36f1c | 366 | } |
va009039 |
58:68fad4f36f1c | 367 | } |
va009039 |
58:68fad4f36f1c | 368 | |
va009039 |
58:68fad4f36f1c | 369 | void USBHAL::ResetCallback() { |
Bradley Scott |
75:a394ea726048 | 370 | suspendStateChanged(0); |
va009039 |
58:68fad4f36f1c | 371 | PktBufArea.reset(); |
va009039 |
58:68fad4f36f1c | 372 | realiseEndpoint(EP0IN, MAX_PACKET_SIZE_EP0, 0); |
va009039 |
58:68fad4f36f1c | 373 | realiseEndpoint(EP0OUT, MAX_PACKET_SIZE_EP0, 0); |
va009039 |
58:68fad4f36f1c | 374 | } |
va009039 |
58:68fad4f36f1c | 375 | |
Bradley Scott |
75:a394ea726048 | 376 | void USBHAL::SuspendCallback() { |
Bradley Scott |
75:a394ea726048 | 377 | suspendStateChanged(1); |
Bradley Scott |
75:a394ea726048 | 378 | } |
Bradley Scott |
75:a394ea726048 | 379 | |
va009039 |
58:68fad4f36f1c | 380 | void USBHAL::SOFCallback() { |
va009039 |
63:05e2f2e4dc3e | 381 | SOF(hpcd_USB_FS.Instance->FNR & USB_FNR_FN); |
va009039 |
58:68fad4f36f1c | 382 | } |
va009039 |
58:68fad4f36f1c | 383 | |
va009039 |
58:68fad4f36f1c | 384 | void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd) { |
va009039 |
58:68fad4f36f1c | 385 | reinterpret_cast<USBHAL*>(hpcd->pData)->SetupStageCallback(); |
va009039 |
58:68fad4f36f1c | 386 | } |
va009039 |
58:68fad4f36f1c | 387 | |
va009039 |
58:68fad4f36f1c | 388 | void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) { |
va009039 |
58:68fad4f36f1c | 389 | reinterpret_cast<USBHAL*>(hpcd->pData)->DataInStageCallback(epnum); |
va009039 |
58:68fad4f36f1c | 390 | } |
va009039 |
58:68fad4f36f1c | 391 | |
va009039 |
58:68fad4f36f1c | 392 | void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) { |
va009039 |
58:68fad4f36f1c | 393 | reinterpret_cast<USBHAL*>(hpcd->pData)->DataOutStageCallback(epnum); |
va009039 |
58:68fad4f36f1c | 394 | } |
va009039 |
58:68fad4f36f1c | 395 | |
va009039 |
58:68fad4f36f1c | 396 | void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd) { |
va009039 |
58:68fad4f36f1c | 397 | reinterpret_cast<USBHAL*>(hpcd->pData)->ResetCallback(); |
va009039 |
58:68fad4f36f1c | 398 | } |
va009039 |
58:68fad4f36f1c | 399 | |
va009039 |
58:68fad4f36f1c | 400 | void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) { |
va009039 |
58:68fad4f36f1c | 401 | reinterpret_cast<USBHAL*>(hpcd->pData)->SOFCallback(); |
va009039 |
58:68fad4f36f1c | 402 | } |
va009039 |
58:68fad4f36f1c | 403 | |
va009039 |
58:68fad4f36f1c | 404 | void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd) { |
Bradley Scott |
75:a394ea726048 | 405 | reinterpret_cast<USBHAL*>(hpcd->pData)->SuspendCallback(); |
va009039 |
58:68fad4f36f1c | 406 | if (hpcd->Init.low_power_enable) { |
va009039 |
58:68fad4f36f1c | 407 | SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk)); |
va009039 |
58:68fad4f36f1c | 408 | } |
va009039 |
58:68fad4f36f1c | 409 | } |
va009039 |
58:68fad4f36f1c | 410 | |
va009039 |
58:68fad4f36f1c | 411 | #endif |