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 (VBUS_SENSING_IO == ENABLED) 00111 Usb_vbus_sense_init(); // init. the I/O used for Vbus sensing 00112 #endif 00113 } 00114 00128 void usb_start_device (void) 00129 { 00130 Usb_freeze_clock(); 00131 #ifndef USE_USB_AUTOBAUD 00132 Pll_start_auto(); 00133 #else 00134 usb_autobaud(); 00135 #endif 00136 Wait_pll_ready(); 00137 Disable_interrupt(); 00138 Usb_unfreeze_clock(); 00139 Usb_attach(); 00140 #if (USB_RESET_CPU == ENABLED) 00141 Usb_reset_all_system(); 00142 #else 00143 Usb_reset_macro_only(); 00144 #endif 00145 usb_init_device(); // configure the USB controller EP0 00146 Usb_enable_suspend_interrupt(); 00147 Usb_enable_reset_interrupt(); 00148 Enable_interrupt(); 00149 } 00150 00161 void usb_device_task(void) 00162 { 00163 if (usb_connected == FALSE) 00164 { 00165 #if (VBUS_SENSING_IO == ENABLED) 00166 if (Is_usb_vbus_on()) // check if Vbus ON to attach 00167 { 00168 Usb_enable(); 00169 usb_connected = TRUE; 00170 usb_start_device(); 00171 Usb_vbus_on_action(); 00172 } 00173 #else 00174 usb_connected = TRUE; // attach if application is not self-powered 00175 usb_start_device(); 00176 Usb_vbus_on_action(); 00177 #endif 00178 } 00179 00180 #if (VBUS_SENSING_IO == ENABLED) 00181 if ((usb_connected == TRUE) && Is_usb_vbus_off()) // check if Vbus OFF to detach 00182 { 00183 Usb_detach(); 00184 Usb_disable(); 00185 Stop_pll(); 00186 usb_connected = FALSE; 00187 usb_configuration_nb=0; 00188 } 00189 #endif 00190 00191 if(Is_usb_event(EVT_USB_RESET)) 00192 { 00193 Usb_ack_event(EVT_USB_RESET); 00194 Usb_reset_endpoint(0); 00195 usb_configuration_nb=0; 00196 } 00197 00198 // Here connection to the device enumeration process 00199 Usb_select_endpoint(EP_CONTROL); 00200 if (Is_usb_receive_setup()) 00201 { 00202 usb_process_request(); 00203 } 00204 } 00205 00206 00207 #ifdef USE_USB_AUTOBAUD 00208 #warning CAUTION Preliminary USB autobaud for USB DFU bootloader Only... 00221 void usb_autobaud(void) 00222 { 00223 00224 U16 count_rc=0; 00225 00226 volatile U16 tempo; 00227 00228 wdtdrv_interrupt_enable(WDTO_16MS); 00229 TCCR1B=0x00; TCCR1A=0x00; 00230 TCNT1=0x00; TIFR1=0x01; 00231 00232 TCCR1B|=(1<<CS01) |(1<<CS00); // ClkIO/64, with prescaler /2 -> XTAL/128 00233 WDTCKD|=(1<<WDEWIE); 00234 while(Is_not_wdt_early_warning()); 00235 TCCR1B=0; 00236 wdtdrv_disable(); 00237 WDTCKD|=(1<<WDEWIF); // Clear early warning flag 00238 WDTCKD=0; // Clear early warning flag 00239 00240 count_rc=TCNT1; 00241 TCCR1B=0x00; TCCR1A=0x00; 00242 TCNT1=0x00; TIFR1=0x01; 00243 if(count_rc>1500) // 16MHz/128 with 16ms watchdog gives 2000 ticks 00244 { 00245 Start_pll(PLLx03); 00246 } 00247 else 00248 { 00249 Start_pll(PLLx06); 00250 } 00251 00252 } 00253 #endif 00254