usb_task.c

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 //_____  I N C L U D E S ___________________________________________________
00033 
00034 #include "config.h"
00035 #include "conf_usb.h"
00036 
00037 #if (USB_OTG_FEATURE == ENABLED)
00038   #include "lib_mcu/timer/timer16_drv.h"
00039   #if (TARGET_BOARD==SPIDER)
00040     #include "lib_board/lcd/lcd_drv.h"
00041   #endif
00042 #endif
00043 
00044 #include "usb_task.h"
00045 #include "lib_mcu/usb/usb_drv.h"
00046 #if ((USB_DEVICE_FEATURE == ENABLED))
00047 #include "usb_descriptors.h"
00048 #endif
00049 #include "lib_mcu/power/power_drv.h"
00050 #include "lib_mcu/wdt/wdt_drv.h"
00051 #include "lib_mcu/pll/pll_drv.h"
00052 
00053 #if ((USB_HOST_FEATURE == ENABLED))
00054    #include "modules/usb/host_chap9/usb_host_task.h"
00055    #if (USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE)
00056       extern U8 g_sav_int_sof_enable;
00057    #endif
00058 #endif
00059 
00060 #if ((USB_DEVICE_FEATURE == ENABLED))
00061    #include "modules/usb/device_chap9/usb_device_task.h"
00062 #endif
00063 
00064 
00065 #ifndef  USE_USB_PADS_REGULATOR
00066    #error "USE_USB_PADS_REGULATOR" should be defined as ENABLE or DISABLE in conf_usb.h file
00067 #endif
00068 
00069 //_____ M A C R O S ________________________________________________________
00070 
00071 #ifndef LOG_STR_CODE
00072 #define LOG_STR_CODE(str)
00073 #else
00074 U8 code log_device_disconnect[]="Device Disconnected";
00075 U8 code log_id_change[]="Pin Id Change";
00076 #endif
00077 
00078 //_____ D E F I N I T I O N S ______________________________________________
00079 
00090 volatile U16 g_usb_event=0;
00091 
00092 
00093 #if (USB_DEVICE_FEATURE == ENABLED)
00100 extern bit   usb_connected;
00101 
00108 extern U8    usb_configuration_nb;
00109 
00114 U8 remote_wakeup_feature;
00115 #endif
00116 
00117 
00118 #if (USB_HOST_FEATURE == ENABLED)
00125 volatile U8 private_sof_counter=0;
00126 
00127    #if (USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE)
00128 extern volatile S_pipe_int   it_pipe_str[MAX_EP_NB];
00129    #endif
00130 
00131 #endif
00132 
00133 #if ((USB_DEVICE_FEATURE == ENABLED)&& (USB_HOST_FEATURE == ENABLED))
00139    U8 g_usb_mode=USB_MODE_UNDEFINED;
00140    U8 g_old_usb_mode;
00141 #endif
00142 
00143 
00144 #if (USB_OTG_FEATURE == ENABLED)
00149   volatile U8 otg_features_supported;
00150   
00153   U8 otg_user_request;
00154      
00157   U16 otg_msg_event_delay;
00158   
00161   U16 otg_msg_failure_delay;
00162  
00165   volatile U16 g_otg_event;
00166    
00169   U8 otg_device_nb_hnp_retry;
00170 
00171   #if (OTG_VBUS_AUTO_AFTER_A_PLUG_INSERTION == ENABLED)
00172    U8 id_changed_to_host_event;
00173   #endif
00174   
00175   #if (OTG_COMPLIANCE_TRICKS == ENABLED)
00176    volatile U8 otg_last_sof_received;     // last SOF received in SOF interrupt
00177    volatile U8 otg_last_sof_stored;       // last SOF value stored in OTG Timer interrupt
00178    volatile U8 reset_received;            // indicates if a reset has been received from B-Host after a HNP (used in A-Periph mode)
00179   #endif
00180   
00186   #ifndef Is_vbus_overcurrent()
00187     #warning  Is_vbus_overcurrent() must be defined if present on board
00188     #define   Is_vbus_overcurrent()       (FALSE)
00189   #endif
00190    
00193   void Otg_message_task_init(void);
00194   void Otg_message_task(void);
00195 #endif
00196 
00197 
00198 
00199 //_____ D E C L A R A T I O N S ____________________________________________
00200 
00211 void usb_task_init(void)
00212 {
00213    #if (USB_HOST_FEATURE == ENABLED && USB_DEVICE_FEATURE == ENABLED)
00214    U8 delay;
00215    #endif
00216 
00217    #if (USE_USB_PADS_REGULATOR==ENABLE)  // Otherwise assume USB PADs regulator is not used
00218    Usb_enable_regulator();
00219    #endif
00220 
00221 // ---- DUAL ROLE DEVICE USB MODE ---------------------------------------------
00222 #if (USB_OTG_FEATURE == ENABLED)
00223    Otg_message_task_init();       // OTG program needs to display event messages to the user
00224    Otg_timer_init();              // OTG program requires TIMER1 or 3 to handle several OTG specific timings
00225    otg_b_device_state = B_IDLE;   // init state machines variables
00226    device_state = DEVICE_UNATTACHED;
00227    otg_device_nb_hnp_retry = BDEV_HNP_NB_RETRY;
00228    Clear_all_user_request();
00229 #endif
00230 
00231 #if (((USB_DEVICE_FEATURE == ENABLED) && (USB_HOST_FEATURE == ENABLED)) || (USB_OTG_FEATURE == ENABLED))
00232    Usb_enable_uid_pin();
00233    delay=PORTA;
00234    g_usb_mode=USB_MODE_UNDEFINED;
00235    if(Is_usb_id_device())
00236    {
00237      g_usb_mode=USB_MODE_DEVICE;
00238      usb_device_task_init();
00239      #if ((OTG_VBUS_AUTO_AFTER_A_PLUG_INSERTION == ENABLED) && (USB_OTG_FEATURE == ENABLED))
00240      id_changed_to_host_event = DISABLED;
00241      #endif
00242    }
00243    else
00244    {
00245      Usb_send_event(EVT_USB_HOST_FUNCTION);
00246      g_usb_mode=USB_MODE_HOST;
00247      Usb_ack_id_transition(); // REQUIRED !!! Startup with ID=0, Ack ID pin transistion (default hwd start up is device mode)
00248      Usb_enable_id_interrupt();
00249      Enable_interrupt();
00250      usb_host_task_init();
00251      #if ((OTG_VBUS_AUTO_AFTER_A_PLUG_INSERTION == ENABLED) && (USB_OTG_FEATURE == ENABLED))
00252      id_changed_to_host_event = ENABLED;
00253      #endif
00254    }
00255    g_old_usb_mode = g_usb_mode;   // Store current usb mode, for mode change detection
00256 // -----------------------------------------------------------------------------
00257 
00258 // ---- DEVICE ONLY USB MODE ---------------------------------------------------
00259 #elif ((USB_DEVICE_FEATURE == ENABLED)&& (USB_HOST_FEATURE == DISABLE))
00260    Usb_force_device_mode();
00261    usb_device_task_init();
00262 // -----------------------------------------------------------------------------
00263 
00264 // ---- REDUCED HOST ONLY USB MODE ---------------------------------------------
00265 #elif ((USB_DEVICE_FEATURE == DISABLE)&& (USB_HOST_FEATURE == ENABLED))
00266    Usb_force_host_mode();
00267    usb_host_task_init();
00268 #elif ((USB_DEVICE_FEATURE == DISABLE)&& (USB_HOST_FEATURE == DISABLE))
00269    #error  at least one of USB_DEVICE_FEATURE or USB_HOST_FEATURE should be enabled
00270 #endif
00271 // -----------------------------------------------------------------------------
00272 
00273 }
00274 
00285 void usb_task(void)
00286 {
00287 
00288 // ---- OTG DEVICE ------------------------------------------------------------
00289 //#if (USB_OTG_FEATURE == ENABLED)
00290 
00291 
00292 
00293 
00294 // ---- DUAL ROLE DEVICE USB MODE ---------------------------------------------
00295 #if ((USB_DEVICE_FEATURE == ENABLED) && (USB_HOST_FEATURE == ENABLED))
00296    if(Is_usb_id_device())
00297    { g_usb_mode=USB_MODE_DEVICE;}
00298    else
00299    { g_usb_mode=USB_MODE_HOST;}
00300   // TODO !!! ID pin hot state change
00301   // Preliminary management: HARDWARE RESET !!!
00302    #if ( ID_PIN_CHANGE_GENERATE_RESET == ENABLE)
00303      // Hot ID transition generates wdt reset
00304       if((g_old_usb_mode!=g_usb_mode))
00305       #ifndef  AVRGCC
00306          {Wdt_change_16ms(); while(1);   LOG_STR_CODE(log_id_change);}
00307       #else
00308          {Wdt_change_enable(); while(1); LOG_STR_CODE(log_id_change);}
00309       #endif
00310 
00311    #endif
00312   g_old_usb_mode=g_usb_mode;   // Store current usb mode, for mode change detection
00313   // Depending on current usb mode, launch the correct usb task (device or host)
00314   
00315    #if (USB_OTG_FEATURE == ENABLED)  
00316      // Configure OTG timers
00317      Set_otg_custom_timer(VBUSRISE_70MS);
00318      Set_otg_custom_timer(VBUSPULSE_40MS);
00319      Set_otg_custom_timer(VFALLTMOUT_131MS);
00320      Set_otg_custom_timer(SRPMINDET_100US);
00321    #endif
00322    
00323    switch(g_usb_mode)
00324    {
00325       case USB_MODE_DEVICE:
00326          usb_device_task();
00327          break;
00328 
00329       case USB_MODE_HOST:
00330          #if (OTG_ADEV_SRP_REACTION == VBUS_PULSE)
00331            Usb_select_vbus_srp_method();
00332          #else
00333            Usb_select_data_srp_method();
00334          #endif
00335          usb_host_task();
00336          // Handle Vbus overcurrent error (auto-disabled if not supported or not defined in board driver file)
00337          #if (USB_OTG_FEATURE == ENABLED)
00338          if (Is_vbus_overcurrent())
00339          {
00340            Otg_print_new_event_message(OTGMSG_VBUS_SURCHARGE,OTG_TEMPO_3SEC);
00341          }
00342          #endif
00343          break;
00344 
00345       case USB_MODE_UNDEFINED:  // No break !
00346       default:
00347          break;
00348    }
00349 // -----------------------------------------------------------------------------
00350 
00351 // ---- DEVICE ONLY USB MODE ---------------------------------------------------
00352 #elif ((USB_DEVICE_FEATURE == ENABLED)&& (USB_HOST_FEATURE == DISABLE))
00353    usb_device_task();
00354 // -----------------------------------------------------------------------------
00355 
00356 // ---- REDUCED HOST ONLY USB MODE ---------------------------------------------
00357 #elif ((USB_DEVICE_FEATURE == DISABLE)&& (USB_HOST_FEATURE == ENABLED))
00358    usb_host_task();
00359 // -----------------------------------------------------------------------------
00360 
00362 #elif ((USB_DEVICE_FEATURE == DISABLE)&& (USB_HOST_FEATURE == DISABLE))
00363    #error  at least one of USB_DEVICE_FEATURE or USB_HOST_FEATURE should be enabled
00364    #error  otherwise the usb task has nothing to do ...
00365 #endif
00366 // -----------------------------------------------------------------------------
00367 
00368 #if (USB_OTG_FEATURE == ENABLED)
00369    Otg_message_task();
00370 #endif
00371 }
00372 
00373 
00374 
00407 #ifdef AVRGCC
00408  ISR(USB_GEN_vect)
00409 #else
00410 #pragma vector = USB_GENERAL_vect
00411 __interrupt void usb_general_interrupt()
00412 #endif
00413 {
00414    #if (USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE)
00415    U8 i;
00416    U8 save_pipe_nb;
00417    #endif
00418 
00419 // ---------- DEVICE events management -----------------------------------
00420 // -----------------------------------------------------------------------
00421 #if ((USB_DEVICE_FEATURE == ENABLED) || (USB_OTG_FEATURE == ENABLED))
00422   //- VBUS state detection
00423    if (Is_usb_vbus_transition() && Is_usb_vbus_interrupt_enabled() && Is_usb_id_device())
00424    {
00425       Usb_ack_vbus_transition();
00426       if (Is_usb_vbus_high())
00427       {
00428          usb_connected = TRUE;
00429          Usb_vbus_on_action();
00430          Usb_send_event(EVT_USB_POWERED);
00431           Usb_enable_reset_interrupt();
00432          usb_start_device();
00433           Usb_attach();
00434       }
00435       else
00436       {
00437          Usb_detach();
00438          #if  (USB_OTG_FEATURE == ENABLED)
00439           Usb_device_stop_hnp();
00440           Usb_select_device();
00441           Clear_all_user_request();
00442          #endif
00443          Usb_vbus_off_action();
00444          usb_connected = FALSE;
00445          usb_configuration_nb = 0;
00446          Usb_send_event(EVT_USB_UNPOWERED);
00447       }
00448    }
00449   // - Device start of frame received
00450    if (Is_usb_sof() && Is_sof_interrupt_enabled())
00451    {
00452       Usb_ack_sof();
00453       Usb_sof_action();
00454       #if (USB_OTG_FEATURE == ENABLED)
00455       sof_seen_in_session = TRUE;
00456         #if (OTG_COMPLIANCE_TRICKS == ENABLED)
00457         otg_last_sof_received = UDFNUML;  // store last frame number received
00458         #endif
00459       #endif
00460    }
00461   // - Device Suspend event (no more USB activity detected)
00462    if (Is_usb_suspend() && Is_suspend_interrupt_enabled())
00463    {
00464       #if (USB_OTG_FEATURE == ENABLED)
00465       // 1st : B-PERIPH mode ?
00466       if (Is_usb_id_device())
00467       {
00468         // HNP Handler
00469         if (Is_host_requested_hnp() // "b_hnp_enable" feature received
00470             && (Is_session_started_with_srp() || Is_user_requested_hnp() || (OTG_B_DEVICE_AUTORUN_HNP_IF_REQUIRED == ENABLED)))
00471         {
00472           if (otg_device_nb_hnp_retry == 0)
00473           {
00474             otg_features_supported &= ~OTG_B_HNP_ENABLE;
00475           }
00476           else
00477           {
00478             Ack_user_request_hnp();
00479             Usb_ack_hnp_error_interrupt();
00480             Usb_ack_role_exchange_interrupt();
00481             Usb_enable_role_exchange_interrupt();
00482             Usb_enable_hnp_error_interrupt();
00483             Usb_device_initiate_hnp();
00484             otg_device_nb_hnp_retry--;
00485           }
00486           Usb_ack_suspend();
00487         }
00488         else
00489         {
00490           // Remote wake-up handler
00491           if ((remote_wakeup_feature == ENABLED) && (usb_configuration_nb != 0))
00492           {
00493             
00494             Usb_disable_suspend_interrupt();
00495             Usb_ack_wake_up();
00496             Usb_enable_wake_up_interrupt();
00497             // After that user can execute "Usb_initiate_remote_wake_up()" to initiate a remote wake-up
00498             // Note that the suspend interrupt flag SUSPI must still be set to enable upstream resume
00499             // So the SUSPE enable bit must be cleared to avoid redundant interrupt
00500             // ****************
00501             // Please note also that is Vbus is lost during an upstream resume (Host disconnection),
00502             // the RMWKUP bit (used to initiate remote wake up and that is normally cleared by hardware when sent)
00503             // remains set after the event, so that a good way to handle this feature is :
00504             //            Usb_initiate_remote_wake_up();
00505             //            while (Is_usb_pending_remote_wake_up())
00506             //            {
00507             //              if (Is_usb_vbus_low())
00508             //              {
00509             //                // Emergency action (reset macro, etc.) if Vbus lost during resuming
00510             //                break;
00511             //              }
00512             //            }
00513             //            Usb_ack_remote_wake_up_start();
00514             // ****************
00515           }
00516           else
00517           {
00518             // No remote wake-up supported
00519             Usb_send_event(EVT_USB_SUSPEND);
00520             Usb_suspend_action();
00521             Usb_ack_suspend();
00522             Usb_ack_wake_up();                  // clear wake up to detect next event
00523             Usb_enable_wake_up_interrupt();
00524             Usb_freeze_clock();
00525           }
00526         }
00527       }
00528       else
00529       {
00530         // A-PERIPH mode (will cause a session end, handled in usb_host_task.c)
00531         Usb_send_event(EVT_USB_SUSPEND);
00532         Usb_suspend_action();
00533         Usb_ack_suspend();
00534       }
00535       #else
00536       // Remote wake-up handler
00537       if ((remote_wakeup_feature == ENABLED) && (usb_configuration_nb != 0))
00538       {
00539         Usb_disable_suspend_interrupt();
00540         Usb_ack_wake_up();
00541         Usb_enable_wake_up_interrupt();
00542         Usb_suspend_action();
00543         Usb_freeze_clock();
00544         // After that user can execute "Usb_initiate_remote_wake_up()" to initiate a remote wake-up
00545         // Note that the suspend interrupt flag SUSPI must still be set to enable upstream resume
00546         // So the SUSPE enable bit must be cleared to avoid redundant interrupt
00547         // ****************
00548         // Please note also that is Vbus is lost during an upstream resume (Host disconnection),
00549         // the RMWKUP bit (used to initiate remote wake up and that is normally cleared by hardware when sent)
00550         // remains set after the event, so that a good way to handle this feature is :
00551         //            Usb_unfreeze_clock();
00552         //            Usb_initiate_remote_wake_up();
00553         //            while (Is_usb_pending_remote_wake_up())
00554         //            {
00555         //              if (Is_usb_vbus_low())
00556         //              {
00557         //                // Emergency action (reset macro, etc.) if Vbus lost during resuming
00558         //                break;
00559         //              }
00560         //            }
00561         //            Usb_ack_remote_wake_up_start();
00562         // ****************
00563       }
00564       else
00565       {
00566         // No remote wake-up supported
00567         Usb_send_event(EVT_USB_SUSPEND);
00568         Usb_suspend_action();
00569         Usb_ack_suspend();
00570         Usb_ack_wake_up();                  // clear wake up to detect next event
00571         Usb_enable_wake_up_interrupt();
00572         Usb_freeze_clock();
00573       }
00574       #endif
00575     }
00576    
00577   // - Wake up event (USB activity detected): Used to resume
00578    if (Is_usb_wake_up() && Is_swake_up_interrupt_enabled())
00579    {
00580       Usb_unfreeze_clock();
00581       Usb_ack_wake_up();
00582       Usb_disable_wake_up_interrupt();
00583       Usb_ack_suspend();
00584       Usb_enable_suspend_interrupt();
00585       Usb_wake_up_action();
00586       Usb_send_event(EVT_USB_WAKE_UP);
00587    }
00588   // - Resume state bus detection
00589    if (Is_usb_resume() && Is_resume_interrupt_enabled())
00590    {
00591       Usb_disable_wake_up_interrupt();
00592       Usb_ack_resume();
00593       Usb_disable_resume_interrupt();
00594       Usb_resume_action();
00595       Usb_send_event(EVT_USB_RESUME);
00596    }
00597   // - USB bus reset detection
00598    if (Is_usb_reset()&& Is_reset_interrupt_enabled())
00599    {
00600       Usb_ack_reset();
00601       usb_init_device();
00602       #if (USB_OTG_FEATURE == ENABLED)
00603         if (Is_usb_id_host())
00604         {
00605           usb_configure_endpoint(EP_CONTROL,      \
00606                                  TYPE_CONTROL,    \
00607                                  DIRECTION_OUT,   \
00608                                  SIZE_8,          \
00609                                  ONE_BANK,        \
00610                                  NYET_DISABLED);
00611         }
00612         #if (OTG_COMPLIANCE_TRICKS == ENABLED)
00613           // First initialization is important to be synchronized
00614           // A reset must first have been received
00615           if (device_state == A_PERIPHERAL)
00616           {
00617             otg_last_sof_received = UDFNUML;
00618             otg_last_sof_stored   = UDFNUML;
00619             Usb_ack_sof();
00620             Usb_enable_sof_interrupt();
00621             reset_received = TRUE;
00622             Timer16_select(OTG_USE_TIMER);    // reinitialize timer
00623             Timer16_set_counter(0);
00624           }
00625         #endif
00626       #endif
00627       Usb_reset_action();
00628       Usb_send_event(EVT_USB_RESET);
00629    }
00630 
00631 // ---------- OTG events management ------------------------------------
00632 // ---------------------------------------------------------------------
00633 #if (USB_OTG_FEATURE == ENABLED)
00634   // - OTG HNP Success detection
00635    if (Is_usb_role_exchange_interrupt() && Is_role_exchange_interrupt_enabled())
00636    {
00637      Usb_ack_role_exchange_interrupt();
00638      Host_ack_device_connection();
00639      Host_ack_device_disconnection();
00640      Otg_send_event(EVT_OTG_HNP_SUCCESS);
00641      End_session_with_srp();
00642      Clear_otg_features_from_host();
00643      if (Is_usb_id_host())
00644      {
00645        // HOST (A- or B-) mode
00646        if ((device_state != A_PERIPHERAL) && (device_state != A_END_HNP_WAIT_VFALL))
00647        {
00648          // Current mode is A-HOST, device will take the A-PERIPHERAL role
00649          otg_b_device_state = B_PERIPHERAL;
00650          device_state = A_PERIPHERAL;
00651          usb_connected = FALSE;
00652          usb_configuration_nb = 0;
00653          Usb_select_device();
00654          Usb_attach();
00655          Usb_unfreeze_clock();
00656          Usb_disable_role_exchange_interrupt();
00657          Usb_disable_hnp_error_interrupt();
00658          Usb_device_stop_hnp();
00659          Usb_ack_suspend();
00660          Usb_ack_reset();
00661          #if (OTG_COMPLIANCE_TRICKS == ENABLED)
00662            Timer16_select(OTG_USE_TIMER);     // 4 first lines compensating test error TD4.5-2,9ms
00663            Timer16_set_counter(0);
00664            Usb_freeze_clock();                // USB clock can be freezed to slow down events and condition detection
00665            while (Timer16_get_counter_low() != 20);
00666            Usb_unfreeze_clock();
00667            reset_received = FALSE;
00668            Usb_disable_sof_interrupt();       // will be set in the next OTG Timer IT (mandatory)
00669          #endif
00670          Usb_enable_suspend_interrupt();
00671          Usb_enable_reset_interrupt();
00672          usb_configure_endpoint(EP_CONTROL,    \
00673                                 TYPE_CONTROL,  \
00674                                 DIRECTION_OUT, \
00675                                 SIZE_8,        \
00676                                 ONE_BANK,      \
00677                                 NYET_DISABLED);
00678        }
00679      }
00680      else
00681      {  // In B_HOST mode, the HNPREQ bit must not be cleared because it releases the bus in suspend mode (and sof can't start)
00682        if ((otg_b_device_state != B_HOST) && (otg_b_device_state != B_END_HNP_SUSPEND))
00683        {
00684          // Current mode is B-PERIPHERAL, device will go into B-HOST role
00685          End_session_with_srp();
00686          Clear_otg_features_from_host();
00687          otg_b_device_state = B_HOST;
00688          device_state = DEVICE_ATTACHED;
00689          usb_connected = FALSE;
00690          usb_configuration_nb = 0;
00691          Usb_select_host();
00692          Host_enable_sof();     // start Host (sof)
00693          Host_send_reset();     // send the first RESET
00694          Host_disable_reset_interrupt();
00695          while (Host_is_reset());
00696          i = 2;
00697          while (i != 1)
00698          {
00699            i++;
00700          };
00701          while (i < 5)  // generates 4 others RESET
00702          {
00703            while (Host_is_reset());
00704            Host_send_reset();
00705            Host_ack_reset();
00706            i++;
00707          }
00708 
00709          Usb_disable_role_exchange_interrupt();
00710          Usb_disable_hnp_error_interrupt();
00711          Clear_all_user_request();
00712        }
00713      }
00714    }
00715   // - OTG HNP Failure detection
00716    if (Is_usb_hnp() && Is_usb_hnp_error_interrupt()&& Is_hnp_error_interrupt_enabled())
00717    {
00718      Usb_device_stop_hnp();
00719      Usb_disable_role_exchange_interrupt();
00720      Usb_disable_hnp_error_interrupt();
00721      Usb_ack_hnp_error_interrupt();
00722      if (Is_usb_id_device())
00723      {
00724        Otg_send_event(EVT_OTG_HNP_ERROR);
00725        Clear_all_user_request();
00726      }
00727    }
00728 #endif
00729 #endif// End DEVICE FEATURE MODE
00730 
00731 // ---------- HOST events management -----------------------------------
00732 // ---------------------------------------------------------------------
00733 #if ((USB_HOST_FEATURE == ENABLED && USB_DEVICE_FEATURE == ENABLED) || (USB_OTG_FEATURE == ENABLED))
00734   // - ID pin change detection
00735    if(Is_usb_id_transition()&&Is_usb_id_interrupt_enabled())
00736    {
00737       Usb_device_stop_hnp();
00738       #if (USB_OTG_FEATURE == ENABLED)
00739       Clear_all_user_request();
00740       #endif
00741       if(Is_usb_id_device())
00742       { g_usb_mode=USB_MODE_DEVICE;}
00743       else
00744       { g_usb_mode=USB_MODE_HOST;}
00745       Usb_ack_id_transition();
00746       if( g_usb_mode != g_old_usb_mode) // Basic Debounce
00747       {
00748          if(Is_usb_id_device()) // Going into device mode
00749          {
00750             Usb_send_event(EVT_USB_DEVICE_FUNCTION);
00751             #if (USB_OTG_FEATURE == ENABLED)
00752               otg_b_device_state = B_IDLE;
00753             #endif
00754             device_state = DEVICE_UNATTACHED;
00755             #if ((OTG_VBUS_AUTO_AFTER_A_PLUG_INSERTION == ENABLED) && (USB_OTG_FEATURE == ENABLED))
00756               id_changed_to_host_event = DISABLED;
00757             #endif
00758          }
00759          else                   // Going into host mode
00760          {
00761            #if (USB_OTG_FEATURE == ENABLED)
00762              otg_b_device_state = B_IDLE;
00763            #endif
00764            device_state = DEVICE_UNATTACHED;
00765            Usb_send_event(EVT_USB_HOST_FUNCTION);
00766            #if ((OTG_VBUS_AUTO_AFTER_A_PLUG_INSERTION == ENABLED) && (USB_OTG_FEATURE == ENABLED))
00767            id_changed_to_host_event = ENABLED;
00768            #endif
00769           }
00770          Usb_id_transition_action();
00771          LOG_STR_CODE(log_id_change);
00772          #if ( ID_PIN_CHANGE_GENERATE_RESET == ENABLE)
00773         // Hot ID transition generates wdt reset
00774             #ifndef  AVRGCC
00775                Wdt_change_16ms(); while(1);
00776             #else
00777                Wdt_change_enable(); while(1);
00778             #endif
00779          #endif
00780       }
00781    }
00782 #endif
00783 #if ((USB_HOST_FEATURE == ENABLED) || (USB_OTG_FEATURE == ENABLED))
00784   // - The device has been disconnected
00785    if(Is_device_disconnection() && Is_host_device_disconnection_interrupt_enabled())
00786    {
00787       host_disable_all_pipe();
00788       Host_ack_device_disconnection();
00789       device_state=DEVICE_DISCONNECTED;
00790       Usb_send_event(EVT_HOST_DISCONNECTION);
00791       LOG_STR_CODE(log_device_disconnect);
00792       Host_device_disconnection_action();
00793       #if (USB_OTG_FEATURE == ENABLED)
00794        Clear_all_user_request();
00795       #endif
00796    }
00797   // - Device connection
00798    if(Is_device_connection() && Is_host_device_connection_interrupt_enabled())
00799    {
00800       Host_ack_device_connection();
00801       host_disable_all_pipe();
00802       Host_device_connection_action();
00803    }
00804   // - Host Start of frame has been sent
00805    if (Is_host_sof() && Is_host_sof_interrupt_enabled())
00806    {
00807       Host_ack_sof();
00808       Usb_send_event(EVT_HOST_SOF);
00809       private_sof_counter++;
00810 
00811       // delay timeout management for interrupt tranfer mode in host mode
00812       #if ((USB_HOST_PIPE_INTERRUPT_TRANSFER==ENABLE) && (TIMEOUT_DELAY_ENABLE==ENABLE))
00813       if (private_sof_counter>=250)   // Count 1/4 sec
00814       {
00815          private_sof_counter=0;
00816          for(i=0;i<MAX_EP_NB;i++)
00817          {
00818             if(it_pipe_str[i].enable==ENABLE)
00819             {
00820                save_pipe_nb=Host_get_selected_pipe();
00821                Host_select_pipe(i);
00822                if((++it_pipe_str[i].timeout>TIMEOUT_DELAY) && (Host_get_pipe_type()!=TYPE_INTERRUPT))
00823                {
00824                   it_pipe_str[i].enable=DISABLE;
00825                   it_pipe_str[i].status=PIPE_DELAY_TIMEOUT;
00826                   Host_stop_pipe_interrupt(i);
00827                   if (is_any_interrupt_pipe_active()==FALSE)    // If no more transfer is armed
00828                   {
00829                      if (g_sav_int_sof_enable==FALSE)
00830                      {
00831                         Host_disable_sof_interrupt();
00832                      }
00833                   }
00834                   it_pipe_str[i].handle(PIPE_DELAY_TIMEOUT,it_pipe_str[i].nb_byte_processed);
00835                }
00836                Host_select_pipe(save_pipe_nb);
00837             }
00838          }
00839       }
00840       #endif  // (USB_HOST_PIPE_INTERRUPT_TRANSFER==ENABLE) && (TIMEOUT_DELAY_ENABLE==ENABLE))
00841       Host_sof_action();
00842    }
00843   // - Host Wake-up has been received
00844    if (Is_host_hwup() && Is_host_hwup_interrupt_enabled())
00845    {
00846       Host_disable_hwup_interrupt();  // Wake up interrupt should be disable host is now wake up !
00847       Host_disable_remote_wakeup_interrupt();
00848       // CAUTION HWUP can be cleared only when USB clock is active (not frozen)!
00849       Pll_start_auto();               // First Restart the PLL for USB operation
00850       Wait_pll_ready();               // Get sure pll is lock
00851       Usb_unfreeze_clock();           // Enable clock on USB interface
00852       Host_enable_sof();              // start sending SOF
00853       Host_ack_hwup();                // Clear HWUP interrupt flag
00854       Host_ack_remote_wakeup();
00855       Usb_send_event(EVT_HOST_HWUP);  // Send software event
00856       Usb_send_event(EVT_HOST_REMOTE_WAKEUP);
00857       Host_hwup_action();             // Map custom action
00858       #if (USB_OTG_FEATURE == ENABLED)
00859         if (Is_usb_hnp())
00860         {
00861 //          Usb_host_reject_hnp();
00862           Usb_disable_hnp_error_interrupt();
00863           Usb_disable_role_exchange_interrupt();
00864         }
00865       #endif
00866      Host_send_resume();
00867    }
00868 
00869    // Remote Wake Up has been received
00870    if (Is_host_remote_wakeup_interrupt_enabled() && Is_host_remote_wakeup())
00871    {
00872      Host_disable_remote_wakeup_interrupt();
00873      Host_disable_hwup_interrupt();
00874      Host_ack_remote_wakeup();
00875      Host_ack_hwup();                // Clear HWUP interrupt flag
00876      Pll_start_auto();               // First Restart the PLL for USB operation
00877      Wait_pll_ready();               // Get sure pll is lock
00878      Usb_unfreeze_clock();           // Enable clock on USB interface
00879      Host_enable_sof();     // start sending SOF
00880      Usb_send_event(EVT_HOST_REMOTE_WAKEUP);
00881      Usb_send_event(EVT_HOST_HWUP);  // Send software event
00882      #if (USB_OTG_FEATURE == ENABLED)
00883        if (Is_usb_hnp())
00884        {
00885 //         Usb_host_reject_hnp();
00886          Usb_disable_hnp_error_interrupt();
00887          Usb_disable_role_exchange_interrupt();
00888        }
00889      #endif
00890      Host_send_resume();
00891    }
00892 #endif // End HOST FEATURE MODE
00893 }
00894 
00895 
00896 #if (USB_OTG_FEATURE == ENABLED)
00897 
00898 // ---------- OTG Timings management -----------------------------------
00899 // ---------------------------------------------------------------------
00908 #ifdef AVRGCC
00909   #if (OTG_USE_TIMER == TIMER16_1)
00910     ISR(TIMER1_COMPA_vect)
00911   #else
00912     ISR(TIMER3_COMPA_vect)
00913   #endif
00914 #else
00915   #if (OTG_USE_TIMER == TIMER16_1)
00916     #pragma vector = TIMER1_COMPA_vect
00917   #else
00918     #pragma vector = TIMER3_COMPA_vect
00919   #endif
00920 __interrupt void otg_timer_interrupt()
00921 #endif
00922 {
00923 #if (USE_TIMER16 == BOTH_TIMER16)
00924   U8 tmr_sel_save = timer16_selected;     // save timer currently selected
00925 #endif
00926   
00927   Timer16_select(OTG_USE_TIMER);
00928   Timer16_clear_compare_a_it();
00929 
00931 #if ((OTG_MESSAGING_OUTPUT == OTGMSG_ALL) || (OTG_MESSAGING_OUTPUT == OTGMSG_FAIL))
00932   if ((Get_failure_msg_delay() != 0x0000) && (Get_failure_msg_delay() != 0xFFFF))   { Decrement_failure_msg_delay(); }
00933   #if (OTG_MESSAGING_OUTPUT == OTGMSG_ALL)
00934     if ((Get_event_msg_delay() != 0x0000) && (Get_event_msg_delay() != 0xFFFF))       { Decrement_event_msg_delay(); }
00935   #endif
00936 #endif
00937 
00939   if (Is_srp_sent_and_waiting_answer())         { otg_tb_srp_cpt++; }
00940 
00942   if (Is_srp_received_and_waiting_connect())    { otg_ta_srp_wait_connect++; }
00943 
00945   if ((device_state == A_SUSPEND) && (otg_ta_aidl_bdis_tmr > 1))
00946                                                 { otg_ta_aidl_bdis_tmr--; }
00947 
00949   if ((device_state == DEVICE_DEFAULT) && (!Is_timeout_bdev_response_overflow()))
00950                                                 { otg_timeout_bdev_respond--; }
00951 
00953   if (!Is_ta_vbus_rise_counter_overflow())      { otg_ta_vbus_rise--; }
00954   
00956   if (!Is_ta_vbus_fall_counter_overflow())      { otg_end_hnp_vbus_delay--; }
00957   
00958   
00959   
00961   #if (OTG_COMPLIANCE_TRICKS == ENABLED)
00962  
00963   if (device_state == A_PERIPHERAL)
00964   {
00965     if (Is_sof_interrupt_enabled() && (reset_received == TRUE))
00966     {
00967       if (otg_last_sof_stored != otg_last_sof_received)
00968       {
00969         // No SOF is missing
00970         otg_last_sof_received = otg_last_sof_stored;
00971       }
00972       else
00973       {
00974         // SOF seems to be missing..
00975         Usb_freeze_clock();
00976         Usb_disable_sof_interrupt();
00977         reset_received = FALSE;
00978         while (Timer16_get_counter_low() != 20);  // overflow set to 62 in usb_task.h
00979         Usb_unfreeze_clock();
00980       }
00981       otg_last_sof_received = UDFNUML;
00982       otg_last_sof_stored = UDFNUML;
00983     }
00984   }
00985   #endif
00986   
00987 #if (USE_TIMER16 == BOTH_TIMER16)
00988   Timer16_select(tmr_sel_save);        // restore timer that was selected before entering IT
00989 #endif
00990 }
00991 
00992 
00993 // ---------- OTG Messaging management ---------------------------------
00994 // ---------------------------------------------------------------------
01003 void Otg_message_task_init(void)
01004 {
01005   Otg_messaging_init();
01006   otg_msg_event_delay = 0;
01007   otg_msg_failure_delay = 0;
01008 }
01009 
01010 
01020 void Otg_message_task(void)
01021 {
01022   // Check if an OTG message must be erased (if it was set up for a specified delay)
01023 #if ((OTG_MESSAGING_OUTPUT == OTGMSG_ALL) || (OTG_MESSAGING_OUTPUT == OTGMSG_FAIL))
01024   if (Get_failure_msg_delay() == 0)  { Otg_clear_failure_message(); }
01025   #if (OTG_MESSAGING_OUTPUT == OTGMSG_ALL)
01026     if (Get_event_msg_delay() == 0)    { Otg_clear_event_message();   }
01027   #endif
01028 #endif
01029 }
01030 #endif
01031 
01032 
01033 
01034 
01035 extern void suspend_action(void)
01036 {
01037    Enable_interrupt();
01038    Enter_power_down_mode();
01039 }
01040 
01041 extern void host_suspend_action(void)
01042 {
01043    //Enter_power_down_mode();  //For example...
01044 }
01045 
01046 void otg_not_supported_device(void)
01047 {
01048   Otg_send_event(EVT_OTG_DEV_UNSUPPORTED);
01049 }
01050 

Generated on Mon Feb 19 09:31:47 2007 for Atmel by  doxygen 1.5.1-p1