/** * @brief This function initializes the USB process. * * Depending on the mode supported (HOST/DEVICE/DUAL_ROLE) the function * calls the coresponding usb mode initialization function * * @param none * * @return none */ void usb_task_init(void) { #if (USB_HOST_FEATURE == ENABLED && USB_DEVICE_FEATURE == ENABLED) U8 delay; #endif #if (USE_USB_PADS_REGULATOR==ENABLE) // Otherwise assume USB PADs regulator is not used Usb_enable_regulator(); #endif // ---- DUAL ROLE DEVICE USB MODE --------------------------------------------- #if ((USB_DEVICE_FEATURE == ENABLED)&& (USB_HOST_FEATURE == ENABLED)) Usb_enable_uid_pin(); delay=PORTA; g_usb_mode=USB_MODE_UNDEFINED; if(Is_usb_id_device()) { g_usb_mode=USB_MODE_DEVICE; usb_device_task_init(); } else { g_usb_mode=USB_MODE_HOST; Usb_ack_id_transition(); // REQUIRED !!! Startup with ID=0, Ack ID pin transistion (default hwd start up is device mode) #if ( ID_PIN_CHANGE_GENERATE_RESET == ENABLE) Usb_enable_id_interrupt(); #endif Enable_interrupt(); usb_host_task_init(); } g_old_usb_mode=g_usb_mode; // Store current usb mode, for mode change detection // ----------------------------------------------------------------------------- // ---- DEVICE ONLY USB MODE --------------------------------------------------- #elif ((USB_DEVICE_FEATURE == ENABLED)&& (USB_HOST_FEATURE == DISABLE)) Usb_force_device_mode(); usb_device_task_init(); // ----------------------------------------------------------------------------- // ---- REDUCED HOST ONLY USB MODE --------------------------------------------- #elif ((USB_DEVICE_FEATURE == DISABLE)&& (USB_HOST_FEATURE == ENABLED)) Usb_force_host_mode(); usb_host_task_init(); #elif ((USB_DEVICE_FEATURE == DISABLE)&& (USB_HOST_FEATURE == DISABLE)) #error at least one of USB_DEVICE_FEATURE or USB_HOST_FEATURE should be enabled #endif // ----------------------------------------------------------------------------- }
void usb_task(void) { // ---- DUAL-ROLE DEVICE/HOST USB MODE ----------------------------------------- #if USB_DEVICE_FEATURE == true && USB_HOST_FEATURE == true if (g_old_usb_mode != g_usb_mode) { if (Is_usb_id_device()) { usb_device_task_init(); }else{ private_sof_counter = 0; usb_host_task_init(); } g_old_usb_mode = g_usb_mode; // Store current USB mode, for mode change detection Usb_enable_id_interrupt(); Enable_global_interrupt(); } // Depending on current USB mode, launch the correct USB task (device or host) switch (g_old_usb_mode) { case USB_MODE_DEVICE: usb_device_task(); break; case USB_MODE_HOST: usb_host_task(); break; case USB_MODE_UNDEFINED: default: break; } // ----------------------------------------------------------------------------- // ---- DEVICE-ONLY USB MODE --------------------------------------------------- #elif USB_DEVICE_FEATURE == true usb_device_task(); // ----------------------------------------------------------------------------- // ---- REDUCED-HOST-ONLY USB MODE --------------------------------------------- #elif USB_HOST_FEATURE == true usb_host_task(); // ----------------------------------------------------------------------------- // ---- ERROR, NO MODE true ------------------------------------------------- #else #error At least one of USB_DEVICE_FEATURE and USB_HOST_FEATURE must be enabled #endif // ----------------------------------------------------------------------------- }
void usb_task(void *pvParameters) { #endif // FREERTOS_USED // Register the USB interrupt handler to the interrupt controller and enable // the USB interrupt. Disable_global_interrupt(); INTC_register_interrupt((__int_handler)&usb_general_interrupt, AVR32_USBB_IRQ, USB_INT_LEV); Enable_global_interrupt(); #ifdef FREERTOS_USED while (true) { // Wait for the semaphore while (!xSemaphoreTake(usb_tsk_semphr, portMAX_DELAY)); #endif // FREERTOS_USED // ---- DUAL-ROLE DEVICE/HOST USB MODE ----------------------------------------- #if USB_DEVICE_FEATURE == true && USB_HOST_FEATURE == true #ifdef FREERTOS_USED if (usb_device_tsk) vTaskDelete(usb_device_tsk), usb_device_tsk = NULL; if (usb_host_tsk) vTaskDelete(usb_host_tsk), usb_host_tsk = NULL; #endif Usb_input_id_pin(); Usb_enable_id_pin(); if (Is_usb_id_device()) { g_usb_mode = USB_MODE_DEVICE; usb_device_task_init(); } else { private_sof_counter = 0; g_usb_mode = USB_MODE_HOST; usb_host_task_init(); } g_old_usb_mode = g_usb_mode; // Store current USB mode, for mode change detection Usb_raise_id_transition(); // Check no ID transition has been missed during initialization Usb_enable_id_interrupt(); Enable_global_interrupt(); // ----------------------------------------------------------------------------- // ---- DEVICE-ONLY USB MODE --------------------------------------------------- #elif USB_DEVICE_FEATURE == true #ifdef FREERTOS_USED if (usb_device_tsk) vTaskDelete(usb_device_tsk), usb_device_tsk = NULL; #endif Usb_force_device_mode(); usb_device_task_init(); // ----------------------------------------------------------------------------- // ---- REDUCED-HOST-ONLY USB MODE --------------------------------------------- #elif USB_HOST_FEATURE == true #ifdef FREERTOS_USED if (usb_host_tsk) vTaskDelete(usb_host_tsk), usb_host_tsk = NULL; #endif private_sof_counter = 0; Usb_force_host_mode(); usb_host_task_init(); // ----------------------------------------------------------------------------- // ---- ERROR, NO MODE true ------------------------------------------------- #else #error At least one of USB_DEVICE_FEATURE and USB_HOST_FEATURE must be enabled #endif // ----------------------------------------------------------------------------- #ifdef FREERTOS_USED } #endif }
/** * @brief Entry point of the USB mamnagement * * Depending on the USB mode supported (HOST/DEVICE/DUAL_ROLE) the function * calls the coresponding usb management function. * * @param none * * @return none */ void usb_task(void) { // ---- DUAL ROLE DEVICE USB MODE --------------------------------------------- #if ((USB_DEVICE_FEATURE == ENABLED)&& (USB_HOST_FEATURE == ENABLED)) if(Is_usb_id_device()) { g_usb_mode=USB_MODE_DEVICE;} else { g_usb_mode=USB_MODE_HOST;} if( g_old_usb_mode != g_usb_mode ) { // ID pin hot state change #if ( ID_PIN_CHANGE_GENERATE_RESET == ENABLE) // Hot ID transition generates wdt reset wdtdrv_enable(WDTO_16MS); while(1); #else // Hot ID transition reset USB mode Usb_ack_id_transition(); // REQUIRED if (Is_usb_id_host()) { Usb_disable_resume_interrupt(); Usb_disable_wake_up_interrupt(); Usb_disable_suspend_interrupt(); Usb_disable_reset_interrupt(); Usb_detach(); Usb_disable(); usb_host_task_init(); } else { Host_disable_device_disconnection_interrupt(); Host_disable_sof_interrupt(); Host_disable_sof(); Usb_disable_vbus(); Usb_disable_manual_vbus(); Usb_freeze_clock(); Usb_disable(); usb_device_task_init(); } #endif } // Store current usb mode, for mode change detection g_old_usb_mode=g_usb_mode; // Depending on current usb mode, launch the correct usb task (device or host) switch(g_usb_mode) { case USB_MODE_DEVICE: usb_device_task(); break; case USB_MODE_HOST: usb_host_task(); break; case USB_MODE_UNDEFINED: // No break ! default: break; } // ----------------------------------------------------------------------------- // ---- DEVICE ONLY USB MODE --------------------------------------------------- #elif ((USB_DEVICE_FEATURE == ENABLED)&& (USB_HOST_FEATURE == DISABLE)) usb_device_task(); // ----------------------------------------------------------------------------- // ---- REDUCED HOST ONLY USB MODE --------------------------------------------- #elif ((USB_DEVICE_FEATURE == DISABLE)&& (USB_HOST_FEATURE == ENABLED)) usb_host_task(); // ----------------------------------------------------------------------------- //! ---- ERROR, NO MODE ENABLED ------------------------------------------------- #elif ((USB_DEVICE_FEATURE == DISABLE)&& (USB_HOST_FEATURE == DISABLE)) #error at least one of USB_DEVICE_FEATURE or USB_HOST_FEATURE should be enabled #error otherwise the usb task has nothing to do ... #endif // ----------------------------------------------------------------------------- }