usb_task.c

Go to the documentation of this file.
00001 /*This file has been prepared for Doxygen automatic documentation generation.*/
00025 
00026 /* Copyright (c) 2007, Atmel Corporation All rights reserved.
00027  *
00028  * Redistribution and use in source and binary forms, with or without
00029  * modification, are permitted provided that the following conditions are met:
00030  *
00031  * 1. Redistributions of source code must retain the above copyright notice,
00032  * this list of conditions and the following disclaimer.
00033  *
00034  * 2. Redistributions in binary form must reproduce the above copyright notice,
00035  * this list of conditions and the following disclaimer in the documentation
00036  * and/or other materials provided with the distribution.
00037  *
00038  * 3. The name of ATMEL may not be used to endorse or promote products derived
00039  * from this software without specific prior written permission.
00040  *
00041  * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
00042  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00043  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
00044  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
00045  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00046  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00047  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00048  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00049  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00050  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00051  */
00052 
00053 //_____  I N C L U D E S ___________________________________________________
00054 
00055 #include "config.h"
00056 #include "conf_usb.h"
00057 #include "usb_task.h"
00058 #include "lib_mcu/usb/usb_drv.h"
00059 #if ((USB_DEVICE_FEATURE == ENABLED))
00060 #include "usb_descriptors.h"
00061 #endif
00062 #include "lib_mcu/power/power_drv.h"
00063 #include "lib_mcu/wdt/wdt_drv.h"
00064 #include "lib_mcu/pll/pll_drv.h"
00065 #include "modules/usb/device_chap9/usb_device_task.h"
00066 
00067 #ifndef  USE_USB_PADS_REGULATOR
00068    #error "USE_USB_PADS_REGULATOR" should be defined as ENABLE or DISABLE in conf_usb.h file
00069 #endif
00070 
00071 //_____ M A C R O S ________________________________________________________
00072 
00073 #ifndef LOG_STR_CODE
00074 #define LOG_STR_CODE(str)
00075 #else
00076 U8 code log_device_disconnect[]="Device Disconnected";
00077 U8 code log_id_change[]="Pin Id Change";
00078 #endif
00079 
00080 //_____ D E F I N I T I O N S ______________________________________________
00081 
00092 volatile U16 g_usb_event=0;
00093 
00094 
00095 #if (USB_DEVICE_FEATURE == ENABLED)
00102 extern bit   usb_connected;
00103 
00110 extern U8    usb_configuration_nb;
00111 
00116 extern U8 remote_wakeup_feature;
00117 
00118 volatile U16 delay_usb;
00119 void usb_delay_ms(U8 ms);
00120 #endif
00121 
00122 
00123 #if (USB_HOST_FEATURE == ENABLED)
00130 volatile U8 private_sof_counter=0;
00131 
00132    #if (USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE)
00133 extern volatile S_pipe_int   it_pipe_str[MAX_EP_NB];
00134    #endif
00135 
00136 #endif
00137 
00138 #if ((USB_DEVICE_FEATURE == ENABLED)&& (USB_HOST_FEATURE == ENABLED))
00144    U8 g_usb_mode=USB_MODE_UNDEFINED;
00145    U8 g_old_usb_mode;
00146 #endif
00147 
00148 //_____ D E C L A R A T I O N S ____________________________________________
00149 
00159 void usb_task_init(void)
00160 {
00161    #if (USE_USB_PADS_REGULATOR==ENABLE)  // Otherwise assume USB PADs regulator is not used
00162    Usb_enable_regulator();
00163    #endif
00164    usb_device_task_init();
00165 #if (USB_REMOTE_WAKEUP == ENABLED)
00166    usb_remote_wup_feature = DISABLED;
00167 #endif
00168 }
00169 
00179 void usb_task(void)
00180 {
00181    usb_device_task();
00182 }
00183 
00202 #ifdef __GNUC__
00203  ISR(USB_GEN_vect)
00204 #else
00205 #pragma vector = USB_General_vect
00206 __interrupt void usb_general_interrupt()
00207 #endif
00208 {
00209   // - Device start of frame received
00210    if (Is_usb_sof() && Is_sof_interrupt_enabled())
00211    {
00212       Usb_ack_sof();
00213       Usb_sof_action();
00214    }
00215   // - Device Suspend event (no more USB activity detected)
00216    if (Is_usb_suspend() && Is_suspend_interrupt_enabled())
00217    {
00218       usb_suspended=TRUE;
00219       Usb_ack_wake_up();                 // clear wake up to detect next event
00220       Usb_send_event(EVT_USB_SUSPEND);
00221       Usb_ack_suspend();
00222       Usb_enable_wake_up_interrupt();
00223       Usb_disable_resume_interrupt();
00224       Usb_freeze_clock();
00225       Stop_pll();
00226       Usb_suspend_action();
00227    }
00228   // - Wake up event (USB activity detected): Used to resume
00229    if (Is_usb_wake_up() && Is_wake_up_interrupt_enabled())
00230    {
00231       if(Is_pll_ready()==FALSE)
00232       {
00233          Pll_start_auto();
00234          Wait_pll_ready();
00235       }
00236       Usb_unfreeze_clock();
00237       Usb_ack_wake_up();
00238       if(usb_suspended)
00239       {
00240          Usb_enable_resume_interrupt();
00241          Usb_enable_reset_interrupt();
00242          while(Is_usb_wake_up())
00243          {
00244             Usb_ack_wake_up();
00245          }
00246          usb_delay_ms(2);
00247          if(Is_usb_sof() || Is_usb_resume() || Is_usb_reset() )
00248          {
00249             Usb_disable_wake_up_interrupt();
00250             Usb_wake_up_action();
00251             Usb_send_event(EVT_USB_WAKE_UP);
00252             Usb_enable_suspend_interrupt();
00253             Usb_enable_resume_interrupt();
00254             Usb_enable_reset_interrupt();
00255             
00256          }
00257          else // Workarround to make the USB enter power down mode again (spurious transcient detected on the USB lines)
00258          {
00259             if(Is_usb_wake_up()) return;
00260             Usb_drive_dp_low();
00261             Usb_direct_drive_usb_enable();
00262             Usb_direct_drive_disable();
00263             Usb_disable_wake_up_interrupt();
00264          }
00265       }
00266    }
00267   // - Resume state bus detection
00268    if (Is_usb_resume() && Is_resume_interrupt_enabled())
00269    {
00270       usb_suspended = FALSE;
00271       Usb_disable_wake_up_interrupt();
00272       Usb_ack_resume();
00273       Usb_disable_resume_interrupt();
00274       Usb_resume_action();
00275       Usb_send_event(EVT_USB_RESUME);
00276    }
00277   // - USB bus reset detection
00278    if (Is_usb_reset()&& Is_reset_interrupt_enabled())
00279    {
00280       #if (USB_REMOTE_WAKEUP == ENABLED)
00281       usb_remote_wup_feature = DISABLED;
00282       #endif
00283       Usb_ack_reset();
00284       usb_init_device();
00285       Usb_reset_action();
00286       Usb_send_event(EVT_USB_RESET);
00287    }
00288 
00289 }
00290 
00291 
00292 void usb_delay_ms(U8 ms)
00293 {
00294    for(;ms;ms--)
00295    {
00296       for(delay_usb=0;delay_usb<FOSC/16;delay_usb++);
00297    }
00298 }

Generated on Mon May 5 15:57:55 2008 for ATMEL by  doxygen 1.5.1-p1