/***************************************************************************//** * @brief Handles USB port reset interrupt * @details After receiving a USB reset, halt all endpoints except for * Endpoint 0, set the device state, and configure USB hardware. ******************************************************************************/ static void handleUsbResetInt(void) { // Setup EP0 to receive SETUP packets myUsbDevice.ep0.state = D_EP_IDLE; // Halt all other endpoints #if SLAB_USB_EP1IN_USED myUsbDevice.ep1in.state = D_EP_HALT; #endif #if SLAB_USB_EP2IN_USED myUsbDevice.ep2in.state = D_EP_HALT; #endif #if SLAB_USB_EP3IN_USED myUsbDevice.ep3in.state = D_EP_HALT; #endif #if SLAB_USB_EP1OUT_USED myUsbDevice.ep1out.state = D_EP_HALT; #endif #if SLAB_USB_EP2OUT_USED myUsbDevice.ep2out.state = D_EP_HALT; #endif #if SLAB_USB_EP3OUT_USED myUsbDevice.ep3out.state = D_EP_HALT; #endif // After a USB reset, some USB hardware configurations will be reset and must // be reconfigured. // Re-enable clock recovery #if SLAB_USB_CLOCK_RECOVERY_ENABLED #if SLAB_USB_FULL_SPEED USB_EnableFullSpeedClockRecovery(); #else USB_EnableLowSpeedClockRecovery(); #endif #endif // Re-enable USB interrupts USB_EnableSuspendDetection(); USB_EnableDeviceInts(); // If VBUS is preset, put the device in the Default state. // Otherwise, put it in the Attached state. //#if (!(SLAB_USB_PWRSAVE_MODE & USB_PWRSAVE_MODE_ONVBUSOFF)) // If VBUS is preset, put the device in the Default state. // Otherwise, put it in the Attached state. // If the device is bus-powered, always put it in the Default state #if (!(SLAB_USB_PWRSAVE_MODE & USB_PWRSAVE_MODE_ONVBUSOFF) && !SLAB_USB_BUS_POWERED) if (USB_IsVbusOn()) { USBD_SetUsbState(USBD_STATE_DEFAULT); } else { USBD_SetUsbState(USBD_STATE_ATTACHED); } #else USBD_SetUsbState(USBD_STATE_DEFAULT); #endif // (!(SLAB_USB_PWRSAVE_MODE & USB_PWRSAVE_MODE_ONVBUSOFF)) #if SLAB_USB_RESET_CB // Make the USB Reset Callback USBD_ResetCb(); #endif }
int8_t USBD_Init(const USBD_Init_TypeDef *p) { uint8_t i; USB_SaveSfrPage(); USB_DisableInts(); // This forces the liner to bring in the contents efm8_usbdint // It is place here since all users MUST call this function // for the library to work properly forceModuleLoad_usbint(); // Zero out the myUsbDevice struct, then initialize all non-zero members for (i = 0; i < sizeof(myUsbDevice); i++) { *((uint8_t MEM_MODEL_SEG *)&myUsbDevice + i) = 0; } // Get the USB descriptors from p myUsbDevice.deviceDescriptor = p->deviceDescriptor; myUsbDevice.configDescriptor = (USB_ConfigurationDescriptor_TypeDef *)p->configDescriptor; myUsbDevice.stringDescriptors = p->stringDescriptors; myUsbDevice.numberOfStrings = p->numberOfStrings; // Enable USB clock #if SLAB_USB_FULL_SPEED USB_SetClockIntOsc(); USB_SelectFullSpeed(); #else USB_SetClockIntOscDiv8(); USB_SelectLowSpeed(); #endif // SLAB_USB_FULL_SPEED // Enable or disable VBUS detection #if SLAB_USB_BUS_POWERED USB_VbusDetectDisable(); #else USB_VbusDetectEnable(); #endif USB_ForceReset(); USB_EnableDeviceInts(); USBD_Connect(); // If VBUS is present, the state should be Default. // Otherwise, it is Attached. #if SLAB_USB_BUS_POWERED myUsbDevice.state = USBD_STATE_DEFAULT; #else if (USB_IsVbusOn()) { myUsbDevice.state = USBD_STATE_DEFAULT; } else { myUsbDevice.state = USBD_STATE_ATTACHED; } #endif // Only enable USB interrupts when not in polled mode #if (SLAB_USB_POLLED_MODE == 0) USB_EnableInts(); #endif USB_RestoreSfrPage(); USB_DisableInhibit(); return USB_STATUS_OK; }