USBDevice for STM support

Dependents:   Nucleo_Usb_JoyMouse Nucleo_usbmouse ELEC350_1-referral-2018-usb-hid USBJoystick_HelloWorld2_wip ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHAL_KL25Z.cpp Source File

USBHAL_KL25Z.cpp

00001 /* Copyright (c) 2010-2011 mbed.org, MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 */
00018 
00019 #if defined(TARGET_KL25Z) | defined(TARGET_KL43Z) | defined(TARGET_KL46Z) | defined(TARGET_K20D50M) | defined(TARGET_K64F) | defined(TARGET_K22F) | defined(TARGET_TEENSY3_1)
00020 
00021 #if defined(TARGET_KSDK2_MCUS)
00022 #include "fsl_common.h"
00023 #endif
00024 #include "USBHAL.h"
00025 
00026 USBHAL * USBHAL::instance;
00027 
00028 static volatile int epComplete = 0;
00029 
00030 // Convert physical endpoint number to register bit
00031 #define EP(endpoint) (1<<(endpoint))
00032 
00033 // Convert physical to logical
00034 #define PHY_TO_LOG(endpoint)    ((endpoint)>>1)
00035 
00036 // Get endpoint direction
00037 #define IN_EP(endpoint)     ((endpoint) & 1U ? true : false)
00038 #define OUT_EP(endpoint)    ((endpoint) & 1U ? false : true)
00039 
00040 #define BD_OWN_MASK        (1<<7)
00041 #define BD_DATA01_MASK     (1<<6)
00042 #define BD_KEEP_MASK       (1<<5)
00043 #define BD_NINC_MASK       (1<<4)
00044 #define BD_DTS_MASK        (1<<3)
00045 #define BD_STALL_MASK      (1<<2)
00046 
00047 #define TX    1
00048 #define RX    0
00049 #define ODD   0
00050 #define EVEN  1
00051 // this macro waits a physical endpoint number
00052 #define EP_BDT_IDX(ep, dir, odd) (((ep * 4) + (2 * dir) + (1 *  odd)))
00053 
00054 #define SETUP_TOKEN    0x0D
00055 #define IN_TOKEN       0x09
00056 #define OUT_TOKEN      0x01
00057 #define TOK_PID(idx)   ((bdt[idx].info >> 2) & 0x0F)
00058 
00059 // for each endpt: 8 bytes
00060 typedef struct BDT {
00061     uint8_t   info;       // BD[0:7]
00062     uint8_t   dummy;      // RSVD: BD[8:15]
00063     uint16_t  byte_count; // BD[16:32]
00064     uint32_t  address;    // Addr
00065 } BDT;
00066 
00067 
00068 // there are:
00069 //    * 16 bidirectionnal endpt -> 32 physical endpt
00070 //    * as there are ODD and EVEN buffer -> 32*2 bdt
00071 MBED_ALIGN(512) BDT bdt[NUMBER_OF_PHYSICAL_ENDPOINTS * 2];  // 512 bytes aligned!
00072 
00073 uint8_t * endpoint_buffer[(NUMBER_OF_PHYSICAL_ENDPOINTS - 2) * 2];
00074 uint8_t * endpoint_buffer_iso[2*2];
00075 
00076 static uint8_t set_addr = 0;
00077 static uint8_t addr = 0;
00078 
00079 static uint32_t Data1  = 0x55555555;
00080 
00081 static uint32_t frameNumber() {
00082     return((USB0->FRMNUML | (USB0->FRMNUMH << 8)) & 0x07FF);
00083 }
00084 
00085 uint32_t USBHAL::endpointReadcore(uint8_t endpoint, uint8_t *buffer) {
00086     return 0;
00087 }
00088 
00089 USBHAL::USBHAL(void) {
00090     // Disable IRQ
00091     NVIC_DisableIRQ(USB0_IRQn);
00092 
00093 #if (defined(FSL_FEATURE_SOC_MPU_COUNT) && (FSL_FEATURE_SOC_MPU_COUNT > 0U))
00094     MPU->CESR=0;
00095 #endif
00096     // fill in callback array
00097     epCallback[0] = &USBHAL::EP1_OUT_callback;
00098     epCallback[1] = &USBHAL::EP1_IN_callback;
00099     epCallback[2] = &USBHAL::EP2_OUT_callback;
00100     epCallback[3] = &USBHAL::EP2_IN_callback;
00101     epCallback[4] = &USBHAL::EP3_OUT_callback;
00102     epCallback[5] = &USBHAL::EP3_IN_callback;
00103     epCallback[6] = &USBHAL::EP4_OUT_callback;
00104     epCallback[7] = &USBHAL::EP4_IN_callback;
00105     epCallback[8] = &USBHAL::EP5_OUT_callback;
00106     epCallback[9] = &USBHAL::EP5_IN_callback;
00107     epCallback[10] = &USBHAL::EP6_OUT_callback;
00108     epCallback[11] = &USBHAL::EP6_IN_callback;
00109     epCallback[12] = &USBHAL::EP7_OUT_callback;
00110     epCallback[13] = &USBHAL::EP7_IN_callback;
00111     epCallback[14] = &USBHAL::EP8_OUT_callback;
00112     epCallback[15] = &USBHAL::EP8_IN_callback;
00113     epCallback[16] = &USBHAL::EP9_OUT_callback;
00114     epCallback[17] = &USBHAL::EP9_IN_callback;
00115     epCallback[18] = &USBHAL::EP10_OUT_callback;
00116     epCallback[19] = &USBHAL::EP10_IN_callback;
00117     epCallback[20] = &USBHAL::EP11_OUT_callback;
00118     epCallback[21] = &USBHAL::EP11_IN_callback;
00119     epCallback[22] = &USBHAL::EP12_OUT_callback;
00120     epCallback[23] = &USBHAL::EP12_IN_callback;
00121     epCallback[24] = &USBHAL::EP13_OUT_callback;
00122     epCallback[25] = &USBHAL::EP13_IN_callback;
00123     epCallback[26] = &USBHAL::EP14_OUT_callback;
00124     epCallback[27] = &USBHAL::EP14_IN_callback;
00125     epCallback[28] = &USBHAL::EP15_OUT_callback;
00126     epCallback[29] = &USBHAL::EP15_IN_callback;
00127 
00128 #if defined(TARGET_KL43Z) || defined(TARGET_K22F) || defined(TARGET_K64F)
00129     // enable USBFS clock
00130     CLOCK_EnableUsbfs0Clock(kCLOCK_UsbSrcIrc48M, 48000000U);
00131 #else
00132     // choose usb src as PLL
00133     SIM->SOPT2 &= ~SIM_SOPT2_PLLFLLSEL_MASK;
00134     SIM->SOPT2 |= (SIM_SOPT2_USBSRC_MASK | (1 << SIM_SOPT2_PLLFLLSEL_SHIFT));
00135 
00136     // enable OTG clock
00137     SIM->SCGC4 |= SIM_SCGC4_USBOTG_MASK;
00138 #endif
00139 
00140     // Attach IRQ
00141     instance = this;
00142     NVIC_SetVector(USB0_IRQn, (uint32_t)&_usbisr);
00143     NVIC_EnableIRQ(USB0_IRQn);
00144 
00145     // USB Module Configuration
00146     // Set BDT Base Register
00147     USB0->BDTPAGE1 = (uint8_t)((uint32_t)bdt>>8);
00148     USB0->BDTPAGE2 = (uint8_t)((uint32_t)bdt>>16);
00149     USB0->BDTPAGE3 = (uint8_t)((uint32_t)bdt>>24);
00150 
00151     // Clear interrupt flag
00152     USB0->ISTAT = 0xff;
00153 
00154     // USB Interrupt Enablers
00155     USB0->INTEN |= USB_INTEN_TOKDNEEN_MASK |
00156                    USB_INTEN_SOFTOKEN_MASK |
00157                    USB_INTEN_ERROREN_MASK  |
00158                    USB_INTEN_USBRSTEN_MASK;
00159 
00160     // Disable weak pull downs
00161     USB0->USBCTRL &= ~(USB_USBCTRL_PDE_MASK | USB_USBCTRL_SUSP_MASK);
00162 
00163     USB0->USBTRC0 |= 0x40;
00164 }
00165 
00166 USBHAL::~USBHAL(void) { }
00167 
00168 void USBHAL::connect(void) {
00169     // enable USB
00170     USB0->CTL |= USB_CTL_USBENSOFEN_MASK;
00171     // Pull up enable
00172     USB0->CONTROL |= USB_CONTROL_DPPULLUPNONOTG_MASK;
00173 }
00174 
00175 void USBHAL::disconnect(void) {
00176     // disable USB
00177     USB0->CTL &= ~USB_CTL_USBENSOFEN_MASK;
00178     // Pull up disable
00179     USB0->CONTROL &= ~USB_CONTROL_DPPULLUPNONOTG_MASK;
00180 
00181     //Free buffers if required:
00182     for (int i = 0; i<(NUMBER_OF_PHYSICAL_ENDPOINTS - 2) * 2; i++) {
00183         free(endpoint_buffer[i]);
00184         endpoint_buffer[i] = NULL;
00185     }
00186     free(endpoint_buffer_iso[2]);
00187     endpoint_buffer_iso[2] = NULL;
00188     free(endpoint_buffer_iso[0]);
00189     endpoint_buffer_iso[0] = NULL;
00190 }
00191 
00192 void USBHAL::configureDevice(void) {
00193     // not needed
00194 }
00195 
00196 void USBHAL::unconfigureDevice(void) {
00197     // not needed
00198 }
00199 
00200 void USBHAL::setAddress(uint8_t address) {
00201     // we don't set the address now otherwise the usb controller does not ack
00202     // we set a flag instead
00203     // see usbisr when an IN token is received
00204     set_addr = 1;
00205     addr = address;
00206 }
00207 
00208 bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t flags) {
00209     uint32_t handshake_flag = 0;
00210     uint8_t * buf;
00211 
00212     if (endpoint > NUMBER_OF_PHYSICAL_ENDPOINTS - 1) {
00213         return false;
00214     }
00215 
00216     uint32_t log_endpoint = PHY_TO_LOG(endpoint);
00217 
00218     if ((flags & ISOCHRONOUS) == 0) {
00219         handshake_flag = USB_ENDPT_EPHSHK_MASK;
00220         if (IN_EP(endpoint)) {
00221             if (endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)] == NULL)
00222                 endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)] = (uint8_t *) malloc (64);
00223             buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)][0];
00224         } else {
00225             if (endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)] == NULL)
00226                 endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)] = (uint8_t *) malloc (64);
00227             buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)][0];
00228         }
00229     } else {
00230         if (IN_EP(endpoint)) {
00231             if (endpoint_buffer_iso[2] == NULL)
00232                 endpoint_buffer_iso[2] = (uint8_t *) malloc (1023);
00233             buf = &endpoint_buffer_iso[2][0];
00234         } else {
00235             if (endpoint_buffer_iso[0] == NULL)
00236                 endpoint_buffer_iso[0] = (uint8_t *) malloc (1023);
00237             buf = &endpoint_buffer_iso[0][0];
00238         }
00239     }
00240 
00241     // IN endpt -> device to host (TX)
00242     if (IN_EP(endpoint)) {
00243         USB0->ENDPOINT[log_endpoint].ENDPT |= handshake_flag |        // ep handshaking (not if iso endpoint)
00244                                               USB_ENDPT_EPTXEN_MASK;  // en TX (IN) tran
00245         bdt[EP_BDT_IDX(log_endpoint, TX, ODD )].address = (uint32_t) buf;
00246         bdt[EP_BDT_IDX(log_endpoint, TX, EVEN)].address = 0;
00247     }
00248     // OUT endpt -> host to device (RX)
00249     else {
00250         USB0->ENDPOINT[log_endpoint].ENDPT |= handshake_flag |        // ep handshaking (not if iso endpoint)
00251                                               USB_ENDPT_EPRXEN_MASK;  // en RX (OUT) tran.
00252         bdt[EP_BDT_IDX(log_endpoint, RX, ODD )].byte_count = maxPacket;
00253         bdt[EP_BDT_IDX(log_endpoint, RX, ODD )].address    = (uint32_t) buf;
00254         bdt[EP_BDT_IDX(log_endpoint, RX, ODD )].info       = BD_OWN_MASK | BD_DTS_MASK;
00255         bdt[EP_BDT_IDX(log_endpoint, RX, EVEN)].info       = 0;
00256     }
00257 
00258     Data1 |= (1 << endpoint);
00259 
00260     return true;
00261 }
00262 
00263 // read setup packet
00264 void USBHAL::EP0setup(uint8_t *buffer) {
00265     uint32_t sz;
00266     endpointReadResult(EP0OUT, buffer, &sz);
00267 }
00268 
00269 void USBHAL::EP0readStage(void) {
00270     Data1 &= ~1UL;  // set DATA0
00271     bdt[0].info = (BD_DTS_MASK | BD_OWN_MASK);
00272 }
00273 
00274 void USBHAL::EP0read(void) {
00275     uint32_t idx = EP_BDT_IDX(PHY_TO_LOG(EP0OUT), RX, 0);
00276     bdt[idx].byte_count = MAX_PACKET_SIZE_EP0;
00277 }
00278 
00279 uint32_t USBHAL::EP0getReadResult(uint8_t *buffer) {
00280     uint32_t sz;
00281     endpointReadResult(EP0OUT, buffer, &sz);
00282     return sz;
00283 }
00284 
00285 void USBHAL::EP0write(uint8_t *buffer, uint32_t size) {
00286     endpointWrite(EP0IN, buffer, size);
00287 }
00288 
00289 void USBHAL::EP0getWriteResult(void) {
00290 }
00291 
00292 void USBHAL::EP0stall(void) {
00293     stallEndpoint(EP0OUT);
00294 }
00295 
00296 EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize) {
00297     endpoint = PHY_TO_LOG(endpoint);
00298     uint32_t idx = EP_BDT_IDX(endpoint, RX, 0);
00299     bdt[idx].byte_count = maximumSize;
00300     return EP_PENDING;
00301 }
00302 
00303 EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t * buffer, uint32_t *bytesRead) {
00304     uint32_t n, sz, idx, setup = 0;
00305     uint8_t not_iso;
00306     uint8_t * ep_buf;
00307 
00308     uint32_t log_endpoint = PHY_TO_LOG(endpoint);
00309 
00310     if (endpoint > NUMBER_OF_PHYSICAL_ENDPOINTS - 1) {
00311         return EP_INVALID;
00312     }
00313 
00314     // if read on a IN endpoint -> error
00315     if (IN_EP(endpoint)) {
00316         return EP_INVALID;
00317     }
00318 
00319     idx = EP_BDT_IDX(log_endpoint, RX, 0);
00320     sz  = bdt[idx].byte_count;
00321     not_iso = USB0->ENDPOINT[log_endpoint].ENDPT & USB_ENDPT_EPHSHK_MASK;
00322 
00323     //for isochronous endpoint, we don't wait an interrupt
00324     if ((log_endpoint != 0) && not_iso && !(epComplete & EP(endpoint))) {
00325         return EP_PENDING;
00326     }
00327 
00328     if ((log_endpoint == 0) && (TOK_PID(idx) == SETUP_TOKEN)) {
00329         setup = 1;
00330     }
00331 
00332     // non iso endpoint
00333     if (not_iso) {
00334         ep_buf = endpoint_buffer[idx];
00335     } else {
00336         ep_buf = endpoint_buffer_iso[0];
00337     }
00338 
00339     for (n = 0; n < sz; n++) {
00340         buffer[n] = ep_buf[n];
00341     }
00342 
00343     if (((Data1 >> endpoint) & 1) == ((bdt[idx].info >> 6) & 1)) {
00344         if (setup && (buffer[6] == 0))  // if no setup data stage,
00345             Data1 &= ~1UL;              // set DATA0
00346         else
00347             Data1 ^= (1 << endpoint);
00348     }
00349 
00350     if (((Data1 >> endpoint) & 1)) {
00351         bdt[idx].info = BD_DTS_MASK | BD_DATA01_MASK | BD_OWN_MASK;
00352     }
00353     else {
00354         bdt[idx].info = BD_DTS_MASK | BD_OWN_MASK;
00355     }
00356 
00357     USB0->CTL &= ~USB_CTL_TXSUSPENDTOKENBUSY_MASK;
00358     *bytesRead = sz;
00359 
00360     epComplete &= ~EP(endpoint);
00361     return EP_COMPLETED;
00362 }
00363 
00364 EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size) {
00365     uint32_t idx, n;
00366     uint8_t * ep_buf;
00367 
00368     if (endpoint > NUMBER_OF_PHYSICAL_ENDPOINTS - 1) {
00369         return EP_INVALID;
00370     }
00371 
00372     // if write on a OUT endpoint -> error
00373     if (OUT_EP(endpoint)) {
00374         return EP_INVALID;
00375     }
00376 
00377     idx = EP_BDT_IDX(PHY_TO_LOG(endpoint), TX, 0);
00378     bdt[idx].byte_count = size;
00379 
00380 
00381     // non iso endpoint
00382     if (USB0->ENDPOINT[PHY_TO_LOG(endpoint)].ENDPT & USB_ENDPT_EPHSHK_MASK) {
00383         ep_buf = endpoint_buffer[idx];
00384     } else {
00385         ep_buf = endpoint_buffer_iso[2];
00386     }
00387 
00388     for (n = 0; n < size; n++) {
00389         ep_buf[n] = data[n];
00390     }
00391 
00392     if ((Data1 >> endpoint) & 1) {
00393         bdt[idx].info = BD_OWN_MASK | BD_DTS_MASK;
00394     } else {
00395         bdt[idx].info = BD_OWN_MASK | BD_DTS_MASK | BD_DATA01_MASK;
00396     }
00397 
00398     Data1 ^= (1 << endpoint);
00399 
00400     return EP_PENDING;
00401 }
00402 
00403 EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint) {
00404     if (epComplete & EP(endpoint)) {
00405         epComplete &= ~EP(endpoint);
00406         return EP_COMPLETED;
00407     }
00408 
00409     return EP_PENDING;
00410 }
00411 
00412 void USBHAL::stallEndpoint(uint8_t endpoint) {
00413     USB0->ENDPOINT[PHY_TO_LOG(endpoint)].ENDPT |= USB_ENDPT_EPSTALL_MASK;
00414 }
00415 
00416 void USBHAL::unstallEndpoint(uint8_t endpoint) {
00417     USB0->ENDPOINT[PHY_TO_LOG(endpoint)].ENDPT &= ~USB_ENDPT_EPSTALL_MASK;
00418 }
00419 
00420 bool USBHAL::getEndpointStallState(uint8_t endpoint) {
00421     uint8_t stall = (USB0->ENDPOINT[PHY_TO_LOG(endpoint)].ENDPT & USB_ENDPT_EPSTALL_MASK);
00422     return (stall) ? true : false;
00423 }
00424 
00425 void USBHAL::remoteWakeup(void) {
00426     // [TODO]
00427 }
00428 
00429 
00430 void USBHAL::_usbisr(void) {
00431     instance->usbisr();
00432 }
00433 
00434 
00435 void USBHAL::usbisr(void) {
00436     uint8_t i;
00437     uint8_t istat = USB0->ISTAT;
00438 
00439     // reset interrupt
00440     if (istat & USB_ISTAT_USBRST_MASK) {
00441         // disable all endpt
00442         for(i = 0; i < 16; i++) {
00443             USB0->ENDPOINT[i].ENDPT = 0x00;
00444         }
00445 
00446         // enable control endpoint
00447         realiseEndpoint(EP0OUT, MAX_PACKET_SIZE_EP0, 0);
00448         realiseEndpoint(EP0IN, MAX_PACKET_SIZE_EP0, 0);
00449 
00450         Data1 = 0x55555555;
00451         USB0->CTL |=  USB_CTL_ODDRST_MASK;
00452 
00453         USB0->ISTAT   =  0xFF;  // clear all interrupt status flags
00454         USB0->ERRSTAT =  0xFF;  // clear all error flags
00455         USB0->ERREN   =  0xFF;  // enable error interrupt sources
00456         USB0->ADDR    =  0x00;  // set default address
00457 
00458         return;
00459     }
00460 
00461     // resume interrupt
00462     if (istat & USB_ISTAT_RESUME_MASK) {
00463         USB0->ISTAT = USB_ISTAT_RESUME_MASK;
00464     }
00465 
00466     // SOF interrupt
00467     if (istat & USB_ISTAT_SOFTOK_MASK) {
00468         USB0->ISTAT = USB_ISTAT_SOFTOK_MASK;
00469         // SOF event, read frame number
00470         SOF(frameNumber());
00471     }
00472 
00473     // stall interrupt
00474     if (istat & 1<<7) {
00475         if (USB0->ENDPOINT[0].ENDPT & USB_ENDPT_EPSTALL_MASK)
00476             USB0->ENDPOINT[0].ENDPT &= ~USB_ENDPT_EPSTALL_MASK;
00477         USB0->ISTAT |= USB_ISTAT_STALL_MASK;
00478     }
00479 
00480     // token interrupt
00481     if (istat & 1<<3) {
00482         uint32_t num  = (USB0->STAT >> 4) & 0x0F;
00483         uint32_t dir  = (USB0->STAT >> 3) & 0x01;
00484         uint32_t ev_odd = (USB0->STAT >> 2) & 0x01;
00485         int endpoint = (num << 1) | dir;
00486 
00487         // setup packet
00488         if ((num == 0) && (TOK_PID((EP_BDT_IDX(num, dir, ev_odd))) == SETUP_TOKEN)) {
00489             Data1 &= ~0x02;
00490             bdt[EP_BDT_IDX(0, TX, EVEN)].info &= ~BD_OWN_MASK;
00491             bdt[EP_BDT_IDX(0, TX, ODD)].info  &= ~BD_OWN_MASK;
00492 
00493             // EP0 SETUP event (SETUP data received)
00494             EP0setupCallback();
00495 
00496         } else {
00497             // OUT packet
00498             if (TOK_PID((EP_BDT_IDX(num, dir, ev_odd))) == OUT_TOKEN) {
00499                 if (num == 0)
00500                     EP0out();
00501                 else {
00502                     epComplete |= EP(endpoint);
00503                     if ((instance->*(epCallback[endpoint - 2]))()) {
00504                         epComplete &= ~EP(endpoint);
00505                     }
00506                 }
00507             }
00508 
00509             // IN packet
00510             if (TOK_PID((EP_BDT_IDX(num, dir, ev_odd))) == IN_TOKEN) {
00511                 if (num == 0) {
00512                     EP0in();
00513                     if (set_addr == 1) {
00514                         USB0->ADDR = addr & 0x7F;
00515                         set_addr = 0;
00516                     }
00517                 }
00518                 else {
00519                     epComplete |= EP(endpoint);
00520                     if ((instance->*(epCallback[endpoint - 2]))()) {
00521                         epComplete &= ~EP(endpoint);
00522                     }
00523                 }
00524             }
00525         }
00526 
00527         USB0->ISTAT = USB_ISTAT_TOKDNE_MASK;
00528     }
00529 
00530     // sleep interrupt
00531     if (istat & 1<<4) {
00532         USB0->ISTAT |= USB_ISTAT_SLEEP_MASK;
00533     }
00534 
00535     // error interrupt
00536     if (istat & USB_ISTAT_ERROR_MASK) {
00537         USB0->ERRSTAT = 0xFF;
00538         USB0->ISTAT |= USB_ISTAT_ERROR_MASK;
00539     }
00540 }
00541 
00542 
00543 #endif