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    Usb_unfreeze_clock();
00170    usb_init_device();         // configure the USB controller EP0
00171    Usb_attach();
00172    Usb_enable_suspend_interrupt();
00173    Usb_enable_reset_interrupt();
00174 #if (USB_OTG_FEATURE == ENABLED)
00175    Usb_enable_id_interrupt();
00176 #endif
00177 }

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

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

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 Mon Nov 3 10:08:27 2008 for ATMEL by  doxygen 1.5.3