//! This function checks if the device class is supported. //! The function looks in all interfaces declared in the received descriptors if //! one of them matches an entry of the CLASS/SUB_CLASS/PROTOCOL table. //! If HOST_AUTO_CFG_ENDPOINT is enabled, a pipe is configured for each endpoint //! of supported interfaces. //! //! @return bool: Status //! bool host_check_class(void) { uint8_t *descriptor, *conf_end; uint8_t device_class, device_subclass, device_protocol; uint8_t c; #if HOST_AUTO_CFG_ENDPOINT == ENABLE uint8_t nb_endpoint_to_configure = 0; uint8_t ep_index = 0; uint8_t physical_pipe = P_1; // P_1 because physical pipe 0 is reserved for control uint16_t ep_size; // By default, the host is configured when returning Host_set_configured(); #endif // First, assume no interface is supported nb_interface_supported = 0; // Check if configuration descriptor if (data_stage[OFFSET_FIELD_DESCRIPTOR_TYPE] != CONFIGURATION_DESCRIPTOR) return false; bmattributes = data_stage[OFFSET_FIELD_BMATTRIBUTES]; maxpower = data_stage[OFFSET_FIELD_MAXPOWER]; conf_end = data_stage + min(usb_format_usb_to_mcu_data(16, *(uint16_t *)(data_stage + OFFSET_FIELD_TOTAL_LENGTH)), SIZEOF_DATA_STAGE - OFFSET_FIELD_PROTOCOL); // Look in all interfaces declared in the configuration for (descriptor = data_stage + data_stage[OFFSET_DESCRIPTOR_LENGTH]; descriptor < conf_end; descriptor += descriptor[OFFSET_DESCRIPTOR_LENGTH]) { // Find next interface descriptor switch (descriptor[OFFSET_FIELD_DESCRIPTOR_TYPE]) { case INTERFACE_DESCRIPTOR: // Check the number of supported interfaces does not exceed the maximum if (nb_interface_supported >= MAX_INTERFACE_SUPPORTED) return true; #if HOST_AUTO_CFG_ENDPOINT == ENABLE // If there are still endpoints to configure although a new interface descriptor has been found if (nb_endpoint_to_configure) { // Mark the host as not configured Host_clear_configured(); // Reset the number of endpoints to configure nb_endpoint_to_configure = 0; } #endif // Found an interface descriptor // Get characteristics of this interface device_class = descriptor[OFFSET_FIELD_CLASS]; device_subclass = descriptor[OFFSET_FIELD_SUB_CLASS]; device_protocol = descriptor[OFFSET_FIELD_PROTOCOL]; // Look in registered class table for match for (c = 0; c < REG_CLASS_CNT; c += 3) { if (registered_class[c] == device_class && // Class is correct registered_class[c + 1] == device_subclass && // Subclass is correct registered_class[c + 2] == device_protocol) // Protocol is correct { // Store this interface as supported interface // Memorize its interface nb interface_supported[nb_interface_supported].interface_nb = descriptor[OFFSET_FIELD_INTERFACE_NB]; // its alternate setting interface_supported[nb_interface_supported].altset_nb = descriptor[OFFSET_FIELD_ALT]; // its USB class interface_supported[nb_interface_supported].uclass = device_class; // its USB subclass interface_supported[nb_interface_supported].subclass = device_subclass; // its USB protocol interface_supported[nb_interface_supported].protocol = device_protocol; // the number of endpoints associated with this interface #if HOST_AUTO_CFG_ENDPOINT == ENABLE ep_index = 0; nb_endpoint_to_configure = #endif interface_supported[nb_interface_supported].nb_ep = min(descriptor[OFFSET_FIELD_NB_OF_EP], MAX_EP_PER_INTERFACE); // Update the number of supported interfaces nb_interface_supported++; // Class/subclass/protocol is registered, so look for next interface descriptor break; } } break; #if HOST_AUTO_CFG_ENDPOINT == ENABLE case ENDPOINT_DESCRIPTOR: // If there are still endpoints to configure while there are free pipes if (physical_pipe < MAX_PEP_NB && nb_endpoint_to_configure) { nb_endpoint_to_configure--; // Reconfigure the new physical pipe to get rid of any previous configuration #if USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE cpu_irq_disable(); #endif Host_disable_pipe(physical_pipe); #if USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE (void)Is_host_pipe_enabled(physical_pipe); cpu_irq_enable(); #endif Host_unallocate_memory(physical_pipe); Host_enable_pipe(physical_pipe); // Fix HW, set freq at 0 in case of no interrupt endpoint if( TYPE_INTERRUPT != descriptor[OFFSET_FIELD_EP_TYPE] ) descriptor[OFFSET_FIELD_EP_INTERVAL] = 0; ep_size = descriptor[OFFSET_FIELD_EP_SIZE] | descriptor[OFFSET_FIELD_EP_SIZE + 1] << 8; #if BOARD != EVK1104 if (ep_size <= 64) { #endif // Build the pipe configuration according to the endpoint descriptor fields received (void)Host_configure_pipe( physical_pipe, // Pipe nb in USB interface descriptor[OFFSET_FIELD_EP_INTERVAL], // Interrupt period (for interrupt pipe) Get_desc_ep_nbr(descriptor[OFFSET_FIELD_EP_ADDR]), // Pipe endpoint number descriptor[OFFSET_FIELD_EP_TYPE], // Pipe type (isochronous/bulk/interrupt) Get_pipe_token(descriptor[OFFSET_FIELD_EP_ADDR]), // Pipe token (IN/OUT) ep_size, // Pipe size (descriptor[OFFSET_FIELD_EP_TYPE] == TYPE_BULK) ? SINGLE_BANK : DOUBLE_BANK // Number of banks to allocate for pipe ); // Update endpoint pipe table in supported interface structure interface_supported[nb_interface_supported - 1].ep_pipe[ep_index++] = physical_pipe++; } #if BOARD != EVK1104 else { // Build the pipe configuration according to the endpoint descriptor fields received (void)Host_configure_pipe( MAX_PEP_NB - 1, // Pipe nb in USB interface descriptor[OFFSET_FIELD_EP_INTERVAL], // Interrupt period (for interrupt pipe) Get_desc_ep_nbr(descriptor[OFFSET_FIELD_EP_ADDR]), // Pipe endpoint number descriptor[OFFSET_FIELD_EP_TYPE], // Pipe type (isochronous/bulk/interrupt) Get_pipe_token(descriptor[OFFSET_FIELD_EP_ADDR]), // Pipe token (IN/OUT) ep_size, // Pipe size (descriptor[OFFSET_FIELD_EP_TYPE] == TYPE_BULK) ? SINGLE_BANK : DOUBLE_BANK // Number of banks to allocate for pipe ); // Update endpoint pipe table in supported interface structure interface_supported[nb_interface_supported - 1].ep_pipe[ep_index++] = MAX_PEP_NB - 1; } } #endif break; #endif } // Call user callback to look more deeply into the configuration descriptor Host_user_check_class_action(descriptor); } #if HOST_AUTO_CFG_ENDPOINT == ENABLE // If there are still endpoints to configure although all descriptors have been parsed if (nb_endpoint_to_configure) { // Mark the host as not configured Host_clear_configured(); } #endif return (nb_interface_supported > 0); }
/** * @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; } }