USB device task


Defines

#define B_IDLE   0
#define B_SRP_INIT   1
#define B_PERIPHERAL   2
#define B_WAIT_ACON   3
#define B_HOST   4
#define B_END_HNP_SUSPEND   5
#define Start_session_with_srp()   (otg_device_sessions |= 0x01)
 Is the current session a result of a SRP ?
#define End_session_with_srp()   (otg_device_sessions &= ~0x01)
#define Is_session_started_with_srp()   (((otg_device_sessions&0x01) != 0) ? TRUE : FALSE)
#define Srp_sent_and_waiting_answer()   (otg_device_sessions |= 0x02)
 Has a SRP been sent, and waiting for an answer.
#define Ack_srp_sent_and_answer()   (otg_device_sessions &= ~0x02)
#define Is_srp_sent_and_waiting_answer()   (((otg_device_sessions&0x02) != 0) ? TRUE : FALSE)
#define TB_SRP_FAIL_MIN   0x0A28
 Is the Tb_Srp counter enabled ? Cleared by timer if Tb_Srp_Fail elapsed.
#define Init_tb_srp_counter()   (otg_tb_srp_cpt = 0)
#define Is_tb_srp_counter_overflow()   ((otg_tb_srp_cpt > TB_SRP_FAIL_MIN) ? TRUE : FALSE)

Functions

void usb_device_task_init (void)
void usb_start_device (void)
void usb_device_task (void)

Variables

bit usb_suspended
 Public : (bit) usb_suspended usb_suspended is set to TRUE when USB is in suspend mode usb_suspended is set to FALSE otherwise /.
U8 otg_device_sessions
 Public : (U8) otg_device_sessions; Store some events and conditions specifics to OTG Devices sessions.
U8 otg_b_device_state
 Public : (U8) otg_b_device_state; Store the current state of the B-Device.
U8 sof_seen_in_session
 Public : (U8) sof_seen_in_session; Indicates if a SOF has been received during the current session /.
U16 otg_tb_srp_cpt
 Public : (U16) otg_tb_srp_cpt; Counter used to signal a SRP fail condition (SRP fails if Tb_Srp_Fail elapsed).

Define Documentation

#define B_IDLE   0

Definitions of B-Device states

Definition at line 67 of file usb_device_task.h.

Referenced by usb_device_task().

#define B_SRP_INIT   1

Definition at line 68 of file usb_device_task.h.

Referenced by usb_device_task().

#define B_PERIPHERAL   2

Definition at line 69 of file usb_device_task.h.

Referenced by usb_device_task().

#define B_WAIT_ACON   3

Definition at line 70 of file usb_device_task.h.

#define B_HOST   4

Definition at line 71 of file usb_device_task.h.

Referenced by usb_device_task().

#define B_END_HNP_SUSPEND   5

Definition at line 72 of file usb_device_task.h.

Referenced by usb_device_task().

 
#define Start_session_with_srp (  )     (otg_device_sessions |= 0x01)

Is the current session a result of a SRP ?

Definition at line 82 of file usb_device_task.h.

Referenced by usb_device_task().

 
#define End_session_with_srp (  )     (otg_device_sessions &= ~0x01)

Definition at line 83 of file usb_device_task.h.

Referenced by usb_device_task().

 
#define Is_session_started_with_srp (  )     (((otg_device_sessions&0x01) != 0) ? TRUE : FALSE)

Definition at line 84 of file usb_device_task.h.

 
#define Srp_sent_and_waiting_answer (  )     (otg_device_sessions |= 0x02)

Has a SRP been sent, and waiting for an answer.

Definition at line 87 of file usb_device_task.h.

Referenced by usb_device_task().

 
#define Ack_srp_sent_and_answer (  )     (otg_device_sessions &= ~0x02)

Definition at line 88 of file usb_device_task.h.

Referenced by usb_device_task().

 
#define Is_srp_sent_and_waiting_answer (  )     (((otg_device_sessions&0x02) != 0) ? TRUE : FALSE)

Definition at line 89 of file usb_device_task.h.

Referenced by usb_device_task().

#define TB_SRP_FAIL_MIN   0x0A28

Is the Tb_Srp counter enabled ? Cleared by timer if Tb_Srp_Fail elapsed.

Tb_Srp_Fail must be between 5 and 6 sec. With an interrupt routine executed each 2ms, its value becomes 2500 (used:5.2sec)

