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