Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.
Upstream: https://github.com/ARMmbed/DAPLink
source/hic_hal/freescale/k26f/usbd_MK26F.c@0:01f31e923fe2, 2020-04-07 (annotated)
- Committer:
- Pawel Zarembski
- Date:
- Tue Apr 07 12:55:42 2020 +0200
- Revision:
- 0:01f31e923fe2
hani: DAPLink with reset workaround
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pawel Zarembski |
0:01f31e923fe2 | 1 | /** |
Pawel Zarembski |
0:01f31e923fe2 | 2 | * @file usbd_LPC43xx_USBD0.c |
Pawel Zarembski |
0:01f31e923fe2 | 3 | * @brief |
Pawel Zarembski |
0:01f31e923fe2 | 4 | * |
Pawel Zarembski |
0:01f31e923fe2 | 5 | * DAPLink Interface Firmware |
Pawel Zarembski |
0:01f31e923fe2 | 6 | * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved |
Pawel Zarembski |
0:01f31e923fe2 | 7 | * SPDX-License-Identifier: Apache-2.0 |
Pawel Zarembski |
0:01f31e923fe2 | 8 | * |
Pawel Zarembski |
0:01f31e923fe2 | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
Pawel Zarembski |
0:01f31e923fe2 | 10 | * not use this file except in compliance with the License. |
Pawel Zarembski |
0:01f31e923fe2 | 11 | * You may obtain a copy of the License at |
Pawel Zarembski |
0:01f31e923fe2 | 12 | * |
Pawel Zarembski |
0:01f31e923fe2 | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
Pawel Zarembski |
0:01f31e923fe2 | 14 | * |
Pawel Zarembski |
0:01f31e923fe2 | 15 | * Unless required by applicable law or agreed to in writing, software |
Pawel Zarembski |
0:01f31e923fe2 | 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
Pawel Zarembski |
0:01f31e923fe2 | 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Pawel Zarembski |
0:01f31e923fe2 | 18 | * See the License for the specific language governing permissions and |
Pawel Zarembski |
0:01f31e923fe2 | 19 | * limitations under the License. |
Pawel Zarembski |
0:01f31e923fe2 | 20 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 21 | |
Pawel Zarembski |
0:01f31e923fe2 | 22 | #include "rl_usb.h" |
Pawel Zarembski |
0:01f31e923fe2 | 23 | #include "usb.h" |
Pawel Zarembski |
0:01f31e923fe2 | 24 | #include "fsl_device_registers.h" |
Pawel Zarembski |
0:01f31e923fe2 | 25 | #include "hic_init.h" |
Pawel Zarembski |
0:01f31e923fe2 | 26 | |
Pawel Zarembski |
0:01f31e923fe2 | 27 | #define __NO_USB_LIB_C |
Pawel Zarembski |
0:01f31e923fe2 | 28 | #include "usb_config.c" |
Pawel Zarembski |
0:01f31e923fe2 | 29 | |
Pawel Zarembski |
0:01f31e923fe2 | 30 | /* Endpoint queue head */ |
Pawel Zarembski |
0:01f31e923fe2 | 31 | typedef struct __EPQH { |
Pawel Zarembski |
0:01f31e923fe2 | 32 | uint32_t cap; |
Pawel Zarembski |
0:01f31e923fe2 | 33 | uint32_t curr_dTD; |
Pawel Zarembski |
0:01f31e923fe2 | 34 | uint32_t next_dTD; |
Pawel Zarembski |
0:01f31e923fe2 | 35 | uint32_t dTD_token; |
Pawel Zarembski |
0:01f31e923fe2 | 36 | uint32_t buf[5]; |
Pawel Zarembski |
0:01f31e923fe2 | 37 | uint32_t reserved; |
Pawel Zarembski |
0:01f31e923fe2 | 38 | uint32_t setup[2]; |
Pawel Zarembski |
0:01f31e923fe2 | 39 | uint32_t reserved1[4]; |
Pawel Zarembski |
0:01f31e923fe2 | 40 | } EPQH; |
Pawel Zarembski |
0:01f31e923fe2 | 41 | |
Pawel Zarembski |
0:01f31e923fe2 | 42 | /* Endpoint transfer descriptor */ |
Pawel Zarembski |
0:01f31e923fe2 | 43 | typedef struct __dTD { |
Pawel Zarembski |
0:01f31e923fe2 | 44 | uint32_t next_dTD; |
Pawel Zarembski |
0:01f31e923fe2 | 45 | uint32_t dTD_token; |
Pawel Zarembski |
0:01f31e923fe2 | 46 | uint32_t buf[5]; |
Pawel Zarembski |
0:01f31e923fe2 | 47 | uint32_t reserved; |
Pawel Zarembski |
0:01f31e923fe2 | 48 | } dTD; |
Pawel Zarembski |
0:01f31e923fe2 | 49 | |
Pawel Zarembski |
0:01f31e923fe2 | 50 | /* Endpoint */ |
Pawel Zarembski |
0:01f31e923fe2 | 51 | typedef struct __EP { |
Pawel Zarembski |
0:01f31e923fe2 | 52 | uint8_t *buf; |
Pawel Zarembski |
0:01f31e923fe2 | 53 | uint32_t maxPacket; |
Pawel Zarembski |
0:01f31e923fe2 | 54 | } EP; |
Pawel Zarembski |
0:01f31e923fe2 | 55 | |
Pawel Zarembski |
0:01f31e923fe2 | 56 | EPQH __align(2048) EPQHx[(USBD_EP_NUM + 1) * 2]; |
Pawel Zarembski |
0:01f31e923fe2 | 57 | dTD __align(32) dTDx[(USBD_EP_NUM + 1) * 2]; |
Pawel Zarembski |
0:01f31e923fe2 | 58 | |
Pawel Zarembski |
0:01f31e923fe2 | 59 | EP Ep[(USBD_EP_NUM + 1) * 2]; |
Pawel Zarembski |
0:01f31e923fe2 | 60 | uint32_t BufUsed; |
Pawel Zarembski |
0:01f31e923fe2 | 61 | uint32_t IsoEp; |
Pawel Zarembski |
0:01f31e923fe2 | 62 | uint32_t cmpl_pnd; |
Pawel Zarembski |
0:01f31e923fe2 | 63 | |
Pawel Zarembski |
0:01f31e923fe2 | 64 | #define ENDPTCTRL(EPNum) *(volatile uint32_t *)((uint32_t)(&USBHS->EPCR0) + 4 * EPNum) |
Pawel Zarembski |
0:01f31e923fe2 | 65 | #define EP_OUT_IDX(EPNum) (EPNum * 2 ) |
Pawel Zarembski |
0:01f31e923fe2 | 66 | #define EP_IN_IDX(EPNum) (EPNum * 2 + 1) |
Pawel Zarembski |
0:01f31e923fe2 | 67 | #define HS(en) (USBD_HS_ENABLE * en) |
Pawel Zarembski |
0:01f31e923fe2 | 68 | |
Pawel Zarembski |
0:01f31e923fe2 | 69 | /* reserve RAM for endpoint buffers */ |
Pawel Zarembski |
0:01f31e923fe2 | 70 | #if USBD_VENDOR_ENABLE |
Pawel Zarembski |
0:01f31e923fe2 | 71 | /* custom class: user defined buffer size */ |
Pawel Zarembski |
0:01f31e923fe2 | 72 | #define EP_BUF_POOL_SIZE 0x1000 |
Pawel Zarembski |
0:01f31e923fe2 | 73 | uint8_t __align(4096) EPBufPool[EP_BUF_POOL_SIZE] |
Pawel Zarembski |
0:01f31e923fe2 | 74 | #else |
Pawel Zarembski |
0:01f31e923fe2 | 75 | /* supported classes are used */ |
Pawel Zarembski |
0:01f31e923fe2 | 76 | uint8_t __align(4096) EPBufPool[ |
Pawel Zarembski |
0:01f31e923fe2 | 77 | USBD_MAX_PACKET0 * 2 + |
Pawel Zarembski |
0:01f31e923fe2 | 78 | USBD_HID_ENABLE * (HS(USBD_HID_HS_ENABLE) ? USBD_HID_HS_WMAXPACKETSIZE : USBD_HID_WMAXPACKETSIZE) * 2 + |
Pawel Zarembski |
0:01f31e923fe2 | 79 | USBD_MSC_ENABLE * (HS(USBD_MSC_HS_ENABLE) ? USBD_MSC_HS_WMAXPACKETSIZE : USBD_MSC_WMAXPACKETSIZE) * 2 + |
Pawel Zarembski |
0:01f31e923fe2 | 80 | USBD_ADC_ENABLE * (HS(USBD_ADC_HS_ENABLE) ? USBD_ADC_HS_WMAXPACKETSIZE : USBD_ADC_WMAXPACKETSIZE) + |
Pawel Zarembski |
0:01f31e923fe2 | 81 | USBD_CDC_ACM_ENABLE * ((HS(USBD_CDC_ACM_HS_ENABLE) ? USBD_CDC_ACM_HS_WMAXPACKETSIZE : USBD_CDC_ACM_WMAXPACKETSIZE) + |
Pawel Zarembski |
0:01f31e923fe2 | 82 | (HS(USBD_CDC_ACM_HS_ENABLE) ? USBD_CDC_ACM_HS_WMAXPACKETSIZE1 : USBD_CDC_ACM_WMAXPACKETSIZE1) * 2) + |
Pawel Zarembski |
0:01f31e923fe2 | 83 | USBD_BULK_ENABLE * (HS(USBD_BULK_HS_ENABLE) ? USBD_BULK_HS_WMAXPACKETSIZE : USBD_BULK_WMAXPACKETSIZE) * 2 |
Pawel Zarembski |
0:01f31e923fe2 | 84 | ]; |
Pawel Zarembski |
0:01f31e923fe2 | 85 | #endif |
Pawel Zarembski |
0:01f31e923fe2 | 86 | |
Pawel Zarembski |
0:01f31e923fe2 | 87 | void USBD_PrimeEp(uint32_t EPNum, uint32_t cnt); |
Pawel Zarembski |
0:01f31e923fe2 | 88 | |
Pawel Zarembski |
0:01f31e923fe2 | 89 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 90 | * Usb interrupt enable/disable |
Pawel Zarembski |
0:01f31e923fe2 | 91 | * Parameters: ena: enable/disable |
Pawel Zarembski |
0:01f31e923fe2 | 92 | * 0: disable interrupt |
Pawel Zarembski |
0:01f31e923fe2 | 93 | * 1: enable interrupt |
Pawel Zarembski |
0:01f31e923fe2 | 94 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 95 | |
Pawel Zarembski |
0:01f31e923fe2 | 96 | #ifdef __RTX |
Pawel Zarembski |
0:01f31e923fe2 | 97 | void __svc(1) USBD_Intr(int ena); |
Pawel Zarembski |
0:01f31e923fe2 | 98 | void __SVC_1(int ena) |
Pawel Zarembski |
0:01f31e923fe2 | 99 | { |
Pawel Zarembski |
0:01f31e923fe2 | 100 | #else |
Pawel Zarembski |
0:01f31e923fe2 | 101 | void USBD_Intr(int ena) |
Pawel Zarembski |
0:01f31e923fe2 | 102 | { |
Pawel Zarembski |
0:01f31e923fe2 | 103 | #endif |
Pawel Zarembski |
0:01f31e923fe2 | 104 | |
Pawel Zarembski |
0:01f31e923fe2 | 105 | if (ena) { |
Pawel Zarembski |
0:01f31e923fe2 | 106 | NVIC_EnableIRQ(USBHS_IRQn); /* Enable USB interrupt */ |
Pawel Zarembski |
0:01f31e923fe2 | 107 | } else { |
Pawel Zarembski |
0:01f31e923fe2 | 108 | NVIC_DisableIRQ(USBHS_IRQn); /* Disable USB interrupt */ |
Pawel Zarembski |
0:01f31e923fe2 | 109 | } |
Pawel Zarembski |
0:01f31e923fe2 | 110 | } |
Pawel Zarembski |
0:01f31e923fe2 | 111 | |
Pawel Zarembski |
0:01f31e923fe2 | 112 | |
Pawel Zarembski |
0:01f31e923fe2 | 113 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 114 | * USB Device Initialize Function |
Pawel Zarembski |
0:01f31e923fe2 | 115 | * Called by the User to initialize USB Device |
Pawel Zarembski |
0:01f31e923fe2 | 116 | * Return Value: None |
Pawel Zarembski |
0:01f31e923fe2 | 117 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 118 | |
Pawel Zarembski |
0:01f31e923fe2 | 119 | void USBD_Init(void) |
Pawel Zarembski |
0:01f31e923fe2 | 120 | { |
Pawel Zarembski |
0:01f31e923fe2 | 121 | USBD_Intr(0); |
Pawel Zarembski |
0:01f31e923fe2 | 122 | |
Pawel Zarembski |
0:01f31e923fe2 | 123 | hic_enable_usb_clocks(); |
Pawel Zarembski |
0:01f31e923fe2 | 124 | |
Pawel Zarembski |
0:01f31e923fe2 | 125 | USBHS->USBCMD |= (1UL << 1); /* usb reset */ |
Pawel Zarembski |
0:01f31e923fe2 | 126 | |
Pawel Zarembski |
0:01f31e923fe2 | 127 | while (USBHS->USBCMD & (1UL << 1)); |
Pawel Zarembski |
0:01f31e923fe2 | 128 | |
Pawel Zarembski |
0:01f31e923fe2 | 129 | USBHS->USBMODE = 2 | (1UL << 3);/* device mode */ |
Pawel Zarembski |
0:01f31e923fe2 | 130 | #if USBD_HS_ENABLE |
Pawel Zarembski |
0:01f31e923fe2 | 131 | USBHS->PORTSC1 &= ~(1UL << 24); |
Pawel Zarembski |
0:01f31e923fe2 | 132 | #else |
Pawel Zarembski |
0:01f31e923fe2 | 133 | USBHS->PORTSC1 |= (1UL << 24); |
Pawel Zarembski |
0:01f31e923fe2 | 134 | #endif |
Pawel Zarembski |
0:01f31e923fe2 | 135 | USBHS->OTGSC = 1 | (1UL << 3); |
Pawel Zarembski |
0:01f31e923fe2 | 136 | Ep[EP_OUT_IDX(0)].maxPacket = USBD_MAX_PACKET0; |
Pawel Zarembski |
0:01f31e923fe2 | 137 | USBHS->USBINTR = (1UL << 0) | /* usb int enable */ |
Pawel Zarembski |
0:01f31e923fe2 | 138 | (1UL << 2) | /* port change detect int enable */ |
Pawel Zarembski |
0:01f31e923fe2 | 139 | (1UL << 8) | /* suspend int enable */ |
Pawel Zarembski |
0:01f31e923fe2 | 140 | (1UL << 16) | /* nak int enable */ |
Pawel Zarembski |
0:01f31e923fe2 | 141 | (1UL << 6) | /* reset int enable */ |
Pawel Zarembski |
0:01f31e923fe2 | 142 | #ifdef __RTX |
Pawel Zarembski |
0:01f31e923fe2 | 143 | ((USBD_RTX_DevTask != 0) ? (1UL << 7) : 0) | /* SOF */ |
Pawel Zarembski |
0:01f31e923fe2 | 144 | ((USBD_RTX_DevTask != 0) ? (1UL << 1) : 0) ; /* Error */ |
Pawel Zarembski |
0:01f31e923fe2 | 145 | #else |
Pawel Zarembski |
0:01f31e923fe2 | 146 | ((USBD_P_SOF_Event != 0) ? (1UL << 7) : 0) | /* SOF */ |
Pawel Zarembski |
0:01f31e923fe2 | 147 | ((USBD_P_Error_Event != 0) ? (1UL << 1) : 0) ; /* Error */ |
Pawel Zarembski |
0:01f31e923fe2 | 148 | #endif |
Pawel Zarembski |
0:01f31e923fe2 | 149 | USBD_Reset(); |
Pawel Zarembski |
0:01f31e923fe2 | 150 | USBD_Intr(1); |
Pawel Zarembski |
0:01f31e923fe2 | 151 | } |
Pawel Zarembski |
0:01f31e923fe2 | 152 | |
Pawel Zarembski |
0:01f31e923fe2 | 153 | |
Pawel Zarembski |
0:01f31e923fe2 | 154 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 155 | * USB Device Connect Function |
Pawel Zarembski |
0:01f31e923fe2 | 156 | * Called by the User to Connect/Disconnect USB Device |
Pawel Zarembski |
0:01f31e923fe2 | 157 | * Parameters: con: Connect/Disconnect |
Pawel Zarembski |
0:01f31e923fe2 | 158 | * Return Value: None |
Pawel Zarembski |
0:01f31e923fe2 | 159 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 160 | |
Pawel Zarembski |
0:01f31e923fe2 | 161 | void USBD_Connect(uint32_t con) |
Pawel Zarembski |
0:01f31e923fe2 | 162 | { |
Pawel Zarembski |
0:01f31e923fe2 | 163 | if (con) { |
Pawel Zarembski |
0:01f31e923fe2 | 164 | USBHS->USBCMD |= 1; /* run */ |
Pawel Zarembski |
0:01f31e923fe2 | 165 | } else { |
Pawel Zarembski |
0:01f31e923fe2 | 166 | USBHS->USBCMD &= ~1; /* stop */ |
Pawel Zarembski |
0:01f31e923fe2 | 167 | } |
Pawel Zarembski |
0:01f31e923fe2 | 168 | } |
Pawel Zarembski |
0:01f31e923fe2 | 169 | |
Pawel Zarembski |
0:01f31e923fe2 | 170 | |
Pawel Zarembski |
0:01f31e923fe2 | 171 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 172 | * USB Device Reset Function |
Pawel Zarembski |
0:01f31e923fe2 | 173 | * Called automatically on USB Device Reset |
Pawel Zarembski |
0:01f31e923fe2 | 174 | * Return Value: None |
Pawel Zarembski |
0:01f31e923fe2 | 175 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 176 | |
Pawel Zarembski |
0:01f31e923fe2 | 177 | void USBD_Reset(void) |
Pawel Zarembski |
0:01f31e923fe2 | 178 | { |
Pawel Zarembski |
0:01f31e923fe2 | 179 | uint32_t i; |
Pawel Zarembski |
0:01f31e923fe2 | 180 | uint8_t *ptr; |
Pawel Zarembski |
0:01f31e923fe2 | 181 | cmpl_pnd = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 182 | |
Pawel Zarembski |
0:01f31e923fe2 | 183 | for (i = 1; i < USBD_EP_NUM + 1; i++) { |
Pawel Zarembski |
0:01f31e923fe2 | 184 | ENDPTCTRL(i) &= ~((1UL << 7) | (1UL << 23)); |
Pawel Zarembski |
0:01f31e923fe2 | 185 | } |
Pawel Zarembski |
0:01f31e923fe2 | 186 | |
Pawel Zarembski |
0:01f31e923fe2 | 187 | /* clear interrupts */ |
Pawel Zarembski |
0:01f31e923fe2 | 188 | USBHS->ENDPTNAK = 0xFFFFFFFF; |
Pawel Zarembski |
0:01f31e923fe2 | 189 | USBHS->ENDPTNAKEN = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 190 | USBHS->USBSTS = 0xFFFFFFFF; |
Pawel Zarembski |
0:01f31e923fe2 | 191 | USBHS->EPSETUPSR = USBHS->EPSETUPSR; |
Pawel Zarembski |
0:01f31e923fe2 | 192 | USBHS->EPCOMPLETE = USBHS->EPCOMPLETE; |
Pawel Zarembski |
0:01f31e923fe2 | 193 | |
Pawel Zarembski |
0:01f31e923fe2 | 194 | while (USBHS->EPPRIME); |
Pawel Zarembski |
0:01f31e923fe2 | 195 | |
Pawel Zarembski |
0:01f31e923fe2 | 196 | USBHS->EPFLUSH = 0xFFFFFFFF; |
Pawel Zarembski |
0:01f31e923fe2 | 197 | |
Pawel Zarembski |
0:01f31e923fe2 | 198 | while (USBHS->EPFLUSH); |
Pawel Zarembski |
0:01f31e923fe2 | 199 | |
Pawel Zarembski |
0:01f31e923fe2 | 200 | USBHS->USBCMD &= ~0x00FF0000; /* immediate intrrupt treshold */ |
Pawel Zarembski |
0:01f31e923fe2 | 201 | /* clear endpoint queue heads */ |
Pawel Zarembski |
0:01f31e923fe2 | 202 | ptr = (uint8_t *)EPQHx; |
Pawel Zarembski |
0:01f31e923fe2 | 203 | |
Pawel Zarembski |
0:01f31e923fe2 | 204 | for (i = 0; i < sizeof(EPQHx); i++) { |
Pawel Zarembski |
0:01f31e923fe2 | 205 | ptr[i] = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 206 | } |
Pawel Zarembski |
0:01f31e923fe2 | 207 | |
Pawel Zarembski |
0:01f31e923fe2 | 208 | /* clear endpoint transfer descriptors */ |
Pawel Zarembski |
0:01f31e923fe2 | 209 | ptr = (uint8_t *)dTDx; |
Pawel Zarembski |
0:01f31e923fe2 | 210 | |
Pawel Zarembski |
0:01f31e923fe2 | 211 | for (i = 0; i < sizeof(dTDx); i++) { |
Pawel Zarembski |
0:01f31e923fe2 | 212 | ptr[i] = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 213 | } |
Pawel Zarembski |
0:01f31e923fe2 | 214 | |
Pawel Zarembski |
0:01f31e923fe2 | 215 | Ep[EP_OUT_IDX(0)].maxPacket = USBD_MAX_PACKET0; |
Pawel Zarembski |
0:01f31e923fe2 | 216 | Ep[EP_OUT_IDX(0)].buf = EPBufPool; |
Pawel Zarembski |
0:01f31e923fe2 | 217 | BufUsed = USBD_MAX_PACKET0; |
Pawel Zarembski |
0:01f31e923fe2 | 218 | Ep[EP_IN_IDX(0)].maxPacket = USBD_MAX_PACKET0; |
Pawel Zarembski |
0:01f31e923fe2 | 219 | Ep[EP_IN_IDX(0)].buf = &(EPBufPool[BufUsed]); |
Pawel Zarembski |
0:01f31e923fe2 | 220 | BufUsed += USBD_MAX_PACKET0; |
Pawel Zarembski |
0:01f31e923fe2 | 221 | dTDx[EP_OUT_IDX(0)].next_dTD = 1; |
Pawel Zarembski |
0:01f31e923fe2 | 222 | dTDx[EP_IN_IDX(0)].next_dTD = 1; |
Pawel Zarembski |
0:01f31e923fe2 | 223 | dTDx[EP_OUT_IDX(0)].dTD_token = (USBD_MAX_PACKET0 << 16) | /* total bytes */ |
Pawel Zarembski |
0:01f31e923fe2 | 224 | (1UL << 15); /* int on compl */ |
Pawel Zarembski |
0:01f31e923fe2 | 225 | dTDx[EP_IN_IDX(0)].dTD_token = (USBD_MAX_PACKET0 << 16) | /* total bytes */ |
Pawel Zarembski |
0:01f31e923fe2 | 226 | (1UL << 15); /* int on compl */ |
Pawel Zarembski |
0:01f31e923fe2 | 227 | EPQHx[EP_OUT_IDX(0)].next_dTD = (uint32_t) &dTDx[EP_OUT_IDX(0)]; |
Pawel Zarembski |
0:01f31e923fe2 | 228 | EPQHx[EP_IN_IDX(0)].next_dTD = (uint32_t) &dTDx[EP_IN_IDX(0)]; |
Pawel Zarembski |
0:01f31e923fe2 | 229 | EPQHx[EP_OUT_IDX(0)].cap = ((USBD_MAX_PACKET0 & 0x0EFF) << 16) | |
Pawel Zarembski |
0:01f31e923fe2 | 230 | (1UL << 29) | |
Pawel Zarembski |
0:01f31e923fe2 | 231 | (1UL << 15); /* int on setup */ |
Pawel Zarembski |
0:01f31e923fe2 | 232 | EPQHx[EP_IN_IDX(0)].cap = (USBD_MAX_PACKET0 << 16) | |
Pawel Zarembski |
0:01f31e923fe2 | 233 | (1UL << 29) | |
Pawel Zarembski |
0:01f31e923fe2 | 234 | (1UL << 15); /* int on setup */ |
Pawel Zarembski |
0:01f31e923fe2 | 235 | USBHS->EPLISTADDR = (uint32_t)EPQHx; |
Pawel Zarembski |
0:01f31e923fe2 | 236 | USBHS->USBMODE |= (1UL << 3); /* Setup lockouts off */ |
Pawel Zarembski |
0:01f31e923fe2 | 237 | USBHS->EPCR0 = 0x00C000C0; |
Pawel Zarembski |
0:01f31e923fe2 | 238 | USBD_PrimeEp(0, Ep[EP_OUT_IDX(0)].maxPacket); |
Pawel Zarembski |
0:01f31e923fe2 | 239 | } |
Pawel Zarembski |
0:01f31e923fe2 | 240 | |
Pawel Zarembski |
0:01f31e923fe2 | 241 | |
Pawel Zarembski |
0:01f31e923fe2 | 242 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 243 | * USB Device Suspend Function |
Pawel Zarembski |
0:01f31e923fe2 | 244 | * Called automatically on USB Device Suspend |
Pawel Zarembski |
0:01f31e923fe2 | 245 | * Return Value: None |
Pawel Zarembski |
0:01f31e923fe2 | 246 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 247 | |
Pawel Zarembski |
0:01f31e923fe2 | 248 | void USBD_Suspend(void) |
Pawel Zarembski |
0:01f31e923fe2 | 249 | { |
Pawel Zarembski |
0:01f31e923fe2 | 250 | /* Performed by Hardware */ |
Pawel Zarembski |
0:01f31e923fe2 | 251 | } |
Pawel Zarembski |
0:01f31e923fe2 | 252 | |
Pawel Zarembski |
0:01f31e923fe2 | 253 | |
Pawel Zarembski |
0:01f31e923fe2 | 254 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 255 | * USB Device Resume Function |
Pawel Zarembski |
0:01f31e923fe2 | 256 | * Called automatically on USB Device Resume |
Pawel Zarembski |
0:01f31e923fe2 | 257 | * Return Value: None |
Pawel Zarembski |
0:01f31e923fe2 | 258 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 259 | |
Pawel Zarembski |
0:01f31e923fe2 | 260 | void USBD_Resume(void) |
Pawel Zarembski |
0:01f31e923fe2 | 261 | { |
Pawel Zarembski |
0:01f31e923fe2 | 262 | /* Performed by Hardware */ |
Pawel Zarembski |
0:01f31e923fe2 | 263 | } |
Pawel Zarembski |
0:01f31e923fe2 | 264 | |
Pawel Zarembski |
0:01f31e923fe2 | 265 | |
Pawel Zarembski |
0:01f31e923fe2 | 266 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 267 | * USB Device Remote Wakeup Function |
Pawel Zarembski |
0:01f31e923fe2 | 268 | * Called automatically on USB Device Remote Wakeup |
Pawel Zarembski |
0:01f31e923fe2 | 269 | * Return Value: None |
Pawel Zarembski |
0:01f31e923fe2 | 270 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 271 | |
Pawel Zarembski |
0:01f31e923fe2 | 272 | void USBD_WakeUp(void) |
Pawel Zarembski |
0:01f31e923fe2 | 273 | { |
Pawel Zarembski |
0:01f31e923fe2 | 274 | USBHS->PORTSC1 |= (1UL << 6); |
Pawel Zarembski |
0:01f31e923fe2 | 275 | } |
Pawel Zarembski |
0:01f31e923fe2 | 276 | |
Pawel Zarembski |
0:01f31e923fe2 | 277 | |
Pawel Zarembski |
0:01f31e923fe2 | 278 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 279 | * USB Device Remote Wakeup Configuration Function |
Pawel Zarembski |
0:01f31e923fe2 | 280 | * Parameters: cfg: Device Enable/Disable |
Pawel Zarembski |
0:01f31e923fe2 | 281 | * Return Value: None |
Pawel Zarembski |
0:01f31e923fe2 | 282 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 283 | |
Pawel Zarembski |
0:01f31e923fe2 | 284 | void USBD_WakeUpCfg(uint32_t cfg) |
Pawel Zarembski |
0:01f31e923fe2 | 285 | { |
Pawel Zarembski |
0:01f31e923fe2 | 286 | /* Not needed */ |
Pawel Zarembski |
0:01f31e923fe2 | 287 | } |
Pawel Zarembski |
0:01f31e923fe2 | 288 | |
Pawel Zarembski |
0:01f31e923fe2 | 289 | |
Pawel Zarembski |
0:01f31e923fe2 | 290 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 291 | * USB Device Set Address Function |
Pawel Zarembski |
0:01f31e923fe2 | 292 | * Parameters: adr: USB Device Address |
Pawel Zarembski |
0:01f31e923fe2 | 293 | * Return Value: None |
Pawel Zarembski |
0:01f31e923fe2 | 294 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 295 | |
Pawel Zarembski |
0:01f31e923fe2 | 296 | void USBD_SetAddress(uint32_t adr, uint32_t setup) |
Pawel Zarembski |
0:01f31e923fe2 | 297 | { |
Pawel Zarembski |
0:01f31e923fe2 | 298 | if (setup == 0) { |
Pawel Zarembski |
0:01f31e923fe2 | 299 | USBHS->DEVICEADDR = (adr << 25); |
Pawel Zarembski |
0:01f31e923fe2 | 300 | USBHS->DEVICEADDR |= (1UL << 24); |
Pawel Zarembski |
0:01f31e923fe2 | 301 | } |
Pawel Zarembski |
0:01f31e923fe2 | 302 | } |
Pawel Zarembski |
0:01f31e923fe2 | 303 | |
Pawel Zarembski |
0:01f31e923fe2 | 304 | |
Pawel Zarembski |
0:01f31e923fe2 | 305 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 306 | * USB Device Configure Function |
Pawel Zarembski |
0:01f31e923fe2 | 307 | * Parameters: cfg: Device Configure/Deconfigure |
Pawel Zarembski |
0:01f31e923fe2 | 308 | * Return Value: None |
Pawel Zarembski |
0:01f31e923fe2 | 309 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 310 | |
Pawel Zarembski |
0:01f31e923fe2 | 311 | void USBD_Configure(uint32_t cfg) |
Pawel Zarembski |
0:01f31e923fe2 | 312 | { |
Pawel Zarembski |
0:01f31e923fe2 | 313 | uint32_t i; |
Pawel Zarembski |
0:01f31e923fe2 | 314 | |
Pawel Zarembski |
0:01f31e923fe2 | 315 | if (!cfg) { |
Pawel Zarembski |
0:01f31e923fe2 | 316 | for (i = 2; i < (2 * (USBD_EP_NUM + 1)); i++) { |
Pawel Zarembski |
0:01f31e923fe2 | 317 | Ep[i].buf = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 318 | Ep[i].maxPacket = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 319 | } |
Pawel Zarembski |
0:01f31e923fe2 | 320 | |
Pawel Zarembski |
0:01f31e923fe2 | 321 | BufUsed = 2 * USBD_MAX_PACKET0; |
Pawel Zarembski |
0:01f31e923fe2 | 322 | } |
Pawel Zarembski |
0:01f31e923fe2 | 323 | } |
Pawel Zarembski |
0:01f31e923fe2 | 324 | |
Pawel Zarembski |
0:01f31e923fe2 | 325 | |
Pawel Zarembski |
0:01f31e923fe2 | 326 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 327 | * Configure USB Device Endpoint according to Descriptor |
Pawel Zarembski |
0:01f31e923fe2 | 328 | * Parameters: pEPD: Pointer to Device Endpoint Descriptor |
Pawel Zarembski |
0:01f31e923fe2 | 329 | * Return Value: None |
Pawel Zarembski |
0:01f31e923fe2 | 330 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 331 | |
Pawel Zarembski |
0:01f31e923fe2 | 332 | void USBD_ConfigEP(USB_ENDPOINT_DESCRIPTOR *pEPD) |
Pawel Zarembski |
0:01f31e923fe2 | 333 | { |
Pawel Zarembski |
0:01f31e923fe2 | 334 | uint32_t num, val, type, idx; |
Pawel Zarembski |
0:01f31e923fe2 | 335 | |
Pawel Zarembski |
0:01f31e923fe2 | 336 | if ((pEPD->bEndpointAddress & USB_ENDPOINT_DIRECTION_MASK)) { |
Pawel Zarembski |
0:01f31e923fe2 | 337 | val = 16; |
Pawel Zarembski |
0:01f31e923fe2 | 338 | num = pEPD->bEndpointAddress & ~0x80; |
Pawel Zarembski |
0:01f31e923fe2 | 339 | idx = EP_IN_IDX(num); |
Pawel Zarembski |
0:01f31e923fe2 | 340 | |
Pawel Zarembski |
0:01f31e923fe2 | 341 | } else { |
Pawel Zarembski |
0:01f31e923fe2 | 342 | val = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 343 | num = pEPD->bEndpointAddress; |
Pawel Zarembski |
0:01f31e923fe2 | 344 | idx = EP_OUT_IDX(num); |
Pawel Zarembski |
0:01f31e923fe2 | 345 | } |
Pawel Zarembski |
0:01f31e923fe2 | 346 | |
Pawel Zarembski |
0:01f31e923fe2 | 347 | type = pEPD->bmAttributes & USB_ENDPOINT_TYPE_MASK; |
Pawel Zarembski |
0:01f31e923fe2 | 348 | |
Pawel Zarembski |
0:01f31e923fe2 | 349 | if (!(Ep[idx].buf)) { |
Pawel Zarembski |
0:01f31e923fe2 | 350 | Ep[idx].buf = &(EPBufPool[BufUsed]); |
Pawel Zarembski |
0:01f31e923fe2 | 351 | Ep[idx].maxPacket = pEPD->wMaxPacketSize; |
Pawel Zarembski |
0:01f31e923fe2 | 352 | BufUsed += pEPD->wMaxPacketSize; |
Pawel Zarembski |
0:01f31e923fe2 | 353 | |
Pawel Zarembski |
0:01f31e923fe2 | 354 | /* Isochronous endpoint */ |
Pawel Zarembski |
0:01f31e923fe2 | 355 | if (type == USB_ENDPOINT_TYPE_ISOCHRONOUS) { |
Pawel Zarembski |
0:01f31e923fe2 | 356 | IsoEp |= (1UL << (num + val)); |
Pawel Zarembski |
0:01f31e923fe2 | 357 | } |
Pawel Zarembski |
0:01f31e923fe2 | 358 | } |
Pawel Zarembski |
0:01f31e923fe2 | 359 | |
Pawel Zarembski |
0:01f31e923fe2 | 360 | dTDx[idx].buf[0] = (uint32_t)(Ep[idx].buf); |
Pawel Zarembski |
0:01f31e923fe2 | 361 | dTDx[idx].next_dTD = 1; |
Pawel Zarembski |
0:01f31e923fe2 | 362 | EPQHx[idx].cap = (Ep[idx].maxPacket << 16) | |
Pawel Zarembski |
0:01f31e923fe2 | 363 | (1UL << 29); |
Pawel Zarembski |
0:01f31e923fe2 | 364 | ENDPTCTRL(num) &= ~(0xFFFF << val); |
Pawel Zarembski |
0:01f31e923fe2 | 365 | ENDPTCTRL(num) |= ((type << 2) << val) | |
Pawel Zarembski |
0:01f31e923fe2 | 366 | ((1UL << 6) << val); /* Data toogle reset */ |
Pawel Zarembski |
0:01f31e923fe2 | 367 | } |
Pawel Zarembski |
0:01f31e923fe2 | 368 | |
Pawel Zarembski |
0:01f31e923fe2 | 369 | |
Pawel Zarembski |
0:01f31e923fe2 | 370 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 371 | * Set Direction for USB Device Control Endpoint |
Pawel Zarembski |
0:01f31e923fe2 | 372 | * Parameters: dir: Out (dir == 0), In (dir <> 0) |
Pawel Zarembski |
0:01f31e923fe2 | 373 | * Return Value: None |
Pawel Zarembski |
0:01f31e923fe2 | 374 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 375 | |
Pawel Zarembski |
0:01f31e923fe2 | 376 | void USBD_DirCtrlEP(uint32_t dir) |
Pawel Zarembski |
0:01f31e923fe2 | 377 | { |
Pawel Zarembski |
0:01f31e923fe2 | 378 | /* Not needed */ |
Pawel Zarembski |
0:01f31e923fe2 | 379 | } |
Pawel Zarembski |
0:01f31e923fe2 | 380 | |
Pawel Zarembski |
0:01f31e923fe2 | 381 | |
Pawel Zarembski |
0:01f31e923fe2 | 382 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 383 | * Enable USB Device Endpoint |
Pawel Zarembski |
0:01f31e923fe2 | 384 | * Parameters: EPNum: Device Endpoint Number |
Pawel Zarembski |
0:01f31e923fe2 | 385 | * EPNum.0..3: Address |
Pawel Zarembski |
0:01f31e923fe2 | 386 | * EPNum.7: Dir |
Pawel Zarembski |
0:01f31e923fe2 | 387 | * Return Value: None |
Pawel Zarembski |
0:01f31e923fe2 | 388 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 389 | |
Pawel Zarembski |
0:01f31e923fe2 | 390 | void USBD_EnableEP(uint32_t EPNum) |
Pawel Zarembski |
0:01f31e923fe2 | 391 | { |
Pawel Zarembski |
0:01f31e923fe2 | 392 | if (EPNum & 0x80) { |
Pawel Zarembski |
0:01f31e923fe2 | 393 | EPNum &= 0x7F; |
Pawel Zarembski |
0:01f31e923fe2 | 394 | ENDPTCTRL(EPNum) |= (1UL << 23); /* EP enabled */ |
Pawel Zarembski |
0:01f31e923fe2 | 395 | } else { |
Pawel Zarembski |
0:01f31e923fe2 | 396 | ENDPTCTRL(EPNum) |= (1UL << 7); /* EP enabled */ |
Pawel Zarembski |
0:01f31e923fe2 | 397 | } |
Pawel Zarembski |
0:01f31e923fe2 | 398 | } |
Pawel Zarembski |
0:01f31e923fe2 | 399 | |
Pawel Zarembski |
0:01f31e923fe2 | 400 | |
Pawel Zarembski |
0:01f31e923fe2 | 401 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 402 | * Disable USB Device Endpoint |
Pawel Zarembski |
0:01f31e923fe2 | 403 | * Parameters: EPNum: Device Endpoint Number |
Pawel Zarembski |
0:01f31e923fe2 | 404 | * EPNum.0..3: Address |
Pawel Zarembski |
0:01f31e923fe2 | 405 | * EPNum.7: Dir |
Pawel Zarembski |
0:01f31e923fe2 | 406 | * Return Value: None |
Pawel Zarembski |
0:01f31e923fe2 | 407 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 408 | |
Pawel Zarembski |
0:01f31e923fe2 | 409 | void USBD_DisableEP(uint32_t EPNum) |
Pawel Zarembski |
0:01f31e923fe2 | 410 | { |
Pawel Zarembski |
0:01f31e923fe2 | 411 | if (EPNum & 0x80) { |
Pawel Zarembski |
0:01f31e923fe2 | 412 | EPNum &= 0x7F; |
Pawel Zarembski |
0:01f31e923fe2 | 413 | ENDPTCTRL(EPNum) &= ~(1UL << 23); /* EP disabled */ |
Pawel Zarembski |
0:01f31e923fe2 | 414 | } else { |
Pawel Zarembski |
0:01f31e923fe2 | 415 | ENDPTCTRL(EPNum) &= ~(1UL << 7); /* EP disabled */ |
Pawel Zarembski |
0:01f31e923fe2 | 416 | } |
Pawel Zarembski |
0:01f31e923fe2 | 417 | } |
Pawel Zarembski |
0:01f31e923fe2 | 418 | |
Pawel Zarembski |
0:01f31e923fe2 | 419 | |
Pawel Zarembski |
0:01f31e923fe2 | 420 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 421 | * Reset USB Device Endpoint |
Pawel Zarembski |
0:01f31e923fe2 | 422 | * Parameters: EPNum: Device Endpoint Number |
Pawel Zarembski |
0:01f31e923fe2 | 423 | * EPNum.0..3: Address |
Pawel Zarembski |
0:01f31e923fe2 | 424 | * EPNum.7: Dir |
Pawel Zarembski |
0:01f31e923fe2 | 425 | * Return Value: None |
Pawel Zarembski |
0:01f31e923fe2 | 426 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 427 | |
Pawel Zarembski |
0:01f31e923fe2 | 428 | void USBD_ResetEP(uint32_t EPNum) |
Pawel Zarembski |
0:01f31e923fe2 | 429 | { |
Pawel Zarembski |
0:01f31e923fe2 | 430 | if (EPNum & 0x80) { |
Pawel Zarembski |
0:01f31e923fe2 | 431 | EPNum &= 0x7F; |
Pawel Zarembski |
0:01f31e923fe2 | 432 | EPQHx[EP_IN_IDX(EPNum)].dTD_token &= 0xC0; |
Pawel Zarembski |
0:01f31e923fe2 | 433 | USBHS->EPFLUSH = (1UL << (EPNum + 16)); /* flush endpoint */ |
Pawel Zarembski |
0:01f31e923fe2 | 434 | |
Pawel Zarembski |
0:01f31e923fe2 | 435 | while (USBHS->EPFLUSH & (1UL << (EPNum + 16))); |
Pawel Zarembski |
0:01f31e923fe2 | 436 | |
Pawel Zarembski |
0:01f31e923fe2 | 437 | ENDPTCTRL(EPNum) |= (1UL << 22); /* data toggle reset */ |
Pawel Zarembski |
0:01f31e923fe2 | 438 | |
Pawel Zarembski |
0:01f31e923fe2 | 439 | } else { |
Pawel Zarembski |
0:01f31e923fe2 | 440 | EPQHx[EP_OUT_IDX(EPNum)].dTD_token &= 0xC0; |
Pawel Zarembski |
0:01f31e923fe2 | 441 | USBHS->EPFLUSH = (1UL << EPNum); /* flush endpoint */ |
Pawel Zarembski |
0:01f31e923fe2 | 442 | |
Pawel Zarembski |
0:01f31e923fe2 | 443 | while (USBHS->EPFLUSH & (1UL << EPNum)); |
Pawel Zarembski |
0:01f31e923fe2 | 444 | |
Pawel Zarembski |
0:01f31e923fe2 | 445 | ENDPTCTRL(EPNum) |= (1UL << 6); /* data toggle reset */ |
Pawel Zarembski |
0:01f31e923fe2 | 446 | USBD_PrimeEp(EPNum, Ep[EP_OUT_IDX(EPNum)].maxPacket); |
Pawel Zarembski |
0:01f31e923fe2 | 447 | } |
Pawel Zarembski |
0:01f31e923fe2 | 448 | } |
Pawel Zarembski |
0:01f31e923fe2 | 449 | |
Pawel Zarembski |
0:01f31e923fe2 | 450 | |
Pawel Zarembski |
0:01f31e923fe2 | 451 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 452 | * Set Stall for USB Device Endpoint |
Pawel Zarembski |
0:01f31e923fe2 | 453 | * Parameters: EPNum: Device Endpoint Number |
Pawel Zarembski |
0:01f31e923fe2 | 454 | * EPNum.0..3: Address |
Pawel Zarembski |
0:01f31e923fe2 | 455 | * EPNum.7: Dir |
Pawel Zarembski |
0:01f31e923fe2 | 456 | * Return Value: None |
Pawel Zarembski |
0:01f31e923fe2 | 457 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 458 | |
Pawel Zarembski |
0:01f31e923fe2 | 459 | void USBD_SetStallEP(uint32_t EPNum) |
Pawel Zarembski |
0:01f31e923fe2 | 460 | { |
Pawel Zarembski |
0:01f31e923fe2 | 461 | if (EPNum & 0x80) { |
Pawel Zarembski |
0:01f31e923fe2 | 462 | EPNum &= 0x7F; |
Pawel Zarembski |
0:01f31e923fe2 | 463 | ENDPTCTRL(EPNum) |= (1UL << 16); /* IN endpoint stall */ |
Pawel Zarembski |
0:01f31e923fe2 | 464 | } else { |
Pawel Zarembski |
0:01f31e923fe2 | 465 | ENDPTCTRL(EPNum) |= (1UL << 0); /* OUT endpoint stall */ |
Pawel Zarembski |
0:01f31e923fe2 | 466 | } |
Pawel Zarembski |
0:01f31e923fe2 | 467 | } |
Pawel Zarembski |
0:01f31e923fe2 | 468 | |
Pawel Zarembski |
0:01f31e923fe2 | 469 | |
Pawel Zarembski |
0:01f31e923fe2 | 470 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 471 | * Clear Stall for USB Device Endpoint |
Pawel Zarembski |
0:01f31e923fe2 | 472 | * Parameters: EPNum: Device Endpoint Number |
Pawel Zarembski |
0:01f31e923fe2 | 473 | * EPNum.0..3: Address |
Pawel Zarembski |
0:01f31e923fe2 | 474 | * EPNum.7: Dir |
Pawel Zarembski |
0:01f31e923fe2 | 475 | * Return Value: None |
Pawel Zarembski |
0:01f31e923fe2 | 476 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 477 | |
Pawel Zarembski |
0:01f31e923fe2 | 478 | void USBD_ClrStallEP(uint32_t EPNum) |
Pawel Zarembski |
0:01f31e923fe2 | 479 | { |
Pawel Zarembski |
0:01f31e923fe2 | 480 | if (EPNum & 0x80) { |
Pawel Zarembski |
0:01f31e923fe2 | 481 | EPNum &= 0x7F; |
Pawel Zarembski |
0:01f31e923fe2 | 482 | ENDPTCTRL(EPNum) &= ~(1UL << 16); /* clear stall */ |
Pawel Zarembski |
0:01f31e923fe2 | 483 | ENDPTCTRL(EPNum) |= (1UL << 22); /* data toggle reset */ |
Pawel Zarembski |
0:01f31e923fe2 | 484 | |
Pawel Zarembski |
0:01f31e923fe2 | 485 | while (ENDPTCTRL(EPNum) & (1UL << 16)); |
Pawel Zarembski |
0:01f31e923fe2 | 486 | |
Pawel Zarembski |
0:01f31e923fe2 | 487 | USBD_ResetEP(EPNum | 0x80); |
Pawel Zarembski |
0:01f31e923fe2 | 488 | |
Pawel Zarembski |
0:01f31e923fe2 | 489 | } else { |
Pawel Zarembski |
0:01f31e923fe2 | 490 | ENDPTCTRL(EPNum) &= ~(1UL << 0); /* clear stall */ |
Pawel Zarembski |
0:01f31e923fe2 | 491 | ENDPTCTRL(EPNum) |= (1UL << 6); /* data toggle reset */ |
Pawel Zarembski |
0:01f31e923fe2 | 492 | } |
Pawel Zarembski |
0:01f31e923fe2 | 493 | } |
Pawel Zarembski |
0:01f31e923fe2 | 494 | |
Pawel Zarembski |
0:01f31e923fe2 | 495 | |
Pawel Zarembski |
0:01f31e923fe2 | 496 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 497 | * Clear USB Device Endpoint Buffer |
Pawel Zarembski |
0:01f31e923fe2 | 498 | * Parameters: EPNum: Device Endpoint Number |
Pawel Zarembski |
0:01f31e923fe2 | 499 | * EPNum.0..3: Address |
Pawel Zarembski |
0:01f31e923fe2 | 500 | * EPNum.7: Dir |
Pawel Zarembski |
0:01f31e923fe2 | 501 | * Return Value: None |
Pawel Zarembski |
0:01f31e923fe2 | 502 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 503 | |
Pawel Zarembski |
0:01f31e923fe2 | 504 | void USBD_ClearEPBuf(uint32_t EPNum) |
Pawel Zarembski |
0:01f31e923fe2 | 505 | { |
Pawel Zarembski |
0:01f31e923fe2 | 506 | } |
Pawel Zarembski |
0:01f31e923fe2 | 507 | |
Pawel Zarembski |
0:01f31e923fe2 | 508 | |
Pawel Zarembski |
0:01f31e923fe2 | 509 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 510 | * USB Device Prime endpoint function |
Pawel Zarembski |
0:01f31e923fe2 | 511 | * Parameters: EPNum: Device Endpoint Number |
Pawel Zarembski |
0:01f31e923fe2 | 512 | * EPNum.0..3: Address |
Pawel Zarembski |
0:01f31e923fe2 | 513 | * EPNum.7: Dir |
Pawel Zarembski |
0:01f31e923fe2 | 514 | * cnt: Bytes to transfer/receive |
Pawel Zarembski |
0:01f31e923fe2 | 515 | * Return Value: None |
Pawel Zarembski |
0:01f31e923fe2 | 516 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 517 | |
Pawel Zarembski |
0:01f31e923fe2 | 518 | void USBD_PrimeEp(uint32_t EPNum, uint32_t cnt) |
Pawel Zarembski |
0:01f31e923fe2 | 519 | { |
Pawel Zarembski |
0:01f31e923fe2 | 520 | uint32_t idx, val; |
Pawel Zarembski |
0:01f31e923fe2 | 521 | |
Pawel Zarembski |
0:01f31e923fe2 | 522 | /* IN endpoint */ |
Pawel Zarembski |
0:01f31e923fe2 | 523 | if (EPNum & 0x80) { |
Pawel Zarembski |
0:01f31e923fe2 | 524 | EPNum &= 0x7F; |
Pawel Zarembski |
0:01f31e923fe2 | 525 | idx = EP_IN_IDX(EPNum); |
Pawel Zarembski |
0:01f31e923fe2 | 526 | val = (1UL << (EPNum + 16)); |
Pawel Zarembski |
0:01f31e923fe2 | 527 | } |
Pawel Zarembski |
0:01f31e923fe2 | 528 | |
Pawel Zarembski |
0:01f31e923fe2 | 529 | /* OUT endpoint */ |
Pawel Zarembski |
0:01f31e923fe2 | 530 | else { |
Pawel Zarembski |
0:01f31e923fe2 | 531 | val = (1UL << EPNum); |
Pawel Zarembski |
0:01f31e923fe2 | 532 | idx = EP_OUT_IDX(EPNum); |
Pawel Zarembski |
0:01f31e923fe2 | 533 | } |
Pawel Zarembski |
0:01f31e923fe2 | 534 | |
Pawel Zarembski |
0:01f31e923fe2 | 535 | dTDx[idx].buf[0] = (uint32_t)(Ep[idx].buf); |
Pawel Zarembski |
0:01f31e923fe2 | 536 | dTDx[idx].next_dTD = 1; |
Pawel Zarembski |
0:01f31e923fe2 | 537 | |
Pawel Zarembski |
0:01f31e923fe2 | 538 | if (IsoEp & val) { |
Pawel Zarembski |
0:01f31e923fe2 | 539 | if (Ep[idx].maxPacket <= cnt) { |
Pawel Zarembski |
0:01f31e923fe2 | 540 | dTDx[idx].dTD_token = (1 << 10); /* MultO = 1 */ |
Pawel Zarembski |
0:01f31e923fe2 | 541 | |
Pawel Zarembski |
0:01f31e923fe2 | 542 | } else if ((Ep[idx].maxPacket * 2) <= cnt) { |
Pawel Zarembski |
0:01f31e923fe2 | 543 | dTDx[idx].dTD_token = (2 << 10); /* MultO = 2 */ |
Pawel Zarembski |
0:01f31e923fe2 | 544 | |
Pawel Zarembski |
0:01f31e923fe2 | 545 | } else { |
Pawel Zarembski |
0:01f31e923fe2 | 546 | dTDx[idx].dTD_token = (3 << 10); /* MultO = 3 */ |
Pawel Zarembski |
0:01f31e923fe2 | 547 | } |
Pawel Zarembski |
0:01f31e923fe2 | 548 | |
Pawel Zarembski |
0:01f31e923fe2 | 549 | } else { |
Pawel Zarembski |
0:01f31e923fe2 | 550 | dTDx[idx].dTD_token = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 551 | } |
Pawel Zarembski |
0:01f31e923fe2 | 552 | |
Pawel Zarembski |
0:01f31e923fe2 | 553 | dTDx[idx].dTD_token |= (cnt << 16) | /* bytes to transfer */ |
Pawel Zarembski |
0:01f31e923fe2 | 554 | (1UL << 15) | /* int on complete */ |
Pawel Zarembski |
0:01f31e923fe2 | 555 | 0x80; /* status - active */ |
Pawel Zarembski |
0:01f31e923fe2 | 556 | EPQHx[idx].next_dTD = (uint32_t)(&dTDx[idx]); |
Pawel Zarembski |
0:01f31e923fe2 | 557 | EPQHx[idx].dTD_token &= ~0xC0; |
Pawel Zarembski |
0:01f31e923fe2 | 558 | USBHS->EPPRIME = (val); |
Pawel Zarembski |
0:01f31e923fe2 | 559 | |
Pawel Zarembski |
0:01f31e923fe2 | 560 | while ((USBHS->EPPRIME & val)); |
Pawel Zarembski |
0:01f31e923fe2 | 561 | } |
Pawel Zarembski |
0:01f31e923fe2 | 562 | |
Pawel Zarembski |
0:01f31e923fe2 | 563 | |
Pawel Zarembski |
0:01f31e923fe2 | 564 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 565 | * Read USB Device Endpoint Data |
Pawel Zarembski |
0:01f31e923fe2 | 566 | * Parameters: EPNum: Device Endpoint Number |
Pawel Zarembski |
0:01f31e923fe2 | 567 | * EPNum.0..3: Address |
Pawel Zarembski |
0:01f31e923fe2 | 568 | * EPNum.7: Dir |
Pawel Zarembski |
0:01f31e923fe2 | 569 | * pData: Pointer to Data Buffer |
Pawel Zarembski |
0:01f31e923fe2 | 570 | * Return Value: Number of bytes read |
Pawel Zarembski |
0:01f31e923fe2 | 571 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 572 | |
Pawel Zarembski |
0:01f31e923fe2 | 573 | uint32_t USBD_ReadEP(uint32_t EPNum, uint8_t *pData, uint32_t size) |
Pawel Zarembski |
0:01f31e923fe2 | 574 | { |
Pawel Zarembski |
0:01f31e923fe2 | 575 | uint32_t cnt = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 576 | uint32_t i; |
Pawel Zarembski |
0:01f31e923fe2 | 577 | |
Pawel Zarembski |
0:01f31e923fe2 | 578 | /* Setup packet */ |
Pawel Zarembski |
0:01f31e923fe2 | 579 | if ((USBHS->EPSETUPSR & 1) && (!EPNum)) { |
Pawel Zarembski |
0:01f31e923fe2 | 580 | USBHS->EPSETUPSR = 1; |
Pawel Zarembski |
0:01f31e923fe2 | 581 | |
Pawel Zarembski |
0:01f31e923fe2 | 582 | while (USBHS->EPSETUPSR & 1); |
Pawel Zarembski |
0:01f31e923fe2 | 583 | |
Pawel Zarembski |
0:01f31e923fe2 | 584 | do { |
Pawel Zarembski |
0:01f31e923fe2 | 585 | *((__packed uint32_t *) pData) = EPQHx[EP_OUT_IDX(0)].setup[0]; |
Pawel Zarembski |
0:01f31e923fe2 | 586 | *((__packed uint32_t *)(pData + 4)) = EPQHx[EP_OUT_IDX(0)].setup[1]; |
Pawel Zarembski |
0:01f31e923fe2 | 587 | cnt = 8; |
Pawel Zarembski |
0:01f31e923fe2 | 588 | USBHS->USBCMD |= (1UL << 13); |
Pawel Zarembski |
0:01f31e923fe2 | 589 | } while (!(USBHS->USBCMD & (1UL << 13))); |
Pawel Zarembski |
0:01f31e923fe2 | 590 | |
Pawel Zarembski |
0:01f31e923fe2 | 591 | USBHS->USBCMD &= (~(1UL << 13)); |
Pawel Zarembski |
0:01f31e923fe2 | 592 | USBHS->EPFLUSH = (1UL << EPNum) | (1UL << (EPNum + 16)); |
Pawel Zarembski |
0:01f31e923fe2 | 593 | |
Pawel Zarembski |
0:01f31e923fe2 | 594 | while (USBHS->EPFLUSH & ((1UL << (EPNum + 16)) | (1UL << EPNum))); |
Pawel Zarembski |
0:01f31e923fe2 | 595 | |
Pawel Zarembski |
0:01f31e923fe2 | 596 | while (USBHS->EPSETUPSR & 1); |
Pawel Zarembski |
0:01f31e923fe2 | 597 | |
Pawel Zarembski |
0:01f31e923fe2 | 598 | USBD_PrimeEp(EPNum, Ep[EP_OUT_IDX(EPNum)].maxPacket); |
Pawel Zarembski |
0:01f31e923fe2 | 599 | } |
Pawel Zarembski |
0:01f31e923fe2 | 600 | |
Pawel Zarembski |
0:01f31e923fe2 | 601 | /* OUT Packet */ |
Pawel Zarembski |
0:01f31e923fe2 | 602 | else { |
Pawel Zarembski |
0:01f31e923fe2 | 603 | if (Ep[EP_OUT_IDX(EPNum)].buf) { |
Pawel Zarembski |
0:01f31e923fe2 | 604 | cnt = Ep[EP_OUT_IDX(EPNum)].maxPacket - |
Pawel Zarembski |
0:01f31e923fe2 | 605 | ((dTDx[EP_OUT_IDX(EPNum)].dTD_token >> 16) & 0x7FFF); |
Pawel Zarembski |
0:01f31e923fe2 | 606 | |
Pawel Zarembski |
0:01f31e923fe2 | 607 | for (i = 0; i < cnt; i++) { |
Pawel Zarembski |
0:01f31e923fe2 | 608 | pData[i] = Ep[EP_OUT_IDX(EPNum)].buf[i]; |
Pawel Zarembski |
0:01f31e923fe2 | 609 | } |
Pawel Zarembski |
0:01f31e923fe2 | 610 | } |
Pawel Zarembski |
0:01f31e923fe2 | 611 | |
Pawel Zarembski |
0:01f31e923fe2 | 612 | USBHS->EPCOMPLETE = (1UL << EPNum); |
Pawel Zarembski |
0:01f31e923fe2 | 613 | cmpl_pnd &= ~(1UL << EPNum); |
Pawel Zarembski |
0:01f31e923fe2 | 614 | USBD_PrimeEp(EPNum, Ep[EP_OUT_IDX(EPNum)].maxPacket); |
Pawel Zarembski |
0:01f31e923fe2 | 615 | } |
Pawel Zarembski |
0:01f31e923fe2 | 616 | |
Pawel Zarembski |
0:01f31e923fe2 | 617 | return (cnt); |
Pawel Zarembski |
0:01f31e923fe2 | 618 | } |
Pawel Zarembski |
0:01f31e923fe2 | 619 | |
Pawel Zarembski |
0:01f31e923fe2 | 620 | |
Pawel Zarembski |
0:01f31e923fe2 | 621 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 622 | * Write USB Device Endpoint Data |
Pawel Zarembski |
0:01f31e923fe2 | 623 | * Parameters: EPNum: Endpoint Number |
Pawel Zarembski |
0:01f31e923fe2 | 624 | * EPNum.0..3: Address |
Pawel Zarembski |
0:01f31e923fe2 | 625 | * EPNum.7: Dir |
Pawel Zarembski |
0:01f31e923fe2 | 626 | * pData: Pointer to Data Buffer |
Pawel Zarembski |
0:01f31e923fe2 | 627 | * cnt: Number of bytes to write |
Pawel Zarembski |
0:01f31e923fe2 | 628 | * Return Value: Number of bytes written |
Pawel Zarembski |
0:01f31e923fe2 | 629 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 630 | |
Pawel Zarembski |
0:01f31e923fe2 | 631 | uint32_t USBD_WriteEP(uint32_t EPNum, uint8_t *pData, uint32_t cnt) |
Pawel Zarembski |
0:01f31e923fe2 | 632 | { |
Pawel Zarembski |
0:01f31e923fe2 | 633 | uint32_t i; |
Pawel Zarembski |
0:01f31e923fe2 | 634 | EPNum &= 0x7f; |
Pawel Zarembski |
0:01f31e923fe2 | 635 | |
Pawel Zarembski |
0:01f31e923fe2 | 636 | for (i = 0; i < cnt; i++) { |
Pawel Zarembski |
0:01f31e923fe2 | 637 | Ep[EP_IN_IDX(EPNum)].buf[i] = pData[i]; |
Pawel Zarembski |
0:01f31e923fe2 | 638 | } |
Pawel Zarembski |
0:01f31e923fe2 | 639 | |
Pawel Zarembski |
0:01f31e923fe2 | 640 | USBD_PrimeEp(EPNum | 0x80, cnt); |
Pawel Zarembski |
0:01f31e923fe2 | 641 | return (cnt); |
Pawel Zarembski |
0:01f31e923fe2 | 642 | } |
Pawel Zarembski |
0:01f31e923fe2 | 643 | |
Pawel Zarembski |
0:01f31e923fe2 | 644 | |
Pawel Zarembski |
0:01f31e923fe2 | 645 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 646 | * Get USB Device Last Frame Number |
Pawel Zarembski |
0:01f31e923fe2 | 647 | * Parameters: None |
Pawel Zarembski |
0:01f31e923fe2 | 648 | * Return Value: Frame Number |
Pawel Zarembski |
0:01f31e923fe2 | 649 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 650 | |
Pawel Zarembski |
0:01f31e923fe2 | 651 | uint32_t USBD_GetFrame(void) |
Pawel Zarembski |
0:01f31e923fe2 | 652 | { |
Pawel Zarembski |
0:01f31e923fe2 | 653 | return ((USBHS->FRINDEX >> 3) & 0x0FFF); |
Pawel Zarembski |
0:01f31e923fe2 | 654 | } |
Pawel Zarembski |
0:01f31e923fe2 | 655 | |
Pawel Zarembski |
0:01f31e923fe2 | 656 | |
Pawel Zarembski |
0:01f31e923fe2 | 657 | #ifdef __RTX |
Pawel Zarembski |
0:01f31e923fe2 | 658 | uint32_t LastError; /* Last Error */ |
Pawel Zarembski |
0:01f31e923fe2 | 659 | |
Pawel Zarembski |
0:01f31e923fe2 | 660 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 661 | * Get USB Device Last Error Code |
Pawel Zarembski |
0:01f31e923fe2 | 662 | * Parameters: None |
Pawel Zarembski |
0:01f31e923fe2 | 663 | * Return Value: Error Code |
Pawel Zarembski |
0:01f31e923fe2 | 664 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 665 | |
Pawel Zarembski |
0:01f31e923fe2 | 666 | uint32_t USBD_GetError(void) |
Pawel Zarembski |
0:01f31e923fe2 | 667 | { |
Pawel Zarembski |
0:01f31e923fe2 | 668 | return (LastError); |
Pawel Zarembski |
0:01f31e923fe2 | 669 | } |
Pawel Zarembski |
0:01f31e923fe2 | 670 | #endif |
Pawel Zarembski |
0:01f31e923fe2 | 671 | |
Pawel Zarembski |
0:01f31e923fe2 | 672 | |
Pawel Zarembski |
0:01f31e923fe2 | 673 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 674 | * USB Device Interrupt Service Routine |
Pawel Zarembski |
0:01f31e923fe2 | 675 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 676 | void USBHS_IRQHandler(void) |
Pawel Zarembski |
0:01f31e923fe2 | 677 | { |
Pawel Zarembski |
0:01f31e923fe2 | 678 | NVIC_DisableIRQ(USBHS_IRQn); |
Pawel Zarembski |
0:01f31e923fe2 | 679 | USBD_SignalHandler(); |
Pawel Zarembski |
0:01f31e923fe2 | 680 | } |
Pawel Zarembski |
0:01f31e923fe2 | 681 | |
Pawel Zarembski |
0:01f31e923fe2 | 682 | /* |
Pawel Zarembski |
0:01f31e923fe2 | 683 | * USB Device Interrupt Service Routine |
Pawel Zarembski |
0:01f31e923fe2 | 684 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 685 | |
Pawel Zarembski |
0:01f31e923fe2 | 686 | void USBD_Handler(void) |
Pawel Zarembski |
0:01f31e923fe2 | 687 | { |
Pawel Zarembski |
0:01f31e923fe2 | 688 | uint32_t sts, cmpl, num; |
Pawel Zarembski |
0:01f31e923fe2 | 689 | sts = USBHS->USBSTS & USBHS->USBINTR; |
Pawel Zarembski |
0:01f31e923fe2 | 690 | cmpl = USBHS->EPCOMPLETE; |
Pawel Zarembski |
0:01f31e923fe2 | 691 | USBHS->USBSTS = sts; /* clear interupt flags */ |
Pawel Zarembski |
0:01f31e923fe2 | 692 | |
Pawel Zarembski |
0:01f31e923fe2 | 693 | /* reset interrupt */ |
Pawel Zarembski |
0:01f31e923fe2 | 694 | if (sts & (1UL << 6)) { |
Pawel Zarembski |
0:01f31e923fe2 | 695 | USBD_Reset(); |
Pawel Zarembski |
0:01f31e923fe2 | 696 | usbd_reset_core(); |
Pawel Zarembski |
0:01f31e923fe2 | 697 | #ifdef __RTX |
Pawel Zarembski |
0:01f31e923fe2 | 698 | |
Pawel Zarembski |
0:01f31e923fe2 | 699 | if (USBD_RTX_DevTask) { |
Pawel Zarembski |
0:01f31e923fe2 | 700 | isr_evt_set(USBD_EVT_RESET, USBD_RTX_DevTask); |
Pawel Zarembski |
0:01f31e923fe2 | 701 | } |
Pawel Zarembski |
0:01f31e923fe2 | 702 | |
Pawel Zarembski |
0:01f31e923fe2 | 703 | #else |
Pawel Zarembski |
0:01f31e923fe2 | 704 | |
Pawel Zarembski |
0:01f31e923fe2 | 705 | if (USBD_P_Reset_Event) { |
Pawel Zarembski |
0:01f31e923fe2 | 706 | USBD_P_Reset_Event(); |
Pawel Zarembski |
0:01f31e923fe2 | 707 | } |
Pawel Zarembski |
0:01f31e923fe2 | 708 | |
Pawel Zarembski |
0:01f31e923fe2 | 709 | #endif |
Pawel Zarembski |
0:01f31e923fe2 | 710 | } |
Pawel Zarembski |
0:01f31e923fe2 | 711 | |
Pawel Zarembski |
0:01f31e923fe2 | 712 | /* suspend interrupt */ |
Pawel Zarembski |
0:01f31e923fe2 | 713 | if (sts & (1UL << 8)) { |
Pawel Zarembski |
0:01f31e923fe2 | 714 | USBD_Suspend(); |
Pawel Zarembski |
0:01f31e923fe2 | 715 | #ifdef __RTX |
Pawel Zarembski |
0:01f31e923fe2 | 716 | |
Pawel Zarembski |
0:01f31e923fe2 | 717 | if (USBD_RTX_DevTask) { |
Pawel Zarembski |
0:01f31e923fe2 | 718 | isr_evt_set(USBD_EVT_SUSPEND, USBD_RTX_DevTask); |
Pawel Zarembski |
0:01f31e923fe2 | 719 | } |
Pawel Zarembski |
0:01f31e923fe2 | 720 | |
Pawel Zarembski |
0:01f31e923fe2 | 721 | #else |
Pawel Zarembski |
0:01f31e923fe2 | 722 | |
Pawel Zarembski |
0:01f31e923fe2 | 723 | if (USBD_P_Suspend_Event) { |
Pawel Zarembski |
0:01f31e923fe2 | 724 | USBD_P_Suspend_Event(); |
Pawel Zarembski |
0:01f31e923fe2 | 725 | } |
Pawel Zarembski |
0:01f31e923fe2 | 726 | |
Pawel Zarembski |
0:01f31e923fe2 | 727 | #endif |
Pawel Zarembski |
0:01f31e923fe2 | 728 | } |
Pawel Zarembski |
0:01f31e923fe2 | 729 | |
Pawel Zarembski |
0:01f31e923fe2 | 730 | /* SOF interrupt */ |
Pawel Zarembski |
0:01f31e923fe2 | 731 | if (sts & (1UL << 7)) { |
Pawel Zarembski |
0:01f31e923fe2 | 732 | if (IsoEp) { |
Pawel Zarembski |
0:01f31e923fe2 | 733 | for (num = 0; num < USBD_EP_NUM + 1; num++) { |
Pawel Zarembski |
0:01f31e923fe2 | 734 | if (IsoEp & (1UL << num)) { |
Pawel Zarembski |
0:01f31e923fe2 | 735 | USBD_PrimeEp(num, Ep[EP_OUT_IDX(num)].maxPacket); |
Pawel Zarembski |
0:01f31e923fe2 | 736 | } |
Pawel Zarembski |
0:01f31e923fe2 | 737 | } |
Pawel Zarembski |
0:01f31e923fe2 | 738 | |
Pawel Zarembski |
0:01f31e923fe2 | 739 | } else { |
Pawel Zarembski |
0:01f31e923fe2 | 740 | #ifdef __RTX |
Pawel Zarembski |
0:01f31e923fe2 | 741 | |
Pawel Zarembski |
0:01f31e923fe2 | 742 | if (USBD_RTX_DevTask) { |
Pawel Zarembski |
0:01f31e923fe2 | 743 | isr_evt_set(USBD_EVT_SOF, USBD_RTX_DevTask); |
Pawel Zarembski |
0:01f31e923fe2 | 744 | } |
Pawel Zarembski |
0:01f31e923fe2 | 745 | |
Pawel Zarembski |
0:01f31e923fe2 | 746 | #else |
Pawel Zarembski |
0:01f31e923fe2 | 747 | |
Pawel Zarembski |
0:01f31e923fe2 | 748 | if (USBD_P_SOF_Event) { |
Pawel Zarembski |
0:01f31e923fe2 | 749 | USBD_P_SOF_Event(); |
Pawel Zarembski |
0:01f31e923fe2 | 750 | } |
Pawel Zarembski |
0:01f31e923fe2 | 751 | |
Pawel Zarembski |
0:01f31e923fe2 | 752 | #endif |
Pawel Zarembski |
0:01f31e923fe2 | 753 | } |
Pawel Zarembski |
0:01f31e923fe2 | 754 | } |
Pawel Zarembski |
0:01f31e923fe2 | 755 | |
Pawel Zarembski |
0:01f31e923fe2 | 756 | /* port change detect interrupt */ |
Pawel Zarembski |
0:01f31e923fe2 | 757 | if (sts & (1UL << 2)) { |
Pawel Zarembski |
0:01f31e923fe2 | 758 | if (((USBHS->PORTSC1 >> 26) & 0x03) == 2) { |
Pawel Zarembski |
0:01f31e923fe2 | 759 | USBD_HighSpeed = __TRUE; |
Pawel Zarembski |
0:01f31e923fe2 | 760 | } |
Pawel Zarembski |
0:01f31e923fe2 | 761 | |
Pawel Zarembski |
0:01f31e923fe2 | 762 | USBD_Resume(); |
Pawel Zarembski |
0:01f31e923fe2 | 763 | #ifdef __RTX |
Pawel Zarembski |
0:01f31e923fe2 | 764 | |
Pawel Zarembski |
0:01f31e923fe2 | 765 | if (USBD_RTX_DevTask) { |
Pawel Zarembski |
0:01f31e923fe2 | 766 | isr_evt_set(USBD_EVT_RESUME, USBD_RTX_DevTask); |
Pawel Zarembski |
0:01f31e923fe2 | 767 | } |
Pawel Zarembski |
0:01f31e923fe2 | 768 | |
Pawel Zarembski |
0:01f31e923fe2 | 769 | #else |
Pawel Zarembski |
0:01f31e923fe2 | 770 | |
Pawel Zarembski |
0:01f31e923fe2 | 771 | if (USBD_P_Resume_Event) { |
Pawel Zarembski |
0:01f31e923fe2 | 772 | USBD_P_Resume_Event(); |
Pawel Zarembski |
0:01f31e923fe2 | 773 | } |
Pawel Zarembski |
0:01f31e923fe2 | 774 | |
Pawel Zarembski |
0:01f31e923fe2 | 775 | #endif |
Pawel Zarembski |
0:01f31e923fe2 | 776 | } |
Pawel Zarembski |
0:01f31e923fe2 | 777 | |
Pawel Zarembski |
0:01f31e923fe2 | 778 | /* USB interrupt - completed transfer */ |
Pawel Zarembski |
0:01f31e923fe2 | 779 | if (sts & 1) { |
Pawel Zarembski |
0:01f31e923fe2 | 780 | /* Setup Packet */ |
Pawel Zarembski |
0:01f31e923fe2 | 781 | if (USBHS->EPSETUPSR) { |
Pawel Zarembski |
0:01f31e923fe2 | 782 | #ifdef __RTX |
Pawel Zarembski |
0:01f31e923fe2 | 783 | |
Pawel Zarembski |
0:01f31e923fe2 | 784 | if (USBD_RTX_EPTask[0]) { |
Pawel Zarembski |
0:01f31e923fe2 | 785 | isr_evt_set(USBD_EVT_SETUP, USBD_RTX_EPTask[0]); |
Pawel Zarembski |
0:01f31e923fe2 | 786 | } |
Pawel Zarembski |
0:01f31e923fe2 | 787 | |
Pawel Zarembski |
0:01f31e923fe2 | 788 | #else |
Pawel Zarembski |
0:01f31e923fe2 | 789 | |
Pawel Zarembski |
0:01f31e923fe2 | 790 | if (USBD_P_EP[0]) { |
Pawel Zarembski |
0:01f31e923fe2 | 791 | USBD_P_EP[0](USBD_EVT_SETUP); |
Pawel Zarembski |
0:01f31e923fe2 | 792 | } |
Pawel Zarembski |
0:01f31e923fe2 | 793 | |
Pawel Zarembski |
0:01f31e923fe2 | 794 | #endif |
Pawel Zarembski |
0:01f31e923fe2 | 795 | } |
Pawel Zarembski |
0:01f31e923fe2 | 796 | |
Pawel Zarembski |
0:01f31e923fe2 | 797 | /* IN Packet */ |
Pawel Zarembski |
0:01f31e923fe2 | 798 | if (cmpl & (0x3F << 16)) { |
Pawel Zarembski |
0:01f31e923fe2 | 799 | for (num = 0; num < USBD_EP_NUM + 1; num++) { |
Pawel Zarembski |
0:01f31e923fe2 | 800 | if (((cmpl >> 16) & 0x3F) & (1UL << num)) { |
Pawel Zarembski |
0:01f31e923fe2 | 801 | USBHS->EPCOMPLETE = (1UL << (num + 16)); /* Clear completed */ |
Pawel Zarembski |
0:01f31e923fe2 | 802 | #ifdef __RTX |
Pawel Zarembski |
0:01f31e923fe2 | 803 | |
Pawel Zarembski |
0:01f31e923fe2 | 804 | if (USBD_RTX_EPTask[num]) { |
Pawel Zarembski |
0:01f31e923fe2 | 805 | isr_evt_set(USBD_EVT_IN, USBD_RTX_EPTask[num]); |
Pawel Zarembski |
0:01f31e923fe2 | 806 | } |
Pawel Zarembski |
0:01f31e923fe2 | 807 | |
Pawel Zarembski |
0:01f31e923fe2 | 808 | #else |
Pawel Zarembski |
0:01f31e923fe2 | 809 | |
Pawel Zarembski |
0:01f31e923fe2 | 810 | if (USBD_P_EP[num]) { |
Pawel Zarembski |
0:01f31e923fe2 | 811 | USBD_P_EP[num](USBD_EVT_IN); |
Pawel Zarembski |
0:01f31e923fe2 | 812 | } |
Pawel Zarembski |
0:01f31e923fe2 | 813 | |
Pawel Zarembski |
0:01f31e923fe2 | 814 | #endif |
Pawel Zarembski |
0:01f31e923fe2 | 815 | } |
Pawel Zarembski |
0:01f31e923fe2 | 816 | } |
Pawel Zarembski |
0:01f31e923fe2 | 817 | } |
Pawel Zarembski |
0:01f31e923fe2 | 818 | |
Pawel Zarembski |
0:01f31e923fe2 | 819 | /* OUT Packet */ |
Pawel Zarembski |
0:01f31e923fe2 | 820 | if (cmpl & 0x3F) { |
Pawel Zarembski |
0:01f31e923fe2 | 821 | for (num = 0; num < USBD_EP_NUM + 1; num++) { |
Pawel Zarembski |
0:01f31e923fe2 | 822 | if ((cmpl ^ cmpl_pnd) & cmpl & (1UL << num)) { |
Pawel Zarembski |
0:01f31e923fe2 | 823 | cmpl_pnd |= 1UL << num; |
Pawel Zarembski |
0:01f31e923fe2 | 824 | #ifdef __RTX |
Pawel Zarembski |
0:01f31e923fe2 | 825 | |
Pawel Zarembski |
0:01f31e923fe2 | 826 | if (USBD_RTX_EPTask[num]) { |
Pawel Zarembski |
0:01f31e923fe2 | 827 | isr_evt_set(USBD_EVT_OUT, USBD_RTX_EPTask[num]); |
Pawel Zarembski |
0:01f31e923fe2 | 828 | |
Pawel Zarembski |
0:01f31e923fe2 | 829 | } else if (IsoEp & (1UL << num)) { |
Pawel Zarembski |
0:01f31e923fe2 | 830 | if (USBD_RTX_DevTask) { |
Pawel Zarembski |
0:01f31e923fe2 | 831 | isr_evt_set(USBD_EVT_SOF, USBD_RTX_DevTask); |
Pawel Zarembski |
0:01f31e923fe2 | 832 | } |
Pawel Zarembski |
0:01f31e923fe2 | 833 | } |
Pawel Zarembski |
0:01f31e923fe2 | 834 | |
Pawel Zarembski |
0:01f31e923fe2 | 835 | #else |
Pawel Zarembski |
0:01f31e923fe2 | 836 | |
Pawel Zarembski |
0:01f31e923fe2 | 837 | if (USBD_P_EP[num]) { |
Pawel Zarembski |
0:01f31e923fe2 | 838 | USBD_P_EP[num](USBD_EVT_OUT); |
Pawel Zarembski |
0:01f31e923fe2 | 839 | |
Pawel Zarembski |
0:01f31e923fe2 | 840 | } else if (IsoEp & (1UL << num)) { |
Pawel Zarembski |
0:01f31e923fe2 | 841 | if (USBD_P_SOF_Event) { |
Pawel Zarembski |
0:01f31e923fe2 | 842 | USBD_P_SOF_Event(); |
Pawel Zarembski |
0:01f31e923fe2 | 843 | } |
Pawel Zarembski |
0:01f31e923fe2 | 844 | } |
Pawel Zarembski |
0:01f31e923fe2 | 845 | |
Pawel Zarembski |
0:01f31e923fe2 | 846 | #endif |
Pawel Zarembski |
0:01f31e923fe2 | 847 | } |
Pawel Zarembski |
0:01f31e923fe2 | 848 | } |
Pawel Zarembski |
0:01f31e923fe2 | 849 | } |
Pawel Zarembski |
0:01f31e923fe2 | 850 | } |
Pawel Zarembski |
0:01f31e923fe2 | 851 | |
Pawel Zarembski |
0:01f31e923fe2 | 852 | /* error interrupt */ |
Pawel Zarembski |
0:01f31e923fe2 | 853 | if (sts & (1UL << 1)) { |
Pawel Zarembski |
0:01f31e923fe2 | 854 | for (num = 0; num < USBD_EP_NUM + 1; num++) { |
Pawel Zarembski |
0:01f31e923fe2 | 855 | if (cmpl & (1UL << num)) { |
Pawel Zarembski |
0:01f31e923fe2 | 856 | #ifdef __RTX |
Pawel Zarembski |
0:01f31e923fe2 | 857 | |
Pawel Zarembski |
0:01f31e923fe2 | 858 | if (USBD_RTX_DevTask) { |
Pawel Zarembski |
0:01f31e923fe2 | 859 | LastError = dTDx[EP_OUT_IDX(num)].dTD_token & 0xE8; |
Pawel Zarembski |
0:01f31e923fe2 | 860 | isr_evt_set(USBD_EVT_ERROR, USBD_RTX_DevTask); |
Pawel Zarembski |
0:01f31e923fe2 | 861 | } |
Pawel Zarembski |
0:01f31e923fe2 | 862 | |
Pawel Zarembski |
0:01f31e923fe2 | 863 | #else |
Pawel Zarembski |
0:01f31e923fe2 | 864 | |
Pawel Zarembski |
0:01f31e923fe2 | 865 | if (USBD_P_Error_Event) { |
Pawel Zarembski |
0:01f31e923fe2 | 866 | USBD_P_Error_Event(dTDx[EP_OUT_IDX(num)].dTD_token & 0xE8); |
Pawel Zarembski |
0:01f31e923fe2 | 867 | } |
Pawel Zarembski |
0:01f31e923fe2 | 868 | |
Pawel Zarembski |
0:01f31e923fe2 | 869 | #endif |
Pawel Zarembski |
0:01f31e923fe2 | 870 | } |
Pawel Zarembski |
0:01f31e923fe2 | 871 | |
Pawel Zarembski |
0:01f31e923fe2 | 872 | if (cmpl & (1UL << (num + 16))) { |
Pawel Zarembski |
0:01f31e923fe2 | 873 | #ifdef __RTX |
Pawel Zarembski |
0:01f31e923fe2 | 874 | |
Pawel Zarembski |
0:01f31e923fe2 | 875 | if (USBD_RTX_DevTask) { |
Pawel Zarembski |
0:01f31e923fe2 | 876 | LastError = dTDx[EP_IN_IDX(num)].dTD_token & 0xE8; |
Pawel Zarembski |
0:01f31e923fe2 | 877 | isr_evt_set(USBD_EVT_ERROR, USBD_RTX_DevTask); |
Pawel Zarembski |
0:01f31e923fe2 | 878 | } |
Pawel Zarembski |
0:01f31e923fe2 | 879 | |
Pawel Zarembski |
0:01f31e923fe2 | 880 | #else |
Pawel Zarembski |
0:01f31e923fe2 | 881 | |
Pawel Zarembski |
0:01f31e923fe2 | 882 | if (USBD_P_Error_Event) { |
Pawel Zarembski |
0:01f31e923fe2 | 883 | USBD_P_Error_Event(dTDx[EP_IN_IDX(num)].dTD_token & 0xE8); |
Pawel Zarembski |
0:01f31e923fe2 | 884 | } |
Pawel Zarembski |
0:01f31e923fe2 | 885 | |
Pawel Zarembski |
0:01f31e923fe2 | 886 | #endif |
Pawel Zarembski |
0:01f31e923fe2 | 887 | } |
Pawel Zarembski |
0:01f31e923fe2 | 888 | } |
Pawel Zarembski |
0:01f31e923fe2 | 889 | } |
Pawel Zarembski |
0:01f31e923fe2 | 890 | |
Pawel Zarembski |
0:01f31e923fe2 | 891 | NVIC_EnableIRQ(USBHS_IRQn); |
Pawel Zarembski |
0:01f31e923fe2 | 892 | } |