//! //! @brief This function initializes the USB device controller //! //! This function enables the USB controller and init the USB interrupts. //! The aim is to allow the USB connection detection in order to send //! the appropriate USB event to the operating mode manager. //! Start device function is executed once VBUS connection has been detected //! either by the VBUS change interrupt either by the VBUS high level //! //! @param none //! //! @return none //! void usb_start_device (void) { Pll_start_auto(); Wait_pll_ready(); Usb_unfreeze_clock(); usb_init_device(); // configure the USB controller EP0 Usb_attach(); Usb_enable_suspend_interrupt(); Usb_enable_reset_interrupt(); #if (USB_OTG_FEATURE == ENABLED) Usb_enable_id_interrupt(); #endif }
/** * @brief This function initializes the USB controller in host mode and * the associated variables. * * This function enables the USB controller for host mode operation. * * @param none * * @return none * */ void usb_host_task_init(void) { Pll_start_auto(); Wait_pll_ready(); Usb_disable(); Usb_enable(); Usb_unfreeze_clock(); Usb_attach(); Usb_enable_uvcon_pin(); Usb_select_host(); Usb_disable_vbus_hw_control(); // Force Vbus generation without timeout Host_enable_device_disconnection_interrupt(); device_state=DEVICE_UNATTACHED; init_usb_tree(); }
__interrupt void usb_general_interrupt() #endif { #if (USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE) U8 i; U8 save_pipe_nb; #endif // ---------- DEVICE events management ----------------------------------- #if (USB_DEVICE_FEATURE == ENABLED) // - Device start of frame received if (Is_usb_sof() && Is_sof_interrupt_enabled()) { Usb_ack_sof(); Usb_sof_action(); } #ifdef WA_USB_SUSPEND_PERTUBATION // - Device Suspend event (no more USB activity detected) if (Is_usb_suspend() && Is_suspend_interrupt_enabled()) { usb_suspended=TRUE; Usb_ack_wake_up(); // clear wake up to detect next event Usb_send_event(EVT_USB_SUSPEND); Usb_ack_suspend(); Usb_enable_wake_up_interrupt(); Usb_disable_resume_interrupt(); Usb_freeze_clock(); Stop_pll(); Usb_suspend_action(); } // - Wake up event (USB activity detected): Used to resume if (Is_usb_wake_up() && Is_wake_up_interrupt_enabled()) { if(Is_pll_ready()==FALSE) { Pll_start_auto(); Wait_pll_ready(); } Usb_unfreeze_clock(); Usb_ack_wake_up(); if(usb_suspended) { Usb_enable_resume_interrupt(); Usb_enable_reset_interrupt(); while(Is_usb_wake_up()) { Usb_ack_wake_up(); } usb_delay_ms(2); if(Is_usb_sof() || Is_usb_resume() || Is_usb_reset() ) { Usb_disable_wake_up_interrupt(); Usb_wake_up_action(); Usb_send_event(EVT_USB_WAKE_UP); Usb_enable_suspend_interrupt(); Usb_enable_resume_interrupt(); Usb_enable_reset_interrupt(); } else // Workarround to make the USB enter power down mode again (spurious transcient detected on the USB lines) { Usb_ack_wake_up(); // clear wake up to detect next event Usb_send_event(EVT_USB_SUSPEND); Usb_enable_wake_up_interrupt(); Usb_disable_resume_interrupt(); Usb_freeze_clock(); Stop_pll(); Usb_suspend_action(); } } } // - Resume state bus detection if (Is_usb_resume() && Is_resume_interrupt_enabled()) { usb_suspended = FALSE; Usb_disable_wake_up_interrupt(); Usb_ack_resume(); Usb_disable_resume_interrupt(); Usb_resume_action(); Usb_send_event(EVT_USB_RESUME); } #else // - Device Suspend event (no more USB activity detected) if (Is_usb_suspend() && Is_suspend_interrupt_enabled()) { // Remote wake-up handler if ((remote_wakeup_feature == ENABLED) && (usb_configuration_nb != 0)) { Usb_disable_suspend_interrupt(); Usb_ack_wake_up(); Usb_enable_wake_up_interrupt(); Stop_pll(); Usb_freeze_clock(); Usb_suspend_action(); // After that user can execute "Usb_initiate_remote_wake_up()" to initiate a remote wake-up // Note that the suspend interrupt flag SUSPI must still be set to enable upstream resume // So the SUSPE enable bit must be cleared to avoid redundant interrupt // **************** // Please note also that is Vbus is lost during an upstream resume (Host disconnection), // the RMWKUP bit (used to initiate remote wake up and that is normally cleared by hardware when sent) // remains set after the event, so that a good way to handle this feature is : // Usb_unfreeze_clock(); // Usb_initiate_remote_wake_up(); // while (Is_usb_pending_remote_wake_up()) // { // if (Is_usb_vbus_low()) // { // // Emergency action (reset macro, etc.) if Vbus lost during resuming // break; // } // } // Usb_ack_remote_wake_up_start(); // **************** } else { // No remote wake-up supported Usb_ack_wake_up(); // clear wake up to detect next event Usb_send_event(EVT_USB_SUSPEND); Usb_ack_suspend(); // must be executed last (after Usb_suspend_action()) to allow upstream resume Usb_enable_wake_up_interrupt(); Usb_freeze_clock(); Stop_pll(); Usb_suspend_action(); } } // - Wake up event (USB activity detected): Used to resume if (Is_usb_wake_up() && Is_wake_up_interrupt_enabled()) { if(Is_pll_ready()==FALSE) { Pll_start_auto(); Wait_pll_ready(); } Usb_unfreeze_clock(); Usb_ack_wake_up(); Usb_disable_wake_up_interrupt(); Usb_wake_up_action(); Usb_send_event(EVT_USB_WAKE_UP); Usb_enable_suspend_interrupt(); } // - Resume state bus detection if (Is_usb_resume() && Is_resume_interrupt_enabled()) { Usb_disable_wake_up_interrupt(); Usb_ack_resume(); Usb_disable_resume_interrupt(); Usb_resume_action(); Usb_send_event(EVT_USB_RESUME); } #endif // - USB bus reset detection if (Is_usb_reset()&& Is_reset_interrupt_enabled()) { Usb_ack_reset(); usb_init_device(); Usb_reset_action(); Usb_send_event(EVT_USB_RESET); } #endif// End DEVICE FEATURE MODE // ---------- HOST events management ----------------------------------- #if (USB_HOST_FEATURE == ENABLED && USB_DEVICE_FEATURE == ENABLED) // - ID pin change detection if(Is_usb_id_transition()&&Is_usb_id_interrupt_enabled()) { if(Is_usb_id_device()) { g_usb_mode=USB_MODE_DEVICE;} else { g_usb_mode=USB_MODE_HOST;} Usb_ack_id_transition(); if( g_usb_mode != g_old_usb_mode) // Basic Debounce { if(Is_usb_id_device()) // Going to device mode { Usb_send_event(EVT_USB_DEVICE_FUNCTION); } else // Going to host mode { Usb_send_event(EVT_USB_HOST_FUNCTION); } Usb_id_transition_action(); LOG_STR_CODE(log_id_change); #if ( ID_PIN_CHANGE_GENERATE_RESET == ENABLE) // Hot ID transition generates wdt reset wdtdrv_enable(WDTO_16MS); while(1); #endif } } #endif #if (USB_HOST_FEATURE == ENABLED) // - The device has been disconnected if(Is_device_disconnection() && Is_host_device_disconnection_interrupt_enabled()) { host_disable_all_pipe(); Host_ack_device_disconnection(); device_state=DEVICE_DISCONNECTED; Usb_send_event(EVT_HOST_DISCONNECTION); init_usb_tree(); LOG_STR_CODE(log_device_disconnect); Host_device_disconnection_action(); } // - Device connection if(Is_device_connection() && Is_host_device_connection_interrupt_enabled()) { Host_ack_device_connection(); host_disable_all_pipe(); Host_device_connection_action(); } // - Host Start of frame has been sent if (Is_host_sof() && Is_host_sof_interrupt_enabled()) { Host_ack_sof(); Usb_send_event(EVT_HOST_SOF); private_sof_counter++; #if (USB_HUB_SUPPORT==ENABLE) hub_interrupt_sof++; #endif // delay timeout management for interrupt tranfer mode in host mode #if ((USB_HOST_PIPE_INTERRUPT_TRANSFER==ENABLE) && (TIMEOUT_DELAY_ENABLE==ENABLE)) if (private_sof_counter>=250) // Count 1/4 sec { private_sof_counter=0; for(i=0;i<MAX_EP_NB;i++) { if(it_pipe_str[i].enable==ENABLE) { save_pipe_nb=Host_get_selected_pipe(); Host_select_pipe(i); if((++it_pipe_str[i].timeout>TIMEOUT_DELAY) && (Host_get_pipe_type()!=TYPE_INTERRUPT)) { it_pipe_str[i].enable=DISABLE; it_pipe_str[i].status=PIPE_DELAY_TIMEOUT; Host_stop_pipe_interrupt(i); if (is_any_interrupt_pipe_active()==FALSE) // If no more transfer is armed { if (g_sav_int_sof_enable==FALSE) { Host_disable_sof_interrupt(); } } it_pipe_str[i].handle(PIPE_DELAY_TIMEOUT,it_pipe_str[i].nb_byte_processed); } Host_select_pipe(save_pipe_nb); } } } #endif // (USB_HOST_PIPE_INTERRUPT_TRANSFER==ENABLE) && (TIMEOUT_DELAY_ENABLE==ENABLE)) Host_sof_action(); } // - Host Wake-up has been received if (Is_host_hwup() && Is_host_hwup_interrupt_enabled()) { Host_disable_hwup_interrupt(); // Wake up interrupt should be disable host is now wake up ! // CAUTION HWUP can be cleared only when USB clock is active (not frozen)! Pll_start_auto(); // First Restart the PLL for USB operation Wait_pll_ready(); // Get sure pll is lock Usb_unfreeze_clock(); // Enable clock on USB interface Host_ack_hwup(); // Clear HWUP interrupt flag Usb_send_event(EVT_HOST_HWUP); // Send software event Host_hwup_action(); // Map custom action } #endif // End HOST FEATURE MODE }
//! @brief Entry point of the USB device mamagement //! //! This function is the entry point of the USB management. Each USB //! event is checked here in order to launch the appropriate action. //! If a Setup request occurs on the Default Control Endpoint, //! the usb_process_request() function is call in the usb_standard_request.c file //! //! @param none //! //! @return none void usb_device_task(void) { #if (USB_OTG_FEATURE == ENABLED) // Check if a reset has been received if(Is_usb_event(EVT_USB_RESET)) { Usb_ack_event(EVT_USB_RESET); Usb_reset_endpoint(0); usb_configuration_nb=0; otg_b_device_state = B_IDLE; Clear_otg_features_from_host(); } // When OTG mode enabled, B-Device is managed thanks to its state machine switch (otg_b_device_state) { //------------------------------------------------------ // B_IDLE state // // - waits for Vbus to rise // - initiate SRP if asked by user // case B_IDLE: if (Is_usb_vbus_high()) { // Vbus rise usb_connected = TRUE; remote_wakeup_feature = DISABLED; usb_start_device(); Usb_vbus_on_action(); Usb_attach(); otg_b_device_state = B_PERIPHERAL; Ack_user_request_srp(); Clear_otg_features_from_host(); remote_wakeup_feature = DISABLED; End_session_with_srp(); if (Is_srp_sent_and_waiting_answer() && (sof_seen_in_session == TRUE)) { Ack_srp_sent_and_answer(); Otg_print_new_failure_message(OTGMSG_A_RESPONDED,OTG_TEMPO_2SEC); } Usb_enable_sof_interrupt(); } else { if (Is_user_requested_srp() && Is_usb_id_device()) { // User has requested a SRP Ack_user_request_srp(); if (!Is_srp_sent_and_waiting_answer()) { Pll_start_auto(); // reinit device mode Wait_pll_ready(); Usb_disable(); Usb_enable_uid_pin(); Usb_enable(); Usb_unfreeze_clock(); Usb_select_device(); Usb_attach(); otg_b_device_state = B_SRP_INIT; Usb_device_initiate_srp(); // hardware waits for initial condition (SE0, Session End level) sof_seen_in_session = FALSE; } } if ((Is_srp_sent_and_waiting_answer()) && (Is_tb_srp_counter_overflow())) { // SRP failed because A-Device did not respond End_session_with_srp(); Ack_srp_sent_and_answer(); Otg_print_new_failure_message(OTGMSG_SRP_A_NO_RESP,OTG_TEMPO_3SEC); } } break; //------------------------------------------------------ // B_SRP_INIT // // - a SRP has been initiated // - B-Device waits it is finished to initialize variables // case B_SRP_INIT: if (!Is_usb_device_initiating_srp()) { otg_b_device_state = B_IDLE; // SRP initiated, return to Idle state (wait for Vbus to rise) Srp_sent_and_waiting_answer(); Init_tb_srp_counter(); Start_session_with_srp(); Otg_print_new_event_message(OTGMSG_SRP_STARTED,TB_SRP_FAIL_MIN); } break; //------------------------------------------------------ // B_PERIPHERAL : the main state of OTG Peripheral // // - all events are interrupt-handled // - but they are saved and this function can execute alternate actions // - also handle user requests (disconnect) // // ====================================================================================== case B_PERIPHERAL: if (Is_otg_event(EVT_OTG_DEVICE_CONNECTED)) { Otg_ack_event(EVT_OTG_DEVICE_CONNECTED); // set on a SetConfiguration descriptor reception Otg_print_new_event_message(OTGMSG_CONNECTED_TO_A,OTG_TEMPO_4SEC); } if (Is_usb_event(EVT_USB_SUSPEND)) // SUSPEND state { // Suspend and HNP operations are handled in the interrupt functions } if (Is_srp_sent_and_waiting_answer() && (sof_seen_in_session == TRUE)) { Ack_srp_sent_and_answer(); Otg_print_new_failure_message(OTGMSG_A_RESPONDED,OTG_TEMPO_2SEC); } if ((Is_srp_sent_and_waiting_answer()) && (Is_tb_srp_counter_overflow())) { // SRP failed because A-Device did not respond End_session_with_srp(); Ack_srp_sent_and_answer(); Otg_print_new_failure_message(OTGMSG_SRP_A_NO_RESP,OTG_TEMPO_3SEC); } if (Is_usb_event(EVT_USB_RESUME) && !Is_usb_pending_remote_wake_up()) // RESUME signal detected { Usb_ack_event(EVT_USB_RESUME); Usb_ack_event(EVT_USB_SUSPEND); Usb_ack_remote_wake_up_start(); } if (Is_usb_event(EVT_USB_UNPOWERED)) { Usb_ack_event(EVT_USB_UNPOWERED); Clear_all_user_request(); otg_b_device_state = B_IDLE; } if(Is_usb_event(EVT_USB_RESET)) { Usb_ack_event(EVT_USB_RESET); Usb_reset_endpoint(0); usb_configuration_nb=0; Clear_otg_features_from_host(); } if (Is_otg_event(EVT_OTG_HNP_ERROR)) { Otg_ack_event(EVT_OTG_HNP_ERROR); Otg_print_new_failure_message(OTGMSG_DEVICE_NO_RESP,OTG_TEMPO_4SEC); PORTC &= ~0x10; } if (Is_user_requested_disc()) { Ack_user_request_disc(); if (Is_usb_id_device()) { Usb_detach(); Usb_freeze_clock(); while (Is_usb_vbus_high()); // wait for Vbus to be under Va_vbus_valid otg_b_device_state = B_IDLE; usb_configuration_nb = 0; usb_connected = FALSE; Clear_all_user_request(); } } break; //------------------------------------------------------ // B_HOST // // - state entered after an HNP success // - handle user requests (disconnection, suspend, hnp) // - call the "host_task()" for Host level handlers // // ====================================================================================== case B_HOST: if (Is_otg_event(EVT_OTG_DEV_UNSUPPORTED)) { Otg_ack_event(EVT_OTG_DEV_UNSUPPORTED); Clear_all_user_request(); otg_b_device_state = B_IDLE; device_state = DEVICE_UNATTACHED; } if (Is_user_requested_disc() || Is_user_requested_suspend() || Is_user_requested_hnp()) { Ack_user_request_disc(); // suspend and hnp requests cleared in B_END_HNP_SUSPEND stage Host_disable_sof(); // go into suspend mode Usb_host_reject_hnp(); otg_b_device_state = B_END_HNP_SUSPEND; Usb_ack_suspend(); Usb_enable_suspend_interrupt(); } if (Is_usb_event(EVT_USB_UNPOWERED)) { Usb_ack_event(EVT_USB_UNPOWERED); Usb_freeze_clock(); otg_b_device_state = B_IDLE; device_state = DEVICE_UNATTACHED; } usb_host_task(); // call the host task break; //------------------------------------------------------ // B_END_HNP_SUSPEND // // - device enters this state after being B_HOST, on a user request to stop bus activity (suspend, disconnect or hnp request) // - macro is reset to peripheral mode // // ====================================================================================== case B_END_HNP_SUSPEND: if (Is_usb_event(EVT_USB_SUSPEND)) { Usb_ack_event(EVT_USB_SUSPEND); Usb_device_stop_hnp(); Usb_select_device(); device_state = DEVICE_UNATTACHED; if (Is_user_requested_hnp() || Is_user_requested_suspend()) { otg_b_device_state = B_PERIPHERAL; Ack_user_request_suspend(); Ack_user_request_hnp(); } else { otg_b_device_state = B_IDLE; Usb_detach(); Usb_freeze_clock(); } } break; default: otg_b_device_state = B_IDLE; Clear_all_user_request(); device_state = DEVICE_UNATTACHED; break; } #else // Non-OTG exclusives Device operations // VBUS state detection if (Is_usb_vbus_high()&& (usb_connected==FALSE)) { usb_connected = TRUE; remote_wakeup_feature = DISABLED; Usb_vbus_on_action(); Usb_send_event(EVT_USB_POWERED); usb_start_device(); } if (Is_usb_vbus_low()&& (usb_connected==TRUE)) { usb_connected = FALSE; usb_configuration_nb = 0; Usb_send_event(EVT_USB_UNPOWERED); Usb_detach(); Usb_freeze_clock(); Usb_vbus_off_action(); } if(Is_usb_event(EVT_USB_RESET)) { Usb_ack_event(EVT_USB_RESET); Usb_reset_endpoint(0); usb_configuration_nb=0; } #endif // ======================================= // Common Standard Device Control Requests // ======================================= // - device enumeration process // - device control commands and features Usb_select_endpoint(EP_CONTROL); if (Is_usb_receive_setup()) { usb_process_request(); } }
/** * @brief Entry point of the USB host management * * The aim is to manage the device target connection and enumeration * depending on the device_state, the function performs the required operations * to get the device enumerated and configured * Once the device is operationnal, the device_state value is DEVICE_READY * This state should be tested by the host task application before performing * any applicative requests to the device. * * @param none * * @return none * * \image html host_task.gif */ void usb_host_task(void) { switch (device_state) { //------------------------------------------------------ // DEVICE_UNATTACHED state // // - Default init state // - Try to give device power supply // case DEVICE_UNATTACHED: Host_clear_device_supported(); // Reset Device status Host_clear_configured(); Host_clear_device_ready(); Usb_clear_all_event(); // Clear all software events new_device_connected=FALSE; selected_device=0; #if (USB_HUB_SUPPORT==ENABLE) nb_hub_present = 0; #endif #if (SOFTWARE_VBUS_CTRL==ENABLE) if( Is_usb_bconnection_error_interrupt()||Is_usb_vbus_error_interrupt()) { Usb_ack_bconnection_error_interrupt(); Usb_ack_vbus_error_interrupt(); Host_clear_vbus_request(); } Usb_disable_vbus_pad(); Usb_enable_manual_vbus(); if(Is_usb_srp_interrupt()) { Usb_ack_srp_interrupt(); Usb_enable_vbus_pad(); Usb_enable_vbus(); device_state=DEVICE_ATTACHED; } #else Usb_enable_vbus(); // Give at least device power supply!!! if(Is_usb_vbus_high()) { device_state=DEVICE_ATTACHED; } // If VBUS ok goto to device connection expectation #endif break; //------------------------------------------------------ // DEVICE_ATTACHED state // // - Vbus is on // - Try to detected device connection // case DEVICE_ATTACHED : if (Is_device_connection() || (force_enumeration==TRUE)) // Device pull-up detected { Host_ack_device_connection(); Host_clear_device_supported(); // Reset Device status Host_clear_configured(); Host_clear_device_ready(); Usb_clear_all_event(); // Clear all software events new_device_connected=FALSE; force_enumeration=FALSE; // Now device is connected, enable disconnection interrupt Host_enable_device_disconnection_interrupt(); Enable_interrupt(); // Reset device status Host_clear_device_supported(); Host_clear_configured(); Host_clear_device_ready(); Host_enable_sof(); // Start Start Of Frame generation Host_enable_sof_interrupt(); // SOF will be detected under interrupt c = 0; while (c<100) // wait 100ms before USB reset { if (Is_usb_event(EVT_HOST_SOF)) { Usb_ack_event(EVT_HOST_SOF); c++; }// Count Start Of frame if (Is_host_emergency_exit() || Is_usb_bconnection_error_interrupt()) {goto device_attached_error;} } Host_disable_device_disconnection_interrupt(); Host_send_reset(); // First USB reset Usb_ack_event(EVT_HOST_SOF); while (Is_host_reset()); // Active wait of end of reset send Host_ack_reset(); //Workaround for some bugly devices with powerless pull up //usually low speed where data line rise slowly and can be interpretaded as disconnection for(c=0;c!=0xFFFF;c++) // Basic Timeout counter { if(Is_usb_event(EVT_HOST_SOF)) //If we detect SOF, device is still alive and connected, just clear false disconnect flag { if(Is_device_disconnection()) { Host_ack_device_connection(); Host_ack_device_disconnection(); break; } } } Host_enable_device_disconnection_interrupt(); // All USB pipes must be reconfigured after a USB reset generation host_configure_pipe(PIPE_CONTROL, \ TYPE_CONTROL, \ TOKEN_SETUP, \ EP_CONTROL, \ SIZE_64, \ ONE_BANK, \ 0 ); c = 0; while (c<100) // wait 100ms after USB reset { if (Is_usb_event(EVT_HOST_SOF)) { Usb_ack_event(EVT_HOST_SOF); c++; }// Count Start Of frame if (Is_host_emergency_exit() || Is_usb_bconnection_error_interrupt()) {goto device_attached_error;} } device_state = DEVICE_POWERED; c=0; } device_attached_error: // Device connection error, or vbus pb -> Retry the connection process from the begining if( Is_usb_bconnection_error_interrupt()||Is_usb_vbus_error_interrupt()||Is_usb_vbus_low()) { Usb_ack_bconnection_error_interrupt(); Usb_enable_vbus_hw_control(); device_state=DEVICE_UNATTACHED; Usb_disable_vbus(); Usb_disable_vbus_pad(); Usb_enable_vbus_pad(); Usb_ack_vbus_error_interrupt(); Usb_enable_vbus(); Usb_disable_vbus_hw_control(); Host_disable_sof(); } break; //------------------------------------------------------ // DEVICE_POWERED state // // - Device connection (attach) as been detected, // - Wait 100ms and configure default control pipe // case DEVICE_POWERED : LOG_STR_CODE(log_device_connected); Host_device_connection_action(); if (Is_usb_event(EVT_HOST_SOF)) { Usb_ack_event(EVT_HOST_SOF); if (c++ >= 100) // Wait 100ms { device_state = DEVICE_DEFAULT; } } break; //------------------------------------------------------ // DEVICE_DEFAULT state // // - Get device descriptor // - Reconfigure Pipe 0 according to Device EP0 // - Attribute device address // case DEVICE_DEFAULT : // Get first device descriptor Host_select_device(0); usb_tree.device[0].ep_ctrl_size=8; if( CONTROL_GOOD == host_get_device_descriptor_uncomplete()) { c = 0; while(c<20) // wait 20ms before USB reset (special buggly devices...) { if (Is_usb_event(EVT_HOST_SOF)) { Usb_ack_event(EVT_HOST_SOF); c++; } if (Is_host_emergency_exit() || Is_usb_bconnection_error_interrupt()) {break;} } Host_disable_device_disconnection_interrupt(); Host_send_reset(); // First USB reset Usb_ack_event(EVT_HOST_SOF); while (Is_host_reset()); // Active wait of end of reset send Host_ack_reset(); //Workaround for some bugly devices with powerless pull up //usually low speed where data line rise slowly and can be interpretaded as disconnection for(c=0;c!=0xFFFF;c++) // Basic Timeout counter { if(Is_usb_event(EVT_HOST_SOF)) //If we detect SOF, device is still alive and connected, just clear false disconnect flag { if(Is_device_disconnection()) { Host_ack_device_connection(); Host_ack_device_disconnection(); break; } } } Host_enable_device_disconnection_interrupt(); c = 0; host_configure_pipe(PIPE_CONTROL, \ TYPE_CONTROL, \ TOKEN_SETUP, \ EP_CONTROL, \ SIZE_64, \ ONE_BANK, \ 0 ); while(c<200) // wait 200ms after USB reset { if (Is_usb_event(EVT_HOST_SOF)) { Usb_ack_event(EVT_HOST_SOF); c++; } if (Is_host_emergency_exit() || Is_usb_bconnection_error_interrupt()) {break;} } usb_tree.device[0].ep_ctrl_size=data_stage[OFFSET_FIELD_MAXPACKETSIZE]; // Give an absolute device address host_set_address(DEVICE_BASE_ADDRESS); usb_tree.device[0].device_address=DEVICE_BASE_ADDRESS; device_state = DEVICE_ADDRESSED; } else { device_state = DEVICE_ERROR; } break; //------------------------------------------------------ // DEVICE_BASE_ADDRESSED state // // - Check if VID PID is in supported list // case DEVICE_ADDRESSED : if (CONTROL_GOOD == host_get_device_descriptor()) { // Detect if the device connected belongs to the supported devices table if (HOST_TRUE == host_check_VID_PID()) { Host_set_device_supported(); Host_device_supported_action(); device_state = DEVICE_CONFIGURED; } else { #if (HOST_STRICT_VID_PID_TABLE==ENABLE) Host_device_not_supported_action(); device_state = DEVICE_ERROR; #else device_state = DEVICE_CONFIGURED; #endif } } else // Can not get device descriptor { device_state = DEVICE_ERROR; } break; //------------------------------------------------------ // DEVICE_CONFIGURED state // // - Configure pipes for the supported interface // - Send Set_configuration() request // - Goto full operating mode (device ready) // case DEVICE_CONFIGURED : if (CONTROL_GOOD == host_get_configuration_descriptor()) { if (HOST_FALSE != host_check_class()) // Class support OK? { usb_tree.nb_device++; #if (HOST_AUTO_CFG_ENDPOINT==ENABLE) if(host_auto_configure_endpoint()) #else Host_set_configured(); // Assumes config is OK with user config if(User_configure_endpoint()) // User call here instead of autoconfig #endif { if (CONTROL_GOOD== host_set_configuration(1)) // Send Set_configuration { //host_set_interface(interface_bound,interface_bound_alt_set); // device and host are now fully configured // goto DEVICE READY normal operation device_state = DEVICE_READY; Host_set_device_ready(); // monitor device disconnection under interrupt Host_enable_device_disconnection_interrupt(); // If user host application requires SOF interrupt event // Keep SOF interrupt enable otherwize, disable this interrupt #if (HOST_CONTINUOUS_SOF_INTERRUPT==DISABLE && USB_HUB_SUPPORT==DISABLE) Host_disable_sof_interrupt(); #endif #if (USB_HUB_SUPPORT==ENABLE) // Check if the connected device is a hub if(Get_class(0)==HUB_CLASS && Get_subclass(0)==0x00 && Get_protocol(0)==0x00) { // Get hub descriptor if( Get_hub_descriptor()==CONTROL_GOOD) { // Power each port of the hub i=data_stage[NB_PORT_OFFSET]; for(c=1;c<=i;c++) { Set_port_feature(PORT_POWER,c); } nb_hub_present = 1; hub_device_address[0]=DEVICE_BASE_ADDRESS; hub_init(nb_hub_present-1); } } else { nb_hub_present = 0; new_device_connected=TRUE; } #else new_device_connected=TRUE; #endif Enable_interrupt(); LOG_STR_CODE(log_device_enumerated); } else// Problem during Set_configuration request... { device_state = DEVICE_ERROR; } } } else // device class not supported... { device_state = DEVICE_ERROR; LOG_STR_CODE(log_device_unsupported); Host_device_class_not_supported_action(); } } else // Can not get configuration descriptors... { device_state = DEVICE_ERROR; } break; //------------------------------------------------------ // DEVICE_READY state // // - Full std operatinf mode // - Nothing to do... // case DEVICE_READY: // Host full std operating mode! new_device_connected=FALSE; #if (USB_HUB_SUPPORT==ENABLE) f_hub_port_disconnect=FALSE; // If one hub is present in the USB tree and the period interval // for the interrupt hub endpoint occurs if(nb_hub_present && hub_interrupt_sof==0) { saved_device=selected_device; // Backup user selected device for(j=1;j<=nb_hub_present;j++) { for(i=0;i<MAX_DEVICE_IN_USB_TREE;i++) { if(usb_tree.device[i].device_address==hub_device_address[j-1]) break; } Host_select_device(i); Host_select_pipe(usb_tree.device[i].interface[0].ep[0].pipe_number); Host_ack_nak_received(); Host_ack_in_received(); Host_unfreeze_pipe(); Host_send_in(); while(1) { if(Is_host_nak_received()) break; if(Is_host_emergency_exit()) break; if(Is_host_in_received()) break; } Host_freeze_pipe(); if(Is_host_nak_received()) { Host_ack_nak_received(); } if(Is_host_in_received()) { if(Is_host_stall()==FALSE) { c=Host_read_byte(); } Host_ack_in_received(); hub_manage_port_change_status(c,j); } } // for all hub Host_select_device(saved_device); // Restore user selected device #if (USER_PERIODIC_PIPE==ENABLE) unfreeze_user_periodic_pipe(); #endif } #endif break; //------------------------------------------------------ // DEVICE_ERROR state // // - Error state // - Do custom action call (probably go to default mode...) // case DEVICE_ERROR : // TODO !!!! #if (HOST_ERROR_RESTART==ENABLE) device_state=DEVICE_UNATTACHED; #endif Host_device_error_action(); break; //------------------------------------------------------ // DEVICE_SUSPENDED state // // - Host application request to suspend the device activity // - State machine comes here thanks to Host_request_suspend() // case DEVICE_SUSPENDED : if(Is_device_supports_remote_wakeup()) // If the connected device supports remote wake up { host_set_feature_remote_wakeup(); // Enable this feature... } LOG_STR_CODE(log_going_to_suspend); c = Is_host_sof_interrupt_enabled(); //Save current sof interrupt enable state Host_disable_sof_interrupt(); Host_ack_sof(); Host_disable_sof(); // Stop start of frame generation, this generates the suspend state Host_ack_hwup(); Host_enable_hwup_interrupt(); // Enable host wake-up interrupt // (this is the unique USB interrupt able to wake up the CPU core from power-down mode) Usb_freeze_clock(); Stop_pll(); Host_suspend_action(); // Custom action here! (for example go to power-save mode...) device_state=DEVICE_WAIT_RESUME; // wait for device resume event break; //------------------------------------------------------ // DEVICE_WAIT_RESUME state // // - Wait in this state till the host receives an upstream resume from the device // - or the host software request the device to resume // case DEVICE_WAIT_RESUME : if(Is_usb_event(EVT_HOST_HWUP)|| Is_host_request_resume())// Remote wake up has been detected // or Local resume request has been received { if(Is_host_request_resume()) // Not a remote wakeup, but an host application request { Host_disable_hwup_interrupt(); // Wake up interrupt should be disable host is now wake up ! // CAUTION HWUP can be cleared only when USB clock is active Pll_start_auto(); // First Restart the PLL for USB operation Wait_pll_ready(); // Get sure pll is lock Usb_unfreeze_clock(); // Enable clock on USB interface Host_ack_hwup(); // Clear HWUP interrupt flag } Host_enable_sof(); Host_send_resume(); // Send down stream resume while (Is_host_down_stream_resume()==FALSE); // Wait Down stream resume sent Host_ack_remote_wakeup(); // Ack remote wake-up reception Host_ack_request_resume(); // Ack software request Host_ack_down_stream_resume(); // Ack down stream resume sent Usb_ack_event(EVT_HOST_HWUP); // Ack software event if(c) { Host_enable_sof_interrupt(); } // Restore SOF interrupt enable state before suspend device_state=DEVICE_READY; // Come back to full operating mode LOG_STR_CODE(log_usb_resumed); } break; //------------------------------------------------------ // DEVICE_DISCONNECTED state // // - Device disconnection has been detected // - Run scheduler in this state at least two times to get sure event is detected by all host application tasks // - Go to DEVICE_DISCONNECTED_ACK state before DEVICE_UNATTACHED, to get sure scheduler calls all app tasks... // case DEVICE_DISCONNECTED : device_state = DEVICE_DISCONNECTED_ACK; break; //------------------------------------------------------ // DEVICE_DISCONNECTED_ACK state // // - Device disconnection has been detected and managed bu applicatives tasks // - Go to DEVICE_UNATTACHED state // case DEVICE_DISCONNECTED_ACK : device_state = DEVICE_UNATTACHED; break; //------------------------------------------------------ // default state // // - Default case: ERROR // - Goto no device state // default : device_state = DEVICE_UNATTACHED; break; } }
__interrupt void usb_general_interrupt() #endif { #if (USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE) U8 i; U8 save_pipe_nb; #endif // ---------- DEVICE events management ----------------------------------- #if (USB_DEVICE_FEATURE == ENABLED) //- VBUS state detection if (Is_usb_vbus_transition() && Is_usb_vbus_interrupt_enabled()) { Usb_ack_vbus_transition(); if (Is_usb_vbus_high()) { usb_connected = TRUE; Usb_vbus_on_action(); Usb_send_event(EVT_USB_POWERED); Usb_enable_reset_interrupt(); usb_start_device(); Usb_attach(); } else { Usb_vbus_off_action(); usb_connected = FALSE; usb_configuration_nb = 0; Usb_send_event(EVT_USB_UNPOWERED); } } // - Device start of frame received if (Is_usb_sof() && Is_sof_interrupt_enabled()) { Usb_ack_sof(); Usb_sof_action(); } // - Device Suspend event (no more USB activity detected) if (Is_usb_suspend() && Is_suspend_interrupt_enabled()) { Usb_ack_suspend(); Usb_enable_wake_up_interrupt(); Usb_ack_wake_up(); // clear wake up to detect next event Usb_freeze_clock(); Usb_send_event(EVT_USB_SUSPEND); Usb_suspend_action(); } // - Wake up event (USB activity detected): Used to resume if (Is_usb_wake_up() && Is_swake_up_interrupt_enabled()) { Usb_unfreeze_clock(); Usb_ack_wake_up(); Usb_disable_wake_up_interrupt(); Usb_wake_up_action(); Usb_send_event(EVT_USB_WAKE_UP); } // - Resume state bus detection if (Is_usb_resume() && Is_resume_interrupt_enabled()) { Usb_disable_wake_up_interrupt(); Usb_ack_resume(); Usb_disable_resume_interrupt(); Usb_resume_action(); Usb_send_event(EVT_USB_RESUME); } // - USB bus reset detection if (Is_usb_reset()&& Is_reset_interrupt_enabled()) { Usb_ack_reset(); usb_init_device(); Usb_reset_action(); Usb_send_event(EVT_USB_RESET); } #endif// End DEVICE FEATURE MODE // ---------- HOST events management ----------------------------------- #if (USB_HOST_FEATURE == ENABLED && USB_DEVICE_FEATURE == ENABLED) // - ID pin change detection if(Is_usb_id_transition()&&Is_usb_id_interrupt_enabled()) { if(Is_usb_id_device()) { g_usb_mode=USB_MODE_DEVICE;} else { g_usb_mode=USB_MODE_HOST;} Usb_ack_id_transition(); if( g_usb_mode != g_old_usb_mode) // Basic Debounce { if(Is_usb_id_device()) // Going to device mode { Usb_send_event(EVT_USB_DEVICE_FUNCTION); } else // Going to host mode { Usb_send_event(EVT_USB_HOST_FUNCTION); } Usb_id_transition_action(); LOG_STR_CODE(log_id_change); #if ( ID_PIN_CHANGE_GENERATE_RESET == ENABLE) // Hot ID transition generates wdt reset #ifndef AVRGCC Wdt_change_16ms(); while(1); #else Wdt_change_enable(); while(1); #endif #endif } } #endif #if (USB_HOST_FEATURE == ENABLED) // - The device has been disconnected if(Is_device_disconnection() && Is_host_device_disconnection_interrupt_enabled()) { host_disable_all_pipe(); Host_ack_device_disconnection(); device_state=DEVICE_DISCONNECTED; Usb_send_event(EVT_HOST_DISCONNECTION); LOG_STR_CODE(log_device_disconnect); Host_device_disconnection_action(); } // - Device connection if(Is_device_connection() && Is_host_device_connection_interrupt_enabled()) { Host_ack_device_connection(); host_disable_all_pipe(); Host_device_connection_action(); } // - Host Start of frame has been sent if (Is_host_sof() && Is_host_sof_interrupt_enabled()) { Host_ack_sof(); Usb_send_event(EVT_HOST_SOF); private_sof_counter++; // delay timeout management for interrupt tranfer mode in host mode #if ((USB_HOST_PIPE_INTERRUPT_TRANSFER==ENABLE) && (TIMEOUT_DELAY_ENABLE==ENABLE)) if (private_sof_counter>=250) // Count 1/4 sec { private_sof_counter=0; for(i=0;i<MAX_EP_NB;i++) { if(it_pipe_str[i].enable==ENABLE) { save_pipe_nb=Host_get_selected_pipe(); Host_select_pipe(i); if((++it_pipe_str[i].timeout>TIMEOUT_DELAY) && (Host_get_pipe_type()!=TYPE_INTERRUPT)) { it_pipe_str[i].enable=DISABLE; it_pipe_str[i].status=PIPE_DELAY_TIMEOUT; Host_stop_pipe_interrupt(i); if (is_any_interrupt_pipe_active()==FALSE) // If no more transfer is armed { if (g_sav_int_sof_enable==FALSE) { Host_disable_sof_interrupt(); } } it_pipe_str[i].handle(PIPE_DELAY_TIMEOUT,it_pipe_str[i].nb_byte_processed); } Host_select_pipe(save_pipe_nb); } } } #endif // (USB_HOST_PIPE_INTERRUPT_TRANSFER==ENABLE) && (TIMEOUT_DELAY_ENABLE==ENABLE)) Host_sof_action(); } // - Host Wake-up has been received if (Is_host_hwup() && Is_host_hwup_interrupt_enabled()) { Host_disable_hwup_interrupt(); // Wake up interrupt should be disable host is now wake up ! // CAUTION HWUP can be cleared only when USB clock is active (not frozen)! Pll_start_auto(); // First Restart the PLL for USB operation Wait_pll_ready(); // Get sure pll is lock Usb_unfreeze_clock(); // Enable clock on USB interface Host_ack_hwup(); // Clear HWUP interrupt flag Usb_send_event(EVT_HOST_HWUP); // Send software event Host_hwup_action(); // Map custom action } #endif // End HOST FEATURE MODE }
//! Configures the PSC1 accordingly to the PSC1 Define Configuration values. //! //! @param None. //! //! @pre - Define the configuration in config.h file //! //! @post - Set the psc1 comparison values //! - Start the PSC1 thanks to Start_psc1() macro void psc1Init(void) { # if (PSC1_PLL_FREQUENCY == 64) pllStart64M(); # elif (PSC1_PLL_FREQUENCY == 32) pllStart32M(); # elif (PSC1_PLL_FREQUENCY == 0) # else # error (PSC1_PLL_FREQUENCY should be 0, 32, or 64... See config.h file) # endif # if (PSC1_OUTPUT_SELECTION == 3) psc1EnableBothOutputs(); # elif (PSC1_OUTPUT_SELECTION == 2) psc1Enable_out11(); # elif (PSC1_OUTPUT_SELECTION == 1) psc1Enable_out10(); # elif (PSC1_OUTPUT_SELECTION == 0) psc1DisableBothOutputs(); # else # error (PSC1_OUTPUT_SELECTION should be 0, 1, 2, or 2... See config.h file) # endif # if (PSC1_ACTIVE_STATE == 1) psc1OutputsActiveHigh(); # elif (PSC1_ACTIVE_STATE == 0) psc1OutputsActiveLow(); # else # error (PSC1_ACTIVE_STATE should be 0, or 1... See config.h file) # endif # if (PSC1_RAMP_MODE == 4) psc1InFourRampMode(); # elif (PSC1_RAMP_MODE == 2) psc1InTwoRampMode(); # elif (PSC1_RAMP_MODE == 1) psc1InOneRampMode(); # elif (PSC1_RAMP_MODE == 0) psc1InCenteredAlignedMode(); # else # error (PSC1_RAMP_MODE should be 0, 1, 3, or 4... See config.h file) # endif # if (PSC1_FIFTY_PERCENT_MODE == 1) psc1EnableFiftyPercentMode(); # elif (PSC1_FIFTY_PERCENT_MODE == 0) psc1DisableFiftyPercentMode(); # else # error (PSC1_FIFTY_PERCENT_MODE should be 0, or 1) # endif # if (PSC1_SYMETRICAL_FLANK_WIDTH_MODULATION == 1) psc1SymetricalFlankWidthModulation(); # elif (PSC1_SYMETRICAL_FLANK_WIDTH_MODULATION == 0) psc1EndOfCycleFlankWidthModulation(); # else # error (PSC1_SYMETRICAL_FLANK_WIDTH_MODULATION should be 0, or 1... See config.h file) # endif # if (PSC1_AUTOLOCK_MODE == 1) psc1EnableAutolockMode(); # elif (PSC1_AUTOLOCK_MODE == 0) psc1DisableAutolockMode(); # else # error (PSC1_AUTOLOCK_MODE should be 0, or 1... See config.h file) # endif # if (PSC1_PRESCALER_DIVIDER == 256) psc1DivideInputClockBy256(); # elif (PSC1_PRESCALER_DIVIDER == 32) psc1DivideInputClockBy32(); # elif (PSC1_PRESCALER_DIVIDER == 4) psc1DivideInputClockBy4(); # elif (PSC1_PRESCALER_DIVIDER == 0) psc1Disableprescaler(); # else # error (PSC1_PRESCALER_DIVIDER should be 0, 4, 32, or 256... See config.h file) # endif # if (PSC1_PLL_FREQUENCY == 64) pllWaitReady(); psc1UsePllClock(); # elif (PSC1_PLL_FREQUENCY == 32) Wait_pll_ready(); psc1UsePllClock(); # elif (PSC1_PLL_FREQUENCY == 0) psc1UseIoClock(); # else # error (PSC1_PLL_FREQUENCY should be 0, 32, or 64... See config.h file) # endif }