USB device task module


Defines

#define B_IDLE   0
 Definitions of B-Device states.
#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
 Tb_Srp_Fail must be between 5 and 6 sec. With an interrupt routine executed each 2ms, its value becomes 2500 (used:5.2sec).
#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)
 This function initializes the USB device controller.
void usb_start_device (void)
 This function initializes the USB device controller.
void usb_device_task (void)
 Entry point of the USB device mamagement.

Variables

U8 otg_device_sessions
 OTG B-Device SRP protocole specific states or events.
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 33 of file usb_device_task.h.

Referenced by usb_device_task(), usb_general_interrupt(), usb_host_task(), and usb_task_init().

#define B_SRP_INIT   1

Definition at line 34 of file usb_device_task.h.

Referenced by usb_device_task().

#define B_PERIPHERAL   2

Definition at line 35 of file usb_device_task.h.

Referenced by usb_device_task(), and usb_general_interrupt().

#define B_WAIT_ACON   3

Definition at line 36 of file usb_device_task.h.

#define B_HOST   4

Definition at line 37 of file usb_device_task.h.

Referenced by usb_device_task(), usb_general_interrupt(), and usb_host_task().

#define B_END_HNP_SUSPEND   5

Definition at line 38 of file usb_device_task.h.

Referenced by usb_device_task(), and usb_general_interrupt().

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

Is the current session a result of a SRP ?

Definition at line 48 of file usb_device_task.h.

Referenced by usb_device_task().

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

Definition at line 49 of file usb_device_task.h.

Referenced by usb_device_task(), usb_general_interrupt(), and usb_host_task().

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

Definition at line 50 of file usb_device_task.h.

Referenced by usb_general_interrupt().

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

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

Definition at line 53 of file usb_device_task.h.

Referenced by usb_device_task().

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

Definition at line 54 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 55 of file usb_device_task.h.

Referenced by otg_timer_interrupt(), and usb_device_task().

#define TB_SRP_FAIL_MIN   0x0A28

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 59 of file usb_device_task.h.

Referenced by usb_device_task().

 
#define Init_tb_srp_counter (  )     (otg_tb_srp_cpt = 0)

Definition at line 61 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 62 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
/

Definition at line 100 of file usb_device_task.c.

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

Referenced by usb_task_init().

00101 {
00102    Enable_interrupt();
00103    Usb_disable();
00104    Usb_enable();
00105    Usb_select_device();
00106 #if (USB_LOW_SPEED_DEVICE==ENABLE)
00107    Usb_low_speed_mode();
00108 #endif
00109    Usb_enable_vbus_interrupt();
00110    Enable_interrupt();
00111 #if (USB_OTG_FEATURE == ENABLED)
00112    Usb_enable_id_interrupt();
00113    Clear_otg_features_from_host();
00114    otg_device_sessions = 0;
00115 #endif
00116 }

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 131 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.

Referenced by usb_device_task(), and usb_general_interrupt().

00132 {
00133    Pll_start_auto();
00134    Wait_pll_ready();
00135    Usb_unfreeze_clock();
00136    Usb_enable_suspend_interrupt();
00137    Usb_enable_reset_interrupt();
00138    usb_init_device();         // configure the USB controller EP0
00139    Usb_attach();
00140 #if (USB_OTG_FEATURE == ENABLED)
00141    Usb_enable_id_interrupt();
00142 #endif
00143 }

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 155 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, Clear_otg_features_from_host, 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_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_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_start_device(), Usb_unfreeze_clock, Usb_vbus_on_action, and Wait_pll_ready.

Referenced by usb_host_task(), and usb_task().

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

Here is the call graph for this function:


Variable Documentation

U8 otg_device_sessions

OTG B-Device SRP protocole specific states or events.

Definition at line 69 of file usb_device_task.c.

Referenced by usb_device_task_init().

U8 otg_b_device_state

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

Definition at line 65 of file usb_device_task.c.

Referenced by usb_device_task(), usb_general_interrupt(), usb_host_task(), and usb_task_init().

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 82 of file usb_device_task.c.

Referenced by usb_device_task(), and usb_general_interrupt().

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 77 of file usb_device_task.c.

Referenced by otg_timer_interrupt().


Generated on Mon Feb 19 09:32:13 2007 for Atmel by  doxygen 1.5.1-p1