ATMEL: USB device task

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

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

Referenced by usb_device_task().

#define B_SRP_INIT 1

Definition at line 71 of file usb_device_task.h.

Referenced by usb_device_task().

#define B_PERIPHERAL 2

Definition at line 72 of file usb_device_task.h.

Referenced by usb_device_task().

#define B_WAIT_ACON 3

Definition at line 73 of file usb_device_task.h.

#define B_HOST 4

Definition at line 74 of file usb_device_task.h.

Referenced by usb_device_task().

#define B_END_HNP_SUSPEND 5

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

Referenced by usb_device_task().

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

Definition at line 86 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 87 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 90 of file usb_device_task.h.

Referenced by usb_device_task().

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

Definition at line 91 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 92 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 96 of file usb_device_task.h.

Referenced by usb_device_task().

#define Init_tb_srp_counter | ( |  | ) | (otg_tb_srp_cpt = 0)

Definition at line 98 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 99 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 138 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.

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

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

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

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

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

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 86 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 107 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 103 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 120 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 115 of file usb_device_task.c.