USB device stack
Dependents: USBMSD_step1 USBMSD_step1_5 picossd_step1_2cs
targets/TARGET_NUVOTON/TARGET_NUC472/USBHAL_NUC472.cpp@71:53949e6131f6, 2017-07-27 (annotated)
- Committer:
- Kojto
- Date:
- Thu Jul 27 12:14:04 2017 +0100
- Revision:
- 71:53949e6131f6
Update libraries
Fixes the previous commmit, as some devices were not copied. USBDevice contains
now targets directory with all targets implementations
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 71:53949e6131f6 | 1 | /* mbed Microcontroller Library |
Kojto | 71:53949e6131f6 | 2 | * Copyright (c) 2015-2016 Nuvoton |
Kojto | 71:53949e6131f6 | 3 | * |
Kojto | 71:53949e6131f6 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Kojto | 71:53949e6131f6 | 5 | * you may not use this file except in compliance with the License. |
Kojto | 71:53949e6131f6 | 6 | * You may obtain a copy of the License at |
Kojto | 71:53949e6131f6 | 7 | * |
Kojto | 71:53949e6131f6 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Kojto | 71:53949e6131f6 | 9 | * |
Kojto | 71:53949e6131f6 | 10 | * Unless required by applicable law or agreed to in writing, software |
Kojto | 71:53949e6131f6 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Kojto | 71:53949e6131f6 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Kojto | 71:53949e6131f6 | 13 | * See the License for the specific language governing permissions and |
Kojto | 71:53949e6131f6 | 14 | * limitations under the License. |
Kojto | 71:53949e6131f6 | 15 | */ |
Kojto | 71:53949e6131f6 | 16 | |
Kojto | 71:53949e6131f6 | 17 | #if defined(TARGET_NUMAKER_PFM_NUC472) |
Kojto | 71:53949e6131f6 | 18 | |
Kojto | 71:53949e6131f6 | 19 | #include "USBHAL.h" |
Kojto | 71:53949e6131f6 | 20 | #include "NUC472_442.h" |
Kojto | 71:53949e6131f6 | 21 | #include "pinmap.h" |
Kojto | 71:53949e6131f6 | 22 | |
Kojto | 71:53949e6131f6 | 23 | /** |
Kojto | 71:53949e6131f6 | 24 | * EP: mbed USBD defined endpoint, e.g. EP0OUT/IN, EP1OUT/IN, EP2OUT/IN. |
Kojto | 71:53949e6131f6 | 25 | * EPX: BSP defined endpoint, e.g. CEP, EPA, EPB, EPC. |
Kojto | 71:53949e6131f6 | 26 | */ |
Kojto | 71:53949e6131f6 | 27 | |
Kojto | 71:53949e6131f6 | 28 | USBHAL * USBHAL::instance; |
Kojto | 71:53949e6131f6 | 29 | |
Kojto | 71:53949e6131f6 | 30 | static volatile uint32_t s_ep_compl = 0; |
Kojto | 71:53949e6131f6 | 31 | static volatile uint32_t s_ep_buf_ind = 0; |
Kojto | 71:53949e6131f6 | 32 | static volatile uint8_t s_usb_addr = 0; |
Kojto | 71:53949e6131f6 | 33 | static volatile S_USBD_CMD_T s_setup; |
Kojto | 71:53949e6131f6 | 34 | static volatile uint16_t s_ctrlin_packetsize; |
Kojto | 71:53949e6131f6 | 35 | static uint8_t *g_usbd_CtrlInPointer = 0; |
Kojto | 71:53949e6131f6 | 36 | static uint32_t g_usbd_CtrlMaxPktSize = 64; |
Kojto | 71:53949e6131f6 | 37 | static uint32_t g_usbd_ShortPkt = 0; |
Kojto | 71:53949e6131f6 | 38 | static uint32_t gEpRead = 0; |
Kojto | 71:53949e6131f6 | 39 | static uint32_t gEpReadCnt = 0; |
Kojto | 71:53949e6131f6 | 40 | |
Kojto | 71:53949e6131f6 | 41 | void USBD_CtrlInput(void) |
Kojto | 71:53949e6131f6 | 42 | { |
Kojto | 71:53949e6131f6 | 43 | int volatile i; |
Kojto | 71:53949e6131f6 | 44 | uint32_t volatile count; |
Kojto | 71:53949e6131f6 | 45 | |
Kojto | 71:53949e6131f6 | 46 | // Process remained data |
Kojto | 71:53949e6131f6 | 47 | if (g_usbd_CtrlInSize >= g_usbd_CtrlMaxPktSize) |
Kojto | 71:53949e6131f6 | 48 | { |
Kojto | 71:53949e6131f6 | 49 | // Data size > MXPLD |
Kojto | 71:53949e6131f6 | 50 | for (i=0; i<(g_usbd_CtrlMaxPktSize >> 2); i++, g_usbd_CtrlInPointer+=4) |
Kojto | 71:53949e6131f6 | 51 | USBD->CEPDAT = *(uint32_t *)g_usbd_CtrlInPointer; |
Kojto | 71:53949e6131f6 | 52 | USBD_START_CEP_IN(g_usbd_CtrlMaxPktSize); |
Kojto | 71:53949e6131f6 | 53 | g_usbd_CtrlInSize -= g_usbd_CtrlMaxPktSize; |
Kojto | 71:53949e6131f6 | 54 | } |
Kojto | 71:53949e6131f6 | 55 | else |
Kojto | 71:53949e6131f6 | 56 | { |
Kojto | 71:53949e6131f6 | 57 | // Data size <= MXPLD |
Kojto | 71:53949e6131f6 | 58 | for (i=0; i<(g_usbd_CtrlInSize >> 2); i++, g_usbd_CtrlInPointer+=4) |
Kojto | 71:53949e6131f6 | 59 | USBD->CEPDAT = *(uint32_t *)g_usbd_CtrlInPointer; |
Kojto | 71:53949e6131f6 | 60 | |
Kojto | 71:53949e6131f6 | 61 | count = g_usbd_CtrlInSize % 4; |
Kojto | 71:53949e6131f6 | 62 | for (i=0; i<count; i++) |
Kojto | 71:53949e6131f6 | 63 | USBD->CEPDAT_BYTE = *(uint8_t *)(g_usbd_CtrlInPointer + i); |
Kojto | 71:53949e6131f6 | 64 | |
Kojto | 71:53949e6131f6 | 65 | USBD_START_CEP_IN(g_usbd_CtrlInSize); |
Kojto | 71:53949e6131f6 | 66 | g_usbd_CtrlInPointer = 0; |
Kojto | 71:53949e6131f6 | 67 | g_usbd_CtrlInSize = 0; |
Kojto | 71:53949e6131f6 | 68 | } |
Kojto | 71:53949e6131f6 | 69 | } |
Kojto | 71:53949e6131f6 | 70 | |
Kojto | 71:53949e6131f6 | 71 | USBHAL::USBHAL(void) |
Kojto | 71:53949e6131f6 | 72 | { |
Kojto | 71:53949e6131f6 | 73 | SYS_UnlockReg(); |
Kojto | 71:53949e6131f6 | 74 | |
Kojto | 71:53949e6131f6 | 75 | s_ep_buf_ind = 0; |
Kojto | 71:53949e6131f6 | 76 | |
Kojto | 71:53949e6131f6 | 77 | memset(epCallback, 0x00, sizeof (epCallback)); |
Kojto | 71:53949e6131f6 | 78 | epCallback[0] = &USBHAL::EP1_OUT_callback; |
Kojto | 71:53949e6131f6 | 79 | epCallback[1] = &USBHAL::EP2_IN_callback; |
Kojto | 71:53949e6131f6 | 80 | epCallback[2] = &USBHAL::EP3_OUT_callback; |
Kojto | 71:53949e6131f6 | 81 | epCallback[3] = &USBHAL::EP4_IN_callback; |
Kojto | 71:53949e6131f6 | 82 | epCallback[4] = &USBHAL::EP5_OUT_callback; |
Kojto | 71:53949e6131f6 | 83 | epCallback[5] = &USBHAL::EP6_IN_callback; |
Kojto | 71:53949e6131f6 | 84 | epCallback[6] = &USBHAL::EP7_OUT_callback; |
Kojto | 71:53949e6131f6 | 85 | epCallback[7] = &USBHAL::EP8_IN_callback; |
Kojto | 71:53949e6131f6 | 86 | epCallback[8] = &USBHAL::EP9_OUT_callback; |
Kojto | 71:53949e6131f6 | 87 | epCallback[9] = &USBHAL::EP10_IN_callback; |
Kojto | 71:53949e6131f6 | 88 | epCallback[10] = &USBHAL::EP11_OUT_callback; |
Kojto | 71:53949e6131f6 | 89 | epCallback[11] = &USBHAL::EP12_IN_callback; |
Kojto | 71:53949e6131f6 | 90 | |
Kojto | 71:53949e6131f6 | 91 | instance = this; |
Kojto | 71:53949e6131f6 | 92 | |
Kojto | 71:53949e6131f6 | 93 | /* Enable USBD module clock */ |
Kojto | 71:53949e6131f6 | 94 | CLK_EnableModuleClock(USBD_MODULE); |
Kojto | 71:53949e6131f6 | 95 | |
Kojto | 71:53949e6131f6 | 96 | /* Enable USB PHY's LDO33. Run as USB device. */ |
Kojto | 71:53949e6131f6 | 97 | SYS->USBPHY = SYS_USBPHY_USBROLE_OTG_V33_EN | SYS_USBPHY_USBROLE_STD_USBD; |
Kojto | 71:53949e6131f6 | 98 | |
Kojto | 71:53949e6131f6 | 99 | /* Enable USB PHY and wait for it ready */ |
Kojto | 71:53949e6131f6 | 100 | USBD_ENABLE_PHY(); |
Kojto | 71:53949e6131f6 | 101 | while (1) |
Kojto | 71:53949e6131f6 | 102 | { |
Kojto | 71:53949e6131f6 | 103 | USBD->EPAMPS = 0x20; |
Kojto | 71:53949e6131f6 | 104 | if (USBD->EPAMPS == 0x20) |
Kojto | 71:53949e6131f6 | 105 | break; |
Kojto | 71:53949e6131f6 | 106 | } |
Kojto | 71:53949e6131f6 | 107 | |
Kojto | 71:53949e6131f6 | 108 | /* Force to full-speed */ |
Kojto | 71:53949e6131f6 | 109 | USBD->OPER = 0;//USBD_OPER_HISPDEN_Msk; |
Kojto | 71:53949e6131f6 | 110 | |
Kojto | 71:53949e6131f6 | 111 | /* Set SE0 (disconnect) */ |
Kojto | 71:53949e6131f6 | 112 | USBD_SET_SE0(); |
Kojto | 71:53949e6131f6 | 113 | |
Kojto | 71:53949e6131f6 | 114 | NVIC_SetVector(USBD_IRQn, (uint32_t) &_usbisr); |
Kojto | 71:53949e6131f6 | 115 | NVIC_EnableIRQ(USBD_IRQn); |
Kojto | 71:53949e6131f6 | 116 | } |
Kojto | 71:53949e6131f6 | 117 | |
Kojto | 71:53949e6131f6 | 118 | USBHAL::~USBHAL(void) |
Kojto | 71:53949e6131f6 | 119 | { |
Kojto | 71:53949e6131f6 | 120 | NVIC_DisableIRQ(USBD_IRQn); |
Kojto | 71:53949e6131f6 | 121 | USBD_SET_SE0(); |
Kojto | 71:53949e6131f6 | 122 | USBD_DISABLE_PHY(); |
Kojto | 71:53949e6131f6 | 123 | } |
Kojto | 71:53949e6131f6 | 124 | |
Kojto | 71:53949e6131f6 | 125 | void USBHAL::connect(void) |
Kojto | 71:53949e6131f6 | 126 | { |
Kojto | 71:53949e6131f6 | 127 | USBD_ResetDMA(); |
Kojto | 71:53949e6131f6 | 128 | USBD_SET_ADDR(0); |
Kojto | 71:53949e6131f6 | 129 | |
Kojto | 71:53949e6131f6 | 130 | /** |
Kojto | 71:53949e6131f6 | 131 | * Control Transfer Packet Size Constraints |
Kojto | 71:53949e6131f6 | 132 | * low-speed: 8 |
Kojto | 71:53949e6131f6 | 133 | * full-speed: 8, 16, 32, 64 |
Kojto | 71:53949e6131f6 | 134 | * high-speed: 64 |
Kojto | 71:53949e6131f6 | 135 | */ |
Kojto | 71:53949e6131f6 | 136 | /* Control endpoint */ |
Kojto | 71:53949e6131f6 | 137 | USBD_SetEpBufAddr(CEP, s_ep_buf_ind, MAX_PACKET_SIZE_EP0); |
Kojto | 71:53949e6131f6 | 138 | s_ep_buf_ind = MAX_PACKET_SIZE_EP0; |
Kojto | 71:53949e6131f6 | 139 | |
Kojto | 71:53949e6131f6 | 140 | /* Enable USB/CEP interrupt */ |
Kojto | 71:53949e6131f6 | 141 | USBD_ENABLE_USB_INT(USBD_GINTEN_USBIE_Msk | USBD_GINTEN_CEPIE_Msk); |
Kojto | 71:53949e6131f6 | 142 | USBD_ENABLE_CEP_INT(USBD_CEPINTEN_SETUPPKIEN_Msk|USBD_CEPINTEN_STSDONEIEN_Msk); |
Kojto | 71:53949e6131f6 | 143 | |
Kojto | 71:53949e6131f6 | 144 | /* Enable BUS interrupt */ |
Kojto | 71:53949e6131f6 | 145 | USBD_ENABLE_BUS_INT( |
Kojto | 71:53949e6131f6 | 146 | USBD_BUSINTEN_DMADONEIEN_Msk | |
Kojto | 71:53949e6131f6 | 147 | USBD_BUSINTEN_RESUMEIEN_Msk | |
Kojto | 71:53949e6131f6 | 148 | USBD_BUSINTEN_RSTIEN_Msk | |
Kojto | 71:53949e6131f6 | 149 | USBD_BUSINTEN_VBUSDETIEN_Msk | |
Kojto | 71:53949e6131f6 | 150 | USBD_BUSINTEN_SOFIEN_Msk |
Kojto | 71:53949e6131f6 | 151 | ); |
Kojto | 71:53949e6131f6 | 152 | |
Kojto | 71:53949e6131f6 | 153 | /* Clear SE0 (connect) */ |
Kojto | 71:53949e6131f6 | 154 | USBD_CLR_SE0(); |
Kojto | 71:53949e6131f6 | 155 | } |
Kojto | 71:53949e6131f6 | 156 | |
Kojto | 71:53949e6131f6 | 157 | void USBHAL::disconnect(void) |
Kojto | 71:53949e6131f6 | 158 | { |
Kojto | 71:53949e6131f6 | 159 | /* Set SE0 (disconnect) */ |
Kojto | 71:53949e6131f6 | 160 | USBD_SET_SE0(); |
Kojto | 71:53949e6131f6 | 161 | } |
Kojto | 71:53949e6131f6 | 162 | |
Kojto | 71:53949e6131f6 | 163 | void USBHAL::configureDevice(void) |
Kojto | 71:53949e6131f6 | 164 | { |
Kojto | 71:53949e6131f6 | 165 | /** |
Kojto | 71:53949e6131f6 | 166 | * In USBDevice.cpp > USBDevice::requestSetConfiguration, configureDevice() is called after realiseEndpoint() (in USBCallback_setConfiguration()). |
Kojto | 71:53949e6131f6 | 167 | * So we have the following USB buffer management policy: |
Kojto | 71:53949e6131f6 | 168 | * 1. Allocate for CEP on connect(). |
Kojto | 71:53949e6131f6 | 169 | * 2. Allocate for EPX in realiseEndpoint(). |
Kojto | 71:53949e6131f6 | 170 | * 3. Deallocate all except for CEP in unconfigureDevice(). |
Kojto | 71:53949e6131f6 | 171 | */ |
Kojto | 71:53949e6131f6 | 172 | } |
Kojto | 71:53949e6131f6 | 173 | |
Kojto | 71:53949e6131f6 | 174 | void USBHAL::unconfigureDevice(void) |
Kojto | 71:53949e6131f6 | 175 | { |
Kojto | 71:53949e6131f6 | 176 | s_ep_buf_ind = MAX_PACKET_SIZE_EP0; |
Kojto | 71:53949e6131f6 | 177 | } |
Kojto | 71:53949e6131f6 | 178 | |
Kojto | 71:53949e6131f6 | 179 | void USBHAL::setAddress(uint8_t address) |
Kojto | 71:53949e6131f6 | 180 | { |
Kojto | 71:53949e6131f6 | 181 | // NOTE: Delay address setting; otherwise, USB controller won't ack. |
Kojto | 71:53949e6131f6 | 182 | s_usb_addr = address; |
Kojto | 71:53949e6131f6 | 183 | } |
Kojto | 71:53949e6131f6 | 184 | |
Kojto | 71:53949e6131f6 | 185 | void USBHAL::remoteWakeup(void) |
Kojto | 71:53949e6131f6 | 186 | { |
Kojto | 71:53949e6131f6 | 187 | USBD->OPER |= USBD_OPER_RESUMEEN_Msk; |
Kojto | 71:53949e6131f6 | 188 | } |
Kojto | 71:53949e6131f6 | 189 | |
Kojto | 71:53949e6131f6 | 190 | bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t options) |
Kojto | 71:53949e6131f6 | 191 | { |
Kojto | 71:53949e6131f6 | 192 | uint32_t ep_type; |
Kojto | 71:53949e6131f6 | 193 | uint32_t ep_hw_index = NU_EP2EPH(endpoint); |
Kojto | 71:53949e6131f6 | 194 | |
Kojto | 71:53949e6131f6 | 195 | USBD_SetEpBufAddr(ep_hw_index, s_ep_buf_ind, maxPacket); |
Kojto | 71:53949e6131f6 | 196 | s_ep_buf_ind += maxPacket; |
Kojto | 71:53949e6131f6 | 197 | USBD_SET_MAX_PAYLOAD(ep_hw_index, maxPacket); |
Kojto | 71:53949e6131f6 | 198 | |
Kojto | 71:53949e6131f6 | 199 | switch (NU_EP2EPL(endpoint)) |
Kojto | 71:53949e6131f6 | 200 | { |
Kojto | 71:53949e6131f6 | 201 | case 1: case 2: |
Kojto | 71:53949e6131f6 | 202 | ep_type = USB_EP_CFG_TYPE_INT; |
Kojto | 71:53949e6131f6 | 203 | break; |
Kojto | 71:53949e6131f6 | 204 | |
Kojto | 71:53949e6131f6 | 205 | case 3: case 4: |
Kojto | 71:53949e6131f6 | 206 | ep_type = USB_EP_CFG_TYPE_ISO; |
Kojto | 71:53949e6131f6 | 207 | break; |
Kojto | 71:53949e6131f6 | 208 | |
Kojto | 71:53949e6131f6 | 209 | default: |
Kojto | 71:53949e6131f6 | 210 | ep_type = USB_EP_CFG_TYPE_BULK; |
Kojto | 71:53949e6131f6 | 211 | } |
Kojto | 71:53949e6131f6 | 212 | uint32_t ep_dir = (NU_EP_DIR(endpoint) == NU_EP_DIR_IN) ? USB_EP_CFG_DIR_IN : USB_EP_CFG_DIR_OUT; |
Kojto | 71:53949e6131f6 | 213 | USBD_ConfigEp(ep_hw_index, NU_EP2EPL(endpoint), ep_type, ep_dir); |
Kojto | 71:53949e6131f6 | 214 | |
Kojto | 71:53949e6131f6 | 215 | /* Enable USB/EPX interrupt */ |
Kojto | 71:53949e6131f6 | 216 | // NOTE: Require USBD_GINTEN_EPAIE_Pos, USBD_GINTEN_EPBIE_Pos, ... USBD_GINTEN_EPLIE_Pos to be consecutive. |
Kojto | 71:53949e6131f6 | 217 | USBD_ENABLE_USB_INT(USBD->GINTEN | USBD_GINTEN_USBIE_Msk | |
Kojto | 71:53949e6131f6 | 218 | USBD_GINTEN_CEPIE_Msk | |
Kojto | 71:53949e6131f6 | 219 | 1 << (ep_hw_index + USBD_GINTEN_EPAIE_Pos)); // Added USB/EPX interrupt |
Kojto | 71:53949e6131f6 | 220 | |
Kojto | 71:53949e6131f6 | 221 | if (ep_dir == 0) |
Kojto | 71:53949e6131f6 | 222 | USBD_ENABLE_EP_INT(ep_hw_index, USBD_EPINTEN_RXPKIEN_Msk); |
Kojto | 71:53949e6131f6 | 223 | else |
Kojto | 71:53949e6131f6 | 224 | USBD_ENABLE_EP_INT(ep_hw_index, USBD_EPINTEN_TXPKIEN_Msk); |
Kojto | 71:53949e6131f6 | 225 | return true; |
Kojto | 71:53949e6131f6 | 226 | } |
Kojto | 71:53949e6131f6 | 227 | |
Kojto | 71:53949e6131f6 | 228 | void USBHAL::EP0setup(uint8_t *buffer) |
Kojto | 71:53949e6131f6 | 229 | { |
Kojto | 71:53949e6131f6 | 230 | uint32_t sz; |
Kojto | 71:53949e6131f6 | 231 | endpointReadResult(EP0OUT, buffer, &sz); |
Kojto | 71:53949e6131f6 | 232 | } |
Kojto | 71:53949e6131f6 | 233 | |
Kojto | 71:53949e6131f6 | 234 | void USBHAL::EP0read(void) |
Kojto | 71:53949e6131f6 | 235 | { |
Kojto | 71:53949e6131f6 | 236 | if (s_setup.wLength && ! (s_setup.bmRequestType & 0x80)) |
Kojto | 71:53949e6131f6 | 237 | { |
Kojto | 71:53949e6131f6 | 238 | // Control OUT |
Kojto | 71:53949e6131f6 | 239 | USBD_ENABLE_CEP_INT(USBD_CEPINTEN_SETUPPKIEN_Msk | USBD_CEPINTEN_RXPKIEN_Msk); |
Kojto | 71:53949e6131f6 | 240 | } |
Kojto | 71:53949e6131f6 | 241 | else |
Kojto | 71:53949e6131f6 | 242 | { |
Kojto | 71:53949e6131f6 | 243 | // Status stage |
Kojto | 71:53949e6131f6 | 244 | USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_STSDONEIF_Msk); |
Kojto | 71:53949e6131f6 | 245 | USBD_SET_CEP_STATE(USB_CEPCTL_NAKCLR); |
Kojto | 71:53949e6131f6 | 246 | USBD_ENABLE_CEP_INT(USBD_CEPINTEN_STSDONEIEN_Msk); |
Kojto | 71:53949e6131f6 | 247 | } |
Kojto | 71:53949e6131f6 | 248 | } |
Kojto | 71:53949e6131f6 | 249 | |
Kojto | 71:53949e6131f6 | 250 | void USBHAL::EP0readStage(void) |
Kojto | 71:53949e6131f6 | 251 | { |
Kojto | 71:53949e6131f6 | 252 | // N/A |
Kojto | 71:53949e6131f6 | 253 | } |
Kojto | 71:53949e6131f6 | 254 | |
Kojto | 71:53949e6131f6 | 255 | uint32_t USBHAL::EP0getReadResult(uint8_t *buffer) |
Kojto | 71:53949e6131f6 | 256 | { |
Kojto | 71:53949e6131f6 | 257 | uint32_t i; |
Kojto | 71:53949e6131f6 | 258 | uint32_t ceprxcnt = USBD->CEPRXCNT; |
Kojto | 71:53949e6131f6 | 259 | for (i = 0; i < ceprxcnt; i ++) |
Kojto | 71:53949e6131f6 | 260 | *buffer ++ = USBD->CEPDAT_BYTE; |
Kojto | 71:53949e6131f6 | 261 | return ceprxcnt; |
Kojto | 71:53949e6131f6 | 262 | } |
Kojto | 71:53949e6131f6 | 263 | |
Kojto | 71:53949e6131f6 | 264 | void USBHAL::EP0write(uint8_t *buffer, uint32_t size) |
Kojto | 71:53949e6131f6 | 265 | { |
Kojto | 71:53949e6131f6 | 266 | if (buffer && size) |
Kojto | 71:53949e6131f6 | 267 | { |
Kojto | 71:53949e6131f6 | 268 | g_usbd_CtrlInPointer = buffer; |
Kojto | 71:53949e6131f6 | 269 | g_usbd_CtrlInSize = size; |
Kojto | 71:53949e6131f6 | 270 | USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_INTKIF_Msk); |
Kojto | 71:53949e6131f6 | 271 | USBD_ENABLE_CEP_INT(USBD_CEPINTEN_INTKIEN_Msk); |
Kojto | 71:53949e6131f6 | 272 | } |
Kojto | 71:53949e6131f6 | 273 | else |
Kojto | 71:53949e6131f6 | 274 | { |
Kojto | 71:53949e6131f6 | 275 | /* Status stage */ |
Kojto | 71:53949e6131f6 | 276 | s_ctrlin_packetsize = 0; |
Kojto | 71:53949e6131f6 | 277 | USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_STSDONEIF_Msk); |
Kojto | 71:53949e6131f6 | 278 | USBD_SET_CEP_STATE(USB_CEPCTL_NAKCLR); |
Kojto | 71:53949e6131f6 | 279 | USBD_ENABLE_CEP_INT(USBD_CEPINTEN_STSDONEIEN_Msk); |
Kojto | 71:53949e6131f6 | 280 | } |
Kojto | 71:53949e6131f6 | 281 | } |
Kojto | 71:53949e6131f6 | 282 | |
Kojto | 71:53949e6131f6 | 283 | void USBHAL::EP0getWriteResult(void) |
Kojto | 71:53949e6131f6 | 284 | { |
Kojto | 71:53949e6131f6 | 285 | // N/A |
Kojto | 71:53949e6131f6 | 286 | } |
Kojto | 71:53949e6131f6 | 287 | |
Kojto | 71:53949e6131f6 | 288 | void USBHAL::EP0stall(void) |
Kojto | 71:53949e6131f6 | 289 | { |
Kojto | 71:53949e6131f6 | 290 | stallEndpoint(EP0OUT); |
Kojto | 71:53949e6131f6 | 291 | } |
Kojto | 71:53949e6131f6 | 292 | |
Kojto | 71:53949e6131f6 | 293 | EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize) |
Kojto | 71:53949e6131f6 | 294 | { |
Kojto | 71:53949e6131f6 | 295 | return EP_PENDING; |
Kojto | 71:53949e6131f6 | 296 | } |
Kojto | 71:53949e6131f6 | 297 | |
Kojto | 71:53949e6131f6 | 298 | EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t * buffer, uint32_t *bytesRead) //spcheng |
Kojto | 71:53949e6131f6 | 299 | { |
Kojto | 71:53949e6131f6 | 300 | if (endpoint == EP0OUT) |
Kojto | 71:53949e6131f6 | 301 | { |
Kojto | 71:53949e6131f6 | 302 | if (buffer) { |
Kojto | 71:53949e6131f6 | 303 | *((uint16_t *) (buffer + 0)) = (uint16_t) USBD->SETUP1_0; |
Kojto | 71:53949e6131f6 | 304 | *((uint16_t *) (buffer + 2)) = (uint16_t) USBD->SETUP3_2; |
Kojto | 71:53949e6131f6 | 305 | *((uint16_t *) (buffer + 4)) = (uint16_t) USBD->SETUP5_4; |
Kojto | 71:53949e6131f6 | 306 | *((uint16_t *) (buffer + 6)) = (uint16_t) USBD->SETUP7_6; |
Kojto | 71:53949e6131f6 | 307 | } |
Kojto | 71:53949e6131f6 | 308 | |
Kojto | 71:53949e6131f6 | 309 | s_setup.bmRequestType = (uint8_t) (USBD->SETUP1_0 & 0xff); |
Kojto | 71:53949e6131f6 | 310 | s_setup.bRequest = (int8_t) (USBD->SETUP1_0 >> 8) & 0xff; |
Kojto | 71:53949e6131f6 | 311 | s_setup.wValue = (uint16_t) USBD->SETUP3_2; |
Kojto | 71:53949e6131f6 | 312 | s_setup.wIndex = (uint16_t) USBD->SETUP5_4; |
Kojto | 71:53949e6131f6 | 313 | s_setup.wLength = (uint16_t) USBD->SETUP7_6; |
Kojto | 71:53949e6131f6 | 314 | } |
Kojto | 71:53949e6131f6 | 315 | else |
Kojto | 71:53949e6131f6 | 316 | { |
Kojto | 71:53949e6131f6 | 317 | if (!(s_ep_compl & (1 << NU_EP2EPL(endpoint)))) |
Kojto | 71:53949e6131f6 | 318 | { |
Kojto | 71:53949e6131f6 | 319 | while (1) |
Kojto | 71:53949e6131f6 | 320 | { |
Kojto | 71:53949e6131f6 | 321 | if (!(USBD->DMACTL & USBD_DMACTL_DMAEN_Msk)) |
Kojto | 71:53949e6131f6 | 322 | break; |
Kojto | 71:53949e6131f6 | 323 | else |
Kojto | 71:53949e6131f6 | 324 | if (!USBD_IS_ATTACHED()) |
Kojto | 71:53949e6131f6 | 325 | break; |
Kojto | 71:53949e6131f6 | 326 | } |
Kojto | 71:53949e6131f6 | 327 | gEpReadCnt = USBD_GET_EP_DATA_COUNT(NU_EP2EPH(endpoint)); |
Kojto | 71:53949e6131f6 | 328 | if (gEpReadCnt == 0) |
Kojto | 71:53949e6131f6 | 329 | { |
Kojto | 71:53949e6131f6 | 330 | *bytesRead = 0; |
Kojto | 71:53949e6131f6 | 331 | return EP_COMPLETED; |
Kojto | 71:53949e6131f6 | 332 | } |
Kojto | 71:53949e6131f6 | 333 | s_ep_compl |= (1 << NU_EP2EPL(endpoint)); |
Kojto | 71:53949e6131f6 | 334 | USBD_SET_DMA_LEN(gEpReadCnt); |
Kojto | 71:53949e6131f6 | 335 | USBD_SET_DMA_ADDR((uint32_t)buffer); |
Kojto | 71:53949e6131f6 | 336 | USBD_SET_DMA_WRITE(NU_EP2EPL(endpoint)); |
Kojto | 71:53949e6131f6 | 337 | USBD_ENABLE_DMA(); |
Kojto | 71:53949e6131f6 | 338 | return EP_PENDING;; |
Kojto | 71:53949e6131f6 | 339 | |
Kojto | 71:53949e6131f6 | 340 | } |
Kojto | 71:53949e6131f6 | 341 | else |
Kojto | 71:53949e6131f6 | 342 | { |
Kojto | 71:53949e6131f6 | 343 | if ((USBD->DMACTL & USBD_DMACTL_DMAEN_Msk)) |
Kojto | 71:53949e6131f6 | 344 | return EP_PENDING;; |
Kojto | 71:53949e6131f6 | 345 | |
Kojto | 71:53949e6131f6 | 346 | USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_DMADONEIF_Msk); |
Kojto | 71:53949e6131f6 | 347 | s_ep_compl &= ~(1 << NU_EP2EPL(endpoint)); |
Kojto | 71:53949e6131f6 | 348 | *bytesRead = gEpReadCnt; |
Kojto | 71:53949e6131f6 | 349 | } |
Kojto | 71:53949e6131f6 | 350 | } |
Kojto | 71:53949e6131f6 | 351 | return EP_COMPLETED; |
Kojto | 71:53949e6131f6 | 352 | } |
Kojto | 71:53949e6131f6 | 353 | |
Kojto | 71:53949e6131f6 | 354 | |
Kojto | 71:53949e6131f6 | 355 | uint32_t USBHAL::endpointReadcore(uint8_t endpoint, uint8_t *buffer) |
Kojto | 71:53949e6131f6 | 356 | { |
Kojto | 71:53949e6131f6 | 357 | return 0; |
Kojto | 71:53949e6131f6 | 358 | } |
Kojto | 71:53949e6131f6 | 359 | |
Kojto | 71:53949e6131f6 | 360 | EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size) |
Kojto | 71:53949e6131f6 | 361 | { |
Kojto | 71:53949e6131f6 | 362 | uint32_t ep_logic_index = NU_EP2EPL(endpoint); |
Kojto | 71:53949e6131f6 | 363 | if (ep_logic_index == 0) |
Kojto | 71:53949e6131f6 | 364 | return EP_INVALID; |
Kojto | 71:53949e6131f6 | 365 | else |
Kojto | 71:53949e6131f6 | 366 | { |
Kojto | 71:53949e6131f6 | 367 | uint32_t ep_hw_index = NU_EP2EPH(endpoint); |
Kojto | 71:53949e6131f6 | 368 | uint32_t mps = USBD_GET_EP_MAX_PAYLOAD(ep_hw_index); |
Kojto | 71:53949e6131f6 | 369 | if (size > mps) { |
Kojto | 71:53949e6131f6 | 370 | return EP_INVALID; |
Kojto | 71:53949e6131f6 | 371 | } |
Kojto | 71:53949e6131f6 | 372 | if (size < mps) |
Kojto | 71:53949e6131f6 | 373 | g_usbd_ShortPkt = 1; |
Kojto | 71:53949e6131f6 | 374 | if (!(s_ep_compl & (1 << NU_EP2EPL(endpoint)))) |
Kojto | 71:53949e6131f6 | 375 | { |
Kojto | 71:53949e6131f6 | 376 | s_ep_compl |= (1 << ep_logic_index); |
Kojto | 71:53949e6131f6 | 377 | |
Kojto | 71:53949e6131f6 | 378 | while (1) |
Kojto | 71:53949e6131f6 | 379 | { |
Kojto | 71:53949e6131f6 | 380 | if (!(USBD->DMACTL & USBD_DMACTL_DMAEN_Msk)) |
Kojto | 71:53949e6131f6 | 381 | break; |
Kojto | 71:53949e6131f6 | 382 | else |
Kojto | 71:53949e6131f6 | 383 | if (!USBD_IS_ATTACHED()) |
Kojto | 71:53949e6131f6 | 384 | break; |
Kojto | 71:53949e6131f6 | 385 | } |
Kojto | 71:53949e6131f6 | 386 | USBD_SET_DMA_LEN(size); |
Kojto | 71:53949e6131f6 | 387 | USBD_SET_DMA_ADDR((uint32_t)data); |
Kojto | 71:53949e6131f6 | 388 | USBD_SET_DMA_READ(ep_logic_index); |
Kojto | 71:53949e6131f6 | 389 | USBD_ENABLE_DMA(); |
Kojto | 71:53949e6131f6 | 390 | } |
Kojto | 71:53949e6131f6 | 391 | } |
Kojto | 71:53949e6131f6 | 392 | return EP_PENDING; |
Kojto | 71:53949e6131f6 | 393 | } |
Kojto | 71:53949e6131f6 | 394 | |
Kojto | 71:53949e6131f6 | 395 | EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint) |
Kojto | 71:53949e6131f6 | 396 | { |
Kojto | 71:53949e6131f6 | 397 | if (!(s_ep_compl & (1 << NU_EP2EPL(endpoint)))) |
Kojto | 71:53949e6131f6 | 398 | return EP_COMPLETED; |
Kojto | 71:53949e6131f6 | 399 | else |
Kojto | 71:53949e6131f6 | 400 | { |
Kojto | 71:53949e6131f6 | 401 | if ((USBD_GET_EP_DATA_COUNT(NU_EP2EPH(endpoint))) == 0 && !(USBD->DMACTL & USBD_DMACTL_DMAEN_Msk)) |
Kojto | 71:53949e6131f6 | 402 | { |
Kojto | 71:53949e6131f6 | 403 | s_ep_compl &= ~(s_ep_compl & (1 << NU_EP2EPL(endpoint))); |
Kojto | 71:53949e6131f6 | 404 | return EP_COMPLETED; |
Kojto | 71:53949e6131f6 | 405 | } |
Kojto | 71:53949e6131f6 | 406 | } |
Kojto | 71:53949e6131f6 | 407 | return EP_PENDING; |
Kojto | 71:53949e6131f6 | 408 | } |
Kojto | 71:53949e6131f6 | 409 | |
Kojto | 71:53949e6131f6 | 410 | void USBHAL::stallEndpoint(uint8_t endpoint) |
Kojto | 71:53949e6131f6 | 411 | { |
Kojto | 71:53949e6131f6 | 412 | uint32_t ep_hw_index = NU_EP2EPH(endpoint); |
Kojto | 71:53949e6131f6 | 413 | if (ep_hw_index >= NUMBER_OF_PHYSICAL_ENDPOINTS) |
Kojto | 71:53949e6131f6 | 414 | return; |
Kojto | 71:53949e6131f6 | 415 | USBD_SetStall(ep_hw_index); |
Kojto | 71:53949e6131f6 | 416 | } |
Kojto | 71:53949e6131f6 | 417 | |
Kojto | 71:53949e6131f6 | 418 | void USBHAL::unstallEndpoint(uint8_t endpoint) |
Kojto | 71:53949e6131f6 | 419 | { |
Kojto | 71:53949e6131f6 | 420 | uint32_t ep_hw_index = NU_EP2EPH(endpoint); |
Kojto | 71:53949e6131f6 | 421 | if (ep_hw_index >= NUMBER_OF_PHYSICAL_ENDPOINTS) |
Kojto | 71:53949e6131f6 | 422 | return; |
Kojto | 71:53949e6131f6 | 423 | USBD_ClearStall(ep_hw_index); |
Kojto | 71:53949e6131f6 | 424 | } |
Kojto | 71:53949e6131f6 | 425 | |
Kojto | 71:53949e6131f6 | 426 | bool USBHAL::getEndpointStallState(uint8_t endpoint) |
Kojto | 71:53949e6131f6 | 427 | { |
Kojto | 71:53949e6131f6 | 428 | uint32_t ep_hw_index = NU_EP2EPH(endpoint); |
Kojto | 71:53949e6131f6 | 429 | if (ep_hw_index >= NUMBER_OF_PHYSICAL_ENDPOINTS) |
Kojto | 71:53949e6131f6 | 430 | return false; |
Kojto | 71:53949e6131f6 | 431 | return USBD_GetStall(ep_hw_index) ? 1 : 0; |
Kojto | 71:53949e6131f6 | 432 | } |
Kojto | 71:53949e6131f6 | 433 | |
Kojto | 71:53949e6131f6 | 434 | void USBHAL::_usbisr(void) |
Kojto | 71:53949e6131f6 | 435 | { |
Kojto | 71:53949e6131f6 | 436 | MBED_ASSERT(instance); |
Kojto | 71:53949e6131f6 | 437 | instance->usbisr(); |
Kojto | 71:53949e6131f6 | 438 | } |
Kojto | 71:53949e6131f6 | 439 | |
Kojto | 71:53949e6131f6 | 440 | void USBHAL::usbisr(void) |
Kojto | 71:53949e6131f6 | 441 | { |
Kojto | 71:53949e6131f6 | 442 | uint32_t gintsts = USBD->GINTSTS & USBD->GINTEN; |
Kojto | 71:53949e6131f6 | 443 | if (! gintsts) |
Kojto | 71:53949e6131f6 | 444 | return; |
Kojto | 71:53949e6131f6 | 445 | |
Kojto | 71:53949e6131f6 | 446 | if (gintsts & USBD_GINTSTS_USBIF_Msk) |
Kojto | 71:53949e6131f6 | 447 | { |
Kojto | 71:53949e6131f6 | 448 | uint32_t busintsts = USBD->BUSINTSTS & USBD->BUSINTEN; |
Kojto | 71:53949e6131f6 | 449 | |
Kojto | 71:53949e6131f6 | 450 | /* SOF */ |
Kojto | 71:53949e6131f6 | 451 | if (busintsts & USBD_BUSINTSTS_SOFIF_Msk) |
Kojto | 71:53949e6131f6 | 452 | { |
Kojto | 71:53949e6131f6 | 453 | USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_SOFIF_Msk); |
Kojto | 71:53949e6131f6 | 454 | // TODO |
Kojto | 71:53949e6131f6 | 455 | SOF(USBD->FRAMECNT >> 3); |
Kojto | 71:53949e6131f6 | 456 | } |
Kojto | 71:53949e6131f6 | 457 | |
Kojto | 71:53949e6131f6 | 458 | /* Reset */ |
Kojto | 71:53949e6131f6 | 459 | if (busintsts & USBD_BUSINTSTS_RSTIF_Msk) |
Kojto | 71:53949e6131f6 | 460 | { |
Kojto | 71:53949e6131f6 | 461 | connect(); |
Kojto | 71:53949e6131f6 | 462 | USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_RSTIF_Msk); |
Kojto | 71:53949e6131f6 | 463 | USBD_CLR_CEP_INT_FLAG(0x1ffc); |
Kojto | 71:53949e6131f6 | 464 | } |
Kojto | 71:53949e6131f6 | 465 | |
Kojto | 71:53949e6131f6 | 466 | /* Resume */ |
Kojto | 71:53949e6131f6 | 467 | if (busintsts & USBD_BUSINTSTS_RESUMEIF_Msk) |
Kojto | 71:53949e6131f6 | 468 | { |
Kojto | 71:53949e6131f6 | 469 | USBD_ENABLE_BUS_INT(USBD_BUSINTEN_RSTIEN_Msk|USBD_BUSINTEN_SUSPENDIEN_Msk | USBD_BUSINTEN_SOFIEN_Msk | USBD_BUSINTEN_SOFIEN_Msk); |
Kojto | 71:53949e6131f6 | 470 | USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_RESUMEIF_Msk); |
Kojto | 71:53949e6131f6 | 471 | } |
Kojto | 71:53949e6131f6 | 472 | |
Kojto | 71:53949e6131f6 | 473 | /* Suspend */ |
Kojto | 71:53949e6131f6 | 474 | if (busintsts & USBD_BUSINTSTS_SUSPENDIF_Msk) |
Kojto | 71:53949e6131f6 | 475 | { |
Kojto | 71:53949e6131f6 | 476 | USBD_ENABLE_BUS_INT(USBD_BUSINTEN_RSTIEN_Msk | USBD_BUSINTEN_RESUMEIEN_Msk |USBD_BUSINTEN_SOFIEN_Msk); |
Kojto | 71:53949e6131f6 | 477 | USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_SUSPENDIF_Msk); |
Kojto | 71:53949e6131f6 | 478 | } |
Kojto | 71:53949e6131f6 | 479 | |
Kojto | 71:53949e6131f6 | 480 | /* High-speed */ |
Kojto | 71:53949e6131f6 | 481 | if (busintsts & USBD_BUSINTSTS_HISPDIF_Msk) |
Kojto | 71:53949e6131f6 | 482 | { |
Kojto | 71:53949e6131f6 | 483 | USBD_ENABLE_CEP_INT(USBD_CEPINTEN_SETUPPKIEN_Msk); |
Kojto | 71:53949e6131f6 | 484 | USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_HISPDIF_Msk); |
Kojto | 71:53949e6131f6 | 485 | } |
Kojto | 71:53949e6131f6 | 486 | |
Kojto | 71:53949e6131f6 | 487 | /* DMA */ |
Kojto | 71:53949e6131f6 | 488 | if (busintsts & USBD_BUSINTSTS_DMADONEIF_Msk) |
Kojto | 71:53949e6131f6 | 489 | { |
Kojto | 71:53949e6131f6 | 490 | if (USBD->DMACTL & 0x10) /* IN - Read */ |
Kojto | 71:53949e6131f6 | 491 | { |
Kojto | 71:53949e6131f6 | 492 | if (g_usbd_ShortPkt) |
Kojto | 71:53949e6131f6 | 493 | { |
Kojto | 71:53949e6131f6 | 494 | uint32_t ep_hw_index = NU_EPL2EPH((USBD->DMACTL & 0xF)); |
Kojto | 71:53949e6131f6 | 495 | USBD_SET_EP_SHORT_PACKET(ep_hw_index); |
Kojto | 71:53949e6131f6 | 496 | g_usbd_ShortPkt = 0; |
Kojto | 71:53949e6131f6 | 497 | } |
Kojto | 71:53949e6131f6 | 498 | } |
Kojto | 71:53949e6131f6 | 499 | USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_DMADONEIF_Msk); |
Kojto | 71:53949e6131f6 | 500 | } |
Kojto | 71:53949e6131f6 | 501 | |
Kojto | 71:53949e6131f6 | 502 | /* PHY clock available */ |
Kojto | 71:53949e6131f6 | 503 | if (busintsts & USBD_BUSINTSTS_PHYCLKVLDIF_Msk) |
Kojto | 71:53949e6131f6 | 504 | { |
Kojto | 71:53949e6131f6 | 505 | USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_PHYCLKVLDIF_Msk); |
Kojto | 71:53949e6131f6 | 506 | } |
Kojto | 71:53949e6131f6 | 507 | |
Kojto | 71:53949e6131f6 | 508 | /* VBUS plug-in */ |
Kojto | 71:53949e6131f6 | 509 | if (busintsts & USBD_BUSINTSTS_VBUSDETIF_Msk) |
Kojto | 71:53949e6131f6 | 510 | { |
Kojto | 71:53949e6131f6 | 511 | if (USBD_IS_ATTACHED()) |
Kojto | 71:53949e6131f6 | 512 | { |
Kojto | 71:53949e6131f6 | 513 | // USB plug-in |
Kojto | 71:53949e6131f6 | 514 | USBD_ENABLE_USB(); |
Kojto | 71:53949e6131f6 | 515 | } |
Kojto | 71:53949e6131f6 | 516 | else |
Kojto | 71:53949e6131f6 | 517 | { |
Kojto | 71:53949e6131f6 | 518 | // USB unplug-out |
Kojto | 71:53949e6131f6 | 519 | USBD_DISABLE_USB(); |
Kojto | 71:53949e6131f6 | 520 | } |
Kojto | 71:53949e6131f6 | 521 | USBD_CLR_BUS_INT_FLAG(USBD_BUSINTSTS_VBUSDETIF_Msk); |
Kojto | 71:53949e6131f6 | 522 | } |
Kojto | 71:53949e6131f6 | 523 | } |
Kojto | 71:53949e6131f6 | 524 | |
Kojto | 71:53949e6131f6 | 525 | /* CEP interrupts */ |
Kojto | 71:53949e6131f6 | 526 | if (gintsts & USBD_GINTSTS_CEPIF_Msk) |
Kojto | 71:53949e6131f6 | 527 | { |
Kojto | 71:53949e6131f6 | 528 | uint32_t cepintsts = USBD->CEPINTSTS & USBD->CEPINTEN; |
Kojto | 71:53949e6131f6 | 529 | |
Kojto | 71:53949e6131f6 | 530 | /* SETUP token packet */ |
Kojto | 71:53949e6131f6 | 531 | if (cepintsts & USBD_CEPINTSTS_SETUPTKIF_Msk) |
Kojto | 71:53949e6131f6 | 532 | { |
Kojto | 71:53949e6131f6 | 533 | USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_SETUPTKIF_Msk); |
Kojto | 71:53949e6131f6 | 534 | return; |
Kojto | 71:53949e6131f6 | 535 | } |
Kojto | 71:53949e6131f6 | 536 | |
Kojto | 71:53949e6131f6 | 537 | /* SETUP transaction */ |
Kojto | 71:53949e6131f6 | 538 | if (cepintsts & USBD_CEPINTSTS_SETUPPKIF_Msk) |
Kojto | 71:53949e6131f6 | 539 | { |
Kojto | 71:53949e6131f6 | 540 | USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_SETUPPKIF_Msk); |
Kojto | 71:53949e6131f6 | 541 | EP0setupCallback(); |
Kojto | 71:53949e6131f6 | 542 | return; |
Kojto | 71:53949e6131f6 | 543 | } |
Kojto | 71:53949e6131f6 | 544 | |
Kojto | 71:53949e6131f6 | 545 | /* OUT token packet */ |
Kojto | 71:53949e6131f6 | 546 | if (cepintsts & USBD_CEPINTSTS_OUTTKIF_Msk) |
Kojto | 71:53949e6131f6 | 547 | { |
Kojto | 71:53949e6131f6 | 548 | USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_OUTTKIF_Msk); |
Kojto | 71:53949e6131f6 | 549 | USBD_ENABLE_CEP_INT(USBD_CEPINTEN_STSDONEIEN_Msk); |
Kojto | 71:53949e6131f6 | 550 | return; |
Kojto | 71:53949e6131f6 | 551 | } |
Kojto | 71:53949e6131f6 | 552 | |
Kojto | 71:53949e6131f6 | 553 | /* IN token packet */ |
Kojto | 71:53949e6131f6 | 554 | if (cepintsts & USBD_CEPINTSTS_INTKIF_Msk) |
Kojto | 71:53949e6131f6 | 555 | { |
Kojto | 71:53949e6131f6 | 556 | USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_INTKIF_Msk); |
Kojto | 71:53949e6131f6 | 557 | if (!(cepintsts & USBD_CEPINTSTS_STSDONEIF_Msk)) |
Kojto | 71:53949e6131f6 | 558 | { |
Kojto | 71:53949e6131f6 | 559 | USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_TXPKIF_Msk); |
Kojto | 71:53949e6131f6 | 560 | USBD_ENABLE_CEP_INT(USBD_CEPINTEN_TXPKIEN_Msk); |
Kojto | 71:53949e6131f6 | 561 | USBD_CtrlInput(); |
Kojto | 71:53949e6131f6 | 562 | } |
Kojto | 71:53949e6131f6 | 563 | else |
Kojto | 71:53949e6131f6 | 564 | { |
Kojto | 71:53949e6131f6 | 565 | USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_TXPKIF_Msk); |
Kojto | 71:53949e6131f6 | 566 | USBD_ENABLE_CEP_INT(USBD_CEPINTEN_TXPKIEN_Msk|USBD_CEPINTEN_STSDONEIEN_Msk); |
Kojto | 71:53949e6131f6 | 567 | } |
Kojto | 71:53949e6131f6 | 568 | return; |
Kojto | 71:53949e6131f6 | 569 | } |
Kojto | 71:53949e6131f6 | 570 | |
Kojto | 71:53949e6131f6 | 571 | /* PING packet */ |
Kojto | 71:53949e6131f6 | 572 | if (cepintsts & USBD_CEPINTSTS_PINGIF_Msk) |
Kojto | 71:53949e6131f6 | 573 | { |
Kojto | 71:53949e6131f6 | 574 | USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_PINGIF_Msk); |
Kojto | 71:53949e6131f6 | 575 | return; |
Kojto | 71:53949e6131f6 | 576 | } |
Kojto | 71:53949e6131f6 | 577 | |
Kojto | 71:53949e6131f6 | 578 | /* IN transaction */ |
Kojto | 71:53949e6131f6 | 579 | if (cepintsts & USBD_CEPINTSTS_TXPKIF_Msk) |
Kojto | 71:53949e6131f6 | 580 | { |
Kojto | 71:53949e6131f6 | 581 | EP0in(); |
Kojto | 71:53949e6131f6 | 582 | USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_TXPKIF_Msk); |
Kojto | 71:53949e6131f6 | 583 | return; |
Kojto | 71:53949e6131f6 | 584 | } |
Kojto | 71:53949e6131f6 | 585 | |
Kojto | 71:53949e6131f6 | 586 | /* OUT transaction */ |
Kojto | 71:53949e6131f6 | 587 | if (cepintsts & USBD_CEPINTSTS_RXPKIF_Msk) |
Kojto | 71:53949e6131f6 | 588 | { |
Kojto | 71:53949e6131f6 | 589 | EP0out(); |
Kojto | 71:53949e6131f6 | 590 | USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_RXPKIF_Msk); |
Kojto | 71:53949e6131f6 | 591 | return; |
Kojto | 71:53949e6131f6 | 592 | } |
Kojto | 71:53949e6131f6 | 593 | |
Kojto | 71:53949e6131f6 | 594 | /* NAK handshake packet */ |
Kojto | 71:53949e6131f6 | 595 | if (cepintsts & USBD_CEPINTSTS_NAKIF_Msk) |
Kojto | 71:53949e6131f6 | 596 | { |
Kojto | 71:53949e6131f6 | 597 | USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_NAKIF_Msk); |
Kojto | 71:53949e6131f6 | 598 | return; |
Kojto | 71:53949e6131f6 | 599 | } |
Kojto | 71:53949e6131f6 | 600 | |
Kojto | 71:53949e6131f6 | 601 | /* STALL handshake packet */ |
Kojto | 71:53949e6131f6 | 602 | if (cepintsts & USBD_CEPINTSTS_STALLIF_Msk) |
Kojto | 71:53949e6131f6 | 603 | { |
Kojto | 71:53949e6131f6 | 604 | USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_STALLIF_Msk); |
Kojto | 71:53949e6131f6 | 605 | return; |
Kojto | 71:53949e6131f6 | 606 | } |
Kojto | 71:53949e6131f6 | 607 | |
Kojto | 71:53949e6131f6 | 608 | /* ERR special packet */ |
Kojto | 71:53949e6131f6 | 609 | if (cepintsts & USBD_CEPINTSTS_ERRIF_Msk) |
Kojto | 71:53949e6131f6 | 610 | { |
Kojto | 71:53949e6131f6 | 611 | USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_ERRIF_Msk); |
Kojto | 71:53949e6131f6 | 612 | return; |
Kojto | 71:53949e6131f6 | 613 | } |
Kojto | 71:53949e6131f6 | 614 | |
Kojto | 71:53949e6131f6 | 615 | /* Status stage transaction */ |
Kojto | 71:53949e6131f6 | 616 | if (cepintsts & USBD_CEPINTSTS_STSDONEIF_Msk) |
Kojto | 71:53949e6131f6 | 617 | { |
Kojto | 71:53949e6131f6 | 618 | if (s_usb_addr) |
Kojto | 71:53949e6131f6 | 619 | { |
Kojto | 71:53949e6131f6 | 620 | USBD_SET_ADDR(s_usb_addr); |
Kojto | 71:53949e6131f6 | 621 | s_usb_addr = 0; |
Kojto | 71:53949e6131f6 | 622 | } |
Kojto | 71:53949e6131f6 | 623 | USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_STSDONEIF_Msk); |
Kojto | 71:53949e6131f6 | 624 | USBD_ENABLE_CEP_INT(USBD_CEPINTEN_SETUPPKIEN_Msk); |
Kojto | 71:53949e6131f6 | 625 | return; |
Kojto | 71:53949e6131f6 | 626 | } |
Kojto | 71:53949e6131f6 | 627 | |
Kojto | 71:53949e6131f6 | 628 | /* Buffer Full */ |
Kojto | 71:53949e6131f6 | 629 | if (cepintsts & USBD_CEPINTSTS_BUFFULLIF_Msk) |
Kojto | 71:53949e6131f6 | 630 | { |
Kojto | 71:53949e6131f6 | 631 | USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_BUFFULLIF_Msk); |
Kojto | 71:53949e6131f6 | 632 | return; |
Kojto | 71:53949e6131f6 | 633 | } |
Kojto | 71:53949e6131f6 | 634 | |
Kojto | 71:53949e6131f6 | 635 | /* Buffer Empty */ |
Kojto | 71:53949e6131f6 | 636 | if (cepintsts & USBD_CEPINTSTS_BUFEMPTYIF_Msk) |
Kojto | 71:53949e6131f6 | 637 | { |
Kojto | 71:53949e6131f6 | 638 | USBD_CLR_CEP_INT_FLAG(USBD_CEPINTSTS_BUFEMPTYIF_Msk); |
Kojto | 71:53949e6131f6 | 639 | return; |
Kojto | 71:53949e6131f6 | 640 | } |
Kojto | 71:53949e6131f6 | 641 | } |
Kojto | 71:53949e6131f6 | 642 | /* EPA, EPB, EPC, ... EPL interrupts */ |
Kojto | 71:53949e6131f6 | 643 | uint32_t gintsts_epx = gintsts >> 2; |
Kojto | 71:53949e6131f6 | 644 | uint32_t ep_hw_index = 0; |
Kojto | 71:53949e6131f6 | 645 | while (gintsts_epx) { |
Kojto | 71:53949e6131f6 | 646 | if (gintsts_epx & 0x01) |
Kojto | 71:53949e6131f6 | 647 | { |
Kojto | 71:53949e6131f6 | 648 | uint32_t epxintsts = USBD_GET_EP_INT_FLAG(ep_hw_index) & USBD_GET_EP_INT_EN(ep_hw_index); |
Kojto | 71:53949e6131f6 | 649 | |
Kojto | 71:53949e6131f6 | 650 | USBD_CLR_EP_INT_FLAG(ep_hw_index, epxintsts); |
Kojto | 71:53949e6131f6 | 651 | |
Kojto | 71:53949e6131f6 | 652 | /* Buffer Full */ |
Kojto | 71:53949e6131f6 | 653 | if (epxintsts & USBD_EPINTSTS_BUFFULLIF_Msk) |
Kojto | 71:53949e6131f6 | 654 | { |
Kojto | 71:53949e6131f6 | 655 | } |
Kojto | 71:53949e6131f6 | 656 | |
Kojto | 71:53949e6131f6 | 657 | /* Buffer Empty */ |
Kojto | 71:53949e6131f6 | 658 | if (epxintsts & USBD_EPINTSTS_BUFEMPTYIF_Msk) |
Kojto | 71:53949e6131f6 | 659 | { |
Kojto | 71:53949e6131f6 | 660 | } |
Kojto | 71:53949e6131f6 | 661 | |
Kojto | 71:53949e6131f6 | 662 | /* Short Packet Transferred */ |
Kojto | 71:53949e6131f6 | 663 | if (epxintsts & USBD_EPINTSTS_SHORTTXIF_Msk) |
Kojto | 71:53949e6131f6 | 664 | { |
Kojto | 71:53949e6131f6 | 665 | } |
Kojto | 71:53949e6131f6 | 666 | |
Kojto | 71:53949e6131f6 | 667 | /* Data Packet Transmitted */ |
Kojto | 71:53949e6131f6 | 668 | if (epxintsts & USBD_EPINTSTS_TXPKIF_Msk) |
Kojto | 71:53949e6131f6 | 669 | { |
Kojto | 71:53949e6131f6 | 670 | s_ep_compl &= ~(1 << (NU_EPH2EPL(ep_hw_index))); |
Kojto | 71:53949e6131f6 | 671 | if ((instance->*(epCallback[ep_hw_index]))()) |
Kojto | 71:53949e6131f6 | 672 | { |
Kojto | 71:53949e6131f6 | 673 | } |
Kojto | 71:53949e6131f6 | 674 | } |
Kojto | 71:53949e6131f6 | 675 | |
Kojto | 71:53949e6131f6 | 676 | /* Data Packet Received */ |
Kojto | 71:53949e6131f6 | 677 | if (epxintsts & USBD_EPINTSTS_RXPKIF_Msk) |
Kojto | 71:53949e6131f6 | 678 | { |
Kojto | 71:53949e6131f6 | 679 | if ((instance->*(epCallback[ep_hw_index]))()) |
Kojto | 71:53949e6131f6 | 680 | { |
Kojto | 71:53949e6131f6 | 681 | |
Kojto | 71:53949e6131f6 | 682 | } |
Kojto | 71:53949e6131f6 | 683 | } |
Kojto | 71:53949e6131f6 | 684 | |
Kojto | 71:53949e6131f6 | 685 | /* OUT token packet */ |
Kojto | 71:53949e6131f6 | 686 | if (epxintsts & USBD_EPINTSTS_OUTTKIF_Msk) |
Kojto | 71:53949e6131f6 | 687 | { |
Kojto | 71:53949e6131f6 | 688 | } |
Kojto | 71:53949e6131f6 | 689 | |
Kojto | 71:53949e6131f6 | 690 | /* IN token packet */ |
Kojto | 71:53949e6131f6 | 691 | if (epxintsts & USBD_EPINTSTS_INTKIF_Msk) |
Kojto | 71:53949e6131f6 | 692 | { |
Kojto | 71:53949e6131f6 | 693 | } |
Kojto | 71:53949e6131f6 | 694 | |
Kojto | 71:53949e6131f6 | 695 | /* PING packet */ |
Kojto | 71:53949e6131f6 | 696 | if (epxintsts & USBD_EPINTSTS_PINGIF_Msk) |
Kojto | 71:53949e6131f6 | 697 | { |
Kojto | 71:53949e6131f6 | 698 | } |
Kojto | 71:53949e6131f6 | 699 | |
Kojto | 71:53949e6131f6 | 700 | /* NAK handshake packet sent to Host */ |
Kojto | 71:53949e6131f6 | 701 | if (epxintsts & USBD_EPINTSTS_NAKIF_Msk) |
Kojto | 71:53949e6131f6 | 702 | { |
Kojto | 71:53949e6131f6 | 703 | } |
Kojto | 71:53949e6131f6 | 704 | |
Kojto | 71:53949e6131f6 | 705 | /* STALL handshake packet sent to Host */ |
Kojto | 71:53949e6131f6 | 706 | if (epxintsts & USBD_EPINTSTS_STALLIF_Msk) |
Kojto | 71:53949e6131f6 | 707 | { |
Kojto | 71:53949e6131f6 | 708 | } |
Kojto | 71:53949e6131f6 | 709 | |
Kojto | 71:53949e6131f6 | 710 | /* NYET handshake packet sent to Host */ |
Kojto | 71:53949e6131f6 | 711 | if (epxintsts & USBD_EPINTSTS_NYETIF_Msk) |
Kojto | 71:53949e6131f6 | 712 | { |
Kojto | 71:53949e6131f6 | 713 | } |
Kojto | 71:53949e6131f6 | 714 | |
Kojto | 71:53949e6131f6 | 715 | /* ERR packet sent to Host */ |
Kojto | 71:53949e6131f6 | 716 | if (epxintsts & USBD_EPINTSTS_ERRIF_Msk) |
Kojto | 71:53949e6131f6 | 717 | { |
Kojto | 71:53949e6131f6 | 718 | } |
Kojto | 71:53949e6131f6 | 719 | |
Kojto | 71:53949e6131f6 | 720 | /* Bulk Out Short Packet Received */ |
Kojto | 71:53949e6131f6 | 721 | if (epxintsts & USBD_EPINTSTS_SHORTRXIF_Msk) |
Kojto | 71:53949e6131f6 | 722 | { |
Kojto | 71:53949e6131f6 | 723 | } |
Kojto | 71:53949e6131f6 | 724 | } |
Kojto | 71:53949e6131f6 | 725 | gintsts_epx = gintsts_epx >> 1; |
Kojto | 71:53949e6131f6 | 726 | ep_hw_index++; |
Kojto | 71:53949e6131f6 | 727 | } |
Kojto | 71:53949e6131f6 | 728 | } |
Kojto | 71:53949e6131f6 | 729 | #endif |
Kojto | 71:53949e6131f6 | 730 |