• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

usb_device_task.c

Go to the documentation of this file.
00001 /*This file is prepared for Doxygen automatic documentation generation.*/
00018 
00019 /* Copyright (c) 2009 Atmel Corporation. All rights reserved.
00020  *
00021  * Redistribution and use in source and binary forms, with or without
00022  * modification, are permitted provided that the following conditions are met:
00023  *
00024  * 1. Redistributions of source code must retain the above copyright notice,
00025  * this list of conditions and the following disclaimer.
00026  *
00027  * 2. Redistributions in binary form must reproduce the above copyright notice,
00028  * this list of conditions and the following disclaimer in the documentation
00029  * and/or other materials provided with the distribution.
00030  *
00031  * 3. The name of Atmel may not be used to endorse or promote products derived
00032  * from this software without specific prior written permission.
00033  *
00034  * 4. This software may only be redistributed and used in connection with an Atmel
00035  * AVR product.
00036  *
00037  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
00038  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00039  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
00040  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
00041  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00042  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00043  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00044  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00045  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00046  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00047  */
00048 
00049 //_____  I N C L U D E S ___________________________________________________
00050 
00051 #include "config.h"
00052 #include "conf_usb.h"
00053 #include "usb_device_task.h"
00054 #include "modules/usb/usb_task.h"
00055 #include "lib_mcu/usb/usb_drv.h"
00056 #include "usb_descriptors.h"
00057 #include "modules/usb/device_chap9/usb_standard_request.h"
00058 #include "lib_mcu/pll/pll_drv.h"
00059 #ifdef USE_USB_AUTOBAUD
00060 #include "lib_mcu/wdt/wdt_drv.h"
00061 #endif
00062 
00063 //_____ M A C R O S ________________________________________________________
00064 
00065 //_____ D E F I N I T I O N S ______________________________________________
00066 
00072 bit   usb_connected=FALSE;
00073 
00079 bit   usb_suspended=FALSE;
00080 
00081 
00082 
00083 
00090 extern U8  usb_configuration_nb;
00091 
00092 //_____ D E C L A R A T I O N S ____________________________________________
00093 
00106 void usb_device_task_init(void)
00107 {
00108    Usb_disable();
00109    Usb_enable();
00110 #if (USB_LOW_SPEED_DEVICE==ENABLE)
00111    Usb_low_speed_mode();
00112 #endif
00113    Usb_enable_vbus_pad();
00114    Enable_interrupt();
00115 }
00116 
00130 void usb_start_device (void)
00131 {
00132    Usb_freeze_clock();
00133 #ifndef USE_USB_AUTOBAUD
00134    Pll_start_auto();
00135 #else
00136    usb_autobaud();
00137 #endif
00138    Wait_pll_ready();
00139    Disable_interrupt();
00140    Usb_unfreeze_clock();
00141    Usb_attach();
00142 #if (USB_RESET_CPU == ENABLED)
00143    Usb_reset_all_system();
00144 #else
00145    Usb_reset_macro_only();
00146 #endif
00147    usb_init_device();         // configure the USB controller EP0
00148    Usb_enable_suspend_interrupt();
00149    Usb_enable_reset_interrupt();
00150    Enable_interrupt();
00151 }
00152 
00163 void usb_device_task(void)
00164 {
00165    //- VBUS state detection
00166    if (Is_usb_vbus_high()&& (usb_connected==FALSE))
00167    {
00168       usb_connected = TRUE;
00169       Usb_send_event(EVT_USB_POWERED);
00170       Usb_vbus_on_action();
00171       Usb_enable();
00172       usb_start_device();
00173       Usb_attach();
00174    }
00175    if (Is_usb_vbus_low()&& (usb_connected==TRUE))
00176    {
00177       usb_connected = FALSE;
00178       usb_configuration_nb = 0;
00179       Usb_send_event(EVT_USB_UNPOWERED);
00180       Usb_detach();
00181       Usb_freeze_clock();
00182       Usb_vbus_off_action();
00183    }
00184 
00185    if(Is_usb_event(EVT_USB_RESET))
00186    {
00187       Usb_ack_event(EVT_USB_RESET);
00188       Usb_reset_endpoint(0);
00189       usb_configuration_nb=0;
00190    }
00191 
00192 #ifdef USB_WAKEUP_POOLING
00193   // - Wake up event (USB activity detected): Used to resume
00194    if (Is_usb_wake_up() && (Is_pll_ready()==FALSE))
00195    {
00196       Pll_start_auto();
00197       Wait_pll_ready();
00198       Usb_unfreeze_clock();
00199       Usb_ack_wake_up();
00200       Usb_wake_up_action();
00201       Usb_send_event(EVT_USB_WAKE_UP);
00202       Usb_enable_suspend_interrupt();
00203    }
00204 #endif
00205 
00206    // =======================================
00207    // Common Standard Device Control Requests
00208    // =======================================
00209    //   - device enumeration process
00210    //   - device control commands and features
00211    Usb_select_endpoint(EP_CONTROL);
00212    if (Is_usb_receive_setup())
00213    {
00214       usb_process_request();
00215    }
00216 }
00217 
00218 
00219 #ifdef USE_USB_AUTOBAUD
00220 #warning CAUTION Preliminary USB autobaud for USB DFU bootloader Only... 
00221 
00222 
00223 
00224 
00225 
00226 
00227 
00228 
00229 
00230 
00231 
00232 
00233 void usb_autobaud(void)
00234 {
00235 
00236    U16 count_rc=0;
00237 
00238    volatile U16 tempo;
00239    
00240    wdtdrv_interrupt_enable(WDTO_16MS);
00241    TCCR1B=0x00; TCCR1A=0x00;
00242    TCNT1=0x00;  TIFR1=0x01;            
00243    
00244    TCCR1B|=(1<<CS01) |(1<<CS00);       // ClkIO/64, with prescaler /2 -> XTAL/128
00245 
00246    while(Is_not_wdt_interrupt());
00247    Wdt_ack_interrupt();
00248    TCCR1B=0;
00249    wdtdrv_disable();
00250 
00251    
00252    count_rc=TCNT1;
00253    TCCR1B=0x00; TCCR1A=0x00;
00254    TCNT1=0x00;  TIFR1=0x01;            
00255    if(count_rc>1500)                   // 16MHz/128 with 16ms watchdog gives 2000 ticks
00256    {
00257       Start_pll(PLL_IN_PRESCAL_ENABLE);               
00258    }
00259    else
00260    {
00261       Start_pll(PLL_IN_PRESCAL_DISABLE);                
00262    }
00263    
00264 }
00265 #endif
00266 

Generated on Tue Nov 8 2011 16:29:45 for ATMEL by  doxygen 1.7.2