usb_device_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 //_____  I N C L U D E S ___________________________________________________
00023 
00024 #include "config.h"
00025 #include "conf_usb.h"
00026 #include "usb_device_task.h"
00027 #include "modules\usb\usb_task.h"
00028 #include "lib_mcu\usb\usb_drv.h"
00029 #include "usb_descriptors.h"
00030 #include "modules\usb\device_chap9\usb_standard_request.h"
00031 #include "lib_mcu\pll\pll_drv.h"
00032 
00033 //_____ M A C R O S ________________________________________________________
00034 
00035 //_____ D E F I N I T I O N S ______________________________________________
00036 
00042 bit   usb_connected=0;
00043 
00044 
00051 extern U8  usb_configuration_nb;
00052 
00053 //_____ D E C L A R A T I O N S ____________________________________________
00054 
00067 void usb_device_task_init(void)
00068 {
00069    Usb_disable();
00070    Usb_enable();
00071 #if (VBUS_SENSING_IO == ENABLED)
00072    Usb_vbus_sense_init();   // init. the I/O used for Vbus sensing
00073 #endif
00074 }
00075 
00089 void usb_start_device (void)
00090 {
00091    Usb_freeze_clock();
00092 #ifdef BYPASS_USB_AUTOBAUD
00093    Pll_start_auto();
00094    Wait_pll_ready();
00095    Usb_unfreeze_clock();
00096 #else
00097    Usb_enable_device();
00098    Usb_attach();
00099    usb_autobaud();
00100 #endif
00101 
00102    Usb_unfreeze_clock();
00103    Usb_attach();
00104 #if (USB_RESET_CPU == ENABLED)
00105    Usb_reset_all_system();
00106 #else
00107    Usb_reset_macro_only();
00108 #endif
00109    Usb_enable_suspend_interrupt();
00110    Usb_enable_reset_interrupt();
00111    Enable_interrupt();
00112    usb_init_device();         // configure the USB controller EP0
00113 }
00114 
00125 void usb_device_task(void)
00126 {
00127    if (usb_connected == FALSE)
00128    {
00129      #if (VBUS_SENSING_IO == ENABLED)
00130      if (Is_usb_vbus_on())    // check if Vbus ON to attach
00131      {
00132        Usb_enable();
00133        usb_connected = TRUE;
00134        usb_start_device();
00135        Usb_vbus_on_action();
00136      }
00137      #else
00138      usb_connected = TRUE;    // attach if application is not self-powered
00139      usb_start_device();
00140      Usb_vbus_on_action();
00141      #endif
00142    }
00143 
00144    #if (VBUS_SENSING_IO == ENABLED)
00145    if ((usb_connected == TRUE) && Is_usb_vbus_off())  // check if Vbus OFF to detach
00146    {
00147      Usb_detach();
00148      Usb_disable();
00149      Stop_pll();
00150      usb_connected = FALSE;
00151      usb_configuration_nb=0;
00152    }
00153    #endif
00154 
00155    if(Is_usb_event(EVT_USB_RESET))
00156    {
00157       Usb_ack_event(EVT_USB_RESET);
00158       Usb_reset_endpoint(0);
00159       usb_configuration_nb=0;
00160    }
00161 
00162    // Here connection to the device enumeration process
00163    Usb_select_endpoint(EP_CONTROL);
00164    if (Is_usb_receive_setup())
00165    {
00166       usb_process_request();
00167    }
00168 }
00169 
00170 #ifndef BYPASS_USB_AUTOBAUD
00183 void usb_autobaud(void)
00184 {
00185    U8 i=0;
00186    while(Is_usb_sof()==FALSE) 
00187    {
00188       Usb_freeze_clock();
00189       Stop_pll();
00190       TCCR0B=0x00; TCCR0A=0x00;    
00191       TCNT0=0;  TIFR0=0x01;        
00192       switch (i)
00193       {
00194          case 0:
00195             Start_pll(PLLx06);  
00196             break;
00197          case 1:
00198             Start_pll(PLLx03);  
00199             break;
00200 
00201          default:
00202             Start_pll(PLLx06);  
00203             i=0;
00204             break;
00205       }
00206       i++;
00207       TCCR0B|=(1<<CS02)|(1<<CS00);            
00208       while(!Is_pll_ready() &&(TIFR0!=0x01)); 
00209       TCCR0B=0x00;TCNT0=0;           
00210       if(TIFR0==0x01)                
00211       {
00212          TIFR0=0x01;                 
00213       }
00214       else                           
00215       {
00216          Usb_unfreeze_clock();
00217          TIFR0=0x01;                    
00218          TCCR0B|=(1<<CS02)|(1<<CS00);   
00219          while((TIFR0!=0x01));          
00220       }
00221    }
00223    TCCR0A=0;TCCR0B=0x00;TCNT0=0;
00224    Usb_ack_sof();
00225 }
00226 #endif

Generated on Fri Jun 15 14:15:33 2007 for Atmel by  doxygen 1.5.1-p1