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