00001 /*This file is prepared for Doxygen automatic documentation generation.*/ 00020 /* Copyright (c) 2009 Atmel Corporation. All rights reserved. 00021 * 00022 * Redistribution and use in source and binary forms, with or without 00023 * modification, are permitted provided that the following conditions are met: 00024 * 00025 * 1. Redistributions of source code must retain the above copyright notice, this 00026 * list of conditions and the following disclaimer. 00027 * 00028 * 2. Redistributions in binary form must reproduce the above copyright notice, 00029 * this list of conditions and the following disclaimer in the documentation 00030 * and/or other materials provided with the distribution. 00031 * 00032 * 3. The name of Atmel may not be used to endorse or promote products derived 00033 * from this software without specific prior written permission. 00034 * 00035 * 4. This software may only be redistributed and used in connection with an Atmel 00036 * AVR product. 00037 * 00038 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED 00039 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00040 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 00041 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR 00042 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00043 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00044 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00045 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00046 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00047 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE 00048 * 00049 */ 00050 00051 //_____ I N C L U D E S ___________________________________________________ 00052 00053 #include "conf_usb.h" 00054 00055 00056 #if USB_DEVICE_FEATURE == ENABLED 00057 00058 #include "compiler.h" 00059 #ifdef FREERTOS_USED 00060 #include "FreeRTOS.h" 00061 #include "task.h" 00062 #endif 00063 #include "usb_drv.h" 00064 #include "usb_task.h" 00065 #include "usb_descriptors.h" 00066 #include "usb_standard_request.h" 00067 #include "usb_device_task.h" 00068 00069 00070 //_____ M A C R O S ________________________________________________________ 00071 00072 00073 //_____ D E F I N I T I O N S ______________________________________________ 00074 00080 volatile Bool usb_connected; 00081 00082 #ifdef FREERTOS_USED 00084 xTaskHandle usb_device_tsk = NULL; 00085 #endif 00086 00087 00088 //_____ D E C L A R A T I O N S ____________________________________________ 00089 00097 void usb_device_task_init(void) 00098 { 00099 usb_connected = FALSE; 00100 usb_configuration_nb = 0; 00102 //Pll_start_auto(); 00103 //Wait_pll_ready(); 00104 Disable_global_interrupt(); 00105 Usb_disable(); 00106 (void)Is_usb_enabled(); 00107 Enable_global_interrupt(); 00108 Usb_disable_otg_pad(); 00109 Usb_enable_otg_pad(); 00110 Usb_enable(); 00111 Usb_unfreeze_clock(); 00112 (void)Is_usb_clock_frozen(); 00113 Usb_ack_suspend(); // A suspend condition may be detected right after enabling the USB macro 00114 Usb_enable_vbus_interrupt(); 00115 Enable_global_interrupt(); 00116 00117 #ifdef FREERTOS_USED 00118 xTaskCreate(usb_device_task, 00119 configTSK_USB_DEV_NAME, 00120 configTSK_USB_DEV_STACK_SIZE, 00121 NULL, 00122 configTSK_USB_DEV_PRIORITY, 00123 &usb_device_tsk); 00124 #endif // FREERTOS_USED 00125 } 00126 00127 00137 void usb_start_device(void) 00138 { 00139 Usb_enable_suspend_interrupt(); 00140 Usb_enable_reset_interrupt(); 00141 00142 #if (USB_HIGH_SPEED_SUPPORT==DISABLED) 00143 Usb_force_full_speed_mode(); 00144 #else 00145 Usb_use_dual_speed_mode(); 00146 #endif 00147 00148 usb_init_device(); // Configure the USB controller EP0 00149 Usb_attach(); 00150 usb_connected = TRUE; 00151 } 00152 00153 00162 #ifdef FREERTOS_USED 00163 void usb_device_task(void *pvParameters) 00164 #else 00165 void usb_device_task(void) 00166 #endif 00167 { 00168 #ifdef FREERTOS_USED 00169 portTickType xLastWakeTime; 00170 00171 xLastWakeTime = xTaskGetTickCount(); 00172 while (TRUE) 00173 { 00174 vTaskDelayUntil(&xLastWakeTime, configTSK_USB_DEV_PERIOD); 00175 00176 #endif // FREERTOS_USED 00177 if (!usb_connected && Is_usb_vbus_high()) 00178 { 00179 usb_start_device(); 00180 Usb_send_event(EVT_USB_POWERED); 00181 Usb_vbus_on_action(); 00182 } 00183 00184 if (Is_usb_event(EVT_USB_RESET)) 00185 { 00186 Usb_ack_event(EVT_USB_RESET); 00187 Usb_reset_endpoint(EP_CONTROL); 00188 usb_configuration_nb = 0; 00189 } 00190 00191 // Connection to the device enumeration process 00192 if (Is_usb_setup_received()) 00193 { 00194 usb_process_request(); 00195 } 00196 #ifdef FREERTOS_USED 00197 } 00198 #endif 00199 } 00200 00201 00202 #endif // USB_DEVICE_FEATURE == ENABLED