USB device stack - Added support for the logo/windows key to USB keyboard.

Dependents:   randomSearch

Fork of USBDevice by mbed official

Committer:
mbed_official
Date:
Thu Aug 13 15:46:06 2015 +0100
Revision:
59:2af474687369
Synchronized with git revision 376d6a73e345b728a788041adb166b08cd8d2b95

Full URL: https://github.com/mbedmicro/mbed/commit/376d6a73e345b728a788041adb166b08cd8d2b95/

Silicon Labs - Add support for USBDevice

Who changed what in which revision?

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