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