Definition at line 93 of file usb_device_task.h.

Referenced by usb_device_task().

 
#define Init_tb_srp_counter (  )     (otg_tb_srp_cpt = 0)

Definition at line 95 of file usb_device_task.h.

Referenced by usb_device_task().

 
#define Is_tb_srp_counter_overflow (  )     ((otg_tb_srp_cpt > TB_SRP_FAIL_MIN) ? TRUE : FALSE)

Definition at line 96 of file usb_device_task.h.

Referenced by usb_device_task().


Function Documentation

void usb_device_task_init ( void   ) 

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.

Parameters:
none 
Returns:
none
This function initializes the USB device controller and system interrupt 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.

Parameters:
none 
Returns:
none
/

Definition at line 135 of file usb_device_task.c.

References otg_device_sessions, Usb_disable, Usb_enable, Usb_enable_id_interrupt, Usb_low_speed_mode, and Usb_select_device.

00136 {
00137    Enable_interrupt();
00138    Usb_disable();
00139    Usb_enable();
00140    Usb_select_device();
00141 #if (USB_LOW_SPEED_DEVICE==ENABLE)
00142    Usb_low_speed_mode();
00143 #endif
00144    Enable_interrupt();
00145 #if (USB_OTG_FEATURE == ENABLED)
00146    Usb_enable_id_interrupt();
00147    Clear_otg_features_from_host();
00148    otg_device_sessions = 0;
00149 #endif
00150 }

void usb_start_device ( void   ) 

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

Parameters:
none 
Returns:
none

Definition at line 165 of file usb_device_task.c.

References Usb_attach, Usb_enable_id_interrupt, Usb_enable_reset_interrupt, Usb_enable_suspend_interrupt, usb_init_device(), Usb_unfreeze_clock, and Wait_pll_ready.

00166 {
00167    Pll_start_auto();
00168    Wait_pll_ready();
00169 
00170    Disable_interrupt();
00171    Usb_unfreeze_clock();
00172    usb_init_device();         // configure the USB controller EP0
00173    Usb_attach();
00174    Enable_interrupt();
00175    
00176    Usb_enable_suspend_interrupt();
00177    Usb_enable_reset_interrupt();
00178 #if (USB_OTG_FEATURE == ENABLED)
00179    Usb_enable_id_interrupt();
00180 #endif
00181 }

Here is the call graph for this function:

void usb_device_task ( void   ) 

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

Parameters:
none 
Returns:
none

Definition at line 193 of file usb_device_task.c.

References Ack_srp_sent_and_answer, Ack_user_request_disc, Ack_user_request_hnp, Ack_user_request_srp, Ack_user_request_suspend, B_END_HNP_SUSPEND, B_HOST, B_IDLE, B_PERIPHERAL, B_SRP_INIT, Clear_all_user_request, device_state, DEVICE_UNATTACHED, DISABLED, End_session_with_srp, EP_CONTROL, EVT_OTG_DEV_UNSUPPORTED, EVT_OTG_DEVICE_CONNECTED, EVT_OTG_HNP_ERROR, EVT_USB_POWERED, EVT_USB_RESET, EVT_USB_RESUME, EVT_USB_SUSPEND, EVT_USB_UNPOWERED, FALSE, Host_disable_sof, Init_tb_srp_counter, Is_otg_event, Is_srp_sent_and_waiting_answer, Is_tb_srp_counter_overflow, Is_usb_device_initiating_srp, Is_usb_event, Is_usb_id_device, Is_usb_pending_remote_wake_up, Is_usb_receive_setup, Is_usb_vbus_high, Is_usb_vbus_low, Is_user_requested_disc, Is_user_requested_hnp, Is_user_requested_srp, Is_user_requested_suspend, Otg_ack_event, otg_b_device_state, Otg_print_new_event_message, Otg_print_new_failure_message, OTG_TEMPO_2SEC, OTG_TEMPO_3SEC, OTG_TEMPO_4SEC, OTGMSG_A_RESPONDED, OTGMSG_CONNECTED_TO_A, OTGMSG_DEVICE_NO_RESP, OTGMSG_SRP_A_NO_RESP, OTGMSG_SRP_STARTED, remote_wakeup_feature, sof_seen_in_session, Srp_sent_and_waiting_answer, Start_session_with_srp, TB_SRP_FAIL_MIN, TRUE, Usb_ack_event, Usb_ack_remote_wake_up_start, Usb_ack_suspend, Usb_attach, usb_configuration_nb, usb_connected, Usb_detach, Usb_device_initiate_srp, Usb_device_stop_hnp, Usb_disable, Usb_enable, Usb_enable_sof_interrupt, Usb_enable_suspend_interrupt, Usb_enable_uid_pin, Usb_freeze_clock, Usb_host_reject_hnp, usb_host_task(), usb_process_request(), Usb_reset_endpoint, Usb_select_device, Usb_select_endpoint, Usb_send_event, usb_start_device(), Usb_unfreeze_clock, Usb_vbus_off_action, Usb_vbus_on_action, and Wait_pll_ready.

