00001 /*This file has been prepared for Doxygen automatic documentation generation.*/ 00018 00019 /* Copyright (c) 2007, 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 * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED 00035 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00036 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND 00037 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, 00038 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00039 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00040 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00041 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00042 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00043 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00044 */ 00045 00046 //_____ I N C L U D E S ___________________________________________________ 00047 00048 #include "config.h" 00049 #include "conf_usb.h" 00050 #include "usb_device_task.h" 00051 #include "modules/usb/usb_task.h" 00052 #include "lib_mcu/usb/usb_drv.h" 00053 #include "usb_descriptors.h" 00054 #include "modules/usb/device_chap9/usb_standard_request.h" 00055 #include "lib_mcu/pll/pll_drv.h" 00056 #ifdef USE_USB_AUTOBAUD 00057 #include "lib_mcu/wdt/wdt_drv.h" 00058 #endif 00059 00060 //_____ M A C R O S ________________________________________________________ 00061 00062 //_____ D E F I N I T I O N S ______________________________________________ 00063 00069 bit usb_connected=FALSE; 00070 00076 bit usb_suspended=FALSE; 00077 00078 00079 00080 00087 extern U8 usb_configuration_nb; 00088 00089 //_____ D E C L A R A T I O N S ____________________________________________ 00090 00103 void usb_device_task_init(void) 00104 { 00105 Usb_disable(); 00106 Usb_enable(); 00107 #if (VBUS_SENSING_IO == ENABLED) 00108 Usb_vbus_sense_init(); // init. the I/O used for Vbus sensing 00109 #endif 00110 } 00111 00125 void usb_start_device (void) 00126 { 00127 Usb_freeze_clock(); 00128 #ifndef USE_USB_AUTOBAUD 00129 Pll_start_auto(); 00130 #else 00131 usb_autobaud(); 00132 #endif 00133 Wait_pll_ready(); 00134 Usb_unfreeze_clock(); 00135 Usb_attach(); 00136 #if (USB_RESET_CPU == ENABLED) 00137 Usb_reset_all_system(); 00138 #else 00139 Usb_reset_macro_only(); 00140 #endif 00141 usb_init_device(); // configure the USB controller EP0 00142 Usb_enable_suspend_interrupt(); 00143 Usb_enable_reset_interrupt(); 00144 Enable_interrupt(); 00145 } 00146 00157 void usb_device_task(void) 00158 { 00159 if (usb_connected == FALSE) 00160 { 00161 #if (VBUS_SENSING_IO == ENABLED) 00162 if (Is_usb_vbus_on()) // check if Vbus ON to attach 00163 { 00164 Usb_enable(); 00165 usb_connected = TRUE; 00166 usb_start_device(); 00167 Usb_vbus_on_action(); 00168 } 00169 #else 00170 usb_connected = TRUE; // attach if application is not self-powered 00171 usb_start_device(); 00172 Usb_vbus_on_action(); 00173 #endif 00174 } 00175 00176 #if (VBUS_SENSING_IO == ENABLED) 00177 if ((usb_connected == TRUE) && Is_usb_vbus_off()) // check if Vbus OFF to detach 00178 { 00179 Usb_detach(); 00180 Usb_disable(); 00181 Stop_pll(); 00182 usb_connected = FALSE; 00183 usb_configuration_nb=0; 00184 } 00185 #endif 00186 00187 if(Is_usb_event(EVT_USB_RESET)) 00188 { 00189 Usb_ack_event(EVT_USB_RESET); 00190 Usb_reset_endpoint(0); 00191 usb_configuration_nb=0; 00192 } 00193 00194 // Here connection to the device enumeration process 00195 Usb_select_endpoint(EP_CONTROL); 00196 if (Is_usb_receive_setup()) 00197 { 00198 usb_process_request(); 00199 } 00200 } 00201 00202 00203 #ifdef USE_USB_AUTOBAUD 00204 #warning CAUTION Preliminary USB autobaud for USB DFU bootloader Only... 00217 void usb_autobaud(void) 00218 { 00219 00220 U16 count_rc=0; 00221 00222 volatile U16 tempo; 00223 00224 wdtdrv_interrupt_enable(WDTO_16MS); 00225 TCCR1B=0x00; TCCR1A=0x00; 00226 TCNT1=0x00; TIFR1=0x01; 00227 00228 TCCR1B|=(1<<CS01) |(1<<CS00); // ClkIO/64, with prescaler /2 -> XTAL/128 00229 WDTCKD|=(1<<WDEWIE); 00230 while(Is_not_wdt_early_warning()); 00231 TCCR1B=0; 00232 wdtdrv_disable(); 00233 WDTCKD|=(1<<WDEWIF); // Clear early warning flag 00234 WDTCKD=0; // Clear early warning flag 00235 00236 count_rc=TCNT1; 00237 TCCR1B=0x00; TCCR1A=0x00; 00238 TCNT1=0x00; TIFR1=0x01; 00239 if(count_rc>1500) // 16MHz/128 with 16ms watchdog gives 2000 ticks 00240 { 00241 Start_pll(PLLx03); 00242 } 00243 else 00244 { 00245 Start_pll(PLLx06); 00246 } 00247 00248 } 00249 #endif 00250