USB device stack
Dependents: USBMSD_step1 USBMSD_step1_5 picossd_step1_2cs
targets/TARGET_Silicon_Labs/src/em_usbdep.c@73:72808bd55ce2, 2021-09-15 (annotated)
- Committer:
- muraguchi
- Date:
- Wed Sep 15 16:31:51 2021 +0000
- Revision:
- 73:72808bd55ce2
- Parent:
- 71:53949e6131f6
AirioBase + 2 chip PicoSSD board
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 71:53949e6131f6 | 1 | /**************************************************************************//** |
Kojto | 71:53949e6131f6 | 2 | * @file em_usbdep.c |
Kojto | 71:53949e6131f6 | 3 | * @brief USB protocol stack library, USB device endpoint handlers. |
Kojto | 71:53949e6131f6 | 4 | * @version 3.20.14 |
Kojto | 71:53949e6131f6 | 5 | ****************************************************************************** |
Kojto | 71:53949e6131f6 | 6 | * @section License |
Kojto | 71:53949e6131f6 | 7 | * <b>(C) Copyright 2014 Silicon Labs, http://www.silabs.com</b> |
Kojto | 71:53949e6131f6 | 8 | ******************************************************************************* |
Kojto | 71:53949e6131f6 | 9 | * |
Kojto | 71:53949e6131f6 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Kojto | 71:53949e6131f6 | 11 | * you may not use this file except in compliance with the License. |
Kojto | 71:53949e6131f6 | 12 | * You may obtain a copy of the License at |
Kojto | 71:53949e6131f6 | 13 | * |
Kojto | 71:53949e6131f6 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
Kojto | 71:53949e6131f6 | 15 | * |
Kojto | 71:53949e6131f6 | 16 | * Unless required by applicable law or agreed to in writing, software |
Kojto | 71:53949e6131f6 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
Kojto | 71:53949e6131f6 | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Kojto | 71:53949e6131f6 | 19 | * See the License for the specific language governing permissions and |
Kojto | 71:53949e6131f6 | 20 | * limitations under the License. |
Kojto | 71:53949e6131f6 | 21 | * |
Kojto | 71:53949e6131f6 | 22 | ******************************************************************************/ |
Kojto | 71:53949e6131f6 | 23 | |
Kojto | 71:53949e6131f6 | 24 | #include "em_device.h" |
Kojto | 71:53949e6131f6 | 25 | #if defined( USB_PRESENT ) && ( USB_COUNT == 1 ) |
Kojto | 71:53949e6131f6 | 26 | #include "em_usb.h" |
Kojto | 71:53949e6131f6 | 27 | #if defined( USB_DEVICE ) |
Kojto | 71:53949e6131f6 | 28 | |
Kojto | 71:53949e6131f6 | 29 | #include "em_usbtypes.h" |
Kojto | 71:53949e6131f6 | 30 | #include "em_usbhal.h" |
Kojto | 71:53949e6131f6 | 31 | #include "em_usbd.h" |
Kojto | 71:53949e6131f6 | 32 | |
Kojto | 71:53949e6131f6 | 33 | #ifdef USB_USE_PRINTF |
Kojto | 71:53949e6131f6 | 34 | static const char *epStatusStr[] = { |
Kojto | 71:53949e6131f6 | 35 | "IDLE","TRANS","RECV","IN_S","OUT_S" |
Kojto | 71:53949e6131f6 | 36 | }; |
Kojto | 71:53949e6131f6 | 37 | #endif |
Kojto | 71:53949e6131f6 | 38 | |
Kojto | 71:53949e6131f6 | 39 | /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */ |
Kojto | 71:53949e6131f6 | 40 | |
Kojto | 71:53949e6131f6 | 41 | /* |
Kojto | 71:53949e6131f6 | 42 | * USBDEP_Ep0Handler() is called each time a packet has been transmitted |
Kojto | 71:53949e6131f6 | 43 | * or recieved on the default endpoint. |
Kojto | 71:53949e6131f6 | 44 | * A state machine navigate us through the phases of a control transfer |
Kojto | 71:53949e6131f6 | 45 | * according to "chapter 9" in the USB spec. |
Kojto | 71:53949e6131f6 | 46 | */ |
Kojto | 71:53949e6131f6 | 47 | #if !defined( USB_DOEP0INT_STUPPKTRCVD ) |
Kojto | 71:53949e6131f6 | 48 | void USBDEP_Ep0Handler( USBD_Device_TypeDef *device ) |
Kojto | 71:53949e6131f6 | 49 | { |
Kojto | 71:53949e6131f6 | 50 | int status; |
Kojto | 71:53949e6131f6 | 51 | USBD_Ep_TypeDef *ep; |
Kojto | 71:53949e6131f6 | 52 | static bool statusIn; |
Kojto | 71:53949e6131f6 | 53 | static uint32_t xferred; |
Kojto | 71:53949e6131f6 | 54 | static USB_XferCompleteCb_TypeDef callback; |
Kojto | 71:53949e6131f6 | 55 | |
Kojto | 71:53949e6131f6 | 56 | ep = &device->ep[ 0 ]; |
Kojto | 71:53949e6131f6 | 57 | |
Kojto | 71:53949e6131f6 | 58 | #ifdef __MBED__ |
Kojto | 71:53949e6131f6 | 59 | |
Kojto | 71:53949e6131f6 | 60 | (void)xferred; |
Kojto | 71:53949e6131f6 | 61 | (void)statusIn; |
Kojto | 71:53949e6131f6 | 62 | (void)status; |
Kojto | 71:53949e6131f6 | 63 | |
Kojto | 71:53949e6131f6 | 64 | USB_PRINTF("USBDEP: ep0 %s, rem %ld, z %d\n", epStatusStr[ep->state], ep->remaining, ep->zlp); |
Kojto | 71:53949e6131f6 | 65 | |
Kojto | 71:53949e6131f6 | 66 | if ( ( ep->state == D_EP_TRANSMITTING ) || ( ep->state == D_EP_RECEIVING ) ) |
Kojto | 71:53949e6131f6 | 67 | { |
Kojto | 71:53949e6131f6 | 68 | ep->state = D_EP_IDLE; |
Kojto | 71:53949e6131f6 | 69 | |
Kojto | 71:53949e6131f6 | 70 | if ( ep->xferCompleteCb ) |
Kojto | 71:53949e6131f6 | 71 | { |
Kojto | 71:53949e6131f6 | 72 | callback = ep->xferCompleteCb; |
Kojto | 71:53949e6131f6 | 73 | ep->xferCompleteCb = NULL; |
Kojto | 71:53949e6131f6 | 74 | callback( USB_STATUS_OK, ep->xferred, ep->remaining ); |
Kojto | 71:53949e6131f6 | 75 | } |
Kojto | 71:53949e6131f6 | 76 | |
Kojto | 71:53949e6131f6 | 77 | USBDHAL_ReenableEp0Setup(device); |
Kojto | 71:53949e6131f6 | 78 | } |
Kojto | 71:53949e6131f6 | 79 | else |
Kojto | 71:53949e6131f6 | 80 | { |
Kojto | 71:53949e6131f6 | 81 | device->callbacks->setupCmd(device->setup); |
Kojto | 71:53949e6131f6 | 82 | } |
Kojto | 71:53949e6131f6 | 83 | |
Kojto | 71:53949e6131f6 | 84 | #else /* not __MBED__ */ |
Kojto | 71:53949e6131f6 | 85 | |
Kojto | 71:53949e6131f6 | 86 | switch ( ep->state ) |
Kojto | 71:53949e6131f6 | 87 | { |
Kojto | 71:53949e6131f6 | 88 | case D_EP_IDLE: |
Kojto | 71:53949e6131f6 | 89 | ep->remaining = 0; |
Kojto | 71:53949e6131f6 | 90 | ep->zlp = 0; |
Kojto | 71:53949e6131f6 | 91 | callback = NULL; |
Kojto | 71:53949e6131f6 | 92 | statusIn = false; |
Kojto | 71:53949e6131f6 | 93 | |
Kojto | 71:53949e6131f6 | 94 | status = USBDCH9_SetupCmd( device ); |
Kojto | 71:53949e6131f6 | 95 | |
Kojto | 71:53949e6131f6 | 96 | if ( status == USB_STATUS_REQ_ERR ) |
Kojto | 71:53949e6131f6 | 97 | { |
Kojto | 71:53949e6131f6 | 98 | ep->in = true; |
Kojto | 71:53949e6131f6 | 99 | USBDHAL_StallEp( ep ); /* Stall Ep0 IN */ |
Kojto | 71:53949e6131f6 | 100 | ep->in = false; /* OUT for next SETUP */ |
Kojto | 71:53949e6131f6 | 101 | USBDHAL_StallEp( ep ); /* Stall Ep0 OUT */ |
Kojto | 71:53949e6131f6 | 102 | USBDHAL_ReenableEp0Setup( device ); /* Prepare for next SETUP packet*/ |
Kojto | 71:53949e6131f6 | 103 | } |
Kojto | 71:53949e6131f6 | 104 | else /* ( Status == USB_STATUS_OK ) */ |
Kojto | 71:53949e6131f6 | 105 | { |
Kojto | 71:53949e6131f6 | 106 | if ( (ep->state == D_EP_RECEIVING) || (ep->state == D_EP_TRANSMITTING) ) |
Kojto | 71:53949e6131f6 | 107 | { |
Kojto | 71:53949e6131f6 | 108 | callback = ep->xferCompleteCb; |
Kojto | 71:53949e6131f6 | 109 | } |
Kojto | 71:53949e6131f6 | 110 | |
Kojto | 71:53949e6131f6 | 111 | if ( ep->state != D_EP_RECEIVING ) |
Kojto | 71:53949e6131f6 | 112 | { |
Kojto | 71:53949e6131f6 | 113 | if ( ep->remaining ) |
Kojto | 71:53949e6131f6 | 114 | { |
Kojto | 71:53949e6131f6 | 115 | /* Data will be sent to host, check if a ZLP must be appended */ |
Kojto | 71:53949e6131f6 | 116 | if ( ( ep->remaining < device->setup->wLength ) && |
Kojto | 71:53949e6131f6 | 117 | ( ep->remaining % ep->packetSize == 0 ) ) |
Kojto | 71:53949e6131f6 | 118 | { |
Kojto | 71:53949e6131f6 | 119 | ep->zlp = 1; |
Kojto | 71:53949e6131f6 | 120 | } |
Kojto | 71:53949e6131f6 | 121 | } |
Kojto | 71:53949e6131f6 | 122 | else |
Kojto | 71:53949e6131f6 | 123 | { |
Kojto | 71:53949e6131f6 | 124 | /* Prepare for next SETUP packet*/ |
Kojto | 71:53949e6131f6 | 125 | USBDHAL_ReenableEp0Setup( device ); |
Kojto | 71:53949e6131f6 | 126 | |
Kojto | 71:53949e6131f6 | 127 | /* No data stage, a ZLP may have been sent. If not, send one */ |
Kojto | 71:53949e6131f6 | 128 | |
Kojto | 71:53949e6131f6 | 129 | xferred = 0; |
Kojto | 71:53949e6131f6 | 130 | if ( ep->zlp == 0 ) |
Kojto | 71:53949e6131f6 | 131 | { |
Kojto | 71:53949e6131f6 | 132 | USBD_Write( 0, NULL, 0, NULL ); /* ACK to host */ |
Kojto | 71:53949e6131f6 | 133 | ep->state = D_EP0_IN_STATUS; |
Kojto | 71:53949e6131f6 | 134 | } |
Kojto | 71:53949e6131f6 | 135 | else |
Kojto | 71:53949e6131f6 | 136 | { |
Kojto | 71:53949e6131f6 | 137 | ep->state = D_EP_IDLE; |
Kojto | 71:53949e6131f6 | 138 | ep->in = false; /* OUT for next SETUP */ |
Kojto | 71:53949e6131f6 | 139 | } |
Kojto | 71:53949e6131f6 | 140 | } |
Kojto | 71:53949e6131f6 | 141 | } |
Kojto | 71:53949e6131f6 | 142 | } |
Kojto | 71:53949e6131f6 | 143 | break; |
Kojto | 71:53949e6131f6 | 144 | |
Kojto | 71:53949e6131f6 | 145 | case D_EP_RECEIVING: |
Kojto | 71:53949e6131f6 | 146 | if ( ep->remaining ) |
Kojto | 71:53949e6131f6 | 147 | { |
Kojto | 71:53949e6131f6 | 148 | /* There is more data to receive */ |
Kojto | 71:53949e6131f6 | 149 | USBD_ReArmEp0( ep ); |
Kojto | 71:53949e6131f6 | 150 | } |
Kojto | 71:53949e6131f6 | 151 | else |
Kojto | 71:53949e6131f6 | 152 | { |
Kojto | 71:53949e6131f6 | 153 | status = USB_STATUS_OK; |
Kojto | 71:53949e6131f6 | 154 | if ( callback != NULL ) |
Kojto | 71:53949e6131f6 | 155 | { |
Kojto | 71:53949e6131f6 | 156 | status = callback( USB_STATUS_OK, ep->xferred, 0 ); |
Kojto | 71:53949e6131f6 | 157 | callback = NULL; |
Kojto | 71:53949e6131f6 | 158 | } |
Kojto | 71:53949e6131f6 | 159 | |
Kojto | 71:53949e6131f6 | 160 | if ( status != USB_STATUS_OK ) |
Kojto | 71:53949e6131f6 | 161 | { |
Kojto | 71:53949e6131f6 | 162 | ep->in = true; |
Kojto | 71:53949e6131f6 | 163 | USBDHAL_StallEp( ep ); /* Stall Ep0 IN */ |
Kojto | 71:53949e6131f6 | 164 | ep->in = false; /* OUT for next SETUP */ |
Kojto | 71:53949e6131f6 | 165 | USBDHAL_StallEp( ep ); /* Stall Ep0 OUT */ |
Kojto | 71:53949e6131f6 | 166 | USBDHAL_ReenableEp0Setup( device ); /* Prepare for next SETUP pkt. */ |
Kojto | 71:53949e6131f6 | 167 | ep->state = D_EP_IDLE; |
Kojto | 71:53949e6131f6 | 168 | } |
Kojto | 71:53949e6131f6 | 169 | else /* Everything OK, send a ZLP (ACK) to host */ |
Kojto | 71:53949e6131f6 | 170 | { |
Kojto | 71:53949e6131f6 | 171 | USBDHAL_ReenableEp0Setup( device );/* Prepare for next SETUP packet*/ |
Kojto | 71:53949e6131f6 | 172 | |
Kojto | 71:53949e6131f6 | 173 | ep->state = D_EP_IDLE; /* USBD_Write() sets state back*/ |
Kojto | 71:53949e6131f6 | 174 | /* to EP_TRANSMITTING */ |
Kojto | 71:53949e6131f6 | 175 | USBD_Write( 0, NULL, 0, NULL ); |
Kojto | 71:53949e6131f6 | 176 | ep->state = D_EP0_IN_STATUS; |
Kojto | 71:53949e6131f6 | 177 | } |
Kojto | 71:53949e6131f6 | 178 | } |
Kojto | 71:53949e6131f6 | 179 | break; |
Kojto | 71:53949e6131f6 | 180 | |
Kojto | 71:53949e6131f6 | 181 | case D_EP_TRANSMITTING: |
Kojto | 71:53949e6131f6 | 182 | if ( ep->remaining ) |
Kojto | 71:53949e6131f6 | 183 | { |
Kojto | 71:53949e6131f6 | 184 | /* There is more data to transmit */ |
Kojto | 71:53949e6131f6 | 185 | USBD_ReArmEp0( ep ); |
Kojto | 71:53949e6131f6 | 186 | } |
Kojto | 71:53949e6131f6 | 187 | else |
Kojto | 71:53949e6131f6 | 188 | { |
Kojto | 71:53949e6131f6 | 189 | /* All data transferred, is a ZLP packet needed ? */ |
Kojto | 71:53949e6131f6 | 190 | if ( ep->zlp == 1 ) |
Kojto | 71:53949e6131f6 | 191 | { |
Kojto | 71:53949e6131f6 | 192 | xferred = ep->xferred; |
Kojto | 71:53949e6131f6 | 193 | ep->state = D_EP_IDLE; /* USBD_Write() sets state back */ |
Kojto | 71:53949e6131f6 | 194 | /* to EP_TRANSMITTING */ |
Kojto | 71:53949e6131f6 | 195 | USBD_Write( 0, NULL, 0, NULL ); /* Send ZLP */ |
Kojto | 71:53949e6131f6 | 196 | ep->zlp = 2; |
Kojto | 71:53949e6131f6 | 197 | } |
Kojto | 71:53949e6131f6 | 198 | else |
Kojto | 71:53949e6131f6 | 199 | { |
Kojto | 71:53949e6131f6 | 200 | if ( ep->zlp == 0 ) |
Kojto | 71:53949e6131f6 | 201 | { |
Kojto | 71:53949e6131f6 | 202 | xferred = ep->xferred; |
Kojto | 71:53949e6131f6 | 203 | } |
Kojto | 71:53949e6131f6 | 204 | |
Kojto | 71:53949e6131f6 | 205 | ep->state = D_EP_IDLE; |
Kojto | 71:53949e6131f6 | 206 | USBD_Read( 0, NULL, 0, NULL ); /* Get ZLP packet (ACK) from host */ |
Kojto | 71:53949e6131f6 | 207 | statusIn = true; |
Kojto | 71:53949e6131f6 | 208 | ep->state = D_EP0_OUT_STATUS; |
Kojto | 71:53949e6131f6 | 209 | } |
Kojto | 71:53949e6131f6 | 210 | } |
Kojto | 71:53949e6131f6 | 211 | break; |
Kojto | 71:53949e6131f6 | 212 | |
Kojto | 71:53949e6131f6 | 213 | case D_EP0_IN_STATUS: |
Kojto | 71:53949e6131f6 | 214 | case D_EP0_OUT_STATUS: |
Kojto | 71:53949e6131f6 | 215 | if ( statusIn ) |
Kojto | 71:53949e6131f6 | 216 | { |
Kojto | 71:53949e6131f6 | 217 | USBDHAL_ReenableEp0Setup( device ); |
Kojto | 71:53949e6131f6 | 218 | } |
Kojto | 71:53949e6131f6 | 219 | |
Kojto | 71:53949e6131f6 | 220 | if ( callback != NULL ) |
Kojto | 71:53949e6131f6 | 221 | { |
Kojto | 71:53949e6131f6 | 222 | callback( USB_STATUS_OK, xferred, 0 ); |
Kojto | 71:53949e6131f6 | 223 | } |
Kojto | 71:53949e6131f6 | 224 | |
Kojto | 71:53949e6131f6 | 225 | ep->state = D_EP_IDLE; |
Kojto | 71:53949e6131f6 | 226 | ep->in = false; /* OUT for next SETUP */ |
Kojto | 71:53949e6131f6 | 227 | break; |
Kojto | 71:53949e6131f6 | 228 | |
Kojto | 71:53949e6131f6 | 229 | default: |
Kojto | 71:53949e6131f6 | 230 | EFM_ASSERT( false ); |
Kojto | 71:53949e6131f6 | 231 | break; |
Kojto | 71:53949e6131f6 | 232 | } |
Kojto | 71:53949e6131f6 | 233 | #endif /* __MBED__ */ |
Kojto | 71:53949e6131f6 | 234 | } |
Kojto | 71:53949e6131f6 | 235 | #endif |
Kojto | 71:53949e6131f6 | 236 | |
Kojto | 71:53949e6131f6 | 237 | #if defined( USB_DOEP0INT_STUPPKTRCVD ) |
Kojto | 71:53949e6131f6 | 238 | void USBDEP_Ep0Handler( USBD_Device_TypeDef *device ) |
Kojto | 71:53949e6131f6 | 239 | { |
Kojto | 71:53949e6131f6 | 240 | int status; |
Kojto | 71:53949e6131f6 | 241 | USBD_Ep_TypeDef *ep; |
Kojto | 71:53949e6131f6 | 242 | static uint32_t xferred; |
Kojto | 71:53949e6131f6 | 243 | static USB_XferCompleteCb_TypeDef callback; |
Kojto | 71:53949e6131f6 | 244 | |
Kojto | 71:53949e6131f6 | 245 | #ifdef __MBED__ |
Kojto | 71:53949e6131f6 | 246 | |
Kojto | 71:53949e6131f6 | 247 | (void)xferred; |
Kojto | 71:53949e6131f6 | 248 | (void)status; |
Kojto | 71:53949e6131f6 | 249 | |
Kojto | 71:53949e6131f6 | 250 | ep = &device->ep[ 0 ]; |
Kojto | 71:53949e6131f6 | 251 | |
Kojto | 71:53949e6131f6 | 252 | if ( ( ep->state == D_EP_TRANSMITTING ) || ( ep->state == D_EP_RECEIVING ) ) |
Kojto | 71:53949e6131f6 | 253 | { |
Kojto | 71:53949e6131f6 | 254 | ep->state = D_EP_IDLE; |
Kojto | 71:53949e6131f6 | 255 | |
Kojto | 71:53949e6131f6 | 256 | if ( ep->xferCompleteCb ) |
Kojto | 71:53949e6131f6 | 257 | { |
Kojto | 71:53949e6131f6 | 258 | callback = ep->xferCompleteCb; |
Kojto | 71:53949e6131f6 | 259 | ep->xferCompleteCb = NULL; |
Kojto | 71:53949e6131f6 | 260 | callback( USB_STATUS_OK, ep->xferred, ep->remaining ); |
Kojto | 71:53949e6131f6 | 261 | } |
Kojto | 71:53949e6131f6 | 262 | |
Kojto | 71:53949e6131f6 | 263 | USBDHAL_StartEp0Setup( dev ); |
Kojto | 71:53949e6131f6 | 264 | } |
Kojto | 71:53949e6131f6 | 265 | else |
Kojto | 71:53949e6131f6 | 266 | { |
Kojto | 71:53949e6131f6 | 267 | device->callbacks->setupCmd(device->setup); |
Kojto | 71:53949e6131f6 | 268 | } |
Kojto | 71:53949e6131f6 | 269 | |
Kojto | 71:53949e6131f6 | 270 | #else |
Kojto | 71:53949e6131f6 | 271 | |
Kojto | 71:53949e6131f6 | 272 | ep = &device->ep[ 0 ]; |
Kojto | 71:53949e6131f6 | 273 | |
Kojto | 71:53949e6131f6 | 274 | switch ( ep->state ) |
Kojto | 71:53949e6131f6 | 275 | { |
Kojto | 71:53949e6131f6 | 276 | case D_EP_IDLE: |
Kojto | 71:53949e6131f6 | 277 | ep->zlp = 0; |
Kojto | 71:53949e6131f6 | 278 | ep->remaining = 0; |
Kojto | 71:53949e6131f6 | 279 | callback = NULL; |
Kojto | 71:53949e6131f6 | 280 | |
Kojto | 71:53949e6131f6 | 281 | status = USBDCH9_SetupCmd( device ); |
Kojto | 71:53949e6131f6 | 282 | |
Kojto | 71:53949e6131f6 | 283 | if ( status == USB_STATUS_REQ_ERR ) |
Kojto | 71:53949e6131f6 | 284 | { |
Kojto | 71:53949e6131f6 | 285 | ep->in = true; |
Kojto | 71:53949e6131f6 | 286 | USBDHAL_StallEp( ep ); /* Stall Ep0 IN */ |
Kojto | 71:53949e6131f6 | 287 | ep->in = false; /* OUT for next SETUP */ |
Kojto | 71:53949e6131f6 | 288 | USBDHAL_StallEp( ep ); /* Stall Ep0 OUT */ |
Kojto | 71:53949e6131f6 | 289 | USBDHAL_StartEp0Setup( dev ); /* Prepare for next SETUP packet*/ |
Kojto | 71:53949e6131f6 | 290 | } |
Kojto | 71:53949e6131f6 | 291 | else /* ( Status == USB_STATUS_OK ) */ |
Kojto | 71:53949e6131f6 | 292 | { |
Kojto | 71:53949e6131f6 | 293 | if ( (ep->state == D_EP_RECEIVING) || (ep->state == D_EP_TRANSMITTING) ) |
Kojto | 71:53949e6131f6 | 294 | { |
Kojto | 71:53949e6131f6 | 295 | callback = ep->xferCompleteCb; |
Kojto | 71:53949e6131f6 | 296 | } |
Kojto | 71:53949e6131f6 | 297 | |
Kojto | 71:53949e6131f6 | 298 | if ( ep->state != D_EP_RECEIVING ) |
Kojto | 71:53949e6131f6 | 299 | { |
Kojto | 71:53949e6131f6 | 300 | if ( ep->remaining ) |
Kojto | 71:53949e6131f6 | 301 | { |
Kojto | 71:53949e6131f6 | 302 | /* Data will be sent to host, check if a ZLP must be appended */ |
Kojto | 71:53949e6131f6 | 303 | if ( ( ep->remaining < device->setup->wLength ) && |
Kojto | 71:53949e6131f6 | 304 | ( ep->remaining % ep->packetSize == 0 ) ) |
Kojto | 71:53949e6131f6 | 305 | { |
Kojto | 71:53949e6131f6 | 306 | ep->zlp = 1; |
Kojto | 71:53949e6131f6 | 307 | } |
Kojto | 71:53949e6131f6 | 308 | } |
Kojto | 71:53949e6131f6 | 309 | else |
Kojto | 71:53949e6131f6 | 310 | { |
Kojto | 71:53949e6131f6 | 311 | /* No data stage, a ZLP may have been sent. If not, send one */ |
Kojto | 71:53949e6131f6 | 312 | xferred = 0; |
Kojto | 71:53949e6131f6 | 313 | if ( ep->zlp == 0 ) |
Kojto | 71:53949e6131f6 | 314 | { |
Kojto | 71:53949e6131f6 | 315 | ep->state = D_EP_IDLE; |
Kojto | 71:53949e6131f6 | 316 | USBD_Write( 0, NULL, 0, NULL ); /* ACK to host */ |
Kojto | 71:53949e6131f6 | 317 | ep->state = D_EP0_IN_STATUS; |
Kojto | 71:53949e6131f6 | 318 | } |
Kojto | 71:53949e6131f6 | 319 | } |
Kojto | 71:53949e6131f6 | 320 | } |
Kojto | 71:53949e6131f6 | 321 | } |
Kojto | 71:53949e6131f6 | 322 | break; |
Kojto | 71:53949e6131f6 | 323 | |
Kojto | 71:53949e6131f6 | 324 | case D_EP_RECEIVING: |
Kojto | 71:53949e6131f6 | 325 | if ( ep->remaining ) |
Kojto | 71:53949e6131f6 | 326 | { |
Kojto | 71:53949e6131f6 | 327 | ep->in = false; |
Kojto | 71:53949e6131f6 | 328 | USBD_ReArmEp0( ep ); |
Kojto | 71:53949e6131f6 | 329 | } |
Kojto | 71:53949e6131f6 | 330 | else |
Kojto | 71:53949e6131f6 | 331 | { |
Kojto | 71:53949e6131f6 | 332 | status = USB_STATUS_OK; |
Kojto | 71:53949e6131f6 | 333 | if ( callback != NULL ) |
Kojto | 71:53949e6131f6 | 334 | { |
Kojto | 71:53949e6131f6 | 335 | status = callback( USB_STATUS_OK, ep->xferred, 0 ); |
Kojto | 71:53949e6131f6 | 336 | callback = NULL; |
Kojto | 71:53949e6131f6 | 337 | } |
Kojto | 71:53949e6131f6 | 338 | |
Kojto | 71:53949e6131f6 | 339 | if ( status != USB_STATUS_OK ) |
Kojto | 71:53949e6131f6 | 340 | { |
Kojto | 71:53949e6131f6 | 341 | ep->in = true; |
Kojto | 71:53949e6131f6 | 342 | USBDHAL_StallEp( ep ); /* Stall Ep0 IN */ |
Kojto | 71:53949e6131f6 | 343 | ep->in = false; /* OUT for next SETUP */ |
Kojto | 71:53949e6131f6 | 344 | USBDHAL_StallEp( ep ); /* Stall Ep0 OUT */ |
Kojto | 71:53949e6131f6 | 345 | USBDHAL_StartEp0Setup( dev ); /* Prepare for next SETUP pkt. */ |
Kojto | 71:53949e6131f6 | 346 | ep->state = D_EP_IDLE; |
Kojto | 71:53949e6131f6 | 347 | } |
Kojto | 71:53949e6131f6 | 348 | else |
Kojto | 71:53949e6131f6 | 349 | { |
Kojto | 71:53949e6131f6 | 350 | |
Kojto | 71:53949e6131f6 | 351 | USBDHAL_StartEp0Setup( dev ); /* Prepare for next SETUP packet*/ |
Kojto | 71:53949e6131f6 | 352 | ep->state = D_EP_IDLE; /* USBD_Write() sets state back */ |
Kojto | 71:53949e6131f6 | 353 | /* to EP_TRANSMITTING */ |
Kojto | 71:53949e6131f6 | 354 | USBD_Write( 0, NULL, 0, NULL ); |
Kojto | 71:53949e6131f6 | 355 | ep->state = D_EP0_IN_STATUS; |
Kojto | 71:53949e6131f6 | 356 | } |
Kojto | 71:53949e6131f6 | 357 | } |
Kojto | 71:53949e6131f6 | 358 | break; |
Kojto | 71:53949e6131f6 | 359 | |
Kojto | 71:53949e6131f6 | 360 | case D_EP_TRANSMITTING: |
Kojto | 71:53949e6131f6 | 361 | if ( ep->remaining ) |
Kojto | 71:53949e6131f6 | 362 | { |
Kojto | 71:53949e6131f6 | 363 | ep->in = true; |
Kojto | 71:53949e6131f6 | 364 | USBD_ReArmEp0( ep ); |
Kojto | 71:53949e6131f6 | 365 | } |
Kojto | 71:53949e6131f6 | 366 | else |
Kojto | 71:53949e6131f6 | 367 | { |
Kojto | 71:53949e6131f6 | 368 | if ( ep->zlp == 1 ) |
Kojto | 71:53949e6131f6 | 369 | { |
Kojto | 71:53949e6131f6 | 370 | xferred = ep->xferred; |
Kojto | 71:53949e6131f6 | 371 | ep->state = D_EP_IDLE; /* USBD_Write() sets state back */ |
Kojto | 71:53949e6131f6 | 372 | /* to EP_TRANSMITTING */ |
Kojto | 71:53949e6131f6 | 373 | USBD_Write( 0, NULL, 0, NULL ); /* Send ZLP */ |
Kojto | 71:53949e6131f6 | 374 | ep->zlp = 2; |
Kojto | 71:53949e6131f6 | 375 | } |
Kojto | 71:53949e6131f6 | 376 | else |
Kojto | 71:53949e6131f6 | 377 | { |
Kojto | 71:53949e6131f6 | 378 | if ( ep->zlp == 0 ) |
Kojto | 71:53949e6131f6 | 379 | { |
Kojto | 71:53949e6131f6 | 380 | xferred = ep->xferred; |
Kojto | 71:53949e6131f6 | 381 | } |
Kojto | 71:53949e6131f6 | 382 | |
Kojto | 71:53949e6131f6 | 383 | ep->state = D_EP_IDLE; |
Kojto | 71:53949e6131f6 | 384 | USBD_Read( 0, NULL, 0, NULL ); /* Get ZLP packet (ACK) from host */ |
Kojto | 71:53949e6131f6 | 385 | ep->state = D_EP0_OUT_STATUS; |
Kojto | 71:53949e6131f6 | 386 | } |
Kojto | 71:53949e6131f6 | 387 | } |
Kojto | 71:53949e6131f6 | 388 | break; |
Kojto | 71:53949e6131f6 | 389 | |
Kojto | 71:53949e6131f6 | 390 | case D_EP0_IN_STATUS: |
Kojto | 71:53949e6131f6 | 391 | if ( ( USB->DOEP0CTL & USB_DOEP0CTL_EPENA ) == 0 ) |
Kojto | 71:53949e6131f6 | 392 | { |
Kojto | 71:53949e6131f6 | 393 | /* Prepare for more SETUP Packets */ |
Kojto | 71:53949e6131f6 | 394 | USBDHAL_StartEp0Setup( dev ); |
Kojto | 71:53949e6131f6 | 395 | } |
Kojto | 71:53949e6131f6 | 396 | if ( callback != NULL ) |
Kojto | 71:53949e6131f6 | 397 | { |
Kojto | 71:53949e6131f6 | 398 | callback( USB_STATUS_OK, xferred, 0 ); |
Kojto | 71:53949e6131f6 | 399 | } |
Kojto | 71:53949e6131f6 | 400 | ep->state = D_EP_IDLE; |
Kojto | 71:53949e6131f6 | 401 | ep->in = false; /* OUT for next SETUP */ |
Kojto | 71:53949e6131f6 | 402 | break; |
Kojto | 71:53949e6131f6 | 403 | |
Kojto | 71:53949e6131f6 | 404 | case D_EP0_OUT_STATUS: |
Kojto | 71:53949e6131f6 | 405 | USBDHAL_StartEp0Setup( dev ); /* Prepare for more SETUP Packets */ |
Kojto | 71:53949e6131f6 | 406 | if ( callback != NULL ) |
Kojto | 71:53949e6131f6 | 407 | { |
Kojto | 71:53949e6131f6 | 408 | callback( USB_STATUS_OK, xferred, 0 ); |
Kojto | 71:53949e6131f6 | 409 | } |
Kojto | 71:53949e6131f6 | 410 | ep->state = D_EP_IDLE; |
Kojto | 71:53949e6131f6 | 411 | ep->in = false; /* OUT for next SETUP */ |
Kojto | 71:53949e6131f6 | 412 | break; |
Kojto | 71:53949e6131f6 | 413 | } |
Kojto | 71:53949e6131f6 | 414 | #endif /* __MBED__ */ |
Kojto | 71:53949e6131f6 | 415 | } |
Kojto | 71:53949e6131f6 | 416 | #endif |
Kojto | 71:53949e6131f6 | 417 | |
Kojto | 71:53949e6131f6 | 418 | /* |
Kojto | 71:53949e6131f6 | 419 | * USBDEP_EpHandler() is called each time a packet has been transmitted |
Kojto | 71:53949e6131f6 | 420 | * or recieved on an endpoint other than the default endpoint. |
Kojto | 71:53949e6131f6 | 421 | */ |
Kojto | 71:53949e6131f6 | 422 | void USBDEP_EpHandler( uint8_t epAddr ) |
Kojto | 71:53949e6131f6 | 423 | { |
Kojto | 71:53949e6131f6 | 424 | USB_XferCompleteCb_TypeDef callback; |
Kojto | 71:53949e6131f6 | 425 | USBD_Ep_TypeDef *ep = USBD_GetEpFromAddr( epAddr ); |
Kojto | 71:53949e6131f6 | 426 | |
Kojto | 71:53949e6131f6 | 427 | if ( ( ep->state == D_EP_TRANSMITTING ) || ( ep->state == D_EP_RECEIVING ) ) |
Kojto | 71:53949e6131f6 | 428 | { |
Kojto | 71:53949e6131f6 | 429 | ep->state = D_EP_IDLE; |
Kojto | 71:53949e6131f6 | 430 | if ( ep->xferCompleteCb ) |
Kojto | 71:53949e6131f6 | 431 | { |
Kojto | 71:53949e6131f6 | 432 | callback = ep->xferCompleteCb; |
Kojto | 71:53949e6131f6 | 433 | ep->xferCompleteCb = NULL; |
Kojto | 71:53949e6131f6 | 434 | callback( USB_STATUS_OK, ep->xferred, ep->remaining ); |
Kojto | 71:53949e6131f6 | 435 | } |
Kojto | 71:53949e6131f6 | 436 | } |
Kojto | 71:53949e6131f6 | 437 | else |
Kojto | 71:53949e6131f6 | 438 | { |
Kojto | 71:53949e6131f6 | 439 | EFM_ASSERT( false ); |
Kojto | 71:53949e6131f6 | 440 | } |
Kojto | 71:53949e6131f6 | 441 | } |
Kojto | 71:53949e6131f6 | 442 | |
Kojto | 71:53949e6131f6 | 443 | /** @endcond */ |
Kojto | 71:53949e6131f6 | 444 | |
Kojto | 71:53949e6131f6 | 445 | #endif /* defined( USB_DEVICE ) */ |
Kojto | 71:53949e6131f6 | 446 | #endif /* defined( USB_PRESENT ) && ( USB_COUNT == 1 ) */ |