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    // Here connection to the device enumeration process
00193    Usb_select_endpoint(EP_CONTROL);
00194    if (Is_usb_receive_setup())
00195    {
00196       usb_process_request();
00197    }
00198 }
00199 
00200 
00201 #ifdef USE_USB_AUTOBAUD
00202 #warning CAUTION Preliminary USB autobaud for USB DFU bootloader Only... 
00215 void usb_autobaud(void)
00216 {
00217 
00218    U16 count_rc=0;
00219 
00220    volatile U16 tempo;
00221    
00222    wdtdrv_interrupt_enable(WDTO_16MS);
00223    TCCR1B=0x00; TCCR1A=0x00;
00224    TCNT1=0x00;  TIFR1=0x01;            
00225    
00226    TCCR1B|=(1<<CS01) |(1<<CS00);       // ClkIO/64, with prescaler /2 -> XTAL/128
00227 
00228    while(Is_not_wdt_interrupt());
00229    Wdt_ack_interrupt();
00230    TCCR1B=0;
00231    wdtdrv_disable();
00232 
00233    
00234    count_rc=TCNT1;
00235    TCCR1B=0x00; TCCR1A=0x00;
00236    TCNT1=0x00;  TIFR1=0x01;            
00237    if(count_rc>1500)                   // 16MHz/128 with 16ms watchdog gives 2000 ticks
00238    {
00239       Start_pll(PLL_IN_PRESCAL_ENABLE);               
00240    }
00241    else
00242    {
00243       Start_pll(PLL_IN_PRESCAL_DISABLE);                
00244    }
00245    
00246 }
00247 #endif
00248 

Generated on Fri Sep 11 15:02:35 2009 for ATMEL by  doxygen 1.5.3