00194 {
00195   
00196 #if (USB_OTG_FEATURE == ENABLED)
00197    // Check if a reset has been received
00198    if(Is_usb_event(EVT_USB_RESET))
00199    {
00200       Usb_ack_event(EVT_USB_RESET);
00201       Usb_reset_endpoint(0);
00202       usb_configuration_nb=0;
00203       otg_b_device_state = B_IDLE;
00204       Clear_otg_features_from_host();
00205    }
00206 
00207    // When OTG mode enabled, B-Device is managed thanks to its state machine
00208    switch (otg_b_device_state)
00209    {
00210    //------------------------------------------------------
00211    //   B_IDLE state
00212    //
00213    //   - waits for Vbus to rise
00214    //   - initiate SRP if asked by user
00215    //
00216    case B_IDLE:
00217      if (Is_usb_vbus_high())
00218      {
00219        // Vbus rise
00220        usb_connected = TRUE;
00221        remote_wakeup_feature = DISABLED;
00222        usb_start_device();
00223        Usb_vbus_on_action();
00224        Usb_attach();
00225        otg_b_device_state = B_PERIPHERAL;
00226        Ack_user_request_srp();
00227        Clear_otg_features_from_host();
00228        remote_wakeup_feature = DISABLED;
00229        End_session_with_srp();
00230        if (Is_srp_sent_and_waiting_answer() && (sof_seen_in_session == TRUE))
00231        {
00232          Ack_srp_sent_and_answer();
00233          Otg_print_new_failure_message(OTGMSG_A_RESPONDED,OTG_TEMPO_2SEC);
00234        }
00235        Usb_enable_sof_interrupt();
00236        
00237      }
00238      else
00239      {
00240        if (Is_user_requested_srp() && Is_usb_id_device())
00241        {
00242          // User has requested a SRP
00243          Ack_user_request_srp();
00244          if (!Is_srp_sent_and_waiting_answer())
00245          {
00246            Pll_start_auto();  // reinit device mode
00247            Wait_pll_ready();
00248            Usb_disable();
00249            Usb_enable_uid_pin();
00250            Usb_enable();
00251            Usb_unfreeze_clock();
00252            Usb_select_device();
00253            Usb_attach();
00254            otg_b_device_state = B_SRP_INIT;
00255            Usb_device_initiate_srp();       // hardware waits for initial condition (SE0, Session End level)
00256            sof_seen_in_session = FALSE;
00257          }
00258        }
00259        if ((Is_srp_sent_and_waiting_answer()) && (Is_tb_srp_counter_overflow()))
00260        {
00261          // SRP failed because A-Device did not respond
00262          End_session_with_srp();
00263          Ack_srp_sent_and_answer();
00264          Otg_print_new_failure_message(OTGMSG_SRP_A_NO_RESP,OTG_TEMPO_3SEC);
00265        }
00266      }
00267      break;
00268 
00269      
00270    //------------------------------------------------------
00271    //   B_SRP_INIT
00272    //
00273    //   - a SRP has been initiated
00274    //   - B-Device waits it is finished to initialize variables
00275    //
00276    case B_SRP_INIT:
00277      if (!Is_usb_device_initiating_srp())
00278      {
00279        otg_b_device_state = B_IDLE;   // SRP initiated, return to Idle state (wait for Vbus to rise)
00280        Srp_sent_and_waiting_answer();
00281        Init_tb_srp_counter();
00282        Start_session_with_srp();
00283        Otg_print_new_event_message(OTGMSG_SRP_STARTED,TB_SRP_FAIL_MIN);
00284      }
00285      break;
00286 
00287      
00288    //------------------------------------------------------
00289      //   B_PERIPHERAL : the main state of OTG Peripheral
00290    //
00291    //   - all events are interrupt-handled
00292    //   - but they are saved and this function can execute alternate actions
00293    //   - also handle user requests (disconnect)
00294    //
00295    // ======================================================================================
00296    case B_PERIPHERAL:
00297      if (Is_otg_event(EVT_OTG_DEVICE_CONNECTED))
00298      {
00299        Otg_ack_event(EVT_OTG_DEVICE_CONNECTED); // set on a SetConfiguration descriptor reception
00300        Otg_print_new_event_message(OTGMSG_CONNECTED_TO_A,OTG_TEMPO_4SEC);
00301      }
00302      if (Is_usb_event(EVT_USB_SUSPEND)) // SUSPEND state
00303      {
00304         // Suspend and HNP operations are handled in the interrupt functions
00305      }
00306      if (Is_srp_sent_and_waiting_answer() && (sof_seen_in_session == TRUE))
00307      {
00308        Ack_srp_sent_and_answer();
00309        Otg_print_new_failure_message(OTGMSG_A_RESPONDED,OTG_TEMPO_2SEC);
00310      }
00311      if ((Is_srp_sent_and_waiting_answer()) && (Is_tb_srp_counter_overflow())) 
00312      {
00313        // SRP failed because A-Device did not respond
00314        End_session_with_srp();
00315        Ack_srp_sent_and_answer();
00316        Otg_print_new_failure_message(OTGMSG_SRP_A_NO_RESP,OTG_TEMPO_3SEC);
00317      }
00318      
00319      if (Is_usb_event(EVT_USB_RESUME) && !Is_usb_pending_remote_wake_up())  // RESUME signal detected
00320      {
00321        Usb_ack_event(EVT_USB_RESUME);
00322        Usb_ack_event(EVT_USB_SUSPEND);
00323        Usb_ack_remote_wake_up_start();
00324      }
00325      if (Is_usb_event(EVT_USB_UNPOWERED))
00326      {
00327        Usb_ack_event(EVT_USB_UNPOWERED);
00328        Clear_all_user_request();
00329        otg_b_device_state = B_IDLE;
00330      }
00331      if(Is_usb_event(EVT_USB_RESET))
00332      {
00333        Usb_ack_event(EVT_USB_RESET);
00334        Usb_reset_endpoint(0);
00335        usb_configuration_nb=0;
00336        Clear_otg_features_from_host();
00337      }
00338      if (Is_otg_event(EVT_OTG_HNP_ERROR))
00339      {
00340        Otg_ack_event(EVT_OTG_HNP_ERROR);
00341        Otg_print_new_failure_message(OTGMSG_DEVICE_NO_RESP,OTG_TEMPO_4SEC);
00342        PORTC &= ~0x10;
00343      }
00344      if (Is_user_requested_disc())
00345      {
00346        Ack_user_request_disc();
00347        if (Is_usb_id_device())
00348        {
00349          Usb_detach();
00350          Usb_freeze_clock();
00351          while (Is_usb_vbus_high());  // wait for Vbus to be under Va_vbus_valid
00352          otg_b_device_state = B_IDLE;
00353          usb_configuration_nb = 0;
00354          usb_connected = FALSE;
00355          Clear_all_user_request();
00356        }
00357      }
00358      break;
00359 
00360    //------------------------------------------------------
00361    //   B_HOST
00362    //
00363    //   - state entered after an HNP success
00364    //   - handle user requests (disconnection, suspend, hnp)
00365    //   - call the "host_task()" for Host level handlers
00366    //
00367    // ======================================================================================
00368    case B_HOST:
00369      if (Is_otg_event(EVT_OTG_DEV_UNSUPPORTED))
00370      {
00371        Otg_ack_event(EVT_OTG_DEV_UNSUPPORTED);
00372        Clear_all_user_request();
00373        otg_b_device_state = B_IDLE;
00374        device_state = DEVICE_UNATTACHED;
00375      }
00376      if (Is_user_requested_disc() || Is_user_requested_suspend() || Is_user_requested_hnp())
00377      {
00378        Ack_user_request_disc();   // suspend and hnp requests cleared in B_END_HNP_SUSPEND stage
00379        Host_disable_sof();        // go into suspend mode
00380        Usb_host_reject_hnp();
00381        otg_b_device_state = B_END_HNP_SUSPEND;
00382        Usb_ack_suspend();
00383        Usb_enable_suspend_interrupt();
00384      }
00385      if (Is_usb_event(EVT_USB_UNPOWERED))
00386      {
00387        Usb_ack_event(EVT_USB_UNPOWERED);
00388        Usb_freeze_clock();
00389        otg_b_device_state = B_IDLE;
00390        device_state = DEVICE_UNATTACHED;
00391      }
00392      usb_host_task();   // call the host task
00393      break;
00394 
00395    //------------------------------------------------------
00396    //   B_END_HNP_SUSPEND
00397    //
00398    //   - device enters this state after being B_HOST, on a user request to stop bus activity (suspend, disconnect or hnp request)
00399    //   - macro is reset to peripheral mode
00400    //
00401    // ======================================================================================
00402    case B_END_HNP_SUSPEND:
00403      if (Is_usb_event(EVT_USB_SUSPEND))
00404      {
00405        Usb_ack_event(EVT_USB_SUSPEND);
00406        Usb_device_stop_hnp();
00407        Usb_select_device();
00408        device_state = DEVICE_UNATTACHED;
00409        if (Is_user_requested_hnp() || Is_user_requested_suspend())
00410        {
00411          otg_b_device_state = B_PERIPHERAL;
00412          Ack_user_request_suspend();
00413          Ack_user_request_hnp();
00414        }
00415        else
00416        {
00417          otg_b_device_state = B_IDLE;
00418          Usb_detach();
00419          Usb_freeze_clock();
00420        }
00421      }
00422      break;
00423 
00424 
00425    default:
00426      otg_b_device_state = B_IDLE;
00427      Clear_all_user_request();
00428      device_state = DEVICE_UNATTACHED;
00429      break;
00430    }
00431 
00432 
00433 #else
00434    
00435    // Non-OTG exclusives Device operations
00436 
00437    // VBUS state detection
00438    if (Is_usb_vbus_high()&& (usb_connected==FALSE))
00439    {
00440       usb_connected = TRUE;
00441       remote_wakeup_feature = DISABLED;
00442       Usb_vbus_on_action();
00443       Usb_send_event(EVT_USB_POWERED);
00444       usb_start_device();
00445    }
00446    if (Is_usb_vbus_low()&& (usb_connected==TRUE))
00447    {
00448       usb_connected = FALSE;
00449       usb_configuration_nb = 0;
00450       Usb_send_event(EVT_USB_UNPOWERED);
00451       Usb_detach();
00452       Usb_freeze_clock();
00453       Usb_vbus_off_action();
00454    }
00455 
00456    if(Is_usb_event(EVT_USB_RESET))
00457    {
00458       Usb_ack_event(EVT_USB_RESET);
00459       Usb_reset_endpoint(0);
00460       usb_configuration_nb=0;
00461    }
00462 
00463 #endif
00464 
00465    
00466    
00467    // =======================================
00468    // Common Standard Device Control Requests
00469    // =======================================
00470    //   - device enumeration process
00471    //   - device control commands and features
00472    Usb_select_endpoint(EP_CONTROL);
00473    if (Is_usb_receive_setup())
00474    {
00475       usb_process_request();
00476    }
00477 }

Here is the call graph for this function:


Variable Documentation

bit usb_suspended

Public : (bit) usb_suspended usb_suspended is set to TRUE when USB is in suspend mode usb_suspended is set to FALSE otherwise /.

Definition at line 83 of file usb_device_task.c.

U8 otg_device_sessions

Public : (U8) otg_device_sessions; Store some events and conditions specifics to OTG Devices sessions.

OTG B-Device SRP protocole specific states or events

Definition at line 104 of file usb_device_task.c.

U8 otg_b_device_state

Public : (U8) otg_b_device_state; Store the current state of the B-Device.

Definition at line 100 of file usb_device_task.c.

U8 sof_seen_in_session

Public : (U8) sof_seen_in_session; Indicates if a SOF has been received during the current session /.

Definition at line 117 of file usb_device_task.c.

U16 otg_tb_srp_cpt

Public : (U16) otg_tb_srp_cpt; Counter used to signal a SRP fail condition (SRP fails if Tb_Srp_Fail elapsed).

Definition at line 112 of file usb_device_task.c.


Generated on Fri May 15 15:41:50 2009 for ATMEL by  doxygen 1.5.3